2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
127 struct commit_queue_head head;
131 STAILQ_ENTRY(tog_color) entry;
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
155 } else if (isbackup1 && !isbackup2) {
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 STAILQ_INSERT_HEAD(colors, tc, entry);
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
296 if (strcasecmp(val, "red") == 0)
298 if (strcasecmp(val, "green") == 0)
300 if (strcasecmp(val, "yellow") == 0)
302 if (strcasecmp(val, "blue") == 0)
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
308 if (strcasecmp(val, "white") == 0)
310 if (strcasecmp(val, "default") == 0)
313 return default_color_value(envvar);
317 struct tog_diff_view_state {
318 struct got_object_id *id1, *id2;
319 const char *label1, *label2;
322 int first_displayed_line;
323 int last_displayed_line;
326 int ignore_whitespace;
328 struct got_repository *repo;
329 struct tog_colors colors;
335 /* passed from log view; may be NULL */
336 struct tog_view *log_view;
339 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
341 struct tog_log_thread_args {
342 pthread_cond_t need_commits;
343 pthread_cond_t commit_loaded;
346 struct got_commit_graph *graph;
347 struct commit_queue *commits;
348 const char *in_repo_path;
349 struct got_object_id *start_id;
350 struct got_repository *repo;
354 struct commit_queue_entry **first_displayed_entry;
355 struct commit_queue_entry **selected_entry;
357 int *search_next_done;
361 struct tog_log_view_state {
362 struct commit_queue commits;
363 struct commit_queue_entry *first_displayed_entry;
364 struct commit_queue_entry *last_displayed_entry;
365 struct commit_queue_entry *selected_entry;
370 struct got_repository *repo;
371 struct got_object_id *start_id;
374 struct tog_log_thread_args thread_args;
375 struct commit_queue_entry *matched_entry;
376 struct commit_queue_entry *search_entry;
377 struct tog_colors colors;
380 #define TOG_COLOR_DIFF_MINUS 1
381 #define TOG_COLOR_DIFF_PLUS 2
382 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
383 #define TOG_COLOR_DIFF_META 4
384 #define TOG_COLOR_TREE_SUBMODULE 5
385 #define TOG_COLOR_TREE_SYMLINK 6
386 #define TOG_COLOR_TREE_DIRECTORY 7
387 #define TOG_COLOR_TREE_EXECUTABLE 8
388 #define TOG_COLOR_COMMIT 9
389 #define TOG_COLOR_AUTHOR 10
390 #define TOG_COLOR_DATE 11
391 #define TOG_COLOR_REFS_HEADS 12
392 #define TOG_COLOR_REFS_TAGS 13
393 #define TOG_COLOR_REFS_REMOTES 14
394 #define TOG_COLOR_REFS_BACKUP 15
396 struct tog_blame_cb_args {
397 struct tog_blame_line *lines; /* one per line */
400 struct tog_view *view;
401 struct got_object_id *commit_id;
405 struct tog_blame_thread_args {
407 struct got_repository *repo;
408 struct tog_blame_cb_args *cb_args;
410 got_cancel_cb cancel_cb;
417 struct tog_blame_line *lines;
421 struct tog_blame_thread_args thread_args;
422 struct tog_blame_cb_args cb_args;
427 struct tog_blame_view_state {
428 int first_displayed_line;
429 int last_displayed_line;
434 struct got_object_id_queue blamed_commits;
435 struct got_object_qid *blamed_commit;
437 struct got_repository *repo;
438 struct got_object_id *commit_id;
439 struct tog_blame blame;
441 struct tog_colors colors;
444 struct tog_parent_tree {
445 TAILQ_ENTRY(tog_parent_tree) entry;
446 struct got_tree_object *tree;
447 struct got_tree_entry *first_displayed_entry;
448 struct got_tree_entry *selected_entry;
452 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
454 struct tog_tree_view_state {
456 struct got_object_id *commit_id;/* commit which this tree belongs to */
457 struct got_tree_object *root; /* the commit's root tree entry */
458 struct got_tree_object *tree; /* currently displayed (sub-)tree */
459 struct got_tree_entry *first_displayed_entry;
460 struct got_tree_entry *last_displayed_entry;
461 struct got_tree_entry *selected_entry;
462 int ndisplayed, selected, show_ids;
463 struct tog_parent_trees parents; /* parent trees of current sub-tree */
465 struct got_repository *repo;
466 struct got_tree_entry *matched_entry;
467 struct tog_colors colors;
470 struct tog_reflist_entry {
471 TAILQ_ENTRY(tog_reflist_entry) entry;
472 struct got_reference *ref;
476 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
478 struct tog_ref_view_state {
479 struct tog_reflist_head refs;
480 struct tog_reflist_entry *first_displayed_entry;
481 struct tog_reflist_entry *last_displayed_entry;
482 struct tog_reflist_entry *selected_entry;
483 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
484 struct got_repository *repo;
485 struct tog_reflist_entry *matched_entry;
486 struct tog_colors colors;
490 * We implement two types of views: parent views and child views.
492 * The 'Tab' key switches focus between a parent view and its child view.
493 * Child views are shown side-by-side to their parent view, provided
494 * there is enough screen estate.
496 * When a new view is opened from within a parent view, this new view
497 * becomes a child view of the parent view, replacing any existing child.
499 * When a new view is opened from within a child view, this new view
500 * becomes a parent view which will obscure the views below until the
501 * user quits the new parent view by typing 'q'.
503 * This list of views contains parent views only.
504 * Child views are only pointed to by their parent view.
506 TAILQ_HEAD(tog_view_list_head, tog_view);
509 TAILQ_ENTRY(tog_view) entry;
512 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
513 int maxx, x; /* max column and current start column */
514 int lines, cols; /* copies of LINES and COLS */
515 int nscrolled, offset; /* lines scrolled and hsplit line offset */
516 int ch, count; /* current keymap and count prefix */
517 int focussed; /* Only set on one parent or child view at a time. */
519 struct tog_view *parent;
520 struct tog_view *child;
523 * This flag is initially set on parent views when a new child view
524 * is created. It gets toggled when the 'Tab' key switches focus
525 * between parent and child.
526 * The flag indicates whether focus should be passed on to our child
527 * view if this parent view gets picked for focus after another parent
528 * view was closed. This prevents child views from losing focus in such
533 enum tog_view_mode mode;
534 /* type-specific state */
535 enum tog_view_type type;
537 struct tog_diff_view_state diff;
538 struct tog_log_view_state log;
539 struct tog_blame_view_state blame;
540 struct tog_tree_view_state tree;
541 struct tog_ref_view_state ref;
544 const struct got_error *(*show)(struct tog_view *);
545 const struct got_error *(*input)(struct tog_view **,
546 struct tog_view *, int);
547 const struct got_error *(*reset)(struct tog_view *);
548 const struct got_error *(*close)(struct tog_view *);
550 const struct got_error *(*search_start)(struct tog_view *);
551 const struct got_error *(*search_next)(struct tog_view *);
554 #define TOG_SEARCH_FORWARD 1
555 #define TOG_SEARCH_BACKWARD 2
556 int search_next_done;
557 #define TOG_SEARCH_HAVE_MORE 1
558 #define TOG_SEARCH_NO_MORE 2
559 #define TOG_SEARCH_HAVE_NONE 3
564 static const struct got_error *open_diff_view(struct tog_view *,
565 struct got_object_id *, struct got_object_id *,
566 const char *, const char *, int, int, int, struct tog_view *,
567 struct got_repository *);
568 static const struct got_error *show_diff_view(struct tog_view *);
569 static const struct got_error *input_diff_view(struct tog_view **,
570 struct tog_view *, int);
571 static const struct got_error *reset_diff_view(struct tog_view *);
572 static const struct got_error* close_diff_view(struct tog_view *);
573 static const struct got_error *search_start_diff_view(struct tog_view *);
574 static const struct got_error *search_next_diff_view(struct tog_view *);
576 static const struct got_error *open_log_view(struct tog_view *,
577 struct got_object_id *, struct got_repository *,
578 const char *, const char *, int);
579 static const struct got_error * show_log_view(struct tog_view *);
580 static const struct got_error *input_log_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *close_log_view(struct tog_view *);
583 static const struct got_error *search_start_log_view(struct tog_view *);
584 static const struct got_error *search_next_log_view(struct tog_view *);
586 static const struct got_error *open_blame_view(struct tog_view *, char *,
587 struct got_object_id *, struct got_repository *);
588 static const struct got_error *show_blame_view(struct tog_view *);
589 static const struct got_error *input_blame_view(struct tog_view **,
590 struct tog_view *, int);
591 static const struct got_error *reset_blame_view(struct tog_view *);
592 static const struct got_error *close_blame_view(struct tog_view *);
593 static const struct got_error *search_start_blame_view(struct tog_view *);
594 static const struct got_error *search_next_blame_view(struct tog_view *);
596 static const struct got_error *open_tree_view(struct tog_view *,
597 struct got_object_id *, const char *, struct got_repository *);
598 static const struct got_error *show_tree_view(struct tog_view *);
599 static const struct got_error *input_tree_view(struct tog_view **,
600 struct tog_view *, int);
601 static const struct got_error *close_tree_view(struct tog_view *);
602 static const struct got_error *search_start_tree_view(struct tog_view *);
603 static const struct got_error *search_next_tree_view(struct tog_view *);
605 static const struct got_error *open_ref_view(struct tog_view *,
606 struct got_repository *);
607 static const struct got_error *show_ref_view(struct tog_view *);
608 static const struct got_error *input_ref_view(struct tog_view **,
609 struct tog_view *, int);
610 static const struct got_error *close_ref_view(struct tog_view *);
611 static const struct got_error *search_start_ref_view(struct tog_view *);
612 static const struct got_error *search_next_ref_view(struct tog_view *);
614 static volatile sig_atomic_t tog_sigwinch_received;
615 static volatile sig_atomic_t tog_sigpipe_received;
616 static volatile sig_atomic_t tog_sigcont_received;
617 static volatile sig_atomic_t tog_sigint_received;
618 static volatile sig_atomic_t tog_sigterm_received;
621 tog_sigwinch(int signo)
623 tog_sigwinch_received = 1;
627 tog_sigpipe(int signo)
629 tog_sigpipe_received = 1;
633 tog_sigcont(int signo)
635 tog_sigcont_received = 1;
639 tog_sigint(int signo)
641 tog_sigint_received = 1;
645 tog_sigterm(int signo)
647 tog_sigterm_received = 1;
651 tog_fatal_signal_received(void)
653 return (tog_sigpipe_received ||
654 tog_sigint_received || tog_sigint_received);
657 static const struct got_error *
658 view_close(struct tog_view *view)
660 const struct got_error *err = NULL;
663 view_close(view->child);
667 err = view->close(view);
669 del_panel(view->panel);
671 delwin(view->window);
676 static struct tog_view *
677 view_open(int nlines, int ncols, int begin_y, int begin_x,
678 enum tog_view_type type)
680 struct tog_view *view = calloc(1, sizeof(*view));
688 view->nlines = nlines ? nlines : LINES - begin_y;
689 view->ncols = ncols ? ncols : COLS - begin_x;
690 view->begin_y = begin_y;
691 view->begin_x = begin_x;
692 view->window = newwin(nlines, ncols, begin_y, begin_x);
693 if (view->window == NULL) {
697 view->panel = new_panel(view->window);
698 if (view->panel == NULL ||
699 set_panel_userptr(view->panel, view) != OK) {
704 keypad(view->window, TRUE);
709 view_split_begin_x(int begin_x)
711 if (begin_x > 0 || COLS < 120)
713 return (COLS - MAX(COLS / 2, 80));
716 /* XXX Stub till we decide what to do. */
718 view_split_begin_y(int lines)
720 return lines * HSPLIT_SCALE;
723 static const struct got_error *view_resize(struct tog_view *);
725 static const struct got_error *
726 view_splitscreen(struct tog_view *view)
728 const struct got_error *err = NULL;
730 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
731 view->begin_y = view_split_begin_y(view->nlines);
734 view->begin_x = view_split_begin_x(0);
737 view->nlines = LINES - view->begin_y;
738 view->ncols = COLS - view->begin_x;
741 err = view_resize(view);
745 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
746 view->parent->nlines = view->begin_y;
748 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
749 return got_error_from_errno("mvwin");
754 static const struct got_error *
755 view_fullscreen(struct tog_view *view)
757 const struct got_error *err = NULL;
761 view->nlines = LINES;
765 err = view_resize(view);
769 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
770 return got_error_from_errno("mvwin");
776 view_is_parent_view(struct tog_view *view)
778 return view->parent == NULL;
782 view_is_splitscreen(struct tog_view *view)
784 return view->begin_x > 0 || view->begin_y > 0;
788 view_is_fullscreen(struct tog_view *view)
790 return view->nlines == LINES && view->ncols == COLS;
794 view_border(struct tog_view *view)
797 const struct tog_view *view_above;
800 return view_border(view->parent);
802 panel = panel_above(view->panel);
806 view_above = panel_userptr(panel);
807 if (view->mode == TOG_VIEW_SPLIT_HRZN)
808 mvwhline(view->window, view_above->begin_y - 1,
809 view->begin_x, got_locale_is_utf8() ?
810 ACS_HLINE : '-', view->ncols);
812 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
813 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
816 static const struct got_error *request_log_commits(struct tog_view *);
817 static const struct got_error *offset_selection_down(struct tog_view *);
818 static void offset_selection_up(struct tog_view *);
820 static const struct got_error *
821 view_resize(struct tog_view *view)
823 const struct got_error *err = NULL;
824 int dif, nlines, ncols;
826 dif = LINES - view->lines; /* line difference */
828 if (view->lines > LINES)
829 nlines = view->nlines - (view->lines - LINES);
831 nlines = view->nlines + (LINES - view->lines);
832 if (view->cols > COLS)
833 ncols = view->ncols - (view->cols - COLS);
835 ncols = view->ncols + (COLS - view->cols);
838 int hs = view->child->begin_y;
840 if (!view_is_fullscreen(view))
841 view->child->begin_x = view_split_begin_x(view->begin_x);
842 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
843 view->child->begin_x == 0) {
846 view_fullscreen(view->child);
847 if (view->child->focussed)
848 show_panel(view->child->panel);
850 show_panel(view->panel);
852 ncols = view->child->begin_x;
854 view_splitscreen(view->child);
855 show_panel(view->child->panel);
858 * Request commits if terminal height was increased in a log
859 * view so we have enough commits loaded to populate the view.
861 if (view->type == TOG_VIEW_LOG && dif > 0) {
862 struct tog_log_view_state *ts = &view->state.log;
864 if (ts->commits.ncommits < ts->selected_entry->idx +
865 view->lines - ts->selected) {
866 view->nscrolled = ts->selected_entry->idx +
867 view->lines - ts->selected -
868 ts->commits.ncommits + dif;
869 err = request_log_commits(view);
876 * XXX This is ugly and needs to be moved into the above
877 * logic but "works" for now and my attempts at moving it
878 * break either 'tab' or 'F' key maps in horizontal splits.
881 err = view_splitscreen(view->child);
884 if (dif < 0) { /* top split decreased */
885 err = offset_selection_down(view);
892 show_panel(view->child->panel);
893 nlines = view->nlines;
895 } else if (view->parent == NULL)
898 if (wresize(view->window, nlines, ncols) == ERR)
899 return got_error_from_errno("wresize");
900 if (replace_panel(view->panel, view->window) == ERR)
901 return got_error_from_errno("replace_panel");
902 wclear(view->window);
904 view->nlines = nlines;
912 static const struct got_error *
913 view_close_child(struct tog_view *view)
915 const struct got_error *err = NULL;
917 if (view->child == NULL)
920 err = view_close(view->child);
925 static const struct got_error *
926 view_set_child(struct tog_view *view, struct tog_view *child)
929 child->parent = view;
931 return view_resize(view);
940 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
941 cols = 80; /* Default */
947 resize_term(lines, cols);
950 static const struct got_error *
951 view_search_start(struct tog_view *view)
953 const struct got_error *err = NULL;
954 struct tog_view *v = view;
958 if (view->search_started) {
959 regfree(&view->regex);
961 memset(&view->regmatch, 0, sizeof(view->regmatch));
963 view->search_started = 0;
965 if (view->nlines < 1)
968 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
969 view_is_splitscreen(view->child))
972 mvwaddstr(v->window, v->nlines - 1, 0, "/");
973 wclrtoeol(v->window);
977 ret = wgetnstr(v->window, pattern, sizeof(pattern));
984 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
985 err = view->search_start(view);
987 regfree(&view->regex);
990 view->search_started = 1;
991 view->searching = TOG_SEARCH_FORWARD;
992 view->search_next_done = 0;
993 view->search_next(view);
1000 * Compute view->count from numeric user input. User has five-tenths of a
1001 * second to follow each numeric keypress with another number to form count.
1002 * Return first non-numeric input or ERR and assign total to view->count.
1003 * XXX Should we add support for user-defined timeout?
1006 get_compound_key(struct tog_view *view, int c)
1008 struct tog_view *v = view;
1011 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1012 view_is_splitscreen(view->child))
1014 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1018 halfdelay(5); /* block for half a second */
1019 wattron(v->window, A_BOLD);
1020 wmove(v->window, v->nlines - 1, 0);
1021 wclrtoeol(v->window);
1022 waddch(v->window, ':');
1025 x = getcurx(v->window);
1026 if (x != ERR && x < view->ncols) {
1027 waddch(v->window, c);
1028 wrefresh(v->window);
1032 * Don't overflow. Max valid request should be the greatest
1033 * between the longest and total lines; cap at 10 million.
1038 n = n * 10 + (c - '0');
1039 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1041 /* Massage excessive or inapplicable values at the input handler. */
1044 wattroff(v->window, A_BOLD);
1045 cbreak(); /* return to blocking */
1049 static const struct got_error *
1050 view_input(struct tog_view **new, int *done, struct tog_view *view,
1051 struct tog_view_list_head *views)
1053 const struct got_error *err = NULL;
1059 /* Clear "no matches" indicator. */
1060 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1061 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1062 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1066 if (view->searching && !view->search_next_done) {
1067 errcode = pthread_mutex_unlock(&tog_mutex);
1069 return got_error_set_errno(errcode,
1070 "pthread_mutex_unlock");
1072 errcode = pthread_mutex_lock(&tog_mutex);
1074 return got_error_set_errno(errcode,
1075 "pthread_mutex_lock");
1076 view->search_next(view);
1080 nodelay(stdscr, FALSE);
1081 /* Allow threads to make progress while we are waiting for input. */
1082 errcode = pthread_mutex_unlock(&tog_mutex);
1084 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1085 /* If we have an unfinished count, don't get a new key map. */
1087 if ((view->count && --view->count == 0) || !view->count) {
1088 ch = wgetch(view->window);
1089 if (ch >= '1' && ch <= '9')
1090 view->ch = ch = get_compound_key(view, ch);
1092 errcode = pthread_mutex_lock(&tog_mutex);
1094 return got_error_set_errno(errcode, "pthread_mutex_lock");
1095 nodelay(stdscr, TRUE);
1097 if (tog_sigwinch_received || tog_sigcont_received) {
1099 tog_sigwinch_received = 0;
1100 tog_sigcont_received = 0;
1101 TAILQ_FOREACH(v, views, entry) {
1102 err = view_resize(v);
1105 err = v->input(new, v, KEY_RESIZE);
1109 err = view_resize(v->child);
1112 err = v->child->input(new, v->child,
1125 view->child->focussed = 1;
1126 view->focus_child = 1;
1127 } else if (view->parent) {
1129 view->parent->focussed = 1;
1130 view->parent->focus_child = 0;
1131 if (!view_is_splitscreen(view)) {
1132 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1133 view->parent->type == TOG_VIEW_LOG) {
1134 err = request_log_commits(view->parent);
1138 offset_selection_up(view->parent);
1139 err = view_fullscreen(view->parent);
1146 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1147 if (view->parent->type == TOG_VIEW_LOG) {
1148 /* might need more commits to fill fullscreen */
1149 err = request_log_commits(view->parent);
1153 offset_selection_up(view->parent);
1154 view->parent->mode = TOG_VIEW_SPLIT_NONE;
1156 err = view->input(new, view, ch);
1164 if (view_is_parent_view(view)) {
1165 if (view->child == NULL)
1167 if (view_is_splitscreen(view->child)) {
1169 view->child->focussed = 1;
1170 err = view_fullscreen(view->child);
1172 err = view_splitscreen(view->child);
1175 err = view->child->input(new, view->child,
1178 if (view_is_splitscreen(view)) {
1179 view->parent->focussed = 0;
1181 err = view_fullscreen(view);
1183 err = view_splitscreen(view);
1184 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1185 err = view_resize(view->parent);
1189 err = view->input(new, view, KEY_RESIZE);
1193 if (view->type == TOG_VIEW_LOG) {
1194 err = request_log_commits(view);
1199 err = offset_selection_down(view->parent);
1201 err = offset_selection_down(view);
1207 if (view->search_start)
1208 view_search_start(view);
1210 err = view->input(new, view, ch);
1214 if (view->search_started && view->search_next) {
1215 view->searching = (ch == 'n' ?
1216 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1217 view->search_next_done = 0;
1218 view->search_next(view);
1220 err = view->input(new, view, ch);
1223 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1224 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1226 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1227 TAILQ_FOREACH(v, views, entry) {
1233 if (v->child && v->child->reset) {
1234 err = v->child->reset(v->child);
1241 err = view->input(new, view, ch);
1249 view_needs_focus_indication(struct tog_view *view)
1251 if (view_is_parent_view(view)) {
1252 if (view->child == NULL || view->child->focussed)
1254 if (!view_is_splitscreen(view->child))
1256 } else if (!view_is_splitscreen(view))
1259 return view->focussed;
1262 static const struct got_error *
1263 view_loop(struct tog_view *view)
1265 const struct got_error *err = NULL;
1266 struct tog_view_list_head views;
1267 struct tog_view *new_view;
1268 int fast_refresh = 10;
1269 int done = 0, errcode;
1271 errcode = pthread_mutex_lock(&tog_mutex);
1273 return got_error_set_errno(errcode, "pthread_mutex_lock");
1276 TAILQ_INSERT_HEAD(&views, view, entry);
1279 err = view->show(view);
1284 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1285 /* Refresh fast during initialization, then become slower. */
1286 if (fast_refresh && fast_refresh-- == 0)
1287 halfdelay(10); /* switch to once per second */
1289 err = view_input(&new_view, &done, view, &views);
1293 struct tog_view *v, *prev = NULL;
1295 if (view_is_parent_view(view))
1296 prev = TAILQ_PREV(view, tog_view_list_head,
1298 else if (view->parent)
1299 prev = view->parent;
1302 view->parent->child = NULL;
1303 view->parent->focus_child = 0;
1304 /* Restore fullscreen line height. */
1305 view->parent->nlines = view->parent->lines;
1306 err = view_resize(view->parent);
1310 TAILQ_REMOVE(&views, view, entry);
1312 err = view_close(view);
1317 TAILQ_FOREACH(v, &views, entry) {
1321 if (view == NULL && new_view == NULL) {
1322 /* No view has focus. Try to pick one. */
1325 else if (!TAILQ_EMPTY(&views)) {
1326 view = TAILQ_LAST(&views,
1327 tog_view_list_head);
1330 if (view->focus_child) {
1331 view->child->focussed = 1;
1339 struct tog_view *v, *t;
1340 /* Only allow one parent view per type. */
1341 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1342 if (v->type != new_view->type)
1344 TAILQ_REMOVE(&views, v, entry);
1345 err = view_close(v);
1350 TAILQ_INSERT_TAIL(&views, new_view, entry);
1354 if (view_is_parent_view(view)) {
1355 if (view->child && view->child->focussed)
1358 if (view->parent && view->parent->focussed)
1359 view = view->parent;
1361 show_panel(view->panel);
1362 if (view->child && view_is_splitscreen(view->child))
1363 show_panel(view->child->panel);
1364 if (view->parent && view_is_splitscreen(view)) {
1365 err = view->parent->show(view->parent);
1369 err = view->show(view);
1373 err = view->child->show(view->child);
1382 while (!TAILQ_EMPTY(&views)) {
1383 view = TAILQ_FIRST(&views);
1384 TAILQ_REMOVE(&views, view, entry);
1388 errcode = pthread_mutex_unlock(&tog_mutex);
1389 if (errcode && err == NULL)
1390 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1400 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1405 /* Create newly allocated wide-character string equivalent to a byte string. */
1406 static const struct got_error *
1407 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1410 const struct got_error *err = NULL;
1413 *wlen = mbstowcs(NULL, s, 0);
1414 if (*wlen == (size_t)-1) {
1416 if (errno != EILSEQ)
1417 return got_error_from_errno("mbstowcs");
1419 /* byte string invalid in current encoding; try to "fix" it */
1420 err = got_mbsavis(&vis, &vislen, s);
1423 *wlen = mbstowcs(NULL, vis, 0);
1424 if (*wlen == (size_t)-1) {
1425 err = got_error_from_errno("mbstowcs"); /* give up */
1430 *ws = calloc(*wlen + 1, sizeof(**ws));
1432 err = got_error_from_errno("calloc");
1436 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1437 err = got_error_from_errno("mbstowcs");
1448 static const struct got_error *
1449 expand_tab(char **ptr, const char *src)
1452 size_t len, n, idx = 0, sz = 0;
1455 n = len = strlen(src);
1456 dst = malloc(n + 1);
1458 return got_error_from_errno("malloc");
1460 while (idx < len && src[idx]) {
1461 const char c = src[idx];
1464 size_t nb = TABSIZE - sz % TABSIZE;
1467 p = realloc(dst, n + nb);
1470 return got_error_from_errno("realloc");
1475 memset(dst + sz, ' ', nb);
1478 dst[sz++] = src[idx];
1488 * Advance at most n columns from wline starting at offset off.
1489 * Return the index to the first character after the span operation.
1490 * Return the combined column width of all spanned wide character in
1494 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1496 int width, i, cols = 0;
1503 for (i = off; wline[i] != L'\0'; ++i) {
1504 if (wline[i] == L'\t')
1505 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1507 width = wcwidth(wline[i]);
1514 if (cols + width > n)
1524 * Format a line for display, ensuring that it won't overflow a width limit.
1525 * With scrolling, the width returned refers to the scrolled version of the
1526 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1528 static const struct got_error *
1529 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1530 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1532 const struct got_error *err = NULL;
1534 wchar_t *wline = NULL;
1543 err = expand_tab(&exstr, line);
1548 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1553 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1555 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1556 wline[wlen - 1] = L'\0';
1559 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1560 wline[wlen - 1] = L'\0';
1564 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1570 *scrollxp = scrollx;
1578 static const struct got_error*
1579 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1580 struct got_object_id *id, struct got_repository *repo)
1582 static const struct got_error *err = NULL;
1583 struct got_reflist_entry *re;
1589 TAILQ_FOREACH(re, refs, entry) {
1590 struct got_tag_object *tag = NULL;
1591 struct got_object_id *ref_id;
1594 name = got_ref_get_name(re->ref);
1595 if (strcmp(name, GOT_REF_HEAD) == 0)
1597 if (strncmp(name, "refs/", 5) == 0)
1599 if (strncmp(name, "got/", 4) == 0 &&
1600 strncmp(name, "got/backup/", 11) != 0)
1602 if (strncmp(name, "heads/", 6) == 0)
1604 if (strncmp(name, "remotes/", 8) == 0) {
1606 s = strstr(name, "/" GOT_REF_HEAD);
1607 if (s != NULL && s[strlen(s)] == '\0')
1610 err = got_ref_resolve(&ref_id, repo, re->ref);
1613 if (strncmp(name, "tags/", 5) == 0) {
1614 err = got_object_open_as_tag(&tag, repo, ref_id);
1616 if (err->code != GOT_ERR_OBJ_TYPE) {
1620 /* Ref points at something other than a tag. */
1625 cmp = got_object_id_cmp(tag ?
1626 got_object_tag_get_object_id(tag) : ref_id, id);
1629 got_object_tag_close(tag);
1633 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1634 s ? ", " : "", name) == -1) {
1635 err = got_error_from_errno("asprintf");
1646 static const struct got_error *
1647 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1652 smallerthan = strchr(author, '<');
1653 if (smallerthan && smallerthan[1] != '\0')
1654 author = smallerthan + 1;
1655 author[strcspn(author, "@>")] = '\0';
1656 return format_line(wauthor, author_width, NULL, author, 0, limit,
1660 static const struct got_error *
1661 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1662 struct got_object_id *id, const size_t date_display_cols,
1663 int author_display_cols)
1665 struct tog_log_view_state *s = &view->state.log;
1666 const struct got_error *err = NULL;
1667 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1668 char *logmsg0 = NULL, *logmsg = NULL;
1669 char *author = NULL;
1670 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1671 int author_width, logmsg_width;
1672 char *newline, *line = NULL;
1673 int col, limit, scrollx;
1674 const int avail = view->ncols;
1676 time_t committer_time;
1677 struct tog_color *tc;
1679 committer_time = got_object_commit_get_committer_time(commit);
1680 if (gmtime_r(&committer_time, &tm) == NULL)
1681 return got_error_from_errno("gmtime_r");
1682 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1683 return got_error(GOT_ERR_NO_SPACE);
1685 if (avail <= date_display_cols)
1686 limit = MIN(sizeof(datebuf) - 1, avail);
1688 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1689 tc = get_color(&s->colors, TOG_COLOR_DATE);
1691 wattr_on(view->window,
1692 COLOR_PAIR(tc->colorpair), NULL);
1693 waddnstr(view->window, datebuf, limit);
1695 wattr_off(view->window,
1696 COLOR_PAIR(tc->colorpair), NULL);
1703 err = got_object_id_str(&id_str, id);
1706 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1708 wattr_on(view->window,
1709 COLOR_PAIR(tc->colorpair), NULL);
1710 wprintw(view->window, "%.8s ", id_str);
1712 wattr_off(view->window,
1713 COLOR_PAIR(tc->colorpair), NULL);
1720 author = strdup(got_object_commit_get_author(commit));
1721 if (author == NULL) {
1722 err = got_error_from_errno("strdup");
1725 err = format_author(&wauthor, &author_width, author, avail - col, col);
1728 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1730 wattr_on(view->window,
1731 COLOR_PAIR(tc->colorpair), NULL);
1732 waddwstr(view->window, wauthor);
1734 wattr_off(view->window,
1735 COLOR_PAIR(tc->colorpair), NULL);
1736 col += author_width;
1737 while (col < avail && author_width < author_display_cols + 2) {
1738 waddch(view->window, ' ');
1745 err = got_object_commit_get_logmsg(&logmsg0, commit);
1749 while (*logmsg == '\n')
1751 newline = strchr(logmsg, '\n');
1754 limit = avail - col;
1755 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1756 limit--; /* for the border */
1757 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1761 waddwstr(view->window, &wlogmsg[scrollx]);
1762 col += MAX(logmsg_width, 0);
1763 while (col < avail) {
1764 waddch(view->window, ' ');
1776 static struct commit_queue_entry *
1777 alloc_commit_queue_entry(struct got_commit_object *commit,
1778 struct got_object_id *id)
1780 struct commit_queue_entry *entry;
1782 entry = calloc(1, sizeof(*entry));
1787 entry->commit = commit;
1792 pop_commit(struct commit_queue *commits)
1794 struct commit_queue_entry *entry;
1796 entry = TAILQ_FIRST(&commits->head);
1797 TAILQ_REMOVE(&commits->head, entry, entry);
1798 got_object_commit_close(entry->commit);
1799 commits->ncommits--;
1800 /* Don't free entry->id! It is owned by the commit graph. */
1805 free_commits(struct commit_queue *commits)
1807 while (!TAILQ_EMPTY(&commits->head))
1808 pop_commit(commits);
1811 static const struct got_error *
1812 match_commit(int *have_match, struct got_object_id *id,
1813 struct got_commit_object *commit, regex_t *regex)
1815 const struct got_error *err = NULL;
1816 regmatch_t regmatch;
1817 char *id_str = NULL, *logmsg = NULL;
1821 err = got_object_id_str(&id_str, id);
1825 err = got_object_commit_get_logmsg(&logmsg, commit);
1829 if (regexec(regex, got_object_commit_get_author(commit), 1,
1830 ®match, 0) == 0 ||
1831 regexec(regex, got_object_commit_get_committer(commit), 1,
1832 ®match, 0) == 0 ||
1833 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1834 regexec(regex, logmsg, 1, ®match, 0) == 0)
1842 static const struct got_error *
1843 queue_commits(struct tog_log_thread_args *a)
1845 const struct got_error *err = NULL;
1848 * We keep all commits open throughout the lifetime of the log
1849 * view in order to avoid having to re-fetch commits from disk
1850 * while updating the display.
1853 struct got_object_id *id;
1854 struct got_commit_object *commit;
1855 struct commit_queue_entry *entry;
1858 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1860 if (err || id == NULL)
1863 err = got_object_open_as_commit(&commit, a->repo, id);
1866 entry = alloc_commit_queue_entry(commit, id);
1867 if (entry == NULL) {
1868 err = got_error_from_errno("alloc_commit_queue_entry");
1872 errcode = pthread_mutex_lock(&tog_mutex);
1874 err = got_error_set_errno(errcode,
1875 "pthread_mutex_lock");
1879 entry->idx = a->commits->ncommits;
1880 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1881 a->commits->ncommits++;
1883 if (*a->searching == TOG_SEARCH_FORWARD &&
1884 !*a->search_next_done) {
1886 err = match_commit(&have_match, id, commit, a->regex);
1890 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1893 errcode = pthread_mutex_unlock(&tog_mutex);
1894 if (errcode && err == NULL)
1895 err = got_error_set_errno(errcode,
1896 "pthread_mutex_unlock");
1899 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1905 select_commit(struct tog_log_view_state *s)
1907 struct commit_queue_entry *entry;
1910 entry = s->first_displayed_entry;
1912 if (ncommits == s->selected) {
1913 s->selected_entry = entry;
1916 entry = TAILQ_NEXT(entry, entry);
1921 static const struct got_error *
1922 draw_commits(struct tog_view *view)
1924 const struct got_error *err = NULL;
1925 struct tog_log_view_state *s = &view->state.log;
1926 struct commit_queue_entry *entry = s->selected_entry;
1927 const int limit = view->nlines;
1929 int ncommits, author_cols = 4;
1930 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1931 char *refs_str = NULL;
1933 struct tog_color *tc;
1934 static const size_t date_display_cols = 12;
1936 if (s->selected_entry &&
1937 !(view->searching && view->search_next_done == 0)) {
1938 struct got_reflist_head *refs;
1939 err = got_object_id_str(&id_str, s->selected_entry->id);
1942 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1943 s->selected_entry->id);
1945 err = build_refs_str(&refs_str, refs,
1946 s->selected_entry->id, s->repo);
1952 if (s->thread_args.commits_needed == 0)
1953 halfdelay(10); /* disable fast refresh */
1955 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1956 if (asprintf(&ncommits_str, " [%d/%d] %s",
1957 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1958 (view->searching && !view->search_next_done) ?
1959 "searching..." : "loading...") == -1) {
1960 err = got_error_from_errno("asprintf");
1964 const char *search_str = NULL;
1966 if (view->searching) {
1967 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1968 search_str = "no more matches";
1969 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1970 search_str = "no matches found";
1971 else if (!view->search_next_done)
1972 search_str = "searching...";
1975 if (asprintf(&ncommits_str, " [%d/%d] %s",
1976 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1977 search_str ? search_str :
1978 (refs_str ? refs_str : "")) == -1) {
1979 err = got_error_from_errno("asprintf");
1984 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1985 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1986 "........................................",
1987 s->in_repo_path, ncommits_str) == -1) {
1988 err = got_error_from_errno("asprintf");
1992 } else if (asprintf(&header, "commit %s%s",
1993 id_str ? id_str : "........................................",
1994 ncommits_str) == -1) {
1995 err = got_error_from_errno("asprintf");
1999 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2003 werase(view->window);
2005 if (view_needs_focus_indication(view))
2006 wstandout(view->window);
2007 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2009 wattr_on(view->window,
2010 COLOR_PAIR(tc->colorpair), NULL);
2011 waddwstr(view->window, wline);
2013 wattr_off(view->window,
2014 COLOR_PAIR(tc->colorpair), NULL);
2015 while (width < view->ncols) {
2016 waddch(view->window, ' ');
2019 if (view_needs_focus_indication(view))
2020 wstandend(view->window);
2025 /* Grow author column size if necessary, and set view->maxx. */
2026 entry = s->first_displayed_entry;
2030 char *author, *eol, *msg, *msg0;
2031 wchar_t *wauthor, *wmsg;
2033 if (ncommits >= limit - 1)
2035 author = strdup(got_object_commit_get_author(entry->commit));
2036 if (author == NULL) {
2037 err = got_error_from_errno("strdup");
2040 err = format_author(&wauthor, &width, author, COLS,
2042 if (author_cols < width)
2043 author_cols = width;
2046 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2050 while (*msg == '\n')
2052 if ((eol = strchr(msg, '\n')))
2054 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2055 date_display_cols + author_cols, 0);
2058 view->maxx = MAX(view->maxx, width);
2062 entry = TAILQ_NEXT(entry, entry);
2065 entry = s->first_displayed_entry;
2066 s->last_displayed_entry = s->first_displayed_entry;
2069 if (ncommits >= limit - 1)
2071 if (ncommits == s->selected)
2072 wstandout(view->window);
2073 err = draw_commit(view, entry->commit, entry->id,
2074 date_display_cols, author_cols);
2075 if (ncommits == s->selected)
2076 wstandend(view->window);
2080 s->last_displayed_entry = entry;
2081 entry = TAILQ_NEXT(entry, entry);
2094 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2096 struct commit_queue_entry *entry;
2099 entry = TAILQ_FIRST(&s->commits.head);
2100 if (s->first_displayed_entry == entry)
2103 entry = s->first_displayed_entry;
2104 while (entry && nscrolled < maxscroll) {
2105 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2107 s->first_displayed_entry = entry;
2113 static const struct got_error *
2114 trigger_log_thread(struct tog_view *view, int wait)
2116 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2119 halfdelay(1); /* fast refresh while loading commits */
2121 while (ta->commits_needed > 0 || ta->load_all) {
2122 if (ta->log_complete)
2125 /* Wake the log thread. */
2126 errcode = pthread_cond_signal(&ta->need_commits);
2128 return got_error_set_errno(errcode,
2129 "pthread_cond_signal");
2132 * The mutex will be released while the view loop waits
2133 * in wgetch(), at which time the log thread will run.
2138 /* Display progress update in log view. */
2139 show_log_view(view);
2143 /* Wait right here while next commit is being loaded. */
2144 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2146 return got_error_set_errno(errcode,
2147 "pthread_cond_wait");
2149 /* Display progress update in log view. */
2150 show_log_view(view);
2158 static const struct got_error *
2159 request_log_commits(struct tog_view *view)
2161 struct tog_log_view_state *state = &view->state.log;
2162 const struct got_error *err = NULL;
2164 state->thread_args.commits_needed = view->nscrolled;
2165 err = trigger_log_thread(view, 1);
2166 view->nscrolled = 0;
2171 static const struct got_error *
2172 log_scroll_down(struct tog_view *view, int maxscroll)
2174 struct tog_log_view_state *s = &view->state.log;
2175 const struct got_error *err = NULL;
2176 struct commit_queue_entry *pentry;
2177 int nscrolled = 0, ncommits_needed;
2179 if (s->last_displayed_entry == NULL)
2182 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2183 if (s->commits.ncommits < ncommits_needed &&
2184 !s->thread_args.log_complete) {
2186 * Ask the log thread for required amount of commits.
2188 s->thread_args.commits_needed += maxscroll;
2189 err = trigger_log_thread(view, 1);
2195 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2196 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2199 s->last_displayed_entry = pentry ?
2200 pentry : s->last_displayed_entry;;
2202 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2205 s->first_displayed_entry = pentry;
2206 } while (++nscrolled < maxscroll);
2208 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2209 view->nscrolled += nscrolled;
2211 view->nscrolled = 0;
2216 static const struct got_error *
2217 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2218 struct got_commit_object *commit, struct got_object_id *commit_id,
2219 struct tog_view *log_view, struct got_repository *repo)
2221 const struct got_error *err;
2222 struct got_object_qid *parent_id;
2223 struct tog_view *diff_view;
2225 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2226 if (diff_view == NULL)
2227 return got_error_from_errno("view_open");
2229 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2230 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2231 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2233 *new_view = diff_view;
2237 static const struct got_error *
2238 tree_view_visit_subtree(struct tog_tree_view_state *s,
2239 struct got_tree_object *subtree)
2241 struct tog_parent_tree *parent;
2243 parent = calloc(1, sizeof(*parent));
2245 return got_error_from_errno("calloc");
2247 parent->tree = s->tree;
2248 parent->first_displayed_entry = s->first_displayed_entry;
2249 parent->selected_entry = s->selected_entry;
2250 parent->selected = s->selected;
2251 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2254 s->first_displayed_entry = NULL;
2258 static const struct got_error *
2259 tree_view_walk_path(struct tog_tree_view_state *s,
2260 struct got_commit_object *commit, const char *path)
2262 const struct got_error *err = NULL;
2263 struct got_tree_object *tree = NULL;
2265 char *slash, *subpath = NULL;
2267 /* Walk the path and open corresponding tree objects. */
2270 struct got_tree_entry *te;
2271 struct got_object_id *tree_id;
2277 /* Ensure the correct subtree entry is selected. */
2278 slash = strchr(p, '/');
2280 te_name = strdup(p);
2282 te_name = strndup(p, slash - p);
2283 if (te_name == NULL) {
2284 err = got_error_from_errno("strndup");
2287 te = got_object_tree_find_entry(s->tree, te_name);
2289 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2294 s->first_displayed_entry = s->selected_entry = te;
2296 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2297 break; /* jump to this file's entry */
2299 slash = strchr(p, '/');
2301 subpath = strndup(path, slash - path);
2303 subpath = strdup(path);
2304 if (subpath == NULL) {
2305 err = got_error_from_errno("strdup");
2309 err = got_object_id_by_path(&tree_id, s->repo, commit,
2314 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2319 err = tree_view_visit_subtree(s, tree);
2321 got_object_tree_close(tree);
2335 static const struct got_error *
2336 browse_commit_tree(struct tog_view **new_view, int begin_x,
2337 struct commit_queue_entry *entry, const char *path,
2338 const char *head_ref_name, struct got_repository *repo)
2340 const struct got_error *err = NULL;
2341 struct tog_tree_view_state *s;
2342 struct tog_view *tree_view;
2344 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2345 if (tree_view == NULL)
2346 return got_error_from_errno("view_open");
2348 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2351 s = &tree_view->state.tree;
2353 *new_view = tree_view;
2355 if (got_path_is_root_dir(path))
2358 return tree_view_walk_path(s, entry->commit, path);
2361 static const struct got_error *
2362 block_signals_used_by_main_thread(void)
2367 if (sigemptyset(&sigset) == -1)
2368 return got_error_from_errno("sigemptyset");
2370 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2371 if (sigaddset(&sigset, SIGWINCH) == -1)
2372 return got_error_from_errno("sigaddset");
2373 if (sigaddset(&sigset, SIGCONT) == -1)
2374 return got_error_from_errno("sigaddset");
2375 if (sigaddset(&sigset, SIGINT) == -1)
2376 return got_error_from_errno("sigaddset");
2377 if (sigaddset(&sigset, SIGTERM) == -1)
2378 return got_error_from_errno("sigaddset");
2380 /* ncurses handles SIGTSTP */
2381 if (sigaddset(&sigset, SIGTSTP) == -1)
2382 return got_error_from_errno("sigaddset");
2384 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2386 return got_error_set_errno(errcode, "pthread_sigmask");
2392 log_thread(void *arg)
2394 const struct got_error *err = NULL;
2396 struct tog_log_thread_args *a = arg;
2399 err = block_signals_used_by_main_thread();
2403 while (!done && !err && !tog_fatal_signal_received()) {
2404 err = queue_commits(a);
2406 if (err->code != GOT_ERR_ITER_COMPLETED)
2410 } else if (a->commits_needed > 0 && !a->load_all)
2411 a->commits_needed--;
2413 errcode = pthread_mutex_lock(&tog_mutex);
2415 err = got_error_set_errno(errcode,
2416 "pthread_mutex_lock");
2418 } else if (*a->quit)
2420 else if (*a->first_displayed_entry == NULL) {
2421 *a->first_displayed_entry =
2422 TAILQ_FIRST(&a->commits->head);
2423 *a->selected_entry = *a->first_displayed_entry;
2426 errcode = pthread_cond_signal(&a->commit_loaded);
2428 err = got_error_set_errno(errcode,
2429 "pthread_cond_signal");
2430 pthread_mutex_unlock(&tog_mutex);
2435 a->commits_needed = 0;
2437 if (a->commits_needed == 0 && !a->load_all) {
2438 errcode = pthread_cond_wait(&a->need_commits,
2441 err = got_error_set_errno(errcode,
2442 "pthread_cond_wait");
2448 errcode = pthread_mutex_unlock(&tog_mutex);
2449 if (errcode && err == NULL)
2450 err = got_error_set_errno(errcode,
2451 "pthread_mutex_unlock");
2453 a->log_complete = 1;
2457 static const struct got_error *
2458 stop_log_thread(struct tog_log_view_state *s)
2460 const struct got_error *err = NULL;
2465 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2467 return got_error_set_errno(errcode,
2468 "pthread_cond_signal");
2469 errcode = pthread_mutex_unlock(&tog_mutex);
2471 return got_error_set_errno(errcode,
2472 "pthread_mutex_unlock");
2473 errcode = pthread_join(s->thread, (void **)&err);
2475 return got_error_set_errno(errcode, "pthread_join");
2476 errcode = pthread_mutex_lock(&tog_mutex);
2478 return got_error_set_errno(errcode,
2479 "pthread_mutex_lock");
2483 if (s->thread_args.repo) {
2484 err = got_repo_close(s->thread_args.repo);
2485 s->thread_args.repo = NULL;
2488 if (s->thread_args.pack_fds) {
2489 const struct got_error *pack_err =
2490 got_repo_pack_fds_close(s->thread_args.pack_fds);
2493 s->thread_args.pack_fds = NULL;
2496 if (s->thread_args.graph) {
2497 got_commit_graph_close(s->thread_args.graph);
2498 s->thread_args.graph = NULL;
2504 static const struct got_error *
2505 close_log_view(struct tog_view *view)
2507 const struct got_error *err = NULL;
2508 struct tog_log_view_state *s = &view->state.log;
2511 err = stop_log_thread(s);
2513 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2514 if (errcode && err == NULL)
2515 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2517 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2518 if (errcode && err == NULL)
2519 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2521 free_commits(&s->commits);
2522 free(s->in_repo_path);
2523 s->in_repo_path = NULL;
2526 free(s->head_ref_name);
2527 s->head_ref_name = NULL;
2531 static const struct got_error *
2532 search_start_log_view(struct tog_view *view)
2534 struct tog_log_view_state *s = &view->state.log;
2536 s->matched_entry = NULL;
2537 s->search_entry = NULL;
2541 static const struct got_error *
2542 search_next_log_view(struct tog_view *view)
2544 const struct got_error *err = NULL;
2545 struct tog_log_view_state *s = &view->state.log;
2546 struct commit_queue_entry *entry;
2548 /* Display progress update in log view. */
2549 show_log_view(view);
2553 if (s->search_entry) {
2555 errcode = pthread_mutex_unlock(&tog_mutex);
2557 return got_error_set_errno(errcode,
2558 "pthread_mutex_unlock");
2559 ch = wgetch(view->window);
2560 errcode = pthread_mutex_lock(&tog_mutex);
2562 return got_error_set_errno(errcode,
2563 "pthread_mutex_lock");
2564 if (ch == KEY_BACKSPACE) {
2565 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2568 if (view->searching == TOG_SEARCH_FORWARD)
2569 entry = TAILQ_NEXT(s->search_entry, entry);
2571 entry = TAILQ_PREV(s->search_entry,
2572 commit_queue_head, entry);
2573 } else if (s->matched_entry) {
2574 int matched_idx = s->matched_entry->idx;
2575 int selected_idx = s->selected_entry->idx;
2578 * If the user has moved the cursor after we hit a match,
2579 * the position from where we should continue searching
2580 * might have changed.
2582 if (view->searching == TOG_SEARCH_FORWARD) {
2583 if (matched_idx > selected_idx)
2584 entry = TAILQ_NEXT(s->selected_entry, entry);
2586 entry = TAILQ_NEXT(s->matched_entry, entry);
2588 if (matched_idx < selected_idx)
2589 entry = TAILQ_PREV(s->selected_entry,
2590 commit_queue_head, entry);
2592 entry = TAILQ_PREV(s->matched_entry,
2593 commit_queue_head, entry);
2596 entry = s->selected_entry;
2602 if (entry == NULL) {
2603 if (s->thread_args.log_complete ||
2604 view->searching == TOG_SEARCH_BACKWARD) {
2605 view->search_next_done =
2606 (s->matched_entry == NULL ?
2607 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2608 s->search_entry = NULL;
2612 * Poke the log thread for more commits and return,
2613 * allowing the main loop to make progress. Search
2614 * will resume at s->search_entry once we come back.
2616 s->thread_args.commits_needed++;
2617 return trigger_log_thread(view, 0);
2620 err = match_commit(&have_match, entry->id, entry->commit,
2625 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2626 s->matched_entry = entry;
2630 s->search_entry = entry;
2631 if (view->searching == TOG_SEARCH_FORWARD)
2632 entry = TAILQ_NEXT(entry, entry);
2634 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2637 if (s->matched_entry) {
2638 int cur = s->selected_entry->idx;
2639 while (cur < s->matched_entry->idx) {
2640 err = input_log_view(NULL, view, KEY_DOWN);
2645 while (cur > s->matched_entry->idx) {
2646 err = input_log_view(NULL, view, KEY_UP);
2653 s->search_entry = NULL;
2658 static const struct got_error *
2659 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2660 struct got_repository *repo, const char *head_ref_name,
2661 const char *in_repo_path, int log_branches)
2663 const struct got_error *err = NULL;
2664 struct tog_log_view_state *s = &view->state.log;
2665 struct got_repository *thread_repo = NULL;
2666 struct got_commit_graph *thread_graph = NULL;
2669 if (in_repo_path != s->in_repo_path) {
2670 free(s->in_repo_path);
2671 s->in_repo_path = strdup(in_repo_path);
2672 if (s->in_repo_path == NULL)
2673 return got_error_from_errno("strdup");
2676 /* The commit queue only contains commits being displayed. */
2677 TAILQ_INIT(&s->commits.head);
2678 s->commits.ncommits = 0;
2681 if (head_ref_name) {
2682 s->head_ref_name = strdup(head_ref_name);
2683 if (s->head_ref_name == NULL) {
2684 err = got_error_from_errno("strdup");
2688 s->start_id = got_object_id_dup(start_id);
2689 if (s->start_id == NULL) {
2690 err = got_error_from_errno("got_object_id_dup");
2693 s->log_branches = log_branches;
2695 STAILQ_INIT(&s->colors);
2696 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2697 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2698 get_color_value("TOG_COLOR_COMMIT"));
2701 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2702 get_color_value("TOG_COLOR_AUTHOR"));
2704 free_colors(&s->colors);
2707 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2708 get_color_value("TOG_COLOR_DATE"));
2710 free_colors(&s->colors);
2715 view->show = show_log_view;
2716 view->input = input_log_view;
2717 view->close = close_log_view;
2718 view->search_start = search_start_log_view;
2719 view->search_next = search_next_log_view;
2721 if (s->thread_args.pack_fds == NULL) {
2722 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2726 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2727 s->thread_args.pack_fds);
2730 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2734 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2735 s->repo, NULL, NULL);
2739 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2741 err = got_error_set_errno(errcode, "pthread_cond_init");
2744 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2746 err = got_error_set_errno(errcode, "pthread_cond_init");
2750 s->thread_args.commits_needed = view->nlines;
2751 s->thread_args.graph = thread_graph;
2752 s->thread_args.commits = &s->commits;
2753 s->thread_args.in_repo_path = s->in_repo_path;
2754 s->thread_args.start_id = s->start_id;
2755 s->thread_args.repo = thread_repo;
2756 s->thread_args.log_complete = 0;
2757 s->thread_args.quit = &s->quit;
2758 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2759 s->thread_args.selected_entry = &s->selected_entry;
2760 s->thread_args.searching = &view->searching;
2761 s->thread_args.search_next_done = &view->search_next_done;
2762 s->thread_args.regex = &view->regex;
2765 close_log_view(view);
2769 static const struct got_error *
2770 show_log_view(struct tog_view *view)
2772 const struct got_error *err;
2773 struct tog_log_view_state *s = &view->state.log;
2775 if (s->thread == NULL) {
2776 int errcode = pthread_create(&s->thread, NULL, log_thread,
2779 return got_error_set_errno(errcode, "pthread_create");
2780 if (s->thread_args.commits_needed > 0) {
2781 err = trigger_log_thread(view, 1);
2787 return draw_commits(view);
2791 log_move_cursor_up(struct tog_view *view, int page, int home)
2793 struct tog_log_view_state *s = &view->state.log;
2795 if (s->selected_entry->idx == 0)
2797 if (s->first_displayed_entry == NULL)
2800 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2802 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2804 if (!page && !home && s->selected > 0)
2807 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2813 static const struct got_error *
2814 log_move_cursor_down(struct tog_view *view, int page)
2816 struct tog_log_view_state *s = &view->state.log;
2817 struct commit_queue_entry *first;
2818 const struct got_error *err = NULL;
2820 first = s->first_displayed_entry;
2821 if (first == NULL) {
2826 if (s->thread_args.log_complete &&
2827 s->selected_entry->idx >= s->commits.ncommits - 1)
2831 int eos = view->nlines - 2;
2833 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2834 view_is_splitscreen(view->child))
2835 --eos; /* border consumes the last line */
2836 if (s->selected < MIN(eos, s->commits.ncommits - 1))
2839 err = log_scroll_down(view, 1);
2840 } else if (s->thread_args.load_all) {
2841 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2842 s->selected += MIN(s->last_displayed_entry->idx -
2843 s->selected_entry->idx, page + 1);
2845 err = log_scroll_down(view, MIN(page,
2846 s->commits.ncommits - s->selected_entry->idx - 1));
2847 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2849 err = log_scroll_down(view, page);
2852 if (first == s->first_displayed_entry && s->selected <
2853 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2854 s->selected = MIN(s->commits.ncommits - 1, page);
2861 * We might necessarily overshoot in horizontal
2862 * splits; if so, select the last displayed commit.
2864 s->selected = MIN(s->selected,
2865 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2869 if (s->thread_args.log_complete &&
2870 s->selected_entry->idx == s->commits.ncommits - 1)
2877 * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2878 * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2879 * TOG_VIEW_SPLIT_HRZN horizontal split
2880 * Assign start column and line of the new split to *x and *y, respectively,
2881 * and assign view mode to view->mode.
2884 view_get_split(struct tog_view *view, int *y, int *x)
2891 mode = getenv("TOG_VIEW_SPLIT_MODE");
2893 if (!mode || mode[0] != 'h') {
2894 view->mode = TOG_VIEW_SPLIT_VERT;
2895 *x = view_split_begin_x(view->begin_x);
2896 } else if (mode && mode[0] == 'h') {
2897 view->mode = TOG_VIEW_SPLIT_HRZN;
2898 *y = view_split_begin_y(view->lines);
2902 /* Split view horizontally at y and offset view->state->selected line. */
2903 static const struct got_error *
2904 view_init_hsplit(struct tog_view *view, int y)
2906 const struct got_error *err = NULL;
2909 err = view_resize(view);
2913 err = offset_selection_down(view);
2918 static const struct got_error *
2919 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2921 const struct got_error *err = NULL;
2922 struct tog_log_view_state *s = &view->state.log;
2923 struct tog_view *diff_view = NULL, *tree_view = NULL;
2924 struct tog_view *ref_view = NULL;
2925 struct commit_queue_entry *entry;
2926 int begin_x = 0, begin_y = 0, eos, n, nscroll;
2928 if (s->thread_args.load_all) {
2929 if (ch == KEY_BACKSPACE)
2930 s->thread_args.load_all = 0;
2931 else if (s->thread_args.log_complete) {
2932 err = log_move_cursor_down(view, s->commits.ncommits);
2933 s->thread_args.load_all = 0;
2938 eos = nscroll = view->nlines - 1;
2939 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2940 view_is_splitscreen(view->child))
2952 view->x = MAX(view->maxx - view->ncols / 2, 0);
2957 if (view->x + view->ncols / 2 < view->maxx)
2958 view->x += 2; /* move two columns right */
2964 view->x -= MIN(view->x, 2); /* move two columns back */
2973 log_move_cursor_up(view, 0, 0);
2977 log_move_cursor_up(view, 0, 1);
2987 log_move_cursor_up(view, nscroll, 0);
2994 err = log_move_cursor_down(view, 0);
2998 /* We don't know yet how many commits, so we're forced to
2999 * traverse them all. */
3001 if (!s->thread_args.log_complete) {
3002 s->thread_args.load_all = 1;
3003 return trigger_log_thread(view, 0);
3007 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3008 for (n = 0; n < eos; n++) {
3011 s->first_displayed_entry = entry;
3012 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3015 s->selected = n - 1;
3027 err = log_move_cursor_down(view, nscroll);
3030 if (s->selected > view->nlines - 2)
3031 s->selected = view->nlines - 2;
3032 if (s->selected > s->commits.ncommits - 1)
3033 s->selected = s->commits.ncommits - 1;
3035 if (s->commits.ncommits < view->nlines - 1 &&
3036 !s->thread_args.log_complete) {
3037 s->thread_args.commits_needed += (view->nlines - 1) -
3038 s->commits.ncommits;
3039 err = trigger_log_thread(view, 1);
3045 if (s->selected_entry == NULL)
3048 /* get dimensions--don't split till initialisation succeeds */
3049 if (view_is_parent_view(view))
3050 view_get_split(view, &begin_y, &begin_x);
3052 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3053 s->selected_entry->commit, s->selected_entry->id,
3058 if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3059 err = view_init_hsplit(view, begin_y);
3065 diff_view->focussed = 1;
3066 diff_view->mode = view->mode;
3067 diff_view->nlines = view->lines - begin_y;
3069 if (view_is_parent_view(view)) {
3070 err = view_close_child(view);
3073 err = view_set_child(view, diff_view);
3076 view->focus_child = 1;
3078 *new_view = diff_view;
3083 if (s->selected_entry == NULL)
3085 if (view_is_parent_view(view))
3086 begin_x = view_split_begin_x(view->begin_x);
3087 err = browse_commit_tree(&tree_view, begin_x,
3088 s->selected_entry, s->in_repo_path, s->head_ref_name,
3093 tree_view->focussed = 1;
3094 if (view_is_parent_view(view)) {
3095 err = view_close_child(view);
3098 err = view_set_child(view, tree_view);
3101 view->focus_child = 1;
3103 *new_view = tree_view;
3109 if (ch == KEY_BACKSPACE &&
3110 got_path_is_root_dir(s->in_repo_path))
3112 err = stop_log_thread(s);
3115 if (ch == KEY_BACKSPACE) {
3117 err = got_path_dirname(&parent_path, s->in_repo_path);
3120 free(s->in_repo_path);
3121 s->in_repo_path = parent_path;
3122 s->thread_args.in_repo_path = s->in_repo_path;
3123 } else if (ch == CTRL('l')) {
3124 struct got_object_id *start_id;
3125 err = got_repo_match_object_id(&start_id, NULL,
3126 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3127 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3131 s->start_id = start_id;
3132 s->thread_args.start_id = s->start_id;
3134 s->log_branches = !s->log_branches;
3136 if (s->thread_args.pack_fds == NULL) {
3137 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3141 err = got_repo_open(&s->thread_args.repo,
3142 got_repo_get_path(s->repo), NULL,
3143 s->thread_args.pack_fds);
3147 err = tog_load_refs(s->repo, 0);
3150 err = got_commit_graph_open(&s->thread_args.graph,
3151 s->in_repo_path, !s->log_branches);
3154 err = got_commit_graph_iter_start(s->thread_args.graph,
3155 s->start_id, s->repo, NULL, NULL);
3158 free_commits(&s->commits);
3159 s->first_displayed_entry = NULL;
3160 s->last_displayed_entry = NULL;
3161 s->selected_entry = NULL;
3163 s->thread_args.log_complete = 0;
3165 s->thread_args.commits_needed = view->lines;
3166 s->matched_entry = NULL;
3167 s->search_entry = NULL;
3171 if (view_is_parent_view(view))
3172 begin_x = view_split_begin_x(view->begin_x);
3173 ref_view = view_open(view->nlines, view->ncols,
3174 view->begin_y, begin_x, TOG_VIEW_REF);
3175 if (ref_view == NULL)
3176 return got_error_from_errno("view_open");
3177 err = open_ref_view(ref_view, s->repo);
3179 view_close(ref_view);
3183 ref_view->focussed = 1;
3184 if (view_is_parent_view(view)) {
3185 err = view_close_child(view);
3188 err = view_set_child(view, ref_view);
3191 view->focus_child = 1;
3193 *new_view = ref_view;
3203 static const struct got_error *
3204 apply_unveil(const char *repo_path, const char *worktree_path)
3206 const struct got_error *error;
3209 if (unveil("gmon.out", "rwc") != 0)
3210 return got_error_from_errno2("unveil", "gmon.out");
3212 if (repo_path && unveil(repo_path, "r") != 0)
3213 return got_error_from_errno2("unveil", repo_path);
3215 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3216 return got_error_from_errno2("unveil", worktree_path);
3218 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3219 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3221 error = got_privsep_unveil_exec_helpers();
3225 if (unveil(NULL, NULL) != 0)
3226 return got_error_from_errno("unveil");
3235 * Override default signal handlers before starting ncurses.
3236 * This should prevent ncurses from installing its own
3237 * broken cleanup() signal handler.
3239 signal(SIGWINCH, tog_sigwinch);
3240 signal(SIGPIPE, tog_sigpipe);
3241 signal(SIGCONT, tog_sigcont);
3242 signal(SIGINT, tog_sigint);
3243 signal(SIGTERM, tog_sigterm);
3247 halfdelay(1); /* Do fast refresh while initial view is loading. */
3250 intrflush(stdscr, FALSE);
3251 keypad(stdscr, TRUE);
3253 if (getenv("TOG_COLORS") != NULL) {
3255 use_default_colors();
3259 static const struct got_error *
3260 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3261 struct got_repository *repo, struct got_worktree *worktree)
3263 const struct got_error *err = NULL;
3266 *in_repo_path = strdup("/");
3267 if (*in_repo_path == NULL)
3268 return got_error_from_errno("strdup");
3273 const char *prefix = got_worktree_get_path_prefix(worktree);
3276 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3279 if (asprintf(in_repo_path, "%s%s%s", prefix,
3280 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3282 err = got_error_from_errno("asprintf");
3283 *in_repo_path = NULL;
3287 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3292 static const struct got_error *
3293 cmd_log(int argc, char *argv[])
3295 const struct got_error *error;
3296 struct got_repository *repo = NULL;
3297 struct got_worktree *worktree = NULL;
3298 struct got_object_id *start_id = NULL;
3299 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3300 char *start_commit = NULL, *label = NULL;
3301 struct got_reference *ref = NULL;
3302 const char *head_ref_name = NULL;
3303 int ch, log_branches = 0;
3304 struct tog_view *view;
3305 int *pack_fds = NULL;
3307 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3313 start_commit = optarg;
3316 repo_path = realpath(optarg, NULL);
3317 if (repo_path == NULL)
3318 return got_error_from_errno2("realpath",
3333 error = got_repo_pack_fds_open(&pack_fds);
3337 if (repo_path == NULL) {
3338 cwd = getcwd(NULL, 0);
3340 return got_error_from_errno("getcwd");
3341 error = got_worktree_open(&worktree, cwd);
3342 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3346 strdup(got_worktree_get_repo_path(worktree));
3348 repo_path = strdup(cwd);
3349 if (repo_path == NULL) {
3350 error = got_error_from_errno("strdup");
3355 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3359 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3366 error = apply_unveil(got_repo_get_path(repo),
3367 worktree ? got_worktree_get_root_path(worktree) : NULL);
3371 /* already loaded by tog_log_with_path()? */
3372 if (TAILQ_EMPTY(&tog_refs)) {
3373 error = tog_load_refs(repo, 0);
3378 if (start_commit == NULL) {
3379 error = got_repo_match_object_id(&start_id, &label,
3380 worktree ? got_worktree_get_head_ref_name(worktree) :
3381 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3384 head_ref_name = label;
3386 error = got_ref_open(&ref, repo, start_commit, 0);
3388 head_ref_name = got_ref_get_name(ref);
3389 else if (error->code != GOT_ERR_NOT_REF)
3391 error = got_repo_match_object_id(&start_id, NULL,
3392 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3397 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3399 error = got_error_from_errno("view_open");
3402 error = open_log_view(view, start_id, repo, head_ref_name,
3403 in_repo_path, log_branches);
3407 /* Release work tree lock. */
3408 got_worktree_close(worktree);
3411 error = view_loop(view);
3421 const struct got_error *close_err = got_repo_close(repo);
3426 got_worktree_close(worktree);
3428 const struct got_error *pack_err =
3429 got_repo_pack_fds_close(pack_fds);
3441 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3442 "[-w] object1 object2\n", getprogname());
3447 match_line(const char *line, regex_t *regex, size_t nmatch,
3448 regmatch_t *regmatch)
3450 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3453 static struct tog_color *
3454 match_color(struct tog_colors *colors, const char *line)
3456 struct tog_color *tc = NULL;
3458 STAILQ_FOREACH(tc, colors, entry) {
3459 if (match_line(line, &tc->regex, 0, NULL))
3466 static const struct got_error *
3467 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3468 WINDOW *window, int skipcol, regmatch_t *regmatch)
3470 const struct got_error *err = NULL;
3472 wchar_t *wline = NULL;
3473 int rme, rms, n, width, scrollx;
3474 int width0 = 0, width1 = 0, width2 = 0;
3475 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3479 rms = regmatch->rm_so;
3480 rme = regmatch->rm_eo;
3482 err = expand_tab(&exstr, line);
3486 /* Split the line into 3 segments, according to match offsets. */
3487 seg0 = strndup(exstr, rms);
3489 err = got_error_from_errno("strndup");
3492 seg1 = strndup(exstr + rms, rme - rms);
3494 err = got_error_from_errno("strndup");
3497 seg2 = strdup(exstr + rme);
3499 err = got_error_from_errno("strndup");
3503 /* draw up to matched token if we haven't scrolled past it */
3504 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3508 n = MAX(width0 - skipcol, 0);
3511 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3512 wlimit, col_tab_align, 1);
3515 waddwstr(window, &wline[scrollx]);
3525 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3529 wlen = wcslen(wline);
3531 width = wcwidth(wline[i]);
3533 /* should not happen, tabs are expanded */
3534 err = got_error(GOT_ERR_RANGE);
3537 if (width0 + w + width > skipcol)
3542 /* draw (visible part of) matched token (if scrolled into it) */
3543 if (width1 - w > 0) {
3544 wattron(window, A_STANDOUT);
3545 waddwstr(window, &wline[i]);
3546 wattroff(window, A_STANDOUT);
3547 wlimit -= (width1 - w);
3548 *wtotal += (width1 - w);
3552 if (wlimit > 0) { /* draw rest of line */
3554 if (skipcol > width0 + width1) {
3555 err = format_line(&wline, &width2, &scrollx, seg2,
3556 skipcol - (width0 + width1), wlimit,
3560 waddwstr(window, &wline[scrollx]);
3562 err = format_line(&wline, &width2, NULL, seg2, 0,
3563 wlimit, col_tab_align, 1);
3566 waddwstr(window, wline);
3579 static const struct got_error *
3580 draw_file(struct tog_view *view, const char *header)
3582 struct tog_diff_view_state *s = &view->state.diff;
3583 regmatch_t *regmatch = &view->regmatch;
3584 const struct got_error *err;
3587 size_t linesize = 0;
3589 struct tog_color *tc;
3592 int max_lines = view->nlines;
3593 int nlines = s->nlines;
3596 line_offset = s->line_offsets[s->first_displayed_line - 1];
3597 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3598 return got_error_from_errno("fseek");
3600 werase(view->window);
3603 if (asprintf(&line, "[%d/%d] %s",
3604 s->first_displayed_line - 1 + s->selected_line, nlines,
3606 return got_error_from_errno("asprintf");
3607 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3613 if (view_needs_focus_indication(view))
3614 wstandout(view->window);
3615 waddwstr(view->window, wline);
3618 if (view_needs_focus_indication(view))
3619 wstandend(view->window);
3620 if (width <= view->ncols - 1)
3621 waddch(view->window, '\n');
3631 while (max_lines > 0 && nprinted < max_lines) {
3632 linelen = getline(&line, &linesize, s->f);
3633 if (linelen == -1) {
3639 return got_ferror(s->f, GOT_ERR_IO);
3642 /* Set view->maxx based on full line length. */
3643 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3649 view->maxx = MAX(view->maxx, width);
3653 tc = match_color(&s->colors, line);
3655 wattr_on(view->window,
3656 COLOR_PAIR(tc->colorpair), NULL);
3657 if (s->first_displayed_line + nprinted == s->matched_line &&
3658 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3659 err = add_matched_line(&width, line, view->ncols, 0,
3660 view->window, view->x, regmatch);
3667 err = format_line(&wline, &width, &skip, line,
3668 view->x, view->ncols, 0, view->x ? 1 : 0);
3673 waddwstr(view->window, &wline[skip]);
3678 wattr_off(view->window,
3679 COLOR_PAIR(tc->colorpair), NULL);
3680 if (width <= view->ncols - 1)
3681 waddch(view->window, '\n');
3686 s->last_displayed_line = s->first_displayed_line +
3689 s->last_displayed_line = s->first_displayed_line;
3694 while (nprinted < view->nlines) {
3695 waddch(view->window, '\n');
3699 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3705 wstandout(view->window);
3706 waddwstr(view->window, wline);
3709 wstandend(view->window);
3716 get_datestr(time_t *time, char *datebuf)
3718 struct tm mytm, *tm;
3721 tm = gmtime_r(time, &mytm);
3724 s = asctime_r(tm, datebuf);
3727 p = strchr(s, '\n');
3733 static const struct got_error *
3734 get_changed_paths(struct got_pathlist_head *paths,
3735 struct got_commit_object *commit, struct got_repository *repo)
3737 const struct got_error *err = NULL;
3738 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3739 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3740 struct got_object_qid *qid;
3742 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3744 struct got_commit_object *pcommit;
3745 err = got_object_open_as_commit(&pcommit, repo,
3750 tree_id1 = got_object_id_dup(
3751 got_object_commit_get_tree_id(pcommit));
3752 if (tree_id1 == NULL) {
3753 got_object_commit_close(pcommit);
3754 return got_error_from_errno("got_object_id_dup");
3756 got_object_commit_close(pcommit);
3761 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3766 tree_id2 = got_object_commit_get_tree_id(commit);
3767 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3771 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3772 got_diff_tree_collect_changed_paths, paths, 0);
3775 got_object_tree_close(tree1);
3777 got_object_tree_close(tree2);
3782 static const struct got_error *
3783 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3787 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3789 return got_error_from_errno("reallocarray");
3791 (*line_offsets)[*nlines] = off;
3796 static const struct got_error *
3797 write_commit_info(off_t **line_offsets, size_t *nlines,
3798 struct got_object_id *commit_id, struct got_reflist_head *refs,
3799 struct got_repository *repo, FILE *outfile)
3801 const struct got_error *err = NULL;
3802 char datebuf[26], *datestr;
3803 struct got_commit_object *commit;
3804 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3805 time_t committer_time;
3806 const char *author, *committer;
3807 char *refs_str = NULL;
3808 struct got_pathlist_head changed_paths;
3809 struct got_pathlist_entry *pe;
3813 TAILQ_INIT(&changed_paths);
3816 err = build_refs_str(&refs_str, refs, commit_id, repo);
3821 err = got_object_open_as_commit(&commit, repo, commit_id);
3825 err = got_object_id_str(&id_str, commit_id);
3827 err = got_error_from_errno("got_object_id_str");
3831 err = add_line_offset(line_offsets, nlines, 0);
3835 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3836 refs_str ? refs_str : "", refs_str ? ")" : "");
3838 err = got_error_from_errno("fprintf");
3842 err = add_line_offset(line_offsets, nlines, outoff);
3846 n = fprintf(outfile, "from: %s\n",
3847 got_object_commit_get_author(commit));
3849 err = got_error_from_errno("fprintf");
3853 err = add_line_offset(line_offsets, nlines, outoff);
3857 committer_time = got_object_commit_get_committer_time(commit);
3858 datestr = get_datestr(&committer_time, datebuf);
3860 n = fprintf(outfile, "date: %s UTC\n", datestr);
3862 err = got_error_from_errno("fprintf");
3866 err = add_line_offset(line_offsets, nlines, outoff);
3870 author = got_object_commit_get_author(commit);
3871 committer = got_object_commit_get_committer(commit);
3872 if (strcmp(author, committer) != 0) {
3873 n = fprintf(outfile, "via: %s\n", committer);
3875 err = got_error_from_errno("fprintf");
3879 err = add_line_offset(line_offsets, nlines, outoff);
3883 if (got_object_commit_get_nparents(commit) > 1) {
3884 const struct got_object_id_queue *parent_ids;
3885 struct got_object_qid *qid;
3887 parent_ids = got_object_commit_get_parent_ids(commit);
3888 STAILQ_FOREACH(qid, parent_ids, entry) {
3889 err = got_object_id_str(&id_str, &qid->id);
3892 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3894 err = got_error_from_errno("fprintf");
3898 err = add_line_offset(line_offsets, nlines, outoff);
3906 err = got_object_commit_get_logmsg(&logmsg, commit);
3910 while ((line = strsep(&s, "\n")) != NULL) {
3911 n = fprintf(outfile, "%s\n", line);
3913 err = got_error_from_errno("fprintf");
3917 err = add_line_offset(line_offsets, nlines, outoff);
3922 err = get_changed_paths(&changed_paths, commit, repo);
3925 TAILQ_FOREACH(pe, &changed_paths, entry) {
3926 struct got_diff_changed_path *cp = pe->data;
3927 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3929 err = got_error_from_errno("fprintf");
3933 err = add_line_offset(line_offsets, nlines, outoff);
3936 free((char *)pe->path);
3940 fputc('\n', outfile);
3942 err = add_line_offset(line_offsets, nlines, outoff);
3944 got_pathlist_free(&changed_paths);
3948 got_object_commit_close(commit);
3950 free(*line_offsets);
3951 *line_offsets = NULL;
3957 static const struct got_error *
3958 create_diff(struct tog_diff_view_state *s)
3960 const struct got_error *err = NULL;
3964 free(s->line_offsets);
3965 s->line_offsets = malloc(sizeof(off_t));
3966 if (s->line_offsets == NULL)
3967 return got_error_from_errno("malloc");
3972 err = got_error_from_errno("got_opentemp");
3975 if (s->f && fclose(s->f) == EOF) {
3976 err = got_error_from_errno("fclose");
3982 err = got_object_get_type(&obj_type, s->repo, s->id1);
3984 err = got_object_get_type(&obj_type, s->repo, s->id2);
3989 case GOT_OBJ_TYPE_BLOB:
3990 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3991 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3992 s->label1, s->label2, tog_diff_algo, s->diff_context,
3993 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3995 case GOT_OBJ_TYPE_TREE:
3996 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3997 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3998 tog_diff_algo, s->diff_context, s->ignore_whitespace,
3999 s->force_text_diff, s->repo, s->f);
4001 case GOT_OBJ_TYPE_COMMIT: {
4002 const struct got_object_id_queue *parent_ids;
4003 struct got_object_qid *pid;
4004 struct got_commit_object *commit2;
4005 struct got_reflist_head *refs;
4007 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4010 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4011 /* Show commit info if we're diffing to a parent/root commit. */
4012 if (s->id1 == NULL) {
4013 err = write_commit_info(&s->line_offsets, &s->nlines,
4014 s->id2, refs, s->repo, s->f);
4018 parent_ids = got_object_commit_get_parent_ids(commit2);
4019 STAILQ_FOREACH(pid, parent_ids, entry) {
4020 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4021 err = write_commit_info(
4022 &s->line_offsets, &s->nlines,
4023 s->id2, refs, s->repo, s->f);
4030 got_object_commit_close(commit2);
4032 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4033 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4034 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4035 s->force_text_diff, s->repo, s->f);
4039 err = got_error(GOT_ERR_OBJ_TYPE);
4045 if (s->f && fflush(s->f) != 0 && err == NULL)
4046 err = got_error_from_errno("fflush");
4051 diff_view_indicate_progress(struct tog_view *view)
4053 mvwaddstr(view->window, 0, 0, "diffing...");
4058 static const struct got_error *
4059 search_start_diff_view(struct tog_view *view)
4061 struct tog_diff_view_state *s = &view->state.diff;
4063 s->matched_line = 0;
4067 static const struct got_error *
4068 search_next_diff_view(struct tog_view *view)
4070 struct tog_diff_view_state *s = &view->state.diff;
4071 const struct got_error *err = NULL;
4074 size_t linesize = 0;
4077 if (!view->searching) {
4078 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4082 if (s->matched_line) {
4083 if (view->searching == TOG_SEARCH_FORWARD)
4084 lineno = s->matched_line + 1;
4086 lineno = s->matched_line - 1;
4088 lineno = s->first_displayed_line;
4093 if (lineno <= 0 || lineno > s->nlines) {
4094 if (s->matched_line == 0) {
4095 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4099 if (view->searching == TOG_SEARCH_FORWARD)
4105 offset = s->line_offsets[lineno - 1];
4106 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4108 return got_error_from_errno("fseeko");
4110 linelen = getline(&line, &linesize, s->f);
4111 if (linelen != -1) {
4113 err = expand_tab(&exstr, line);
4116 if (match_line(exstr, &view->regex, 1,
4118 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4119 s->matched_line = lineno;
4125 if (view->searching == TOG_SEARCH_FORWARD)
4132 if (s->matched_line) {
4133 s->first_displayed_line = s->matched_line;
4134 s->selected_line = 1;
4140 static const struct got_error *
4141 close_diff_view(struct tog_view *view)
4143 const struct got_error *err = NULL;
4144 struct tog_diff_view_state *s = &view->state.diff;
4150 if (s->f && fclose(s->f) == EOF)
4151 err = got_error_from_errno("fclose");
4153 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4154 err = got_error_from_errno("fclose");
4156 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4157 err = got_error_from_errno("fclose");
4159 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4160 err = got_error_from_errno("close");
4162 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4163 err = got_error_from_errno("close");
4165 free_colors(&s->colors);
4166 free(s->line_offsets);
4167 s->line_offsets = NULL;
4172 static const struct got_error *
4173 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4174 struct got_object_id *id2, const char *label1, const char *label2,
4175 int diff_context, int ignore_whitespace, int force_text_diff,
4176 struct tog_view *log_view, struct got_repository *repo)
4178 const struct got_error *err;
4179 struct tog_diff_view_state *s = &view->state.diff;
4181 memset(s, 0, sizeof(*s));
4185 if (id1 != NULL && id2 != NULL) {
4187 err = got_object_get_type(&type1, repo, id1);
4190 err = got_object_get_type(&type2, repo, id2);
4195 return got_error(GOT_ERR_OBJ_TYPE);
4197 s->first_displayed_line = 1;
4198 s->last_displayed_line = view->nlines;
4199 s->selected_line = 1;
4207 s->id1 = got_object_id_dup(id1);
4209 return got_error_from_errno("got_object_id_dup");
4213 s->id2 = got_object_id_dup(id2);
4214 if (s->id2 == NULL) {
4215 err = got_error_from_errno("got_object_id_dup");
4219 s->f1 = got_opentemp();
4220 if (s->f1 == NULL) {
4221 err = got_error_from_errno("got_opentemp");
4225 s->f2 = got_opentemp();
4226 if (s->f2 == NULL) {
4227 err = got_error_from_errno("got_opentemp");
4231 s->fd1 = got_opentempfd();
4233 err = got_error_from_errno("got_opentempfd");
4237 s->fd2 = got_opentempfd();
4239 err = got_error_from_errno("got_opentempfd");
4243 s->first_displayed_line = 1;
4244 s->last_displayed_line = view->nlines;
4245 s->diff_context = diff_context;
4246 s->ignore_whitespace = ignore_whitespace;
4247 s->force_text_diff = force_text_diff;
4248 s->log_view = log_view;
4251 STAILQ_INIT(&s->colors);
4252 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4253 err = add_color(&s->colors,
4254 "^-", TOG_COLOR_DIFF_MINUS,
4255 get_color_value("TOG_COLOR_DIFF_MINUS"));
4258 err = add_color(&s->colors, "^\\+",
4259 TOG_COLOR_DIFF_PLUS,
4260 get_color_value("TOG_COLOR_DIFF_PLUS"));
4263 err = add_color(&s->colors,
4264 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4265 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4269 err = add_color(&s->colors,
4270 "^(commit [0-9a-f]|parent [0-9]|"
4271 "(blob|file|tree|commit) [-+] |"
4272 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4273 get_color_value("TOG_COLOR_DIFF_META"));
4277 err = add_color(&s->colors,
4278 "^(from|via): ", TOG_COLOR_AUTHOR,
4279 get_color_value("TOG_COLOR_AUTHOR"));
4283 err = add_color(&s->colors,
4284 "^date: ", TOG_COLOR_DATE,
4285 get_color_value("TOG_COLOR_DATE"));
4290 if (log_view && view_is_splitscreen(view))
4291 show_log_view(log_view); /* draw vborder */
4292 diff_view_indicate_progress(view);
4294 err = create_diff(s);
4296 view->show = show_diff_view;
4297 view->input = input_diff_view;
4298 view->reset = reset_diff_view;
4299 view->close = close_diff_view;
4300 view->search_start = search_start_diff_view;
4301 view->search_next = search_next_diff_view;
4304 close_diff_view(view);
4308 static const struct got_error *
4309 show_diff_view(struct tog_view *view)
4311 const struct got_error *err;
4312 struct tog_diff_view_state *s = &view->state.diff;
4313 char *id_str1 = NULL, *id_str2, *header;
4314 const char *label1, *label2;
4317 err = got_object_id_str(&id_str1, s->id1);
4320 label1 = s->label1 ? : id_str1;
4322 label1 = "/dev/null";
4324 err = got_object_id_str(&id_str2, s->id2);
4327 label2 = s->label2 ? : id_str2;
4329 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4330 err = got_error_from_errno("asprintf");
4338 err = draw_file(view, header);
4343 static const struct got_error *
4344 set_selected_commit(struct tog_diff_view_state *s,
4345 struct commit_queue_entry *entry)
4347 const struct got_error *err;
4348 const struct got_object_id_queue *parent_ids;
4349 struct got_commit_object *selected_commit;
4350 struct got_object_qid *pid;
4353 s->id2 = got_object_id_dup(entry->id);
4355 return got_error_from_errno("got_object_id_dup");
4357 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4360 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4362 pid = STAILQ_FIRST(parent_ids);
4363 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4364 got_object_commit_close(selected_commit);
4368 static const struct got_error *
4369 reset_diff_view(struct tog_view *view)
4371 struct tog_diff_view_state *s = &view->state.diff;
4374 wclear(view->window);
4375 s->first_displayed_line = 1;
4376 s->last_displayed_line = view->nlines;
4377 s->matched_line = 0;
4378 diff_view_indicate_progress(view);
4379 return create_diff(s);
4382 static const struct got_error *
4383 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4385 const struct got_error *err = NULL;
4386 struct tog_diff_view_state *s = &view->state.diff;
4387 struct tog_log_view_state *ls;
4388 struct commit_queue_entry *old_selected_entry;
4390 size_t linesize = 0;
4392 int i, nscroll = view->nlines - 1;
4399 view->x = MAX(view->maxx - view->ncols / 3, 0);
4404 if (view->x + view->ncols / 3 < view->maxx)
4405 view->x += 2; /* move two columns right */
4411 view->x -= MIN(view->x, 2); /* move two columns back */
4418 s->force_text_diff = !s->force_text_diff;
4420 s->ignore_whitespace = !s->ignore_whitespace;
4421 err = reset_diff_view(view);
4425 s->first_displayed_line = 1;
4434 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4440 if (s->first_displayed_line > 1)
4441 s->first_displayed_line--;
4452 if (s->first_displayed_line == 1) {
4457 while (i++ < nscroll && s->first_displayed_line > 1)
4458 s->first_displayed_line--;
4464 s->first_displayed_line++;
4481 while (!s->eof && i++ < nscroll) {
4482 linelen = getline(&line, &linesize, s->f);
4483 s->first_displayed_line++;
4484 if (linelen == -1) {
4488 err = got_ferror(s->f, GOT_ERR_IO);
4495 if (s->diff_context > 0) {
4497 s->matched_line = 0;
4498 diff_view_indicate_progress(view);
4499 err = create_diff(s);
4500 if (s->first_displayed_line + view->nlines - 1 >
4502 s->first_displayed_line = 1;
4503 s->last_displayed_line = view->nlines;
4509 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4511 s->matched_line = 0;
4512 diff_view_indicate_progress(view);
4513 err = create_diff(s);
4519 if (s->log_view == NULL) {
4523 ls = &s->log_view->state.log;
4524 old_selected_entry = ls->selected_entry;
4526 /* view->count handled in input_log_view() */
4527 err = input_log_view(NULL, s->log_view, KEY_UP);
4531 if (old_selected_entry == ls->selected_entry)
4534 err = set_selected_commit(s, ls->selected_entry);
4538 s->first_displayed_line = 1;
4539 s->last_displayed_line = view->nlines;
4540 s->matched_line = 0;
4543 diff_view_indicate_progress(view);
4544 err = create_diff(s);
4548 if (s->log_view == NULL) {
4552 ls = &s->log_view->state.log;
4553 old_selected_entry = ls->selected_entry;
4555 /* view->count handled in input_log_view() */
4556 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4560 if (old_selected_entry == ls->selected_entry)
4563 err = set_selected_commit(s, ls->selected_entry);
4567 s->first_displayed_line = 1;
4568 s->last_displayed_line = view->nlines;
4569 s->matched_line = 0;
4572 diff_view_indicate_progress(view);
4573 err = create_diff(s);
4583 static const struct got_error *
4584 cmd_diff(int argc, char *argv[])
4586 const struct got_error *error = NULL;
4587 struct got_repository *repo = NULL;
4588 struct got_worktree *worktree = NULL;
4589 struct got_object_id *id1 = NULL, *id2 = NULL;
4590 char *repo_path = NULL, *cwd = NULL;
4591 char *id_str1 = NULL, *id_str2 = NULL;
4592 char *label1 = NULL, *label2 = NULL;
4593 int diff_context = 3, ignore_whitespace = 0;
4594 int ch, force_text_diff = 0;
4596 struct tog_view *view;
4597 int *pack_fds = NULL;
4599 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4602 force_text_diff = 1;
4605 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4608 errx(1, "number of context lines is %s: %s",
4612 repo_path = realpath(optarg, NULL);
4613 if (repo_path == NULL)
4614 return got_error_from_errno2("realpath",
4616 got_path_strip_trailing_slashes(repo_path);
4619 ignore_whitespace = 1;
4631 usage_diff(); /* TODO show local worktree changes */
4632 } else if (argc == 2) {
4638 error = got_repo_pack_fds_open(&pack_fds);
4642 if (repo_path == NULL) {
4643 cwd = getcwd(NULL, 0);
4645 return got_error_from_errno("getcwd");
4646 error = got_worktree_open(&worktree, cwd);
4647 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4651 strdup(got_worktree_get_repo_path(worktree));
4653 repo_path = strdup(cwd);
4654 if (repo_path == NULL) {
4655 error = got_error_from_errno("strdup");
4660 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4666 error = apply_unveil(got_repo_get_path(repo), NULL);
4670 error = tog_load_refs(repo, 0);
4674 error = got_repo_match_object_id(&id1, &label1, id_str1,
4675 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4679 error = got_repo_match_object_id(&id2, &label2, id_str2,
4680 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4684 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4686 error = got_error_from_errno("view_open");
4689 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4690 ignore_whitespace, force_text_diff, NULL, repo);
4693 error = view_loop(view);
4700 const struct got_error *close_err = got_repo_close(repo);
4705 got_worktree_close(worktree);
4707 const struct got_error *pack_err =
4708 got_repo_pack_fds_close(pack_fds);
4721 "usage: %s blame [-c commit] [-r repository-path] path\n",
4726 struct tog_blame_line {
4728 struct got_object_id *id;
4731 static const struct got_error *
4732 draw_blame(struct tog_view *view)
4734 struct tog_blame_view_state *s = &view->state.blame;
4735 struct tog_blame *blame = &s->blame;
4736 regmatch_t *regmatch = &view->regmatch;
4737 const struct got_error *err;
4738 int lineno = 0, nprinted = 0;
4740 size_t linesize = 0;
4744 struct tog_blame_line *blame_line;
4745 struct got_object_id *prev_id = NULL;
4747 struct tog_color *tc;
4749 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4754 werase(view->window);
4756 if (asprintf(&line, "commit %s", id_str) == -1) {
4757 err = got_error_from_errno("asprintf");
4762 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4767 if (view_needs_focus_indication(view))
4768 wstandout(view->window);
4769 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4771 wattr_on(view->window,
4772 COLOR_PAIR(tc->colorpair), NULL);
4773 waddwstr(view->window, wline);
4775 wattr_off(view->window,
4776 COLOR_PAIR(tc->colorpair), NULL);
4777 if (view_needs_focus_indication(view))
4778 wstandend(view->window);
4781 if (width < view->ncols - 1)
4782 waddch(view->window, '\n');
4784 if (asprintf(&line, "[%d/%d] %s%s",
4785 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4786 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4788 return got_error_from_errno("asprintf");
4791 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4796 waddwstr(view->window, wline);
4799 if (width < view->ncols - 1)
4800 waddch(view->window, '\n');
4804 while (nprinted < view->nlines - 2) {
4805 linelen = getline(&line, &linesize, blame->f);
4806 if (linelen == -1) {
4807 if (feof(blame->f)) {
4812 return got_ferror(blame->f, GOT_ERR_IO);
4814 if (++lineno < s->first_displayed_line)
4817 /* Set view->maxx based on full line length. */
4818 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4825 view->maxx = MAX(view->maxx, width);
4827 if (view->focussed && nprinted == s->selected_line - 1)
4828 wstandout(view->window);
4830 if (blame->nlines > 0) {
4831 blame_line = &blame->lines[lineno - 1];
4832 if (blame_line->annotated && prev_id &&
4833 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4835 nprinted == s->selected_line - 1)) {
4836 waddstr(view->window, " ");
4837 } else if (blame_line->annotated) {
4839 err = got_object_id_str(&id_str,
4845 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4847 wattr_on(view->window,
4848 COLOR_PAIR(tc->colorpair), NULL);
4849 wprintw(view->window, "%.8s", id_str);
4851 wattr_off(view->window,
4852 COLOR_PAIR(tc->colorpair), NULL);
4854 prev_id = blame_line->id;
4856 waddstr(view->window, "........");
4860 waddstr(view->window, "........");
4864 if (view->focussed && nprinted == s->selected_line - 1)
4865 wstandend(view->window);
4866 waddstr(view->window, " ");
4868 if (view->ncols <= 9) {
4870 } else if (s->first_displayed_line + nprinted ==
4872 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4873 err = add_matched_line(&width, line, view->ncols - 9, 9,
4874 view->window, view->x, regmatch);
4882 err = format_line(&wline, &width, &skip, line,
4883 view->x, view->ncols - 9, 9, 1);
4888 waddwstr(view->window, &wline[skip]);
4894 if (width <= view->ncols - 1)
4895 waddch(view->window, '\n');
4896 if (++nprinted == 1)
4897 s->first_displayed_line = lineno;
4900 s->last_displayed_line = lineno;
4907 static const struct got_error *
4908 blame_cb(void *arg, int nlines, int lineno,
4909 struct got_commit_object *commit, struct got_object_id *id)
4911 const struct got_error *err = NULL;
4912 struct tog_blame_cb_args *a = arg;
4913 struct tog_blame_line *line;
4916 if (nlines != a->nlines ||
4917 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4918 return got_error(GOT_ERR_RANGE);
4920 errcode = pthread_mutex_lock(&tog_mutex);
4922 return got_error_set_errno(errcode, "pthread_mutex_lock");
4924 if (*a->quit) { /* user has quit the blame view */
4925 err = got_error(GOT_ERR_ITER_COMPLETED);
4930 goto done; /* no change in this commit */
4932 line = &a->lines[lineno - 1];
4933 if (line->annotated)
4936 line->id = got_object_id_dup(id);
4937 if (line->id == NULL) {
4938 err = got_error_from_errno("got_object_id_dup");
4941 line->annotated = 1;
4943 errcode = pthread_mutex_unlock(&tog_mutex);
4945 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4950 blame_thread(void *arg)
4952 const struct got_error *err, *close_err;
4953 struct tog_blame_thread_args *ta = arg;
4954 struct tog_blame_cb_args *a = ta->cb_args;
4955 int errcode, fd1 = -1, fd2 = -1;
4956 FILE *f1 = NULL, *f2 = NULL;
4958 fd1 = got_opentempfd();
4960 return (void *)got_error_from_errno("got_opentempfd");
4962 fd2 = got_opentempfd();
4964 err = got_error_from_errno("got_opentempfd");
4968 f1 = got_opentemp();
4970 err = (void *)got_error_from_errno("got_opentemp");
4973 f2 = got_opentemp();
4975 err = (void *)got_error_from_errno("got_opentemp");
4979 err = block_signals_used_by_main_thread();
4983 err = got_blame(ta->path, a->commit_id, ta->repo,
4984 tog_diff_algo, blame_cb, ta->cb_args,
4985 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
4986 if (err && err->code == GOT_ERR_CANCELLED)
4989 errcode = pthread_mutex_lock(&tog_mutex);
4991 err = got_error_set_errno(errcode, "pthread_mutex_lock");
4995 close_err = got_repo_close(ta->repo);
5001 errcode = pthread_mutex_unlock(&tog_mutex);
5002 if (errcode && err == NULL)
5003 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5006 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5007 err = got_error_from_errno("close");
5008 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5009 err = got_error_from_errno("close");
5010 if (f1 && fclose(f1) == EOF && err == NULL)
5011 err = got_error_from_errno("fclose");
5012 if (f2 && fclose(f2) == EOF && err == NULL)
5013 err = got_error_from_errno("fclose");
5018 static struct got_object_id *
5019 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5020 int first_displayed_line, int selected_line)
5022 struct tog_blame_line *line;
5027 line = &lines[first_displayed_line - 1 + selected_line - 1];
5028 if (!line->annotated)
5034 static const struct got_error *
5035 stop_blame(struct tog_blame *blame)
5037 const struct got_error *err = NULL;
5040 if (blame->thread) {
5042 errcode = pthread_mutex_unlock(&tog_mutex);
5044 return got_error_set_errno(errcode,
5045 "pthread_mutex_unlock");
5046 errcode = pthread_join(blame->thread, (void **)&err);
5048 return got_error_set_errno(errcode, "pthread_join");
5049 errcode = pthread_mutex_lock(&tog_mutex);
5051 return got_error_set_errno(errcode,
5052 "pthread_mutex_lock");
5053 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5055 blame->thread = NULL;
5057 if (blame->thread_args.repo) {
5058 const struct got_error *close_err;
5059 close_err = got_repo_close(blame->thread_args.repo);
5062 blame->thread_args.repo = NULL;
5065 if (fclose(blame->f) == EOF && err == NULL)
5066 err = got_error_from_errno("fclose");
5070 for (i = 0; i < blame->nlines; i++)
5071 free(blame->lines[i].id);
5073 blame->lines = NULL;
5075 free(blame->cb_args.commit_id);
5076 blame->cb_args.commit_id = NULL;
5077 if (blame->pack_fds) {
5078 const struct got_error *pack_err =
5079 got_repo_pack_fds_close(blame->pack_fds);
5082 blame->pack_fds = NULL;
5087 static const struct got_error *
5088 cancel_blame_view(void *arg)
5090 const struct got_error *err = NULL;
5094 errcode = pthread_mutex_lock(&tog_mutex);
5096 return got_error_set_errno(errcode,
5097 "pthread_mutex_unlock");
5100 err = got_error(GOT_ERR_CANCELLED);
5102 errcode = pthread_mutex_unlock(&tog_mutex);
5104 return got_error_set_errno(errcode,
5105 "pthread_mutex_lock");
5110 static const struct got_error *
5111 run_blame(struct tog_view *view)
5113 struct tog_blame_view_state *s = &view->state.blame;
5114 struct tog_blame *blame = &s->blame;
5115 const struct got_error *err = NULL;
5116 struct got_commit_object *commit = NULL;
5117 struct got_blob_object *blob = NULL;
5118 struct got_repository *thread_repo = NULL;
5119 struct got_object_id *obj_id = NULL;
5120 int obj_type, fd = -1;
5121 int *pack_fds = NULL;
5123 err = got_object_open_as_commit(&commit, s->repo,
5124 &s->blamed_commit->id);
5128 fd = got_opentempfd();
5130 err = got_error_from_errno("got_opentempfd");
5134 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5138 err = got_object_get_type(&obj_type, s->repo, obj_id);
5142 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5143 err = got_error(GOT_ERR_OBJ_TYPE);
5147 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5150 blame->f = got_opentemp();
5151 if (blame->f == NULL) {
5152 err = got_error_from_errno("got_opentemp");
5155 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5156 &blame->line_offsets, blame->f, blob);
5159 if (blame->nlines == 0) {
5160 s->blame_complete = 1;
5164 /* Don't include \n at EOF in the blame line count. */
5165 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5168 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5169 if (blame->lines == NULL) {
5170 err = got_error_from_errno("calloc");
5174 err = got_repo_pack_fds_open(&pack_fds);
5177 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5182 blame->pack_fds = pack_fds;
5183 blame->cb_args.view = view;
5184 blame->cb_args.lines = blame->lines;
5185 blame->cb_args.nlines = blame->nlines;
5186 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5187 if (blame->cb_args.commit_id == NULL) {
5188 err = got_error_from_errno("got_object_id_dup");
5191 blame->cb_args.quit = &s->done;
5193 blame->thread_args.path = s->path;
5194 blame->thread_args.repo = thread_repo;
5195 blame->thread_args.cb_args = &blame->cb_args;
5196 blame->thread_args.complete = &s->blame_complete;
5197 blame->thread_args.cancel_cb = cancel_blame_view;
5198 blame->thread_args.cancel_arg = &s->done;
5199 s->blame_complete = 0;
5201 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5202 s->first_displayed_line = 1;
5203 s->last_displayed_line = view->nlines;
5204 s->selected_line = 1;
5206 s->matched_line = 0;
5210 got_object_commit_close(commit);
5211 if (fd != -1 && close(fd) == -1 && err == NULL)
5212 err = got_error_from_errno("close");
5214 got_object_blob_close(blob);
5221 static const struct got_error *
5222 open_blame_view(struct tog_view *view, char *path,
5223 struct got_object_id *commit_id, struct got_repository *repo)
5225 const struct got_error *err = NULL;
5226 struct tog_blame_view_state *s = &view->state.blame;
5228 STAILQ_INIT(&s->blamed_commits);
5230 s->path = strdup(path);
5231 if (s->path == NULL)
5232 return got_error_from_errno("strdup");
5234 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5240 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5241 s->first_displayed_line = 1;
5242 s->last_displayed_line = view->nlines;
5243 s->selected_line = 1;
5244 s->blame_complete = 0;
5246 s->commit_id = commit_id;
5247 memset(&s->blame, 0, sizeof(s->blame));
5249 STAILQ_INIT(&s->colors);
5250 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5251 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5252 get_color_value("TOG_COLOR_COMMIT"));
5257 view->show = show_blame_view;
5258 view->input = input_blame_view;
5259 view->reset = reset_blame_view;
5260 view->close = close_blame_view;
5261 view->search_start = search_start_blame_view;
5262 view->search_next = search_next_blame_view;
5264 return run_blame(view);
5267 static const struct got_error *
5268 close_blame_view(struct tog_view *view)
5270 const struct got_error *err = NULL;
5271 struct tog_blame_view_state *s = &view->state.blame;
5273 if (s->blame.thread)
5274 err = stop_blame(&s->blame);
5276 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5277 struct got_object_qid *blamed_commit;
5278 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5279 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5280 got_object_qid_free(blamed_commit);
5284 free_colors(&s->colors);
5288 static const struct got_error *
5289 search_start_blame_view(struct tog_view *view)
5291 struct tog_blame_view_state *s = &view->state.blame;
5293 s->matched_line = 0;
5297 static const struct got_error *
5298 search_next_blame_view(struct tog_view *view)
5300 struct tog_blame_view_state *s = &view->state.blame;
5301 const struct got_error *err = NULL;
5304 size_t linesize = 0;
5307 if (!view->searching) {
5308 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5312 if (s->matched_line) {
5313 if (view->searching == TOG_SEARCH_FORWARD)
5314 lineno = s->matched_line + 1;
5316 lineno = s->matched_line - 1;
5318 lineno = s->first_displayed_line - 1 + s->selected_line;
5323 if (lineno <= 0 || lineno > s->blame.nlines) {
5324 if (s->matched_line == 0) {
5325 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5329 if (view->searching == TOG_SEARCH_FORWARD)
5332 lineno = s->blame.nlines;
5335 offset = s->blame.line_offsets[lineno - 1];
5336 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5338 return got_error_from_errno("fseeko");
5340 linelen = getline(&line, &linesize, s->blame.f);
5341 if (linelen != -1) {
5343 err = expand_tab(&exstr, line);
5346 if (match_line(exstr, &view->regex, 1,
5348 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5349 s->matched_line = lineno;
5355 if (view->searching == TOG_SEARCH_FORWARD)
5362 if (s->matched_line) {
5363 s->first_displayed_line = s->matched_line;
5364 s->selected_line = 1;
5370 static const struct got_error *
5371 show_blame_view(struct tog_view *view)
5373 const struct got_error *err = NULL;
5374 struct tog_blame_view_state *s = &view->state.blame;
5377 if (s->blame.thread == NULL && !s->blame_complete) {
5378 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5379 &s->blame.thread_args);
5381 return got_error_set_errno(errcode, "pthread_create");
5383 halfdelay(1); /* fast refresh while annotating */
5386 if (s->blame_complete)
5387 halfdelay(10); /* disable fast refresh */
5389 err = draw_blame(view);
5395 static const struct got_error *
5396 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5398 const struct got_error *err = NULL, *thread_err = NULL;
5399 struct tog_view *diff_view;
5400 struct tog_blame_view_state *s = &view->state.blame;
5401 int eos, nscroll, begin_y = 0, begin_x = 0;
5403 eos = nscroll = view->nlines - 2;
5404 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5405 view_is_splitscreen(view->child))
5413 view->x = MAX(view->maxx - view->ncols / 3, 0);
5418 if (view->x + view->ncols / 3 < view->maxx)
5419 view->x += 2; /* move two columns right */
5425 view->x -= MIN(view->x, 2); /* move two columns back */
5434 s->selected_line = 1;
5435 s->first_displayed_line = 1;
5440 if (s->blame.nlines < eos) {
5441 s->selected_line = s->blame.nlines;
5442 s->first_displayed_line = 1;
5444 s->selected_line = eos;
5445 s->first_displayed_line = s->blame.nlines - (eos - 1);
5452 if (s->selected_line > 1)
5454 else if (s->selected_line == 1 &&
5455 s->first_displayed_line > 1)
5456 s->first_displayed_line--;
5467 if (s->first_displayed_line == 1) {
5468 if (view->count > 1)
5470 s->selected_line = MAX(1, s->selected_line - nscroll);
5474 if (s->first_displayed_line > nscroll)
5475 s->first_displayed_line -= nscroll;
5477 s->first_displayed_line = 1;
5482 if (s->selected_line < eos && s->first_displayed_line +
5483 s->selected_line <= s->blame.nlines)
5485 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5486 s->first_displayed_line++;
5492 struct got_object_id *id = NULL;
5495 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5496 s->first_displayed_line, s->selected_line);
5500 struct got_commit_object *commit, *pcommit;
5501 struct got_object_qid *pid;
5502 struct got_object_id *blob_id = NULL;
5504 err = got_object_open_as_commit(&commit,
5509 got_object_commit_get_parent_ids(commit));
5511 got_object_commit_close(commit);
5514 /* Check if path history ends here. */
5515 err = got_object_open_as_commit(&pcommit,
5519 err = got_object_id_by_path(&blob_id, s->repo,
5521 got_object_commit_close(pcommit);
5523 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5525 got_object_commit_close(commit);
5528 err = got_object_get_type(&obj_type, s->repo,
5531 /* Can't blame non-blob type objects. */
5532 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5533 got_object_commit_close(commit);
5536 err = got_object_qid_alloc(&s->blamed_commit,
5538 got_object_commit_close(commit);
5540 if (got_object_id_cmp(id,
5541 &s->blamed_commit->id) == 0)
5543 err = got_object_qid_alloc(&s->blamed_commit,
5549 thread_err = stop_blame(&s->blame);
5553 STAILQ_INSERT_HEAD(&s->blamed_commits,
5554 s->blamed_commit, entry);
5555 err = run_blame(view);
5561 struct got_object_qid *first;
5564 first = STAILQ_FIRST(&s->blamed_commits);
5565 if (!got_object_id_cmp(&first->id, s->commit_id))
5568 thread_err = stop_blame(&s->blame);
5572 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5573 got_object_qid_free(s->blamed_commit);
5575 STAILQ_FIRST(&s->blamed_commits);
5576 err = run_blame(view);
5583 struct got_object_id *id = NULL;
5584 struct got_object_qid *pid;
5585 struct got_commit_object *commit = NULL;
5588 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5589 s->first_displayed_line, s->selected_line);
5592 err = got_object_open_as_commit(&commit, s->repo, id);
5595 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5596 if (view_is_parent_view(view))
5597 view_get_split(view, &begin_y, &begin_x);
5599 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5600 if (diff_view == NULL) {
5601 got_object_commit_close(commit);
5602 err = got_error_from_errno("view_open");
5605 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5606 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5607 got_object_commit_close(commit);
5609 view_close(diff_view);
5612 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5613 err = view_init_hsplit(view, begin_y);
5619 diff_view->focussed = 1;
5620 diff_view->mode = view->mode;
5621 diff_view->nlines = view->lines - begin_y;
5622 if (view_is_parent_view(view)) {
5623 err = view_close_child(view);
5626 err = view_set_child(view, diff_view);
5629 view->focus_child = 1;
5631 *new_view = diff_view;
5644 if (s->last_displayed_line >= s->blame.nlines &&
5645 s->selected_line >= MIN(s->blame.nlines,
5646 view->nlines - 2)) {
5650 if (s->last_displayed_line >= s->blame.nlines &&
5651 s->selected_line < view->nlines - 2) {
5653 MIN(nscroll, s->last_displayed_line -
5654 s->first_displayed_line - s->selected_line + 1);
5656 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5657 s->first_displayed_line += nscroll;
5659 s->first_displayed_line =
5660 s->blame.nlines - (view->nlines - 3);
5663 if (s->selected_line > view->nlines - 2) {
5664 s->selected_line = MIN(s->blame.nlines,
5672 return thread_err ? thread_err : err;
5675 static const struct got_error *
5676 reset_blame_view(struct tog_view *view)
5678 const struct got_error *err;
5679 struct tog_blame_view_state *s = &view->state.blame;
5683 err = stop_blame(&s->blame);
5687 return run_blame(view);
5690 static const struct got_error *
5691 cmd_blame(int argc, char *argv[])
5693 const struct got_error *error;
5694 struct got_repository *repo = NULL;
5695 struct got_worktree *worktree = NULL;
5696 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5697 char *link_target = NULL;
5698 struct got_object_id *commit_id = NULL;
5699 struct got_commit_object *commit = NULL;
5700 char *commit_id_str = NULL;
5702 struct tog_view *view;
5703 int *pack_fds = NULL;
5705 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5708 commit_id_str = optarg;
5711 repo_path = realpath(optarg, NULL);
5712 if (repo_path == NULL)
5713 return got_error_from_errno2("realpath",
5728 error = got_repo_pack_fds_open(&pack_fds);
5732 if (repo_path == NULL) {
5733 cwd = getcwd(NULL, 0);
5735 return got_error_from_errno("getcwd");
5736 error = got_worktree_open(&worktree, cwd);
5737 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5741 strdup(got_worktree_get_repo_path(worktree));
5743 repo_path = strdup(cwd);
5744 if (repo_path == NULL) {
5745 error = got_error_from_errno("strdup");
5750 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5754 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5761 error = apply_unveil(got_repo_get_path(repo), NULL);
5765 error = tog_load_refs(repo, 0);
5769 if (commit_id_str == NULL) {
5770 struct got_reference *head_ref;
5771 error = got_ref_open(&head_ref, repo, worktree ?
5772 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5775 error = got_ref_resolve(&commit_id, repo, head_ref);
5776 got_ref_close(head_ref);
5778 error = got_repo_match_object_id(&commit_id, NULL,
5779 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5784 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5786 error = got_error_from_errno("view_open");
5790 error = got_object_open_as_commit(&commit, repo, commit_id);
5794 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5799 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5804 /* Release work tree lock. */
5805 got_worktree_close(worktree);
5808 error = view_loop(view);
5816 got_object_commit_close(commit);
5818 got_worktree_close(worktree);
5820 const struct got_error *close_err = got_repo_close(repo);
5825 const struct got_error *pack_err =
5826 got_repo_pack_fds_close(pack_fds);
5834 static const struct got_error *
5835 draw_tree_entries(struct tog_view *view, const char *parent_path)
5837 struct tog_tree_view_state *s = &view->state.tree;
5838 const struct got_error *err = NULL;
5839 struct got_tree_entry *te;
5841 struct tog_color *tc;
5842 int width, n, i, nentries;
5843 int limit = view->nlines;
5846 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5847 view_is_splitscreen(view->child))
5848 --limit; /* border */
5850 werase(view->window);
5855 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5859 if (view_needs_focus_indication(view))
5860 wstandout(view->window);
5861 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5863 wattr_on(view->window,
5864 COLOR_PAIR(tc->colorpair), NULL);
5865 waddwstr(view->window, wline);
5867 wattr_off(view->window,
5868 COLOR_PAIR(tc->colorpair), NULL);
5869 if (view_needs_focus_indication(view))
5870 wstandend(view->window);
5873 if (width < view->ncols - 1)
5874 waddch(view->window, '\n');
5877 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5881 waddwstr(view->window, wline);
5884 if (width < view->ncols - 1)
5885 waddch(view->window, '\n');
5888 waddch(view->window, '\n');
5892 if (s->first_displayed_entry == NULL) {
5893 te = got_object_tree_get_first_entry(s->tree);
5894 if (s->selected == 0) {
5896 wstandout(view->window);
5897 s->selected_entry = NULL;
5899 waddstr(view->window, " ..\n"); /* parent directory */
5900 if (s->selected == 0 && view->focussed)
5901 wstandend(view->window);
5908 te = s->first_displayed_entry;
5911 nentries = got_object_tree_get_nentries(s->tree);
5912 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5913 char *line = NULL, *id_str = NULL, *link_target = NULL;
5914 const char *modestr = "";
5917 te = got_object_tree_get_entry(s->tree, i);
5918 mode = got_tree_entry_get_mode(te);
5921 err = got_object_id_str(&id_str,
5922 got_tree_entry_get_id(te));
5924 return got_error_from_errno(
5925 "got_object_id_str");
5927 if (got_object_tree_entry_is_submodule(te))
5929 else if (S_ISLNK(mode)) {
5932 err = got_tree_entry_get_symlink_target(&link_target,
5938 for (i = 0; i < strlen(link_target); i++) {
5939 if (!isprint((unsigned char)link_target[i]))
5940 link_target[i] = '?';
5944 else if (S_ISDIR(mode))
5946 else if (mode & S_IXUSR)
5948 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5949 got_tree_entry_get_name(te), modestr,
5950 link_target ? " -> ": "",
5951 link_target ? link_target : "") == -1) {
5954 return got_error_from_errno("asprintf");
5958 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5964 if (n == s->selected) {
5966 wstandout(view->window);
5967 s->selected_entry = te;
5969 tc = match_color(&s->colors, line);
5971 wattr_on(view->window,
5972 COLOR_PAIR(tc->colorpair), NULL);
5973 waddwstr(view->window, wline);
5975 wattr_off(view->window,
5976 COLOR_PAIR(tc->colorpair), NULL);
5977 if (width < view->ncols - 1)
5978 waddch(view->window, '\n');
5979 if (n == s->selected && view->focussed)
5980 wstandend(view->window);
5986 s->last_displayed_entry = te;
5995 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5997 struct got_tree_entry *te;
5998 int isroot = s->tree == s->root;
6001 if (s->first_displayed_entry == NULL)
6004 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6005 while (i++ < maxscroll) {
6008 s->first_displayed_entry = NULL;
6011 s->first_displayed_entry = te;
6012 te = got_tree_entry_get_prev(s->tree, te);
6016 static const struct got_error *
6017 tree_scroll_down(struct tog_view *view, int maxscroll)
6019 struct tog_tree_view_state *s = &view->state.tree;
6020 struct got_tree_entry *next, *last;
6023 if (s->first_displayed_entry)
6024 next = got_tree_entry_get_next(s->tree,
6025 s->first_displayed_entry);
6027 next = got_object_tree_get_first_entry(s->tree);
6029 last = s->last_displayed_entry;
6030 while (next && n++ < maxscroll) {
6032 last = got_tree_entry_get_next(s->tree, last);
6033 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6034 s->first_displayed_entry = next;
6035 next = got_tree_entry_get_next(s->tree, next);
6042 static const struct got_error *
6043 tree_entry_path(char **path, struct tog_parent_trees *parents,
6044 struct got_tree_entry *te)
6046 const struct got_error *err = NULL;
6047 struct tog_parent_tree *pt;
6048 size_t len = 2; /* for leading slash and NUL */
6050 TAILQ_FOREACH(pt, parents, entry)
6051 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6054 len += strlen(got_tree_entry_get_name(te));
6056 *path = calloc(1, len);
6058 return got_error_from_errno("calloc");
6061 pt = TAILQ_LAST(parents, tog_parent_trees);
6063 const char *name = got_tree_entry_get_name(pt->selected_entry);
6064 if (strlcat(*path, name, len) >= len) {
6065 err = got_error(GOT_ERR_NO_SPACE);
6068 if (strlcat(*path, "/", len) >= len) {
6069 err = got_error(GOT_ERR_NO_SPACE);
6072 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6075 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6076 err = got_error(GOT_ERR_NO_SPACE);
6088 static const struct got_error *
6089 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6090 struct got_tree_entry *te, struct tog_parent_trees *parents,
6091 struct got_object_id *commit_id, struct got_repository *repo)
6093 const struct got_error *err = NULL;
6095 struct tog_view *blame_view;
6099 err = tree_entry_path(&path, parents, te);
6103 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6104 if (blame_view == NULL) {
6105 err = got_error_from_errno("view_open");
6109 err = open_blame_view(blame_view, path, commit_id, repo);
6111 if (err->code == GOT_ERR_CANCELLED)
6113 view_close(blame_view);
6115 *new_view = blame_view;
6121 static const struct got_error *
6122 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6123 struct tog_tree_view_state *s)
6125 struct tog_view *log_view;
6126 const struct got_error *err = NULL;
6131 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6132 if (log_view == NULL)
6133 return got_error_from_errno("view_open");
6135 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6139 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6142 view_close(log_view);
6144 *new_view = log_view;
6149 static const struct got_error *
6150 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6151 const char *head_ref_name, struct got_repository *repo)
6153 const struct got_error *err = NULL;
6154 char *commit_id_str = NULL;
6155 struct tog_tree_view_state *s = &view->state.tree;
6156 struct got_commit_object *commit = NULL;
6158 TAILQ_INIT(&s->parents);
6159 STAILQ_INIT(&s->colors);
6161 s->commit_id = got_object_id_dup(commit_id);
6162 if (s->commit_id == NULL)
6163 return got_error_from_errno("got_object_id_dup");
6165 err = got_object_open_as_commit(&commit, repo, commit_id);
6170 * The root is opened here and will be closed when the view is closed.
6171 * Any visited subtrees and their path-wise parents are opened and
6174 err = got_object_open_as_tree(&s->root, repo,
6175 got_object_commit_get_tree_id(commit));
6180 err = got_object_id_str(&commit_id_str, commit_id);
6184 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6185 err = got_error_from_errno("asprintf");
6189 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6190 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6191 if (head_ref_name) {
6192 s->head_ref_name = strdup(head_ref_name);
6193 if (s->head_ref_name == NULL) {
6194 err = got_error_from_errno("strdup");
6200 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6201 err = add_color(&s->colors, "\\$$",
6202 TOG_COLOR_TREE_SUBMODULE,
6203 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6206 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6207 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6210 err = add_color(&s->colors, "/$",
6211 TOG_COLOR_TREE_DIRECTORY,
6212 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6216 err = add_color(&s->colors, "\\*$",
6217 TOG_COLOR_TREE_EXECUTABLE,
6218 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6222 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6223 get_color_value("TOG_COLOR_COMMIT"));
6228 view->show = show_tree_view;
6229 view->input = input_tree_view;
6230 view->close = close_tree_view;
6231 view->search_start = search_start_tree_view;
6232 view->search_next = search_next_tree_view;
6234 free(commit_id_str);
6236 got_object_commit_close(commit);
6238 close_tree_view(view);
6242 static const struct got_error *
6243 close_tree_view(struct tog_view *view)
6245 struct tog_tree_view_state *s = &view->state.tree;
6247 free_colors(&s->colors);
6248 free(s->tree_label);
6249 s->tree_label = NULL;
6251 s->commit_id = NULL;
6252 free(s->head_ref_name);
6253 s->head_ref_name = NULL;
6254 while (!TAILQ_EMPTY(&s->parents)) {
6255 struct tog_parent_tree *parent;
6256 parent = TAILQ_FIRST(&s->parents);
6257 TAILQ_REMOVE(&s->parents, parent, entry);
6258 if (parent->tree != s->root)
6259 got_object_tree_close(parent->tree);
6263 if (s->tree != NULL && s->tree != s->root)
6264 got_object_tree_close(s->tree);
6266 got_object_tree_close(s->root);
6270 static const struct got_error *
6271 search_start_tree_view(struct tog_view *view)
6273 struct tog_tree_view_state *s = &view->state.tree;
6275 s->matched_entry = NULL;
6280 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6282 regmatch_t regmatch;
6284 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6288 static const struct got_error *
6289 search_next_tree_view(struct tog_view *view)
6291 struct tog_tree_view_state *s = &view->state.tree;
6292 struct got_tree_entry *te = NULL;
6294 if (!view->searching) {
6295 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6299 if (s->matched_entry) {
6300 if (view->searching == TOG_SEARCH_FORWARD) {
6301 if (s->selected_entry)
6302 te = got_tree_entry_get_next(s->tree,
6305 te = got_object_tree_get_first_entry(s->tree);
6307 if (s->selected_entry == NULL)
6308 te = got_object_tree_get_last_entry(s->tree);
6310 te = got_tree_entry_get_prev(s->tree,
6314 if (s->selected_entry)
6315 te = s->selected_entry;
6316 else if (view->searching == TOG_SEARCH_FORWARD)
6317 te = got_object_tree_get_first_entry(s->tree);
6319 te = got_object_tree_get_last_entry(s->tree);
6324 if (s->matched_entry == NULL) {
6325 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6328 if (view->searching == TOG_SEARCH_FORWARD)
6329 te = got_object_tree_get_first_entry(s->tree);
6331 te = got_object_tree_get_last_entry(s->tree);
6334 if (match_tree_entry(te, &view->regex)) {
6335 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6336 s->matched_entry = te;
6340 if (view->searching == TOG_SEARCH_FORWARD)
6341 te = got_tree_entry_get_next(s->tree, te);
6343 te = got_tree_entry_get_prev(s->tree, te);
6346 if (s->matched_entry) {
6347 s->first_displayed_entry = s->matched_entry;
6354 static const struct got_error *
6355 show_tree_view(struct tog_view *view)
6357 const struct got_error *err = NULL;
6358 struct tog_tree_view_state *s = &view->state.tree;
6361 err = tree_entry_path(&parent_path, &s->parents, NULL);
6365 err = draw_tree_entries(view, parent_path);
6372 static const struct got_error *
6373 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6375 const struct got_error *err = NULL;
6376 struct tog_tree_view_state *s = &view->state.tree;
6377 struct tog_view *log_view, *ref_view;
6378 struct got_tree_entry *te;
6379 int begin_x = 0, n, nscroll = view->nlines - 3;
6383 s->show_ids = !s->show_ids;
6388 if (!s->selected_entry)
6390 if (view_is_parent_view(view))
6391 begin_x = view_split_begin_x(view->begin_x);
6392 err = log_selected_tree_entry(&log_view, begin_x, s);
6394 log_view->focussed = 1;
6395 if (view_is_parent_view(view)) {
6396 err = view_close_child(view);
6399 err = view_set_child(view, log_view);
6402 view->focus_child = 1;
6404 *new_view = log_view;
6408 if (view_is_parent_view(view))
6409 begin_x = view_split_begin_x(view->begin_x);
6410 ref_view = view_open(view->nlines, view->ncols,
6411 view->begin_y, begin_x, TOG_VIEW_REF);
6412 if (ref_view == NULL)
6413 return got_error_from_errno("view_open");
6414 err = open_ref_view(ref_view, s->repo);
6416 view_close(ref_view);
6420 ref_view->focussed = 1;
6421 if (view_is_parent_view(view)) {
6422 err = view_close_child(view);
6425 err = view_set_child(view, ref_view);
6428 view->focus_child = 1;
6430 *new_view = ref_view;
6436 if (s->tree == s->root)
6437 s->first_displayed_entry =
6438 got_object_tree_get_first_entry(s->tree);
6440 s->first_displayed_entry = NULL;
6444 int eos = view->nlines - 3;
6446 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6450 te = got_object_tree_get_last_entry(s->tree);
6451 for (n = 0; n < eos; n++) {
6453 if(s->tree != s->root) {
6454 s->first_displayed_entry = NULL;
6459 s->first_displayed_entry = te;
6460 te = got_tree_entry_get_prev(s->tree, te);
6463 s->selected = n - 1;
6469 if (s->selected > 0) {
6473 tree_scroll_up(s, 1);
6474 if (s->selected_entry == NULL ||
6475 (s->tree == s->root && s->selected_entry ==
6476 got_object_tree_get_first_entry(s->tree)))
6486 if (s->tree == s->root) {
6487 if (got_object_tree_get_first_entry(s->tree) ==
6488 s->first_displayed_entry)
6489 s->selected -= MIN(s->selected, nscroll);
6491 if (s->first_displayed_entry == NULL)
6492 s->selected -= MIN(s->selected, nscroll);
6494 tree_scroll_up(s, MAX(0, nscroll));
6495 if (s->selected_entry == NULL ||
6496 (s->tree == s->root && s->selected_entry ==
6497 got_object_tree_get_first_entry(s->tree)))
6503 if (s->selected < s->ndisplayed - 1) {
6507 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6509 /* can't scroll any further */
6513 tree_scroll_down(view, 1);
6523 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6525 /* can't scroll any further; move cursor down */
6526 if (s->selected < s->ndisplayed - 1)
6527 s->selected += MIN(nscroll,
6528 s->ndisplayed - s->selected - 1);
6533 tree_scroll_down(view, nscroll);
6538 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6539 struct tog_parent_tree *parent;
6540 /* user selected '..' */
6541 if (s->tree == s->root) {
6545 parent = TAILQ_FIRST(&s->parents);
6546 TAILQ_REMOVE(&s->parents, parent,
6548 got_object_tree_close(s->tree);
6549 s->tree = parent->tree;
6550 s->first_displayed_entry =
6551 parent->first_displayed_entry;
6553 parent->selected_entry;
6554 s->selected = parent->selected;
6555 if (s->selected > view->nlines - 3) {
6556 err = offset_selection_down(view);
6561 } else if (S_ISDIR(got_tree_entry_get_mode(
6562 s->selected_entry))) {
6563 struct got_tree_object *subtree;
6565 err = got_object_open_as_tree(&subtree, s->repo,
6566 got_tree_entry_get_id(s->selected_entry));
6569 err = tree_view_visit_subtree(s, subtree);
6571 got_object_tree_close(subtree);
6574 } else if (S_ISREG(got_tree_entry_get_mode(
6575 s->selected_entry))) {
6576 struct tog_view *blame_view;
6577 int begin_x = 0, begin_y = 0;
6579 if (view_is_parent_view(view))
6580 view_get_split(view, &begin_y, &begin_x);
6582 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6583 s->selected_entry, &s->parents,
6584 s->commit_id, s->repo);
6588 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6589 err = view_init_hsplit(view, begin_y);
6596 blame_view->focussed = 1;
6597 blame_view->mode = view->mode;
6598 blame_view->nlines = view->lines - begin_y;
6599 if (view_is_parent_view(view)) {
6600 err = view_close_child(view);
6603 err = view_set_child(view, blame_view);
6606 view->focus_child = 1;
6608 *new_view = blame_view;
6612 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6613 s->selected = view->nlines - 4;
6629 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6634 static const struct got_error *
6635 cmd_tree(int argc, char *argv[])
6637 const struct got_error *error;
6638 struct got_repository *repo = NULL;
6639 struct got_worktree *worktree = NULL;
6640 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6641 struct got_object_id *commit_id = NULL;
6642 struct got_commit_object *commit = NULL;
6643 const char *commit_id_arg = NULL;
6645 struct got_reference *ref = NULL;
6646 const char *head_ref_name = NULL;
6648 struct tog_view *view;
6649 int *pack_fds = NULL;
6651 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6654 commit_id_arg = optarg;
6657 repo_path = realpath(optarg, NULL);
6658 if (repo_path == NULL)
6659 return got_error_from_errno2("realpath",
6674 error = got_repo_pack_fds_open(&pack_fds);
6678 if (repo_path == NULL) {
6679 cwd = getcwd(NULL, 0);
6681 return got_error_from_errno("getcwd");
6682 error = got_worktree_open(&worktree, cwd);
6683 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6687 strdup(got_worktree_get_repo_path(worktree));
6689 repo_path = strdup(cwd);
6690 if (repo_path == NULL) {
6691 error = got_error_from_errno("strdup");
6696 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6700 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6707 error = apply_unveil(got_repo_get_path(repo), NULL);
6711 error = tog_load_refs(repo, 0);
6715 if (commit_id_arg == NULL) {
6716 error = got_repo_match_object_id(&commit_id, &label,
6717 worktree ? got_worktree_get_head_ref_name(worktree) :
6718 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6721 head_ref_name = label;
6723 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6725 head_ref_name = got_ref_get_name(ref);
6726 else if (error->code != GOT_ERR_NOT_REF)
6728 error = got_repo_match_object_id(&commit_id, NULL,
6729 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6734 error = got_object_open_as_commit(&commit, repo, commit_id);
6738 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6740 error = got_error_from_errno("view_open");
6743 error = open_tree_view(view, commit_id, head_ref_name, repo);
6746 if (!got_path_is_root_dir(in_repo_path)) {
6747 error = tree_view_walk_path(&view->state.tree, commit,
6754 /* Release work tree lock. */
6755 got_worktree_close(worktree);
6758 error = view_loop(view);
6767 const struct got_error *close_err = got_repo_close(repo);
6772 const struct got_error *pack_err =
6773 got_repo_pack_fds_close(pack_fds);
6781 static const struct got_error *
6782 ref_view_load_refs(struct tog_ref_view_state *s)
6784 struct got_reflist_entry *sre;
6785 struct tog_reflist_entry *re;
6788 TAILQ_FOREACH(sre, &tog_refs, entry) {
6789 if (strncmp(got_ref_get_name(sre->ref),
6790 "refs/got/", 9) == 0 &&
6791 strncmp(got_ref_get_name(sre->ref),
6792 "refs/got/backup/", 16) != 0)
6795 re = malloc(sizeof(*re));
6797 return got_error_from_errno("malloc");
6799 re->ref = got_ref_dup(sre->ref);
6800 if (re->ref == NULL)
6801 return got_error_from_errno("got_ref_dup");
6802 re->idx = s->nrefs++;
6803 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6806 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6811 ref_view_free_refs(struct tog_ref_view_state *s)
6813 struct tog_reflist_entry *re;
6815 while (!TAILQ_EMPTY(&s->refs)) {
6816 re = TAILQ_FIRST(&s->refs);
6817 TAILQ_REMOVE(&s->refs, re, entry);
6818 got_ref_close(re->ref);
6823 static const struct got_error *
6824 open_ref_view(struct tog_view *view, struct got_repository *repo)
6826 const struct got_error *err = NULL;
6827 struct tog_ref_view_state *s = &view->state.ref;
6829 s->selected_entry = 0;
6832 TAILQ_INIT(&s->refs);
6833 STAILQ_INIT(&s->colors);
6835 err = ref_view_load_refs(s);
6839 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6840 err = add_color(&s->colors, "^refs/heads/",
6841 TOG_COLOR_REFS_HEADS,
6842 get_color_value("TOG_COLOR_REFS_HEADS"));
6846 err = add_color(&s->colors, "^refs/tags/",
6847 TOG_COLOR_REFS_TAGS,
6848 get_color_value("TOG_COLOR_REFS_TAGS"));
6852 err = add_color(&s->colors, "^refs/remotes/",
6853 TOG_COLOR_REFS_REMOTES,
6854 get_color_value("TOG_COLOR_REFS_REMOTES"));
6858 err = add_color(&s->colors, "^refs/got/backup/",
6859 TOG_COLOR_REFS_BACKUP,
6860 get_color_value("TOG_COLOR_REFS_BACKUP"));
6865 view->show = show_ref_view;
6866 view->input = input_ref_view;
6867 view->close = close_ref_view;
6868 view->search_start = search_start_ref_view;
6869 view->search_next = search_next_ref_view;
6872 free_colors(&s->colors);
6876 static const struct got_error *
6877 close_ref_view(struct tog_view *view)
6879 struct tog_ref_view_state *s = &view->state.ref;
6881 ref_view_free_refs(s);
6882 free_colors(&s->colors);
6887 static const struct got_error *
6888 resolve_reflist_entry(struct got_object_id **commit_id,
6889 struct tog_reflist_entry *re, struct got_repository *repo)
6891 const struct got_error *err = NULL;
6892 struct got_object_id *obj_id;
6893 struct got_tag_object *tag = NULL;
6898 err = got_ref_resolve(&obj_id, repo, re->ref);
6902 err = got_object_get_type(&obj_type, repo, obj_id);
6907 case GOT_OBJ_TYPE_COMMIT:
6908 *commit_id = obj_id;
6910 case GOT_OBJ_TYPE_TAG:
6911 err = got_object_open_as_tag(&tag, repo, obj_id);
6915 err = got_object_get_type(&obj_type, repo,
6916 got_object_tag_get_object_id(tag));
6919 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6920 err = got_error(GOT_ERR_OBJ_TYPE);
6923 *commit_id = got_object_id_dup(
6924 got_object_tag_get_object_id(tag));
6925 if (*commit_id == NULL) {
6926 err = got_error_from_errno("got_object_id_dup");
6931 err = got_error(GOT_ERR_OBJ_TYPE);
6937 got_object_tag_close(tag);
6945 static const struct got_error *
6946 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6947 struct tog_reflist_entry *re, struct got_repository *repo)
6949 struct tog_view *log_view;
6950 const struct got_error *err = NULL;
6951 struct got_object_id *commit_id = NULL;
6955 err = resolve_reflist_entry(&commit_id, re, repo);
6957 if (err->code != GOT_ERR_OBJ_TYPE)
6963 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6964 if (log_view == NULL) {
6965 err = got_error_from_errno("view_open");
6969 err = open_log_view(log_view, commit_id, repo,
6970 got_ref_get_name(re->ref), "", 0);
6973 view_close(log_view);
6975 *new_view = log_view;
6981 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6983 struct tog_reflist_entry *re;
6986 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6989 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6990 while (i++ < maxscroll) {
6993 s->first_displayed_entry = re;
6994 re = TAILQ_PREV(re, tog_reflist_head, entry);
6998 static const struct got_error *
6999 ref_scroll_down(struct tog_view *view, int maxscroll)
7001 struct tog_ref_view_state *s = &view->state.ref;
7002 struct tog_reflist_entry *next, *last;
7005 if (s->first_displayed_entry)
7006 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7008 next = TAILQ_FIRST(&s->refs);
7010 last = s->last_displayed_entry;
7011 while (next && n++ < maxscroll) {
7013 last = TAILQ_NEXT(last, entry);
7014 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7015 s->first_displayed_entry = next;
7016 next = TAILQ_NEXT(next, entry);
7023 static const struct got_error *
7024 search_start_ref_view(struct tog_view *view)
7026 struct tog_ref_view_state *s = &view->state.ref;
7028 s->matched_entry = NULL;
7033 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7035 regmatch_t regmatch;
7037 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7041 static const struct got_error *
7042 search_next_ref_view(struct tog_view *view)
7044 struct tog_ref_view_state *s = &view->state.ref;
7045 struct tog_reflist_entry *re = NULL;
7047 if (!view->searching) {
7048 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7052 if (s->matched_entry) {
7053 if (view->searching == TOG_SEARCH_FORWARD) {
7054 if (s->selected_entry)
7055 re = TAILQ_NEXT(s->selected_entry, entry);
7057 re = TAILQ_PREV(s->selected_entry,
7058 tog_reflist_head, entry);
7060 if (s->selected_entry == NULL)
7061 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7063 re = TAILQ_PREV(s->selected_entry,
7064 tog_reflist_head, entry);
7067 if (s->selected_entry)
7068 re = s->selected_entry;
7069 else if (view->searching == TOG_SEARCH_FORWARD)
7070 re = TAILQ_FIRST(&s->refs);
7072 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7077 if (s->matched_entry == NULL) {
7078 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7081 if (view->searching == TOG_SEARCH_FORWARD)
7082 re = TAILQ_FIRST(&s->refs);
7084 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7087 if (match_reflist_entry(re, &view->regex)) {
7088 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7089 s->matched_entry = re;
7093 if (view->searching == TOG_SEARCH_FORWARD)
7094 re = TAILQ_NEXT(re, entry);
7096 re = TAILQ_PREV(re, tog_reflist_head, entry);
7099 if (s->matched_entry) {
7100 s->first_displayed_entry = s->matched_entry;
7107 static const struct got_error *
7108 show_ref_view(struct tog_view *view)
7110 const struct got_error *err = NULL;
7111 struct tog_ref_view_state *s = &view->state.ref;
7112 struct tog_reflist_entry *re;
7115 struct tog_color *tc;
7117 int limit = view->nlines;
7119 werase(view->window);
7122 if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7123 view_is_splitscreen(view->child))
7124 --limit; /* border */
7129 re = s->first_displayed_entry;
7131 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7133 return got_error_from_errno("asprintf");
7135 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7140 if (view_needs_focus_indication(view))
7141 wstandout(view->window);
7142 waddwstr(view->window, wline);
7143 if (view_needs_focus_indication(view))
7144 wstandend(view->window);
7149 if (width < view->ncols - 1)
7150 waddch(view->window, '\n');
7155 while (re && limit > 0) {
7157 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7160 struct got_commit_object *ci;
7161 struct got_tag_object *tag;
7162 struct got_object_id *id;
7166 err = got_ref_resolve(&id, s->repo, re->ref);
7169 err = got_object_open_as_tag(&tag, s->repo, id);
7171 if (err->code != GOT_ERR_OBJ_TYPE) {
7175 err = got_object_open_as_commit(&ci, s->repo,
7181 t = got_object_commit_get_committer_time(ci);
7182 got_object_commit_close(ci);
7184 t = got_object_tag_get_tagger_time(tag);
7185 got_object_tag_close(tag);
7188 if (gmtime_r(&t, &tm) == NULL)
7189 return got_error_from_errno("gmtime_r");
7190 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7191 return got_error(GOT_ERR_NO_SPACE);
7193 if (got_ref_is_symbolic(re->ref)) {
7194 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7195 ymd : "", got_ref_get_name(re->ref),
7196 got_ref_get_symref_target(re->ref)) == -1)
7197 return got_error_from_errno("asprintf");
7198 } else if (s->show_ids) {
7199 struct got_object_id *id;
7201 err = got_ref_resolve(&id, s->repo, re->ref);
7204 err = got_object_id_str(&id_str, id);
7209 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7210 got_ref_get_name(re->ref), id_str) == -1) {
7211 err = got_error_from_errno("asprintf");
7218 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7219 got_ref_get_name(re->ref)) == -1)
7220 return got_error_from_errno("asprintf");
7222 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7228 if (n == s->selected) {
7230 wstandout(view->window);
7231 s->selected_entry = re;
7233 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7235 wattr_on(view->window,
7236 COLOR_PAIR(tc->colorpair), NULL);
7237 waddwstr(view->window, wline);
7239 wattr_off(view->window,
7240 COLOR_PAIR(tc->colorpair), NULL);
7241 if (width < view->ncols - 1)
7242 waddch(view->window, '\n');
7243 if (n == s->selected && view->focussed)
7244 wstandend(view->window);
7250 s->last_displayed_entry = re;
7253 re = TAILQ_NEXT(re, entry);
7260 static const struct got_error *
7261 browse_ref_tree(struct tog_view **new_view, int begin_x,
7262 struct tog_reflist_entry *re, struct got_repository *repo)
7264 const struct got_error *err = NULL;
7265 struct got_object_id *commit_id = NULL;
7266 struct tog_view *tree_view;
7270 err = resolve_reflist_entry(&commit_id, re, repo);
7272 if (err->code != GOT_ERR_OBJ_TYPE)
7279 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7280 if (tree_view == NULL) {
7281 err = got_error_from_errno("view_open");
7285 err = open_tree_view(tree_view, commit_id,
7286 got_ref_get_name(re->ref), repo);
7290 *new_view = tree_view;
7295 static const struct got_error *
7296 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7298 const struct got_error *err = NULL;
7299 struct tog_ref_view_state *s = &view->state.ref;
7300 struct tog_view *log_view, *tree_view;
7301 struct tog_reflist_entry *re;
7302 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7306 s->show_ids = !s->show_ids;
7310 s->show_date = !s->show_date;
7314 s->sort_by_date = !s->sort_by_date;
7316 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7317 got_ref_cmp_by_commit_timestamp_descending :
7318 tog_ref_cmp_by_name, s->repo);
7321 got_reflist_object_id_map_free(tog_refs_idmap);
7322 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7323 &tog_refs, s->repo);
7326 ref_view_free_refs(s);
7327 err = ref_view_load_refs(s);
7332 if (!s->selected_entry)
7334 if (view_is_parent_view(view))
7335 view_get_split(view, &begin_y, &begin_x);
7337 err = log_ref_entry(&log_view, begin_y, begin_x,
7338 s->selected_entry, s->repo);
7342 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7343 err = view_init_hsplit(view, begin_y);
7349 log_view->focussed = 1;
7350 log_view->mode = view->mode;
7351 log_view->nlines = view->lines - begin_y;
7352 if (view_is_parent_view(view)) {
7353 err = view_close_child(view);
7356 err = view_set_child(view, log_view);
7359 view->focus_child = 1;
7361 *new_view = log_view;
7365 if (!s->selected_entry)
7367 if (view_is_parent_view(view))
7368 begin_x = view_split_begin_x(view->begin_x);
7369 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7371 if (err || tree_view == NULL)
7374 tree_view->focussed = 1;
7375 if (view_is_parent_view(view)) {
7376 err = view_close_child(view);
7379 err = view_set_child(view, tree_view);
7382 view->focus_child = 1;
7384 *new_view = tree_view;
7390 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7394 int eos = view->nlines - 1;
7396 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7400 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7401 for (n = 0; n < eos; n++) {
7404 s->first_displayed_entry = re;
7405 re = TAILQ_PREV(re, tog_reflist_head, entry);
7408 s->selected = n - 1;
7414 if (s->selected > 0) {
7418 ref_scroll_up(s, 1);
7419 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7429 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7430 s->selected -= MIN(nscroll, s->selected);
7431 ref_scroll_up(s, MAX(0, nscroll));
7432 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7438 if (s->selected < s->ndisplayed - 1) {
7442 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7443 /* can't scroll any further */
7447 ref_scroll_down(view, 1);
7457 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7458 /* can't scroll any further; move cursor down */
7459 if (s->selected < s->ndisplayed - 1)
7460 s->selected += MIN(nscroll,
7461 s->ndisplayed - s->selected - 1);
7462 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7463 s->selected += s->ndisplayed - s->selected - 1;
7467 ref_scroll_down(view, nscroll);
7472 err = tog_load_refs(s->repo, s->sort_by_date);
7475 ref_view_free_refs(s);
7476 err = ref_view_load_refs(s);
7479 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7480 s->selected = view->nlines - 2;
7494 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7499 static const struct got_error *
7500 cmd_ref(int argc, char *argv[])
7502 const struct got_error *error;
7503 struct got_repository *repo = NULL;
7504 struct got_worktree *worktree = NULL;
7505 char *cwd = NULL, *repo_path = NULL;
7507 struct tog_view *view;
7508 int *pack_fds = NULL;
7510 while ((ch = getopt(argc, argv, "r:")) != -1) {
7513 repo_path = realpath(optarg, NULL);
7514 if (repo_path == NULL)
7515 return got_error_from_errno2("realpath",
7530 error = got_repo_pack_fds_open(&pack_fds);
7534 if (repo_path == NULL) {
7535 cwd = getcwd(NULL, 0);
7537 return got_error_from_errno("getcwd");
7538 error = got_worktree_open(&worktree, cwd);
7539 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7543 strdup(got_worktree_get_repo_path(worktree));
7545 repo_path = strdup(cwd);
7546 if (repo_path == NULL) {
7547 error = got_error_from_errno("strdup");
7552 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7558 error = apply_unveil(got_repo_get_path(repo), NULL);
7562 error = tog_load_refs(repo, 0);
7566 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7568 error = got_error_from_errno("view_open");
7572 error = open_ref_view(view, repo);
7577 /* Release work tree lock. */
7578 got_worktree_close(worktree);
7581 error = view_loop(view);
7586 const struct got_error *close_err = got_repo_close(repo);
7591 const struct got_error *pack_err =
7592 got_repo_pack_fds_close(pack_fds);
7601 * If view was scrolled down to move the selected line into view when opening a
7602 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7605 offset_selection_up(struct tog_view *view)
7607 switch (view->type) {
7608 case TOG_VIEW_BLAME: {
7609 struct tog_blame_view_state *s = &view->state.blame;
7610 if (s->first_displayed_line == 1) {
7611 s->selected_line = MAX(s->selected_line - view->offset,
7615 if (s->first_displayed_line > view->offset)
7616 s->first_displayed_line -= view->offset;
7618 s->first_displayed_line = 1;
7619 s->selected_line += view->offset;
7623 log_scroll_up(&view->state.log, view->offset);
7624 view->state.log.selected += view->offset;
7627 ref_scroll_up(&view->state.ref, view->offset);
7628 view->state.ref.selected += view->offset;
7631 tree_scroll_up(&view->state.tree, view->offset);
7632 view->state.tree.selected += view->offset;
7642 * If the selected line is in the section of screen covered by the bottom split,
7643 * scroll down offset lines to move it into view and index its new position.
7645 static const struct got_error *
7646 offset_selection_down(struct tog_view *view)
7648 const struct got_error *err = NULL;
7649 const struct got_error *(*scrolld)(struct tog_view *, int);
7650 int *selected = NULL;
7653 switch (view->type) {
7654 case TOG_VIEW_BLAME: {
7655 struct tog_blame_view_state *s = &view->state.blame;
7658 if (s->selected_line > view->nlines - header) {
7659 offset = abs(view->nlines - s->selected_line - header);
7660 s->first_displayed_line += offset;
7661 s->selected_line -= offset;
7662 view->offset = offset;
7666 case TOG_VIEW_LOG: {
7667 struct tog_log_view_state *s = &view->state.log;
7668 scrolld = &log_scroll_down;
7670 selected = &s->selected;
7673 case TOG_VIEW_REF: {
7674 struct tog_ref_view_state *s = &view->state.ref;
7675 scrolld = &ref_scroll_down;
7677 selected = &s->selected;
7680 case TOG_VIEW_TREE: {
7681 struct tog_tree_view_state *s = &view->state.tree;
7682 scrolld = &tree_scroll_down;
7684 selected = &s->selected;
7694 if (selected && *selected > view->nlines - header) {
7695 offset = abs(view->nlines - *selected - header);
7696 view->offset = offset;
7697 if (scrolld && offset) {
7698 err = scrolld(view, offset);
7699 *selected -= offset;
7707 list_commands(FILE *fp)
7711 fprintf(fp, "commands:");
7712 for (i = 0; i < nitems(tog_commands); i++) {
7713 const struct tog_cmd *cmd = &tog_commands[i];
7714 fprintf(fp, " %s", cmd->name);
7720 usage(int hflag, int status)
7722 FILE *fp = (status == 0) ? stdout : stderr;
7724 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7727 fprintf(fp, "lazy usage: %s path\n", getprogname());
7734 make_argv(int argc, ...)
7742 argv = calloc(argc, sizeof(char *));
7745 for (i = 0; i < argc; i++) {
7746 argv[i] = strdup(va_arg(ap, char *));
7747 if (argv[i] == NULL)
7756 * Try to convert 'tog path' into a 'tog log path' command.
7757 * The user could simply have mistyped the command rather than knowingly
7758 * provided a path. So check whether argv[0] can in fact be resolved
7759 * to a path in the HEAD commit and print a special error if not.
7760 * This hack is for mpi@ <3
7762 static const struct got_error *
7763 tog_log_with_path(int argc, char *argv[])
7765 const struct got_error *error = NULL, *close_err;
7766 const struct tog_cmd *cmd = NULL;
7767 struct got_repository *repo = NULL;
7768 struct got_worktree *worktree = NULL;
7769 struct got_object_id *commit_id = NULL, *id = NULL;
7770 struct got_commit_object *commit = NULL;
7771 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7772 char *commit_id_str = NULL, **cmd_argv = NULL;
7773 int *pack_fds = NULL;
7775 cwd = getcwd(NULL, 0);
7777 return got_error_from_errno("getcwd");
7779 error = got_repo_pack_fds_open(&pack_fds);
7783 error = got_worktree_open(&worktree, cwd);
7784 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7788 repo_path = strdup(got_worktree_get_repo_path(worktree));
7790 repo_path = strdup(cwd);
7791 if (repo_path == NULL) {
7792 error = got_error_from_errno("strdup");
7796 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7800 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7805 error = tog_load_refs(repo, 0);
7808 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7809 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7810 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7815 got_worktree_close(worktree);
7819 error = got_object_open_as_commit(&commit, repo, commit_id);
7823 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7825 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7827 fprintf(stderr, "%s: '%s' is no known command or path\n",
7828 getprogname(), argv[0]);
7833 close_err = got_repo_close(repo);
7838 error = got_object_id_str(&commit_id_str, commit_id);
7842 cmd = &tog_commands[0]; /* log */
7844 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7845 error = cmd->cmd_main(argc, cmd_argv);
7848 close_err = got_repo_close(repo);
7853 got_object_commit_close(commit);
7855 got_worktree_close(worktree);
7857 const struct got_error *pack_err =
7858 got_repo_pack_fds_close(pack_fds);
7863 free(commit_id_str);
7870 for (i = 0; i < argc; i++)
7879 main(int argc, char *argv[])
7881 const struct got_error *error = NULL;
7882 const struct tog_cmd *cmd = NULL;
7883 int ch, hflag = 0, Vflag = 0;
7884 char **cmd_argv = NULL;
7885 static const struct option longopts[] = {
7886 { "version", no_argument, NULL, 'V' },
7889 char *diff_algo_str = NULL;
7891 setlocale(LC_CTYPE, "");
7893 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7913 got_version_print_str();
7918 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7926 /* Build an argument vector which runs a default command. */
7927 cmd = &tog_commands[0];
7929 cmd_argv = make_argv(argc, cmd->name);
7933 /* Did the user specify a command? */
7934 for (i = 0; i < nitems(tog_commands); i++) {
7935 if (strncmp(tog_commands[i].name, argv[0],
7936 strlen(argv[0])) == 0) {
7937 cmd = &tog_commands[i];
7943 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7944 if (diff_algo_str) {
7945 if (strcasecmp(diff_algo_str, "patience") == 0)
7946 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7947 if (strcasecmp(diff_algo_str, "myers") == 0)
7948 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
7954 /* No command specified; try log with a path */
7955 error = tog_log_with_path(argc, argv);
7960 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7967 for (i = 0; i < argc; i++)
7972 if (error && error->code != GOT_ERR_CANCELLED)
7973 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);