2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
127 struct commit_queue_head head;
131 STAILQ_ENTRY(tog_color) entry;
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
155 } else if (isbackup1 && !isbackup2) {
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 STAILQ_INSERT_HEAD(colors, tc, entry);
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
296 if (strcasecmp(val, "red") == 0)
298 if (strcasecmp(val, "green") == 0)
300 if (strcasecmp(val, "yellow") == 0)
302 if (strcasecmp(val, "blue") == 0)
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
308 if (strcasecmp(val, "white") == 0)
310 if (strcasecmp(val, "default") == 0)
313 return default_color_value(envvar);
317 struct tog_diff_view_state {
318 struct got_object_id *id1, *id2;
319 const char *label1, *label2;
322 int first_displayed_line;
323 int last_displayed_line;
326 int ignore_whitespace;
328 struct got_repository *repo;
329 struct tog_colors colors;
335 /* passed from log view; may be NULL */
336 struct tog_view *log_view;
339 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
341 struct tog_log_thread_args {
342 pthread_cond_t need_commits;
343 pthread_cond_t commit_loaded;
346 struct got_commit_graph *graph;
347 struct commit_queue *commits;
348 const char *in_repo_path;
349 struct got_object_id *start_id;
350 struct got_repository *repo;
354 struct commit_queue_entry **first_displayed_entry;
355 struct commit_queue_entry **selected_entry;
357 int *search_next_done;
361 struct tog_log_view_state {
362 struct commit_queue commits;
363 struct commit_queue_entry *first_displayed_entry;
364 struct commit_queue_entry *last_displayed_entry;
365 struct commit_queue_entry *selected_entry;
370 struct got_repository *repo;
371 struct got_object_id *start_id;
374 struct tog_log_thread_args thread_args;
375 struct commit_queue_entry *matched_entry;
376 struct commit_queue_entry *search_entry;
377 struct tog_colors colors;
380 #define TOG_COLOR_DIFF_MINUS 1
381 #define TOG_COLOR_DIFF_PLUS 2
382 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
383 #define TOG_COLOR_DIFF_META 4
384 #define TOG_COLOR_TREE_SUBMODULE 5
385 #define TOG_COLOR_TREE_SYMLINK 6
386 #define TOG_COLOR_TREE_DIRECTORY 7
387 #define TOG_COLOR_TREE_EXECUTABLE 8
388 #define TOG_COLOR_COMMIT 9
389 #define TOG_COLOR_AUTHOR 10
390 #define TOG_COLOR_DATE 11
391 #define TOG_COLOR_REFS_HEADS 12
392 #define TOG_COLOR_REFS_TAGS 13
393 #define TOG_COLOR_REFS_REMOTES 14
394 #define TOG_COLOR_REFS_BACKUP 15
396 struct tog_blame_cb_args {
397 struct tog_blame_line *lines; /* one per line */
400 struct tog_view *view;
401 struct got_object_id *commit_id;
405 struct tog_blame_thread_args {
407 struct got_repository *repo;
408 struct tog_blame_cb_args *cb_args;
410 got_cancel_cb cancel_cb;
417 struct tog_blame_line *lines;
421 struct tog_blame_thread_args thread_args;
422 struct tog_blame_cb_args cb_args;
427 struct tog_blame_view_state {
428 int first_displayed_line;
429 int last_displayed_line;
434 struct got_object_id_queue blamed_commits;
435 struct got_object_qid *blamed_commit;
437 struct got_repository *repo;
438 struct got_object_id *commit_id;
439 struct tog_blame blame;
441 struct tog_colors colors;
444 struct tog_parent_tree {
445 TAILQ_ENTRY(tog_parent_tree) entry;
446 struct got_tree_object *tree;
447 struct got_tree_entry *first_displayed_entry;
448 struct got_tree_entry *selected_entry;
452 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
454 struct tog_tree_view_state {
456 struct got_object_id *commit_id;/* commit which this tree belongs to */
457 struct got_tree_object *root; /* the commit's root tree entry */
458 struct got_tree_object *tree; /* currently displayed (sub-)tree */
459 struct got_tree_entry *first_displayed_entry;
460 struct got_tree_entry *last_displayed_entry;
461 struct got_tree_entry *selected_entry;
462 int ndisplayed, selected, show_ids;
463 struct tog_parent_trees parents; /* parent trees of current sub-tree */
465 struct got_repository *repo;
466 struct got_tree_entry *matched_entry;
467 struct tog_colors colors;
470 struct tog_reflist_entry {
471 TAILQ_ENTRY(tog_reflist_entry) entry;
472 struct got_reference *ref;
476 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
478 struct tog_ref_view_state {
479 struct tog_reflist_head refs;
480 struct tog_reflist_entry *first_displayed_entry;
481 struct tog_reflist_entry *last_displayed_entry;
482 struct tog_reflist_entry *selected_entry;
483 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
484 struct got_repository *repo;
485 struct tog_reflist_entry *matched_entry;
486 struct tog_colors colors;
490 * We implement two types of views: parent views and child views.
492 * The 'Tab' key switches focus between a parent view and its child view.
493 * Child views are shown side-by-side to their parent view, provided
494 * there is enough screen estate.
496 * When a new view is opened from within a parent view, this new view
497 * becomes a child view of the parent view, replacing any existing child.
499 * When a new view is opened from within a child view, this new view
500 * becomes a parent view which will obscure the views below until the
501 * user quits the new parent view by typing 'q'.
503 * This list of views contains parent views only.
504 * Child views are only pointed to by their parent view.
506 TAILQ_HEAD(tog_view_list_head, tog_view);
509 TAILQ_ENTRY(tog_view) entry;
512 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
513 int maxx, x; /* max column and current start column */
514 int lines, cols; /* copies of LINES and COLS */
515 int nscrolled, offset; /* lines scrolled and hsplit line offset */
516 int ch, count; /* current keymap and count prefix */
517 int focussed; /* Only set on one parent or child view at a time. */
519 struct tog_view *parent;
520 struct tog_view *child;
523 * This flag is initially set on parent views when a new child view
524 * is created. It gets toggled when the 'Tab' key switches focus
525 * between parent and child.
526 * The flag indicates whether focus should be passed on to our child
527 * view if this parent view gets picked for focus after another parent
528 * view was closed. This prevents child views from losing focus in such
533 enum tog_view_mode mode;
534 /* type-specific state */
535 enum tog_view_type type;
537 struct tog_diff_view_state diff;
538 struct tog_log_view_state log;
539 struct tog_blame_view_state blame;
540 struct tog_tree_view_state tree;
541 struct tog_ref_view_state ref;
544 const struct got_error *(*show)(struct tog_view *);
545 const struct got_error *(*input)(struct tog_view **,
546 struct tog_view *, int);
547 const struct got_error *(*reset)(struct tog_view *);
548 const struct got_error *(*close)(struct tog_view *);
550 const struct got_error *(*search_start)(struct tog_view *);
551 const struct got_error *(*search_next)(struct tog_view *);
554 #define TOG_SEARCH_FORWARD 1
555 #define TOG_SEARCH_BACKWARD 2
556 int search_next_done;
557 #define TOG_SEARCH_HAVE_MORE 1
558 #define TOG_SEARCH_NO_MORE 2
559 #define TOG_SEARCH_HAVE_NONE 3
564 static const struct got_error *open_diff_view(struct tog_view *,
565 struct got_object_id *, struct got_object_id *,
566 const char *, const char *, int, int, int, struct tog_view *,
567 struct got_repository *);
568 static const struct got_error *show_diff_view(struct tog_view *);
569 static const struct got_error *input_diff_view(struct tog_view **,
570 struct tog_view *, int);
571 static const struct got_error *reset_diff_view(struct tog_view *);
572 static const struct got_error* close_diff_view(struct tog_view *);
573 static const struct got_error *search_start_diff_view(struct tog_view *);
574 static const struct got_error *search_next_diff_view(struct tog_view *);
576 static const struct got_error *open_log_view(struct tog_view *,
577 struct got_object_id *, struct got_repository *,
578 const char *, const char *, int);
579 static const struct got_error * show_log_view(struct tog_view *);
580 static const struct got_error *input_log_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *close_log_view(struct tog_view *);
583 static const struct got_error *search_start_log_view(struct tog_view *);
584 static const struct got_error *search_next_log_view(struct tog_view *);
586 static const struct got_error *open_blame_view(struct tog_view *, char *,
587 struct got_object_id *, struct got_repository *);
588 static const struct got_error *show_blame_view(struct tog_view *);
589 static const struct got_error *input_blame_view(struct tog_view **,
590 struct tog_view *, int);
591 static const struct got_error *reset_blame_view(struct tog_view *);
592 static const struct got_error *close_blame_view(struct tog_view *);
593 static const struct got_error *search_start_blame_view(struct tog_view *);
594 static const struct got_error *search_next_blame_view(struct tog_view *);
596 static const struct got_error *open_tree_view(struct tog_view *,
597 struct got_object_id *, const char *, struct got_repository *);
598 static const struct got_error *show_tree_view(struct tog_view *);
599 static const struct got_error *input_tree_view(struct tog_view **,
600 struct tog_view *, int);
601 static const struct got_error *close_tree_view(struct tog_view *);
602 static const struct got_error *search_start_tree_view(struct tog_view *);
603 static const struct got_error *search_next_tree_view(struct tog_view *);
605 static const struct got_error *open_ref_view(struct tog_view *,
606 struct got_repository *);
607 static const struct got_error *show_ref_view(struct tog_view *);
608 static const struct got_error *input_ref_view(struct tog_view **,
609 struct tog_view *, int);
610 static const struct got_error *close_ref_view(struct tog_view *);
611 static const struct got_error *search_start_ref_view(struct tog_view *);
612 static const struct got_error *search_next_ref_view(struct tog_view *);
614 static volatile sig_atomic_t tog_sigwinch_received;
615 static volatile sig_atomic_t tog_sigpipe_received;
616 static volatile sig_atomic_t tog_sigcont_received;
617 static volatile sig_atomic_t tog_sigint_received;
618 static volatile sig_atomic_t tog_sigterm_received;
621 tog_sigwinch(int signo)
623 tog_sigwinch_received = 1;
627 tog_sigpipe(int signo)
629 tog_sigpipe_received = 1;
633 tog_sigcont(int signo)
635 tog_sigcont_received = 1;
639 tog_sigint(int signo)
641 tog_sigint_received = 1;
645 tog_sigterm(int signo)
647 tog_sigterm_received = 1;
651 tog_fatal_signal_received(void)
653 return (tog_sigpipe_received ||
654 tog_sigint_received || tog_sigint_received);
657 static const struct got_error *
658 view_close(struct tog_view *view)
660 const struct got_error *err = NULL;
663 view_close(view->child);
667 err = view->close(view);
669 del_panel(view->panel);
671 delwin(view->window);
676 static struct tog_view *
677 view_open(int nlines, int ncols, int begin_y, int begin_x,
678 enum tog_view_type type)
680 struct tog_view *view = calloc(1, sizeof(*view));
688 view->nlines = nlines ? nlines : LINES - begin_y;
689 view->ncols = ncols ? ncols : COLS - begin_x;
690 view->begin_y = begin_y;
691 view->begin_x = begin_x;
692 view->window = newwin(nlines, ncols, begin_y, begin_x);
693 if (view->window == NULL) {
697 view->panel = new_panel(view->window);
698 if (view->panel == NULL ||
699 set_panel_userptr(view->panel, view) != OK) {
704 keypad(view->window, TRUE);
709 view_split_begin_x(int begin_x)
711 if (begin_x > 0 || COLS < 120)
713 return (COLS - MAX(COLS / 2, 80));
716 /* XXX Stub till we decide what to do. */
718 view_split_begin_y(int lines)
720 return lines * HSPLIT_SCALE;
723 static const struct got_error *view_resize(struct tog_view *);
725 static const struct got_error *
726 view_splitscreen(struct tog_view *view)
728 const struct got_error *err = NULL;
730 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
731 view->begin_y = view_split_begin_y(view->nlines);
734 view->begin_x = view_split_begin_x(0);
737 view->nlines = LINES - view->begin_y;
738 view->ncols = COLS - view->begin_x;
741 err = view_resize(view);
745 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
746 view->parent->nlines = view->begin_y;
748 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
749 return got_error_from_errno("mvwin");
754 static const struct got_error *
755 view_fullscreen(struct tog_view *view)
757 const struct got_error *err = NULL;
761 view->nlines = LINES;
765 err = view_resize(view);
769 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
770 return got_error_from_errno("mvwin");
776 view_is_parent_view(struct tog_view *view)
778 return view->parent == NULL;
782 view_is_splitscreen(struct tog_view *view)
784 return view->begin_x > 0 || view->begin_y > 0;
788 view_is_fullscreen(struct tog_view *view)
790 return view->nlines == LINES && view->ncols == COLS;
794 view_border(struct tog_view *view)
797 const struct tog_view *view_above;
800 return view_border(view->parent);
802 panel = panel_above(view->panel);
806 view_above = panel_userptr(panel);
807 if (view->mode == TOG_VIEW_SPLIT_HRZN)
808 mvwhline(view->window, view_above->begin_y - 1,
809 view->begin_x, got_locale_is_utf8() ?
810 ACS_HLINE : '-', view->ncols);
812 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
813 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
816 static const struct got_error *request_log_commits(struct tog_view *);
817 static const struct got_error *offset_selection_down(struct tog_view *);
818 static void offset_selection_up(struct tog_view *);
820 static const struct got_error *
821 view_resize(struct tog_view *view)
823 const struct got_error *err = NULL;
824 int dif, nlines, ncols;
826 dif = LINES - view->lines; /* line difference */
828 if (view->lines > LINES)
829 nlines = view->nlines - (view->lines - LINES);
831 nlines = view->nlines + (LINES - view->lines);
832 if (view->cols > COLS)
833 ncols = view->ncols - (view->cols - COLS);
835 ncols = view->ncols + (COLS - view->cols);
838 int hs = view->child->begin_y;
840 if (!view_is_fullscreen(view))
841 view->child->begin_x = view_split_begin_x(view->begin_x);
842 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
843 view->child->begin_x == 0) {
846 view_fullscreen(view->child);
847 if (view->child->focussed)
848 show_panel(view->child->panel);
850 show_panel(view->panel);
852 ncols = view->child->begin_x;
854 view_splitscreen(view->child);
855 show_panel(view->child->panel);
858 * Request commits if terminal height was increased in a log
859 * view so we have enough commits loaded to populate the view.
861 if (view->type == TOG_VIEW_LOG && dif > 0) {
862 struct tog_log_view_state *ts = &view->state.log;
864 if (ts->commits.ncommits < ts->selected_entry->idx +
865 view->lines - ts->selected) {
866 view->nscrolled = ts->selected_entry->idx +
867 view->lines - ts->selected -
868 ts->commits.ncommits + dif;
869 err = request_log_commits(view);
876 * XXX This is ugly and needs to be moved into the above
877 * logic but "works" for now and my attempts at moving it
878 * break either 'tab' or 'F' key maps in horizontal splits.
881 err = view_splitscreen(view->child);
884 if (dif < 0) { /* top split decreased */
885 err = offset_selection_down(view);
892 show_panel(view->child->panel);
893 nlines = view->nlines;
895 } else if (view->parent == NULL)
898 if (wresize(view->window, nlines, ncols) == ERR)
899 return got_error_from_errno("wresize");
900 if (replace_panel(view->panel, view->window) == ERR)
901 return got_error_from_errno("replace_panel");
902 wclear(view->window);
904 view->nlines = nlines;
912 static const struct got_error *
913 view_close_child(struct tog_view *view)
915 const struct got_error *err = NULL;
917 if (view->child == NULL)
920 err = view_close(view->child);
925 static const struct got_error *
926 view_set_child(struct tog_view *view, struct tog_view *child)
929 child->parent = view;
931 return view_resize(view);
940 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
941 cols = 80; /* Default */
947 resize_term(lines, cols);
950 static const struct got_error *
951 view_search_start(struct tog_view *view)
953 const struct got_error *err = NULL;
954 struct tog_view *v = view;
958 if (view->search_started) {
959 regfree(&view->regex);
961 memset(&view->regmatch, 0, sizeof(view->regmatch));
963 view->search_started = 0;
965 if (view->nlines < 1)
968 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
969 view_is_splitscreen(view->child))
972 mvwaddstr(v->window, v->nlines - 1, 0, "/");
973 wclrtoeol(v->window);
977 ret = wgetnstr(v->window, pattern, sizeof(pattern));
984 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
985 err = view->search_start(view);
987 regfree(&view->regex);
990 view->search_started = 1;
991 view->searching = TOG_SEARCH_FORWARD;
992 view->search_next_done = 0;
993 view->search_next(view);
1000 * Compute view->count from numeric user input. User has five-tenths of a
1001 * second to follow each numeric keypress with another number to form count.
1002 * Return first non-numeric input or ERR and assign total to view->count.
1003 * XXX Should we add support for user-defined timeout?
1006 get_compound_key(struct tog_view *view, int c)
1008 struct tog_view *v = view;
1011 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1012 view_is_splitscreen(view->child))
1014 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1018 halfdelay(5); /* block for half a second */
1019 wattron(v->window, A_BOLD);
1020 wmove(v->window, v->nlines - 1, 0);
1021 wclrtoeol(v->window);
1022 waddch(v->window, ':');
1025 x = getcurx(v->window);
1026 if (x != ERR && x < view->ncols) {
1027 waddch(v->window, c);
1028 wrefresh(v->window);
1032 * Don't overflow. Max valid request should be the greatest
1033 * between the longest and total lines; cap at 10 million.
1038 n = n * 10 + (c - '0');
1039 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1041 /* Massage excessive or inapplicable values at the input handler. */
1044 wattroff(v->window, A_BOLD);
1045 cbreak(); /* return to blocking */
1049 static const struct got_error *
1050 view_input(struct tog_view **new, int *done, struct tog_view *view,
1051 struct tog_view_list_head *views)
1053 const struct got_error *err = NULL;
1059 /* Clear "no matches" indicator. */
1060 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1061 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1062 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1066 if (view->searching && !view->search_next_done) {
1067 errcode = pthread_mutex_unlock(&tog_mutex);
1069 return got_error_set_errno(errcode,
1070 "pthread_mutex_unlock");
1072 errcode = pthread_mutex_lock(&tog_mutex);
1074 return got_error_set_errno(errcode,
1075 "pthread_mutex_lock");
1076 view->search_next(view);
1080 nodelay(stdscr, FALSE);
1081 /* Allow threads to make progress while we are waiting for input. */
1082 errcode = pthread_mutex_unlock(&tog_mutex);
1084 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1085 /* If we have an unfinished count, don't get a new key map. */
1087 if ((view->count && --view->count == 0) || !view->count) {
1088 ch = wgetch(view->window);
1089 if (ch >= '1' && ch <= '9')
1090 view->ch = ch = get_compound_key(view, ch);
1092 errcode = pthread_mutex_lock(&tog_mutex);
1094 return got_error_set_errno(errcode, "pthread_mutex_lock");
1095 nodelay(stdscr, TRUE);
1097 if (tog_sigwinch_received || tog_sigcont_received) {
1099 tog_sigwinch_received = 0;
1100 tog_sigcont_received = 0;
1101 TAILQ_FOREACH(v, views, entry) {
1102 err = view_resize(v);
1105 err = v->input(new, v, KEY_RESIZE);
1109 err = view_resize(v->child);
1112 err = v->child->input(new, v->child,
1125 view->child->focussed = 1;
1126 view->focus_child = 1;
1127 } else if (view->parent) {
1129 view->parent->focussed = 1;
1130 view->parent->focus_child = 0;
1131 if (!view_is_splitscreen(view)) {
1132 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1133 view->parent->type == TOG_VIEW_LOG) {
1134 err = request_log_commits(view->parent);
1138 offset_selection_up(view->parent);
1139 err = view_fullscreen(view->parent);
1146 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1147 if (view->parent->type == TOG_VIEW_LOG) {
1148 /* might need more commits to fill fullscreen */
1149 err = request_log_commits(view->parent);
1153 offset_selection_up(view->parent);
1154 view->parent->mode = TOG_VIEW_SPLIT_NONE;
1156 err = view->input(new, view, ch);
1164 if (view_is_parent_view(view)) {
1165 if (view->child == NULL)
1167 if (view_is_splitscreen(view->child)) {
1169 view->child->focussed = 1;
1170 err = view_fullscreen(view->child);
1172 err = view_splitscreen(view->child);
1175 err = view->child->input(new, view->child,
1178 if (view_is_splitscreen(view)) {
1179 view->parent->focussed = 0;
1181 err = view_fullscreen(view);
1183 err = view_splitscreen(view);
1184 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1185 err = view_resize(view->parent);
1189 err = view->input(new, view, KEY_RESIZE);
1193 if (view->type == TOG_VIEW_LOG) {
1194 err = request_log_commits(view);
1199 err = offset_selection_down(view->parent);
1201 err = offset_selection_down(view);
1207 if (view->search_start)
1208 view_search_start(view);
1210 err = view->input(new, view, ch);
1214 if (view->search_started && view->search_next) {
1215 view->searching = (ch == 'n' ?
1216 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1217 view->search_next_done = 0;
1218 view->search_next(view);
1220 err = view->input(new, view, ch);
1223 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1224 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1226 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1227 TAILQ_FOREACH(v, views, entry) {
1233 if (v->child && v->child->reset) {
1234 err = v->child->reset(v->child);
1241 err = view->input(new, view, ch);
1249 view_needs_focus_indication(struct tog_view *view)
1251 if (view_is_parent_view(view)) {
1252 if (view->child == NULL || view->child->focussed)
1254 if (!view_is_splitscreen(view->child))
1256 } else if (!view_is_splitscreen(view))
1259 return view->focussed;
1262 static const struct got_error *
1263 view_loop(struct tog_view *view)
1265 const struct got_error *err = NULL;
1266 struct tog_view_list_head views;
1267 struct tog_view *new_view;
1268 int fast_refresh = 10;
1269 int done = 0, errcode;
1271 errcode = pthread_mutex_lock(&tog_mutex);
1273 return got_error_set_errno(errcode, "pthread_mutex_lock");
1276 TAILQ_INSERT_HEAD(&views, view, entry);
1279 err = view->show(view);
1284 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1285 /* Refresh fast during initialization, then become slower. */
1286 if (fast_refresh && fast_refresh-- == 0)
1287 halfdelay(10); /* switch to once per second */
1289 err = view_input(&new_view, &done, view, &views);
1293 struct tog_view *v, *prev = NULL;
1295 if (view_is_parent_view(view))
1296 prev = TAILQ_PREV(view, tog_view_list_head,
1298 else if (view->parent)
1299 prev = view->parent;
1302 view->parent->child = NULL;
1303 view->parent->focus_child = 0;
1304 /* Restore fullscreen line height. */
1305 view->parent->nlines = view->parent->lines;
1306 err = view_resize(view->parent);
1310 TAILQ_REMOVE(&views, view, entry);
1312 err = view_close(view);
1317 TAILQ_FOREACH(v, &views, entry) {
1321 if (view == NULL && new_view == NULL) {
1322 /* No view has focus. Try to pick one. */
1325 else if (!TAILQ_EMPTY(&views)) {
1326 view = TAILQ_LAST(&views,
1327 tog_view_list_head);
1330 if (view->focus_child) {
1331 view->child->focussed = 1;
1339 struct tog_view *v, *t;
1340 /* Only allow one parent view per type. */
1341 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1342 if (v->type != new_view->type)
1344 TAILQ_REMOVE(&views, v, entry);
1345 err = view_close(v);
1350 TAILQ_INSERT_TAIL(&views, new_view, entry);
1354 if (view_is_parent_view(view)) {
1355 if (view->child && view->child->focussed)
1358 if (view->parent && view->parent->focussed)
1359 view = view->parent;
1361 show_panel(view->panel);
1362 if (view->child && view_is_splitscreen(view->child))
1363 show_panel(view->child->panel);
1364 if (view->parent && view_is_splitscreen(view)) {
1365 err = view->parent->show(view->parent);
1369 err = view->show(view);
1373 err = view->child->show(view->child);
1382 while (!TAILQ_EMPTY(&views)) {
1383 view = TAILQ_FIRST(&views);
1384 TAILQ_REMOVE(&views, view, entry);
1388 errcode = pthread_mutex_unlock(&tog_mutex);
1389 if (errcode && err == NULL)
1390 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1400 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1405 /* Create newly allocated wide-character string equivalent to a byte string. */
1406 static const struct got_error *
1407 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1410 const struct got_error *err = NULL;
1413 *wlen = mbstowcs(NULL, s, 0);
1414 if (*wlen == (size_t)-1) {
1416 if (errno != EILSEQ)
1417 return got_error_from_errno("mbstowcs");
1419 /* byte string invalid in current encoding; try to "fix" it */
1420 err = got_mbsavis(&vis, &vislen, s);
1423 *wlen = mbstowcs(NULL, vis, 0);
1424 if (*wlen == (size_t)-1) {
1425 err = got_error_from_errno("mbstowcs"); /* give up */
1430 *ws = calloc(*wlen + 1, sizeof(**ws));
1432 err = got_error_from_errno("calloc");
1436 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1437 err = got_error_from_errno("mbstowcs");
1448 static const struct got_error *
1449 expand_tab(char **ptr, const char *src)
1452 size_t len, n, idx = 0, sz = 0;
1455 n = len = strlen(src);
1456 dst = malloc(n + 1);
1458 return got_error_from_errno("malloc");
1460 while (idx < len && src[idx]) {
1461 const char c = src[idx];
1464 size_t nb = TABSIZE - sz % TABSIZE;
1467 p = realloc(dst, n + nb);
1470 return got_error_from_errno("realloc");
1475 memset(dst + sz, ' ', nb);
1478 dst[sz++] = src[idx];
1488 * Advance at most n columns from wline starting at offset off.
1489 * Return the index to the first character after the span operation.
1490 * Return the combined column width of all spanned wide character in
1494 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1496 int width, i, cols = 0;
1503 for (i = off; wline[i] != L'\0'; ++i) {
1504 if (wline[i] == L'\t')
1505 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1507 width = wcwidth(wline[i]);
1514 if (cols + width > n)
1524 * Format a line for display, ensuring that it won't overflow a width limit.
1525 * With scrolling, the width returned refers to the scrolled version of the
1526 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1528 static const struct got_error *
1529 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1530 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1532 const struct got_error *err = NULL;
1534 wchar_t *wline = NULL;
1543 err = expand_tab(&exstr, line);
1548 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1553 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1555 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1556 wline[wlen - 1] = L'\0';
1559 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1560 wline[wlen - 1] = L'\0';
1564 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1570 *scrollxp = scrollx;
1578 static const struct got_error*
1579 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1580 struct got_object_id *id, struct got_repository *repo)
1582 static const struct got_error *err = NULL;
1583 struct got_reflist_entry *re;
1589 TAILQ_FOREACH(re, refs, entry) {
1590 struct got_tag_object *tag = NULL;
1591 struct got_object_id *ref_id;
1594 name = got_ref_get_name(re->ref);
1595 if (strcmp(name, GOT_REF_HEAD) == 0)
1597 if (strncmp(name, "refs/", 5) == 0)
1599 if (strncmp(name, "got/", 4) == 0 &&
1600 strncmp(name, "got/backup/", 11) != 0)
1602 if (strncmp(name, "heads/", 6) == 0)
1604 if (strncmp(name, "remotes/", 8) == 0) {
1606 s = strstr(name, "/" GOT_REF_HEAD);
1607 if (s != NULL && s[strlen(s)] == '\0')
1610 err = got_ref_resolve(&ref_id, repo, re->ref);
1613 if (strncmp(name, "tags/", 5) == 0) {
1614 err = got_object_open_as_tag(&tag, repo, ref_id);
1616 if (err->code != GOT_ERR_OBJ_TYPE) {
1620 /* Ref points at something other than a tag. */
1625 cmp = got_object_id_cmp(tag ?
1626 got_object_tag_get_object_id(tag) : ref_id, id);
1629 got_object_tag_close(tag);
1633 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1634 s ? ", " : "", name) == -1) {
1635 err = got_error_from_errno("asprintf");
1646 static const struct got_error *
1647 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1652 smallerthan = strchr(author, '<');
1653 if (smallerthan && smallerthan[1] != '\0')
1654 author = smallerthan + 1;
1655 author[strcspn(author, "@>")] = '\0';
1656 return format_line(wauthor, author_width, NULL, author, 0, limit,
1660 static const struct got_error *
1661 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1662 struct got_object_id *id, const size_t date_display_cols,
1663 int author_display_cols)
1665 struct tog_log_view_state *s = &view->state.log;
1666 const struct got_error *err = NULL;
1667 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1668 char *logmsg0 = NULL, *logmsg = NULL;
1669 char *author = NULL;
1670 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1671 int author_width, logmsg_width;
1672 char *newline, *line = NULL;
1673 int col, limit, scrollx;
1674 const int avail = view->ncols;
1676 time_t committer_time;
1677 struct tog_color *tc;
1679 committer_time = got_object_commit_get_committer_time(commit);
1680 if (gmtime_r(&committer_time, &tm) == NULL)
1681 return got_error_from_errno("gmtime_r");
1682 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1683 return got_error(GOT_ERR_NO_SPACE);
1685 if (avail <= date_display_cols)
1686 limit = MIN(sizeof(datebuf) - 1, avail);
1688 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1689 tc = get_color(&s->colors, TOG_COLOR_DATE);
1691 wattr_on(view->window,
1692 COLOR_PAIR(tc->colorpair), NULL);
1693 waddnstr(view->window, datebuf, limit);
1695 wattr_off(view->window,
1696 COLOR_PAIR(tc->colorpair), NULL);
1703 err = got_object_id_str(&id_str, id);
1706 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1708 wattr_on(view->window,
1709 COLOR_PAIR(tc->colorpair), NULL);
1710 wprintw(view->window, "%.8s ", id_str);
1712 wattr_off(view->window,
1713 COLOR_PAIR(tc->colorpair), NULL);
1720 author = strdup(got_object_commit_get_author(commit));
1721 if (author == NULL) {
1722 err = got_error_from_errno("strdup");
1725 err = format_author(&wauthor, &author_width, author, avail - col, col);
1728 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1730 wattr_on(view->window,
1731 COLOR_PAIR(tc->colorpair), NULL);
1732 waddwstr(view->window, wauthor);
1734 wattr_off(view->window,
1735 COLOR_PAIR(tc->colorpair), NULL);
1736 col += author_width;
1737 while (col < avail && author_width < author_display_cols + 2) {
1738 waddch(view->window, ' ');
1745 err = got_object_commit_get_logmsg(&logmsg0, commit);
1749 while (*logmsg == '\n')
1751 newline = strchr(logmsg, '\n');
1754 limit = avail - col;
1755 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1756 limit--; /* for the border */
1757 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1761 waddwstr(view->window, &wlogmsg[scrollx]);
1762 col += MAX(logmsg_width, 0);
1763 while (col < avail) {
1764 waddch(view->window, ' ');
1776 static struct commit_queue_entry *
1777 alloc_commit_queue_entry(struct got_commit_object *commit,
1778 struct got_object_id *id)
1780 struct commit_queue_entry *entry;
1782 entry = calloc(1, sizeof(*entry));
1787 entry->commit = commit;
1792 pop_commit(struct commit_queue *commits)
1794 struct commit_queue_entry *entry;
1796 entry = TAILQ_FIRST(&commits->head);
1797 TAILQ_REMOVE(&commits->head, entry, entry);
1798 got_object_commit_close(entry->commit);
1799 commits->ncommits--;
1800 /* Don't free entry->id! It is owned by the commit graph. */
1805 free_commits(struct commit_queue *commits)
1807 while (!TAILQ_EMPTY(&commits->head))
1808 pop_commit(commits);
1811 static const struct got_error *
1812 match_commit(int *have_match, struct got_object_id *id,
1813 struct got_commit_object *commit, regex_t *regex)
1815 const struct got_error *err = NULL;
1816 regmatch_t regmatch;
1817 char *id_str = NULL, *logmsg = NULL;
1821 err = got_object_id_str(&id_str, id);
1825 err = got_object_commit_get_logmsg(&logmsg, commit);
1829 if (regexec(regex, got_object_commit_get_author(commit), 1,
1830 ®match, 0) == 0 ||
1831 regexec(regex, got_object_commit_get_committer(commit), 1,
1832 ®match, 0) == 0 ||
1833 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1834 regexec(regex, logmsg, 1, ®match, 0) == 0)
1842 static const struct got_error *
1843 queue_commits(struct tog_log_thread_args *a)
1845 const struct got_error *err = NULL;
1848 * We keep all commits open throughout the lifetime of the log
1849 * view in order to avoid having to re-fetch commits from disk
1850 * while updating the display.
1853 struct got_object_id *id;
1854 struct got_commit_object *commit;
1855 struct commit_queue_entry *entry;
1858 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1860 if (err || id == NULL)
1863 err = got_object_open_as_commit(&commit, a->repo, id);
1866 entry = alloc_commit_queue_entry(commit, id);
1867 if (entry == NULL) {
1868 err = got_error_from_errno("alloc_commit_queue_entry");
1872 errcode = pthread_mutex_lock(&tog_mutex);
1874 err = got_error_set_errno(errcode,
1875 "pthread_mutex_lock");
1879 entry->idx = a->commits->ncommits;
1880 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1881 a->commits->ncommits++;
1883 if (*a->searching == TOG_SEARCH_FORWARD &&
1884 !*a->search_next_done) {
1886 err = match_commit(&have_match, id, commit, a->regex);
1890 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1893 errcode = pthread_mutex_unlock(&tog_mutex);
1894 if (errcode && err == NULL)
1895 err = got_error_set_errno(errcode,
1896 "pthread_mutex_unlock");
1899 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1905 select_commit(struct tog_log_view_state *s)
1907 struct commit_queue_entry *entry;
1910 entry = s->first_displayed_entry;
1912 if (ncommits == s->selected) {
1913 s->selected_entry = entry;
1916 entry = TAILQ_NEXT(entry, entry);
1921 static const struct got_error *
1922 draw_commits(struct tog_view *view)
1924 const struct got_error *err = NULL;
1925 struct tog_log_view_state *s = &view->state.log;
1926 struct commit_queue_entry *entry = s->selected_entry;
1927 const int limit = view->nlines;
1929 int ncommits, author_cols = 4;
1930 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1931 char *refs_str = NULL;
1933 struct tog_color *tc;
1934 static const size_t date_display_cols = 12;
1936 if (s->selected_entry &&
1937 !(view->searching && view->search_next_done == 0)) {
1938 struct got_reflist_head *refs;
1939 err = got_object_id_str(&id_str, s->selected_entry->id);
1942 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1943 s->selected_entry->id);
1945 err = build_refs_str(&refs_str, refs,
1946 s->selected_entry->id, s->repo);
1952 if (s->thread_args.commits_needed == 0)
1953 halfdelay(10); /* disable fast refresh */
1955 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1956 if (asprintf(&ncommits_str, " [%d/%d] %s",
1957 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1958 (view->searching && !view->search_next_done) ?
1959 "searching..." : "loading...") == -1) {
1960 err = got_error_from_errno("asprintf");
1964 const char *search_str = NULL;
1966 if (view->searching) {
1967 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1968 search_str = "no more matches";
1969 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1970 search_str = "no matches found";
1971 else if (!view->search_next_done)
1972 search_str = "searching...";
1975 if (asprintf(&ncommits_str, " [%d/%d] %s",
1976 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1977 search_str ? search_str :
1978 (refs_str ? refs_str : "")) == -1) {
1979 err = got_error_from_errno("asprintf");
1984 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1985 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1986 "........................................",
1987 s->in_repo_path, ncommits_str) == -1) {
1988 err = got_error_from_errno("asprintf");
1992 } else if (asprintf(&header, "commit %s%s",
1993 id_str ? id_str : "........................................",
1994 ncommits_str) == -1) {
1995 err = got_error_from_errno("asprintf");
1999 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2003 werase(view->window);
2005 if (view_needs_focus_indication(view))
2006 wstandout(view->window);
2007 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2009 wattr_on(view->window,
2010 COLOR_PAIR(tc->colorpair), NULL);
2011 waddwstr(view->window, wline);
2013 wattr_off(view->window,
2014 COLOR_PAIR(tc->colorpair), NULL);
2015 while (width < view->ncols) {
2016 waddch(view->window, ' ');
2019 if (view_needs_focus_indication(view))
2020 wstandend(view->window);
2025 /* Grow author column size if necessary, and set view->maxx. */
2026 entry = s->first_displayed_entry;
2030 char *author, *eol, *msg, *msg0;
2031 wchar_t *wauthor, *wmsg;
2033 if (ncommits >= limit - 1)
2035 author = strdup(got_object_commit_get_author(entry->commit));
2036 if (author == NULL) {
2037 err = got_error_from_errno("strdup");
2040 err = format_author(&wauthor, &width, author, COLS,
2042 if (author_cols < width)
2043 author_cols = width;
2046 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2050 while (*msg == '\n')
2052 if ((eol = strchr(msg, '\n')))
2054 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2055 date_display_cols + author_cols, 0);
2058 view->maxx = MAX(view->maxx, width);
2062 entry = TAILQ_NEXT(entry, entry);
2065 entry = s->first_displayed_entry;
2066 s->last_displayed_entry = s->first_displayed_entry;
2069 if (ncommits >= limit - 1)
2071 if (ncommits == s->selected)
2072 wstandout(view->window);
2073 err = draw_commit(view, entry->commit, entry->id,
2074 date_display_cols, author_cols);
2075 if (ncommits == s->selected)
2076 wstandend(view->window);
2080 s->last_displayed_entry = entry;
2081 entry = TAILQ_NEXT(entry, entry);
2094 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2096 struct commit_queue_entry *entry;
2099 entry = TAILQ_FIRST(&s->commits.head);
2100 if (s->first_displayed_entry == entry)
2103 entry = s->first_displayed_entry;
2104 while (entry && nscrolled < maxscroll) {
2105 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2107 s->first_displayed_entry = entry;
2113 static const struct got_error *
2114 trigger_log_thread(struct tog_view *view, int wait)
2116 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2119 halfdelay(1); /* fast refresh while loading commits */
2121 while (ta->commits_needed > 0 || ta->load_all) {
2122 if (ta->log_complete)
2125 /* Wake the log thread. */
2126 errcode = pthread_cond_signal(&ta->need_commits);
2128 return got_error_set_errno(errcode,
2129 "pthread_cond_signal");
2132 * The mutex will be released while the view loop waits
2133 * in wgetch(), at which time the log thread will run.
2138 /* Display progress update in log view. */
2139 show_log_view(view);
2143 /* Wait right here while next commit is being loaded. */
2144 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2146 return got_error_set_errno(errcode,
2147 "pthread_cond_wait");
2149 /* Display progress update in log view. */
2150 show_log_view(view);
2158 static const struct got_error *
2159 request_log_commits(struct tog_view *view)
2161 struct tog_log_view_state *state = &view->state.log;
2162 const struct got_error *err = NULL;
2164 state->thread_args.commits_needed = view->nscrolled;
2165 err = trigger_log_thread(view, 1);
2166 view->nscrolled = 0;
2171 static const struct got_error *
2172 log_scroll_down(struct tog_view *view, int maxscroll)
2174 struct tog_log_view_state *s = &view->state.log;
2175 const struct got_error *err = NULL;
2176 struct commit_queue_entry *pentry;
2177 int nscrolled = 0, ncommits_needed;
2179 if (s->last_displayed_entry == NULL)
2182 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2183 if (s->commits.ncommits < ncommits_needed &&
2184 !s->thread_args.log_complete) {
2186 * Ask the log thread for required amount of commits.
2188 s->thread_args.commits_needed += maxscroll;
2189 err = trigger_log_thread(view, 1);
2195 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2196 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2199 s->last_displayed_entry = pentry ?
2200 pentry : s->last_displayed_entry;;
2202 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2205 s->first_displayed_entry = pentry;
2206 } while (++nscrolled < maxscroll);
2208 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2209 view->nscrolled += nscrolled;
2211 view->nscrolled = 0;
2216 static const struct got_error *
2217 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2218 struct got_commit_object *commit, struct got_object_id *commit_id,
2219 struct tog_view *log_view, struct got_repository *repo)
2221 const struct got_error *err;
2222 struct got_object_qid *parent_id;
2223 struct tog_view *diff_view;
2225 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2226 if (diff_view == NULL)
2227 return got_error_from_errno("view_open");
2229 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2230 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2231 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2233 *new_view = diff_view;
2237 static const struct got_error *
2238 tree_view_visit_subtree(struct tog_tree_view_state *s,
2239 struct got_tree_object *subtree)
2241 struct tog_parent_tree *parent;
2243 parent = calloc(1, sizeof(*parent));
2245 return got_error_from_errno("calloc");
2247 parent->tree = s->tree;
2248 parent->first_displayed_entry = s->first_displayed_entry;
2249 parent->selected_entry = s->selected_entry;
2250 parent->selected = s->selected;
2251 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2254 s->first_displayed_entry = NULL;
2258 static const struct got_error *
2259 tree_view_walk_path(struct tog_tree_view_state *s,
2260 struct got_commit_object *commit, const char *path)
2262 const struct got_error *err = NULL;
2263 struct got_tree_object *tree = NULL;
2265 char *slash, *subpath = NULL;
2267 /* Walk the path and open corresponding tree objects. */
2270 struct got_tree_entry *te;
2271 struct got_object_id *tree_id;
2277 /* Ensure the correct subtree entry is selected. */
2278 slash = strchr(p, '/');
2280 te_name = strdup(p);
2282 te_name = strndup(p, slash - p);
2283 if (te_name == NULL) {
2284 err = got_error_from_errno("strndup");
2287 te = got_object_tree_find_entry(s->tree, te_name);
2289 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2294 s->first_displayed_entry = s->selected_entry = te;
2296 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2297 break; /* jump to this file's entry */
2299 slash = strchr(p, '/');
2301 subpath = strndup(path, slash - path);
2303 subpath = strdup(path);
2304 if (subpath == NULL) {
2305 err = got_error_from_errno("strdup");
2309 err = got_object_id_by_path(&tree_id, s->repo, commit,
2314 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2319 err = tree_view_visit_subtree(s, tree);
2321 got_object_tree_close(tree);
2335 static const struct got_error *
2336 browse_commit_tree(struct tog_view **new_view, int begin_x,
2337 struct commit_queue_entry *entry, const char *path,
2338 const char *head_ref_name, struct got_repository *repo)
2340 const struct got_error *err = NULL;
2341 struct tog_tree_view_state *s;
2342 struct tog_view *tree_view;
2344 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2345 if (tree_view == NULL)
2346 return got_error_from_errno("view_open");
2348 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2351 s = &tree_view->state.tree;
2353 *new_view = tree_view;
2355 if (got_path_is_root_dir(path))
2358 return tree_view_walk_path(s, entry->commit, path);
2361 static const struct got_error *
2362 block_signals_used_by_main_thread(void)
2367 if (sigemptyset(&sigset) == -1)
2368 return got_error_from_errno("sigemptyset");
2370 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2371 if (sigaddset(&sigset, SIGWINCH) == -1)
2372 return got_error_from_errno("sigaddset");
2373 if (sigaddset(&sigset, SIGCONT) == -1)
2374 return got_error_from_errno("sigaddset");
2375 if (sigaddset(&sigset, SIGINT) == -1)
2376 return got_error_from_errno("sigaddset");
2377 if (sigaddset(&sigset, SIGTERM) == -1)
2378 return got_error_from_errno("sigaddset");
2380 /* ncurses handles SIGTSTP */
2381 if (sigaddset(&sigset, SIGTSTP) == -1)
2382 return got_error_from_errno("sigaddset");
2384 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2386 return got_error_set_errno(errcode, "pthread_sigmask");
2392 log_thread(void *arg)
2394 const struct got_error *err = NULL;
2396 struct tog_log_thread_args *a = arg;
2399 err = block_signals_used_by_main_thread();
2403 while (!done && !err && !tog_fatal_signal_received()) {
2404 err = queue_commits(a);
2406 if (err->code != GOT_ERR_ITER_COMPLETED)
2410 } else if (a->commits_needed > 0 && !a->load_all)
2411 a->commits_needed--;
2413 errcode = pthread_mutex_lock(&tog_mutex);
2415 err = got_error_set_errno(errcode,
2416 "pthread_mutex_lock");
2418 } else if (*a->quit)
2420 else if (*a->first_displayed_entry == NULL) {
2421 *a->first_displayed_entry =
2422 TAILQ_FIRST(&a->commits->head);
2423 *a->selected_entry = *a->first_displayed_entry;
2426 errcode = pthread_cond_signal(&a->commit_loaded);
2428 err = got_error_set_errno(errcode,
2429 "pthread_cond_signal");
2430 pthread_mutex_unlock(&tog_mutex);
2435 a->commits_needed = 0;
2437 if (a->commits_needed == 0 && !a->load_all) {
2438 errcode = pthread_cond_wait(&a->need_commits,
2441 err = got_error_set_errno(errcode,
2442 "pthread_cond_wait");
2448 errcode = pthread_mutex_unlock(&tog_mutex);
2449 if (errcode && err == NULL)
2450 err = got_error_set_errno(errcode,
2451 "pthread_mutex_unlock");
2453 a->log_complete = 1;
2457 static const struct got_error *
2458 stop_log_thread(struct tog_log_view_state *s)
2460 const struct got_error *err = NULL;
2465 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2467 return got_error_set_errno(errcode,
2468 "pthread_cond_signal");
2469 errcode = pthread_mutex_unlock(&tog_mutex);
2471 return got_error_set_errno(errcode,
2472 "pthread_mutex_unlock");
2473 errcode = pthread_join(s->thread, (void **)&err);
2475 return got_error_set_errno(errcode, "pthread_join");
2476 errcode = pthread_mutex_lock(&tog_mutex);
2478 return got_error_set_errno(errcode,
2479 "pthread_mutex_lock");
2483 if (s->thread_args.repo) {
2484 err = got_repo_close(s->thread_args.repo);
2485 s->thread_args.repo = NULL;
2488 if (s->thread_args.pack_fds) {
2489 const struct got_error *pack_err =
2490 got_repo_pack_fds_close(s->thread_args.pack_fds);
2493 s->thread_args.pack_fds = NULL;
2496 if (s->thread_args.graph) {
2497 got_commit_graph_close(s->thread_args.graph);
2498 s->thread_args.graph = NULL;
2504 static const struct got_error *
2505 close_log_view(struct tog_view *view)
2507 const struct got_error *err = NULL;
2508 struct tog_log_view_state *s = &view->state.log;
2511 err = stop_log_thread(s);
2513 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2514 if (errcode && err == NULL)
2515 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2517 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2518 if (errcode && err == NULL)
2519 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2521 free_commits(&s->commits);
2522 free(s->in_repo_path);
2523 s->in_repo_path = NULL;
2526 free(s->head_ref_name);
2527 s->head_ref_name = NULL;
2531 static const struct got_error *
2532 search_start_log_view(struct tog_view *view)
2534 struct tog_log_view_state *s = &view->state.log;
2536 s->matched_entry = NULL;
2537 s->search_entry = NULL;
2541 static const struct got_error *
2542 search_next_log_view(struct tog_view *view)
2544 const struct got_error *err = NULL;
2545 struct tog_log_view_state *s = &view->state.log;
2546 struct commit_queue_entry *entry;
2548 /* Display progress update in log view. */
2549 show_log_view(view);
2553 if (s->search_entry) {
2555 errcode = pthread_mutex_unlock(&tog_mutex);
2557 return got_error_set_errno(errcode,
2558 "pthread_mutex_unlock");
2559 ch = wgetch(view->window);
2560 errcode = pthread_mutex_lock(&tog_mutex);
2562 return got_error_set_errno(errcode,
2563 "pthread_mutex_lock");
2564 if (ch == KEY_BACKSPACE) {
2565 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2568 if (view->searching == TOG_SEARCH_FORWARD)
2569 entry = TAILQ_NEXT(s->search_entry, entry);
2571 entry = TAILQ_PREV(s->search_entry,
2572 commit_queue_head, entry);
2573 } else if (s->matched_entry) {
2574 int matched_idx = s->matched_entry->idx;
2575 int selected_idx = s->selected_entry->idx;
2578 * If the user has moved the cursor after we hit a match,
2579 * the position from where we should continue searching
2580 * might have changed.
2582 if (view->searching == TOG_SEARCH_FORWARD) {
2583 if (matched_idx > selected_idx)
2584 entry = TAILQ_NEXT(s->selected_entry, entry);
2586 entry = TAILQ_NEXT(s->matched_entry, entry);
2588 if (matched_idx < selected_idx)
2589 entry = TAILQ_PREV(s->selected_entry,
2590 commit_queue_head, entry);
2592 entry = TAILQ_PREV(s->matched_entry,
2593 commit_queue_head, entry);
2596 entry = s->selected_entry;
2602 if (entry == NULL) {
2603 if (s->thread_args.log_complete ||
2604 view->searching == TOG_SEARCH_BACKWARD) {
2605 view->search_next_done =
2606 (s->matched_entry == NULL ?
2607 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2608 s->search_entry = NULL;
2612 * Poke the log thread for more commits and return,
2613 * allowing the main loop to make progress. Search
2614 * will resume at s->search_entry once we come back.
2616 s->thread_args.commits_needed++;
2617 return trigger_log_thread(view, 0);
2620 err = match_commit(&have_match, entry->id, entry->commit,
2625 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2626 s->matched_entry = entry;
2630 s->search_entry = entry;
2631 if (view->searching == TOG_SEARCH_FORWARD)
2632 entry = TAILQ_NEXT(entry, entry);
2634 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2637 if (s->matched_entry) {
2638 int cur = s->selected_entry->idx;
2639 while (cur < s->matched_entry->idx) {
2640 err = input_log_view(NULL, view, KEY_DOWN);
2645 while (cur > s->matched_entry->idx) {
2646 err = input_log_view(NULL, view, KEY_UP);
2653 s->search_entry = NULL;
2658 static const struct got_error *
2659 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2660 struct got_repository *repo, const char *head_ref_name,
2661 const char *in_repo_path, int log_branches)
2663 const struct got_error *err = NULL;
2664 struct tog_log_view_state *s = &view->state.log;
2665 struct got_repository *thread_repo = NULL;
2666 struct got_commit_graph *thread_graph = NULL;
2669 if (in_repo_path != s->in_repo_path) {
2670 free(s->in_repo_path);
2671 s->in_repo_path = strdup(in_repo_path);
2672 if (s->in_repo_path == NULL)
2673 return got_error_from_errno("strdup");
2676 /* The commit queue only contains commits being displayed. */
2677 TAILQ_INIT(&s->commits.head);
2678 s->commits.ncommits = 0;
2681 if (head_ref_name) {
2682 s->head_ref_name = strdup(head_ref_name);
2683 if (s->head_ref_name == NULL) {
2684 err = got_error_from_errno("strdup");
2688 s->start_id = got_object_id_dup(start_id);
2689 if (s->start_id == NULL) {
2690 err = got_error_from_errno("got_object_id_dup");
2693 s->log_branches = log_branches;
2695 STAILQ_INIT(&s->colors);
2696 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2697 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2698 get_color_value("TOG_COLOR_COMMIT"));
2701 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2702 get_color_value("TOG_COLOR_AUTHOR"));
2704 free_colors(&s->colors);
2707 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2708 get_color_value("TOG_COLOR_DATE"));
2710 free_colors(&s->colors);
2715 view->show = show_log_view;
2716 view->input = input_log_view;
2717 view->close = close_log_view;
2718 view->search_start = search_start_log_view;
2719 view->search_next = search_next_log_view;
2721 if (s->thread_args.pack_fds == NULL) {
2722 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2726 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2727 s->thread_args.pack_fds);
2730 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2734 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2735 s->repo, NULL, NULL);
2739 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2741 err = got_error_set_errno(errcode, "pthread_cond_init");
2744 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2746 err = got_error_set_errno(errcode, "pthread_cond_init");
2750 s->thread_args.commits_needed = view->nlines;
2751 s->thread_args.graph = thread_graph;
2752 s->thread_args.commits = &s->commits;
2753 s->thread_args.in_repo_path = s->in_repo_path;
2754 s->thread_args.start_id = s->start_id;
2755 s->thread_args.repo = thread_repo;
2756 s->thread_args.log_complete = 0;
2757 s->thread_args.quit = &s->quit;
2758 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2759 s->thread_args.selected_entry = &s->selected_entry;
2760 s->thread_args.searching = &view->searching;
2761 s->thread_args.search_next_done = &view->search_next_done;
2762 s->thread_args.regex = &view->regex;
2765 close_log_view(view);
2769 static const struct got_error *
2770 show_log_view(struct tog_view *view)
2772 const struct got_error *err;
2773 struct tog_log_view_state *s = &view->state.log;
2775 if (s->thread == NULL) {
2776 int errcode = pthread_create(&s->thread, NULL, log_thread,
2779 return got_error_set_errno(errcode, "pthread_create");
2780 if (s->thread_args.commits_needed > 0) {
2781 err = trigger_log_thread(view, 1);
2787 return draw_commits(view);
2791 log_move_cursor_up(struct tog_view *view, int page, int home)
2793 struct tog_log_view_state *s = &view->state.log;
2795 if (s->selected_entry->idx == 0)
2797 if (s->first_displayed_entry == NULL)
2800 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2802 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2804 if (!page && !home && s->selected > 0)
2807 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2813 static const struct got_error *
2814 log_move_cursor_down(struct tog_view *view, int page)
2816 struct tog_log_view_state *s = &view->state.log;
2817 struct commit_queue_entry *first;
2818 const struct got_error *err = NULL;
2820 first = s->first_displayed_entry;
2821 if (first == NULL) {
2826 if (s->thread_args.log_complete &&
2827 s->selected_entry->idx >= s->commits.ncommits - 1)
2831 int eos = view->nlines - 2;
2833 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2834 view_is_splitscreen(view->child))
2835 --eos; /* border consumes the last line */
2836 if (s->selected < MIN(eos, s->commits.ncommits - 1))
2839 err = log_scroll_down(view, 1);
2840 } else if (s->thread_args.load_all) {
2841 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2842 s->selected += MIN(s->last_displayed_entry->idx -
2843 s->selected_entry->idx, page + 1);
2845 err = log_scroll_down(view, MIN(page,
2846 s->commits.ncommits - s->selected_entry->idx - 1));
2847 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2849 err = log_scroll_down(view, page);
2852 if (first == s->first_displayed_entry && s->selected <
2853 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2854 s->selected = MIN(s->commits.ncommits - 1, page);
2861 * We might necessarily overshoot in horizontal
2862 * splits; if so, select the last displayed commit.
2864 s->selected = MIN(s->selected,
2865 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2869 if (s->thread_args.log_complete &&
2870 s->selected_entry->idx == s->commits.ncommits - 1)
2877 * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2878 * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2879 * TOG_VIEW_SPLIT_HRZN horizontal split
2880 * Assign start column and line of the new split to *x and *y, respectively,
2881 * and assign view mode to view->mode.
2884 view_get_split(struct tog_view *view, int *y, int *x)
2888 mode = getenv("TOG_VIEW_SPLIT_MODE");
2890 if (!mode || mode[0] != 'h') {
2891 view->mode = TOG_VIEW_SPLIT_VERT;
2892 *x = view_split_begin_x(view->begin_x);
2893 } else if (mode && mode[0] == 'h') {
2894 view->mode = TOG_VIEW_SPLIT_HRZN;
2895 *y = view_split_begin_y(view->lines);
2899 /* Split view horizontally at y and offset view->state->selected line. */
2900 static const struct got_error *
2901 view_init_hsplit(struct tog_view *view, int y)
2903 const struct got_error *err = NULL;
2906 err = view_resize(view);
2910 err = offset_selection_down(view);
2915 static const struct got_error *
2916 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2918 const struct got_error *err = NULL;
2919 struct tog_log_view_state *s = &view->state.log;
2920 struct tog_view *diff_view = NULL, *tree_view = NULL;
2921 struct tog_view *ref_view = NULL;
2922 struct commit_queue_entry *entry;
2923 int begin_x = 0, begin_y = 0, eos, n, nscroll;
2925 if (s->thread_args.load_all) {
2926 if (ch == KEY_BACKSPACE)
2927 s->thread_args.load_all = 0;
2928 else if (s->thread_args.log_complete) {
2929 err = log_move_cursor_down(view, s->commits.ncommits);
2930 s->thread_args.load_all = 0;
2935 eos = nscroll = view->nlines - 1;
2936 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2937 view_is_splitscreen(view->child))
2949 view->x = MAX(view->maxx - view->ncols / 2, 0);
2954 if (view->x + view->ncols / 2 < view->maxx)
2955 view->x += 2; /* move two columns right */
2961 view->x -= MIN(view->x, 2); /* move two columns back */
2970 log_move_cursor_up(view, 0, 0);
2974 log_move_cursor_up(view, 0, 1);
2984 log_move_cursor_up(view, nscroll, 0);
2991 err = log_move_cursor_down(view, 0);
2995 /* We don't know yet how many commits, so we're forced to
2996 * traverse them all. */
2998 if (!s->thread_args.log_complete) {
2999 s->thread_args.load_all = 1;
3000 return trigger_log_thread(view, 0);
3004 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3005 for (n = 0; n < eos; n++) {
3008 s->first_displayed_entry = entry;
3009 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3012 s->selected = n - 1;
3024 err = log_move_cursor_down(view, nscroll);
3027 if (s->selected > view->nlines - 2)
3028 s->selected = view->nlines - 2;
3029 if (s->selected > s->commits.ncommits - 1)
3030 s->selected = s->commits.ncommits - 1;
3032 if (s->commits.ncommits < view->nlines - 1 &&
3033 !s->thread_args.log_complete) {
3034 s->thread_args.commits_needed += (view->nlines - 1) -
3035 s->commits.ncommits;
3036 err = trigger_log_thread(view, 1);
3042 if (s->selected_entry == NULL)
3045 /* get dimensions--don't split till initialisation succeeds */
3046 if (view_is_parent_view(view))
3047 view_get_split(view, &begin_y, &begin_x);
3049 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3050 s->selected_entry->commit, s->selected_entry->id,
3055 if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3056 err = view_init_hsplit(view, begin_y);
3062 diff_view->focussed = 1;
3063 diff_view->mode = view->mode;
3064 diff_view->nlines = view->lines - begin_y;
3066 if (view_is_parent_view(view)) {
3067 err = view_close_child(view);
3070 err = view_set_child(view, diff_view);
3073 view->focus_child = 1;
3075 *new_view = diff_view;
3080 if (s->selected_entry == NULL)
3082 if (view_is_parent_view(view))
3083 begin_x = view_split_begin_x(view->begin_x);
3084 err = browse_commit_tree(&tree_view, begin_x,
3085 s->selected_entry, s->in_repo_path, s->head_ref_name,
3090 tree_view->focussed = 1;
3091 if (view_is_parent_view(view)) {
3092 err = view_close_child(view);
3095 err = view_set_child(view, tree_view);
3098 view->focus_child = 1;
3100 *new_view = tree_view;
3106 if (ch == KEY_BACKSPACE &&
3107 got_path_is_root_dir(s->in_repo_path))
3109 err = stop_log_thread(s);
3112 if (ch == KEY_BACKSPACE) {
3114 err = got_path_dirname(&parent_path, s->in_repo_path);
3117 free(s->in_repo_path);
3118 s->in_repo_path = parent_path;
3119 s->thread_args.in_repo_path = s->in_repo_path;
3120 } else if (ch == CTRL('l')) {
3121 struct got_object_id *start_id;
3122 err = got_repo_match_object_id(&start_id, NULL,
3123 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3124 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3128 s->start_id = start_id;
3129 s->thread_args.start_id = s->start_id;
3131 s->log_branches = !s->log_branches;
3133 if (s->thread_args.pack_fds == NULL) {
3134 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3138 err = got_repo_open(&s->thread_args.repo,
3139 got_repo_get_path(s->repo), NULL,
3140 s->thread_args.pack_fds);
3144 err = tog_load_refs(s->repo, 0);
3147 err = got_commit_graph_open(&s->thread_args.graph,
3148 s->in_repo_path, !s->log_branches);
3151 err = got_commit_graph_iter_start(s->thread_args.graph,
3152 s->start_id, s->repo, NULL, NULL);
3155 free_commits(&s->commits);
3156 s->first_displayed_entry = NULL;
3157 s->last_displayed_entry = NULL;
3158 s->selected_entry = NULL;
3160 s->thread_args.log_complete = 0;
3162 s->thread_args.commits_needed = view->lines;
3163 s->matched_entry = NULL;
3164 s->search_entry = NULL;
3168 if (view_is_parent_view(view))
3169 begin_x = view_split_begin_x(view->begin_x);
3170 ref_view = view_open(view->nlines, view->ncols,
3171 view->begin_y, begin_x, TOG_VIEW_REF);
3172 if (ref_view == NULL)
3173 return got_error_from_errno("view_open");
3174 err = open_ref_view(ref_view, s->repo);
3176 view_close(ref_view);
3180 ref_view->focussed = 1;
3181 if (view_is_parent_view(view)) {
3182 err = view_close_child(view);
3185 err = view_set_child(view, ref_view);
3188 view->focus_child = 1;
3190 *new_view = ref_view;
3200 static const struct got_error *
3201 apply_unveil(const char *repo_path, const char *worktree_path)
3203 const struct got_error *error;
3206 if (unveil("gmon.out", "rwc") != 0)
3207 return got_error_from_errno2("unveil", "gmon.out");
3209 if (repo_path && unveil(repo_path, "r") != 0)
3210 return got_error_from_errno2("unveil", repo_path);
3212 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3213 return got_error_from_errno2("unveil", worktree_path);
3215 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3216 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3218 error = got_privsep_unveil_exec_helpers();
3222 if (unveil(NULL, NULL) != 0)
3223 return got_error_from_errno("unveil");
3232 * Override default signal handlers before starting ncurses.
3233 * This should prevent ncurses from installing its own
3234 * broken cleanup() signal handler.
3236 signal(SIGWINCH, tog_sigwinch);
3237 signal(SIGPIPE, tog_sigpipe);
3238 signal(SIGCONT, tog_sigcont);
3239 signal(SIGINT, tog_sigint);
3240 signal(SIGTERM, tog_sigterm);
3244 halfdelay(1); /* Do fast refresh while initial view is loading. */
3247 intrflush(stdscr, FALSE);
3248 keypad(stdscr, TRUE);
3250 if (getenv("TOG_COLORS") != NULL) {
3252 use_default_colors();
3256 static const struct got_error *
3257 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3258 struct got_repository *repo, struct got_worktree *worktree)
3260 const struct got_error *err = NULL;
3263 *in_repo_path = strdup("/");
3264 if (*in_repo_path == NULL)
3265 return got_error_from_errno("strdup");
3270 const char *prefix = got_worktree_get_path_prefix(worktree);
3273 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3276 if (asprintf(in_repo_path, "%s%s%s", prefix,
3277 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3279 err = got_error_from_errno("asprintf");
3280 *in_repo_path = NULL;
3284 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3289 static const struct got_error *
3290 cmd_log(int argc, char *argv[])
3292 const struct got_error *error;
3293 struct got_repository *repo = NULL;
3294 struct got_worktree *worktree = NULL;
3295 struct got_object_id *start_id = NULL;
3296 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3297 char *start_commit = NULL, *label = NULL;
3298 struct got_reference *ref = NULL;
3299 const char *head_ref_name = NULL;
3300 int ch, log_branches = 0;
3301 struct tog_view *view;
3302 int *pack_fds = NULL;
3304 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3310 start_commit = optarg;
3313 repo_path = realpath(optarg, NULL);
3314 if (repo_path == NULL)
3315 return got_error_from_errno2("realpath",
3330 error = got_repo_pack_fds_open(&pack_fds);
3334 if (repo_path == NULL) {
3335 cwd = getcwd(NULL, 0);
3337 return got_error_from_errno("getcwd");
3338 error = got_worktree_open(&worktree, cwd);
3339 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3343 strdup(got_worktree_get_repo_path(worktree));
3345 repo_path = strdup(cwd);
3346 if (repo_path == NULL) {
3347 error = got_error_from_errno("strdup");
3352 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3356 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3363 error = apply_unveil(got_repo_get_path(repo),
3364 worktree ? got_worktree_get_root_path(worktree) : NULL);
3368 /* already loaded by tog_log_with_path()? */
3369 if (TAILQ_EMPTY(&tog_refs)) {
3370 error = tog_load_refs(repo, 0);
3375 if (start_commit == NULL) {
3376 error = got_repo_match_object_id(&start_id, &label,
3377 worktree ? got_worktree_get_head_ref_name(worktree) :
3378 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3381 head_ref_name = label;
3383 error = got_ref_open(&ref, repo, start_commit, 0);
3385 head_ref_name = got_ref_get_name(ref);
3386 else if (error->code != GOT_ERR_NOT_REF)
3388 error = got_repo_match_object_id(&start_id, NULL,
3389 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3394 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3396 error = got_error_from_errno("view_open");
3399 error = open_log_view(view, start_id, repo, head_ref_name,
3400 in_repo_path, log_branches);
3404 /* Release work tree lock. */
3405 got_worktree_close(worktree);
3408 error = view_loop(view);
3418 const struct got_error *close_err = got_repo_close(repo);
3423 got_worktree_close(worktree);
3425 const struct got_error *pack_err =
3426 got_repo_pack_fds_close(pack_fds);
3438 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3439 "[-w] object1 object2\n", getprogname());
3444 match_line(const char *line, regex_t *regex, size_t nmatch,
3445 regmatch_t *regmatch)
3447 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3450 static struct tog_color *
3451 match_color(struct tog_colors *colors, const char *line)
3453 struct tog_color *tc = NULL;
3455 STAILQ_FOREACH(tc, colors, entry) {
3456 if (match_line(line, &tc->regex, 0, NULL))
3463 static const struct got_error *
3464 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3465 WINDOW *window, int skipcol, regmatch_t *regmatch)
3467 const struct got_error *err = NULL;
3469 wchar_t *wline = NULL;
3470 int rme, rms, n, width, scrollx;
3471 int width0 = 0, width1 = 0, width2 = 0;
3472 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3476 rms = regmatch->rm_so;
3477 rme = regmatch->rm_eo;
3479 err = expand_tab(&exstr, line);
3483 /* Split the line into 3 segments, according to match offsets. */
3484 seg0 = strndup(exstr, rms);
3486 err = got_error_from_errno("strndup");
3489 seg1 = strndup(exstr + rms, rme - rms);
3491 err = got_error_from_errno("strndup");
3494 seg2 = strdup(exstr + rme);
3496 err = got_error_from_errno("strndup");
3500 /* draw up to matched token if we haven't scrolled past it */
3501 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3505 n = MAX(width0 - skipcol, 0);
3508 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3509 wlimit, col_tab_align, 1);
3512 waddwstr(window, &wline[scrollx]);
3522 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3526 wlen = wcslen(wline);
3528 width = wcwidth(wline[i]);
3530 /* should not happen, tabs are expanded */
3531 err = got_error(GOT_ERR_RANGE);
3534 if (width0 + w + width > skipcol)
3539 /* draw (visible part of) matched token (if scrolled into it) */
3540 if (width1 - w > 0) {
3541 wattron(window, A_STANDOUT);
3542 waddwstr(window, &wline[i]);
3543 wattroff(window, A_STANDOUT);
3544 wlimit -= (width1 - w);
3545 *wtotal += (width1 - w);
3549 if (wlimit > 0) { /* draw rest of line */
3551 if (skipcol > width0 + width1) {
3552 err = format_line(&wline, &width2, &scrollx, seg2,
3553 skipcol - (width0 + width1), wlimit,
3557 waddwstr(window, &wline[scrollx]);
3559 err = format_line(&wline, &width2, NULL, seg2, 0,
3560 wlimit, col_tab_align, 1);
3563 waddwstr(window, wline);
3576 static const struct got_error *
3577 draw_file(struct tog_view *view, const char *header)
3579 struct tog_diff_view_state *s = &view->state.diff;
3580 regmatch_t *regmatch = &view->regmatch;
3581 const struct got_error *err;
3584 size_t linesize = 0;
3586 struct tog_color *tc;
3589 int max_lines = view->nlines;
3590 int nlines = s->nlines;
3593 line_offset = s->line_offsets[s->first_displayed_line - 1];
3594 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3595 return got_error_from_errno("fseek");
3597 werase(view->window);
3600 if (asprintf(&line, "[%d/%d] %s",
3601 s->first_displayed_line - 1 + s->selected_line, nlines,
3603 return got_error_from_errno("asprintf");
3604 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3610 if (view_needs_focus_indication(view))
3611 wstandout(view->window);
3612 waddwstr(view->window, wline);
3615 if (view_needs_focus_indication(view))
3616 wstandend(view->window);
3617 if (width <= view->ncols - 1)
3618 waddch(view->window, '\n');
3628 while (max_lines > 0 && nprinted < max_lines) {
3629 linelen = getline(&line, &linesize, s->f);
3630 if (linelen == -1) {
3636 return got_ferror(s->f, GOT_ERR_IO);
3639 /* Set view->maxx based on full line length. */
3640 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3646 view->maxx = MAX(view->maxx, width);
3650 tc = match_color(&s->colors, line);
3652 wattr_on(view->window,
3653 COLOR_PAIR(tc->colorpair), NULL);
3654 if (s->first_displayed_line + nprinted == s->matched_line &&
3655 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3656 err = add_matched_line(&width, line, view->ncols, 0,
3657 view->window, view->x, regmatch);
3664 err = format_line(&wline, &width, &skip, line,
3665 view->x, view->ncols, 0, view->x ? 1 : 0);
3670 waddwstr(view->window, &wline[skip]);
3675 wattr_off(view->window,
3676 COLOR_PAIR(tc->colorpair), NULL);
3677 if (width <= view->ncols - 1)
3678 waddch(view->window, '\n');
3683 s->last_displayed_line = s->first_displayed_line +
3686 s->last_displayed_line = s->first_displayed_line;
3691 while (nprinted < view->nlines) {
3692 waddch(view->window, '\n');
3696 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3702 wstandout(view->window);
3703 waddwstr(view->window, wline);
3706 wstandend(view->window);
3713 get_datestr(time_t *time, char *datebuf)
3715 struct tm mytm, *tm;
3718 tm = gmtime_r(time, &mytm);
3721 s = asctime_r(tm, datebuf);
3724 p = strchr(s, '\n');
3730 static const struct got_error *
3731 get_changed_paths(struct got_pathlist_head *paths,
3732 struct got_commit_object *commit, struct got_repository *repo)
3734 const struct got_error *err = NULL;
3735 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3736 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3737 struct got_object_qid *qid;
3739 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3741 struct got_commit_object *pcommit;
3742 err = got_object_open_as_commit(&pcommit, repo,
3747 tree_id1 = got_object_id_dup(
3748 got_object_commit_get_tree_id(pcommit));
3749 if (tree_id1 == NULL) {
3750 got_object_commit_close(pcommit);
3751 return got_error_from_errno("got_object_id_dup");
3753 got_object_commit_close(pcommit);
3758 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3763 tree_id2 = got_object_commit_get_tree_id(commit);
3764 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3768 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3769 got_diff_tree_collect_changed_paths, paths, 0);
3772 got_object_tree_close(tree1);
3774 got_object_tree_close(tree2);
3779 static const struct got_error *
3780 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3784 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3786 return got_error_from_errno("reallocarray");
3788 (*line_offsets)[*nlines] = off;
3793 static const struct got_error *
3794 write_commit_info(off_t **line_offsets, size_t *nlines,
3795 struct got_object_id *commit_id, struct got_reflist_head *refs,
3796 struct got_repository *repo, FILE *outfile)
3798 const struct got_error *err = NULL;
3799 char datebuf[26], *datestr;
3800 struct got_commit_object *commit;
3801 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3802 time_t committer_time;
3803 const char *author, *committer;
3804 char *refs_str = NULL;
3805 struct got_pathlist_head changed_paths;
3806 struct got_pathlist_entry *pe;
3810 TAILQ_INIT(&changed_paths);
3813 err = build_refs_str(&refs_str, refs, commit_id, repo);
3818 err = got_object_open_as_commit(&commit, repo, commit_id);
3822 err = got_object_id_str(&id_str, commit_id);
3824 err = got_error_from_errno("got_object_id_str");
3828 err = add_line_offset(line_offsets, nlines, 0);
3832 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3833 refs_str ? refs_str : "", refs_str ? ")" : "");
3835 err = got_error_from_errno("fprintf");
3839 err = add_line_offset(line_offsets, nlines, outoff);
3843 n = fprintf(outfile, "from: %s\n",
3844 got_object_commit_get_author(commit));
3846 err = got_error_from_errno("fprintf");
3850 err = add_line_offset(line_offsets, nlines, outoff);
3854 committer_time = got_object_commit_get_committer_time(commit);
3855 datestr = get_datestr(&committer_time, datebuf);
3857 n = fprintf(outfile, "date: %s UTC\n", datestr);
3859 err = got_error_from_errno("fprintf");
3863 err = add_line_offset(line_offsets, nlines, outoff);
3867 author = got_object_commit_get_author(commit);
3868 committer = got_object_commit_get_committer(commit);
3869 if (strcmp(author, committer) != 0) {
3870 n = fprintf(outfile, "via: %s\n", committer);
3872 err = got_error_from_errno("fprintf");
3876 err = add_line_offset(line_offsets, nlines, outoff);
3880 if (got_object_commit_get_nparents(commit) > 1) {
3881 const struct got_object_id_queue *parent_ids;
3882 struct got_object_qid *qid;
3884 parent_ids = got_object_commit_get_parent_ids(commit);
3885 STAILQ_FOREACH(qid, parent_ids, entry) {
3886 err = got_object_id_str(&id_str, &qid->id);
3889 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3891 err = got_error_from_errno("fprintf");
3895 err = add_line_offset(line_offsets, nlines, outoff);
3903 err = got_object_commit_get_logmsg(&logmsg, commit);
3907 while ((line = strsep(&s, "\n")) != NULL) {
3908 n = fprintf(outfile, "%s\n", line);
3910 err = got_error_from_errno("fprintf");
3914 err = add_line_offset(line_offsets, nlines, outoff);
3919 err = get_changed_paths(&changed_paths, commit, repo);
3922 TAILQ_FOREACH(pe, &changed_paths, entry) {
3923 struct got_diff_changed_path *cp = pe->data;
3924 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3926 err = got_error_from_errno("fprintf");
3930 err = add_line_offset(line_offsets, nlines, outoff);
3933 free((char *)pe->path);
3937 fputc('\n', outfile);
3939 err = add_line_offset(line_offsets, nlines, outoff);
3941 got_pathlist_free(&changed_paths);
3945 got_object_commit_close(commit);
3947 free(*line_offsets);
3948 *line_offsets = NULL;
3954 static const struct got_error *
3955 create_diff(struct tog_diff_view_state *s)
3957 const struct got_error *err = NULL;
3961 free(s->line_offsets);
3962 s->line_offsets = malloc(sizeof(off_t));
3963 if (s->line_offsets == NULL)
3964 return got_error_from_errno("malloc");
3969 err = got_error_from_errno("got_opentemp");
3972 if (s->f && fclose(s->f) == EOF) {
3973 err = got_error_from_errno("fclose");
3979 err = got_object_get_type(&obj_type, s->repo, s->id1);
3981 err = got_object_get_type(&obj_type, s->repo, s->id2);
3986 case GOT_OBJ_TYPE_BLOB:
3987 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3988 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3989 s->label1, s->label2, tog_diff_algo, s->diff_context,
3990 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3992 case GOT_OBJ_TYPE_TREE:
3993 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3994 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3995 tog_diff_algo, s->diff_context, s->ignore_whitespace,
3996 s->force_text_diff, s->repo, s->f);
3998 case GOT_OBJ_TYPE_COMMIT: {
3999 const struct got_object_id_queue *parent_ids;
4000 struct got_object_qid *pid;
4001 struct got_commit_object *commit2;
4002 struct got_reflist_head *refs;
4004 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4007 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4008 /* Show commit info if we're diffing to a parent/root commit. */
4009 if (s->id1 == NULL) {
4010 err = write_commit_info(&s->line_offsets, &s->nlines,
4011 s->id2, refs, s->repo, s->f);
4015 parent_ids = got_object_commit_get_parent_ids(commit2);
4016 STAILQ_FOREACH(pid, parent_ids, entry) {
4017 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4018 err = write_commit_info(
4019 &s->line_offsets, &s->nlines,
4020 s->id2, refs, s->repo, s->f);
4027 got_object_commit_close(commit2);
4029 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4030 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4031 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4032 s->force_text_diff, s->repo, s->f);
4036 err = got_error(GOT_ERR_OBJ_TYPE);
4042 if (s->f && fflush(s->f) != 0 && err == NULL)
4043 err = got_error_from_errno("fflush");
4048 diff_view_indicate_progress(struct tog_view *view)
4050 mvwaddstr(view->window, 0, 0, "diffing...");
4055 static const struct got_error *
4056 search_start_diff_view(struct tog_view *view)
4058 struct tog_diff_view_state *s = &view->state.diff;
4060 s->matched_line = 0;
4064 static const struct got_error *
4065 search_next_diff_view(struct tog_view *view)
4067 struct tog_diff_view_state *s = &view->state.diff;
4068 const struct got_error *err = NULL;
4071 size_t linesize = 0;
4074 if (!view->searching) {
4075 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4079 if (s->matched_line) {
4080 if (view->searching == TOG_SEARCH_FORWARD)
4081 lineno = s->matched_line + 1;
4083 lineno = s->matched_line - 1;
4085 lineno = s->first_displayed_line;
4090 if (lineno <= 0 || lineno > s->nlines) {
4091 if (s->matched_line == 0) {
4092 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4096 if (view->searching == TOG_SEARCH_FORWARD)
4102 offset = s->line_offsets[lineno - 1];
4103 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4105 return got_error_from_errno("fseeko");
4107 linelen = getline(&line, &linesize, s->f);
4108 if (linelen != -1) {
4110 err = expand_tab(&exstr, line);
4113 if (match_line(exstr, &view->regex, 1,
4115 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4116 s->matched_line = lineno;
4122 if (view->searching == TOG_SEARCH_FORWARD)
4129 if (s->matched_line) {
4130 s->first_displayed_line = s->matched_line;
4131 s->selected_line = 1;
4137 static const struct got_error *
4138 close_diff_view(struct tog_view *view)
4140 const struct got_error *err = NULL;
4141 struct tog_diff_view_state *s = &view->state.diff;
4147 if (s->f && fclose(s->f) == EOF)
4148 err = got_error_from_errno("fclose");
4150 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4151 err = got_error_from_errno("fclose");
4153 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4154 err = got_error_from_errno("fclose");
4156 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4157 err = got_error_from_errno("close");
4159 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4160 err = got_error_from_errno("close");
4162 free_colors(&s->colors);
4163 free(s->line_offsets);
4164 s->line_offsets = NULL;
4169 static const struct got_error *
4170 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4171 struct got_object_id *id2, const char *label1, const char *label2,
4172 int diff_context, int ignore_whitespace, int force_text_diff,
4173 struct tog_view *log_view, struct got_repository *repo)
4175 const struct got_error *err;
4176 struct tog_diff_view_state *s = &view->state.diff;
4178 memset(s, 0, sizeof(*s));
4182 if (id1 != NULL && id2 != NULL) {
4184 err = got_object_get_type(&type1, repo, id1);
4187 err = got_object_get_type(&type2, repo, id2);
4192 return got_error(GOT_ERR_OBJ_TYPE);
4194 s->first_displayed_line = 1;
4195 s->last_displayed_line = view->nlines;
4196 s->selected_line = 1;
4204 s->id1 = got_object_id_dup(id1);
4206 return got_error_from_errno("got_object_id_dup");
4210 s->id2 = got_object_id_dup(id2);
4211 if (s->id2 == NULL) {
4212 err = got_error_from_errno("got_object_id_dup");
4216 s->f1 = got_opentemp();
4217 if (s->f1 == NULL) {
4218 err = got_error_from_errno("got_opentemp");
4222 s->f2 = got_opentemp();
4223 if (s->f2 == NULL) {
4224 err = got_error_from_errno("got_opentemp");
4228 s->fd1 = got_opentempfd();
4230 err = got_error_from_errno("got_opentempfd");
4234 s->fd2 = got_opentempfd();
4236 err = got_error_from_errno("got_opentempfd");
4240 s->first_displayed_line = 1;
4241 s->last_displayed_line = view->nlines;
4242 s->diff_context = diff_context;
4243 s->ignore_whitespace = ignore_whitespace;
4244 s->force_text_diff = force_text_diff;
4245 s->log_view = log_view;
4248 STAILQ_INIT(&s->colors);
4249 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4250 err = add_color(&s->colors,
4251 "^-", TOG_COLOR_DIFF_MINUS,
4252 get_color_value("TOG_COLOR_DIFF_MINUS"));
4255 err = add_color(&s->colors, "^\\+",
4256 TOG_COLOR_DIFF_PLUS,
4257 get_color_value("TOG_COLOR_DIFF_PLUS"));
4260 err = add_color(&s->colors,
4261 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4262 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4266 err = add_color(&s->colors,
4267 "^(commit [0-9a-f]|parent [0-9]|"
4268 "(blob|file|tree|commit) [-+] |"
4269 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4270 get_color_value("TOG_COLOR_DIFF_META"));
4274 err = add_color(&s->colors,
4275 "^(from|via): ", TOG_COLOR_AUTHOR,
4276 get_color_value("TOG_COLOR_AUTHOR"));
4280 err = add_color(&s->colors,
4281 "^date: ", TOG_COLOR_DATE,
4282 get_color_value("TOG_COLOR_DATE"));
4287 if (log_view && view_is_splitscreen(view))
4288 show_log_view(log_view); /* draw vborder */
4289 diff_view_indicate_progress(view);
4291 err = create_diff(s);
4293 view->show = show_diff_view;
4294 view->input = input_diff_view;
4295 view->reset = reset_diff_view;
4296 view->close = close_diff_view;
4297 view->search_start = search_start_diff_view;
4298 view->search_next = search_next_diff_view;
4301 close_diff_view(view);
4305 static const struct got_error *
4306 show_diff_view(struct tog_view *view)
4308 const struct got_error *err;
4309 struct tog_diff_view_state *s = &view->state.diff;
4310 char *id_str1 = NULL, *id_str2, *header;
4311 const char *label1, *label2;
4314 err = got_object_id_str(&id_str1, s->id1);
4317 label1 = s->label1 ? : id_str1;
4319 label1 = "/dev/null";
4321 err = got_object_id_str(&id_str2, s->id2);
4324 label2 = s->label2 ? : id_str2;
4326 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4327 err = got_error_from_errno("asprintf");
4335 err = draw_file(view, header);
4340 static const struct got_error *
4341 set_selected_commit(struct tog_diff_view_state *s,
4342 struct commit_queue_entry *entry)
4344 const struct got_error *err;
4345 const struct got_object_id_queue *parent_ids;
4346 struct got_commit_object *selected_commit;
4347 struct got_object_qid *pid;
4350 s->id2 = got_object_id_dup(entry->id);
4352 return got_error_from_errno("got_object_id_dup");
4354 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4357 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4359 pid = STAILQ_FIRST(parent_ids);
4360 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4361 got_object_commit_close(selected_commit);
4365 static const struct got_error *
4366 reset_diff_view(struct tog_view *view)
4368 struct tog_diff_view_state *s = &view->state.diff;
4371 wclear(view->window);
4372 s->first_displayed_line = 1;
4373 s->last_displayed_line = view->nlines;
4374 s->matched_line = 0;
4375 diff_view_indicate_progress(view);
4376 return create_diff(s);
4379 static const struct got_error *
4380 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4382 const struct got_error *err = NULL;
4383 struct tog_diff_view_state *s = &view->state.diff;
4384 struct tog_log_view_state *ls;
4385 struct commit_queue_entry *old_selected_entry;
4387 size_t linesize = 0;
4389 int i, nscroll = view->nlines - 1;
4396 view->x = MAX(view->maxx - view->ncols / 3, 0);
4401 if (view->x + view->ncols / 3 < view->maxx)
4402 view->x += 2; /* move two columns right */
4408 view->x -= MIN(view->x, 2); /* move two columns back */
4415 s->force_text_diff = !s->force_text_diff;
4417 s->ignore_whitespace = !s->ignore_whitespace;
4418 err = reset_diff_view(view);
4422 s->first_displayed_line = 1;
4431 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4437 if (s->first_displayed_line > 1)
4438 s->first_displayed_line--;
4449 if (s->first_displayed_line == 1) {
4454 while (i++ < nscroll && s->first_displayed_line > 1)
4455 s->first_displayed_line--;
4461 s->first_displayed_line++;
4478 while (!s->eof && i++ < nscroll) {
4479 linelen = getline(&line, &linesize, s->f);
4480 s->first_displayed_line++;
4481 if (linelen == -1) {
4485 err = got_ferror(s->f, GOT_ERR_IO);
4492 if (s->diff_context > 0) {
4494 s->matched_line = 0;
4495 diff_view_indicate_progress(view);
4496 err = create_diff(s);
4497 if (s->first_displayed_line + view->nlines - 1 >
4499 s->first_displayed_line = 1;
4500 s->last_displayed_line = view->nlines;
4506 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4508 s->matched_line = 0;
4509 diff_view_indicate_progress(view);
4510 err = create_diff(s);
4516 if (s->log_view == NULL) {
4520 ls = &s->log_view->state.log;
4521 old_selected_entry = ls->selected_entry;
4523 /* view->count handled in input_log_view() */
4524 err = input_log_view(NULL, s->log_view, KEY_UP);
4528 if (old_selected_entry == ls->selected_entry)
4531 err = set_selected_commit(s, ls->selected_entry);
4535 s->first_displayed_line = 1;
4536 s->last_displayed_line = view->nlines;
4537 s->matched_line = 0;
4540 diff_view_indicate_progress(view);
4541 err = create_diff(s);
4545 if (s->log_view == NULL) {
4549 ls = &s->log_view->state.log;
4550 old_selected_entry = ls->selected_entry;
4552 /* view->count handled in input_log_view() */
4553 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4557 if (old_selected_entry == ls->selected_entry)
4560 err = set_selected_commit(s, ls->selected_entry);
4564 s->first_displayed_line = 1;
4565 s->last_displayed_line = view->nlines;
4566 s->matched_line = 0;
4569 diff_view_indicate_progress(view);
4570 err = create_diff(s);
4580 static const struct got_error *
4581 cmd_diff(int argc, char *argv[])
4583 const struct got_error *error = NULL;
4584 struct got_repository *repo = NULL;
4585 struct got_worktree *worktree = NULL;
4586 struct got_object_id *id1 = NULL, *id2 = NULL;
4587 char *repo_path = NULL, *cwd = NULL;
4588 char *id_str1 = NULL, *id_str2 = NULL;
4589 char *label1 = NULL, *label2 = NULL;
4590 int diff_context = 3, ignore_whitespace = 0;
4591 int ch, force_text_diff = 0;
4593 struct tog_view *view;
4594 int *pack_fds = NULL;
4596 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4599 force_text_diff = 1;
4602 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4605 errx(1, "number of context lines is %s: %s",
4609 repo_path = realpath(optarg, NULL);
4610 if (repo_path == NULL)
4611 return got_error_from_errno2("realpath",
4613 got_path_strip_trailing_slashes(repo_path);
4616 ignore_whitespace = 1;
4628 usage_diff(); /* TODO show local worktree changes */
4629 } else if (argc == 2) {
4635 error = got_repo_pack_fds_open(&pack_fds);
4639 if (repo_path == NULL) {
4640 cwd = getcwd(NULL, 0);
4642 return got_error_from_errno("getcwd");
4643 error = got_worktree_open(&worktree, cwd);
4644 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4648 strdup(got_worktree_get_repo_path(worktree));
4650 repo_path = strdup(cwd);
4651 if (repo_path == NULL) {
4652 error = got_error_from_errno("strdup");
4657 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4663 error = apply_unveil(got_repo_get_path(repo), NULL);
4667 error = tog_load_refs(repo, 0);
4671 error = got_repo_match_object_id(&id1, &label1, id_str1,
4672 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4676 error = got_repo_match_object_id(&id2, &label2, id_str2,
4677 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4681 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4683 error = got_error_from_errno("view_open");
4686 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4687 ignore_whitespace, force_text_diff, NULL, repo);
4690 error = view_loop(view);
4697 const struct got_error *close_err = got_repo_close(repo);
4702 got_worktree_close(worktree);
4704 const struct got_error *pack_err =
4705 got_repo_pack_fds_close(pack_fds);
4718 "usage: %s blame [-c commit] [-r repository-path] path\n",
4723 struct tog_blame_line {
4725 struct got_object_id *id;
4728 static const struct got_error *
4729 draw_blame(struct tog_view *view)
4731 struct tog_blame_view_state *s = &view->state.blame;
4732 struct tog_blame *blame = &s->blame;
4733 regmatch_t *regmatch = &view->regmatch;
4734 const struct got_error *err;
4735 int lineno = 0, nprinted = 0;
4737 size_t linesize = 0;
4741 struct tog_blame_line *blame_line;
4742 struct got_object_id *prev_id = NULL;
4744 struct tog_color *tc;
4746 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4751 werase(view->window);
4753 if (asprintf(&line, "commit %s", id_str) == -1) {
4754 err = got_error_from_errno("asprintf");
4759 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4764 if (view_needs_focus_indication(view))
4765 wstandout(view->window);
4766 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4768 wattr_on(view->window,
4769 COLOR_PAIR(tc->colorpair), NULL);
4770 waddwstr(view->window, wline);
4772 wattr_off(view->window,
4773 COLOR_PAIR(tc->colorpair), NULL);
4774 if (view_needs_focus_indication(view))
4775 wstandend(view->window);
4778 if (width < view->ncols - 1)
4779 waddch(view->window, '\n');
4781 if (asprintf(&line, "[%d/%d] %s%s",
4782 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4783 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4785 return got_error_from_errno("asprintf");
4788 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4793 waddwstr(view->window, wline);
4796 if (width < view->ncols - 1)
4797 waddch(view->window, '\n');
4801 while (nprinted < view->nlines - 2) {
4802 linelen = getline(&line, &linesize, blame->f);
4803 if (linelen == -1) {
4804 if (feof(blame->f)) {
4809 return got_ferror(blame->f, GOT_ERR_IO);
4811 if (++lineno < s->first_displayed_line)
4814 /* Set view->maxx based on full line length. */
4815 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4822 view->maxx = MAX(view->maxx, width);
4824 if (view->focussed && nprinted == s->selected_line - 1)
4825 wstandout(view->window);
4827 if (blame->nlines > 0) {
4828 blame_line = &blame->lines[lineno - 1];
4829 if (blame_line->annotated && prev_id &&
4830 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4832 nprinted == s->selected_line - 1)) {
4833 waddstr(view->window, " ");
4834 } else if (blame_line->annotated) {
4836 err = got_object_id_str(&id_str,
4842 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4844 wattr_on(view->window,
4845 COLOR_PAIR(tc->colorpair), NULL);
4846 wprintw(view->window, "%.8s", id_str);
4848 wattr_off(view->window,
4849 COLOR_PAIR(tc->colorpair), NULL);
4851 prev_id = blame_line->id;
4853 waddstr(view->window, "........");
4857 waddstr(view->window, "........");
4861 if (view->focussed && nprinted == s->selected_line - 1)
4862 wstandend(view->window);
4863 waddstr(view->window, " ");
4865 if (view->ncols <= 9) {
4867 } else if (s->first_displayed_line + nprinted ==
4869 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4870 err = add_matched_line(&width, line, view->ncols - 9, 9,
4871 view->window, view->x, regmatch);
4879 err = format_line(&wline, &width, &skip, line,
4880 view->x, view->ncols - 9, 9, 1);
4885 waddwstr(view->window, &wline[skip]);
4891 if (width <= view->ncols - 1)
4892 waddch(view->window, '\n');
4893 if (++nprinted == 1)
4894 s->first_displayed_line = lineno;
4897 s->last_displayed_line = lineno;
4904 static const struct got_error *
4905 blame_cb(void *arg, int nlines, int lineno,
4906 struct got_commit_object *commit, struct got_object_id *id)
4908 const struct got_error *err = NULL;
4909 struct tog_blame_cb_args *a = arg;
4910 struct tog_blame_line *line;
4913 if (nlines != a->nlines ||
4914 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4915 return got_error(GOT_ERR_RANGE);
4917 errcode = pthread_mutex_lock(&tog_mutex);
4919 return got_error_set_errno(errcode, "pthread_mutex_lock");
4921 if (*a->quit) { /* user has quit the blame view */
4922 err = got_error(GOT_ERR_ITER_COMPLETED);
4927 goto done; /* no change in this commit */
4929 line = &a->lines[lineno - 1];
4930 if (line->annotated)
4933 line->id = got_object_id_dup(id);
4934 if (line->id == NULL) {
4935 err = got_error_from_errno("got_object_id_dup");
4938 line->annotated = 1;
4940 errcode = pthread_mutex_unlock(&tog_mutex);
4942 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4947 blame_thread(void *arg)
4949 const struct got_error *err, *close_err;
4950 struct tog_blame_thread_args *ta = arg;
4951 struct tog_blame_cb_args *a = ta->cb_args;
4952 int errcode, fd1 = -1, fd2 = -1;
4953 FILE *f1 = NULL, *f2 = NULL;
4955 fd1 = got_opentempfd();
4957 return (void *)got_error_from_errno("got_opentempfd");
4959 fd2 = got_opentempfd();
4961 err = got_error_from_errno("got_opentempfd");
4965 f1 = got_opentemp();
4967 err = (void *)got_error_from_errno("got_opentemp");
4970 f2 = got_opentemp();
4972 err = (void *)got_error_from_errno("got_opentemp");
4976 err = block_signals_used_by_main_thread();
4980 err = got_blame(ta->path, a->commit_id, ta->repo,
4981 tog_diff_algo, blame_cb, ta->cb_args,
4982 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
4983 if (err && err->code == GOT_ERR_CANCELLED)
4986 errcode = pthread_mutex_lock(&tog_mutex);
4988 err = got_error_set_errno(errcode, "pthread_mutex_lock");
4992 close_err = got_repo_close(ta->repo);
4998 errcode = pthread_mutex_unlock(&tog_mutex);
4999 if (errcode && err == NULL)
5000 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5003 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5004 err = got_error_from_errno("close");
5005 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5006 err = got_error_from_errno("close");
5007 if (f1 && fclose(f1) == EOF && err == NULL)
5008 err = got_error_from_errno("fclose");
5009 if (f2 && fclose(f2) == EOF && err == NULL)
5010 err = got_error_from_errno("fclose");
5015 static struct got_object_id *
5016 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5017 int first_displayed_line, int selected_line)
5019 struct tog_blame_line *line;
5024 line = &lines[first_displayed_line - 1 + selected_line - 1];
5025 if (!line->annotated)
5031 static const struct got_error *
5032 stop_blame(struct tog_blame *blame)
5034 const struct got_error *err = NULL;
5037 if (blame->thread) {
5039 errcode = pthread_mutex_unlock(&tog_mutex);
5041 return got_error_set_errno(errcode,
5042 "pthread_mutex_unlock");
5043 errcode = pthread_join(blame->thread, (void **)&err);
5045 return got_error_set_errno(errcode, "pthread_join");
5046 errcode = pthread_mutex_lock(&tog_mutex);
5048 return got_error_set_errno(errcode,
5049 "pthread_mutex_lock");
5050 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5052 blame->thread = NULL;
5054 if (blame->thread_args.repo) {
5055 const struct got_error *close_err;
5056 close_err = got_repo_close(blame->thread_args.repo);
5059 blame->thread_args.repo = NULL;
5062 if (fclose(blame->f) == EOF && err == NULL)
5063 err = got_error_from_errno("fclose");
5067 for (i = 0; i < blame->nlines; i++)
5068 free(blame->lines[i].id);
5070 blame->lines = NULL;
5072 free(blame->cb_args.commit_id);
5073 blame->cb_args.commit_id = NULL;
5074 if (blame->pack_fds) {
5075 const struct got_error *pack_err =
5076 got_repo_pack_fds_close(blame->pack_fds);
5079 blame->pack_fds = NULL;
5084 static const struct got_error *
5085 cancel_blame_view(void *arg)
5087 const struct got_error *err = NULL;
5091 errcode = pthread_mutex_lock(&tog_mutex);
5093 return got_error_set_errno(errcode,
5094 "pthread_mutex_unlock");
5097 err = got_error(GOT_ERR_CANCELLED);
5099 errcode = pthread_mutex_unlock(&tog_mutex);
5101 return got_error_set_errno(errcode,
5102 "pthread_mutex_lock");
5107 static const struct got_error *
5108 run_blame(struct tog_view *view)
5110 struct tog_blame_view_state *s = &view->state.blame;
5111 struct tog_blame *blame = &s->blame;
5112 const struct got_error *err = NULL;
5113 struct got_commit_object *commit = NULL;
5114 struct got_blob_object *blob = NULL;
5115 struct got_repository *thread_repo = NULL;
5116 struct got_object_id *obj_id = NULL;
5117 int obj_type, fd = -1;
5118 int *pack_fds = NULL;
5120 err = got_object_open_as_commit(&commit, s->repo,
5121 &s->blamed_commit->id);
5125 fd = got_opentempfd();
5127 err = got_error_from_errno("got_opentempfd");
5131 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5135 err = got_object_get_type(&obj_type, s->repo, obj_id);
5139 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5140 err = got_error(GOT_ERR_OBJ_TYPE);
5144 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5147 blame->f = got_opentemp();
5148 if (blame->f == NULL) {
5149 err = got_error_from_errno("got_opentemp");
5152 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5153 &blame->line_offsets, blame->f, blob);
5156 if (blame->nlines == 0) {
5157 s->blame_complete = 1;
5161 /* Don't include \n at EOF in the blame line count. */
5162 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5165 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5166 if (blame->lines == NULL) {
5167 err = got_error_from_errno("calloc");
5171 err = got_repo_pack_fds_open(&pack_fds);
5174 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5179 blame->pack_fds = pack_fds;
5180 blame->cb_args.view = view;
5181 blame->cb_args.lines = blame->lines;
5182 blame->cb_args.nlines = blame->nlines;
5183 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5184 if (blame->cb_args.commit_id == NULL) {
5185 err = got_error_from_errno("got_object_id_dup");
5188 blame->cb_args.quit = &s->done;
5190 blame->thread_args.path = s->path;
5191 blame->thread_args.repo = thread_repo;
5192 blame->thread_args.cb_args = &blame->cb_args;
5193 blame->thread_args.complete = &s->blame_complete;
5194 blame->thread_args.cancel_cb = cancel_blame_view;
5195 blame->thread_args.cancel_arg = &s->done;
5196 s->blame_complete = 0;
5198 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5199 s->first_displayed_line = 1;
5200 s->last_displayed_line = view->nlines;
5201 s->selected_line = 1;
5203 s->matched_line = 0;
5207 got_object_commit_close(commit);
5208 if (fd != -1 && close(fd) == -1 && err == NULL)
5209 err = got_error_from_errno("close");
5211 got_object_blob_close(blob);
5218 static const struct got_error *
5219 open_blame_view(struct tog_view *view, char *path,
5220 struct got_object_id *commit_id, struct got_repository *repo)
5222 const struct got_error *err = NULL;
5223 struct tog_blame_view_state *s = &view->state.blame;
5225 STAILQ_INIT(&s->blamed_commits);
5227 s->path = strdup(path);
5228 if (s->path == NULL)
5229 return got_error_from_errno("strdup");
5231 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5237 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5238 s->first_displayed_line = 1;
5239 s->last_displayed_line = view->nlines;
5240 s->selected_line = 1;
5241 s->blame_complete = 0;
5243 s->commit_id = commit_id;
5244 memset(&s->blame, 0, sizeof(s->blame));
5246 STAILQ_INIT(&s->colors);
5247 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5248 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5249 get_color_value("TOG_COLOR_COMMIT"));
5254 view->show = show_blame_view;
5255 view->input = input_blame_view;
5256 view->reset = reset_blame_view;
5257 view->close = close_blame_view;
5258 view->search_start = search_start_blame_view;
5259 view->search_next = search_next_blame_view;
5261 return run_blame(view);
5264 static const struct got_error *
5265 close_blame_view(struct tog_view *view)
5267 const struct got_error *err = NULL;
5268 struct tog_blame_view_state *s = &view->state.blame;
5270 if (s->blame.thread)
5271 err = stop_blame(&s->blame);
5273 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5274 struct got_object_qid *blamed_commit;
5275 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5276 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5277 got_object_qid_free(blamed_commit);
5281 free_colors(&s->colors);
5285 static const struct got_error *
5286 search_start_blame_view(struct tog_view *view)
5288 struct tog_blame_view_state *s = &view->state.blame;
5290 s->matched_line = 0;
5294 static const struct got_error *
5295 search_next_blame_view(struct tog_view *view)
5297 struct tog_blame_view_state *s = &view->state.blame;
5298 const struct got_error *err = NULL;
5301 size_t linesize = 0;
5304 if (!view->searching) {
5305 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5309 if (s->matched_line) {
5310 if (view->searching == TOG_SEARCH_FORWARD)
5311 lineno = s->matched_line + 1;
5313 lineno = s->matched_line - 1;
5315 lineno = s->first_displayed_line - 1 + s->selected_line;
5320 if (lineno <= 0 || lineno > s->blame.nlines) {
5321 if (s->matched_line == 0) {
5322 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5326 if (view->searching == TOG_SEARCH_FORWARD)
5329 lineno = s->blame.nlines;
5332 offset = s->blame.line_offsets[lineno - 1];
5333 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5335 return got_error_from_errno("fseeko");
5337 linelen = getline(&line, &linesize, s->blame.f);
5338 if (linelen != -1) {
5340 err = expand_tab(&exstr, line);
5343 if (match_line(exstr, &view->regex, 1,
5345 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5346 s->matched_line = lineno;
5352 if (view->searching == TOG_SEARCH_FORWARD)
5359 if (s->matched_line) {
5360 s->first_displayed_line = s->matched_line;
5361 s->selected_line = 1;
5367 static const struct got_error *
5368 show_blame_view(struct tog_view *view)
5370 const struct got_error *err = NULL;
5371 struct tog_blame_view_state *s = &view->state.blame;
5374 if (s->blame.thread == NULL && !s->blame_complete) {
5375 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5376 &s->blame.thread_args);
5378 return got_error_set_errno(errcode, "pthread_create");
5380 halfdelay(1); /* fast refresh while annotating */
5383 if (s->blame_complete)
5384 halfdelay(10); /* disable fast refresh */
5386 err = draw_blame(view);
5392 static const struct got_error *
5393 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5395 const struct got_error *err = NULL, *thread_err = NULL;
5396 struct tog_view *diff_view;
5397 struct tog_blame_view_state *s = &view->state.blame;
5398 int eos, nscroll, begin_y = 0, begin_x = 0;
5400 eos = nscroll = view->nlines - 2;
5401 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5402 view_is_splitscreen(view->child))
5410 view->x = MAX(view->maxx - view->ncols / 3, 0);
5415 if (view->x + view->ncols / 3 < view->maxx)
5416 view->x += 2; /* move two columns right */
5422 view->x -= MIN(view->x, 2); /* move two columns back */
5431 s->selected_line = 1;
5432 s->first_displayed_line = 1;
5437 if (s->blame.nlines < eos) {
5438 s->selected_line = s->blame.nlines;
5439 s->first_displayed_line = 1;
5441 s->selected_line = eos;
5442 s->first_displayed_line = s->blame.nlines - (eos - 1);
5449 if (s->selected_line > 1)
5451 else if (s->selected_line == 1 &&
5452 s->first_displayed_line > 1)
5453 s->first_displayed_line--;
5464 if (s->first_displayed_line == 1) {
5465 if (view->count > 1)
5467 s->selected_line = MAX(1, s->selected_line - nscroll);
5471 if (s->first_displayed_line > nscroll)
5472 s->first_displayed_line -= nscroll;
5474 s->first_displayed_line = 1;
5479 if (s->selected_line < eos && s->first_displayed_line +
5480 s->selected_line <= s->blame.nlines)
5482 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5483 s->first_displayed_line++;
5489 struct got_object_id *id = NULL;
5492 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5493 s->first_displayed_line, s->selected_line);
5497 struct got_commit_object *commit, *pcommit;
5498 struct got_object_qid *pid;
5499 struct got_object_id *blob_id = NULL;
5501 err = got_object_open_as_commit(&commit,
5506 got_object_commit_get_parent_ids(commit));
5508 got_object_commit_close(commit);
5511 /* Check if path history ends here. */
5512 err = got_object_open_as_commit(&pcommit,
5516 err = got_object_id_by_path(&blob_id, s->repo,
5518 got_object_commit_close(pcommit);
5520 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5522 got_object_commit_close(commit);
5525 err = got_object_get_type(&obj_type, s->repo,
5528 /* Can't blame non-blob type objects. */
5529 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5530 got_object_commit_close(commit);
5533 err = got_object_qid_alloc(&s->blamed_commit,
5535 got_object_commit_close(commit);
5537 if (got_object_id_cmp(id,
5538 &s->blamed_commit->id) == 0)
5540 err = got_object_qid_alloc(&s->blamed_commit,
5546 thread_err = stop_blame(&s->blame);
5550 STAILQ_INSERT_HEAD(&s->blamed_commits,
5551 s->blamed_commit, entry);
5552 err = run_blame(view);
5558 struct got_object_qid *first;
5561 first = STAILQ_FIRST(&s->blamed_commits);
5562 if (!got_object_id_cmp(&first->id, s->commit_id))
5565 thread_err = stop_blame(&s->blame);
5569 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5570 got_object_qid_free(s->blamed_commit);
5572 STAILQ_FIRST(&s->blamed_commits);
5573 err = run_blame(view);
5580 struct got_object_id *id = NULL;
5581 struct got_object_qid *pid;
5582 struct got_commit_object *commit = NULL;
5585 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5586 s->first_displayed_line, s->selected_line);
5589 err = got_object_open_as_commit(&commit, s->repo, id);
5592 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5593 if (view_is_parent_view(view))
5594 view_get_split(view, &begin_y, &begin_x);
5596 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5597 if (diff_view == NULL) {
5598 got_object_commit_close(commit);
5599 err = got_error_from_errno("view_open");
5602 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5603 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5604 got_object_commit_close(commit);
5606 view_close(diff_view);
5609 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5610 err = view_init_hsplit(view, begin_y);
5616 diff_view->focussed = 1;
5617 diff_view->mode = view->mode;
5618 diff_view->nlines = view->lines - begin_y;
5619 if (view_is_parent_view(view)) {
5620 err = view_close_child(view);
5623 err = view_set_child(view, diff_view);
5626 view->focus_child = 1;
5628 *new_view = diff_view;
5641 if (s->last_displayed_line >= s->blame.nlines &&
5642 s->selected_line >= MIN(s->blame.nlines,
5643 view->nlines - 2)) {
5647 if (s->last_displayed_line >= s->blame.nlines &&
5648 s->selected_line < view->nlines - 2) {
5650 MIN(nscroll, s->last_displayed_line -
5651 s->first_displayed_line - s->selected_line + 1);
5653 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5654 s->first_displayed_line += nscroll;
5656 s->first_displayed_line =
5657 s->blame.nlines - (view->nlines - 3);
5660 if (s->selected_line > view->nlines - 2) {
5661 s->selected_line = MIN(s->blame.nlines,
5669 return thread_err ? thread_err : err;
5672 static const struct got_error *
5673 reset_blame_view(struct tog_view *view)
5675 const struct got_error *err;
5676 struct tog_blame_view_state *s = &view->state.blame;
5680 err = stop_blame(&s->blame);
5684 return run_blame(view);
5687 static const struct got_error *
5688 cmd_blame(int argc, char *argv[])
5690 const struct got_error *error;
5691 struct got_repository *repo = NULL;
5692 struct got_worktree *worktree = NULL;
5693 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5694 char *link_target = NULL;
5695 struct got_object_id *commit_id = NULL;
5696 struct got_commit_object *commit = NULL;
5697 char *commit_id_str = NULL;
5699 struct tog_view *view;
5700 int *pack_fds = NULL;
5702 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5705 commit_id_str = optarg;
5708 repo_path = realpath(optarg, NULL);
5709 if (repo_path == NULL)
5710 return got_error_from_errno2("realpath",
5725 error = got_repo_pack_fds_open(&pack_fds);
5729 if (repo_path == NULL) {
5730 cwd = getcwd(NULL, 0);
5732 return got_error_from_errno("getcwd");
5733 error = got_worktree_open(&worktree, cwd);
5734 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5738 strdup(got_worktree_get_repo_path(worktree));
5740 repo_path = strdup(cwd);
5741 if (repo_path == NULL) {
5742 error = got_error_from_errno("strdup");
5747 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5751 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5758 error = apply_unveil(got_repo_get_path(repo), NULL);
5762 error = tog_load_refs(repo, 0);
5766 if (commit_id_str == NULL) {
5767 struct got_reference *head_ref;
5768 error = got_ref_open(&head_ref, repo, worktree ?
5769 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5772 error = got_ref_resolve(&commit_id, repo, head_ref);
5773 got_ref_close(head_ref);
5775 error = got_repo_match_object_id(&commit_id, NULL,
5776 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5781 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5783 error = got_error_from_errno("view_open");
5787 error = got_object_open_as_commit(&commit, repo, commit_id);
5791 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5796 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5801 /* Release work tree lock. */
5802 got_worktree_close(worktree);
5805 error = view_loop(view);
5813 got_object_commit_close(commit);
5815 got_worktree_close(worktree);
5817 const struct got_error *close_err = got_repo_close(repo);
5822 const struct got_error *pack_err =
5823 got_repo_pack_fds_close(pack_fds);
5831 static const struct got_error *
5832 draw_tree_entries(struct tog_view *view, const char *parent_path)
5834 struct tog_tree_view_state *s = &view->state.tree;
5835 const struct got_error *err = NULL;
5836 struct got_tree_entry *te;
5838 struct tog_color *tc;
5839 int width, n, i, nentries;
5840 int limit = view->nlines;
5843 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5844 view_is_splitscreen(view->child))
5845 --limit; /* border */
5847 werase(view->window);
5852 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5856 if (view_needs_focus_indication(view))
5857 wstandout(view->window);
5858 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5860 wattr_on(view->window,
5861 COLOR_PAIR(tc->colorpair), NULL);
5862 waddwstr(view->window, wline);
5864 wattr_off(view->window,
5865 COLOR_PAIR(tc->colorpair), NULL);
5866 if (view_needs_focus_indication(view))
5867 wstandend(view->window);
5870 if (width < view->ncols - 1)
5871 waddch(view->window, '\n');
5874 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5878 waddwstr(view->window, wline);
5881 if (width < view->ncols - 1)
5882 waddch(view->window, '\n');
5885 waddch(view->window, '\n');
5889 if (s->first_displayed_entry == NULL) {
5890 te = got_object_tree_get_first_entry(s->tree);
5891 if (s->selected == 0) {
5893 wstandout(view->window);
5894 s->selected_entry = NULL;
5896 waddstr(view->window, " ..\n"); /* parent directory */
5897 if (s->selected == 0 && view->focussed)
5898 wstandend(view->window);
5905 te = s->first_displayed_entry;
5908 nentries = got_object_tree_get_nentries(s->tree);
5909 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5910 char *line = NULL, *id_str = NULL, *link_target = NULL;
5911 const char *modestr = "";
5914 te = got_object_tree_get_entry(s->tree, i);
5915 mode = got_tree_entry_get_mode(te);
5918 err = got_object_id_str(&id_str,
5919 got_tree_entry_get_id(te));
5921 return got_error_from_errno(
5922 "got_object_id_str");
5924 if (got_object_tree_entry_is_submodule(te))
5926 else if (S_ISLNK(mode)) {
5929 err = got_tree_entry_get_symlink_target(&link_target,
5935 for (i = 0; i < strlen(link_target); i++) {
5936 if (!isprint((unsigned char)link_target[i]))
5937 link_target[i] = '?';
5941 else if (S_ISDIR(mode))
5943 else if (mode & S_IXUSR)
5945 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5946 got_tree_entry_get_name(te), modestr,
5947 link_target ? " -> ": "",
5948 link_target ? link_target : "") == -1) {
5951 return got_error_from_errno("asprintf");
5955 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5961 if (n == s->selected) {
5963 wstandout(view->window);
5964 s->selected_entry = te;
5966 tc = match_color(&s->colors, line);
5968 wattr_on(view->window,
5969 COLOR_PAIR(tc->colorpair), NULL);
5970 waddwstr(view->window, wline);
5972 wattr_off(view->window,
5973 COLOR_PAIR(tc->colorpair), NULL);
5974 if (width < view->ncols - 1)
5975 waddch(view->window, '\n');
5976 if (n == s->selected && view->focussed)
5977 wstandend(view->window);
5983 s->last_displayed_entry = te;
5992 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5994 struct got_tree_entry *te;
5995 int isroot = s->tree == s->root;
5998 if (s->first_displayed_entry == NULL)
6001 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6002 while (i++ < maxscroll) {
6005 s->first_displayed_entry = NULL;
6008 s->first_displayed_entry = te;
6009 te = got_tree_entry_get_prev(s->tree, te);
6013 static const struct got_error *
6014 tree_scroll_down(struct tog_view *view, int maxscroll)
6016 struct tog_tree_view_state *s = &view->state.tree;
6017 struct got_tree_entry *next, *last;
6020 if (s->first_displayed_entry)
6021 next = got_tree_entry_get_next(s->tree,
6022 s->first_displayed_entry);
6024 next = got_object_tree_get_first_entry(s->tree);
6026 last = s->last_displayed_entry;
6027 while (next && n++ < maxscroll) {
6029 last = got_tree_entry_get_next(s->tree, last);
6030 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6031 s->first_displayed_entry = next;
6032 next = got_tree_entry_get_next(s->tree, next);
6039 static const struct got_error *
6040 tree_entry_path(char **path, struct tog_parent_trees *parents,
6041 struct got_tree_entry *te)
6043 const struct got_error *err = NULL;
6044 struct tog_parent_tree *pt;
6045 size_t len = 2; /* for leading slash and NUL */
6047 TAILQ_FOREACH(pt, parents, entry)
6048 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6051 len += strlen(got_tree_entry_get_name(te));
6053 *path = calloc(1, len);
6055 return got_error_from_errno("calloc");
6058 pt = TAILQ_LAST(parents, tog_parent_trees);
6060 const char *name = got_tree_entry_get_name(pt->selected_entry);
6061 if (strlcat(*path, name, len) >= len) {
6062 err = got_error(GOT_ERR_NO_SPACE);
6065 if (strlcat(*path, "/", len) >= len) {
6066 err = got_error(GOT_ERR_NO_SPACE);
6069 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6072 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6073 err = got_error(GOT_ERR_NO_SPACE);
6085 static const struct got_error *
6086 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6087 struct got_tree_entry *te, struct tog_parent_trees *parents,
6088 struct got_object_id *commit_id, struct got_repository *repo)
6090 const struct got_error *err = NULL;
6092 struct tog_view *blame_view;
6096 err = tree_entry_path(&path, parents, te);
6100 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6101 if (blame_view == NULL) {
6102 err = got_error_from_errno("view_open");
6106 err = open_blame_view(blame_view, path, commit_id, repo);
6108 if (err->code == GOT_ERR_CANCELLED)
6110 view_close(blame_view);
6112 *new_view = blame_view;
6118 static const struct got_error *
6119 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6120 struct tog_tree_view_state *s)
6122 struct tog_view *log_view;
6123 const struct got_error *err = NULL;
6128 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6129 if (log_view == NULL)
6130 return got_error_from_errno("view_open");
6132 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6136 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6139 view_close(log_view);
6141 *new_view = log_view;
6146 static const struct got_error *
6147 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6148 const char *head_ref_name, struct got_repository *repo)
6150 const struct got_error *err = NULL;
6151 char *commit_id_str = NULL;
6152 struct tog_tree_view_state *s = &view->state.tree;
6153 struct got_commit_object *commit = NULL;
6155 TAILQ_INIT(&s->parents);
6156 STAILQ_INIT(&s->colors);
6158 s->commit_id = got_object_id_dup(commit_id);
6159 if (s->commit_id == NULL)
6160 return got_error_from_errno("got_object_id_dup");
6162 err = got_object_open_as_commit(&commit, repo, commit_id);
6167 * The root is opened here and will be closed when the view is closed.
6168 * Any visited subtrees and their path-wise parents are opened and
6171 err = got_object_open_as_tree(&s->root, repo,
6172 got_object_commit_get_tree_id(commit));
6177 err = got_object_id_str(&commit_id_str, commit_id);
6181 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6182 err = got_error_from_errno("asprintf");
6186 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6187 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6188 if (head_ref_name) {
6189 s->head_ref_name = strdup(head_ref_name);
6190 if (s->head_ref_name == NULL) {
6191 err = got_error_from_errno("strdup");
6197 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6198 err = add_color(&s->colors, "\\$$",
6199 TOG_COLOR_TREE_SUBMODULE,
6200 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6203 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6204 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6207 err = add_color(&s->colors, "/$",
6208 TOG_COLOR_TREE_DIRECTORY,
6209 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6213 err = add_color(&s->colors, "\\*$",
6214 TOG_COLOR_TREE_EXECUTABLE,
6215 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6219 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6220 get_color_value("TOG_COLOR_COMMIT"));
6225 view->show = show_tree_view;
6226 view->input = input_tree_view;
6227 view->close = close_tree_view;
6228 view->search_start = search_start_tree_view;
6229 view->search_next = search_next_tree_view;
6231 free(commit_id_str);
6233 got_object_commit_close(commit);
6235 close_tree_view(view);
6239 static const struct got_error *
6240 close_tree_view(struct tog_view *view)
6242 struct tog_tree_view_state *s = &view->state.tree;
6244 free_colors(&s->colors);
6245 free(s->tree_label);
6246 s->tree_label = NULL;
6248 s->commit_id = NULL;
6249 free(s->head_ref_name);
6250 s->head_ref_name = NULL;
6251 while (!TAILQ_EMPTY(&s->parents)) {
6252 struct tog_parent_tree *parent;
6253 parent = TAILQ_FIRST(&s->parents);
6254 TAILQ_REMOVE(&s->parents, parent, entry);
6255 if (parent->tree != s->root)
6256 got_object_tree_close(parent->tree);
6260 if (s->tree != NULL && s->tree != s->root)
6261 got_object_tree_close(s->tree);
6263 got_object_tree_close(s->root);
6267 static const struct got_error *
6268 search_start_tree_view(struct tog_view *view)
6270 struct tog_tree_view_state *s = &view->state.tree;
6272 s->matched_entry = NULL;
6277 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6279 regmatch_t regmatch;
6281 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6285 static const struct got_error *
6286 search_next_tree_view(struct tog_view *view)
6288 struct tog_tree_view_state *s = &view->state.tree;
6289 struct got_tree_entry *te = NULL;
6291 if (!view->searching) {
6292 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6296 if (s->matched_entry) {
6297 if (view->searching == TOG_SEARCH_FORWARD) {
6298 if (s->selected_entry)
6299 te = got_tree_entry_get_next(s->tree,
6302 te = got_object_tree_get_first_entry(s->tree);
6304 if (s->selected_entry == NULL)
6305 te = got_object_tree_get_last_entry(s->tree);
6307 te = got_tree_entry_get_prev(s->tree,
6311 if (s->selected_entry)
6312 te = s->selected_entry;
6313 else if (view->searching == TOG_SEARCH_FORWARD)
6314 te = got_object_tree_get_first_entry(s->tree);
6316 te = got_object_tree_get_last_entry(s->tree);
6321 if (s->matched_entry == NULL) {
6322 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6325 if (view->searching == TOG_SEARCH_FORWARD)
6326 te = got_object_tree_get_first_entry(s->tree);
6328 te = got_object_tree_get_last_entry(s->tree);
6331 if (match_tree_entry(te, &view->regex)) {
6332 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6333 s->matched_entry = te;
6337 if (view->searching == TOG_SEARCH_FORWARD)
6338 te = got_tree_entry_get_next(s->tree, te);
6340 te = got_tree_entry_get_prev(s->tree, te);
6343 if (s->matched_entry) {
6344 s->first_displayed_entry = s->matched_entry;
6351 static const struct got_error *
6352 show_tree_view(struct tog_view *view)
6354 const struct got_error *err = NULL;
6355 struct tog_tree_view_state *s = &view->state.tree;
6358 err = tree_entry_path(&parent_path, &s->parents, NULL);
6362 err = draw_tree_entries(view, parent_path);
6369 static const struct got_error *
6370 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6372 const struct got_error *err = NULL;
6373 struct tog_tree_view_state *s = &view->state.tree;
6374 struct tog_view *log_view, *ref_view;
6375 struct got_tree_entry *te;
6376 int begin_x = 0, n, nscroll = view->nlines - 3;
6380 s->show_ids = !s->show_ids;
6385 if (!s->selected_entry)
6387 if (view_is_parent_view(view))
6388 begin_x = view_split_begin_x(view->begin_x);
6389 err = log_selected_tree_entry(&log_view, begin_x, s);
6391 log_view->focussed = 1;
6392 if (view_is_parent_view(view)) {
6393 err = view_close_child(view);
6396 err = view_set_child(view, log_view);
6399 view->focus_child = 1;
6401 *new_view = log_view;
6405 if (view_is_parent_view(view))
6406 begin_x = view_split_begin_x(view->begin_x);
6407 ref_view = view_open(view->nlines, view->ncols,
6408 view->begin_y, begin_x, TOG_VIEW_REF);
6409 if (ref_view == NULL)
6410 return got_error_from_errno("view_open");
6411 err = open_ref_view(ref_view, s->repo);
6413 view_close(ref_view);
6417 ref_view->focussed = 1;
6418 if (view_is_parent_view(view)) {
6419 err = view_close_child(view);
6422 err = view_set_child(view, ref_view);
6425 view->focus_child = 1;
6427 *new_view = ref_view;
6433 if (s->tree == s->root)
6434 s->first_displayed_entry =
6435 got_object_tree_get_first_entry(s->tree);
6437 s->first_displayed_entry = NULL;
6441 int eos = view->nlines - 3;
6443 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6447 te = got_object_tree_get_last_entry(s->tree);
6448 for (n = 0; n < eos; n++) {
6450 if(s->tree != s->root) {
6451 s->first_displayed_entry = NULL;
6456 s->first_displayed_entry = te;
6457 te = got_tree_entry_get_prev(s->tree, te);
6460 s->selected = n - 1;
6466 if (s->selected > 0) {
6470 tree_scroll_up(s, 1);
6471 if (s->selected_entry == NULL ||
6472 (s->tree == s->root && s->selected_entry ==
6473 got_object_tree_get_first_entry(s->tree)))
6483 if (s->tree == s->root) {
6484 if (got_object_tree_get_first_entry(s->tree) ==
6485 s->first_displayed_entry)
6486 s->selected -= MIN(s->selected, nscroll);
6488 if (s->first_displayed_entry == NULL)
6489 s->selected -= MIN(s->selected, nscroll);
6491 tree_scroll_up(s, MAX(0, nscroll));
6492 if (s->selected_entry == NULL ||
6493 (s->tree == s->root && s->selected_entry ==
6494 got_object_tree_get_first_entry(s->tree)))
6500 if (s->selected < s->ndisplayed - 1) {
6504 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6506 /* can't scroll any further */
6510 tree_scroll_down(view, 1);
6520 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6522 /* can't scroll any further; move cursor down */
6523 if (s->selected < s->ndisplayed - 1)
6524 s->selected += MIN(nscroll,
6525 s->ndisplayed - s->selected - 1);
6530 tree_scroll_down(view, nscroll);
6535 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6536 struct tog_parent_tree *parent;
6537 /* user selected '..' */
6538 if (s->tree == s->root) {
6542 parent = TAILQ_FIRST(&s->parents);
6543 TAILQ_REMOVE(&s->parents, parent,
6545 got_object_tree_close(s->tree);
6546 s->tree = parent->tree;
6547 s->first_displayed_entry =
6548 parent->first_displayed_entry;
6550 parent->selected_entry;
6551 s->selected = parent->selected;
6552 if (s->selected > view->nlines - 3) {
6553 err = offset_selection_down(view);
6558 } else if (S_ISDIR(got_tree_entry_get_mode(
6559 s->selected_entry))) {
6560 struct got_tree_object *subtree;
6562 err = got_object_open_as_tree(&subtree, s->repo,
6563 got_tree_entry_get_id(s->selected_entry));
6566 err = tree_view_visit_subtree(s, subtree);
6568 got_object_tree_close(subtree);
6571 } else if (S_ISREG(got_tree_entry_get_mode(
6572 s->selected_entry))) {
6573 struct tog_view *blame_view;
6574 int begin_x = 0, begin_y = 0;
6576 if (view_is_parent_view(view))
6577 view_get_split(view, &begin_y, &begin_x);
6579 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6580 s->selected_entry, &s->parents,
6581 s->commit_id, s->repo);
6585 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6586 err = view_init_hsplit(view, begin_y);
6593 blame_view->focussed = 1;
6594 blame_view->mode = view->mode;
6595 blame_view->nlines = view->lines - begin_y;
6596 if (view_is_parent_view(view)) {
6597 err = view_close_child(view);
6600 err = view_set_child(view, blame_view);
6603 view->focus_child = 1;
6605 *new_view = blame_view;
6609 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6610 s->selected = view->nlines - 4;
6626 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6631 static const struct got_error *
6632 cmd_tree(int argc, char *argv[])
6634 const struct got_error *error;
6635 struct got_repository *repo = NULL;
6636 struct got_worktree *worktree = NULL;
6637 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6638 struct got_object_id *commit_id = NULL;
6639 struct got_commit_object *commit = NULL;
6640 const char *commit_id_arg = NULL;
6642 struct got_reference *ref = NULL;
6643 const char *head_ref_name = NULL;
6645 struct tog_view *view;
6646 int *pack_fds = NULL;
6648 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6651 commit_id_arg = optarg;
6654 repo_path = realpath(optarg, NULL);
6655 if (repo_path == NULL)
6656 return got_error_from_errno2("realpath",
6671 error = got_repo_pack_fds_open(&pack_fds);
6675 if (repo_path == NULL) {
6676 cwd = getcwd(NULL, 0);
6678 return got_error_from_errno("getcwd");
6679 error = got_worktree_open(&worktree, cwd);
6680 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6684 strdup(got_worktree_get_repo_path(worktree));
6686 repo_path = strdup(cwd);
6687 if (repo_path == NULL) {
6688 error = got_error_from_errno("strdup");
6693 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6697 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6704 error = apply_unveil(got_repo_get_path(repo), NULL);
6708 error = tog_load_refs(repo, 0);
6712 if (commit_id_arg == NULL) {
6713 error = got_repo_match_object_id(&commit_id, &label,
6714 worktree ? got_worktree_get_head_ref_name(worktree) :
6715 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6718 head_ref_name = label;
6720 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6722 head_ref_name = got_ref_get_name(ref);
6723 else if (error->code != GOT_ERR_NOT_REF)
6725 error = got_repo_match_object_id(&commit_id, NULL,
6726 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6731 error = got_object_open_as_commit(&commit, repo, commit_id);
6735 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6737 error = got_error_from_errno("view_open");
6740 error = open_tree_view(view, commit_id, head_ref_name, repo);
6743 if (!got_path_is_root_dir(in_repo_path)) {
6744 error = tree_view_walk_path(&view->state.tree, commit,
6751 /* Release work tree lock. */
6752 got_worktree_close(worktree);
6755 error = view_loop(view);
6764 const struct got_error *close_err = got_repo_close(repo);
6769 const struct got_error *pack_err =
6770 got_repo_pack_fds_close(pack_fds);
6778 static const struct got_error *
6779 ref_view_load_refs(struct tog_ref_view_state *s)
6781 struct got_reflist_entry *sre;
6782 struct tog_reflist_entry *re;
6785 TAILQ_FOREACH(sre, &tog_refs, entry) {
6786 if (strncmp(got_ref_get_name(sre->ref),
6787 "refs/got/", 9) == 0 &&
6788 strncmp(got_ref_get_name(sre->ref),
6789 "refs/got/backup/", 16) != 0)
6792 re = malloc(sizeof(*re));
6794 return got_error_from_errno("malloc");
6796 re->ref = got_ref_dup(sre->ref);
6797 if (re->ref == NULL)
6798 return got_error_from_errno("got_ref_dup");
6799 re->idx = s->nrefs++;
6800 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6803 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6808 ref_view_free_refs(struct tog_ref_view_state *s)
6810 struct tog_reflist_entry *re;
6812 while (!TAILQ_EMPTY(&s->refs)) {
6813 re = TAILQ_FIRST(&s->refs);
6814 TAILQ_REMOVE(&s->refs, re, entry);
6815 got_ref_close(re->ref);
6820 static const struct got_error *
6821 open_ref_view(struct tog_view *view, struct got_repository *repo)
6823 const struct got_error *err = NULL;
6824 struct tog_ref_view_state *s = &view->state.ref;
6826 s->selected_entry = 0;
6829 TAILQ_INIT(&s->refs);
6830 STAILQ_INIT(&s->colors);
6832 err = ref_view_load_refs(s);
6836 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6837 err = add_color(&s->colors, "^refs/heads/",
6838 TOG_COLOR_REFS_HEADS,
6839 get_color_value("TOG_COLOR_REFS_HEADS"));
6843 err = add_color(&s->colors, "^refs/tags/",
6844 TOG_COLOR_REFS_TAGS,
6845 get_color_value("TOG_COLOR_REFS_TAGS"));
6849 err = add_color(&s->colors, "^refs/remotes/",
6850 TOG_COLOR_REFS_REMOTES,
6851 get_color_value("TOG_COLOR_REFS_REMOTES"));
6855 err = add_color(&s->colors, "^refs/got/backup/",
6856 TOG_COLOR_REFS_BACKUP,
6857 get_color_value("TOG_COLOR_REFS_BACKUP"));
6862 view->show = show_ref_view;
6863 view->input = input_ref_view;
6864 view->close = close_ref_view;
6865 view->search_start = search_start_ref_view;
6866 view->search_next = search_next_ref_view;
6869 free_colors(&s->colors);
6873 static const struct got_error *
6874 close_ref_view(struct tog_view *view)
6876 struct tog_ref_view_state *s = &view->state.ref;
6878 ref_view_free_refs(s);
6879 free_colors(&s->colors);
6884 static const struct got_error *
6885 resolve_reflist_entry(struct got_object_id **commit_id,
6886 struct tog_reflist_entry *re, struct got_repository *repo)
6888 const struct got_error *err = NULL;
6889 struct got_object_id *obj_id;
6890 struct got_tag_object *tag = NULL;
6895 err = got_ref_resolve(&obj_id, repo, re->ref);
6899 err = got_object_get_type(&obj_type, repo, obj_id);
6904 case GOT_OBJ_TYPE_COMMIT:
6905 *commit_id = obj_id;
6907 case GOT_OBJ_TYPE_TAG:
6908 err = got_object_open_as_tag(&tag, repo, obj_id);
6912 err = got_object_get_type(&obj_type, repo,
6913 got_object_tag_get_object_id(tag));
6916 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6917 err = got_error(GOT_ERR_OBJ_TYPE);
6920 *commit_id = got_object_id_dup(
6921 got_object_tag_get_object_id(tag));
6922 if (*commit_id == NULL) {
6923 err = got_error_from_errno("got_object_id_dup");
6928 err = got_error(GOT_ERR_OBJ_TYPE);
6934 got_object_tag_close(tag);
6942 static const struct got_error *
6943 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6944 struct tog_reflist_entry *re, struct got_repository *repo)
6946 struct tog_view *log_view;
6947 const struct got_error *err = NULL;
6948 struct got_object_id *commit_id = NULL;
6952 err = resolve_reflist_entry(&commit_id, re, repo);
6954 if (err->code != GOT_ERR_OBJ_TYPE)
6960 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6961 if (log_view == NULL) {
6962 err = got_error_from_errno("view_open");
6966 err = open_log_view(log_view, commit_id, repo,
6967 got_ref_get_name(re->ref), "", 0);
6970 view_close(log_view);
6972 *new_view = log_view;
6978 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6980 struct tog_reflist_entry *re;
6983 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6986 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6987 while (i++ < maxscroll) {
6990 s->first_displayed_entry = re;
6991 re = TAILQ_PREV(re, tog_reflist_head, entry);
6995 static const struct got_error *
6996 ref_scroll_down(struct tog_view *view, int maxscroll)
6998 struct tog_ref_view_state *s = &view->state.ref;
6999 struct tog_reflist_entry *next, *last;
7002 if (s->first_displayed_entry)
7003 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7005 next = TAILQ_FIRST(&s->refs);
7007 last = s->last_displayed_entry;
7008 while (next && n++ < maxscroll) {
7010 last = TAILQ_NEXT(last, entry);
7011 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7012 s->first_displayed_entry = next;
7013 next = TAILQ_NEXT(next, entry);
7020 static const struct got_error *
7021 search_start_ref_view(struct tog_view *view)
7023 struct tog_ref_view_state *s = &view->state.ref;
7025 s->matched_entry = NULL;
7030 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7032 regmatch_t regmatch;
7034 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7038 static const struct got_error *
7039 search_next_ref_view(struct tog_view *view)
7041 struct tog_ref_view_state *s = &view->state.ref;
7042 struct tog_reflist_entry *re = NULL;
7044 if (!view->searching) {
7045 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7049 if (s->matched_entry) {
7050 if (view->searching == TOG_SEARCH_FORWARD) {
7051 if (s->selected_entry)
7052 re = TAILQ_NEXT(s->selected_entry, entry);
7054 re = TAILQ_PREV(s->selected_entry,
7055 tog_reflist_head, entry);
7057 if (s->selected_entry == NULL)
7058 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7060 re = TAILQ_PREV(s->selected_entry,
7061 tog_reflist_head, entry);
7064 if (s->selected_entry)
7065 re = s->selected_entry;
7066 else if (view->searching == TOG_SEARCH_FORWARD)
7067 re = TAILQ_FIRST(&s->refs);
7069 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7074 if (s->matched_entry == NULL) {
7075 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7078 if (view->searching == TOG_SEARCH_FORWARD)
7079 re = TAILQ_FIRST(&s->refs);
7081 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7084 if (match_reflist_entry(re, &view->regex)) {
7085 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7086 s->matched_entry = re;
7090 if (view->searching == TOG_SEARCH_FORWARD)
7091 re = TAILQ_NEXT(re, entry);
7093 re = TAILQ_PREV(re, tog_reflist_head, entry);
7096 if (s->matched_entry) {
7097 s->first_displayed_entry = s->matched_entry;
7104 static const struct got_error *
7105 show_ref_view(struct tog_view *view)
7107 const struct got_error *err = NULL;
7108 struct tog_ref_view_state *s = &view->state.ref;
7109 struct tog_reflist_entry *re;
7112 struct tog_color *tc;
7114 int limit = view->nlines;
7116 werase(view->window);
7119 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7120 view_is_splitscreen(view->child))
7121 --limit; /* border */
7126 re = s->first_displayed_entry;
7128 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7130 return got_error_from_errno("asprintf");
7132 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7137 if (view_needs_focus_indication(view))
7138 wstandout(view->window);
7139 waddwstr(view->window, wline);
7140 if (view_needs_focus_indication(view))
7141 wstandend(view->window);
7146 if (width < view->ncols - 1)
7147 waddch(view->window, '\n');
7152 while (re && limit > 0) {
7154 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7157 struct got_commit_object *ci;
7158 struct got_tag_object *tag;
7159 struct got_object_id *id;
7163 err = got_ref_resolve(&id, s->repo, re->ref);
7166 err = got_object_open_as_tag(&tag, s->repo, id);
7168 if (err->code != GOT_ERR_OBJ_TYPE) {
7172 err = got_object_open_as_commit(&ci, s->repo,
7178 t = got_object_commit_get_committer_time(ci);
7179 got_object_commit_close(ci);
7181 t = got_object_tag_get_tagger_time(tag);
7182 got_object_tag_close(tag);
7185 if (gmtime_r(&t, &tm) == NULL)
7186 return got_error_from_errno("gmtime_r");
7187 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7188 return got_error(GOT_ERR_NO_SPACE);
7190 if (got_ref_is_symbolic(re->ref)) {
7191 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7192 ymd : "", got_ref_get_name(re->ref),
7193 got_ref_get_symref_target(re->ref)) == -1)
7194 return got_error_from_errno("asprintf");
7195 } else if (s->show_ids) {
7196 struct got_object_id *id;
7198 err = got_ref_resolve(&id, s->repo, re->ref);
7201 err = got_object_id_str(&id_str, id);
7206 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7207 got_ref_get_name(re->ref), id_str) == -1) {
7208 err = got_error_from_errno("asprintf");
7215 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7216 got_ref_get_name(re->ref)) == -1)
7217 return got_error_from_errno("asprintf");
7219 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7225 if (n == s->selected) {
7227 wstandout(view->window);
7228 s->selected_entry = re;
7230 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7232 wattr_on(view->window,
7233 COLOR_PAIR(tc->colorpair), NULL);
7234 waddwstr(view->window, wline);
7236 wattr_off(view->window,
7237 COLOR_PAIR(tc->colorpair), NULL);
7238 if (width < view->ncols - 1)
7239 waddch(view->window, '\n');
7240 if (n == s->selected && view->focussed)
7241 wstandend(view->window);
7247 s->last_displayed_entry = re;
7250 re = TAILQ_NEXT(re, entry);
7257 static const struct got_error *
7258 browse_ref_tree(struct tog_view **new_view, int begin_x,
7259 struct tog_reflist_entry *re, struct got_repository *repo)
7261 const struct got_error *err = NULL;
7262 struct got_object_id *commit_id = NULL;
7263 struct tog_view *tree_view;
7267 err = resolve_reflist_entry(&commit_id, re, repo);
7269 if (err->code != GOT_ERR_OBJ_TYPE)
7276 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7277 if (tree_view == NULL) {
7278 err = got_error_from_errno("view_open");
7282 err = open_tree_view(tree_view, commit_id,
7283 got_ref_get_name(re->ref), repo);
7287 *new_view = tree_view;
7292 static const struct got_error *
7293 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7295 const struct got_error *err = NULL;
7296 struct tog_ref_view_state *s = &view->state.ref;
7297 struct tog_view *log_view, *tree_view;
7298 struct tog_reflist_entry *re;
7299 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7303 s->show_ids = !s->show_ids;
7307 s->show_date = !s->show_date;
7311 s->sort_by_date = !s->sort_by_date;
7313 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7314 got_ref_cmp_by_commit_timestamp_descending :
7315 tog_ref_cmp_by_name, s->repo);
7318 got_reflist_object_id_map_free(tog_refs_idmap);
7319 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7320 &tog_refs, s->repo);
7323 ref_view_free_refs(s);
7324 err = ref_view_load_refs(s);
7329 if (!s->selected_entry)
7331 if (view_is_parent_view(view))
7332 view_get_split(view, &begin_y, &begin_x);
7334 err = log_ref_entry(&log_view, begin_y, begin_x,
7335 s->selected_entry, s->repo);
7339 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7340 err = view_init_hsplit(view, begin_y);
7346 log_view->focussed = 1;
7347 log_view->mode = view->mode;
7348 log_view->nlines = view->lines - begin_y;
7349 if (view_is_parent_view(view)) {
7350 err = view_close_child(view);
7353 err = view_set_child(view, log_view);
7356 view->focus_child = 1;
7358 *new_view = log_view;
7362 if (!s->selected_entry)
7364 if (view_is_parent_view(view))
7365 begin_x = view_split_begin_x(view->begin_x);
7366 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7368 if (err || tree_view == NULL)
7371 tree_view->focussed = 1;
7372 if (view_is_parent_view(view)) {
7373 err = view_close_child(view);
7376 err = view_set_child(view, tree_view);
7379 view->focus_child = 1;
7381 *new_view = tree_view;
7387 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7391 int eos = view->nlines - 1;
7393 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7397 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7398 for (n = 0; n < eos; n++) {
7401 s->first_displayed_entry = re;
7402 re = TAILQ_PREV(re, tog_reflist_head, entry);
7405 s->selected = n - 1;
7411 if (s->selected > 0) {
7415 ref_scroll_up(s, 1);
7416 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7426 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7427 s->selected -= MIN(nscroll, s->selected);
7428 ref_scroll_up(s, MAX(0, nscroll));
7429 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7435 if (s->selected < s->ndisplayed - 1) {
7439 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7440 /* can't scroll any further */
7444 ref_scroll_down(view, 1);
7454 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7455 /* can't scroll any further; move cursor down */
7456 if (s->selected < s->ndisplayed - 1)
7457 s->selected += MIN(nscroll,
7458 s->ndisplayed - s->selected - 1);
7459 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7460 s->selected += s->ndisplayed - s->selected - 1;
7464 ref_scroll_down(view, nscroll);
7469 err = tog_load_refs(s->repo, s->sort_by_date);
7472 ref_view_free_refs(s);
7473 err = ref_view_load_refs(s);
7476 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7477 s->selected = view->nlines - 2;
7491 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7496 static const struct got_error *
7497 cmd_ref(int argc, char *argv[])
7499 const struct got_error *error;
7500 struct got_repository *repo = NULL;
7501 struct got_worktree *worktree = NULL;
7502 char *cwd = NULL, *repo_path = NULL;
7504 struct tog_view *view;
7505 int *pack_fds = NULL;
7507 while ((ch = getopt(argc, argv, "r:")) != -1) {
7510 repo_path = realpath(optarg, NULL);
7511 if (repo_path == NULL)
7512 return got_error_from_errno2("realpath",
7527 error = got_repo_pack_fds_open(&pack_fds);
7531 if (repo_path == NULL) {
7532 cwd = getcwd(NULL, 0);
7534 return got_error_from_errno("getcwd");
7535 error = got_worktree_open(&worktree, cwd);
7536 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7540 strdup(got_worktree_get_repo_path(worktree));
7542 repo_path = strdup(cwd);
7543 if (repo_path == NULL) {
7544 error = got_error_from_errno("strdup");
7549 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7555 error = apply_unveil(got_repo_get_path(repo), NULL);
7559 error = tog_load_refs(repo, 0);
7563 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7565 error = got_error_from_errno("view_open");
7569 error = open_ref_view(view, repo);
7574 /* Release work tree lock. */
7575 got_worktree_close(worktree);
7578 error = view_loop(view);
7583 const struct got_error *close_err = got_repo_close(repo);
7588 const struct got_error *pack_err =
7589 got_repo_pack_fds_close(pack_fds);
7598 * If view was scrolled down to move the selected line into view when opening a
7599 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7602 offset_selection_up(struct tog_view *view)
7604 switch (view->type) {
7605 case TOG_VIEW_BLAME: {
7606 struct tog_blame_view_state *s = &view->state.blame;
7607 if (s->first_displayed_line == 1) {
7608 s->selected_line = MAX(s->selected_line - view->offset,
7612 if (s->first_displayed_line > view->offset)
7613 s->first_displayed_line -= view->offset;
7615 s->first_displayed_line = 1;
7616 s->selected_line += view->offset;
7620 log_scroll_up(&view->state.log, view->offset);
7621 view->state.log.selected += view->offset;
7624 ref_scroll_up(&view->state.ref, view->offset);
7625 view->state.ref.selected += view->offset;
7628 tree_scroll_up(&view->state.tree, view->offset);
7629 view->state.tree.selected += view->offset;
7639 * If the selected line is in the section of screen covered by the bottom split,
7640 * scroll down offset lines to move it into view and index its new position.
7642 static const struct got_error *
7643 offset_selection_down(struct tog_view *view)
7645 const struct got_error *err = NULL;
7646 const struct got_error *(*scrolld)(struct tog_view *, int);
7647 int *selected = NULL;
7650 switch (view->type) {
7651 case TOG_VIEW_BLAME: {
7652 struct tog_blame_view_state *s = &view->state.blame;
7655 if (s->selected_line > view->nlines - header) {
7656 offset = abs(view->nlines - s->selected_line - header);
7657 s->first_displayed_line += offset;
7658 s->selected_line -= offset;
7659 view->offset = offset;
7663 case TOG_VIEW_LOG: {
7664 struct tog_log_view_state *s = &view->state.log;
7665 scrolld = &log_scroll_down;
7667 selected = &s->selected;
7670 case TOG_VIEW_REF: {
7671 struct tog_ref_view_state *s = &view->state.ref;
7672 scrolld = &ref_scroll_down;
7674 selected = &s->selected;
7677 case TOG_VIEW_TREE: {
7678 struct tog_tree_view_state *s = &view->state.tree;
7679 scrolld = &tree_scroll_down;
7681 selected = &s->selected;
7691 if (selected && *selected > view->nlines - header) {
7692 offset = abs(view->nlines - *selected - header);
7693 view->offset = offset;
7694 if (scrolld && offset) {
7695 err = scrolld(view, offset);
7696 *selected -= offset;
7704 list_commands(FILE *fp)
7708 fprintf(fp, "commands:");
7709 for (i = 0; i < nitems(tog_commands); i++) {
7710 const struct tog_cmd *cmd = &tog_commands[i];
7711 fprintf(fp, " %s", cmd->name);
7717 usage(int hflag, int status)
7719 FILE *fp = (status == 0) ? stdout : stderr;
7721 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7724 fprintf(fp, "lazy usage: %s path\n", getprogname());
7731 make_argv(int argc, ...)
7739 argv = calloc(argc, sizeof(char *));
7742 for (i = 0; i < argc; i++) {
7743 argv[i] = strdup(va_arg(ap, char *));
7744 if (argv[i] == NULL)
7753 * Try to convert 'tog path' into a 'tog log path' command.
7754 * The user could simply have mistyped the command rather than knowingly
7755 * provided a path. So check whether argv[0] can in fact be resolved
7756 * to a path in the HEAD commit and print a special error if not.
7757 * This hack is for mpi@ <3
7759 static const struct got_error *
7760 tog_log_with_path(int argc, char *argv[])
7762 const struct got_error *error = NULL, *close_err;
7763 const struct tog_cmd *cmd = NULL;
7764 struct got_repository *repo = NULL;
7765 struct got_worktree *worktree = NULL;
7766 struct got_object_id *commit_id = NULL, *id = NULL;
7767 struct got_commit_object *commit = NULL;
7768 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7769 char *commit_id_str = NULL, **cmd_argv = NULL;
7770 int *pack_fds = NULL;
7772 cwd = getcwd(NULL, 0);
7774 return got_error_from_errno("getcwd");
7776 error = got_repo_pack_fds_open(&pack_fds);
7780 error = got_worktree_open(&worktree, cwd);
7781 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7785 repo_path = strdup(got_worktree_get_repo_path(worktree));
7787 repo_path = strdup(cwd);
7788 if (repo_path == NULL) {
7789 error = got_error_from_errno("strdup");
7793 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7797 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7802 error = tog_load_refs(repo, 0);
7805 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7806 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7807 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7812 got_worktree_close(worktree);
7816 error = got_object_open_as_commit(&commit, repo, commit_id);
7820 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7822 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7824 fprintf(stderr, "%s: '%s' is no known command or path\n",
7825 getprogname(), argv[0]);
7830 close_err = got_repo_close(repo);
7835 error = got_object_id_str(&commit_id_str, commit_id);
7839 cmd = &tog_commands[0]; /* log */
7841 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7842 error = cmd->cmd_main(argc, cmd_argv);
7845 close_err = got_repo_close(repo);
7850 got_object_commit_close(commit);
7852 got_worktree_close(worktree);
7854 const struct got_error *pack_err =
7855 got_repo_pack_fds_close(pack_fds);
7860 free(commit_id_str);
7867 for (i = 0; i < argc; i++)
7876 main(int argc, char *argv[])
7878 const struct got_error *error = NULL;
7879 const struct tog_cmd *cmd = NULL;
7880 int ch, hflag = 0, Vflag = 0;
7881 char **cmd_argv = NULL;
7882 static const struct option longopts[] = {
7883 { "version", no_argument, NULL, 'V' },
7886 char *diff_algo_str = NULL;
7888 setlocale(LC_CTYPE, "");
7890 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7910 got_version_print_str();
7915 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7923 /* Build an argument vector which runs a default command. */
7924 cmd = &tog_commands[0];
7926 cmd_argv = make_argv(argc, cmd->name);
7930 /* Did the user specify a command? */
7931 for (i = 0; i < nitems(tog_commands); i++) {
7932 if (strncmp(tog_commands[i].name, argv[0],
7933 strlen(argv[0])) == 0) {
7934 cmd = &tog_commands[i];
7940 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7941 if (diff_algo_str) {
7942 if (strcasecmp(diff_algo_str, "patience") == 0)
7943 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7944 if (strcasecmp(diff_algo_str, "myers") == 0)
7945 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
7951 /* No command specified; try log with a path */
7952 error = tog_log_with_path(argc, argv);
7957 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7964 for (i = 0; i < argc; i++)
7969 if (error && error->code != GOT_ERR_CANCELLED)
7970 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);