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 input. Assign total to view->count and
1001 * return first non-numeric key entered.
1004 get_compound_key(struct tog_view *view, int c)
1006 struct tog_view *v = view;
1009 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1010 view_is_splitscreen(view->child))
1012 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1016 cbreak(); /* block for input */
1017 wmove(v->window, v->nlines - 1, 0);
1018 wclrtoeol(v->window);
1019 waddch(v->window, ':');
1022 x = getcurx(v->window);
1023 if (x != ERR && x < view->ncols) {
1024 waddch(v->window, c);
1025 wrefresh(v->window);
1029 * Don't overflow. Max valid request should be the greatest
1030 * between the longest and total lines; cap at 10 million.
1035 n = n * 10 + (c - '0');
1036 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1038 /* Massage excessive or inapplicable values at the input handler. */
1044 static const struct got_error *
1045 view_input(struct tog_view **new, int *done, struct tog_view *view,
1046 struct tog_view_list_head *views)
1048 const struct got_error *err = NULL;
1054 /* Clear "no matches" indicator. */
1055 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1056 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1057 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1061 if (view->searching && !view->search_next_done) {
1062 errcode = pthread_mutex_unlock(&tog_mutex);
1064 return got_error_set_errno(errcode,
1065 "pthread_mutex_unlock");
1067 errcode = pthread_mutex_lock(&tog_mutex);
1069 return got_error_set_errno(errcode,
1070 "pthread_mutex_lock");
1071 view->search_next(view);
1075 nodelay(stdscr, FALSE);
1076 /* Allow threads to make progress while we are waiting for input. */
1077 errcode = pthread_mutex_unlock(&tog_mutex);
1079 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1080 /* If we have an unfinished count, don't get a new key map. */
1082 if ((view->count && --view->count == 0) || !view->count) {
1083 ch = wgetch(view->window);
1084 if (ch >= '1' && ch <= '9')
1085 view->ch = ch = get_compound_key(view, ch);
1087 errcode = pthread_mutex_lock(&tog_mutex);
1089 return got_error_set_errno(errcode, "pthread_mutex_lock");
1090 nodelay(stdscr, TRUE);
1092 if (tog_sigwinch_received || tog_sigcont_received) {
1094 tog_sigwinch_received = 0;
1095 tog_sigcont_received = 0;
1096 TAILQ_FOREACH(v, views, entry) {
1097 err = view_resize(v);
1100 err = v->input(new, v, KEY_RESIZE);
1104 err = view_resize(v->child);
1107 err = v->child->input(new, v->child,
1120 view->child->focussed = 1;
1121 view->focus_child = 1;
1122 } else if (view->parent) {
1124 view->parent->focussed = 1;
1125 view->parent->focus_child = 0;
1126 if (!view_is_splitscreen(view)) {
1127 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1128 view->parent->type == TOG_VIEW_LOG) {
1129 err = request_log_commits(view->parent);
1133 offset_selection_up(view->parent);
1134 err = view_fullscreen(view->parent);
1141 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1142 if (view->parent->type == TOG_VIEW_LOG) {
1143 /* might need more commits to fill fullscreen */
1144 err = request_log_commits(view->parent);
1148 offset_selection_up(view->parent);
1149 view->parent->mode = TOG_VIEW_SPLIT_NONE;
1151 err = view->input(new, view, ch);
1159 if (view_is_parent_view(view)) {
1160 if (view->child == NULL)
1162 if (view_is_splitscreen(view->child)) {
1164 view->child->focussed = 1;
1165 err = view_fullscreen(view->child);
1167 err = view_splitscreen(view->child);
1170 err = view->child->input(new, view->child,
1173 if (view_is_splitscreen(view)) {
1174 view->parent->focussed = 0;
1176 err = view_fullscreen(view);
1178 err = view_splitscreen(view);
1179 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1180 err = view_resize(view->parent);
1184 err = view->input(new, view, KEY_RESIZE);
1188 if (view->type == TOG_VIEW_LOG) {
1189 err = request_log_commits(view);
1194 err = offset_selection_down(view->parent);
1196 err = offset_selection_down(view);
1202 if (view->search_start)
1203 view_search_start(view);
1205 err = view->input(new, view, ch);
1209 if (view->search_started && view->search_next) {
1210 view->searching = (ch == 'n' ?
1211 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1212 view->search_next_done = 0;
1213 view->search_next(view);
1215 err = view->input(new, view, ch);
1218 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1219 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1221 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1222 TAILQ_FOREACH(v, views, entry) {
1228 if (v->child && v->child->reset) {
1229 err = v->child->reset(v->child);
1236 err = view->input(new, view, ch);
1244 view_needs_focus_indication(struct tog_view *view)
1246 if (view_is_parent_view(view)) {
1247 if (view->child == NULL || view->child->focussed)
1249 if (!view_is_splitscreen(view->child))
1251 } else if (!view_is_splitscreen(view))
1254 return view->focussed;
1257 static const struct got_error *
1258 view_loop(struct tog_view *view)
1260 const struct got_error *err = NULL;
1261 struct tog_view_list_head views;
1262 struct tog_view *new_view;
1263 int fast_refresh = 10;
1264 int done = 0, errcode;
1266 errcode = pthread_mutex_lock(&tog_mutex);
1268 return got_error_set_errno(errcode, "pthread_mutex_lock");
1271 TAILQ_INSERT_HEAD(&views, view, entry);
1274 err = view->show(view);
1279 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1280 /* Refresh fast during initialization, then become slower. */
1281 if (fast_refresh && fast_refresh-- == 0)
1282 halfdelay(10); /* switch to once per second */
1284 err = view_input(&new_view, &done, view, &views);
1288 struct tog_view *v, *prev = NULL;
1290 if (view_is_parent_view(view))
1291 prev = TAILQ_PREV(view, tog_view_list_head,
1293 else if (view->parent)
1294 prev = view->parent;
1297 view->parent->child = NULL;
1298 view->parent->focus_child = 0;
1299 /* Restore fullscreen line height. */
1300 view->parent->nlines = view->parent->lines;
1301 err = view_resize(view->parent);
1305 TAILQ_REMOVE(&views, view, entry);
1307 err = view_close(view);
1312 TAILQ_FOREACH(v, &views, entry) {
1316 if (view == NULL && new_view == NULL) {
1317 /* No view has focus. Try to pick one. */
1320 else if (!TAILQ_EMPTY(&views)) {
1321 view = TAILQ_LAST(&views,
1322 tog_view_list_head);
1325 if (view->focus_child) {
1326 view->child->focussed = 1;
1334 struct tog_view *v, *t;
1335 /* Only allow one parent view per type. */
1336 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1337 if (v->type != new_view->type)
1339 TAILQ_REMOVE(&views, v, entry);
1340 err = view_close(v);
1345 TAILQ_INSERT_TAIL(&views, new_view, entry);
1349 if (view_is_parent_view(view)) {
1350 if (view->child && view->child->focussed)
1353 if (view->parent && view->parent->focussed)
1354 view = view->parent;
1356 show_panel(view->panel);
1357 if (view->child && view_is_splitscreen(view->child))
1358 show_panel(view->child->panel);
1359 if (view->parent && view_is_splitscreen(view)) {
1360 err = view->parent->show(view->parent);
1364 err = view->show(view);
1368 err = view->child->show(view->child);
1377 while (!TAILQ_EMPTY(&views)) {
1378 view = TAILQ_FIRST(&views);
1379 TAILQ_REMOVE(&views, view, entry);
1383 errcode = pthread_mutex_unlock(&tog_mutex);
1384 if (errcode && err == NULL)
1385 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1395 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1400 /* Create newly allocated wide-character string equivalent to a byte string. */
1401 static const struct got_error *
1402 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1405 const struct got_error *err = NULL;
1408 *wlen = mbstowcs(NULL, s, 0);
1409 if (*wlen == (size_t)-1) {
1411 if (errno != EILSEQ)
1412 return got_error_from_errno("mbstowcs");
1414 /* byte string invalid in current encoding; try to "fix" it */
1415 err = got_mbsavis(&vis, &vislen, s);
1418 *wlen = mbstowcs(NULL, vis, 0);
1419 if (*wlen == (size_t)-1) {
1420 err = got_error_from_errno("mbstowcs"); /* give up */
1425 *ws = calloc(*wlen + 1, sizeof(**ws));
1427 err = got_error_from_errno("calloc");
1431 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1432 err = got_error_from_errno("mbstowcs");
1443 static const struct got_error *
1444 expand_tab(char **ptr, const char *src)
1447 size_t len, n, idx = 0, sz = 0;
1450 n = len = strlen(src);
1451 dst = malloc(n + 1);
1453 return got_error_from_errno("malloc");
1455 while (idx < len && src[idx]) {
1456 const char c = src[idx];
1459 size_t nb = TABSIZE - sz % TABSIZE;
1462 p = realloc(dst, n + nb);
1465 return got_error_from_errno("realloc");
1470 memset(dst + sz, ' ', nb);
1473 dst[sz++] = src[idx];
1483 * Advance at most n columns from wline starting at offset off.
1484 * Return the index to the first character after the span operation.
1485 * Return the combined column width of all spanned wide character in
1489 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1491 int width, i, cols = 0;
1498 for (i = off; wline[i] != L'\0'; ++i) {
1499 if (wline[i] == L'\t')
1500 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1502 width = wcwidth(wline[i]);
1509 if (cols + width > n)
1519 * Format a line for display, ensuring that it won't overflow a width limit.
1520 * With scrolling, the width returned refers to the scrolled version of the
1521 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1523 static const struct got_error *
1524 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1525 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1527 const struct got_error *err = NULL;
1529 wchar_t *wline = NULL;
1538 err = expand_tab(&exstr, line);
1543 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1548 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1550 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1551 wline[wlen - 1] = L'\0';
1554 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1555 wline[wlen - 1] = L'\0';
1559 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1565 *scrollxp = scrollx;
1573 static const struct got_error*
1574 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1575 struct got_object_id *id, struct got_repository *repo)
1577 static const struct got_error *err = NULL;
1578 struct got_reflist_entry *re;
1584 TAILQ_FOREACH(re, refs, entry) {
1585 struct got_tag_object *tag = NULL;
1586 struct got_object_id *ref_id;
1589 name = got_ref_get_name(re->ref);
1590 if (strcmp(name, GOT_REF_HEAD) == 0)
1592 if (strncmp(name, "refs/", 5) == 0)
1594 if (strncmp(name, "got/", 4) == 0 &&
1595 strncmp(name, "got/backup/", 11) != 0)
1597 if (strncmp(name, "heads/", 6) == 0)
1599 if (strncmp(name, "remotes/", 8) == 0) {
1601 s = strstr(name, "/" GOT_REF_HEAD);
1602 if (s != NULL && s[strlen(s)] == '\0')
1605 err = got_ref_resolve(&ref_id, repo, re->ref);
1608 if (strncmp(name, "tags/", 5) == 0) {
1609 err = got_object_open_as_tag(&tag, repo, ref_id);
1611 if (err->code != GOT_ERR_OBJ_TYPE) {
1615 /* Ref points at something other than a tag. */
1620 cmp = got_object_id_cmp(tag ?
1621 got_object_tag_get_object_id(tag) : ref_id, id);
1624 got_object_tag_close(tag);
1628 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1629 s ? ", " : "", name) == -1) {
1630 err = got_error_from_errno("asprintf");
1641 static const struct got_error *
1642 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1647 smallerthan = strchr(author, '<');
1648 if (smallerthan && smallerthan[1] != '\0')
1649 author = smallerthan + 1;
1650 author[strcspn(author, "@>")] = '\0';
1651 return format_line(wauthor, author_width, NULL, author, 0, limit,
1655 static const struct got_error *
1656 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1657 struct got_object_id *id, const size_t date_display_cols,
1658 int author_display_cols)
1660 struct tog_log_view_state *s = &view->state.log;
1661 const struct got_error *err = NULL;
1662 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1663 char *logmsg0 = NULL, *logmsg = NULL;
1664 char *author = NULL;
1665 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1666 int author_width, logmsg_width;
1667 char *newline, *line = NULL;
1668 int col, limit, scrollx;
1669 const int avail = view->ncols;
1671 time_t committer_time;
1672 struct tog_color *tc;
1674 committer_time = got_object_commit_get_committer_time(commit);
1675 if (gmtime_r(&committer_time, &tm) == NULL)
1676 return got_error_from_errno("gmtime_r");
1677 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1678 return got_error(GOT_ERR_NO_SPACE);
1680 if (avail <= date_display_cols)
1681 limit = MIN(sizeof(datebuf) - 1, avail);
1683 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1684 tc = get_color(&s->colors, TOG_COLOR_DATE);
1686 wattr_on(view->window,
1687 COLOR_PAIR(tc->colorpair), NULL);
1688 waddnstr(view->window, datebuf, limit);
1690 wattr_off(view->window,
1691 COLOR_PAIR(tc->colorpair), NULL);
1698 err = got_object_id_str(&id_str, id);
1701 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1703 wattr_on(view->window,
1704 COLOR_PAIR(tc->colorpair), NULL);
1705 wprintw(view->window, "%.8s ", id_str);
1707 wattr_off(view->window,
1708 COLOR_PAIR(tc->colorpair), NULL);
1715 author = strdup(got_object_commit_get_author(commit));
1716 if (author == NULL) {
1717 err = got_error_from_errno("strdup");
1720 err = format_author(&wauthor, &author_width, author, avail - col, col);
1723 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1725 wattr_on(view->window,
1726 COLOR_PAIR(tc->colorpair), NULL);
1727 waddwstr(view->window, wauthor);
1729 wattr_off(view->window,
1730 COLOR_PAIR(tc->colorpair), NULL);
1731 col += author_width;
1732 while (col < avail && author_width < author_display_cols + 2) {
1733 waddch(view->window, ' ');
1740 err = got_object_commit_get_logmsg(&logmsg0, commit);
1744 while (*logmsg == '\n')
1746 newline = strchr(logmsg, '\n');
1749 limit = avail - col;
1750 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1751 limit--; /* for the border */
1752 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1756 waddwstr(view->window, &wlogmsg[scrollx]);
1757 col += MAX(logmsg_width, 0);
1758 while (col < avail) {
1759 waddch(view->window, ' ');
1771 static struct commit_queue_entry *
1772 alloc_commit_queue_entry(struct got_commit_object *commit,
1773 struct got_object_id *id)
1775 struct commit_queue_entry *entry;
1777 entry = calloc(1, sizeof(*entry));
1782 entry->commit = commit;
1787 pop_commit(struct commit_queue *commits)
1789 struct commit_queue_entry *entry;
1791 entry = TAILQ_FIRST(&commits->head);
1792 TAILQ_REMOVE(&commits->head, entry, entry);
1793 got_object_commit_close(entry->commit);
1794 commits->ncommits--;
1795 /* Don't free entry->id! It is owned by the commit graph. */
1800 free_commits(struct commit_queue *commits)
1802 while (!TAILQ_EMPTY(&commits->head))
1803 pop_commit(commits);
1806 static const struct got_error *
1807 match_commit(int *have_match, struct got_object_id *id,
1808 struct got_commit_object *commit, regex_t *regex)
1810 const struct got_error *err = NULL;
1811 regmatch_t regmatch;
1812 char *id_str = NULL, *logmsg = NULL;
1816 err = got_object_id_str(&id_str, id);
1820 err = got_object_commit_get_logmsg(&logmsg, commit);
1824 if (regexec(regex, got_object_commit_get_author(commit), 1,
1825 ®match, 0) == 0 ||
1826 regexec(regex, got_object_commit_get_committer(commit), 1,
1827 ®match, 0) == 0 ||
1828 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1829 regexec(regex, logmsg, 1, ®match, 0) == 0)
1837 static const struct got_error *
1838 queue_commits(struct tog_log_thread_args *a)
1840 const struct got_error *err = NULL;
1843 * We keep all commits open throughout the lifetime of the log
1844 * view in order to avoid having to re-fetch commits from disk
1845 * while updating the display.
1848 struct got_object_id *id;
1849 struct got_commit_object *commit;
1850 struct commit_queue_entry *entry;
1853 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1855 if (err || id == NULL)
1858 err = got_object_open_as_commit(&commit, a->repo, id);
1861 entry = alloc_commit_queue_entry(commit, id);
1862 if (entry == NULL) {
1863 err = got_error_from_errno("alloc_commit_queue_entry");
1867 errcode = pthread_mutex_lock(&tog_mutex);
1869 err = got_error_set_errno(errcode,
1870 "pthread_mutex_lock");
1874 entry->idx = a->commits->ncommits;
1875 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1876 a->commits->ncommits++;
1878 if (*a->searching == TOG_SEARCH_FORWARD &&
1879 !*a->search_next_done) {
1881 err = match_commit(&have_match, id, commit, a->regex);
1885 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1888 errcode = pthread_mutex_unlock(&tog_mutex);
1889 if (errcode && err == NULL)
1890 err = got_error_set_errno(errcode,
1891 "pthread_mutex_unlock");
1894 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1900 select_commit(struct tog_log_view_state *s)
1902 struct commit_queue_entry *entry;
1905 entry = s->first_displayed_entry;
1907 if (ncommits == s->selected) {
1908 s->selected_entry = entry;
1911 entry = TAILQ_NEXT(entry, entry);
1916 static const struct got_error *
1917 draw_commits(struct tog_view *view)
1919 const struct got_error *err = NULL;
1920 struct tog_log_view_state *s = &view->state.log;
1921 struct commit_queue_entry *entry = s->selected_entry;
1922 const int limit = view->nlines;
1924 int ncommits, author_cols = 4;
1925 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1926 char *refs_str = NULL;
1928 struct tog_color *tc;
1929 static const size_t date_display_cols = 12;
1931 if (s->selected_entry &&
1932 !(view->searching && view->search_next_done == 0)) {
1933 struct got_reflist_head *refs;
1934 err = got_object_id_str(&id_str, s->selected_entry->id);
1937 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1938 s->selected_entry->id);
1940 err = build_refs_str(&refs_str, refs,
1941 s->selected_entry->id, s->repo);
1947 if (s->thread_args.commits_needed == 0)
1948 halfdelay(10); /* disable fast refresh */
1950 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1951 if (asprintf(&ncommits_str, " [%d/%d] %s",
1952 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1953 (view->searching && !view->search_next_done) ?
1954 "searching..." : "loading...") == -1) {
1955 err = got_error_from_errno("asprintf");
1959 const char *search_str = NULL;
1961 if (view->searching) {
1962 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1963 search_str = "no more matches";
1964 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1965 search_str = "no matches found";
1966 else if (!view->search_next_done)
1967 search_str = "searching...";
1970 if (asprintf(&ncommits_str, " [%d/%d] %s",
1971 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1972 search_str ? search_str :
1973 (refs_str ? refs_str : "")) == -1) {
1974 err = got_error_from_errno("asprintf");
1979 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1980 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1981 "........................................",
1982 s->in_repo_path, ncommits_str) == -1) {
1983 err = got_error_from_errno("asprintf");
1987 } else if (asprintf(&header, "commit %s%s",
1988 id_str ? id_str : "........................................",
1989 ncommits_str) == -1) {
1990 err = got_error_from_errno("asprintf");
1994 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1998 werase(view->window);
2000 if (view_needs_focus_indication(view))
2001 wstandout(view->window);
2002 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2004 wattr_on(view->window,
2005 COLOR_PAIR(tc->colorpair), NULL);
2006 waddwstr(view->window, wline);
2008 wattr_off(view->window,
2009 COLOR_PAIR(tc->colorpair), NULL);
2010 while (width < view->ncols) {
2011 waddch(view->window, ' ');
2014 if (view_needs_focus_indication(view))
2015 wstandend(view->window);
2020 /* Grow author column size if necessary, and set view->maxx. */
2021 entry = s->first_displayed_entry;
2025 char *author, *eol, *msg, *msg0;
2026 wchar_t *wauthor, *wmsg;
2028 if (ncommits >= limit - 1)
2030 author = strdup(got_object_commit_get_author(entry->commit));
2031 if (author == NULL) {
2032 err = got_error_from_errno("strdup");
2035 err = format_author(&wauthor, &width, author, COLS,
2037 if (author_cols < width)
2038 author_cols = width;
2041 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2045 while (*msg == '\n')
2047 if ((eol = strchr(msg, '\n')))
2049 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2050 date_display_cols + author_cols, 0);
2053 view->maxx = MAX(view->maxx, width);
2057 entry = TAILQ_NEXT(entry, entry);
2060 entry = s->first_displayed_entry;
2061 s->last_displayed_entry = s->first_displayed_entry;
2064 if (ncommits >= limit - 1)
2066 if (ncommits == s->selected)
2067 wstandout(view->window);
2068 err = draw_commit(view, entry->commit, entry->id,
2069 date_display_cols, author_cols);
2070 if (ncommits == s->selected)
2071 wstandend(view->window);
2075 s->last_displayed_entry = entry;
2076 entry = TAILQ_NEXT(entry, entry);
2089 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2091 struct commit_queue_entry *entry;
2094 entry = TAILQ_FIRST(&s->commits.head);
2095 if (s->first_displayed_entry == entry)
2098 entry = s->first_displayed_entry;
2099 while (entry && nscrolled < maxscroll) {
2100 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2102 s->first_displayed_entry = entry;
2108 static const struct got_error *
2109 trigger_log_thread(struct tog_view *view, int wait)
2111 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2114 halfdelay(1); /* fast refresh while loading commits */
2116 while (ta->commits_needed > 0 || ta->load_all) {
2117 if (ta->log_complete)
2120 /* Wake the log thread. */
2121 errcode = pthread_cond_signal(&ta->need_commits);
2123 return got_error_set_errno(errcode,
2124 "pthread_cond_signal");
2127 * The mutex will be released while the view loop waits
2128 * in wgetch(), at which time the log thread will run.
2133 /* Display progress update in log view. */
2134 show_log_view(view);
2138 /* Wait right here while next commit is being loaded. */
2139 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2141 return got_error_set_errno(errcode,
2142 "pthread_cond_wait");
2144 /* Display progress update in log view. */
2145 show_log_view(view);
2153 static const struct got_error *
2154 request_log_commits(struct tog_view *view)
2156 struct tog_log_view_state *state = &view->state.log;
2157 const struct got_error *err = NULL;
2159 state->thread_args.commits_needed = view->nscrolled;
2160 err = trigger_log_thread(view, 1);
2161 view->nscrolled = 0;
2166 static const struct got_error *
2167 log_scroll_down(struct tog_view *view, int maxscroll)
2169 struct tog_log_view_state *s = &view->state.log;
2170 const struct got_error *err = NULL;
2171 struct commit_queue_entry *pentry;
2172 int nscrolled = 0, ncommits_needed;
2174 if (s->last_displayed_entry == NULL)
2177 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2178 if (s->commits.ncommits < ncommits_needed &&
2179 !s->thread_args.log_complete) {
2181 * Ask the log thread for required amount of commits.
2183 s->thread_args.commits_needed += maxscroll;
2184 err = trigger_log_thread(view, 1);
2190 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2191 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2194 s->last_displayed_entry = pentry ?
2195 pentry : s->last_displayed_entry;;
2197 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2200 s->first_displayed_entry = pentry;
2201 } while (++nscrolled < maxscroll);
2203 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2204 view->nscrolled += nscrolled;
2206 view->nscrolled = 0;
2211 static const struct got_error *
2212 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2213 struct got_commit_object *commit, struct got_object_id *commit_id,
2214 struct tog_view *log_view, struct got_repository *repo)
2216 const struct got_error *err;
2217 struct got_object_qid *parent_id;
2218 struct tog_view *diff_view;
2220 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2221 if (diff_view == NULL)
2222 return got_error_from_errno("view_open");
2224 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2225 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2226 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2228 *new_view = diff_view;
2232 static const struct got_error *
2233 tree_view_visit_subtree(struct tog_tree_view_state *s,
2234 struct got_tree_object *subtree)
2236 struct tog_parent_tree *parent;
2238 parent = calloc(1, sizeof(*parent));
2240 return got_error_from_errno("calloc");
2242 parent->tree = s->tree;
2243 parent->first_displayed_entry = s->first_displayed_entry;
2244 parent->selected_entry = s->selected_entry;
2245 parent->selected = s->selected;
2246 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2249 s->first_displayed_entry = NULL;
2253 static const struct got_error *
2254 tree_view_walk_path(struct tog_tree_view_state *s,
2255 struct got_commit_object *commit, const char *path)
2257 const struct got_error *err = NULL;
2258 struct got_tree_object *tree = NULL;
2260 char *slash, *subpath = NULL;
2262 /* Walk the path and open corresponding tree objects. */
2265 struct got_tree_entry *te;
2266 struct got_object_id *tree_id;
2272 /* Ensure the correct subtree entry is selected. */
2273 slash = strchr(p, '/');
2275 te_name = strdup(p);
2277 te_name = strndup(p, slash - p);
2278 if (te_name == NULL) {
2279 err = got_error_from_errno("strndup");
2282 te = got_object_tree_find_entry(s->tree, te_name);
2284 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2289 s->first_displayed_entry = s->selected_entry = te;
2291 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2292 break; /* jump to this file's entry */
2294 slash = strchr(p, '/');
2296 subpath = strndup(path, slash - path);
2298 subpath = strdup(path);
2299 if (subpath == NULL) {
2300 err = got_error_from_errno("strdup");
2304 err = got_object_id_by_path(&tree_id, s->repo, commit,
2309 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2314 err = tree_view_visit_subtree(s, tree);
2316 got_object_tree_close(tree);
2330 static const struct got_error *
2331 browse_commit_tree(struct tog_view **new_view, int begin_x,
2332 struct commit_queue_entry *entry, const char *path,
2333 const char *head_ref_name, struct got_repository *repo)
2335 const struct got_error *err = NULL;
2336 struct tog_tree_view_state *s;
2337 struct tog_view *tree_view;
2339 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2340 if (tree_view == NULL)
2341 return got_error_from_errno("view_open");
2343 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2346 s = &tree_view->state.tree;
2348 *new_view = tree_view;
2350 if (got_path_is_root_dir(path))
2353 return tree_view_walk_path(s, entry->commit, path);
2356 static const struct got_error *
2357 block_signals_used_by_main_thread(void)
2362 if (sigemptyset(&sigset) == -1)
2363 return got_error_from_errno("sigemptyset");
2365 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2366 if (sigaddset(&sigset, SIGWINCH) == -1)
2367 return got_error_from_errno("sigaddset");
2368 if (sigaddset(&sigset, SIGCONT) == -1)
2369 return got_error_from_errno("sigaddset");
2370 if (sigaddset(&sigset, SIGINT) == -1)
2371 return got_error_from_errno("sigaddset");
2372 if (sigaddset(&sigset, SIGTERM) == -1)
2373 return got_error_from_errno("sigaddset");
2375 /* ncurses handles SIGTSTP */
2376 if (sigaddset(&sigset, SIGTSTP) == -1)
2377 return got_error_from_errno("sigaddset");
2379 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2381 return got_error_set_errno(errcode, "pthread_sigmask");
2387 log_thread(void *arg)
2389 const struct got_error *err = NULL;
2391 struct tog_log_thread_args *a = arg;
2394 err = block_signals_used_by_main_thread();
2398 while (!done && !err && !tog_fatal_signal_received()) {
2399 err = queue_commits(a);
2401 if (err->code != GOT_ERR_ITER_COMPLETED)
2405 } else if (a->commits_needed > 0 && !a->load_all)
2406 a->commits_needed--;
2408 errcode = pthread_mutex_lock(&tog_mutex);
2410 err = got_error_set_errno(errcode,
2411 "pthread_mutex_lock");
2413 } else if (*a->quit)
2415 else if (*a->first_displayed_entry == NULL) {
2416 *a->first_displayed_entry =
2417 TAILQ_FIRST(&a->commits->head);
2418 *a->selected_entry = *a->first_displayed_entry;
2421 errcode = pthread_cond_signal(&a->commit_loaded);
2423 err = got_error_set_errno(errcode,
2424 "pthread_cond_signal");
2425 pthread_mutex_unlock(&tog_mutex);
2430 a->commits_needed = 0;
2432 if (a->commits_needed == 0 && !a->load_all) {
2433 errcode = pthread_cond_wait(&a->need_commits,
2436 err = got_error_set_errno(errcode,
2437 "pthread_cond_wait");
2443 errcode = pthread_mutex_unlock(&tog_mutex);
2444 if (errcode && err == NULL)
2445 err = got_error_set_errno(errcode,
2446 "pthread_mutex_unlock");
2448 a->log_complete = 1;
2452 static const struct got_error *
2453 stop_log_thread(struct tog_log_view_state *s)
2455 const struct got_error *err = NULL;
2460 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2462 return got_error_set_errno(errcode,
2463 "pthread_cond_signal");
2464 errcode = pthread_mutex_unlock(&tog_mutex);
2466 return got_error_set_errno(errcode,
2467 "pthread_mutex_unlock");
2468 errcode = pthread_join(s->thread, (void **)&err);
2470 return got_error_set_errno(errcode, "pthread_join");
2471 errcode = pthread_mutex_lock(&tog_mutex);
2473 return got_error_set_errno(errcode,
2474 "pthread_mutex_lock");
2478 if (s->thread_args.repo) {
2479 err = got_repo_close(s->thread_args.repo);
2480 s->thread_args.repo = NULL;
2483 if (s->thread_args.pack_fds) {
2484 const struct got_error *pack_err =
2485 got_repo_pack_fds_close(s->thread_args.pack_fds);
2488 s->thread_args.pack_fds = NULL;
2491 if (s->thread_args.graph) {
2492 got_commit_graph_close(s->thread_args.graph);
2493 s->thread_args.graph = NULL;
2499 static const struct got_error *
2500 close_log_view(struct tog_view *view)
2502 const struct got_error *err = NULL;
2503 struct tog_log_view_state *s = &view->state.log;
2506 err = stop_log_thread(s);
2508 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2509 if (errcode && err == NULL)
2510 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2512 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2513 if (errcode && err == NULL)
2514 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2516 free_commits(&s->commits);
2517 free(s->in_repo_path);
2518 s->in_repo_path = NULL;
2521 free(s->head_ref_name);
2522 s->head_ref_name = NULL;
2526 static const struct got_error *
2527 search_start_log_view(struct tog_view *view)
2529 struct tog_log_view_state *s = &view->state.log;
2531 s->matched_entry = NULL;
2532 s->search_entry = NULL;
2536 static const struct got_error *
2537 search_next_log_view(struct tog_view *view)
2539 const struct got_error *err = NULL;
2540 struct tog_log_view_state *s = &view->state.log;
2541 struct commit_queue_entry *entry;
2543 /* Display progress update in log view. */
2544 show_log_view(view);
2548 if (s->search_entry) {
2550 errcode = pthread_mutex_unlock(&tog_mutex);
2552 return got_error_set_errno(errcode,
2553 "pthread_mutex_unlock");
2554 ch = wgetch(view->window);
2555 errcode = pthread_mutex_lock(&tog_mutex);
2557 return got_error_set_errno(errcode,
2558 "pthread_mutex_lock");
2559 if (ch == KEY_BACKSPACE) {
2560 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2563 if (view->searching == TOG_SEARCH_FORWARD)
2564 entry = TAILQ_NEXT(s->search_entry, entry);
2566 entry = TAILQ_PREV(s->search_entry,
2567 commit_queue_head, entry);
2568 } else if (s->matched_entry) {
2569 int matched_idx = s->matched_entry->idx;
2570 int selected_idx = s->selected_entry->idx;
2573 * If the user has moved the cursor after we hit a match,
2574 * the position from where we should continue searching
2575 * might have changed.
2577 if (view->searching == TOG_SEARCH_FORWARD) {
2578 if (matched_idx > selected_idx)
2579 entry = TAILQ_NEXT(s->selected_entry, entry);
2581 entry = TAILQ_NEXT(s->matched_entry, entry);
2583 if (matched_idx < selected_idx)
2584 entry = TAILQ_PREV(s->selected_entry,
2585 commit_queue_head, entry);
2587 entry = TAILQ_PREV(s->matched_entry,
2588 commit_queue_head, entry);
2591 entry = s->selected_entry;
2597 if (entry == NULL) {
2598 if (s->thread_args.log_complete ||
2599 view->searching == TOG_SEARCH_BACKWARD) {
2600 view->search_next_done =
2601 (s->matched_entry == NULL ?
2602 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2603 s->search_entry = NULL;
2607 * Poke the log thread for more commits and return,
2608 * allowing the main loop to make progress. Search
2609 * will resume at s->search_entry once we come back.
2611 s->thread_args.commits_needed++;
2612 return trigger_log_thread(view, 0);
2615 err = match_commit(&have_match, entry->id, entry->commit,
2620 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2621 s->matched_entry = entry;
2625 s->search_entry = entry;
2626 if (view->searching == TOG_SEARCH_FORWARD)
2627 entry = TAILQ_NEXT(entry, entry);
2629 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2632 if (s->matched_entry) {
2633 int cur = s->selected_entry->idx;
2634 while (cur < s->matched_entry->idx) {
2635 err = input_log_view(NULL, view, KEY_DOWN);
2640 while (cur > s->matched_entry->idx) {
2641 err = input_log_view(NULL, view, KEY_UP);
2648 s->search_entry = NULL;
2653 static const struct got_error *
2654 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2655 struct got_repository *repo, const char *head_ref_name,
2656 const char *in_repo_path, int log_branches)
2658 const struct got_error *err = NULL;
2659 struct tog_log_view_state *s = &view->state.log;
2660 struct got_repository *thread_repo = NULL;
2661 struct got_commit_graph *thread_graph = NULL;
2664 if (in_repo_path != s->in_repo_path) {
2665 free(s->in_repo_path);
2666 s->in_repo_path = strdup(in_repo_path);
2667 if (s->in_repo_path == NULL)
2668 return got_error_from_errno("strdup");
2671 /* The commit queue only contains commits being displayed. */
2672 TAILQ_INIT(&s->commits.head);
2673 s->commits.ncommits = 0;
2676 if (head_ref_name) {
2677 s->head_ref_name = strdup(head_ref_name);
2678 if (s->head_ref_name == NULL) {
2679 err = got_error_from_errno("strdup");
2683 s->start_id = got_object_id_dup(start_id);
2684 if (s->start_id == NULL) {
2685 err = got_error_from_errno("got_object_id_dup");
2688 s->log_branches = log_branches;
2690 STAILQ_INIT(&s->colors);
2691 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2692 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2693 get_color_value("TOG_COLOR_COMMIT"));
2696 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2697 get_color_value("TOG_COLOR_AUTHOR"));
2699 free_colors(&s->colors);
2702 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2703 get_color_value("TOG_COLOR_DATE"));
2705 free_colors(&s->colors);
2710 view->show = show_log_view;
2711 view->input = input_log_view;
2712 view->close = close_log_view;
2713 view->search_start = search_start_log_view;
2714 view->search_next = search_next_log_view;
2716 if (s->thread_args.pack_fds == NULL) {
2717 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2721 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2722 s->thread_args.pack_fds);
2725 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2729 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2730 s->repo, NULL, NULL);
2734 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2736 err = got_error_set_errno(errcode, "pthread_cond_init");
2739 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2741 err = got_error_set_errno(errcode, "pthread_cond_init");
2745 s->thread_args.commits_needed = view->nlines;
2746 s->thread_args.graph = thread_graph;
2747 s->thread_args.commits = &s->commits;
2748 s->thread_args.in_repo_path = s->in_repo_path;
2749 s->thread_args.start_id = s->start_id;
2750 s->thread_args.repo = thread_repo;
2751 s->thread_args.log_complete = 0;
2752 s->thread_args.quit = &s->quit;
2753 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2754 s->thread_args.selected_entry = &s->selected_entry;
2755 s->thread_args.searching = &view->searching;
2756 s->thread_args.search_next_done = &view->search_next_done;
2757 s->thread_args.regex = &view->regex;
2760 close_log_view(view);
2764 static const struct got_error *
2765 show_log_view(struct tog_view *view)
2767 const struct got_error *err;
2768 struct tog_log_view_state *s = &view->state.log;
2770 if (s->thread == NULL) {
2771 int errcode = pthread_create(&s->thread, NULL, log_thread,
2774 return got_error_set_errno(errcode, "pthread_create");
2775 if (s->thread_args.commits_needed > 0) {
2776 err = trigger_log_thread(view, 1);
2782 return draw_commits(view);
2786 log_move_cursor_up(struct tog_view *view, int page, int home)
2788 struct tog_log_view_state *s = &view->state.log;
2790 if (s->selected_entry->idx == 0)
2792 if (s->first_displayed_entry == NULL)
2795 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2797 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2799 if (!page && !home && s->selected > 0)
2802 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2808 static const struct got_error *
2809 log_move_cursor_down(struct tog_view *view, int page)
2811 struct tog_log_view_state *s = &view->state.log;
2812 struct commit_queue_entry *first;
2813 const struct got_error *err = NULL;
2815 first = s->first_displayed_entry;
2816 if (first == NULL) {
2821 if (s->thread_args.log_complete &&
2822 s->selected_entry->idx >= s->commits.ncommits - 1)
2826 int eos = view->nlines - 2;
2828 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2829 view_is_splitscreen(view->child))
2830 --eos; /* border consumes the last line */
2831 if (s->selected < MIN(eos, s->commits.ncommits - 1))
2834 err = log_scroll_down(view, 1);
2835 } else if (s->thread_args.load_all) {
2836 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2837 s->selected += MIN(s->last_displayed_entry->idx -
2838 s->selected_entry->idx, page + 1);
2840 err = log_scroll_down(view, MIN(page,
2841 s->commits.ncommits - s->selected_entry->idx - 1));
2842 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2844 err = log_scroll_down(view, page);
2847 if (first == s->first_displayed_entry && s->selected <
2848 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2849 s->selected = MIN(s->commits.ncommits - 1, page);
2856 * We might necessarily overshoot in horizontal
2857 * splits; if so, select the last displayed commit.
2859 s->selected = MIN(s->selected,
2860 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2864 if (s->thread_args.log_complete &&
2865 s->selected_entry->idx == s->commits.ncommits - 1)
2872 * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2873 * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2874 * TOG_VIEW_SPLIT_HRZN horizontal split
2875 * Assign start column and line of the new split to *x and *y, respectively,
2876 * and assign view mode to view->mode.
2879 view_get_split(struct tog_view *view, int *y, int *x)
2886 mode = getenv("TOG_VIEW_SPLIT_MODE");
2888 if (!mode || mode[0] != 'h') {
2889 view->mode = TOG_VIEW_SPLIT_VERT;
2890 *x = view_split_begin_x(view->begin_x);
2891 } else if (mode && mode[0] == 'h') {
2892 view->mode = TOG_VIEW_SPLIT_HRZN;
2893 *y = view_split_begin_y(view->lines);
2897 /* Split view horizontally at y and offset view->state->selected line. */
2898 static const struct got_error *
2899 view_init_hsplit(struct tog_view *view, int y)
2901 const struct got_error *err = NULL;
2904 err = view_resize(view);
2908 err = offset_selection_down(view);
2913 static const struct got_error *
2914 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2916 const struct got_error *err = NULL;
2917 struct tog_log_view_state *s = &view->state.log;
2918 struct tog_view *diff_view = NULL, *tree_view = NULL;
2919 struct tog_view *ref_view = NULL;
2920 struct commit_queue_entry *entry;
2921 int begin_x = 0, begin_y = 0, eos, n, nscroll;
2923 if (s->thread_args.load_all) {
2924 if (ch == KEY_BACKSPACE)
2925 s->thread_args.load_all = 0;
2926 else if (s->thread_args.log_complete) {
2927 err = log_move_cursor_down(view, s->commits.ncommits);
2928 s->thread_args.load_all = 0;
2933 eos = nscroll = view->nlines - 1;
2934 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2935 view_is_splitscreen(view->child))
2947 view->x = MAX(view->maxx - view->ncols / 2, 0);
2952 if (view->x + view->ncols / 2 < view->maxx)
2953 view->x += 2; /* move two columns right */
2959 view->x -= MIN(view->x, 2); /* move two columns back */
2968 log_move_cursor_up(view, 0, 0);
2972 log_move_cursor_up(view, 0, 1);
2982 log_move_cursor_up(view, nscroll, 0);
2989 err = log_move_cursor_down(view, 0);
2993 /* We don't know yet how many commits, so we're forced to
2994 * traverse them all. */
2996 if (!s->thread_args.log_complete) {
2997 s->thread_args.load_all = 1;
2998 return trigger_log_thread(view, 0);
3002 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3003 for (n = 0; n < eos; n++) {
3006 s->first_displayed_entry = entry;
3007 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3010 s->selected = n - 1;
3022 err = log_move_cursor_down(view, nscroll);
3025 if (s->selected > view->nlines - 2)
3026 s->selected = view->nlines - 2;
3027 if (s->selected > s->commits.ncommits - 1)
3028 s->selected = s->commits.ncommits - 1;
3030 if (s->commits.ncommits < view->nlines - 1 &&
3031 !s->thread_args.log_complete) {
3032 s->thread_args.commits_needed += (view->nlines - 1) -
3033 s->commits.ncommits;
3034 err = trigger_log_thread(view, 1);
3040 if (s->selected_entry == NULL)
3043 /* get dimensions--don't split till initialisation succeeds */
3044 if (view_is_parent_view(view))
3045 view_get_split(view, &begin_y, &begin_x);
3047 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3048 s->selected_entry->commit, s->selected_entry->id,
3053 if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3054 err = view_init_hsplit(view, begin_y);
3060 diff_view->focussed = 1;
3061 diff_view->mode = view->mode;
3062 diff_view->nlines = view->lines - begin_y;
3064 if (view_is_parent_view(view)) {
3065 err = view_close_child(view);
3068 err = view_set_child(view, diff_view);
3071 view->focus_child = 1;
3073 *new_view = diff_view;
3078 if (s->selected_entry == NULL)
3080 if (view_is_parent_view(view))
3081 begin_x = view_split_begin_x(view->begin_x);
3082 err = browse_commit_tree(&tree_view, begin_x,
3083 s->selected_entry, s->in_repo_path, s->head_ref_name,
3088 tree_view->focussed = 1;
3089 if (view_is_parent_view(view)) {
3090 err = view_close_child(view);
3093 err = view_set_child(view, tree_view);
3096 view->focus_child = 1;
3098 *new_view = tree_view;
3104 if (ch == KEY_BACKSPACE &&
3105 got_path_is_root_dir(s->in_repo_path))
3107 err = stop_log_thread(s);
3110 if (ch == KEY_BACKSPACE) {
3112 err = got_path_dirname(&parent_path, s->in_repo_path);
3115 free(s->in_repo_path);
3116 s->in_repo_path = parent_path;
3117 s->thread_args.in_repo_path = s->in_repo_path;
3118 } else if (ch == CTRL('l')) {
3119 struct got_object_id *start_id;
3120 err = got_repo_match_object_id(&start_id, NULL,
3121 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3122 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3126 s->start_id = start_id;
3127 s->thread_args.start_id = s->start_id;
3129 s->log_branches = !s->log_branches;
3131 if (s->thread_args.pack_fds == NULL) {
3132 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3136 err = got_repo_open(&s->thread_args.repo,
3137 got_repo_get_path(s->repo), NULL,
3138 s->thread_args.pack_fds);
3142 err = tog_load_refs(s->repo, 0);
3145 err = got_commit_graph_open(&s->thread_args.graph,
3146 s->in_repo_path, !s->log_branches);
3149 err = got_commit_graph_iter_start(s->thread_args.graph,
3150 s->start_id, s->repo, NULL, NULL);
3153 free_commits(&s->commits);
3154 s->first_displayed_entry = NULL;
3155 s->last_displayed_entry = NULL;
3156 s->selected_entry = NULL;
3158 s->thread_args.log_complete = 0;
3160 s->thread_args.commits_needed = view->lines;
3161 s->matched_entry = NULL;
3162 s->search_entry = NULL;
3166 if (view_is_parent_view(view))
3167 begin_x = view_split_begin_x(view->begin_x);
3168 ref_view = view_open(view->nlines, view->ncols,
3169 view->begin_y, begin_x, TOG_VIEW_REF);
3170 if (ref_view == NULL)
3171 return got_error_from_errno("view_open");
3172 err = open_ref_view(ref_view, s->repo);
3174 view_close(ref_view);
3178 ref_view->focussed = 1;
3179 if (view_is_parent_view(view)) {
3180 err = view_close_child(view);
3183 err = view_set_child(view, ref_view);
3186 view->focus_child = 1;
3188 *new_view = ref_view;
3198 static const struct got_error *
3199 apply_unveil(const char *repo_path, const char *worktree_path)
3201 const struct got_error *error;
3204 if (unveil("gmon.out", "rwc") != 0)
3205 return got_error_from_errno2("unveil", "gmon.out");
3207 if (repo_path && unveil(repo_path, "r") != 0)
3208 return got_error_from_errno2("unveil", repo_path);
3210 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3211 return got_error_from_errno2("unveil", worktree_path);
3213 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3214 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3216 error = got_privsep_unveil_exec_helpers();
3220 if (unveil(NULL, NULL) != 0)
3221 return got_error_from_errno("unveil");
3230 * Override default signal handlers before starting ncurses.
3231 * This should prevent ncurses from installing its own
3232 * broken cleanup() signal handler.
3234 signal(SIGWINCH, tog_sigwinch);
3235 signal(SIGPIPE, tog_sigpipe);
3236 signal(SIGCONT, tog_sigcont);
3237 signal(SIGINT, tog_sigint);
3238 signal(SIGTERM, tog_sigterm);
3242 halfdelay(1); /* Do fast refresh while initial view is loading. */
3245 intrflush(stdscr, FALSE);
3246 keypad(stdscr, TRUE);
3248 if (getenv("TOG_COLORS") != NULL) {
3250 use_default_colors();
3254 static const struct got_error *
3255 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3256 struct got_repository *repo, struct got_worktree *worktree)
3258 const struct got_error *err = NULL;
3261 *in_repo_path = strdup("/");
3262 if (*in_repo_path == NULL)
3263 return got_error_from_errno("strdup");
3268 const char *prefix = got_worktree_get_path_prefix(worktree);
3271 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3274 if (asprintf(in_repo_path, "%s%s%s", prefix,
3275 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3277 err = got_error_from_errno("asprintf");
3278 *in_repo_path = NULL;
3282 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3287 static const struct got_error *
3288 cmd_log(int argc, char *argv[])
3290 const struct got_error *error;
3291 struct got_repository *repo = NULL;
3292 struct got_worktree *worktree = NULL;
3293 struct got_object_id *start_id = NULL;
3294 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3295 char *start_commit = NULL, *label = NULL;
3296 struct got_reference *ref = NULL;
3297 const char *head_ref_name = NULL;
3298 int ch, log_branches = 0;
3299 struct tog_view *view;
3300 int *pack_fds = NULL;
3302 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3308 start_commit = optarg;
3311 repo_path = realpath(optarg, NULL);
3312 if (repo_path == NULL)
3313 return got_error_from_errno2("realpath",
3328 error = got_repo_pack_fds_open(&pack_fds);
3332 if (repo_path == NULL) {
3333 cwd = getcwd(NULL, 0);
3335 return got_error_from_errno("getcwd");
3336 error = got_worktree_open(&worktree, cwd);
3337 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3341 strdup(got_worktree_get_repo_path(worktree));
3343 repo_path = strdup(cwd);
3344 if (repo_path == NULL) {
3345 error = got_error_from_errno("strdup");
3350 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3354 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3361 error = apply_unveil(got_repo_get_path(repo),
3362 worktree ? got_worktree_get_root_path(worktree) : NULL);
3366 /* already loaded by tog_log_with_path()? */
3367 if (TAILQ_EMPTY(&tog_refs)) {
3368 error = tog_load_refs(repo, 0);
3373 if (start_commit == NULL) {
3374 error = got_repo_match_object_id(&start_id, &label,
3375 worktree ? got_worktree_get_head_ref_name(worktree) :
3376 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3379 head_ref_name = label;
3381 error = got_ref_open(&ref, repo, start_commit, 0);
3383 head_ref_name = got_ref_get_name(ref);
3384 else if (error->code != GOT_ERR_NOT_REF)
3386 error = got_repo_match_object_id(&start_id, NULL,
3387 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3392 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3394 error = got_error_from_errno("view_open");
3397 error = open_log_view(view, start_id, repo, head_ref_name,
3398 in_repo_path, log_branches);
3402 /* Release work tree lock. */
3403 got_worktree_close(worktree);
3406 error = view_loop(view);
3416 const struct got_error *close_err = got_repo_close(repo);
3421 got_worktree_close(worktree);
3423 const struct got_error *pack_err =
3424 got_repo_pack_fds_close(pack_fds);
3436 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3437 "[-w] object1 object2\n", getprogname());
3442 match_line(const char *line, regex_t *regex, size_t nmatch,
3443 regmatch_t *regmatch)
3445 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3448 static struct tog_color *
3449 match_color(struct tog_colors *colors, const char *line)
3451 struct tog_color *tc = NULL;
3453 STAILQ_FOREACH(tc, colors, entry) {
3454 if (match_line(line, &tc->regex, 0, NULL))
3461 static const struct got_error *
3462 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3463 WINDOW *window, int skipcol, regmatch_t *regmatch)
3465 const struct got_error *err = NULL;
3467 wchar_t *wline = NULL;
3468 int rme, rms, n, width, scrollx;
3469 int width0 = 0, width1 = 0, width2 = 0;
3470 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3474 rms = regmatch->rm_so;
3475 rme = regmatch->rm_eo;
3477 err = expand_tab(&exstr, line);
3481 /* Split the line into 3 segments, according to match offsets. */
3482 seg0 = strndup(exstr, rms);
3484 err = got_error_from_errno("strndup");
3487 seg1 = strndup(exstr + rms, rme - rms);
3489 err = got_error_from_errno("strndup");
3492 seg2 = strdup(exstr + rme);
3494 err = got_error_from_errno("strndup");
3498 /* draw up to matched token if we haven't scrolled past it */
3499 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3503 n = MAX(width0 - skipcol, 0);
3506 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3507 wlimit, col_tab_align, 1);
3510 waddwstr(window, &wline[scrollx]);
3520 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3524 wlen = wcslen(wline);
3526 width = wcwidth(wline[i]);
3528 /* should not happen, tabs are expanded */
3529 err = got_error(GOT_ERR_RANGE);
3532 if (width0 + w + width > skipcol)
3537 /* draw (visible part of) matched token (if scrolled into it) */
3538 if (width1 - w > 0) {
3539 wattron(window, A_STANDOUT);
3540 waddwstr(window, &wline[i]);
3541 wattroff(window, A_STANDOUT);
3542 wlimit -= (width1 - w);
3543 *wtotal += (width1 - w);
3547 if (wlimit > 0) { /* draw rest of line */
3549 if (skipcol > width0 + width1) {
3550 err = format_line(&wline, &width2, &scrollx, seg2,
3551 skipcol - (width0 + width1), wlimit,
3555 waddwstr(window, &wline[scrollx]);
3557 err = format_line(&wline, &width2, NULL, seg2, 0,
3558 wlimit, col_tab_align, 1);
3561 waddwstr(window, wline);
3574 static const struct got_error *
3575 draw_file(struct tog_view *view, const char *header)
3577 struct tog_diff_view_state *s = &view->state.diff;
3578 regmatch_t *regmatch = &view->regmatch;
3579 const struct got_error *err;
3582 size_t linesize = 0;
3584 struct tog_color *tc;
3587 int max_lines = view->nlines;
3588 int nlines = s->nlines;
3591 line_offset = s->line_offsets[s->first_displayed_line - 1];
3592 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3593 return got_error_from_errno("fseek");
3595 werase(view->window);
3598 if (asprintf(&line, "[%d/%d] %s",
3599 s->first_displayed_line - 1 + s->selected_line, nlines,
3601 return got_error_from_errno("asprintf");
3602 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3608 if (view_needs_focus_indication(view))
3609 wstandout(view->window);
3610 waddwstr(view->window, wline);
3613 if (view_needs_focus_indication(view))
3614 wstandend(view->window);
3615 if (width <= view->ncols - 1)
3616 waddch(view->window, '\n');
3626 while (max_lines > 0 && nprinted < max_lines) {
3627 linelen = getline(&line, &linesize, s->f);
3628 if (linelen == -1) {
3634 return got_ferror(s->f, GOT_ERR_IO);
3637 /* Set view->maxx based on full line length. */
3638 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3644 view->maxx = MAX(view->maxx, width);
3648 tc = match_color(&s->colors, line);
3650 wattr_on(view->window,
3651 COLOR_PAIR(tc->colorpair), NULL);
3652 if (s->first_displayed_line + nprinted == s->matched_line &&
3653 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3654 err = add_matched_line(&width, line, view->ncols, 0,
3655 view->window, view->x, regmatch);
3662 err = format_line(&wline, &width, &skip, line,
3663 view->x, view->ncols, 0, view->x ? 1 : 0);
3668 waddwstr(view->window, &wline[skip]);
3673 wattr_off(view->window,
3674 COLOR_PAIR(tc->colorpair), NULL);
3675 if (width <= view->ncols - 1)
3676 waddch(view->window, '\n');
3681 s->last_displayed_line = s->first_displayed_line +
3684 s->last_displayed_line = s->first_displayed_line;
3689 while (nprinted < view->nlines) {
3690 waddch(view->window, '\n');
3694 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3700 wstandout(view->window);
3701 waddwstr(view->window, wline);
3704 wstandend(view->window);
3711 get_datestr(time_t *time, char *datebuf)
3713 struct tm mytm, *tm;
3716 tm = gmtime_r(time, &mytm);
3719 s = asctime_r(tm, datebuf);
3722 p = strchr(s, '\n');
3728 static const struct got_error *
3729 get_changed_paths(struct got_pathlist_head *paths,
3730 struct got_commit_object *commit, struct got_repository *repo)
3732 const struct got_error *err = NULL;
3733 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3734 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3735 struct got_object_qid *qid;
3737 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3739 struct got_commit_object *pcommit;
3740 err = got_object_open_as_commit(&pcommit, repo,
3745 tree_id1 = got_object_id_dup(
3746 got_object_commit_get_tree_id(pcommit));
3747 if (tree_id1 == NULL) {
3748 got_object_commit_close(pcommit);
3749 return got_error_from_errno("got_object_id_dup");
3751 got_object_commit_close(pcommit);
3756 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3761 tree_id2 = got_object_commit_get_tree_id(commit);
3762 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3766 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3767 got_diff_tree_collect_changed_paths, paths, 0);
3770 got_object_tree_close(tree1);
3772 got_object_tree_close(tree2);
3777 static const struct got_error *
3778 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3782 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3784 return got_error_from_errno("reallocarray");
3786 (*line_offsets)[*nlines] = off;
3791 static const struct got_error *
3792 write_commit_info(off_t **line_offsets, size_t *nlines,
3793 struct got_object_id *commit_id, struct got_reflist_head *refs,
3794 struct got_repository *repo, FILE *outfile)
3796 const struct got_error *err = NULL;
3797 char datebuf[26], *datestr;
3798 struct got_commit_object *commit;
3799 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3800 time_t committer_time;
3801 const char *author, *committer;
3802 char *refs_str = NULL;
3803 struct got_pathlist_head changed_paths;
3804 struct got_pathlist_entry *pe;
3808 TAILQ_INIT(&changed_paths);
3811 err = build_refs_str(&refs_str, refs, commit_id, repo);
3816 err = got_object_open_as_commit(&commit, repo, commit_id);
3820 err = got_object_id_str(&id_str, commit_id);
3822 err = got_error_from_errno("got_object_id_str");
3826 err = add_line_offset(line_offsets, nlines, 0);
3830 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3831 refs_str ? refs_str : "", refs_str ? ")" : "");
3833 err = got_error_from_errno("fprintf");
3837 err = add_line_offset(line_offsets, nlines, outoff);
3841 n = fprintf(outfile, "from: %s\n",
3842 got_object_commit_get_author(commit));
3844 err = got_error_from_errno("fprintf");
3848 err = add_line_offset(line_offsets, nlines, outoff);
3852 committer_time = got_object_commit_get_committer_time(commit);
3853 datestr = get_datestr(&committer_time, datebuf);
3855 n = fprintf(outfile, "date: %s UTC\n", datestr);
3857 err = got_error_from_errno("fprintf");
3861 err = add_line_offset(line_offsets, nlines, outoff);
3865 author = got_object_commit_get_author(commit);
3866 committer = got_object_commit_get_committer(commit);
3867 if (strcmp(author, committer) != 0) {
3868 n = fprintf(outfile, "via: %s\n", committer);
3870 err = got_error_from_errno("fprintf");
3874 err = add_line_offset(line_offsets, nlines, outoff);
3878 if (got_object_commit_get_nparents(commit) > 1) {
3879 const struct got_object_id_queue *parent_ids;
3880 struct got_object_qid *qid;
3882 parent_ids = got_object_commit_get_parent_ids(commit);
3883 STAILQ_FOREACH(qid, parent_ids, entry) {
3884 err = got_object_id_str(&id_str, &qid->id);
3887 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3889 err = got_error_from_errno("fprintf");
3893 err = add_line_offset(line_offsets, nlines, outoff);
3901 err = got_object_commit_get_logmsg(&logmsg, commit);
3905 while ((line = strsep(&s, "\n")) != NULL) {
3906 n = fprintf(outfile, "%s\n", line);
3908 err = got_error_from_errno("fprintf");
3912 err = add_line_offset(line_offsets, nlines, outoff);
3917 err = get_changed_paths(&changed_paths, commit, repo);
3920 TAILQ_FOREACH(pe, &changed_paths, entry) {
3921 struct got_diff_changed_path *cp = pe->data;
3922 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3924 err = got_error_from_errno("fprintf");
3928 err = add_line_offset(line_offsets, nlines, outoff);
3931 free((char *)pe->path);
3935 fputc('\n', outfile);
3937 err = add_line_offset(line_offsets, nlines, outoff);
3939 got_pathlist_free(&changed_paths);
3943 got_object_commit_close(commit);
3945 free(*line_offsets);
3946 *line_offsets = NULL;
3952 static const struct got_error *
3953 create_diff(struct tog_diff_view_state *s)
3955 const struct got_error *err = NULL;
3959 free(s->line_offsets);
3960 s->line_offsets = malloc(sizeof(off_t));
3961 if (s->line_offsets == NULL)
3962 return got_error_from_errno("malloc");
3967 err = got_error_from_errno("got_opentemp");
3970 if (s->f && fclose(s->f) == EOF) {
3971 err = got_error_from_errno("fclose");
3977 err = got_object_get_type(&obj_type, s->repo, s->id1);
3979 err = got_object_get_type(&obj_type, s->repo, s->id2);
3984 case GOT_OBJ_TYPE_BLOB:
3985 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3986 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3987 s->label1, s->label2, tog_diff_algo, s->diff_context,
3988 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3990 case GOT_OBJ_TYPE_TREE:
3991 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3992 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3993 tog_diff_algo, s->diff_context, s->ignore_whitespace,
3994 s->force_text_diff, s->repo, s->f);
3996 case GOT_OBJ_TYPE_COMMIT: {
3997 const struct got_object_id_queue *parent_ids;
3998 struct got_object_qid *pid;
3999 struct got_commit_object *commit2;
4000 struct got_reflist_head *refs;
4002 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4005 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4006 /* Show commit info if we're diffing to a parent/root commit. */
4007 if (s->id1 == NULL) {
4008 err = write_commit_info(&s->line_offsets, &s->nlines,
4009 s->id2, refs, s->repo, s->f);
4013 parent_ids = got_object_commit_get_parent_ids(commit2);
4014 STAILQ_FOREACH(pid, parent_ids, entry) {
4015 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4016 err = write_commit_info(
4017 &s->line_offsets, &s->nlines,
4018 s->id2, refs, s->repo, s->f);
4025 got_object_commit_close(commit2);
4027 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4028 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4029 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4030 s->force_text_diff, s->repo, s->f);
4034 err = got_error(GOT_ERR_OBJ_TYPE);
4040 if (s->f && fflush(s->f) != 0 && err == NULL)
4041 err = got_error_from_errno("fflush");
4046 diff_view_indicate_progress(struct tog_view *view)
4048 mvwaddstr(view->window, 0, 0, "diffing...");
4053 static const struct got_error *
4054 search_start_diff_view(struct tog_view *view)
4056 struct tog_diff_view_state *s = &view->state.diff;
4058 s->matched_line = 0;
4062 static const struct got_error *
4063 search_next_diff_view(struct tog_view *view)
4065 struct tog_diff_view_state *s = &view->state.diff;
4066 const struct got_error *err = NULL;
4069 size_t linesize = 0;
4072 if (!view->searching) {
4073 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4077 if (s->matched_line) {
4078 if (view->searching == TOG_SEARCH_FORWARD)
4079 lineno = s->matched_line + 1;
4081 lineno = s->matched_line - 1;
4083 lineno = s->first_displayed_line;
4088 if (lineno <= 0 || lineno > s->nlines) {
4089 if (s->matched_line == 0) {
4090 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4094 if (view->searching == TOG_SEARCH_FORWARD)
4100 offset = s->line_offsets[lineno - 1];
4101 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4103 return got_error_from_errno("fseeko");
4105 linelen = getline(&line, &linesize, s->f);
4106 if (linelen != -1) {
4108 err = expand_tab(&exstr, line);
4111 if (match_line(exstr, &view->regex, 1,
4113 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4114 s->matched_line = lineno;
4120 if (view->searching == TOG_SEARCH_FORWARD)
4127 if (s->matched_line) {
4128 s->first_displayed_line = s->matched_line;
4129 s->selected_line = 1;
4135 static const struct got_error *
4136 close_diff_view(struct tog_view *view)
4138 const struct got_error *err = NULL;
4139 struct tog_diff_view_state *s = &view->state.diff;
4145 if (s->f && fclose(s->f) == EOF)
4146 err = got_error_from_errno("fclose");
4148 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4149 err = got_error_from_errno("fclose");
4151 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4152 err = got_error_from_errno("fclose");
4154 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4155 err = got_error_from_errno("close");
4157 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4158 err = got_error_from_errno("close");
4160 free_colors(&s->colors);
4161 free(s->line_offsets);
4162 s->line_offsets = NULL;
4167 static const struct got_error *
4168 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4169 struct got_object_id *id2, const char *label1, const char *label2,
4170 int diff_context, int ignore_whitespace, int force_text_diff,
4171 struct tog_view *log_view, struct got_repository *repo)
4173 const struct got_error *err;
4174 struct tog_diff_view_state *s = &view->state.diff;
4176 memset(s, 0, sizeof(*s));
4180 if (id1 != NULL && id2 != NULL) {
4182 err = got_object_get_type(&type1, repo, id1);
4185 err = got_object_get_type(&type2, repo, id2);
4190 return got_error(GOT_ERR_OBJ_TYPE);
4192 s->first_displayed_line = 1;
4193 s->last_displayed_line = view->nlines;
4194 s->selected_line = 1;
4202 s->id1 = got_object_id_dup(id1);
4204 return got_error_from_errno("got_object_id_dup");
4208 s->id2 = got_object_id_dup(id2);
4209 if (s->id2 == NULL) {
4210 err = got_error_from_errno("got_object_id_dup");
4214 s->f1 = got_opentemp();
4215 if (s->f1 == NULL) {
4216 err = got_error_from_errno("got_opentemp");
4220 s->f2 = got_opentemp();
4221 if (s->f2 == NULL) {
4222 err = got_error_from_errno("got_opentemp");
4226 s->fd1 = got_opentempfd();
4228 err = got_error_from_errno("got_opentempfd");
4232 s->fd2 = got_opentempfd();
4234 err = got_error_from_errno("got_opentempfd");
4238 s->first_displayed_line = 1;
4239 s->last_displayed_line = view->nlines;
4240 s->diff_context = diff_context;
4241 s->ignore_whitespace = ignore_whitespace;
4242 s->force_text_diff = force_text_diff;
4243 s->log_view = log_view;
4246 STAILQ_INIT(&s->colors);
4247 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4248 err = add_color(&s->colors,
4249 "^-", TOG_COLOR_DIFF_MINUS,
4250 get_color_value("TOG_COLOR_DIFF_MINUS"));
4253 err = add_color(&s->colors, "^\\+",
4254 TOG_COLOR_DIFF_PLUS,
4255 get_color_value("TOG_COLOR_DIFF_PLUS"));
4258 err = add_color(&s->colors,
4259 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4260 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4264 err = add_color(&s->colors,
4265 "^(commit [0-9a-f]|parent [0-9]|"
4266 "(blob|file|tree|commit) [-+] |"
4267 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4268 get_color_value("TOG_COLOR_DIFF_META"));
4272 err = add_color(&s->colors,
4273 "^(from|via): ", TOG_COLOR_AUTHOR,
4274 get_color_value("TOG_COLOR_AUTHOR"));
4278 err = add_color(&s->colors,
4279 "^date: ", TOG_COLOR_DATE,
4280 get_color_value("TOG_COLOR_DATE"));
4285 if (log_view && view_is_splitscreen(view))
4286 show_log_view(log_view); /* draw vborder */
4287 diff_view_indicate_progress(view);
4289 err = create_diff(s);
4291 view->show = show_diff_view;
4292 view->input = input_diff_view;
4293 view->reset = reset_diff_view;
4294 view->close = close_diff_view;
4295 view->search_start = search_start_diff_view;
4296 view->search_next = search_next_diff_view;
4299 close_diff_view(view);
4303 static const struct got_error *
4304 show_diff_view(struct tog_view *view)
4306 const struct got_error *err;
4307 struct tog_diff_view_state *s = &view->state.diff;
4308 char *id_str1 = NULL, *id_str2, *header;
4309 const char *label1, *label2;
4312 err = got_object_id_str(&id_str1, s->id1);
4315 label1 = s->label1 ? : id_str1;
4317 label1 = "/dev/null";
4319 err = got_object_id_str(&id_str2, s->id2);
4322 label2 = s->label2 ? : id_str2;
4324 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4325 err = got_error_from_errno("asprintf");
4333 err = draw_file(view, header);
4338 static const struct got_error *
4339 set_selected_commit(struct tog_diff_view_state *s,
4340 struct commit_queue_entry *entry)
4342 const struct got_error *err;
4343 const struct got_object_id_queue *parent_ids;
4344 struct got_commit_object *selected_commit;
4345 struct got_object_qid *pid;
4348 s->id2 = got_object_id_dup(entry->id);
4350 return got_error_from_errno("got_object_id_dup");
4352 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4355 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4357 pid = STAILQ_FIRST(parent_ids);
4358 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4359 got_object_commit_close(selected_commit);
4363 static const struct got_error *
4364 reset_diff_view(struct tog_view *view)
4366 struct tog_diff_view_state *s = &view->state.diff;
4369 wclear(view->window);
4370 s->first_displayed_line = 1;
4371 s->last_displayed_line = view->nlines;
4372 s->matched_line = 0;
4373 diff_view_indicate_progress(view);
4374 return create_diff(s);
4377 static const struct got_error *
4378 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4380 const struct got_error *err = NULL;
4381 struct tog_diff_view_state *s = &view->state.diff;
4382 struct tog_log_view_state *ls;
4383 struct commit_queue_entry *old_selected_entry;
4385 size_t linesize = 0;
4387 int i, nscroll = view->nlines - 1;
4394 view->x = MAX(view->maxx - view->ncols / 3, 0);
4399 if (view->x + view->ncols / 3 < view->maxx)
4400 view->x += 2; /* move two columns right */
4406 view->x -= MIN(view->x, 2); /* move two columns back */
4413 s->force_text_diff = !s->force_text_diff;
4415 s->ignore_whitespace = !s->ignore_whitespace;
4416 err = reset_diff_view(view);
4420 s->first_displayed_line = 1;
4429 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4435 if (s->first_displayed_line > 1)
4436 s->first_displayed_line--;
4447 if (s->first_displayed_line == 1) {
4452 while (i++ < nscroll && s->first_displayed_line > 1)
4453 s->first_displayed_line--;
4459 s->first_displayed_line++;
4476 while (!s->eof && i++ < nscroll) {
4477 linelen = getline(&line, &linesize, s->f);
4478 s->first_displayed_line++;
4479 if (linelen == -1) {
4483 err = got_ferror(s->f, GOT_ERR_IO);
4490 if (s->diff_context > 0) {
4492 s->matched_line = 0;
4493 diff_view_indicate_progress(view);
4494 err = create_diff(s);
4495 if (s->first_displayed_line + view->nlines - 1 >
4497 s->first_displayed_line = 1;
4498 s->last_displayed_line = view->nlines;
4504 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4506 s->matched_line = 0;
4507 diff_view_indicate_progress(view);
4508 err = create_diff(s);
4514 if (s->log_view == NULL) {
4518 ls = &s->log_view->state.log;
4519 old_selected_entry = ls->selected_entry;
4521 /* view->count handled in input_log_view() */
4522 err = input_log_view(NULL, s->log_view, KEY_UP);
4526 if (old_selected_entry == ls->selected_entry)
4529 err = set_selected_commit(s, ls->selected_entry);
4533 s->first_displayed_line = 1;
4534 s->last_displayed_line = view->nlines;
4535 s->matched_line = 0;
4538 diff_view_indicate_progress(view);
4539 err = create_diff(s);
4543 if (s->log_view == NULL) {
4547 ls = &s->log_view->state.log;
4548 old_selected_entry = ls->selected_entry;
4550 /* view->count handled in input_log_view() */
4551 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4555 if (old_selected_entry == ls->selected_entry)
4558 err = set_selected_commit(s, ls->selected_entry);
4562 s->first_displayed_line = 1;
4563 s->last_displayed_line = view->nlines;
4564 s->matched_line = 0;
4567 diff_view_indicate_progress(view);
4568 err = create_diff(s);
4578 static const struct got_error *
4579 cmd_diff(int argc, char *argv[])
4581 const struct got_error *error = NULL;
4582 struct got_repository *repo = NULL;
4583 struct got_worktree *worktree = NULL;
4584 struct got_object_id *id1 = NULL, *id2 = NULL;
4585 char *repo_path = NULL, *cwd = NULL;
4586 char *id_str1 = NULL, *id_str2 = NULL;
4587 char *label1 = NULL, *label2 = NULL;
4588 int diff_context = 3, ignore_whitespace = 0;
4589 int ch, force_text_diff = 0;
4591 struct tog_view *view;
4592 int *pack_fds = NULL;
4594 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4597 force_text_diff = 1;
4600 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4603 errx(1, "number of context lines is %s: %s",
4607 repo_path = realpath(optarg, NULL);
4608 if (repo_path == NULL)
4609 return got_error_from_errno2("realpath",
4611 got_path_strip_trailing_slashes(repo_path);
4614 ignore_whitespace = 1;
4626 usage_diff(); /* TODO show local worktree changes */
4627 } else if (argc == 2) {
4633 error = got_repo_pack_fds_open(&pack_fds);
4637 if (repo_path == NULL) {
4638 cwd = getcwd(NULL, 0);
4640 return got_error_from_errno("getcwd");
4641 error = got_worktree_open(&worktree, cwd);
4642 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4646 strdup(got_worktree_get_repo_path(worktree));
4648 repo_path = strdup(cwd);
4649 if (repo_path == NULL) {
4650 error = got_error_from_errno("strdup");
4655 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4661 error = apply_unveil(got_repo_get_path(repo), NULL);
4665 error = tog_load_refs(repo, 0);
4669 error = got_repo_match_object_id(&id1, &label1, id_str1,
4670 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4674 error = got_repo_match_object_id(&id2, &label2, id_str2,
4675 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4679 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4681 error = got_error_from_errno("view_open");
4684 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4685 ignore_whitespace, force_text_diff, NULL, repo);
4688 error = view_loop(view);
4695 const struct got_error *close_err = got_repo_close(repo);
4700 got_worktree_close(worktree);
4702 const struct got_error *pack_err =
4703 got_repo_pack_fds_close(pack_fds);
4716 "usage: %s blame [-c commit] [-r repository-path] path\n",
4721 struct tog_blame_line {
4723 struct got_object_id *id;
4726 static const struct got_error *
4727 draw_blame(struct tog_view *view)
4729 struct tog_blame_view_state *s = &view->state.blame;
4730 struct tog_blame *blame = &s->blame;
4731 regmatch_t *regmatch = &view->regmatch;
4732 const struct got_error *err;
4733 int lineno = 0, nprinted = 0;
4735 size_t linesize = 0;
4739 struct tog_blame_line *blame_line;
4740 struct got_object_id *prev_id = NULL;
4742 struct tog_color *tc;
4744 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4749 werase(view->window);
4751 if (asprintf(&line, "commit %s", id_str) == -1) {
4752 err = got_error_from_errno("asprintf");
4757 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4762 if (view_needs_focus_indication(view))
4763 wstandout(view->window);
4764 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4766 wattr_on(view->window,
4767 COLOR_PAIR(tc->colorpair), NULL);
4768 waddwstr(view->window, wline);
4770 wattr_off(view->window,
4771 COLOR_PAIR(tc->colorpair), NULL);
4772 if (view_needs_focus_indication(view))
4773 wstandend(view->window);
4776 if (width < view->ncols - 1)
4777 waddch(view->window, '\n');
4779 if (asprintf(&line, "[%d/%d] %s%s",
4780 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4781 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4783 return got_error_from_errno("asprintf");
4786 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4791 waddwstr(view->window, wline);
4794 if (width < view->ncols - 1)
4795 waddch(view->window, '\n');
4799 while (nprinted < view->nlines - 2) {
4800 linelen = getline(&line, &linesize, blame->f);
4801 if (linelen == -1) {
4802 if (feof(blame->f)) {
4807 return got_ferror(blame->f, GOT_ERR_IO);
4809 if (++lineno < s->first_displayed_line)
4812 /* Set view->maxx based on full line length. */
4813 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4820 view->maxx = MAX(view->maxx, width);
4822 if (view->focussed && nprinted == s->selected_line - 1)
4823 wstandout(view->window);
4825 if (blame->nlines > 0) {
4826 blame_line = &blame->lines[lineno - 1];
4827 if (blame_line->annotated && prev_id &&
4828 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4830 nprinted == s->selected_line - 1)) {
4831 waddstr(view->window, " ");
4832 } else if (blame_line->annotated) {
4834 err = got_object_id_str(&id_str,
4840 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4842 wattr_on(view->window,
4843 COLOR_PAIR(tc->colorpair), NULL);
4844 wprintw(view->window, "%.8s", id_str);
4846 wattr_off(view->window,
4847 COLOR_PAIR(tc->colorpair), NULL);
4849 prev_id = blame_line->id;
4851 waddstr(view->window, "........");
4855 waddstr(view->window, "........");
4859 if (view->focussed && nprinted == s->selected_line - 1)
4860 wstandend(view->window);
4861 waddstr(view->window, " ");
4863 if (view->ncols <= 9) {
4865 } else if (s->first_displayed_line + nprinted ==
4867 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4868 err = add_matched_line(&width, line, view->ncols - 9, 9,
4869 view->window, view->x, regmatch);
4877 err = format_line(&wline, &width, &skip, line,
4878 view->x, view->ncols - 9, 9, 1);
4883 waddwstr(view->window, &wline[skip]);
4889 if (width <= view->ncols - 1)
4890 waddch(view->window, '\n');
4891 if (++nprinted == 1)
4892 s->first_displayed_line = lineno;
4895 s->last_displayed_line = lineno;
4902 static const struct got_error *
4903 blame_cb(void *arg, int nlines, int lineno,
4904 struct got_commit_object *commit, struct got_object_id *id)
4906 const struct got_error *err = NULL;
4907 struct tog_blame_cb_args *a = arg;
4908 struct tog_blame_line *line;
4911 if (nlines != a->nlines ||
4912 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4913 return got_error(GOT_ERR_RANGE);
4915 errcode = pthread_mutex_lock(&tog_mutex);
4917 return got_error_set_errno(errcode, "pthread_mutex_lock");
4919 if (*a->quit) { /* user has quit the blame view */
4920 err = got_error(GOT_ERR_ITER_COMPLETED);
4925 goto done; /* no change in this commit */
4927 line = &a->lines[lineno - 1];
4928 if (line->annotated)
4931 line->id = got_object_id_dup(id);
4932 if (line->id == NULL) {
4933 err = got_error_from_errno("got_object_id_dup");
4936 line->annotated = 1;
4938 errcode = pthread_mutex_unlock(&tog_mutex);
4940 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4945 blame_thread(void *arg)
4947 const struct got_error *err, *close_err;
4948 struct tog_blame_thread_args *ta = arg;
4949 struct tog_blame_cb_args *a = ta->cb_args;
4950 int errcode, fd1 = -1, fd2 = -1;
4951 FILE *f1 = NULL, *f2 = NULL;
4953 fd1 = got_opentempfd();
4955 return (void *)got_error_from_errno("got_opentempfd");
4957 fd2 = got_opentempfd();
4959 err = got_error_from_errno("got_opentempfd");
4963 f1 = got_opentemp();
4965 err = (void *)got_error_from_errno("got_opentemp");
4968 f2 = got_opentemp();
4970 err = (void *)got_error_from_errno("got_opentemp");
4974 err = block_signals_used_by_main_thread();
4978 err = got_blame(ta->path, a->commit_id, ta->repo,
4979 tog_diff_algo, blame_cb, ta->cb_args,
4980 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
4981 if (err && err->code == GOT_ERR_CANCELLED)
4984 errcode = pthread_mutex_lock(&tog_mutex);
4986 err = got_error_set_errno(errcode, "pthread_mutex_lock");
4990 close_err = got_repo_close(ta->repo);
4996 errcode = pthread_mutex_unlock(&tog_mutex);
4997 if (errcode && err == NULL)
4998 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5001 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5002 err = got_error_from_errno("close");
5003 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5004 err = got_error_from_errno("close");
5005 if (f1 && fclose(f1) == EOF && err == NULL)
5006 err = got_error_from_errno("fclose");
5007 if (f2 && fclose(f2) == EOF && err == NULL)
5008 err = got_error_from_errno("fclose");
5013 static struct got_object_id *
5014 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5015 int first_displayed_line, int selected_line)
5017 struct tog_blame_line *line;
5022 line = &lines[first_displayed_line - 1 + selected_line - 1];
5023 if (!line->annotated)
5029 static const struct got_error *
5030 stop_blame(struct tog_blame *blame)
5032 const struct got_error *err = NULL;
5035 if (blame->thread) {
5037 errcode = pthread_mutex_unlock(&tog_mutex);
5039 return got_error_set_errno(errcode,
5040 "pthread_mutex_unlock");
5041 errcode = pthread_join(blame->thread, (void **)&err);
5043 return got_error_set_errno(errcode, "pthread_join");
5044 errcode = pthread_mutex_lock(&tog_mutex);
5046 return got_error_set_errno(errcode,
5047 "pthread_mutex_lock");
5048 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5050 blame->thread = NULL;
5052 if (blame->thread_args.repo) {
5053 const struct got_error *close_err;
5054 close_err = got_repo_close(blame->thread_args.repo);
5057 blame->thread_args.repo = NULL;
5060 if (fclose(blame->f) == EOF && err == NULL)
5061 err = got_error_from_errno("fclose");
5065 for (i = 0; i < blame->nlines; i++)
5066 free(blame->lines[i].id);
5068 blame->lines = NULL;
5070 free(blame->cb_args.commit_id);
5071 blame->cb_args.commit_id = NULL;
5072 if (blame->pack_fds) {
5073 const struct got_error *pack_err =
5074 got_repo_pack_fds_close(blame->pack_fds);
5077 blame->pack_fds = NULL;
5082 static const struct got_error *
5083 cancel_blame_view(void *arg)
5085 const struct got_error *err = NULL;
5089 errcode = pthread_mutex_lock(&tog_mutex);
5091 return got_error_set_errno(errcode,
5092 "pthread_mutex_unlock");
5095 err = got_error(GOT_ERR_CANCELLED);
5097 errcode = pthread_mutex_unlock(&tog_mutex);
5099 return got_error_set_errno(errcode,
5100 "pthread_mutex_lock");
5105 static const struct got_error *
5106 run_blame(struct tog_view *view)
5108 struct tog_blame_view_state *s = &view->state.blame;
5109 struct tog_blame *blame = &s->blame;
5110 const struct got_error *err = NULL;
5111 struct got_commit_object *commit = NULL;
5112 struct got_blob_object *blob = NULL;
5113 struct got_repository *thread_repo = NULL;
5114 struct got_object_id *obj_id = NULL;
5115 int obj_type, fd = -1;
5116 int *pack_fds = NULL;
5118 err = got_object_open_as_commit(&commit, s->repo,
5119 &s->blamed_commit->id);
5123 fd = got_opentempfd();
5125 err = got_error_from_errno("got_opentempfd");
5129 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5133 err = got_object_get_type(&obj_type, s->repo, obj_id);
5137 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5138 err = got_error(GOT_ERR_OBJ_TYPE);
5142 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5145 blame->f = got_opentemp();
5146 if (blame->f == NULL) {
5147 err = got_error_from_errno("got_opentemp");
5150 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5151 &blame->line_offsets, blame->f, blob);
5154 if (blame->nlines == 0) {
5155 s->blame_complete = 1;
5159 /* Don't include \n at EOF in the blame line count. */
5160 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5163 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5164 if (blame->lines == NULL) {
5165 err = got_error_from_errno("calloc");
5169 err = got_repo_pack_fds_open(&pack_fds);
5172 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5177 blame->pack_fds = pack_fds;
5178 blame->cb_args.view = view;
5179 blame->cb_args.lines = blame->lines;
5180 blame->cb_args.nlines = blame->nlines;
5181 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5182 if (blame->cb_args.commit_id == NULL) {
5183 err = got_error_from_errno("got_object_id_dup");
5186 blame->cb_args.quit = &s->done;
5188 blame->thread_args.path = s->path;
5189 blame->thread_args.repo = thread_repo;
5190 blame->thread_args.cb_args = &blame->cb_args;
5191 blame->thread_args.complete = &s->blame_complete;
5192 blame->thread_args.cancel_cb = cancel_blame_view;
5193 blame->thread_args.cancel_arg = &s->done;
5194 s->blame_complete = 0;
5196 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5197 s->first_displayed_line = 1;
5198 s->last_displayed_line = view->nlines;
5199 s->selected_line = 1;
5201 s->matched_line = 0;
5205 got_object_commit_close(commit);
5206 if (fd != -1 && close(fd) == -1 && err == NULL)
5207 err = got_error_from_errno("close");
5209 got_object_blob_close(blob);
5216 static const struct got_error *
5217 open_blame_view(struct tog_view *view, char *path,
5218 struct got_object_id *commit_id, struct got_repository *repo)
5220 const struct got_error *err = NULL;
5221 struct tog_blame_view_state *s = &view->state.blame;
5223 STAILQ_INIT(&s->blamed_commits);
5225 s->path = strdup(path);
5226 if (s->path == NULL)
5227 return got_error_from_errno("strdup");
5229 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5235 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5236 s->first_displayed_line = 1;
5237 s->last_displayed_line = view->nlines;
5238 s->selected_line = 1;
5239 s->blame_complete = 0;
5241 s->commit_id = commit_id;
5242 memset(&s->blame, 0, sizeof(s->blame));
5244 STAILQ_INIT(&s->colors);
5245 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5246 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5247 get_color_value("TOG_COLOR_COMMIT"));
5252 view->show = show_blame_view;
5253 view->input = input_blame_view;
5254 view->reset = reset_blame_view;
5255 view->close = close_blame_view;
5256 view->search_start = search_start_blame_view;
5257 view->search_next = search_next_blame_view;
5259 return run_blame(view);
5262 static const struct got_error *
5263 close_blame_view(struct tog_view *view)
5265 const struct got_error *err = NULL;
5266 struct tog_blame_view_state *s = &view->state.blame;
5268 if (s->blame.thread)
5269 err = stop_blame(&s->blame);
5271 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5272 struct got_object_qid *blamed_commit;
5273 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5274 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5275 got_object_qid_free(blamed_commit);
5279 free_colors(&s->colors);
5283 static const struct got_error *
5284 search_start_blame_view(struct tog_view *view)
5286 struct tog_blame_view_state *s = &view->state.blame;
5288 s->matched_line = 0;
5292 static const struct got_error *
5293 search_next_blame_view(struct tog_view *view)
5295 struct tog_blame_view_state *s = &view->state.blame;
5296 const struct got_error *err = NULL;
5299 size_t linesize = 0;
5302 if (!view->searching) {
5303 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5307 if (s->matched_line) {
5308 if (view->searching == TOG_SEARCH_FORWARD)
5309 lineno = s->matched_line + 1;
5311 lineno = s->matched_line - 1;
5313 lineno = s->first_displayed_line - 1 + s->selected_line;
5318 if (lineno <= 0 || lineno > s->blame.nlines) {
5319 if (s->matched_line == 0) {
5320 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5324 if (view->searching == TOG_SEARCH_FORWARD)
5327 lineno = s->blame.nlines;
5330 offset = s->blame.line_offsets[lineno - 1];
5331 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5333 return got_error_from_errno("fseeko");
5335 linelen = getline(&line, &linesize, s->blame.f);
5336 if (linelen != -1) {
5338 err = expand_tab(&exstr, line);
5341 if (match_line(exstr, &view->regex, 1,
5343 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5344 s->matched_line = lineno;
5350 if (view->searching == TOG_SEARCH_FORWARD)
5357 if (s->matched_line) {
5358 s->first_displayed_line = s->matched_line;
5359 s->selected_line = 1;
5365 static const struct got_error *
5366 show_blame_view(struct tog_view *view)
5368 const struct got_error *err = NULL;
5369 struct tog_blame_view_state *s = &view->state.blame;
5372 if (s->blame.thread == NULL && !s->blame_complete) {
5373 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5374 &s->blame.thread_args);
5376 return got_error_set_errno(errcode, "pthread_create");
5378 halfdelay(1); /* fast refresh while annotating */
5381 if (s->blame_complete)
5382 halfdelay(10); /* disable fast refresh */
5384 err = draw_blame(view);
5390 static const struct got_error *
5391 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5393 const struct got_error *err = NULL, *thread_err = NULL;
5394 struct tog_view *diff_view;
5395 struct tog_blame_view_state *s = &view->state.blame;
5396 int eos, nscroll, begin_y = 0, begin_x = 0;
5398 eos = nscroll = view->nlines - 2;
5399 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5400 view_is_splitscreen(view->child))
5408 view->x = MAX(view->maxx - view->ncols / 3, 0);
5413 if (view->x + view->ncols / 3 < view->maxx)
5414 view->x += 2; /* move two columns right */
5420 view->x -= MIN(view->x, 2); /* move two columns back */
5429 s->selected_line = 1;
5430 s->first_displayed_line = 1;
5435 if (s->blame.nlines < eos) {
5436 s->selected_line = s->blame.nlines;
5437 s->first_displayed_line = 1;
5439 s->selected_line = eos;
5440 s->first_displayed_line = s->blame.nlines - (eos - 1);
5447 if (s->selected_line > 1)
5449 else if (s->selected_line == 1 &&
5450 s->first_displayed_line > 1)
5451 s->first_displayed_line--;
5462 if (s->first_displayed_line == 1) {
5463 if (view->count > 1)
5465 s->selected_line = MAX(1, s->selected_line - nscroll);
5469 if (s->first_displayed_line > nscroll)
5470 s->first_displayed_line -= nscroll;
5472 s->first_displayed_line = 1;
5477 if (s->selected_line < eos && s->first_displayed_line +
5478 s->selected_line <= s->blame.nlines)
5480 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5481 s->first_displayed_line++;
5487 struct got_object_id *id = NULL;
5490 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5491 s->first_displayed_line, s->selected_line);
5495 struct got_commit_object *commit, *pcommit;
5496 struct got_object_qid *pid;
5497 struct got_object_id *blob_id = NULL;
5499 err = got_object_open_as_commit(&commit,
5504 got_object_commit_get_parent_ids(commit));
5506 got_object_commit_close(commit);
5509 /* Check if path history ends here. */
5510 err = got_object_open_as_commit(&pcommit,
5514 err = got_object_id_by_path(&blob_id, s->repo,
5516 got_object_commit_close(pcommit);
5518 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5520 got_object_commit_close(commit);
5523 err = got_object_get_type(&obj_type, s->repo,
5526 /* Can't blame non-blob type objects. */
5527 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5528 got_object_commit_close(commit);
5531 err = got_object_qid_alloc(&s->blamed_commit,
5533 got_object_commit_close(commit);
5535 if (got_object_id_cmp(id,
5536 &s->blamed_commit->id) == 0)
5538 err = got_object_qid_alloc(&s->blamed_commit,
5544 thread_err = stop_blame(&s->blame);
5548 STAILQ_INSERT_HEAD(&s->blamed_commits,
5549 s->blamed_commit, entry);
5550 err = run_blame(view);
5556 struct got_object_qid *first;
5559 first = STAILQ_FIRST(&s->blamed_commits);
5560 if (!got_object_id_cmp(&first->id, s->commit_id))
5563 thread_err = stop_blame(&s->blame);
5567 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5568 got_object_qid_free(s->blamed_commit);
5570 STAILQ_FIRST(&s->blamed_commits);
5571 err = run_blame(view);
5578 struct got_object_id *id = NULL;
5579 struct got_object_qid *pid;
5580 struct got_commit_object *commit = NULL;
5583 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5584 s->first_displayed_line, s->selected_line);
5587 err = got_object_open_as_commit(&commit, s->repo, id);
5590 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5591 if (view_is_parent_view(view))
5592 view_get_split(view, &begin_y, &begin_x);
5594 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5595 if (diff_view == NULL) {
5596 got_object_commit_close(commit);
5597 err = got_error_from_errno("view_open");
5600 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5601 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5602 got_object_commit_close(commit);
5604 view_close(diff_view);
5607 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5608 err = view_init_hsplit(view, begin_y);
5614 diff_view->focussed = 1;
5615 diff_view->mode = view->mode;
5616 diff_view->nlines = view->lines - begin_y;
5617 if (view_is_parent_view(view)) {
5618 err = view_close_child(view);
5621 err = view_set_child(view, diff_view);
5624 view->focus_child = 1;
5626 *new_view = diff_view;
5639 if (s->last_displayed_line >= s->blame.nlines &&
5640 s->selected_line >= MIN(s->blame.nlines,
5641 view->nlines - 2)) {
5645 if (s->last_displayed_line >= s->blame.nlines &&
5646 s->selected_line < view->nlines - 2) {
5648 MIN(nscroll, s->last_displayed_line -
5649 s->first_displayed_line - s->selected_line + 1);
5651 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5652 s->first_displayed_line += nscroll;
5654 s->first_displayed_line =
5655 s->blame.nlines - (view->nlines - 3);
5658 if (s->selected_line > view->nlines - 2) {
5659 s->selected_line = MIN(s->blame.nlines,
5667 return thread_err ? thread_err : err;
5670 static const struct got_error *
5671 reset_blame_view(struct tog_view *view)
5673 const struct got_error *err;
5674 struct tog_blame_view_state *s = &view->state.blame;
5678 err = stop_blame(&s->blame);
5682 return run_blame(view);
5685 static const struct got_error *
5686 cmd_blame(int argc, char *argv[])
5688 const struct got_error *error;
5689 struct got_repository *repo = NULL;
5690 struct got_worktree *worktree = NULL;
5691 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5692 char *link_target = NULL;
5693 struct got_object_id *commit_id = NULL;
5694 struct got_commit_object *commit = NULL;
5695 char *commit_id_str = NULL;
5697 struct tog_view *view;
5698 int *pack_fds = NULL;
5700 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5703 commit_id_str = optarg;
5706 repo_path = realpath(optarg, NULL);
5707 if (repo_path == NULL)
5708 return got_error_from_errno2("realpath",
5723 error = got_repo_pack_fds_open(&pack_fds);
5727 if (repo_path == NULL) {
5728 cwd = getcwd(NULL, 0);
5730 return got_error_from_errno("getcwd");
5731 error = got_worktree_open(&worktree, cwd);
5732 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5736 strdup(got_worktree_get_repo_path(worktree));
5738 repo_path = strdup(cwd);
5739 if (repo_path == NULL) {
5740 error = got_error_from_errno("strdup");
5745 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5749 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5756 error = apply_unveil(got_repo_get_path(repo), NULL);
5760 error = tog_load_refs(repo, 0);
5764 if (commit_id_str == NULL) {
5765 struct got_reference *head_ref;
5766 error = got_ref_open(&head_ref, repo, worktree ?
5767 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5770 error = got_ref_resolve(&commit_id, repo, head_ref);
5771 got_ref_close(head_ref);
5773 error = got_repo_match_object_id(&commit_id, NULL,
5774 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5779 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5781 error = got_error_from_errno("view_open");
5785 error = got_object_open_as_commit(&commit, repo, commit_id);
5789 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5794 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5799 /* Release work tree lock. */
5800 got_worktree_close(worktree);
5803 error = view_loop(view);
5811 got_object_commit_close(commit);
5813 got_worktree_close(worktree);
5815 const struct got_error *close_err = got_repo_close(repo);
5820 const struct got_error *pack_err =
5821 got_repo_pack_fds_close(pack_fds);
5829 static const struct got_error *
5830 draw_tree_entries(struct tog_view *view, const char *parent_path)
5832 struct tog_tree_view_state *s = &view->state.tree;
5833 const struct got_error *err = NULL;
5834 struct got_tree_entry *te;
5836 struct tog_color *tc;
5837 int width, n, i, nentries;
5838 int limit = view->nlines;
5841 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5842 view_is_splitscreen(view->child))
5843 --limit; /* border */
5845 werase(view->window);
5850 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5854 if (view_needs_focus_indication(view))
5855 wstandout(view->window);
5856 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5858 wattr_on(view->window,
5859 COLOR_PAIR(tc->colorpair), NULL);
5860 waddwstr(view->window, wline);
5862 wattr_off(view->window,
5863 COLOR_PAIR(tc->colorpair), NULL);
5864 if (view_needs_focus_indication(view))
5865 wstandend(view->window);
5868 if (width < view->ncols - 1)
5869 waddch(view->window, '\n');
5872 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5876 waddwstr(view->window, wline);
5879 if (width < view->ncols - 1)
5880 waddch(view->window, '\n');
5883 waddch(view->window, '\n');
5887 if (s->first_displayed_entry == NULL) {
5888 te = got_object_tree_get_first_entry(s->tree);
5889 if (s->selected == 0) {
5891 wstandout(view->window);
5892 s->selected_entry = NULL;
5894 waddstr(view->window, " ..\n"); /* parent directory */
5895 if (s->selected == 0 && view->focussed)
5896 wstandend(view->window);
5903 te = s->first_displayed_entry;
5906 nentries = got_object_tree_get_nentries(s->tree);
5907 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5908 char *line = NULL, *id_str = NULL, *link_target = NULL;
5909 const char *modestr = "";
5912 te = got_object_tree_get_entry(s->tree, i);
5913 mode = got_tree_entry_get_mode(te);
5916 err = got_object_id_str(&id_str,
5917 got_tree_entry_get_id(te));
5919 return got_error_from_errno(
5920 "got_object_id_str");
5922 if (got_object_tree_entry_is_submodule(te))
5924 else if (S_ISLNK(mode)) {
5927 err = got_tree_entry_get_symlink_target(&link_target,
5933 for (i = 0; i < strlen(link_target); i++) {
5934 if (!isprint((unsigned char)link_target[i]))
5935 link_target[i] = '?';
5939 else if (S_ISDIR(mode))
5941 else if (mode & S_IXUSR)
5943 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5944 got_tree_entry_get_name(te), modestr,
5945 link_target ? " -> ": "",
5946 link_target ? link_target : "") == -1) {
5949 return got_error_from_errno("asprintf");
5953 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5959 if (n == s->selected) {
5961 wstandout(view->window);
5962 s->selected_entry = te;
5964 tc = match_color(&s->colors, line);
5966 wattr_on(view->window,
5967 COLOR_PAIR(tc->colorpair), NULL);
5968 waddwstr(view->window, wline);
5970 wattr_off(view->window,
5971 COLOR_PAIR(tc->colorpair), NULL);
5972 if (width < view->ncols - 1)
5973 waddch(view->window, '\n');
5974 if (n == s->selected && view->focussed)
5975 wstandend(view->window);
5981 s->last_displayed_entry = te;
5990 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5992 struct got_tree_entry *te;
5993 int isroot = s->tree == s->root;
5996 if (s->first_displayed_entry == NULL)
5999 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6000 while (i++ < maxscroll) {
6003 s->first_displayed_entry = NULL;
6006 s->first_displayed_entry = te;
6007 te = got_tree_entry_get_prev(s->tree, te);
6011 static const struct got_error *
6012 tree_scroll_down(struct tog_view *view, int maxscroll)
6014 struct tog_tree_view_state *s = &view->state.tree;
6015 struct got_tree_entry *next, *last;
6018 if (s->first_displayed_entry)
6019 next = got_tree_entry_get_next(s->tree,
6020 s->first_displayed_entry);
6022 next = got_object_tree_get_first_entry(s->tree);
6024 last = s->last_displayed_entry;
6025 while (next && n++ < maxscroll) {
6027 last = got_tree_entry_get_next(s->tree, last);
6028 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6029 s->first_displayed_entry = next;
6030 next = got_tree_entry_get_next(s->tree, next);
6037 static const struct got_error *
6038 tree_entry_path(char **path, struct tog_parent_trees *parents,
6039 struct got_tree_entry *te)
6041 const struct got_error *err = NULL;
6042 struct tog_parent_tree *pt;
6043 size_t len = 2; /* for leading slash and NUL */
6045 TAILQ_FOREACH(pt, parents, entry)
6046 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6049 len += strlen(got_tree_entry_get_name(te));
6051 *path = calloc(1, len);
6053 return got_error_from_errno("calloc");
6056 pt = TAILQ_LAST(parents, tog_parent_trees);
6058 const char *name = got_tree_entry_get_name(pt->selected_entry);
6059 if (strlcat(*path, name, len) >= len) {
6060 err = got_error(GOT_ERR_NO_SPACE);
6063 if (strlcat(*path, "/", len) >= len) {
6064 err = got_error(GOT_ERR_NO_SPACE);
6067 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6070 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6071 err = got_error(GOT_ERR_NO_SPACE);
6083 static const struct got_error *
6084 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6085 struct got_tree_entry *te, struct tog_parent_trees *parents,
6086 struct got_object_id *commit_id, struct got_repository *repo)
6088 const struct got_error *err = NULL;
6090 struct tog_view *blame_view;
6094 err = tree_entry_path(&path, parents, te);
6098 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6099 if (blame_view == NULL) {
6100 err = got_error_from_errno("view_open");
6104 err = open_blame_view(blame_view, path, commit_id, repo);
6106 if (err->code == GOT_ERR_CANCELLED)
6108 view_close(blame_view);
6110 *new_view = blame_view;
6116 static const struct got_error *
6117 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6118 struct tog_tree_view_state *s)
6120 struct tog_view *log_view;
6121 const struct got_error *err = NULL;
6126 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6127 if (log_view == NULL)
6128 return got_error_from_errno("view_open");
6130 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6134 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6137 view_close(log_view);
6139 *new_view = log_view;
6144 static const struct got_error *
6145 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6146 const char *head_ref_name, struct got_repository *repo)
6148 const struct got_error *err = NULL;
6149 char *commit_id_str = NULL;
6150 struct tog_tree_view_state *s = &view->state.tree;
6151 struct got_commit_object *commit = NULL;
6153 TAILQ_INIT(&s->parents);
6154 STAILQ_INIT(&s->colors);
6156 s->commit_id = got_object_id_dup(commit_id);
6157 if (s->commit_id == NULL)
6158 return got_error_from_errno("got_object_id_dup");
6160 err = got_object_open_as_commit(&commit, repo, commit_id);
6165 * The root is opened here and will be closed when the view is closed.
6166 * Any visited subtrees and their path-wise parents are opened and
6169 err = got_object_open_as_tree(&s->root, repo,
6170 got_object_commit_get_tree_id(commit));
6175 err = got_object_id_str(&commit_id_str, commit_id);
6179 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6180 err = got_error_from_errno("asprintf");
6184 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6185 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6186 if (head_ref_name) {
6187 s->head_ref_name = strdup(head_ref_name);
6188 if (s->head_ref_name == NULL) {
6189 err = got_error_from_errno("strdup");
6195 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6196 err = add_color(&s->colors, "\\$$",
6197 TOG_COLOR_TREE_SUBMODULE,
6198 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6201 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6202 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6205 err = add_color(&s->colors, "/$",
6206 TOG_COLOR_TREE_DIRECTORY,
6207 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6211 err = add_color(&s->colors, "\\*$",
6212 TOG_COLOR_TREE_EXECUTABLE,
6213 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6217 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6218 get_color_value("TOG_COLOR_COMMIT"));
6223 view->show = show_tree_view;
6224 view->input = input_tree_view;
6225 view->close = close_tree_view;
6226 view->search_start = search_start_tree_view;
6227 view->search_next = search_next_tree_view;
6229 free(commit_id_str);
6231 got_object_commit_close(commit);
6233 close_tree_view(view);
6237 static const struct got_error *
6238 close_tree_view(struct tog_view *view)
6240 struct tog_tree_view_state *s = &view->state.tree;
6242 free_colors(&s->colors);
6243 free(s->tree_label);
6244 s->tree_label = NULL;
6246 s->commit_id = NULL;
6247 free(s->head_ref_name);
6248 s->head_ref_name = NULL;
6249 while (!TAILQ_EMPTY(&s->parents)) {
6250 struct tog_parent_tree *parent;
6251 parent = TAILQ_FIRST(&s->parents);
6252 TAILQ_REMOVE(&s->parents, parent, entry);
6253 if (parent->tree != s->root)
6254 got_object_tree_close(parent->tree);
6258 if (s->tree != NULL && s->tree != s->root)
6259 got_object_tree_close(s->tree);
6261 got_object_tree_close(s->root);
6265 static const struct got_error *
6266 search_start_tree_view(struct tog_view *view)
6268 struct tog_tree_view_state *s = &view->state.tree;
6270 s->matched_entry = NULL;
6275 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6277 regmatch_t regmatch;
6279 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6283 static const struct got_error *
6284 search_next_tree_view(struct tog_view *view)
6286 struct tog_tree_view_state *s = &view->state.tree;
6287 struct got_tree_entry *te = NULL;
6289 if (!view->searching) {
6290 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6294 if (s->matched_entry) {
6295 if (view->searching == TOG_SEARCH_FORWARD) {
6296 if (s->selected_entry)
6297 te = got_tree_entry_get_next(s->tree,
6300 te = got_object_tree_get_first_entry(s->tree);
6302 if (s->selected_entry == NULL)
6303 te = got_object_tree_get_last_entry(s->tree);
6305 te = got_tree_entry_get_prev(s->tree,
6309 if (s->selected_entry)
6310 te = s->selected_entry;
6311 else if (view->searching == TOG_SEARCH_FORWARD)
6312 te = got_object_tree_get_first_entry(s->tree);
6314 te = got_object_tree_get_last_entry(s->tree);
6319 if (s->matched_entry == NULL) {
6320 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6323 if (view->searching == TOG_SEARCH_FORWARD)
6324 te = got_object_tree_get_first_entry(s->tree);
6326 te = got_object_tree_get_last_entry(s->tree);
6329 if (match_tree_entry(te, &view->regex)) {
6330 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6331 s->matched_entry = te;
6335 if (view->searching == TOG_SEARCH_FORWARD)
6336 te = got_tree_entry_get_next(s->tree, te);
6338 te = got_tree_entry_get_prev(s->tree, te);
6341 if (s->matched_entry) {
6342 s->first_displayed_entry = s->matched_entry;
6349 static const struct got_error *
6350 show_tree_view(struct tog_view *view)
6352 const struct got_error *err = NULL;
6353 struct tog_tree_view_state *s = &view->state.tree;
6356 err = tree_entry_path(&parent_path, &s->parents, NULL);
6360 err = draw_tree_entries(view, parent_path);
6367 static const struct got_error *
6368 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6370 const struct got_error *err = NULL;
6371 struct tog_tree_view_state *s = &view->state.tree;
6372 struct tog_view *log_view, *ref_view;
6373 struct got_tree_entry *te;
6374 int begin_x = 0, n, nscroll = view->nlines - 3;
6378 s->show_ids = !s->show_ids;
6383 if (!s->selected_entry)
6385 if (view_is_parent_view(view))
6386 begin_x = view_split_begin_x(view->begin_x);
6387 err = log_selected_tree_entry(&log_view, begin_x, s);
6389 log_view->focussed = 1;
6390 if (view_is_parent_view(view)) {
6391 err = view_close_child(view);
6394 err = view_set_child(view, log_view);
6397 view->focus_child = 1;
6399 *new_view = log_view;
6403 if (view_is_parent_view(view))
6404 begin_x = view_split_begin_x(view->begin_x);
6405 ref_view = view_open(view->nlines, view->ncols,
6406 view->begin_y, begin_x, TOG_VIEW_REF);
6407 if (ref_view == NULL)
6408 return got_error_from_errno("view_open");
6409 err = open_ref_view(ref_view, s->repo);
6411 view_close(ref_view);
6415 ref_view->focussed = 1;
6416 if (view_is_parent_view(view)) {
6417 err = view_close_child(view);
6420 err = view_set_child(view, ref_view);
6423 view->focus_child = 1;
6425 *new_view = ref_view;
6431 if (s->tree == s->root)
6432 s->first_displayed_entry =
6433 got_object_tree_get_first_entry(s->tree);
6435 s->first_displayed_entry = NULL;
6439 int eos = view->nlines - 3;
6441 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6445 te = got_object_tree_get_last_entry(s->tree);
6446 for (n = 0; n < eos; n++) {
6448 if(s->tree != s->root) {
6449 s->first_displayed_entry = NULL;
6454 s->first_displayed_entry = te;
6455 te = got_tree_entry_get_prev(s->tree, te);
6458 s->selected = n - 1;
6464 if (s->selected > 0) {
6468 tree_scroll_up(s, 1);
6469 if (s->selected_entry == NULL ||
6470 (s->tree == s->root && s->selected_entry ==
6471 got_object_tree_get_first_entry(s->tree)))
6481 if (s->tree == s->root) {
6482 if (got_object_tree_get_first_entry(s->tree) ==
6483 s->first_displayed_entry)
6484 s->selected -= MIN(s->selected, nscroll);
6486 if (s->first_displayed_entry == NULL)
6487 s->selected -= MIN(s->selected, nscroll);
6489 tree_scroll_up(s, MAX(0, nscroll));
6490 if (s->selected_entry == NULL ||
6491 (s->tree == s->root && s->selected_entry ==
6492 got_object_tree_get_first_entry(s->tree)))
6498 if (s->selected < s->ndisplayed - 1) {
6502 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6504 /* can't scroll any further */
6508 tree_scroll_down(view, 1);
6518 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6520 /* can't scroll any further; move cursor down */
6521 if (s->selected < s->ndisplayed - 1)
6522 s->selected += MIN(nscroll,
6523 s->ndisplayed - s->selected - 1);
6528 tree_scroll_down(view, nscroll);
6533 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6534 struct tog_parent_tree *parent;
6535 /* user selected '..' */
6536 if (s->tree == s->root) {
6540 parent = TAILQ_FIRST(&s->parents);
6541 TAILQ_REMOVE(&s->parents, parent,
6543 got_object_tree_close(s->tree);
6544 s->tree = parent->tree;
6545 s->first_displayed_entry =
6546 parent->first_displayed_entry;
6548 parent->selected_entry;
6549 s->selected = parent->selected;
6550 if (s->selected > view->nlines - 3) {
6551 err = offset_selection_down(view);
6556 } else if (S_ISDIR(got_tree_entry_get_mode(
6557 s->selected_entry))) {
6558 struct got_tree_object *subtree;
6560 err = got_object_open_as_tree(&subtree, s->repo,
6561 got_tree_entry_get_id(s->selected_entry));
6564 err = tree_view_visit_subtree(s, subtree);
6566 got_object_tree_close(subtree);
6569 } else if (S_ISREG(got_tree_entry_get_mode(
6570 s->selected_entry))) {
6571 struct tog_view *blame_view;
6572 int begin_x = 0, begin_y = 0;
6574 if (view_is_parent_view(view))
6575 view_get_split(view, &begin_y, &begin_x);
6577 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6578 s->selected_entry, &s->parents,
6579 s->commit_id, s->repo);
6583 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6584 err = view_init_hsplit(view, begin_y);
6591 blame_view->focussed = 1;
6592 blame_view->mode = view->mode;
6593 blame_view->nlines = view->lines - begin_y;
6594 if (view_is_parent_view(view)) {
6595 err = view_close_child(view);
6598 err = view_set_child(view, blame_view);
6601 view->focus_child = 1;
6603 *new_view = blame_view;
6607 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6608 s->selected = view->nlines - 4;
6624 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6629 static const struct got_error *
6630 cmd_tree(int argc, char *argv[])
6632 const struct got_error *error;
6633 struct got_repository *repo = NULL;
6634 struct got_worktree *worktree = NULL;
6635 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6636 struct got_object_id *commit_id = NULL;
6637 struct got_commit_object *commit = NULL;
6638 const char *commit_id_arg = NULL;
6640 struct got_reference *ref = NULL;
6641 const char *head_ref_name = NULL;
6643 struct tog_view *view;
6644 int *pack_fds = NULL;
6646 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6649 commit_id_arg = optarg;
6652 repo_path = realpath(optarg, NULL);
6653 if (repo_path == NULL)
6654 return got_error_from_errno2("realpath",
6669 error = got_repo_pack_fds_open(&pack_fds);
6673 if (repo_path == NULL) {
6674 cwd = getcwd(NULL, 0);
6676 return got_error_from_errno("getcwd");
6677 error = got_worktree_open(&worktree, cwd);
6678 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6682 strdup(got_worktree_get_repo_path(worktree));
6684 repo_path = strdup(cwd);
6685 if (repo_path == NULL) {
6686 error = got_error_from_errno("strdup");
6691 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6695 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6702 error = apply_unveil(got_repo_get_path(repo), NULL);
6706 error = tog_load_refs(repo, 0);
6710 if (commit_id_arg == NULL) {
6711 error = got_repo_match_object_id(&commit_id, &label,
6712 worktree ? got_worktree_get_head_ref_name(worktree) :
6713 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6716 head_ref_name = label;
6718 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6720 head_ref_name = got_ref_get_name(ref);
6721 else if (error->code != GOT_ERR_NOT_REF)
6723 error = got_repo_match_object_id(&commit_id, NULL,
6724 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6729 error = got_object_open_as_commit(&commit, repo, commit_id);
6733 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6735 error = got_error_from_errno("view_open");
6738 error = open_tree_view(view, commit_id, head_ref_name, repo);
6741 if (!got_path_is_root_dir(in_repo_path)) {
6742 error = tree_view_walk_path(&view->state.tree, commit,
6749 /* Release work tree lock. */
6750 got_worktree_close(worktree);
6753 error = view_loop(view);
6762 const struct got_error *close_err = got_repo_close(repo);
6767 const struct got_error *pack_err =
6768 got_repo_pack_fds_close(pack_fds);
6776 static const struct got_error *
6777 ref_view_load_refs(struct tog_ref_view_state *s)
6779 struct got_reflist_entry *sre;
6780 struct tog_reflist_entry *re;
6783 TAILQ_FOREACH(sre, &tog_refs, entry) {
6784 if (strncmp(got_ref_get_name(sre->ref),
6785 "refs/got/", 9) == 0 &&
6786 strncmp(got_ref_get_name(sre->ref),
6787 "refs/got/backup/", 16) != 0)
6790 re = malloc(sizeof(*re));
6792 return got_error_from_errno("malloc");
6794 re->ref = got_ref_dup(sre->ref);
6795 if (re->ref == NULL)
6796 return got_error_from_errno("got_ref_dup");
6797 re->idx = s->nrefs++;
6798 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6801 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6806 ref_view_free_refs(struct tog_ref_view_state *s)
6808 struct tog_reflist_entry *re;
6810 while (!TAILQ_EMPTY(&s->refs)) {
6811 re = TAILQ_FIRST(&s->refs);
6812 TAILQ_REMOVE(&s->refs, re, entry);
6813 got_ref_close(re->ref);
6818 static const struct got_error *
6819 open_ref_view(struct tog_view *view, struct got_repository *repo)
6821 const struct got_error *err = NULL;
6822 struct tog_ref_view_state *s = &view->state.ref;
6824 s->selected_entry = 0;
6827 TAILQ_INIT(&s->refs);
6828 STAILQ_INIT(&s->colors);
6830 err = ref_view_load_refs(s);
6834 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6835 err = add_color(&s->colors, "^refs/heads/",
6836 TOG_COLOR_REFS_HEADS,
6837 get_color_value("TOG_COLOR_REFS_HEADS"));
6841 err = add_color(&s->colors, "^refs/tags/",
6842 TOG_COLOR_REFS_TAGS,
6843 get_color_value("TOG_COLOR_REFS_TAGS"));
6847 err = add_color(&s->colors, "^refs/remotes/",
6848 TOG_COLOR_REFS_REMOTES,
6849 get_color_value("TOG_COLOR_REFS_REMOTES"));
6853 err = add_color(&s->colors, "^refs/got/backup/",
6854 TOG_COLOR_REFS_BACKUP,
6855 get_color_value("TOG_COLOR_REFS_BACKUP"));
6860 view->show = show_ref_view;
6861 view->input = input_ref_view;
6862 view->close = close_ref_view;
6863 view->search_start = search_start_ref_view;
6864 view->search_next = search_next_ref_view;
6867 free_colors(&s->colors);
6871 static const struct got_error *
6872 close_ref_view(struct tog_view *view)
6874 struct tog_ref_view_state *s = &view->state.ref;
6876 ref_view_free_refs(s);
6877 free_colors(&s->colors);
6882 static const struct got_error *
6883 resolve_reflist_entry(struct got_object_id **commit_id,
6884 struct tog_reflist_entry *re, struct got_repository *repo)
6886 const struct got_error *err = NULL;
6887 struct got_object_id *obj_id;
6888 struct got_tag_object *tag = NULL;
6893 err = got_ref_resolve(&obj_id, repo, re->ref);
6897 err = got_object_get_type(&obj_type, repo, obj_id);
6902 case GOT_OBJ_TYPE_COMMIT:
6903 *commit_id = obj_id;
6905 case GOT_OBJ_TYPE_TAG:
6906 err = got_object_open_as_tag(&tag, repo, obj_id);
6910 err = got_object_get_type(&obj_type, repo,
6911 got_object_tag_get_object_id(tag));
6914 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6915 err = got_error(GOT_ERR_OBJ_TYPE);
6918 *commit_id = got_object_id_dup(
6919 got_object_tag_get_object_id(tag));
6920 if (*commit_id == NULL) {
6921 err = got_error_from_errno("got_object_id_dup");
6926 err = got_error(GOT_ERR_OBJ_TYPE);
6932 got_object_tag_close(tag);
6940 static const struct got_error *
6941 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6942 struct tog_reflist_entry *re, struct got_repository *repo)
6944 struct tog_view *log_view;
6945 const struct got_error *err = NULL;
6946 struct got_object_id *commit_id = NULL;
6950 err = resolve_reflist_entry(&commit_id, re, repo);
6952 if (err->code != GOT_ERR_OBJ_TYPE)
6958 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6959 if (log_view == NULL) {
6960 err = got_error_from_errno("view_open");
6964 err = open_log_view(log_view, commit_id, repo,
6965 got_ref_get_name(re->ref), "", 0);
6968 view_close(log_view);
6970 *new_view = log_view;
6976 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6978 struct tog_reflist_entry *re;
6981 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6984 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6985 while (i++ < maxscroll) {
6988 s->first_displayed_entry = re;
6989 re = TAILQ_PREV(re, tog_reflist_head, entry);
6993 static const struct got_error *
6994 ref_scroll_down(struct tog_view *view, int maxscroll)
6996 struct tog_ref_view_state *s = &view->state.ref;
6997 struct tog_reflist_entry *next, *last;
7000 if (s->first_displayed_entry)
7001 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7003 next = TAILQ_FIRST(&s->refs);
7005 last = s->last_displayed_entry;
7006 while (next && n++ < maxscroll) {
7008 last = TAILQ_NEXT(last, entry);
7009 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7010 s->first_displayed_entry = next;
7011 next = TAILQ_NEXT(next, entry);
7018 static const struct got_error *
7019 search_start_ref_view(struct tog_view *view)
7021 struct tog_ref_view_state *s = &view->state.ref;
7023 s->matched_entry = NULL;
7028 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7030 regmatch_t regmatch;
7032 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7036 static const struct got_error *
7037 search_next_ref_view(struct tog_view *view)
7039 struct tog_ref_view_state *s = &view->state.ref;
7040 struct tog_reflist_entry *re = NULL;
7042 if (!view->searching) {
7043 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7047 if (s->matched_entry) {
7048 if (view->searching == TOG_SEARCH_FORWARD) {
7049 if (s->selected_entry)
7050 re = TAILQ_NEXT(s->selected_entry, entry);
7052 re = TAILQ_PREV(s->selected_entry,
7053 tog_reflist_head, entry);
7055 if (s->selected_entry == NULL)
7056 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7058 re = TAILQ_PREV(s->selected_entry,
7059 tog_reflist_head, entry);
7062 if (s->selected_entry)
7063 re = s->selected_entry;
7064 else if (view->searching == TOG_SEARCH_FORWARD)
7065 re = TAILQ_FIRST(&s->refs);
7067 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7072 if (s->matched_entry == NULL) {
7073 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7076 if (view->searching == TOG_SEARCH_FORWARD)
7077 re = TAILQ_FIRST(&s->refs);
7079 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7082 if (match_reflist_entry(re, &view->regex)) {
7083 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7084 s->matched_entry = re;
7088 if (view->searching == TOG_SEARCH_FORWARD)
7089 re = TAILQ_NEXT(re, entry);
7091 re = TAILQ_PREV(re, tog_reflist_head, entry);
7094 if (s->matched_entry) {
7095 s->first_displayed_entry = s->matched_entry;
7102 static const struct got_error *
7103 show_ref_view(struct tog_view *view)
7105 const struct got_error *err = NULL;
7106 struct tog_ref_view_state *s = &view->state.ref;
7107 struct tog_reflist_entry *re;
7110 struct tog_color *tc;
7112 int limit = view->nlines;
7114 werase(view->window);
7117 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7118 view_is_splitscreen(view->child))
7119 --limit; /* border */
7124 re = s->first_displayed_entry;
7126 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7128 return got_error_from_errno("asprintf");
7130 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7135 if (view_needs_focus_indication(view))
7136 wstandout(view->window);
7137 waddwstr(view->window, wline);
7138 if (view_needs_focus_indication(view))
7139 wstandend(view->window);
7144 if (width < view->ncols - 1)
7145 waddch(view->window, '\n');
7150 while (re && limit > 0) {
7152 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7155 struct got_commit_object *ci;
7156 struct got_tag_object *tag;
7157 struct got_object_id *id;
7161 err = got_ref_resolve(&id, s->repo, re->ref);
7164 err = got_object_open_as_tag(&tag, s->repo, id);
7166 if (err->code != GOT_ERR_OBJ_TYPE) {
7170 err = got_object_open_as_commit(&ci, s->repo,
7176 t = got_object_commit_get_committer_time(ci);
7177 got_object_commit_close(ci);
7179 t = got_object_tag_get_tagger_time(tag);
7180 got_object_tag_close(tag);
7183 if (gmtime_r(&t, &tm) == NULL)
7184 return got_error_from_errno("gmtime_r");
7185 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7186 return got_error(GOT_ERR_NO_SPACE);
7188 if (got_ref_is_symbolic(re->ref)) {
7189 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7190 ymd : "", got_ref_get_name(re->ref),
7191 got_ref_get_symref_target(re->ref)) == -1)
7192 return got_error_from_errno("asprintf");
7193 } else if (s->show_ids) {
7194 struct got_object_id *id;
7196 err = got_ref_resolve(&id, s->repo, re->ref);
7199 err = got_object_id_str(&id_str, id);
7204 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7205 got_ref_get_name(re->ref), id_str) == -1) {
7206 err = got_error_from_errno("asprintf");
7213 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7214 got_ref_get_name(re->ref)) == -1)
7215 return got_error_from_errno("asprintf");
7217 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7223 if (n == s->selected) {
7225 wstandout(view->window);
7226 s->selected_entry = re;
7228 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7230 wattr_on(view->window,
7231 COLOR_PAIR(tc->colorpair), NULL);
7232 waddwstr(view->window, wline);
7234 wattr_off(view->window,
7235 COLOR_PAIR(tc->colorpair), NULL);
7236 if (width < view->ncols - 1)
7237 waddch(view->window, '\n');
7238 if (n == s->selected && view->focussed)
7239 wstandend(view->window);
7245 s->last_displayed_entry = re;
7248 re = TAILQ_NEXT(re, entry);
7255 static const struct got_error *
7256 browse_ref_tree(struct tog_view **new_view, int begin_x,
7257 struct tog_reflist_entry *re, struct got_repository *repo)
7259 const struct got_error *err = NULL;
7260 struct got_object_id *commit_id = NULL;
7261 struct tog_view *tree_view;
7265 err = resolve_reflist_entry(&commit_id, re, repo);
7267 if (err->code != GOT_ERR_OBJ_TYPE)
7274 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7275 if (tree_view == NULL) {
7276 err = got_error_from_errno("view_open");
7280 err = open_tree_view(tree_view, commit_id,
7281 got_ref_get_name(re->ref), repo);
7285 *new_view = tree_view;
7290 static const struct got_error *
7291 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7293 const struct got_error *err = NULL;
7294 struct tog_ref_view_state *s = &view->state.ref;
7295 struct tog_view *log_view, *tree_view;
7296 struct tog_reflist_entry *re;
7297 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7301 s->show_ids = !s->show_ids;
7305 s->show_date = !s->show_date;
7309 s->sort_by_date = !s->sort_by_date;
7311 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7312 got_ref_cmp_by_commit_timestamp_descending :
7313 tog_ref_cmp_by_name, s->repo);
7316 got_reflist_object_id_map_free(tog_refs_idmap);
7317 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7318 &tog_refs, s->repo);
7321 ref_view_free_refs(s);
7322 err = ref_view_load_refs(s);
7327 if (!s->selected_entry)
7329 if (view_is_parent_view(view))
7330 view_get_split(view, &begin_y, &begin_x);
7332 err = log_ref_entry(&log_view, begin_y, begin_x,
7333 s->selected_entry, s->repo);
7337 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7338 err = view_init_hsplit(view, begin_y);
7344 log_view->focussed = 1;
7345 log_view->mode = view->mode;
7346 log_view->nlines = view->lines - begin_y;
7347 if (view_is_parent_view(view)) {
7348 err = view_close_child(view);
7351 err = view_set_child(view, log_view);
7354 view->focus_child = 1;
7356 *new_view = log_view;
7360 if (!s->selected_entry)
7362 if (view_is_parent_view(view))
7363 begin_x = view_split_begin_x(view->begin_x);
7364 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7366 if (err || tree_view == NULL)
7369 tree_view->focussed = 1;
7370 if (view_is_parent_view(view)) {
7371 err = view_close_child(view);
7374 err = view_set_child(view, tree_view);
7377 view->focus_child = 1;
7379 *new_view = tree_view;
7385 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7389 int eos = view->nlines - 1;
7391 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7395 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7396 for (n = 0; n < eos; n++) {
7399 s->first_displayed_entry = re;
7400 re = TAILQ_PREV(re, tog_reflist_head, entry);
7403 s->selected = n - 1;
7409 if (s->selected > 0) {
7413 ref_scroll_up(s, 1);
7414 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7424 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7425 s->selected -= MIN(nscroll, s->selected);
7426 ref_scroll_up(s, MAX(0, nscroll));
7427 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7433 if (s->selected < s->ndisplayed - 1) {
7437 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7438 /* can't scroll any further */
7442 ref_scroll_down(view, 1);
7452 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7453 /* can't scroll any further; move cursor down */
7454 if (s->selected < s->ndisplayed - 1)
7455 s->selected += MIN(nscroll,
7456 s->ndisplayed - s->selected - 1);
7457 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7458 s->selected += s->ndisplayed - s->selected - 1;
7462 ref_scroll_down(view, nscroll);
7467 err = tog_load_refs(s->repo, s->sort_by_date);
7470 ref_view_free_refs(s);
7471 err = ref_view_load_refs(s);
7474 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7475 s->selected = view->nlines - 2;
7489 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7494 static const struct got_error *
7495 cmd_ref(int argc, char *argv[])
7497 const struct got_error *error;
7498 struct got_repository *repo = NULL;
7499 struct got_worktree *worktree = NULL;
7500 char *cwd = NULL, *repo_path = NULL;
7502 struct tog_view *view;
7503 int *pack_fds = NULL;
7505 while ((ch = getopt(argc, argv, "r:")) != -1) {
7508 repo_path = realpath(optarg, NULL);
7509 if (repo_path == NULL)
7510 return got_error_from_errno2("realpath",
7525 error = got_repo_pack_fds_open(&pack_fds);
7529 if (repo_path == NULL) {
7530 cwd = getcwd(NULL, 0);
7532 return got_error_from_errno("getcwd");
7533 error = got_worktree_open(&worktree, cwd);
7534 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7538 strdup(got_worktree_get_repo_path(worktree));
7540 repo_path = strdup(cwd);
7541 if (repo_path == NULL) {
7542 error = got_error_from_errno("strdup");
7547 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7553 error = apply_unveil(got_repo_get_path(repo), NULL);
7557 error = tog_load_refs(repo, 0);
7561 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7563 error = got_error_from_errno("view_open");
7567 error = open_ref_view(view, repo);
7572 /* Release work tree lock. */
7573 got_worktree_close(worktree);
7576 error = view_loop(view);
7581 const struct got_error *close_err = got_repo_close(repo);
7586 const struct got_error *pack_err =
7587 got_repo_pack_fds_close(pack_fds);
7596 * If view was scrolled down to move the selected line into view when opening a
7597 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7600 offset_selection_up(struct tog_view *view)
7602 switch (view->type) {
7603 case TOG_VIEW_BLAME: {
7604 struct tog_blame_view_state *s = &view->state.blame;
7605 if (s->first_displayed_line == 1) {
7606 s->selected_line = MAX(s->selected_line - view->offset,
7610 if (s->first_displayed_line > view->offset)
7611 s->first_displayed_line -= view->offset;
7613 s->first_displayed_line = 1;
7614 s->selected_line += view->offset;
7618 log_scroll_up(&view->state.log, view->offset);
7619 view->state.log.selected += view->offset;
7622 ref_scroll_up(&view->state.ref, view->offset);
7623 view->state.ref.selected += view->offset;
7626 tree_scroll_up(&view->state.tree, view->offset);
7627 view->state.tree.selected += view->offset;
7637 * If the selected line is in the section of screen covered by the bottom split,
7638 * scroll down offset lines to move it into view and index its new position.
7640 static const struct got_error *
7641 offset_selection_down(struct tog_view *view)
7643 const struct got_error *err = NULL;
7644 const struct got_error *(*scrolld)(struct tog_view *, int);
7645 int *selected = NULL;
7648 switch (view->type) {
7649 case TOG_VIEW_BLAME: {
7650 struct tog_blame_view_state *s = &view->state.blame;
7653 if (s->selected_line > view->nlines - header) {
7654 offset = abs(view->nlines - s->selected_line - header);
7655 s->first_displayed_line += offset;
7656 s->selected_line -= offset;
7657 view->offset = offset;
7661 case TOG_VIEW_LOG: {
7662 struct tog_log_view_state *s = &view->state.log;
7663 scrolld = &log_scroll_down;
7665 selected = &s->selected;
7668 case TOG_VIEW_REF: {
7669 struct tog_ref_view_state *s = &view->state.ref;
7670 scrolld = &ref_scroll_down;
7672 selected = &s->selected;
7675 case TOG_VIEW_TREE: {
7676 struct tog_tree_view_state *s = &view->state.tree;
7677 scrolld = &tree_scroll_down;
7679 selected = &s->selected;
7689 if (selected && *selected > view->nlines - header) {
7690 offset = abs(view->nlines - *selected - header);
7691 view->offset = offset;
7692 if (scrolld && offset) {
7693 err = scrolld(view, offset);
7694 *selected -= offset;
7702 list_commands(FILE *fp)
7706 fprintf(fp, "commands:");
7707 for (i = 0; i < nitems(tog_commands); i++) {
7708 const struct tog_cmd *cmd = &tog_commands[i];
7709 fprintf(fp, " %s", cmd->name);
7715 usage(int hflag, int status)
7717 FILE *fp = (status == 0) ? stdout : stderr;
7719 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7722 fprintf(fp, "lazy usage: %s path\n", getprogname());
7729 make_argv(int argc, ...)
7737 argv = calloc(argc, sizeof(char *));
7740 for (i = 0; i < argc; i++) {
7741 argv[i] = strdup(va_arg(ap, char *));
7742 if (argv[i] == NULL)
7751 * Try to convert 'tog path' into a 'tog log path' command.
7752 * The user could simply have mistyped the command rather than knowingly
7753 * provided a path. So check whether argv[0] can in fact be resolved
7754 * to a path in the HEAD commit and print a special error if not.
7755 * This hack is for mpi@ <3
7757 static const struct got_error *
7758 tog_log_with_path(int argc, char *argv[])
7760 const struct got_error *error = NULL, *close_err;
7761 const struct tog_cmd *cmd = NULL;
7762 struct got_repository *repo = NULL;
7763 struct got_worktree *worktree = NULL;
7764 struct got_object_id *commit_id = NULL, *id = NULL;
7765 struct got_commit_object *commit = NULL;
7766 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7767 char *commit_id_str = NULL, **cmd_argv = NULL;
7768 int *pack_fds = NULL;
7770 cwd = getcwd(NULL, 0);
7772 return got_error_from_errno("getcwd");
7774 error = got_repo_pack_fds_open(&pack_fds);
7778 error = got_worktree_open(&worktree, cwd);
7779 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7783 repo_path = strdup(got_worktree_get_repo_path(worktree));
7785 repo_path = strdup(cwd);
7786 if (repo_path == NULL) {
7787 error = got_error_from_errno("strdup");
7791 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7795 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7800 error = tog_load_refs(repo, 0);
7803 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7804 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7805 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7810 got_worktree_close(worktree);
7814 error = got_object_open_as_commit(&commit, repo, commit_id);
7818 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7820 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7822 fprintf(stderr, "%s: '%s' is no known command or path\n",
7823 getprogname(), argv[0]);
7828 close_err = got_repo_close(repo);
7833 error = got_object_id_str(&commit_id_str, commit_id);
7837 cmd = &tog_commands[0]; /* log */
7839 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7840 error = cmd->cmd_main(argc, cmd_argv);
7843 close_err = got_repo_close(repo);
7848 got_object_commit_close(commit);
7850 got_worktree_close(worktree);
7852 const struct got_error *pack_err =
7853 got_repo_pack_fds_close(pack_fds);
7858 free(commit_id_str);
7865 for (i = 0; i < argc; i++)
7874 main(int argc, char *argv[])
7876 const struct got_error *error = NULL;
7877 const struct tog_cmd *cmd = NULL;
7878 int ch, hflag = 0, Vflag = 0;
7879 char **cmd_argv = NULL;
7880 static const struct option longopts[] = {
7881 { "version", no_argument, NULL, 'V' },
7884 char *diff_algo_str = NULL;
7886 setlocale(LC_CTYPE, "");
7888 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7908 got_version_print_str();
7913 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7921 /* Build an argument vector which runs a default command. */
7922 cmd = &tog_commands[0];
7924 cmd_argv = make_argv(argc, cmd->name);
7928 /* Did the user specify a command? */
7929 for (i = 0; i < nitems(tog_commands); i++) {
7930 if (strncmp(tog_commands[i].name, argv[0],
7931 strlen(argv[0])) == 0) {
7932 cmd = &tog_commands[i];
7938 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7939 if (diff_algo_str) {
7940 if (strcasecmp(diff_algo_str, "patience") == 0)
7941 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7942 if (strcasecmp(diff_algo_str, "myers") == 0)
7943 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
7949 /* No command specified; try log with a path */
7950 error = tog_log_with_path(argc, argv);
7955 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7962 for (i = 0; i < argc; i++)
7967 if (error && error->code != GOT_ERR_CANCELLED)
7968 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);