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.
18 #include <sys/ioctl.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
52 #include "got_opentemp.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
59 #include "got_worktree.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #define CTRL(x) ((x) & 0x1f)
74 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
83 __dead static void usage(int, int);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_ref(void);
90 static const struct got_error* cmd_log(int, char *[]);
91 static const struct got_error* cmd_diff(int, char *[]);
92 static const struct got_error* cmd_blame(int, char *[]);
93 static const struct got_error* cmd_tree(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
96 static const struct tog_cmd tog_commands[] = {
97 { "log", cmd_log, usage_log },
98 { "diff", cmd_diff, usage_diff },
99 { "blame", cmd_blame, usage_blame },
100 { "tree", cmd_tree, usage_tree },
101 { "ref", cmd_ref, usage_ref },
118 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 #define TOG_EOF_STRING "(END)"
122 struct commit_queue_entry {
123 TAILQ_ENTRY(commit_queue_entry) entry;
124 struct got_object_id *id;
125 struct got_commit_object *commit;
128 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
129 struct commit_queue {
131 struct commit_queue_head head;
135 STAILQ_ENTRY(tog_color) entry;
139 STAILQ_HEAD(tog_colors, tog_color);
141 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
142 static struct got_reflist_object_id_map *tog_refs_idmap;
143 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 static const struct got_error *
146 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
147 struct got_reference* re2)
149 const char *name1 = got_ref_get_name(re1);
150 const char *name2 = got_ref_get_name(re2);
151 int isbackup1, isbackup2;
153 /* Sort backup refs towards the bottom of the list. */
154 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
155 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
156 if (!isbackup1 && isbackup2) {
159 } else if (isbackup1 && !isbackup2) {
164 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
168 static const struct got_error *
169 tog_load_refs(struct got_repository *repo, int sort_by_date)
171 const struct got_error *err;
173 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
174 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
179 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
186 if (tog_refs_idmap) {
187 got_reflist_object_id_map_free(tog_refs_idmap);
188 tog_refs_idmap = NULL;
190 got_ref_list_free(&tog_refs);
193 static const struct got_error *
194 add_color(struct tog_colors *colors, const char *pattern,
195 int idx, short color)
197 const struct got_error *err = NULL;
198 struct tog_color *tc;
201 if (idx < 1 || idx > COLOR_PAIRS - 1)
204 init_pair(idx, color, -1);
206 tc = calloc(1, sizeof(*tc));
208 return got_error_from_errno("calloc");
209 regerr = regcomp(&tc->regex, pattern,
210 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 static char regerr_msg[512];
213 static char err_msg[512];
214 regerror(regerr, &tc->regex, regerr_msg,
216 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 err = got_error_msg(GOT_ERR_REGEX, err_msg);
223 STAILQ_INSERT_HEAD(colors, tc, entry);
228 free_colors(struct tog_colors *colors)
230 struct tog_color *tc;
232 while (!STAILQ_EMPTY(colors)) {
233 tc = STAILQ_FIRST(colors);
234 STAILQ_REMOVE_HEAD(colors, entry);
240 static struct tog_color *
241 get_color(struct tog_colors *colors, int colorpair)
243 struct tog_color *tc = NULL;
245 STAILQ_FOREACH(tc, colors, entry) {
246 if (tc->colorpair == colorpair)
254 default_color_value(const char *envvar)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
257 return COLOR_MAGENTA;
258 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
265 return COLOR_MAGENTA;
266 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
291 get_color_value(const char *envvar)
293 const char *val = getenv(envvar);
296 return default_color_value(envvar);
298 if (strcasecmp(val, "black") == 0)
300 if (strcasecmp(val, "red") == 0)
302 if (strcasecmp(val, "green") == 0)
304 if (strcasecmp(val, "yellow") == 0)
306 if (strcasecmp(val, "blue") == 0)
308 if (strcasecmp(val, "magenta") == 0)
309 return COLOR_MAGENTA;
310 if (strcasecmp(val, "cyan") == 0)
312 if (strcasecmp(val, "white") == 0)
314 if (strcasecmp(val, "default") == 0)
317 return default_color_value(envvar);
321 struct tog_diff_view_state {
322 struct got_object_id *id1, *id2;
323 const char *label1, *label2;
326 int first_displayed_line;
327 int last_displayed_line;
330 int ignore_whitespace;
332 struct got_repository *repo;
333 struct tog_colors colors;
339 /* passed from log view; may be NULL */
340 struct tog_view *log_view;
343 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 struct tog_log_thread_args {
346 pthread_cond_t need_commits;
347 pthread_cond_t commit_loaded;
350 struct got_commit_graph *graph;
351 struct commit_queue *commits;
352 const char *in_repo_path;
353 struct got_object_id *start_id;
354 struct got_repository *repo;
358 struct commit_queue_entry **first_displayed_entry;
359 struct commit_queue_entry **selected_entry;
361 int *search_next_done;
365 struct tog_log_view_state {
366 struct commit_queue commits;
367 struct commit_queue_entry *first_displayed_entry;
368 struct commit_queue_entry *last_displayed_entry;
369 struct commit_queue_entry *selected_entry;
374 struct got_repository *repo;
375 struct got_object_id *start_id;
378 struct tog_log_thread_args thread_args;
379 struct commit_queue_entry *matched_entry;
380 struct commit_queue_entry *search_entry;
381 struct tog_colors colors;
384 #define TOG_COLOR_DIFF_MINUS 1
385 #define TOG_COLOR_DIFF_PLUS 2
386 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
387 #define TOG_COLOR_DIFF_META 4
388 #define TOG_COLOR_TREE_SUBMODULE 5
389 #define TOG_COLOR_TREE_SYMLINK 6
390 #define TOG_COLOR_TREE_DIRECTORY 7
391 #define TOG_COLOR_TREE_EXECUTABLE 8
392 #define TOG_COLOR_COMMIT 9
393 #define TOG_COLOR_AUTHOR 10
394 #define TOG_COLOR_DATE 11
395 #define TOG_COLOR_REFS_HEADS 12
396 #define TOG_COLOR_REFS_TAGS 13
397 #define TOG_COLOR_REFS_REMOTES 14
398 #define TOG_COLOR_REFS_BACKUP 15
400 struct tog_blame_cb_args {
401 struct tog_blame_line *lines; /* one per line */
404 struct tog_view *view;
405 struct got_object_id *commit_id;
409 struct tog_blame_thread_args {
411 struct got_repository *repo;
412 struct tog_blame_cb_args *cb_args;
414 got_cancel_cb cancel_cb;
421 struct tog_blame_line *lines;
425 struct tog_blame_thread_args thread_args;
426 struct tog_blame_cb_args cb_args;
431 struct tog_blame_view_state {
432 int first_displayed_line;
433 int last_displayed_line;
438 struct got_object_id_queue blamed_commits;
439 struct got_object_qid *blamed_commit;
441 struct got_repository *repo;
442 struct got_object_id *commit_id;
443 struct tog_blame blame;
445 struct tog_colors colors;
448 struct tog_parent_tree {
449 TAILQ_ENTRY(tog_parent_tree) entry;
450 struct got_tree_object *tree;
451 struct got_tree_entry *first_displayed_entry;
452 struct got_tree_entry *selected_entry;
456 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
458 struct tog_tree_view_state {
460 struct got_object_id *commit_id;/* commit which this tree belongs to */
461 struct got_tree_object *root; /* the commit's root tree entry */
462 struct got_tree_object *tree; /* currently displayed (sub-)tree */
463 struct got_tree_entry *first_displayed_entry;
464 struct got_tree_entry *last_displayed_entry;
465 struct got_tree_entry *selected_entry;
466 int ndisplayed, selected, show_ids;
467 struct tog_parent_trees parents; /* parent trees of current sub-tree */
469 struct got_repository *repo;
470 struct got_tree_entry *matched_entry;
471 struct tog_colors colors;
474 struct tog_reflist_entry {
475 TAILQ_ENTRY(tog_reflist_entry) entry;
476 struct got_reference *ref;
480 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
482 struct tog_ref_view_state {
483 struct tog_reflist_head refs;
484 struct tog_reflist_entry *first_displayed_entry;
485 struct tog_reflist_entry *last_displayed_entry;
486 struct tog_reflist_entry *selected_entry;
487 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
488 struct got_repository *repo;
489 struct tog_reflist_entry *matched_entry;
490 struct tog_colors colors;
494 * We implement two types of views: parent views and child views.
496 * The 'Tab' key switches focus between a parent view and its child view.
497 * Child views are shown side-by-side to their parent view, provided
498 * there is enough screen estate.
500 * When a new view is opened from within a parent view, this new view
501 * becomes a child view of the parent view, replacing any existing child.
503 * When a new view is opened from within a child view, this new view
504 * becomes a parent view which will obscure the views below until the
505 * user quits the new parent view by typing 'q'.
507 * This list of views contains parent views only.
508 * Child views are only pointed to by their parent view.
510 TAILQ_HEAD(tog_view_list_head, tog_view);
513 TAILQ_ENTRY(tog_view) entry;
516 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
517 int resized_y, resized_x; /* begin_y/x based on user resizing */
518 int maxx, x; /* max column and current start column */
519 int lines, cols; /* copies of LINES and COLS */
520 int nscrolled, offset; /* lines scrolled and hsplit line offset */
521 int ch, count; /* current keymap and count prefix */
522 int resize; /* set when in a resize event */
523 int focussed; /* Only set on one parent or child view at a time. */
525 struct tog_view *parent;
526 struct tog_view *child;
529 * This flag is initially set on parent views when a new child view
530 * is created. It gets toggled when the 'Tab' key switches focus
531 * between parent and child.
532 * The flag indicates whether focus should be passed on to our child
533 * view if this parent view gets picked for focus after another parent
534 * view was closed. This prevents child views from losing focus in such
539 enum tog_view_mode mode;
540 /* type-specific state */
541 enum tog_view_type type;
543 struct tog_diff_view_state diff;
544 struct tog_log_view_state log;
545 struct tog_blame_view_state blame;
546 struct tog_tree_view_state tree;
547 struct tog_ref_view_state ref;
550 const struct got_error *(*show)(struct tog_view *);
551 const struct got_error *(*input)(struct tog_view **,
552 struct tog_view *, int);
553 const struct got_error *(*reset)(struct tog_view *);
554 const struct got_error *(*close)(struct tog_view *);
556 const struct got_error *(*search_start)(struct tog_view *);
557 const struct got_error *(*search_next)(struct tog_view *);
560 #define TOG_SEARCH_FORWARD 1
561 #define TOG_SEARCH_BACKWARD 2
562 int search_next_done;
563 #define TOG_SEARCH_HAVE_MORE 1
564 #define TOG_SEARCH_NO_MORE 2
565 #define TOG_SEARCH_HAVE_NONE 3
570 static const struct got_error *open_diff_view(struct tog_view *,
571 struct got_object_id *, struct got_object_id *,
572 const char *, const char *, int, int, int, struct tog_view *,
573 struct got_repository *);
574 static const struct got_error *show_diff_view(struct tog_view *);
575 static const struct got_error *input_diff_view(struct tog_view **,
576 struct tog_view *, int);
577 static const struct got_error *reset_diff_view(struct tog_view *);
578 static const struct got_error* close_diff_view(struct tog_view *);
579 static const struct got_error *search_start_diff_view(struct tog_view *);
580 static const struct got_error *search_next_diff_view(struct tog_view *);
582 static const struct got_error *open_log_view(struct tog_view *,
583 struct got_object_id *, struct got_repository *,
584 const char *, const char *, int);
585 static const struct got_error * show_log_view(struct tog_view *);
586 static const struct got_error *input_log_view(struct tog_view **,
587 struct tog_view *, int);
588 static const struct got_error *close_log_view(struct tog_view *);
589 static const struct got_error *search_start_log_view(struct tog_view *);
590 static const struct got_error *search_next_log_view(struct tog_view *);
592 static const struct got_error *open_blame_view(struct tog_view *, char *,
593 struct got_object_id *, struct got_repository *);
594 static const struct got_error *show_blame_view(struct tog_view *);
595 static const struct got_error *input_blame_view(struct tog_view **,
596 struct tog_view *, int);
597 static const struct got_error *reset_blame_view(struct tog_view *);
598 static const struct got_error *close_blame_view(struct tog_view *);
599 static const struct got_error *search_start_blame_view(struct tog_view *);
600 static const struct got_error *search_next_blame_view(struct tog_view *);
602 static const struct got_error *open_tree_view(struct tog_view *,
603 struct got_object_id *, const char *, struct got_repository *);
604 static const struct got_error *show_tree_view(struct tog_view *);
605 static const struct got_error *input_tree_view(struct tog_view **,
606 struct tog_view *, int);
607 static const struct got_error *close_tree_view(struct tog_view *);
608 static const struct got_error *search_start_tree_view(struct tog_view *);
609 static const struct got_error *search_next_tree_view(struct tog_view *);
611 static const struct got_error *open_ref_view(struct tog_view *,
612 struct got_repository *);
613 static const struct got_error *show_ref_view(struct tog_view *);
614 static const struct got_error *input_ref_view(struct tog_view **,
615 struct tog_view *, int);
616 static const struct got_error *close_ref_view(struct tog_view *);
617 static const struct got_error *search_start_ref_view(struct tog_view *);
618 static const struct got_error *search_next_ref_view(struct tog_view *);
620 static volatile sig_atomic_t tog_sigwinch_received;
621 static volatile sig_atomic_t tog_sigpipe_received;
622 static volatile sig_atomic_t tog_sigcont_received;
623 static volatile sig_atomic_t tog_sigint_received;
624 static volatile sig_atomic_t tog_sigterm_received;
627 tog_sigwinch(int signo)
629 tog_sigwinch_received = 1;
633 tog_sigpipe(int signo)
635 tog_sigpipe_received = 1;
639 tog_sigcont(int signo)
641 tog_sigcont_received = 1;
645 tog_sigint(int signo)
647 tog_sigint_received = 1;
651 tog_sigterm(int signo)
653 tog_sigterm_received = 1;
657 tog_fatal_signal_received(void)
659 return (tog_sigpipe_received ||
660 tog_sigint_received || tog_sigint_received);
663 static const struct got_error *
664 view_close(struct tog_view *view)
666 const struct got_error *err = NULL;
669 view_close(view->child);
673 err = view->close(view);
675 del_panel(view->panel);
677 delwin(view->window);
682 static struct tog_view *
683 view_open(int nlines, int ncols, int begin_y, int begin_x,
684 enum tog_view_type type)
686 struct tog_view *view = calloc(1, sizeof(*view));
694 view->nlines = nlines ? nlines : LINES - begin_y;
695 view->ncols = ncols ? ncols : COLS - begin_x;
696 view->begin_y = begin_y;
697 view->begin_x = begin_x;
698 view->window = newwin(nlines, ncols, begin_y, begin_x);
699 if (view->window == NULL) {
703 view->panel = new_panel(view->window);
704 if (view->panel == NULL ||
705 set_panel_userptr(view->panel, view) != OK) {
710 keypad(view->window, TRUE);
715 view_split_begin_x(int begin_x)
717 if (begin_x > 0 || COLS < 120)
719 return (COLS - MAX(COLS / 2, 80));
722 /* XXX Stub till we decide what to do. */
724 view_split_begin_y(int lines)
726 return lines * HSPLIT_SCALE;
729 static const struct got_error *view_resize(struct tog_view *);
731 static const struct got_error *
732 view_splitscreen(struct tog_view *view)
734 const struct got_error *err = NULL;
736 if (!view->resize && view->mode == TOG_VIEW_SPLIT_HRZN) {
737 if (view->resized_y && view->resized_y < view->lines)
738 view->begin_y = view->resized_y;
740 view->begin_y = view_split_begin_y(view->nlines);
742 } else if (!view->resize) {
743 if (view->resized_x && view->resized_x < view->cols - 1 &&
745 view->begin_x = view->resized_x;
747 view->begin_x = view_split_begin_x(0);
750 view->nlines = LINES - view->begin_y;
751 view->ncols = COLS - view->begin_x;
754 err = view_resize(view);
758 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
759 view->parent->nlines = view->begin_y;
761 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
762 return got_error_from_errno("mvwin");
767 static const struct got_error *
768 view_fullscreen(struct tog_view *view)
770 const struct got_error *err = NULL;
773 view->begin_y = view->resize ? view->begin_y : 0;
774 view->nlines = view->resize ? view->nlines : LINES;
778 err = view_resize(view);
782 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
783 return got_error_from_errno("mvwin");
789 view_is_parent_view(struct tog_view *view)
791 return view->parent == NULL;
795 view_is_splitscreen(struct tog_view *view)
797 return view->begin_x > 0 || view->begin_y > 0;
801 view_is_fullscreen(struct tog_view *view)
803 return view->nlines == LINES && view->ncols == COLS;
807 view_is_hsplit_top(struct tog_view *view)
809 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
810 view_is_splitscreen(view->child);
814 view_border(struct tog_view *view)
817 const struct tog_view *view_above;
820 return view_border(view->parent);
822 panel = panel_above(view->panel);
826 view_above = panel_userptr(panel);
827 if (view->mode == TOG_VIEW_SPLIT_HRZN)
828 mvwhline(view->window, view_above->begin_y - 1,
829 view->begin_x, got_locale_is_utf8() ?
830 ACS_HLINE : '-', view->ncols);
832 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
833 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
836 static const struct got_error *view_init_hsplit(struct tog_view *, int);
837 static const struct got_error *request_log_commits(struct tog_view *);
838 static const struct got_error *offset_selection_down(struct tog_view *);
839 static void offset_selection_up(struct tog_view *);
840 static void view_get_split(struct tog_view *, int *, int *);
842 static const struct got_error *
843 view_resize(struct tog_view *view)
845 const struct got_error *err = NULL;
846 int dif, nlines, ncols;
848 dif = LINES - view->lines; /* line difference */
850 if (view->lines > LINES)
851 nlines = view->nlines - (view->lines - LINES);
853 nlines = view->nlines + (LINES - view->lines);
854 if (view->cols > COLS)
855 ncols = view->ncols - (view->cols - COLS);
857 ncols = view->ncols + (COLS - view->cols);
860 int hs = view->child->begin_y;
862 if (!view_is_fullscreen(view))
863 view->child->begin_x = view_split_begin_x(view->begin_x);
864 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
865 view->child->begin_x == 0) {
868 view_fullscreen(view->child);
869 if (view->child->focussed)
870 show_panel(view->child->panel);
872 show_panel(view->panel);
874 ncols = view->child->begin_x;
876 view_splitscreen(view->child);
877 show_panel(view->child->panel);
880 * Request commits if terminal height was increased in a log
881 * view so we have enough commits loaded to populate the view.
883 if (view->type == TOG_VIEW_LOG && dif > 0) {
884 struct tog_log_view_state *ts = &view->state.log;
886 if (ts->commits.ncommits < ts->selected_entry->idx +
887 view->lines - ts->selected) {
888 view->nscrolled = ts->selected_entry->idx +
889 view->lines - ts->selected -
890 ts->commits.ncommits + dif;
891 err = request_log_commits(view);
898 * XXX This is ugly and needs to be moved into the above
899 * logic but "works" for now and my attempts at moving it
900 * break either 'tab' or 'F' key maps in horizontal splits.
903 err = view_splitscreen(view->child);
906 if (dif < 0) { /* top split decreased */
907 err = offset_selection_down(view);
914 show_panel(view->child->panel);
915 nlines = view->nlines;
917 } else if (view->parent == NULL)
920 if (wresize(view->window, nlines, ncols) == ERR)
921 return got_error_from_errno("wresize");
922 if (replace_panel(view->panel, view->window) == ERR)
923 return got_error_from_errno("replace_panel");
924 wclear(view->window);
926 view->nlines = nlines;
934 static const struct got_error *
935 view_resize_split(struct tog_view *view, int resize)
937 const struct got_error *err = NULL;
938 struct tog_view *v = NULL;
945 if (!v->child || !view_is_splitscreen(v->child))
948 v->resize = v->child->resize = resize; /* lock for resize event */
950 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
951 if (v->child->resized_y)
952 v->child->begin_y = v->child->resized_y;
954 v->child->begin_y -= resize;
956 v->child->begin_y += resize;
957 if (v->child->begin_y < 3) {
959 v->child->begin_y = 3;
960 } else if (v->child->begin_y > LINES - 1) {
962 v->child->begin_y = LINES - 1;
965 v->child->ncols = COLS;
966 err = view_init_hsplit(v, v->child->begin_y);
969 v->child->resized_y = v->child->begin_y;
971 if (v->child->resized_x)
972 v->child->begin_x = v->child->resized_x;
974 v->child->begin_x -= resize;
976 v->child->begin_x += resize;
977 if (v->child->begin_x < 11) {
979 v->child->begin_x = 11;
980 } else if (v->child->begin_x > COLS - 1) {
982 v->child->begin_x = COLS - 1;
984 v->child->resized_x = v->child->begin_x;
987 v->child->mode = v->mode;
988 v->child->nlines = v->lines - v->child->begin_y;
989 v->child->ncols = v->cols - v->child->begin_x;
992 err = view_fullscreen(v);
995 err = view_splitscreen(v->child);
999 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1000 err = offset_selection_down(v->child);
1005 if (v->type == TOG_VIEW_LOG)
1006 err = request_log_commits(v);
1007 else if (v->child->type == TOG_VIEW_LOG)
1008 err = request_log_commits(v->child);
1010 v->resize = v->child->resize = 0;
1016 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1018 struct tog_view *v = src->child ? src->child : src;
1020 dst->resized_x = v->resized_x;
1021 dst->resized_y = v->resized_y;
1024 static const struct got_error *
1025 view_close_child(struct tog_view *view)
1027 const struct got_error *err = NULL;
1029 if (view->child == NULL)
1032 err = view_close(view->child);
1037 static const struct got_error *
1038 view_set_child(struct tog_view *view, struct tog_view *child)
1040 const struct got_error *err = NULL;
1042 view->child = child;
1043 child->parent = view;
1045 err = view_resize(view);
1049 if (view->child->resized_x || view->child->resized_y)
1050 err = view_resize_split(view, 0);
1056 tog_resizeterm(void)
1059 struct winsize size;
1061 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1062 cols = 80; /* Default */
1066 lines = size.ws_row;
1068 resize_term(lines, cols);
1071 static const struct got_error *
1072 view_search_start(struct tog_view *view)
1074 const struct got_error *err = NULL;
1075 struct tog_view *v = view;
1079 if (view->search_started) {
1080 regfree(&view->regex);
1081 view->searching = 0;
1082 memset(&view->regmatch, 0, sizeof(view->regmatch));
1084 view->search_started = 0;
1086 if (view->nlines < 1)
1089 if (view_is_hsplit_top(view))
1092 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1093 wclrtoeol(v->window);
1095 nodelay(view->window, FALSE); /* block for search term input */
1098 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1099 wrefresh(v->window);
1102 nodelay(view->window, TRUE);
1106 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1107 err = view->search_start(view);
1109 regfree(&view->regex);
1112 view->search_started = 1;
1113 view->searching = TOG_SEARCH_FORWARD;
1114 view->search_next_done = 0;
1115 view->search_next(view);
1122 * If view is a parent or child view and is currently in a splitscreen, switch
1123 * to the alternate split. If in a hsplit and LINES < 120, don't vsplit.
1125 static const struct got_error *
1126 switch_split(struct tog_view *view)
1128 const struct got_error *err = NULL;
1129 struct tog_view *v = NULL;
1136 if (!v->child || !view_is_splitscreen(v->child))
1138 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->cols < 120)
1141 if (!v->mode || v->mode == TOG_VIEW_SPLIT_HRZN) {
1142 v->child->nscrolled = LINES - v->child->nlines;
1143 v->mode = TOG_VIEW_SPLIT_VERT;
1144 } else if (v->mode == TOG_VIEW_SPLIT_VERT)
1145 v->mode = TOG_VIEW_SPLIT_HRZN;
1147 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1148 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1149 v->child->begin_y = v->child->resized_y;
1150 if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1151 v->child->begin_x = v->child->resized_x;
1153 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1155 v->child->ncols = COLS;
1157 err = view_init_hsplit(v, v->child->begin_y);
1161 v->child->mode = v->mode;
1162 v->child->nlines = v->lines - v->child->begin_y;
1165 err = view_fullscreen(v);
1168 err = view_splitscreen(v->child);
1172 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1173 err = offset_selection_down(v->child);
1174 else if (v->mode == TOG_VIEW_SPLIT_VERT) {
1175 if (v->type == TOG_VIEW_LOG)
1176 err = request_log_commits(v);
1177 else if (v->child->type == TOG_VIEW_LOG)
1178 err = request_log_commits(v->child);
1185 * Compute view->count from numeric input. Assign total to view->count and
1186 * return first non-numeric key entered.
1189 get_compound_key(struct tog_view *view, int c)
1191 struct tog_view *v = view;
1194 if (view_is_hsplit_top(view))
1196 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1200 cbreak(); /* block for input */
1201 wmove(v->window, v->nlines - 1, 0);
1202 wclrtoeol(v->window);
1203 waddch(v->window, ':');
1206 x = getcurx(v->window);
1207 if (x != ERR && x < view->ncols) {
1208 waddch(v->window, c);
1209 wrefresh(v->window);
1213 * Don't overflow. Max valid request should be the greatest
1214 * between the longest and total lines; cap at 10 million.
1219 n = n * 10 + (c - '0');
1220 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1222 /* Massage excessive or inapplicable values at the input handler. */
1228 static const struct got_error *
1229 view_input(struct tog_view **new, int *done, struct tog_view *view,
1230 struct tog_view_list_head *views)
1232 const struct got_error *err = NULL;
1238 /* Clear "no matches" indicator. */
1239 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1240 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1241 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1245 if (view->searching && !view->search_next_done) {
1246 errcode = pthread_mutex_unlock(&tog_mutex);
1248 return got_error_set_errno(errcode,
1249 "pthread_mutex_unlock");
1251 errcode = pthread_mutex_lock(&tog_mutex);
1253 return got_error_set_errno(errcode,
1254 "pthread_mutex_lock");
1255 view->search_next(view);
1259 nodelay(view->window, FALSE);
1260 /* Allow threads to make progress while we are waiting for input. */
1261 errcode = pthread_mutex_unlock(&tog_mutex);
1263 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1264 /* If we have an unfinished count, let C-g or backspace abort. */
1265 if (view->count && --view->count) {
1267 nodelay(view->window, TRUE);
1268 ch = wgetch(view->window);
1269 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1274 ch = wgetch(view->window);
1275 if (ch >= '1' && ch <= '9')
1276 view->ch = ch = get_compound_key(view, ch);
1278 errcode = pthread_mutex_lock(&tog_mutex);
1280 return got_error_set_errno(errcode, "pthread_mutex_lock");
1281 nodelay(view->window, TRUE);
1283 if (tog_sigwinch_received || tog_sigcont_received) {
1285 tog_sigwinch_received = 0;
1286 tog_sigcont_received = 0;
1287 TAILQ_FOREACH(v, views, entry) {
1288 err = view_resize(v);
1291 err = v->input(new, v, KEY_RESIZE);
1295 err = view_resize(v->child);
1298 err = v->child->input(new, v->child,
1302 if (v->child->resized_x || v->child->resized_y) {
1303 err = view_resize_split(v, 0);
1316 view->child->focussed = 1;
1317 view->focus_child = 1;
1318 } else if (view->parent) {
1320 view->parent->focussed = 1;
1321 view->parent->focus_child = 0;
1322 if (!view_is_splitscreen(view)) {
1323 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1324 view->parent->type == TOG_VIEW_LOG) {
1325 err = request_log_commits(view->parent);
1329 offset_selection_up(view->parent);
1330 err = view_fullscreen(view->parent);
1337 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1338 if (view->parent->type == TOG_VIEW_LOG) {
1339 /* might need more commits to fill fullscreen */
1340 err = request_log_commits(view->parent);
1344 offset_selection_up(view->parent);
1346 err = view->input(new, view, ch);
1354 if (view_is_parent_view(view)) {
1355 if (view->child == NULL)
1357 if (view_is_splitscreen(view->child)) {
1359 view->child->focussed = 1;
1360 err = view_fullscreen(view->child);
1362 err = view_splitscreen(view->child);
1364 err = view_resize_split(view, 0);
1368 err = view->child->input(new, view->child,
1371 if (view_is_splitscreen(view)) {
1372 view->parent->focussed = 0;
1374 err = view_fullscreen(view);
1376 err = view_splitscreen(view);
1377 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1378 err = view_resize(view->parent);
1380 err = view_resize_split(view, 0);
1384 err = view->input(new, view, KEY_RESIZE);
1388 if (view->type == TOG_VIEW_LOG) {
1389 err = request_log_commits(view);
1394 err = offset_selection_down(view->parent);
1396 err = offset_selection_down(view);
1400 err = switch_split(view);
1403 err = view_resize_split(view, -1);
1406 err = view_resize_split(view, 1);
1412 if (view->search_start)
1413 view_search_start(view);
1415 err = view->input(new, view, ch);
1419 if (view->search_started && view->search_next) {
1420 view->searching = (ch == 'n' ?
1421 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1422 view->search_next_done = 0;
1423 view->search_next(view);
1425 err = view->input(new, view, ch);
1428 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1429 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1431 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1432 TAILQ_FOREACH(v, views, entry) {
1438 if (v->child && v->child->reset) {
1439 err = v->child->reset(v->child);
1446 err = view->input(new, view, ch);
1454 view_needs_focus_indication(struct tog_view *view)
1456 if (view_is_parent_view(view)) {
1457 if (view->child == NULL || view->child->focussed)
1459 if (!view_is_splitscreen(view->child))
1461 } else if (!view_is_splitscreen(view))
1464 return view->focussed;
1467 static const struct got_error *
1468 view_loop(struct tog_view *view)
1470 const struct got_error *err = NULL;
1471 struct tog_view_list_head views;
1472 struct tog_view *new_view;
1474 int fast_refresh = 10;
1475 int done = 0, errcode;
1477 mode = getenv("TOG_VIEW_SPLIT_MODE");
1478 if (!mode || !(*mode == 'h' || *mode == 'H'))
1479 view->mode = TOG_VIEW_SPLIT_VERT;
1481 view->mode = TOG_VIEW_SPLIT_HRZN;
1483 errcode = pthread_mutex_lock(&tog_mutex);
1485 return got_error_set_errno(errcode, "pthread_mutex_lock");
1488 TAILQ_INSERT_HEAD(&views, view, entry);
1491 err = view->show(view);
1496 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1497 /* Refresh fast during initialization, then become slower. */
1498 if (fast_refresh && fast_refresh-- == 0)
1499 halfdelay(10); /* switch to once per second */
1501 err = view_input(&new_view, &done, view, &views);
1505 struct tog_view *v, *prev = NULL;
1507 if (view_is_parent_view(view))
1508 prev = TAILQ_PREV(view, tog_view_list_head,
1510 else if (view->parent)
1511 prev = view->parent;
1514 view->parent->child = NULL;
1515 view->parent->focus_child = 0;
1516 /* Restore fullscreen line height. */
1517 view->parent->nlines = view->parent->lines;
1518 err = view_resize(view->parent);
1521 /* Make resized splits persist. */
1522 view_transfer_size(view->parent, view);
1524 TAILQ_REMOVE(&views, view, entry);
1526 err = view_close(view);
1531 TAILQ_FOREACH(v, &views, entry) {
1535 if (view == NULL && new_view == NULL) {
1536 /* No view has focus. Try to pick one. */
1539 else if (!TAILQ_EMPTY(&views)) {
1540 view = TAILQ_LAST(&views,
1541 tog_view_list_head);
1544 if (view->focus_child) {
1545 view->child->focussed = 1;
1553 struct tog_view *v, *t;
1554 /* Only allow one parent view per type. */
1555 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1556 if (v->type != new_view->type)
1558 TAILQ_REMOVE(&views, v, entry);
1559 err = view_close(v);
1564 TAILQ_INSERT_TAIL(&views, new_view, entry);
1568 if (view_is_parent_view(view)) {
1569 if (view->child && view->child->focussed)
1572 if (view->parent && view->parent->focussed)
1573 view = view->parent;
1575 show_panel(view->panel);
1576 if (view->child && view_is_splitscreen(view->child))
1577 show_panel(view->child->panel);
1578 if (view->parent && view_is_splitscreen(view)) {
1579 err = view->parent->show(view->parent);
1583 err = view->show(view);
1587 err = view->child->show(view->child);
1596 while (!TAILQ_EMPTY(&views)) {
1597 view = TAILQ_FIRST(&views);
1598 TAILQ_REMOVE(&views, view, entry);
1602 errcode = pthread_mutex_unlock(&tog_mutex);
1603 if (errcode && err == NULL)
1604 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1614 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1619 /* Create newly allocated wide-character string equivalent to a byte string. */
1620 static const struct got_error *
1621 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1624 const struct got_error *err = NULL;
1627 *wlen = mbstowcs(NULL, s, 0);
1628 if (*wlen == (size_t)-1) {
1630 if (errno != EILSEQ)
1631 return got_error_from_errno("mbstowcs");
1633 /* byte string invalid in current encoding; try to "fix" it */
1634 err = got_mbsavis(&vis, &vislen, s);
1637 *wlen = mbstowcs(NULL, vis, 0);
1638 if (*wlen == (size_t)-1) {
1639 err = got_error_from_errno("mbstowcs"); /* give up */
1644 *ws = calloc(*wlen + 1, sizeof(**ws));
1646 err = got_error_from_errno("calloc");
1650 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1651 err = got_error_from_errno("mbstowcs");
1662 static const struct got_error *
1663 expand_tab(char **ptr, const char *src)
1666 size_t len, n, idx = 0, sz = 0;
1669 n = len = strlen(src);
1670 dst = malloc(n + 1);
1672 return got_error_from_errno("malloc");
1674 while (idx < len && src[idx]) {
1675 const char c = src[idx];
1678 size_t nb = TABSIZE - sz % TABSIZE;
1681 p = realloc(dst, n + nb);
1684 return got_error_from_errno("realloc");
1689 memset(dst + sz, ' ', nb);
1692 dst[sz++] = src[idx];
1702 * Advance at most n columns from wline starting at offset off.
1703 * Return the index to the first character after the span operation.
1704 * Return the combined column width of all spanned wide character in
1708 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1710 int width, i, cols = 0;
1717 for (i = off; wline[i] != L'\0'; ++i) {
1718 if (wline[i] == L'\t')
1719 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1721 width = wcwidth(wline[i]);
1728 if (cols + width > n)
1738 * Format a line for display, ensuring that it won't overflow a width limit.
1739 * With scrolling, the width returned refers to the scrolled version of the
1740 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1742 static const struct got_error *
1743 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1744 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1746 const struct got_error *err = NULL;
1748 wchar_t *wline = NULL;
1757 err = expand_tab(&exstr, line);
1762 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1767 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1769 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1770 wline[wlen - 1] = L'\0';
1773 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1774 wline[wlen - 1] = L'\0';
1778 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1784 *scrollxp = scrollx;
1792 static const struct got_error*
1793 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1794 struct got_object_id *id, struct got_repository *repo)
1796 static const struct got_error *err = NULL;
1797 struct got_reflist_entry *re;
1803 TAILQ_FOREACH(re, refs, entry) {
1804 struct got_tag_object *tag = NULL;
1805 struct got_object_id *ref_id;
1808 name = got_ref_get_name(re->ref);
1809 if (strcmp(name, GOT_REF_HEAD) == 0)
1811 if (strncmp(name, "refs/", 5) == 0)
1813 if (strncmp(name, "got/", 4) == 0 &&
1814 strncmp(name, "got/backup/", 11) != 0)
1816 if (strncmp(name, "heads/", 6) == 0)
1818 if (strncmp(name, "remotes/", 8) == 0) {
1820 s = strstr(name, "/" GOT_REF_HEAD);
1821 if (s != NULL && s[strlen(s)] == '\0')
1824 err = got_ref_resolve(&ref_id, repo, re->ref);
1827 if (strncmp(name, "tags/", 5) == 0) {
1828 err = got_object_open_as_tag(&tag, repo, ref_id);
1830 if (err->code != GOT_ERR_OBJ_TYPE) {
1834 /* Ref points at something other than a tag. */
1839 cmp = got_object_id_cmp(tag ?
1840 got_object_tag_get_object_id(tag) : ref_id, id);
1843 got_object_tag_close(tag);
1847 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1848 s ? ", " : "", name) == -1) {
1849 err = got_error_from_errno("asprintf");
1860 static const struct got_error *
1861 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1866 smallerthan = strchr(author, '<');
1867 if (smallerthan && smallerthan[1] != '\0')
1868 author = smallerthan + 1;
1869 author[strcspn(author, "@>")] = '\0';
1870 return format_line(wauthor, author_width, NULL, author, 0, limit,
1874 static const struct got_error *
1875 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1876 struct got_object_id *id, const size_t date_display_cols,
1877 int author_display_cols)
1879 struct tog_log_view_state *s = &view->state.log;
1880 const struct got_error *err = NULL;
1881 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1882 char *logmsg0 = NULL, *logmsg = NULL;
1883 char *author = NULL;
1884 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1885 int author_width, logmsg_width;
1886 char *newline, *line = NULL;
1887 int col, limit, scrollx;
1888 const int avail = view->ncols;
1890 time_t committer_time;
1891 struct tog_color *tc;
1893 committer_time = got_object_commit_get_committer_time(commit);
1894 if (gmtime_r(&committer_time, &tm) == NULL)
1895 return got_error_from_errno("gmtime_r");
1896 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1897 return got_error(GOT_ERR_NO_SPACE);
1899 if (avail <= date_display_cols)
1900 limit = MIN(sizeof(datebuf) - 1, avail);
1902 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1903 tc = get_color(&s->colors, TOG_COLOR_DATE);
1905 wattr_on(view->window,
1906 COLOR_PAIR(tc->colorpair), NULL);
1907 waddnstr(view->window, datebuf, limit);
1909 wattr_off(view->window,
1910 COLOR_PAIR(tc->colorpair), NULL);
1917 err = got_object_id_str(&id_str, id);
1920 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1922 wattr_on(view->window,
1923 COLOR_PAIR(tc->colorpair), NULL);
1924 wprintw(view->window, "%.8s ", id_str);
1926 wattr_off(view->window,
1927 COLOR_PAIR(tc->colorpair), NULL);
1934 author = strdup(got_object_commit_get_author(commit));
1935 if (author == NULL) {
1936 err = got_error_from_errno("strdup");
1939 err = format_author(&wauthor, &author_width, author, avail - col, col);
1942 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1944 wattr_on(view->window,
1945 COLOR_PAIR(tc->colorpair), NULL);
1946 waddwstr(view->window, wauthor);
1948 wattr_off(view->window,
1949 COLOR_PAIR(tc->colorpair), NULL);
1950 col += author_width;
1951 while (col < avail && author_width < author_display_cols + 2) {
1952 waddch(view->window, ' ');
1959 err = got_object_commit_get_logmsg(&logmsg0, commit);
1963 while (*logmsg == '\n')
1965 newline = strchr(logmsg, '\n');
1968 limit = avail - col;
1969 if (view->child && !view_is_hsplit_top(view) && limit > 0)
1970 limit--; /* for the border */
1971 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1975 waddwstr(view->window, &wlogmsg[scrollx]);
1976 col += MAX(logmsg_width, 0);
1977 while (col < avail) {
1978 waddch(view->window, ' ');
1990 static struct commit_queue_entry *
1991 alloc_commit_queue_entry(struct got_commit_object *commit,
1992 struct got_object_id *id)
1994 struct commit_queue_entry *entry;
1996 entry = calloc(1, sizeof(*entry));
2001 entry->commit = commit;
2006 pop_commit(struct commit_queue *commits)
2008 struct commit_queue_entry *entry;
2010 entry = TAILQ_FIRST(&commits->head);
2011 TAILQ_REMOVE(&commits->head, entry, entry);
2012 got_object_commit_close(entry->commit);
2013 commits->ncommits--;
2014 /* Don't free entry->id! It is owned by the commit graph. */
2019 free_commits(struct commit_queue *commits)
2021 while (!TAILQ_EMPTY(&commits->head))
2022 pop_commit(commits);
2025 static const struct got_error *
2026 match_commit(int *have_match, struct got_object_id *id,
2027 struct got_commit_object *commit, regex_t *regex)
2029 const struct got_error *err = NULL;
2030 regmatch_t regmatch;
2031 char *id_str = NULL, *logmsg = NULL;
2035 err = got_object_id_str(&id_str, id);
2039 err = got_object_commit_get_logmsg(&logmsg, commit);
2043 if (regexec(regex, got_object_commit_get_author(commit), 1,
2044 ®match, 0) == 0 ||
2045 regexec(regex, got_object_commit_get_committer(commit), 1,
2046 ®match, 0) == 0 ||
2047 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2048 regexec(regex, logmsg, 1, ®match, 0) == 0)
2056 static const struct got_error *
2057 queue_commits(struct tog_log_thread_args *a)
2059 const struct got_error *err = NULL;
2062 * We keep all commits open throughout the lifetime of the log
2063 * view in order to avoid having to re-fetch commits from disk
2064 * while updating the display.
2067 struct got_object_id *id;
2068 struct got_commit_object *commit;
2069 struct commit_queue_entry *entry;
2072 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2074 if (err || id == NULL)
2077 err = got_object_open_as_commit(&commit, a->repo, id);
2080 entry = alloc_commit_queue_entry(commit, id);
2081 if (entry == NULL) {
2082 err = got_error_from_errno("alloc_commit_queue_entry");
2086 errcode = pthread_mutex_lock(&tog_mutex);
2088 err = got_error_set_errno(errcode,
2089 "pthread_mutex_lock");
2093 entry->idx = a->commits->ncommits;
2094 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2095 a->commits->ncommits++;
2097 if (*a->searching == TOG_SEARCH_FORWARD &&
2098 !*a->search_next_done) {
2100 err = match_commit(&have_match, id, commit, a->regex);
2104 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2107 errcode = pthread_mutex_unlock(&tog_mutex);
2108 if (errcode && err == NULL)
2109 err = got_error_set_errno(errcode,
2110 "pthread_mutex_unlock");
2113 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2119 select_commit(struct tog_log_view_state *s)
2121 struct commit_queue_entry *entry;
2124 entry = s->first_displayed_entry;
2126 if (ncommits == s->selected) {
2127 s->selected_entry = entry;
2130 entry = TAILQ_NEXT(entry, entry);
2135 static const struct got_error *
2136 draw_commits(struct tog_view *view)
2138 const struct got_error *err = NULL;
2139 struct tog_log_view_state *s = &view->state.log;
2140 struct commit_queue_entry *entry = s->selected_entry;
2141 const int limit = view->nlines;
2143 int ncommits, author_cols = 4;
2144 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2145 char *refs_str = NULL;
2147 struct tog_color *tc;
2148 static const size_t date_display_cols = 12;
2150 if (s->selected_entry &&
2151 !(view->searching && view->search_next_done == 0)) {
2152 struct got_reflist_head *refs;
2153 err = got_object_id_str(&id_str, s->selected_entry->id);
2156 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2157 s->selected_entry->id);
2159 err = build_refs_str(&refs_str, refs,
2160 s->selected_entry->id, s->repo);
2166 if (s->thread_args.commits_needed == 0)
2167 halfdelay(10); /* disable fast refresh */
2169 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2170 if (asprintf(&ncommits_str, " [%d/%d] %s",
2171 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2172 (view->searching && !view->search_next_done) ?
2173 "searching..." : "loading...") == -1) {
2174 err = got_error_from_errno("asprintf");
2178 const char *search_str = NULL;
2180 if (view->searching) {
2181 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2182 search_str = "no more matches";
2183 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2184 search_str = "no matches found";
2185 else if (!view->search_next_done)
2186 search_str = "searching...";
2189 if (asprintf(&ncommits_str, " [%d/%d] %s",
2190 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2191 search_str ? search_str :
2192 (refs_str ? refs_str : "")) == -1) {
2193 err = got_error_from_errno("asprintf");
2198 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2199 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2200 "........................................",
2201 s->in_repo_path, ncommits_str) == -1) {
2202 err = got_error_from_errno("asprintf");
2206 } else if (asprintf(&header, "commit %s%s",
2207 id_str ? id_str : "........................................",
2208 ncommits_str) == -1) {
2209 err = got_error_from_errno("asprintf");
2213 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2217 werase(view->window);
2219 if (view_needs_focus_indication(view))
2220 wstandout(view->window);
2221 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2223 wattr_on(view->window,
2224 COLOR_PAIR(tc->colorpair), NULL);
2225 waddwstr(view->window, wline);
2227 wattr_off(view->window,
2228 COLOR_PAIR(tc->colorpair), NULL);
2229 while (width < view->ncols) {
2230 waddch(view->window, ' ');
2233 if (view_needs_focus_indication(view))
2234 wstandend(view->window);
2239 /* Grow author column size if necessary, and set view->maxx. */
2240 entry = s->first_displayed_entry;
2244 char *author, *eol, *msg, *msg0;
2245 wchar_t *wauthor, *wmsg;
2247 if (ncommits >= limit - 1)
2249 author = strdup(got_object_commit_get_author(entry->commit));
2250 if (author == NULL) {
2251 err = got_error_from_errno("strdup");
2254 err = format_author(&wauthor, &width, author, COLS,
2256 if (author_cols < width)
2257 author_cols = width;
2260 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2264 while (*msg == '\n')
2266 if ((eol = strchr(msg, '\n')))
2268 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2269 date_display_cols + author_cols, 0);
2272 view->maxx = MAX(view->maxx, width);
2276 entry = TAILQ_NEXT(entry, entry);
2279 entry = s->first_displayed_entry;
2280 s->last_displayed_entry = s->first_displayed_entry;
2283 if (ncommits >= limit - 1)
2285 if (ncommits == s->selected)
2286 wstandout(view->window);
2287 err = draw_commit(view, entry->commit, entry->id,
2288 date_display_cols, author_cols);
2289 if (ncommits == s->selected)
2290 wstandend(view->window);
2294 s->last_displayed_entry = entry;
2295 entry = TAILQ_NEXT(entry, entry);
2308 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2310 struct commit_queue_entry *entry;
2313 entry = TAILQ_FIRST(&s->commits.head);
2314 if (s->first_displayed_entry == entry)
2317 entry = s->first_displayed_entry;
2318 while (entry && nscrolled < maxscroll) {
2319 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2321 s->first_displayed_entry = entry;
2327 static const struct got_error *
2328 trigger_log_thread(struct tog_view *view, int wait)
2330 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2333 halfdelay(1); /* fast refresh while loading commits */
2335 while (ta->commits_needed > 0 || ta->load_all) {
2336 if (ta->log_complete)
2339 /* Wake the log thread. */
2340 errcode = pthread_cond_signal(&ta->need_commits);
2342 return got_error_set_errno(errcode,
2343 "pthread_cond_signal");
2346 * The mutex will be released while the view loop waits
2347 * in wgetch(), at which time the log thread will run.
2352 /* Display progress update in log view. */
2353 show_log_view(view);
2357 /* Wait right here while next commit is being loaded. */
2358 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2360 return got_error_set_errno(errcode,
2361 "pthread_cond_wait");
2363 /* Display progress update in log view. */
2364 show_log_view(view);
2372 static const struct got_error *
2373 request_log_commits(struct tog_view *view)
2375 struct tog_log_view_state *state = &view->state.log;
2376 const struct got_error *err = NULL;
2378 state->thread_args.commits_needed = view->nscrolled;
2379 err = trigger_log_thread(view, 1);
2380 view->nscrolled = 0;
2385 static const struct got_error *
2386 log_scroll_down(struct tog_view *view, int maxscroll)
2388 struct tog_log_view_state *s = &view->state.log;
2389 const struct got_error *err = NULL;
2390 struct commit_queue_entry *pentry;
2391 int nscrolled = 0, ncommits_needed;
2393 if (s->last_displayed_entry == NULL)
2396 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2397 if (s->commits.ncommits < ncommits_needed &&
2398 !s->thread_args.log_complete) {
2400 * Ask the log thread for required amount of commits.
2402 s->thread_args.commits_needed += maxscroll;
2403 err = trigger_log_thread(view, 1);
2409 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2410 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2413 s->last_displayed_entry = pentry ?
2414 pentry : s->last_displayed_entry;;
2416 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2419 s->first_displayed_entry = pentry;
2420 } while (++nscrolled < maxscroll);
2422 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2423 view->nscrolled += nscrolled;
2425 view->nscrolled = 0;
2430 static const struct got_error *
2431 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2432 struct got_commit_object *commit, struct got_object_id *commit_id,
2433 struct tog_view *log_view, struct got_repository *repo)
2435 const struct got_error *err;
2436 struct got_object_qid *parent_id;
2437 struct tog_view *diff_view;
2439 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2440 if (diff_view == NULL)
2441 return got_error_from_errno("view_open");
2443 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2444 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2445 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2447 *new_view = diff_view;
2451 static const struct got_error *
2452 tree_view_visit_subtree(struct tog_tree_view_state *s,
2453 struct got_tree_object *subtree)
2455 struct tog_parent_tree *parent;
2457 parent = calloc(1, sizeof(*parent));
2459 return got_error_from_errno("calloc");
2461 parent->tree = s->tree;
2462 parent->first_displayed_entry = s->first_displayed_entry;
2463 parent->selected_entry = s->selected_entry;
2464 parent->selected = s->selected;
2465 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2468 s->first_displayed_entry = NULL;
2472 static const struct got_error *
2473 tree_view_walk_path(struct tog_tree_view_state *s,
2474 struct got_commit_object *commit, const char *path)
2476 const struct got_error *err = NULL;
2477 struct got_tree_object *tree = NULL;
2479 char *slash, *subpath = NULL;
2481 /* Walk the path and open corresponding tree objects. */
2484 struct got_tree_entry *te;
2485 struct got_object_id *tree_id;
2491 /* Ensure the correct subtree entry is selected. */
2492 slash = strchr(p, '/');
2494 te_name = strdup(p);
2496 te_name = strndup(p, slash - p);
2497 if (te_name == NULL) {
2498 err = got_error_from_errno("strndup");
2501 te = got_object_tree_find_entry(s->tree, te_name);
2503 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2508 s->first_displayed_entry = s->selected_entry = te;
2510 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2511 break; /* jump to this file's entry */
2513 slash = strchr(p, '/');
2515 subpath = strndup(path, slash - path);
2517 subpath = strdup(path);
2518 if (subpath == NULL) {
2519 err = got_error_from_errno("strdup");
2523 err = got_object_id_by_path(&tree_id, s->repo, commit,
2528 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2533 err = tree_view_visit_subtree(s, tree);
2535 got_object_tree_close(tree);
2549 static const struct got_error *
2550 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2551 struct commit_queue_entry *entry, const char *path,
2552 const char *head_ref_name, struct got_repository *repo)
2554 const struct got_error *err = NULL;
2555 struct tog_tree_view_state *s;
2556 struct tog_view *tree_view;
2558 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2559 if (tree_view == NULL)
2560 return got_error_from_errno("view_open");
2562 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2565 s = &tree_view->state.tree;
2567 *new_view = tree_view;
2569 if (got_path_is_root_dir(path))
2572 return tree_view_walk_path(s, entry->commit, path);
2575 static const struct got_error *
2576 block_signals_used_by_main_thread(void)
2581 if (sigemptyset(&sigset) == -1)
2582 return got_error_from_errno("sigemptyset");
2584 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2585 if (sigaddset(&sigset, SIGWINCH) == -1)
2586 return got_error_from_errno("sigaddset");
2587 if (sigaddset(&sigset, SIGCONT) == -1)
2588 return got_error_from_errno("sigaddset");
2589 if (sigaddset(&sigset, SIGINT) == -1)
2590 return got_error_from_errno("sigaddset");
2591 if (sigaddset(&sigset, SIGTERM) == -1)
2592 return got_error_from_errno("sigaddset");
2594 /* ncurses handles SIGTSTP */
2595 if (sigaddset(&sigset, SIGTSTP) == -1)
2596 return got_error_from_errno("sigaddset");
2598 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2600 return got_error_set_errno(errcode, "pthread_sigmask");
2606 log_thread(void *arg)
2608 const struct got_error *err = NULL;
2610 struct tog_log_thread_args *a = arg;
2613 err = block_signals_used_by_main_thread();
2617 while (!done && !err && !tog_fatal_signal_received()) {
2618 err = queue_commits(a);
2620 if (err->code != GOT_ERR_ITER_COMPLETED)
2624 } else if (a->commits_needed > 0 && !a->load_all)
2625 a->commits_needed--;
2627 errcode = pthread_mutex_lock(&tog_mutex);
2629 err = got_error_set_errno(errcode,
2630 "pthread_mutex_lock");
2632 } else if (*a->quit)
2634 else if (*a->first_displayed_entry == NULL) {
2635 *a->first_displayed_entry =
2636 TAILQ_FIRST(&a->commits->head);
2637 *a->selected_entry = *a->first_displayed_entry;
2640 errcode = pthread_cond_signal(&a->commit_loaded);
2642 err = got_error_set_errno(errcode,
2643 "pthread_cond_signal");
2644 pthread_mutex_unlock(&tog_mutex);
2649 a->commits_needed = 0;
2651 if (a->commits_needed == 0 && !a->load_all) {
2652 errcode = pthread_cond_wait(&a->need_commits,
2655 err = got_error_set_errno(errcode,
2656 "pthread_cond_wait");
2662 errcode = pthread_mutex_unlock(&tog_mutex);
2663 if (errcode && err == NULL)
2664 err = got_error_set_errno(errcode,
2665 "pthread_mutex_unlock");
2667 a->log_complete = 1;
2671 static const struct got_error *
2672 stop_log_thread(struct tog_log_view_state *s)
2674 const struct got_error *err = NULL;
2679 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2681 return got_error_set_errno(errcode,
2682 "pthread_cond_signal");
2683 errcode = pthread_mutex_unlock(&tog_mutex);
2685 return got_error_set_errno(errcode,
2686 "pthread_mutex_unlock");
2687 errcode = pthread_join(s->thread, (void **)&err);
2689 return got_error_set_errno(errcode, "pthread_join");
2690 errcode = pthread_mutex_lock(&tog_mutex);
2692 return got_error_set_errno(errcode,
2693 "pthread_mutex_lock");
2694 s->thread = 0; //NULL;
2697 if (s->thread_args.repo) {
2698 err = got_repo_close(s->thread_args.repo);
2699 s->thread_args.repo = NULL;
2702 if (s->thread_args.pack_fds) {
2703 const struct got_error *pack_err =
2704 got_repo_pack_fds_close(s->thread_args.pack_fds);
2707 s->thread_args.pack_fds = NULL;
2710 if (s->thread_args.graph) {
2711 got_commit_graph_close(s->thread_args.graph);
2712 s->thread_args.graph = NULL;
2718 static const struct got_error *
2719 close_log_view(struct tog_view *view)
2721 const struct got_error *err = NULL;
2722 struct tog_log_view_state *s = &view->state.log;
2725 err = stop_log_thread(s);
2727 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2728 if (errcode && err == NULL)
2729 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2731 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2732 if (errcode && err == NULL)
2733 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2735 free_commits(&s->commits);
2736 free(s->in_repo_path);
2737 s->in_repo_path = NULL;
2740 free(s->head_ref_name);
2741 s->head_ref_name = NULL;
2745 static const struct got_error *
2746 search_start_log_view(struct tog_view *view)
2748 struct tog_log_view_state *s = &view->state.log;
2750 s->matched_entry = NULL;
2751 s->search_entry = NULL;
2755 static const struct got_error *
2756 search_next_log_view(struct tog_view *view)
2758 const struct got_error *err = NULL;
2759 struct tog_log_view_state *s = &view->state.log;
2760 struct commit_queue_entry *entry;
2762 /* Display progress update in log view. */
2763 show_log_view(view);
2767 if (s->search_entry) {
2769 errcode = pthread_mutex_unlock(&tog_mutex);
2771 return got_error_set_errno(errcode,
2772 "pthread_mutex_unlock");
2773 ch = wgetch(view->window);
2774 errcode = pthread_mutex_lock(&tog_mutex);
2776 return got_error_set_errno(errcode,
2777 "pthread_mutex_lock");
2778 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2779 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2782 if (view->searching == TOG_SEARCH_FORWARD)
2783 entry = TAILQ_NEXT(s->search_entry, entry);
2785 entry = TAILQ_PREV(s->search_entry,
2786 commit_queue_head, entry);
2787 } else if (s->matched_entry) {
2788 int matched_idx = s->matched_entry->idx;
2789 int selected_idx = s->selected_entry->idx;
2792 * If the user has moved the cursor after we hit a match,
2793 * the position from where we should continue searching
2794 * might have changed.
2796 if (view->searching == TOG_SEARCH_FORWARD) {
2797 if (matched_idx > selected_idx)
2798 entry = TAILQ_NEXT(s->selected_entry, entry);
2800 entry = TAILQ_NEXT(s->matched_entry, entry);
2802 if (matched_idx < selected_idx)
2803 entry = TAILQ_PREV(s->selected_entry,
2804 commit_queue_head, entry);
2806 entry = TAILQ_PREV(s->matched_entry,
2807 commit_queue_head, entry);
2810 entry = s->selected_entry;
2816 if (entry == NULL) {
2817 if (s->thread_args.log_complete ||
2818 view->searching == TOG_SEARCH_BACKWARD) {
2819 view->search_next_done =
2820 (s->matched_entry == NULL ?
2821 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2822 s->search_entry = NULL;
2826 * Poke the log thread for more commits and return,
2827 * allowing the main loop to make progress. Search
2828 * will resume at s->search_entry once we come back.
2830 s->thread_args.commits_needed++;
2831 return trigger_log_thread(view, 0);
2834 err = match_commit(&have_match, entry->id, entry->commit,
2839 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2840 s->matched_entry = entry;
2844 s->search_entry = entry;
2845 if (view->searching == TOG_SEARCH_FORWARD)
2846 entry = TAILQ_NEXT(entry, entry);
2848 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2851 if (s->matched_entry) {
2852 int cur = s->selected_entry->idx;
2853 while (cur < s->matched_entry->idx) {
2854 err = input_log_view(NULL, view, KEY_DOWN);
2859 while (cur > s->matched_entry->idx) {
2860 err = input_log_view(NULL, view, KEY_UP);
2867 s->search_entry = NULL;
2872 static const struct got_error *
2873 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2874 struct got_repository *repo, const char *head_ref_name,
2875 const char *in_repo_path, int log_branches)
2877 const struct got_error *err = NULL;
2878 struct tog_log_view_state *s = &view->state.log;
2879 struct got_repository *thread_repo = NULL;
2880 struct got_commit_graph *thread_graph = NULL;
2883 if (in_repo_path != s->in_repo_path) {
2884 free(s->in_repo_path);
2885 s->in_repo_path = strdup(in_repo_path);
2886 if (s->in_repo_path == NULL)
2887 return got_error_from_errno("strdup");
2890 /* The commit queue only contains commits being displayed. */
2891 TAILQ_INIT(&s->commits.head);
2892 s->commits.ncommits = 0;
2895 if (head_ref_name) {
2896 s->head_ref_name = strdup(head_ref_name);
2897 if (s->head_ref_name == NULL) {
2898 err = got_error_from_errno("strdup");
2902 s->start_id = got_object_id_dup(start_id);
2903 if (s->start_id == NULL) {
2904 err = got_error_from_errno("got_object_id_dup");
2907 s->log_branches = log_branches;
2909 STAILQ_INIT(&s->colors);
2910 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2911 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2912 get_color_value("TOG_COLOR_COMMIT"));
2915 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2916 get_color_value("TOG_COLOR_AUTHOR"));
2918 free_colors(&s->colors);
2921 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2922 get_color_value("TOG_COLOR_DATE"));
2924 free_colors(&s->colors);
2929 view->show = show_log_view;
2930 view->input = input_log_view;
2931 view->close = close_log_view;
2932 view->search_start = search_start_log_view;
2933 view->search_next = search_next_log_view;
2935 if (s->thread_args.pack_fds == NULL) {
2936 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2940 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2941 s->thread_args.pack_fds);
2944 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2948 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2949 s->repo, NULL, NULL);
2953 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2955 err = got_error_set_errno(errcode, "pthread_cond_init");
2958 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2960 err = got_error_set_errno(errcode, "pthread_cond_init");
2964 s->thread_args.commits_needed = view->nlines;
2965 s->thread_args.graph = thread_graph;
2966 s->thread_args.commits = &s->commits;
2967 s->thread_args.in_repo_path = s->in_repo_path;
2968 s->thread_args.start_id = s->start_id;
2969 s->thread_args.repo = thread_repo;
2970 s->thread_args.log_complete = 0;
2971 s->thread_args.quit = &s->quit;
2972 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2973 s->thread_args.selected_entry = &s->selected_entry;
2974 s->thread_args.searching = &view->searching;
2975 s->thread_args.search_next_done = &view->search_next_done;
2976 s->thread_args.regex = &view->regex;
2979 close_log_view(view);
2983 static const struct got_error *
2984 show_log_view(struct tog_view *view)
2986 const struct got_error *err;
2987 struct tog_log_view_state *s = &view->state.log;
2989 if (s->thread == 0) { //NULL) {
2990 int errcode = pthread_create(&s->thread, NULL, log_thread,
2993 return got_error_set_errno(errcode, "pthread_create");
2994 if (s->thread_args.commits_needed > 0) {
2995 err = trigger_log_thread(view, 1);
3001 return draw_commits(view);
3005 log_move_cursor_up(struct tog_view *view, int page, int home)
3007 struct tog_log_view_state *s = &view->state.log;
3009 if (s->selected_entry->idx == 0)
3011 if (s->first_displayed_entry == NULL)
3014 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3016 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3018 if (!page && !home && s->selected > 0)
3021 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3027 static const struct got_error *
3028 log_move_cursor_down(struct tog_view *view, int page)
3030 struct tog_log_view_state *s = &view->state.log;
3031 struct commit_queue_entry *first;
3032 const struct got_error *err = NULL;
3034 first = s->first_displayed_entry;
3035 if (first == NULL) {
3040 if (s->thread_args.log_complete &&
3041 s->selected_entry->idx >= s->commits.ncommits - 1)
3045 int eos = view->nlines - 2;
3047 if (view_is_hsplit_top(view))
3048 --eos; /* border consumes the last line */
3049 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3052 err = log_scroll_down(view, 1);
3053 } else if (s->thread_args.load_all) {
3054 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3055 s->selected += MIN(s->last_displayed_entry->idx -
3056 s->selected_entry->idx, page + 1);
3058 err = log_scroll_down(view, MIN(page,
3059 s->commits.ncommits - s->selected_entry->idx - 1));
3060 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3062 err = log_scroll_down(view, page);
3065 if (first == s->first_displayed_entry && s->selected <
3066 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3067 s->selected = MIN(s->commits.ncommits - 1, page);
3074 * We might necessarily overshoot in horizontal
3075 * splits; if so, select the last displayed commit.
3077 s->selected = MIN(s->selected,
3078 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3082 if (s->thread_args.log_complete &&
3083 s->selected_entry->idx == s->commits.ncommits - 1)
3090 view_get_split(struct tog_view *view, int *y, int *x)
3095 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3096 if (view->child && view->child->resized_y)
3097 *y = view->child->resized_y;
3099 *y = view_split_begin_y(view->lines);
3101 if (view->child && view->child->resized_x)
3102 *x = view->child->resized_x;
3104 *x = view_split_begin_x(view->begin_x);
3108 /* Split view horizontally at y and offset view->state->selected line. */
3109 static const struct got_error *
3110 view_init_hsplit(struct tog_view *view, int y)
3112 const struct got_error *err = NULL;
3116 err = view_resize(view);
3120 err = offset_selection_down(view);
3125 static const struct got_error *
3126 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3128 const struct got_error *err = NULL;
3129 struct tog_log_view_state *s = &view->state.log;
3130 struct tog_view *diff_view = NULL, *tree_view = NULL;
3131 struct tog_view *ref_view = NULL;
3132 struct commit_queue_entry *entry;
3133 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3135 if (s->thread_args.load_all) {
3136 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3137 s->thread_args.load_all = 0;
3138 else if (s->thread_args.log_complete) {
3139 err = log_move_cursor_down(view, s->commits.ncommits);
3140 s->thread_args.load_all = 0;
3145 eos = nscroll = view->nlines - 1;
3146 if (view_is_hsplit_top(view))
3157 view->x = MAX(view->maxx - view->ncols / 2, 0);
3162 if (view->x + view->ncols / 2 < view->maxx)
3163 view->x += 2; /* move two columns right */
3169 view->x -= MIN(view->x, 2); /* move two columns back */
3178 log_move_cursor_up(view, 0, 0);
3182 log_move_cursor_up(view, 0, 1);
3192 log_move_cursor_up(view, nscroll, 0);
3199 err = log_move_cursor_down(view, 0);
3203 /* We don't know yet how many commits, so we're forced to
3204 * traverse them all. */
3206 if (!s->thread_args.log_complete) {
3207 s->thread_args.load_all = 1;
3208 return trigger_log_thread(view, 0);
3212 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3213 for (n = 0; n < eos; n++) {
3216 s->first_displayed_entry = entry;
3217 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3220 s->selected = n - 1;
3232 err = log_move_cursor_down(view, nscroll);
3235 if (s->selected > view->nlines - 2)
3236 s->selected = view->nlines - 2;
3237 if (s->selected > s->commits.ncommits - 1)
3238 s->selected = s->commits.ncommits - 1;
3240 if (s->commits.ncommits < view->nlines - 1 &&
3241 !s->thread_args.log_complete) {
3242 s->thread_args.commits_needed += (view->nlines - 1) -
3243 s->commits.ncommits;
3244 err = trigger_log_thread(view, 1);
3250 if (s->selected_entry == NULL)
3253 /* get dimensions--don't split till initialisation succeeds */
3254 if (view_is_parent_view(view))
3255 view_get_split(view, &begin_y, &begin_x);
3257 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3258 s->selected_entry->commit, s->selected_entry->id,
3263 if (view_is_parent_view(view) &&
3264 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3265 err = view_init_hsplit(view, begin_y);
3271 diff_view->focussed = 1;
3272 diff_view->mode = view->mode;
3273 diff_view->nlines = view->lines - begin_y;
3275 if (view_is_parent_view(view)) {
3276 view_transfer_size(diff_view, view);
3277 err = view_close_child(view);
3280 err = view_set_child(view, diff_view);
3283 view->focus_child = 1;
3285 *new_view = diff_view;
3289 if (s->selected_entry == NULL)
3291 if (view_is_parent_view(view))
3292 view_get_split(view, &begin_y, &begin_x);
3293 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3294 s->selected_entry, s->in_repo_path, s->head_ref_name,
3298 if (view_is_parent_view(view) &&
3299 view->mode == TOG_VIEW_SPLIT_HRZN) {
3300 err = view_init_hsplit(view, begin_y);
3305 tree_view->focussed = 1;
3306 tree_view->mode = view->mode;
3307 tree_view->nlines = view->lines - begin_y;
3308 if (view_is_parent_view(view)) {
3309 view_transfer_size(tree_view, view);
3310 err = view_close_child(view);
3313 err = view_set_child(view, tree_view);
3316 view->focus_child = 1;
3318 *new_view = tree_view;
3324 if (ch == KEY_BACKSPACE &&
3325 got_path_is_root_dir(s->in_repo_path))
3327 err = stop_log_thread(s);
3330 if (ch == KEY_BACKSPACE) {
3332 err = got_path_dirname(&parent_path, s->in_repo_path);
3335 free(s->in_repo_path);
3336 s->in_repo_path = parent_path;
3337 s->thread_args.in_repo_path = s->in_repo_path;
3338 } else if (ch == CTRL('l')) {
3339 struct got_object_id *start_id;
3340 err = got_repo_match_object_id(&start_id, NULL,
3341 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3342 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3346 s->start_id = start_id;
3347 s->thread_args.start_id = s->start_id;
3349 s->log_branches = !s->log_branches;
3351 if (s->thread_args.pack_fds == NULL) {
3352 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3356 err = got_repo_open(&s->thread_args.repo,
3357 got_repo_get_path(s->repo), NULL,
3358 s->thread_args.pack_fds);
3362 err = tog_load_refs(s->repo, 0);
3365 err = got_commit_graph_open(&s->thread_args.graph,
3366 s->in_repo_path, !s->log_branches);
3369 err = got_commit_graph_iter_start(s->thread_args.graph,
3370 s->start_id, s->repo, NULL, NULL);
3373 free_commits(&s->commits);
3374 s->first_displayed_entry = NULL;
3375 s->last_displayed_entry = NULL;
3376 s->selected_entry = NULL;
3378 s->thread_args.log_complete = 0;
3380 s->thread_args.commits_needed = view->lines;
3381 s->matched_entry = NULL;
3382 s->search_entry = NULL;
3386 if (view_is_parent_view(view))
3387 view_get_split(view, &begin_y, &begin_x);
3388 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3389 if (ref_view == NULL)
3390 return got_error_from_errno("view_open");
3391 err = open_ref_view(ref_view, s->repo);
3393 view_close(ref_view);
3396 if (view_is_parent_view(view) &&
3397 view->mode == TOG_VIEW_SPLIT_HRZN) {
3398 err = view_init_hsplit(view, begin_y);
3403 ref_view->focussed = 1;
3404 ref_view->mode = view->mode;
3405 ref_view->nlines = view->lines - begin_y;
3406 if (view_is_parent_view(view)) {
3407 view_transfer_size(ref_view, view);
3408 err = view_close_child(view);
3411 err = view_set_child(view, ref_view);
3414 view->focus_child = 1;
3416 *new_view = ref_view;
3426 static const struct got_error *
3427 apply_unveil(const char *repo_path, const char *worktree_path)
3429 const struct got_error *error;
3432 if (unveil("gmon.out", "rwc") != 0)
3433 return got_error_from_errno2("unveil", "gmon.out");
3435 if (repo_path && unveil(repo_path, "r") != 0)
3436 return got_error_from_errno2("unveil", repo_path);
3438 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3439 return got_error_from_errno2("unveil", worktree_path);
3441 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3442 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3444 error = got_privsep_unveil_exec_helpers();
3448 if (unveil(NULL, NULL) != 0)
3449 return got_error_from_errno("unveil");
3458 * Override default signal handlers before starting ncurses.
3459 * This should prevent ncurses from installing its own
3460 * broken cleanup() signal handler.
3462 signal(SIGWINCH, tog_sigwinch);
3463 signal(SIGPIPE, tog_sigpipe);
3464 signal(SIGCONT, tog_sigcont);
3465 signal(SIGINT, tog_sigint);
3466 signal(SIGTERM, tog_sigterm);
3470 halfdelay(1); /* Do fast refresh while initial view is loading. */
3473 intrflush(stdscr, FALSE);
3474 keypad(stdscr, TRUE);
3476 if (getenv("TOG_COLORS") != NULL) {
3478 use_default_colors();
3482 static const struct got_error *
3483 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3484 struct got_repository *repo, struct got_worktree *worktree)
3486 const struct got_error *err = NULL;
3489 *in_repo_path = strdup("/");
3490 if (*in_repo_path == NULL)
3491 return got_error_from_errno("strdup");
3496 const char *prefix = got_worktree_get_path_prefix(worktree);
3499 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3502 if (asprintf(in_repo_path, "%s%s%s", prefix,
3503 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3505 err = got_error_from_errno("asprintf");
3506 *in_repo_path = NULL;
3510 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3515 static const struct got_error *
3516 cmd_log(int argc, char *argv[])
3518 const struct got_error *error;
3519 struct got_repository *repo = NULL;
3520 struct got_worktree *worktree = NULL;
3521 struct got_object_id *start_id = NULL;
3522 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3523 char *start_commit = NULL, *label = NULL;
3524 struct got_reference *ref = NULL;
3525 const char *head_ref_name = NULL;
3526 int ch, log_branches = 0;
3527 struct tog_view *view;
3528 int *pack_fds = NULL;
3530 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3536 start_commit = optarg;
3539 repo_path = realpath(optarg, NULL);
3540 if (repo_path == NULL)
3541 return got_error_from_errno2("realpath",
3556 error = got_repo_pack_fds_open(&pack_fds);
3560 if (repo_path == NULL) {
3561 cwd = getcwd(NULL, 0);
3563 return got_error_from_errno("getcwd");
3564 error = got_worktree_open(&worktree, cwd);
3565 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3569 strdup(got_worktree_get_repo_path(worktree));
3571 repo_path = strdup(cwd);
3572 if (repo_path == NULL) {
3573 error = got_error_from_errno("strdup");
3578 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3582 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3589 error = apply_unveil(got_repo_get_path(repo),
3590 worktree ? got_worktree_get_root_path(worktree) : NULL);
3594 /* already loaded by tog_log_with_path()? */
3595 if (TAILQ_EMPTY(&tog_refs)) {
3596 error = tog_load_refs(repo, 0);
3601 if (start_commit == NULL) {
3602 error = got_repo_match_object_id(&start_id, &label,
3603 worktree ? got_worktree_get_head_ref_name(worktree) :
3604 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3607 head_ref_name = label;
3609 error = got_ref_open(&ref, repo, start_commit, 0);
3611 head_ref_name = got_ref_get_name(ref);
3612 else if (error->code != GOT_ERR_NOT_REF)
3614 error = got_repo_match_object_id(&start_id, NULL,
3615 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3620 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3622 error = got_error_from_errno("view_open");
3625 error = open_log_view(view, start_id, repo, head_ref_name,
3626 in_repo_path, log_branches);
3630 /* Release work tree lock. */
3631 got_worktree_close(worktree);
3634 error = view_loop(view);
3644 const struct got_error *close_err = got_repo_close(repo);
3649 got_worktree_close(worktree);
3651 const struct got_error *pack_err =
3652 got_repo_pack_fds_close(pack_fds);
3664 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3665 "[-w] object1 object2\n", getprogname());
3670 match_line(const char *line, regex_t *regex, size_t nmatch,
3671 regmatch_t *regmatch)
3673 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3676 static struct tog_color *
3677 match_color(struct tog_colors *colors, const char *line)
3679 struct tog_color *tc = NULL;
3681 STAILQ_FOREACH(tc, colors, entry) {
3682 if (match_line(line, &tc->regex, 0, NULL))
3689 static const struct got_error *
3690 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3691 WINDOW *window, int skipcol, regmatch_t *regmatch)
3693 const struct got_error *err = NULL;
3695 wchar_t *wline = NULL;
3696 int rme, rms, n, width, scrollx;
3697 int width0 = 0, width1 = 0, width2 = 0;
3698 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3702 rms = regmatch->rm_so;
3703 rme = regmatch->rm_eo;
3705 err = expand_tab(&exstr, line);
3709 /* Split the line into 3 segments, according to match offsets. */
3710 seg0 = strndup(exstr, rms);
3712 err = got_error_from_errno("strndup");
3715 seg1 = strndup(exstr + rms, rme - rms);
3717 err = got_error_from_errno("strndup");
3720 seg2 = strdup(exstr + rme);
3722 err = got_error_from_errno("strndup");
3726 /* draw up to matched token if we haven't scrolled past it */
3727 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3731 n = MAX(width0 - skipcol, 0);
3734 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3735 wlimit, col_tab_align, 1);
3738 waddwstr(window, &wline[scrollx]);
3748 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3752 wlen = wcslen(wline);
3754 width = wcwidth(wline[i]);
3756 /* should not happen, tabs are expanded */
3757 err = got_error(GOT_ERR_RANGE);
3760 if (width0 + w + width > skipcol)
3765 /* draw (visible part of) matched token (if scrolled into it) */
3766 if (width1 - w > 0) {
3767 wattron(window, A_STANDOUT);
3768 waddwstr(window, &wline[i]);
3769 wattroff(window, A_STANDOUT);
3770 wlimit -= (width1 - w);
3771 *wtotal += (width1 - w);
3775 if (wlimit > 0) { /* draw rest of line */
3777 if (skipcol > width0 + width1) {
3778 err = format_line(&wline, &width2, &scrollx, seg2,
3779 skipcol - (width0 + width1), wlimit,
3783 waddwstr(window, &wline[scrollx]);
3785 err = format_line(&wline, &width2, NULL, seg2, 0,
3786 wlimit, col_tab_align, 1);
3789 waddwstr(window, wline);
3802 static const struct got_error *
3803 draw_file(struct tog_view *view, const char *header)
3805 struct tog_diff_view_state *s = &view->state.diff;
3806 regmatch_t *regmatch = &view->regmatch;
3807 const struct got_error *err;
3810 size_t linesize = 0;
3812 struct tog_color *tc;
3815 int max_lines = view->nlines;
3816 int nlines = s->nlines;
3819 line_offset = s->line_offsets[s->first_displayed_line - 1];
3820 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3821 return got_error_from_errno("fseek");
3823 werase(view->window);
3826 if (asprintf(&line, "[%d/%d] %s",
3827 s->first_displayed_line - 1 + s->selected_line, nlines,
3829 return got_error_from_errno("asprintf");
3830 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3836 if (view_needs_focus_indication(view))
3837 wstandout(view->window);
3838 waddwstr(view->window, wline);
3841 if (view_needs_focus_indication(view))
3842 wstandend(view->window);
3843 if (width <= view->ncols - 1)
3844 waddch(view->window, '\n');
3854 while (max_lines > 0 && nprinted < max_lines) {
3855 linelen = getline(&line, &linesize, s->f);
3856 if (linelen == -1) {
3862 return got_ferror(s->f, GOT_ERR_IO);
3865 /* Set view->maxx based on full line length. */
3866 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3872 view->maxx = MAX(view->maxx, width);
3876 tc = match_color(&s->colors, line);
3878 wattr_on(view->window,
3879 COLOR_PAIR(tc->colorpair), NULL);
3880 if (s->first_displayed_line + nprinted == s->matched_line &&
3881 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3882 err = add_matched_line(&width, line, view->ncols, 0,
3883 view->window, view->x, regmatch);
3890 err = format_line(&wline, &width, &skip, line,
3891 view->x, view->ncols, 0, view->x ? 1 : 0);
3896 waddwstr(view->window, &wline[skip]);
3901 wattr_off(view->window,
3902 COLOR_PAIR(tc->colorpair), NULL);
3903 if (width <= view->ncols - 1)
3904 waddch(view->window, '\n');
3909 s->last_displayed_line = s->first_displayed_line +
3912 s->last_displayed_line = s->first_displayed_line;
3917 while (nprinted < view->nlines) {
3918 waddch(view->window, '\n');
3922 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3928 wstandout(view->window);
3929 waddwstr(view->window, wline);
3932 wstandend(view->window);
3939 get_datestr(time_t *time, char *datebuf)
3941 struct tm mytm, *tm;
3944 tm = gmtime_r(time, &mytm);
3947 s = asctime_r(tm, datebuf);
3950 p = strchr(s, '\n');
3956 static const struct got_error *
3957 get_changed_paths(struct got_pathlist_head *paths,
3958 struct got_commit_object *commit, struct got_repository *repo)
3960 const struct got_error *err = NULL;
3961 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3962 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3963 struct got_object_qid *qid;
3965 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3967 struct got_commit_object *pcommit;
3968 err = got_object_open_as_commit(&pcommit, repo,
3973 tree_id1 = got_object_id_dup(
3974 got_object_commit_get_tree_id(pcommit));
3975 if (tree_id1 == NULL) {
3976 got_object_commit_close(pcommit);
3977 return got_error_from_errno("got_object_id_dup");
3979 got_object_commit_close(pcommit);
3984 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3989 tree_id2 = got_object_commit_get_tree_id(commit);
3990 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3994 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3995 got_diff_tree_collect_changed_paths, paths, 0);
3998 got_object_tree_close(tree1);
4000 got_object_tree_close(tree2);
4005 static const struct got_error *
4006 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4010 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4012 return got_error_from_errno("reallocarray");
4014 (*line_offsets)[*nlines] = off;
4019 static const struct got_error *
4020 write_commit_info(off_t **line_offsets, size_t *nlines,
4021 struct got_object_id *commit_id, struct got_reflist_head *refs,
4022 struct got_repository *repo, FILE *outfile)
4024 const struct got_error *err = NULL;
4025 char datebuf[26], *datestr;
4026 struct got_commit_object *commit;
4027 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4028 time_t committer_time;
4029 const char *author, *committer;
4030 char *refs_str = NULL;
4031 struct got_pathlist_head changed_paths;
4032 struct got_pathlist_entry *pe;
4036 TAILQ_INIT(&changed_paths);
4039 err = build_refs_str(&refs_str, refs, commit_id, repo);
4044 err = got_object_open_as_commit(&commit, repo, commit_id);
4048 err = got_object_id_str(&id_str, commit_id);
4050 err = got_error_from_errno("got_object_id_str");
4054 err = add_line_offset(line_offsets, nlines, 0);
4058 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4059 refs_str ? refs_str : "", refs_str ? ")" : "");
4061 err = got_error_from_errno("fprintf");
4065 err = add_line_offset(line_offsets, nlines, outoff);
4069 n = fprintf(outfile, "from: %s\n",
4070 got_object_commit_get_author(commit));
4072 err = got_error_from_errno("fprintf");
4076 err = add_line_offset(line_offsets, nlines, outoff);
4080 committer_time = got_object_commit_get_committer_time(commit);
4081 datestr = get_datestr(&committer_time, datebuf);
4083 n = fprintf(outfile, "date: %s UTC\n", datestr);
4085 err = got_error_from_errno("fprintf");
4089 err = add_line_offset(line_offsets, nlines, outoff);
4093 author = got_object_commit_get_author(commit);
4094 committer = got_object_commit_get_committer(commit);
4095 if (strcmp(author, committer) != 0) {
4096 n = fprintf(outfile, "via: %s\n", committer);
4098 err = got_error_from_errno("fprintf");
4102 err = add_line_offset(line_offsets, nlines, outoff);
4106 if (got_object_commit_get_nparents(commit) > 1) {
4107 const struct got_object_id_queue *parent_ids;
4108 struct got_object_qid *qid;
4110 parent_ids = got_object_commit_get_parent_ids(commit);
4111 STAILQ_FOREACH(qid, parent_ids, entry) {
4112 err = got_object_id_str(&id_str, &qid->id);
4115 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4117 err = got_error_from_errno("fprintf");
4121 err = add_line_offset(line_offsets, nlines, outoff);
4129 err = got_object_commit_get_logmsg(&logmsg, commit);
4133 while ((line = strsep(&s, "\n")) != NULL) {
4134 n = fprintf(outfile, "%s\n", line);
4136 err = got_error_from_errno("fprintf");
4140 err = add_line_offset(line_offsets, nlines, outoff);
4145 err = get_changed_paths(&changed_paths, commit, repo);
4148 TAILQ_FOREACH(pe, &changed_paths, entry) {
4149 struct got_diff_changed_path *cp = pe->data;
4150 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4152 err = got_error_from_errno("fprintf");
4156 err = add_line_offset(line_offsets, nlines, outoff);
4159 free((char *)pe->path);
4163 fputc('\n', outfile);
4165 err = add_line_offset(line_offsets, nlines, outoff);
4167 got_pathlist_free(&changed_paths);
4171 got_object_commit_close(commit);
4173 free(*line_offsets);
4174 *line_offsets = NULL;
4180 static const struct got_error *
4181 create_diff(struct tog_diff_view_state *s)
4183 const struct got_error *err = NULL;
4187 free(s->line_offsets);
4188 s->line_offsets = malloc(sizeof(off_t));
4189 if (s->line_offsets == NULL)
4190 return got_error_from_errno("malloc");
4195 err = got_error_from_errno("got_opentemp");
4198 if (s->f && fclose(s->f) == EOF) {
4199 err = got_error_from_errno("fclose");
4205 err = got_object_get_type(&obj_type, s->repo, s->id1);
4207 err = got_object_get_type(&obj_type, s->repo, s->id2);
4212 case GOT_OBJ_TYPE_BLOB:
4213 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4214 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4215 s->label1, s->label2, tog_diff_algo, s->diff_context,
4216 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4218 case GOT_OBJ_TYPE_TREE:
4219 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4220 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4221 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4222 s->force_text_diff, s->repo, s->f);
4224 case GOT_OBJ_TYPE_COMMIT: {
4225 const struct got_object_id_queue *parent_ids;
4226 struct got_object_qid *pid;
4227 struct got_commit_object *commit2;
4228 struct got_reflist_head *refs;
4230 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4233 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4234 /* Show commit info if we're diffing to a parent/root commit. */
4235 if (s->id1 == NULL) {
4236 err = write_commit_info(&s->line_offsets, &s->nlines,
4237 s->id2, refs, s->repo, s->f);
4241 parent_ids = got_object_commit_get_parent_ids(commit2);
4242 STAILQ_FOREACH(pid, parent_ids, entry) {
4243 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4244 err = write_commit_info(
4245 &s->line_offsets, &s->nlines,
4246 s->id2, refs, s->repo, s->f);
4253 got_object_commit_close(commit2);
4255 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4256 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4257 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4258 s->force_text_diff, s->repo, s->f);
4262 err = got_error(GOT_ERR_OBJ_TYPE);
4268 if (s->f && fflush(s->f) != 0 && err == NULL)
4269 err = got_error_from_errno("fflush");
4274 diff_view_indicate_progress(struct tog_view *view)
4276 mvwaddstr(view->window, 0, 0, "diffing...");
4281 static const struct got_error *
4282 search_start_diff_view(struct tog_view *view)
4284 struct tog_diff_view_state *s = &view->state.diff;
4286 s->matched_line = 0;
4290 static const struct got_error *
4291 search_next_diff_view(struct tog_view *view)
4293 struct tog_diff_view_state *s = &view->state.diff;
4294 const struct got_error *err = NULL;
4297 size_t linesize = 0;
4300 if (!view->searching) {
4301 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4305 if (s->matched_line) {
4306 if (view->searching == TOG_SEARCH_FORWARD)
4307 lineno = s->matched_line + 1;
4309 lineno = s->matched_line - 1;
4311 lineno = s->first_displayed_line;
4316 if (lineno <= 0 || lineno > s->nlines) {
4317 if (s->matched_line == 0) {
4318 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4322 if (view->searching == TOG_SEARCH_FORWARD)
4328 offset = s->line_offsets[lineno - 1];
4329 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4331 return got_error_from_errno("fseeko");
4333 linelen = getline(&line, &linesize, s->f);
4334 if (linelen != -1) {
4336 err = expand_tab(&exstr, line);
4339 if (match_line(exstr, &view->regex, 1,
4341 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4342 s->matched_line = lineno;
4348 if (view->searching == TOG_SEARCH_FORWARD)
4355 if (s->matched_line) {
4356 s->first_displayed_line = s->matched_line;
4357 s->selected_line = 1;
4363 static const struct got_error *
4364 close_diff_view(struct tog_view *view)
4366 const struct got_error *err = NULL;
4367 struct tog_diff_view_state *s = &view->state.diff;
4373 if (s->f && fclose(s->f) == EOF)
4374 err = got_error_from_errno("fclose");
4376 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4377 err = got_error_from_errno("fclose");
4379 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4380 err = got_error_from_errno("fclose");
4382 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4383 err = got_error_from_errno("close");
4385 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4386 err = got_error_from_errno("close");
4388 free_colors(&s->colors);
4389 free(s->line_offsets);
4390 s->line_offsets = NULL;
4395 static const struct got_error *
4396 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4397 struct got_object_id *id2, const char *label1, const char *label2,
4398 int diff_context, int ignore_whitespace, int force_text_diff,
4399 struct tog_view *log_view, struct got_repository *repo)
4401 const struct got_error *err;
4402 struct tog_diff_view_state *s = &view->state.diff;
4404 memset(s, 0, sizeof(*s));
4408 if (id1 != NULL && id2 != NULL) {
4410 err = got_object_get_type(&type1, repo, id1);
4413 err = got_object_get_type(&type2, repo, id2);
4418 return got_error(GOT_ERR_OBJ_TYPE);
4420 s->first_displayed_line = 1;
4421 s->last_displayed_line = view->nlines;
4422 s->selected_line = 1;
4430 s->id1 = got_object_id_dup(id1);
4432 return got_error_from_errno("got_object_id_dup");
4436 s->id2 = got_object_id_dup(id2);
4437 if (s->id2 == NULL) {
4438 err = got_error_from_errno("got_object_id_dup");
4442 s->f1 = got_opentemp();
4443 if (s->f1 == NULL) {
4444 err = got_error_from_errno("got_opentemp");
4448 s->f2 = got_opentemp();
4449 if (s->f2 == NULL) {
4450 err = got_error_from_errno("got_opentemp");
4454 s->fd1 = got_opentempfd();
4456 err = got_error_from_errno("got_opentempfd");
4460 s->fd2 = got_opentempfd();
4462 err = got_error_from_errno("got_opentempfd");
4466 s->first_displayed_line = 1;
4467 s->last_displayed_line = view->nlines;
4468 s->diff_context = diff_context;
4469 s->ignore_whitespace = ignore_whitespace;
4470 s->force_text_diff = force_text_diff;
4471 s->log_view = log_view;
4474 STAILQ_INIT(&s->colors);
4475 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4476 err = add_color(&s->colors,
4477 "^-", TOG_COLOR_DIFF_MINUS,
4478 get_color_value("TOG_COLOR_DIFF_MINUS"));
4481 err = add_color(&s->colors, "^\\+",
4482 TOG_COLOR_DIFF_PLUS,
4483 get_color_value("TOG_COLOR_DIFF_PLUS"));
4486 err = add_color(&s->colors,
4487 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4488 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4492 err = add_color(&s->colors,
4493 "^(commit [0-9a-f]|parent [0-9]|"
4494 "(blob|file|tree|commit) [-+] |"
4495 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4496 get_color_value("TOG_COLOR_DIFF_META"));
4500 err = add_color(&s->colors,
4501 "^(from|via): ", TOG_COLOR_AUTHOR,
4502 get_color_value("TOG_COLOR_AUTHOR"));
4506 err = add_color(&s->colors,
4507 "^date: ", TOG_COLOR_DATE,
4508 get_color_value("TOG_COLOR_DATE"));
4513 if (log_view && view_is_splitscreen(view))
4514 show_log_view(log_view); /* draw vborder */
4515 diff_view_indicate_progress(view);
4517 err = create_diff(s);
4519 view->show = show_diff_view;
4520 view->input = input_diff_view;
4521 view->reset = reset_diff_view;
4522 view->close = close_diff_view;
4523 view->search_start = search_start_diff_view;
4524 view->search_next = search_next_diff_view;
4527 close_diff_view(view);
4531 static const struct got_error *
4532 show_diff_view(struct tog_view *view)
4534 const struct got_error *err;
4535 struct tog_diff_view_state *s = &view->state.diff;
4536 char *id_str1 = NULL, *id_str2, *header;
4537 const char *label1, *label2;
4540 err = got_object_id_str(&id_str1, s->id1);
4543 label1 = s->label1 ? : id_str1;
4545 label1 = "/dev/null";
4547 err = got_object_id_str(&id_str2, s->id2);
4550 label2 = s->label2 ? : id_str2;
4552 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4553 err = got_error_from_errno("asprintf");
4561 err = draw_file(view, header);
4566 static const struct got_error *
4567 set_selected_commit(struct tog_diff_view_state *s,
4568 struct commit_queue_entry *entry)
4570 const struct got_error *err;
4571 const struct got_object_id_queue *parent_ids;
4572 struct got_commit_object *selected_commit;
4573 struct got_object_qid *pid;
4576 s->id2 = got_object_id_dup(entry->id);
4578 return got_error_from_errno("got_object_id_dup");
4580 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4583 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4585 pid = STAILQ_FIRST(parent_ids);
4586 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4587 got_object_commit_close(selected_commit);
4591 static const struct got_error *
4592 reset_diff_view(struct tog_view *view)
4594 struct tog_diff_view_state *s = &view->state.diff;
4597 wclear(view->window);
4598 s->first_displayed_line = 1;
4599 s->last_displayed_line = view->nlines;
4600 s->matched_line = 0;
4601 diff_view_indicate_progress(view);
4602 return create_diff(s);
4605 static const struct got_error *
4606 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4608 const struct got_error *err = NULL;
4609 struct tog_diff_view_state *s = &view->state.diff;
4610 struct tog_log_view_state *ls;
4611 struct commit_queue_entry *old_selected_entry;
4613 size_t linesize = 0;
4615 int i, nscroll = view->nlines - 1;
4622 view->x = MAX(view->maxx - view->ncols / 3, 0);
4627 if (view->x + view->ncols / 3 < view->maxx)
4628 view->x += 2; /* move two columns right */
4634 view->x -= MIN(view->x, 2); /* move two columns back */
4641 s->force_text_diff = !s->force_text_diff;
4643 s->ignore_whitespace = !s->ignore_whitespace;
4644 err = reset_diff_view(view);
4648 s->first_displayed_line = 1;
4657 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4663 if (s->first_displayed_line > 1)
4664 s->first_displayed_line--;
4675 if (s->first_displayed_line == 1) {
4680 while (i++ < nscroll && s->first_displayed_line > 1)
4681 s->first_displayed_line--;
4687 s->first_displayed_line++;
4704 while (!s->eof && i++ < nscroll) {
4705 linelen = getline(&line, &linesize, s->f);
4706 s->first_displayed_line++;
4707 if (linelen == -1) {
4711 err = got_ferror(s->f, GOT_ERR_IO);
4718 if (s->diff_context > 0) {
4720 s->matched_line = 0;
4721 diff_view_indicate_progress(view);
4722 err = create_diff(s);
4723 if (s->first_displayed_line + view->nlines - 1 >
4725 s->first_displayed_line = 1;
4726 s->last_displayed_line = view->nlines;
4732 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4734 s->matched_line = 0;
4735 diff_view_indicate_progress(view);
4736 err = create_diff(s);
4742 if (s->log_view == NULL) {
4746 ls = &s->log_view->state.log;
4747 old_selected_entry = ls->selected_entry;
4749 /* view->count handled in input_log_view() */
4750 err = input_log_view(NULL, s->log_view, KEY_UP);
4754 if (old_selected_entry == ls->selected_entry)
4757 err = set_selected_commit(s, ls->selected_entry);
4761 s->first_displayed_line = 1;
4762 s->last_displayed_line = view->nlines;
4763 s->matched_line = 0;
4766 diff_view_indicate_progress(view);
4767 err = create_diff(s);
4771 if (s->log_view == NULL) {
4775 ls = &s->log_view->state.log;
4776 old_selected_entry = ls->selected_entry;
4778 /* view->count handled in input_log_view() */
4779 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4783 if (old_selected_entry == ls->selected_entry)
4786 err = set_selected_commit(s, ls->selected_entry);
4790 s->first_displayed_line = 1;
4791 s->last_displayed_line = view->nlines;
4792 s->matched_line = 0;
4795 diff_view_indicate_progress(view);
4796 err = create_diff(s);
4806 static const struct got_error *
4807 cmd_diff(int argc, char *argv[])
4809 const struct got_error *error = NULL;
4810 struct got_repository *repo = NULL;
4811 struct got_worktree *worktree = NULL;
4812 struct got_object_id *id1 = NULL, *id2 = NULL;
4813 char *repo_path = NULL, *cwd = NULL;
4814 char *id_str1 = NULL, *id_str2 = NULL;
4815 char *label1 = NULL, *label2 = NULL;
4816 int diff_context = 3, ignore_whitespace = 0;
4817 int ch, force_text_diff = 0;
4819 struct tog_view *view;
4820 int *pack_fds = NULL;
4822 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4825 force_text_diff = 1;
4828 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4831 errx(1, "number of context lines is %s: %s",
4835 repo_path = realpath(optarg, NULL);
4836 if (repo_path == NULL)
4837 return got_error_from_errno2("realpath",
4839 got_path_strip_trailing_slashes(repo_path);
4842 ignore_whitespace = 1;
4854 usage_diff(); /* TODO show local worktree changes */
4855 } else if (argc == 2) {
4861 error = got_repo_pack_fds_open(&pack_fds);
4865 if (repo_path == NULL) {
4866 cwd = getcwd(NULL, 0);
4868 return got_error_from_errno("getcwd");
4869 error = got_worktree_open(&worktree, cwd);
4870 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4874 strdup(got_worktree_get_repo_path(worktree));
4876 repo_path = strdup(cwd);
4877 if (repo_path == NULL) {
4878 error = got_error_from_errno("strdup");
4883 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4889 error = apply_unveil(got_repo_get_path(repo), NULL);
4893 error = tog_load_refs(repo, 0);
4897 error = got_repo_match_object_id(&id1, &label1, id_str1,
4898 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4902 error = got_repo_match_object_id(&id2, &label2, id_str2,
4903 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4907 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4909 error = got_error_from_errno("view_open");
4912 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4913 ignore_whitespace, force_text_diff, NULL, repo);
4916 error = view_loop(view);
4923 const struct got_error *close_err = got_repo_close(repo);
4928 got_worktree_close(worktree);
4930 const struct got_error *pack_err =
4931 got_repo_pack_fds_close(pack_fds);
4944 "usage: %s blame [-c commit] [-r repository-path] path\n",
4949 struct tog_blame_line {
4951 struct got_object_id *id;
4954 static const struct got_error *
4955 draw_blame(struct tog_view *view)
4957 struct tog_blame_view_state *s = &view->state.blame;
4958 struct tog_blame *blame = &s->blame;
4959 regmatch_t *regmatch = &view->regmatch;
4960 const struct got_error *err;
4961 int lineno = 0, nprinted = 0;
4963 size_t linesize = 0;
4967 struct tog_blame_line *blame_line;
4968 struct got_object_id *prev_id = NULL;
4970 struct tog_color *tc;
4972 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4977 werase(view->window);
4979 if (asprintf(&line, "commit %s", id_str) == -1) {
4980 err = got_error_from_errno("asprintf");
4985 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4990 if (view_needs_focus_indication(view))
4991 wstandout(view->window);
4992 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4994 wattr_on(view->window,
4995 COLOR_PAIR(tc->colorpair), NULL);
4996 waddwstr(view->window, wline);
4998 wattr_off(view->window,
4999 COLOR_PAIR(tc->colorpair), NULL);
5000 if (view_needs_focus_indication(view))
5001 wstandend(view->window);
5004 if (width < view->ncols - 1)
5005 waddch(view->window, '\n');
5007 if (asprintf(&line, "[%d/%d] %s%s",
5008 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5009 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5011 return got_error_from_errno("asprintf");
5014 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5019 waddwstr(view->window, wline);
5022 if (width < view->ncols - 1)
5023 waddch(view->window, '\n');
5027 while (nprinted < view->nlines - 2) {
5028 linelen = getline(&line, &linesize, blame->f);
5029 if (linelen == -1) {
5030 if (feof(blame->f)) {
5035 return got_ferror(blame->f, GOT_ERR_IO);
5037 if (++lineno < s->first_displayed_line)
5040 /* Set view->maxx based on full line length. */
5041 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5048 view->maxx = MAX(view->maxx, width);
5050 if (view->focussed && nprinted == s->selected_line - 1)
5051 wstandout(view->window);
5053 if (blame->nlines > 0) {
5054 blame_line = &blame->lines[lineno - 1];
5055 if (blame_line->annotated && prev_id &&
5056 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5058 nprinted == s->selected_line - 1)) {
5059 waddstr(view->window, " ");
5060 } else if (blame_line->annotated) {
5062 err = got_object_id_str(&id_str,
5068 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5070 wattr_on(view->window,
5071 COLOR_PAIR(tc->colorpair), NULL);
5072 wprintw(view->window, "%.8s", id_str);
5074 wattr_off(view->window,
5075 COLOR_PAIR(tc->colorpair), NULL);
5077 prev_id = blame_line->id;
5079 waddstr(view->window, "........");
5083 waddstr(view->window, "........");
5087 if (view->focussed && nprinted == s->selected_line - 1)
5088 wstandend(view->window);
5089 waddstr(view->window, " ");
5091 if (view->ncols <= 9) {
5093 } else if (s->first_displayed_line + nprinted ==
5095 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5096 err = add_matched_line(&width, line, view->ncols - 9, 9,
5097 view->window, view->x, regmatch);
5105 err = format_line(&wline, &width, &skip, line,
5106 view->x, view->ncols - 9, 9, 1);
5111 waddwstr(view->window, &wline[skip]);
5117 if (width <= view->ncols - 1)
5118 waddch(view->window, '\n');
5119 if (++nprinted == 1)
5120 s->first_displayed_line = lineno;
5123 s->last_displayed_line = lineno;
5130 static const struct got_error *
5131 blame_cb(void *arg, int nlines, int lineno,
5132 struct got_commit_object *commit, struct got_object_id *id)
5134 const struct got_error *err = NULL;
5135 struct tog_blame_cb_args *a = arg;
5136 struct tog_blame_line *line;
5139 if (nlines != a->nlines ||
5140 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5141 return got_error(GOT_ERR_RANGE);
5143 errcode = pthread_mutex_lock(&tog_mutex);
5145 return got_error_set_errno(errcode, "pthread_mutex_lock");
5147 if (*a->quit) { /* user has quit the blame view */
5148 err = got_error(GOT_ERR_ITER_COMPLETED);
5153 goto done; /* no change in this commit */
5155 line = &a->lines[lineno - 1];
5156 if (line->annotated)
5159 line->id = got_object_id_dup(id);
5160 if (line->id == NULL) {
5161 err = got_error_from_errno("got_object_id_dup");
5164 line->annotated = 1;
5166 errcode = pthread_mutex_unlock(&tog_mutex);
5168 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5173 blame_thread(void *arg)
5175 const struct got_error *err, *close_err;
5176 struct tog_blame_thread_args *ta = arg;
5177 struct tog_blame_cb_args *a = ta->cb_args;
5178 int errcode, fd1 = -1, fd2 = -1;
5179 FILE *f1 = NULL, *f2 = NULL;
5181 fd1 = got_opentempfd();
5183 return (void *)got_error_from_errno("got_opentempfd");
5185 fd2 = got_opentempfd();
5187 err = got_error_from_errno("got_opentempfd");
5191 f1 = got_opentemp();
5193 err = (void *)got_error_from_errno("got_opentemp");
5196 f2 = got_opentemp();
5198 err = (void *)got_error_from_errno("got_opentemp");
5202 err = block_signals_used_by_main_thread();
5206 err = got_blame(ta->path, a->commit_id, ta->repo,
5207 tog_diff_algo, blame_cb, ta->cb_args,
5208 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5209 if (err && err->code == GOT_ERR_CANCELLED)
5212 errcode = pthread_mutex_lock(&tog_mutex);
5214 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5218 close_err = got_repo_close(ta->repo);
5224 errcode = pthread_mutex_unlock(&tog_mutex);
5225 if (errcode && err == NULL)
5226 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5229 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5230 err = got_error_from_errno("close");
5231 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5232 err = got_error_from_errno("close");
5233 if (f1 && fclose(f1) == EOF && err == NULL)
5234 err = got_error_from_errno("fclose");
5235 if (f2 && fclose(f2) == EOF && err == NULL)
5236 err = got_error_from_errno("fclose");
5241 static struct got_object_id *
5242 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5243 int first_displayed_line, int selected_line)
5245 struct tog_blame_line *line;
5250 line = &lines[first_displayed_line - 1 + selected_line - 1];
5251 if (!line->annotated)
5257 static const struct got_error *
5258 stop_blame(struct tog_blame *blame)
5260 const struct got_error *err = NULL;
5263 if (blame->thread) {
5265 errcode = pthread_mutex_unlock(&tog_mutex);
5267 return got_error_set_errno(errcode,
5268 "pthread_mutex_unlock");
5269 errcode = pthread_join(blame->thread, (void **)&err);
5271 return got_error_set_errno(errcode, "pthread_join");
5272 errcode = pthread_mutex_lock(&tog_mutex);
5274 return got_error_set_errno(errcode,
5275 "pthread_mutex_lock");
5276 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5278 blame->thread = 0; //NULL;
5280 if (blame->thread_args.repo) {
5281 const struct got_error *close_err;
5282 close_err = got_repo_close(blame->thread_args.repo);
5285 blame->thread_args.repo = NULL;
5288 if (fclose(blame->f) == EOF && err == NULL)
5289 err = got_error_from_errno("fclose");
5293 for (i = 0; i < blame->nlines; i++)
5294 free(blame->lines[i].id);
5296 blame->lines = NULL;
5298 free(blame->cb_args.commit_id);
5299 blame->cb_args.commit_id = NULL;
5300 if (blame->pack_fds) {
5301 const struct got_error *pack_err =
5302 got_repo_pack_fds_close(blame->pack_fds);
5305 blame->pack_fds = NULL;
5310 static const struct got_error *
5311 cancel_blame_view(void *arg)
5313 const struct got_error *err = NULL;
5317 errcode = pthread_mutex_lock(&tog_mutex);
5319 return got_error_set_errno(errcode,
5320 "pthread_mutex_unlock");
5323 err = got_error(GOT_ERR_CANCELLED);
5325 errcode = pthread_mutex_unlock(&tog_mutex);
5327 return got_error_set_errno(errcode,
5328 "pthread_mutex_lock");
5333 static const struct got_error *
5334 run_blame(struct tog_view *view)
5336 struct tog_blame_view_state *s = &view->state.blame;
5337 struct tog_blame *blame = &s->blame;
5338 const struct got_error *err = NULL;
5339 struct got_commit_object *commit = NULL;
5340 struct got_blob_object *blob = NULL;
5341 struct got_repository *thread_repo = NULL;
5342 struct got_object_id *obj_id = NULL;
5343 int obj_type, fd = -1;
5344 int *pack_fds = NULL;
5346 err = got_object_open_as_commit(&commit, s->repo,
5347 &s->blamed_commit->id);
5351 fd = got_opentempfd();
5353 err = got_error_from_errno("got_opentempfd");
5357 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5361 err = got_object_get_type(&obj_type, s->repo, obj_id);
5365 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5366 err = got_error(GOT_ERR_OBJ_TYPE);
5370 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5373 blame->f = got_opentemp();
5374 if (blame->f == NULL) {
5375 err = got_error_from_errno("got_opentemp");
5378 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5379 &blame->line_offsets, blame->f, blob);
5382 if (blame->nlines == 0) {
5383 s->blame_complete = 1;
5387 /* Don't include \n at EOF in the blame line count. */
5388 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5391 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5392 if (blame->lines == NULL) {
5393 err = got_error_from_errno("calloc");
5397 err = got_repo_pack_fds_open(&pack_fds);
5400 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5405 blame->pack_fds = pack_fds;
5406 blame->cb_args.view = view;
5407 blame->cb_args.lines = blame->lines;
5408 blame->cb_args.nlines = blame->nlines;
5409 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5410 if (blame->cb_args.commit_id == NULL) {
5411 err = got_error_from_errno("got_object_id_dup");
5414 blame->cb_args.quit = &s->done;
5416 blame->thread_args.path = s->path;
5417 blame->thread_args.repo = thread_repo;
5418 blame->thread_args.cb_args = &blame->cb_args;
5419 blame->thread_args.complete = &s->blame_complete;
5420 blame->thread_args.cancel_cb = cancel_blame_view;
5421 blame->thread_args.cancel_arg = &s->done;
5422 s->blame_complete = 0;
5424 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5425 s->first_displayed_line = 1;
5426 s->last_displayed_line = view->nlines;
5427 s->selected_line = 1;
5429 s->matched_line = 0;
5433 got_object_commit_close(commit);
5434 if (fd != -1 && close(fd) == -1 && err == NULL)
5435 err = got_error_from_errno("close");
5437 got_object_blob_close(blob);
5444 static const struct got_error *
5445 open_blame_view(struct tog_view *view, char *path,
5446 struct got_object_id *commit_id, struct got_repository *repo)
5448 const struct got_error *err = NULL;
5449 struct tog_blame_view_state *s = &view->state.blame;
5451 STAILQ_INIT(&s->blamed_commits);
5453 s->path = strdup(path);
5454 if (s->path == NULL)
5455 return got_error_from_errno("strdup");
5457 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5463 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5464 s->first_displayed_line = 1;
5465 s->last_displayed_line = view->nlines;
5466 s->selected_line = 1;
5467 s->blame_complete = 0;
5469 s->commit_id = commit_id;
5470 memset(&s->blame, 0, sizeof(s->blame));
5472 STAILQ_INIT(&s->colors);
5473 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5474 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5475 get_color_value("TOG_COLOR_COMMIT"));
5480 view->show = show_blame_view;
5481 view->input = input_blame_view;
5482 view->reset = reset_blame_view;
5483 view->close = close_blame_view;
5484 view->search_start = search_start_blame_view;
5485 view->search_next = search_next_blame_view;
5487 return run_blame(view);
5490 static const struct got_error *
5491 close_blame_view(struct tog_view *view)
5493 const struct got_error *err = NULL;
5494 struct tog_blame_view_state *s = &view->state.blame;
5496 if (s->blame.thread)
5497 err = stop_blame(&s->blame);
5499 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5500 struct got_object_qid *blamed_commit;
5501 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5502 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5503 got_object_qid_free(blamed_commit);
5507 free_colors(&s->colors);
5511 static const struct got_error *
5512 search_start_blame_view(struct tog_view *view)
5514 struct tog_blame_view_state *s = &view->state.blame;
5516 s->matched_line = 0;
5520 static const struct got_error *
5521 search_next_blame_view(struct tog_view *view)
5523 struct tog_blame_view_state *s = &view->state.blame;
5524 const struct got_error *err = NULL;
5527 size_t linesize = 0;
5530 if (!view->searching) {
5531 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5535 if (s->matched_line) {
5536 if (view->searching == TOG_SEARCH_FORWARD)
5537 lineno = s->matched_line + 1;
5539 lineno = s->matched_line - 1;
5541 lineno = s->first_displayed_line - 1 + s->selected_line;
5546 if (lineno <= 0 || lineno > s->blame.nlines) {
5547 if (s->matched_line == 0) {
5548 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5552 if (view->searching == TOG_SEARCH_FORWARD)
5555 lineno = s->blame.nlines;
5558 offset = s->blame.line_offsets[lineno - 1];
5559 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5561 return got_error_from_errno("fseeko");
5563 linelen = getline(&line, &linesize, s->blame.f);
5564 if (linelen != -1) {
5566 err = expand_tab(&exstr, line);
5569 if (match_line(exstr, &view->regex, 1,
5571 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5572 s->matched_line = lineno;
5578 if (view->searching == TOG_SEARCH_FORWARD)
5585 if (s->matched_line) {
5586 s->first_displayed_line = s->matched_line;
5587 s->selected_line = 1;
5593 static const struct got_error *
5594 show_blame_view(struct tog_view *view)
5596 const struct got_error *err = NULL;
5597 struct tog_blame_view_state *s = &view->state.blame;
5600 if (s->blame.thread == 0 && !s->blame_complete) {
5601 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5602 &s->blame.thread_args);
5604 return got_error_set_errno(errcode, "pthread_create");
5606 halfdelay(1); /* fast refresh while annotating */
5609 if (s->blame_complete)
5610 halfdelay(10); /* disable fast refresh */
5612 err = draw_blame(view);
5618 static const struct got_error *
5619 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5621 const struct got_error *err = NULL, *thread_err = NULL;
5622 struct tog_view *diff_view;
5623 struct tog_blame_view_state *s = &view->state.blame;
5624 int eos, nscroll, begin_y = 0, begin_x = 0;
5626 eos = nscroll = view->nlines - 2;
5627 if (view_is_hsplit_top(view))
5635 view->x = MAX(view->maxx - view->ncols / 3, 0);
5640 if (view->x + view->ncols / 3 < view->maxx)
5641 view->x += 2; /* move two columns right */
5647 view->x -= MIN(view->x, 2); /* move two columns back */
5656 s->selected_line = 1;
5657 s->first_displayed_line = 1;
5662 if (s->blame.nlines < eos) {
5663 s->selected_line = s->blame.nlines;
5664 s->first_displayed_line = 1;
5666 s->selected_line = eos;
5667 s->first_displayed_line = s->blame.nlines - (eos - 1);
5674 if (s->selected_line > 1)
5676 else if (s->selected_line == 1 &&
5677 s->first_displayed_line > 1)
5678 s->first_displayed_line--;
5689 if (s->first_displayed_line == 1) {
5690 if (view->count > 1)
5692 s->selected_line = MAX(1, s->selected_line - nscroll);
5696 if (s->first_displayed_line > nscroll)
5697 s->first_displayed_line -= nscroll;
5699 s->first_displayed_line = 1;
5704 if (s->selected_line < eos && s->first_displayed_line +
5705 s->selected_line <= s->blame.nlines)
5707 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5708 s->first_displayed_line++;
5714 struct got_object_id *id = NULL;
5717 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5718 s->first_displayed_line, s->selected_line);
5722 struct got_commit_object *commit, *pcommit;
5723 struct got_object_qid *pid;
5724 struct got_object_id *blob_id = NULL;
5726 err = got_object_open_as_commit(&commit,
5731 got_object_commit_get_parent_ids(commit));
5733 got_object_commit_close(commit);
5736 /* Check if path history ends here. */
5737 err = got_object_open_as_commit(&pcommit,
5741 err = got_object_id_by_path(&blob_id, s->repo,
5743 got_object_commit_close(pcommit);
5745 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5747 got_object_commit_close(commit);
5750 err = got_object_get_type(&obj_type, s->repo,
5753 /* Can't blame non-blob type objects. */
5754 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5755 got_object_commit_close(commit);
5758 err = got_object_qid_alloc(&s->blamed_commit,
5760 got_object_commit_close(commit);
5762 if (got_object_id_cmp(id,
5763 &s->blamed_commit->id) == 0)
5765 err = got_object_qid_alloc(&s->blamed_commit,
5771 thread_err = stop_blame(&s->blame);
5775 STAILQ_INSERT_HEAD(&s->blamed_commits,
5776 s->blamed_commit, entry);
5777 err = run_blame(view);
5783 struct got_object_qid *first;
5786 first = STAILQ_FIRST(&s->blamed_commits);
5787 if (!got_object_id_cmp(&first->id, s->commit_id))
5790 thread_err = stop_blame(&s->blame);
5794 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5795 got_object_qid_free(s->blamed_commit);
5797 STAILQ_FIRST(&s->blamed_commits);
5798 err = run_blame(view);
5805 struct got_object_id *id = NULL;
5806 struct got_object_qid *pid;
5807 struct got_commit_object *commit = NULL;
5810 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5811 s->first_displayed_line, s->selected_line);
5814 err = got_object_open_as_commit(&commit, s->repo, id);
5817 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5818 if (view_is_parent_view(view))
5819 view_get_split(view, &begin_y, &begin_x);
5821 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5822 if (diff_view == NULL) {
5823 got_object_commit_close(commit);
5824 err = got_error_from_errno("view_open");
5827 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5828 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5829 got_object_commit_close(commit);
5831 view_close(diff_view);
5834 if (view_is_parent_view(view) &&
5835 view->mode == TOG_VIEW_SPLIT_HRZN) {
5836 err = view_init_hsplit(view, begin_y);
5842 diff_view->focussed = 1;
5843 diff_view->mode = view->mode;
5844 diff_view->nlines = view->lines - begin_y;
5845 if (view_is_parent_view(view)) {
5846 view_transfer_size(diff_view, view);
5847 err = view_close_child(view);
5850 err = view_set_child(view, diff_view);
5853 view->focus_child = 1;
5855 *new_view = diff_view;
5868 if (s->last_displayed_line >= s->blame.nlines &&
5869 s->selected_line >= MIN(s->blame.nlines,
5870 view->nlines - 2)) {
5874 if (s->last_displayed_line >= s->blame.nlines &&
5875 s->selected_line < view->nlines - 2) {
5877 MIN(nscroll, s->last_displayed_line -
5878 s->first_displayed_line - s->selected_line + 1);
5880 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5881 s->first_displayed_line += nscroll;
5883 s->first_displayed_line =
5884 s->blame.nlines - (view->nlines - 3);
5887 if (s->selected_line > view->nlines - 2) {
5888 s->selected_line = MIN(s->blame.nlines,
5896 return thread_err ? thread_err : err;
5899 static const struct got_error *
5900 reset_blame_view(struct tog_view *view)
5902 const struct got_error *err;
5903 struct tog_blame_view_state *s = &view->state.blame;
5907 err = stop_blame(&s->blame);
5911 return run_blame(view);
5914 static const struct got_error *
5915 cmd_blame(int argc, char *argv[])
5917 const struct got_error *error;
5918 struct got_repository *repo = NULL;
5919 struct got_worktree *worktree = NULL;
5920 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5921 char *link_target = NULL;
5922 struct got_object_id *commit_id = NULL;
5923 struct got_commit_object *commit = NULL;
5924 char *commit_id_str = NULL;
5926 struct tog_view *view;
5927 int *pack_fds = NULL;
5929 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5932 commit_id_str = optarg;
5935 repo_path = realpath(optarg, NULL);
5936 if (repo_path == NULL)
5937 return got_error_from_errno2("realpath",
5952 error = got_repo_pack_fds_open(&pack_fds);
5956 if (repo_path == NULL) {
5957 cwd = getcwd(NULL, 0);
5959 return got_error_from_errno("getcwd");
5960 error = got_worktree_open(&worktree, cwd);
5961 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5965 strdup(got_worktree_get_repo_path(worktree));
5967 repo_path = strdup(cwd);
5968 if (repo_path == NULL) {
5969 error = got_error_from_errno("strdup");
5974 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5978 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5985 error = apply_unveil(got_repo_get_path(repo), NULL);
5989 error = tog_load_refs(repo, 0);
5993 if (commit_id_str == NULL) {
5994 struct got_reference *head_ref;
5995 error = got_ref_open(&head_ref, repo, worktree ?
5996 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5999 error = got_ref_resolve(&commit_id, repo, head_ref);
6000 got_ref_close(head_ref);
6002 error = got_repo_match_object_id(&commit_id, NULL,
6003 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6008 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6010 error = got_error_from_errno("view_open");
6014 error = got_object_open_as_commit(&commit, repo, commit_id);
6018 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6023 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6028 /* Release work tree lock. */
6029 got_worktree_close(worktree);
6032 error = view_loop(view);
6040 got_object_commit_close(commit);
6042 got_worktree_close(worktree);
6044 const struct got_error *close_err = got_repo_close(repo);
6049 const struct got_error *pack_err =
6050 got_repo_pack_fds_close(pack_fds);
6058 static const struct got_error *
6059 draw_tree_entries(struct tog_view *view, const char *parent_path)
6061 struct tog_tree_view_state *s = &view->state.tree;
6062 const struct got_error *err = NULL;
6063 struct got_tree_entry *te;
6065 struct tog_color *tc;
6066 int width, n, i, nentries;
6067 int limit = view->nlines;
6070 if (view_is_hsplit_top(view))
6071 --limit; /* border */
6073 werase(view->window);
6078 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6082 if (view_needs_focus_indication(view))
6083 wstandout(view->window);
6084 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6086 wattr_on(view->window,
6087 COLOR_PAIR(tc->colorpair), NULL);
6088 waddwstr(view->window, wline);
6090 wattr_off(view->window,
6091 COLOR_PAIR(tc->colorpair), NULL);
6092 if (view_needs_focus_indication(view))
6093 wstandend(view->window);
6096 if (width < view->ncols - 1)
6097 waddch(view->window, '\n');
6100 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6104 waddwstr(view->window, wline);
6107 if (width < view->ncols - 1)
6108 waddch(view->window, '\n');
6111 waddch(view->window, '\n');
6115 if (s->first_displayed_entry == NULL) {
6116 te = got_object_tree_get_first_entry(s->tree);
6117 if (s->selected == 0) {
6119 wstandout(view->window);
6120 s->selected_entry = NULL;
6122 waddstr(view->window, " ..\n"); /* parent directory */
6123 if (s->selected == 0 && view->focussed)
6124 wstandend(view->window);
6131 te = s->first_displayed_entry;
6134 nentries = got_object_tree_get_nentries(s->tree);
6135 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6136 char *line = NULL, *id_str = NULL, *link_target = NULL;
6137 const char *modestr = "";
6140 te = got_object_tree_get_entry(s->tree, i);
6141 mode = got_tree_entry_get_mode(te);
6144 err = got_object_id_str(&id_str,
6145 got_tree_entry_get_id(te));
6147 return got_error_from_errno(
6148 "got_object_id_str");
6150 if (got_object_tree_entry_is_submodule(te))
6152 else if (S_ISLNK(mode)) {
6155 err = got_tree_entry_get_symlink_target(&link_target,
6161 for (i = 0; i < strlen(link_target); i++) {
6162 if (!isprint((unsigned char)link_target[i]))
6163 link_target[i] = '?';
6167 else if (S_ISDIR(mode))
6169 else if (mode & S_IXUSR)
6171 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6172 got_tree_entry_get_name(te), modestr,
6173 link_target ? " -> ": "",
6174 link_target ? link_target : "") == -1) {
6177 return got_error_from_errno("asprintf");
6181 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6187 if (n == s->selected) {
6189 wstandout(view->window);
6190 s->selected_entry = te;
6192 tc = match_color(&s->colors, line);
6194 wattr_on(view->window,
6195 COLOR_PAIR(tc->colorpair), NULL);
6196 waddwstr(view->window, wline);
6198 wattr_off(view->window,
6199 COLOR_PAIR(tc->colorpair), NULL);
6200 if (width < view->ncols - 1)
6201 waddch(view->window, '\n');
6202 if (n == s->selected && view->focussed)
6203 wstandend(view->window);
6209 s->last_displayed_entry = te;
6218 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6220 struct got_tree_entry *te;
6221 int isroot = s->tree == s->root;
6224 if (s->first_displayed_entry == NULL)
6227 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6228 while (i++ < maxscroll) {
6231 s->first_displayed_entry = NULL;
6234 s->first_displayed_entry = te;
6235 te = got_tree_entry_get_prev(s->tree, te);
6239 static const struct got_error *
6240 tree_scroll_down(struct tog_view *view, int maxscroll)
6242 struct tog_tree_view_state *s = &view->state.tree;
6243 struct got_tree_entry *next, *last;
6246 if (s->first_displayed_entry)
6247 next = got_tree_entry_get_next(s->tree,
6248 s->first_displayed_entry);
6250 next = got_object_tree_get_first_entry(s->tree);
6252 last = s->last_displayed_entry;
6253 while (next && n++ < maxscroll) {
6255 last = got_tree_entry_get_next(s->tree, last);
6256 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6257 s->first_displayed_entry = next;
6258 next = got_tree_entry_get_next(s->tree, next);
6265 static const struct got_error *
6266 tree_entry_path(char **path, struct tog_parent_trees *parents,
6267 struct got_tree_entry *te)
6269 const struct got_error *err = NULL;
6270 struct tog_parent_tree *pt;
6271 size_t len = 2; /* for leading slash and NUL */
6273 TAILQ_FOREACH(pt, parents, entry)
6274 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6277 len += strlen(got_tree_entry_get_name(te));
6279 *path = calloc(1, len);
6281 return got_error_from_errno("calloc");
6284 pt = TAILQ_LAST(parents, tog_parent_trees);
6286 const char *name = got_tree_entry_get_name(pt->selected_entry);
6287 if (strlcat(*path, name, len) >= len) {
6288 err = got_error(GOT_ERR_NO_SPACE);
6291 if (strlcat(*path, "/", len) >= len) {
6292 err = got_error(GOT_ERR_NO_SPACE);
6295 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6298 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6299 err = got_error(GOT_ERR_NO_SPACE);
6311 static const struct got_error *
6312 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6313 struct got_tree_entry *te, struct tog_parent_trees *parents,
6314 struct got_object_id *commit_id, struct got_repository *repo)
6316 const struct got_error *err = NULL;
6318 struct tog_view *blame_view;
6322 err = tree_entry_path(&path, parents, te);
6326 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6327 if (blame_view == NULL) {
6328 err = got_error_from_errno("view_open");
6332 err = open_blame_view(blame_view, path, commit_id, repo);
6334 if (err->code == GOT_ERR_CANCELLED)
6336 view_close(blame_view);
6338 *new_view = blame_view;
6344 static const struct got_error *
6345 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6346 struct tog_tree_view_state *s)
6348 struct tog_view *log_view;
6349 const struct got_error *err = NULL;
6354 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6355 if (log_view == NULL)
6356 return got_error_from_errno("view_open");
6358 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6362 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6365 view_close(log_view);
6367 *new_view = log_view;
6372 static const struct got_error *
6373 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6374 const char *head_ref_name, struct got_repository *repo)
6376 const struct got_error *err = NULL;
6377 char *commit_id_str = NULL;
6378 struct tog_tree_view_state *s = &view->state.tree;
6379 struct got_commit_object *commit = NULL;
6381 TAILQ_INIT(&s->parents);
6382 STAILQ_INIT(&s->colors);
6384 s->commit_id = got_object_id_dup(commit_id);
6385 if (s->commit_id == NULL)
6386 return got_error_from_errno("got_object_id_dup");
6388 err = got_object_open_as_commit(&commit, repo, commit_id);
6393 * The root is opened here and will be closed when the view is closed.
6394 * Any visited subtrees and their path-wise parents are opened and
6397 err = got_object_open_as_tree(&s->root, repo,
6398 got_object_commit_get_tree_id(commit));
6403 err = got_object_id_str(&commit_id_str, commit_id);
6407 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6408 err = got_error_from_errno("asprintf");
6412 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6413 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6414 if (head_ref_name) {
6415 s->head_ref_name = strdup(head_ref_name);
6416 if (s->head_ref_name == NULL) {
6417 err = got_error_from_errno("strdup");
6423 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6424 err = add_color(&s->colors, "\\$$",
6425 TOG_COLOR_TREE_SUBMODULE,
6426 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6429 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6430 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6433 err = add_color(&s->colors, "/$",
6434 TOG_COLOR_TREE_DIRECTORY,
6435 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6439 err = add_color(&s->colors, "\\*$",
6440 TOG_COLOR_TREE_EXECUTABLE,
6441 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6445 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6446 get_color_value("TOG_COLOR_COMMIT"));
6451 view->show = show_tree_view;
6452 view->input = input_tree_view;
6453 view->close = close_tree_view;
6454 view->search_start = search_start_tree_view;
6455 view->search_next = search_next_tree_view;
6457 free(commit_id_str);
6459 got_object_commit_close(commit);
6461 close_tree_view(view);
6465 static const struct got_error *
6466 close_tree_view(struct tog_view *view)
6468 struct tog_tree_view_state *s = &view->state.tree;
6470 free_colors(&s->colors);
6471 free(s->tree_label);
6472 s->tree_label = NULL;
6474 s->commit_id = NULL;
6475 free(s->head_ref_name);
6476 s->head_ref_name = NULL;
6477 while (!TAILQ_EMPTY(&s->parents)) {
6478 struct tog_parent_tree *parent;
6479 parent = TAILQ_FIRST(&s->parents);
6480 TAILQ_REMOVE(&s->parents, parent, entry);
6481 if (parent->tree != s->root)
6482 got_object_tree_close(parent->tree);
6486 if (s->tree != NULL && s->tree != s->root)
6487 got_object_tree_close(s->tree);
6489 got_object_tree_close(s->root);
6493 static const struct got_error *
6494 search_start_tree_view(struct tog_view *view)
6496 struct tog_tree_view_state *s = &view->state.tree;
6498 s->matched_entry = NULL;
6503 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6505 regmatch_t regmatch;
6507 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6511 static const struct got_error *
6512 search_next_tree_view(struct tog_view *view)
6514 struct tog_tree_view_state *s = &view->state.tree;
6515 struct got_tree_entry *te = NULL;
6517 if (!view->searching) {
6518 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6522 if (s->matched_entry) {
6523 if (view->searching == TOG_SEARCH_FORWARD) {
6524 if (s->selected_entry)
6525 te = got_tree_entry_get_next(s->tree,
6528 te = got_object_tree_get_first_entry(s->tree);
6530 if (s->selected_entry == NULL)
6531 te = got_object_tree_get_last_entry(s->tree);
6533 te = got_tree_entry_get_prev(s->tree,
6537 if (s->selected_entry)
6538 te = s->selected_entry;
6539 else if (view->searching == TOG_SEARCH_FORWARD)
6540 te = got_object_tree_get_first_entry(s->tree);
6542 te = got_object_tree_get_last_entry(s->tree);
6547 if (s->matched_entry == NULL) {
6548 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6551 if (view->searching == TOG_SEARCH_FORWARD)
6552 te = got_object_tree_get_first_entry(s->tree);
6554 te = got_object_tree_get_last_entry(s->tree);
6557 if (match_tree_entry(te, &view->regex)) {
6558 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6559 s->matched_entry = te;
6563 if (view->searching == TOG_SEARCH_FORWARD)
6564 te = got_tree_entry_get_next(s->tree, te);
6566 te = got_tree_entry_get_prev(s->tree, te);
6569 if (s->matched_entry) {
6570 s->first_displayed_entry = s->matched_entry;
6577 static const struct got_error *
6578 show_tree_view(struct tog_view *view)
6580 const struct got_error *err = NULL;
6581 struct tog_tree_view_state *s = &view->state.tree;
6584 err = tree_entry_path(&parent_path, &s->parents, NULL);
6588 err = draw_tree_entries(view, parent_path);
6595 static const struct got_error *
6596 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6598 const struct got_error *err = NULL;
6599 struct tog_tree_view_state *s = &view->state.tree;
6600 struct tog_view *log_view, *ref_view;
6601 struct got_tree_entry *te;
6602 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6606 s->show_ids = !s->show_ids;
6611 if (!s->selected_entry)
6613 if (view_is_parent_view(view))
6614 view_get_split(view, &begin_y, &begin_x);
6615 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6616 if (view_is_parent_view(view) &&
6617 view->mode == TOG_VIEW_SPLIT_HRZN) {
6618 err = view_init_hsplit(view, begin_y);
6623 log_view->focussed = 1;
6624 log_view->mode = view->mode;
6625 log_view->nlines = view->lines - begin_y;
6626 if (view_is_parent_view(view)) {
6627 view_transfer_size(log_view, view);
6628 err = view_close_child(view);
6631 err = view_set_child(view, log_view);
6634 view->focus_child = 1;
6636 *new_view = log_view;
6640 if (view_is_parent_view(view))
6641 view_get_split(view, &begin_y, &begin_x);
6642 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6643 if (ref_view == NULL)
6644 return got_error_from_errno("view_open");
6645 err = open_ref_view(ref_view, s->repo);
6647 view_close(ref_view);
6650 if (view_is_parent_view(view) &&
6651 view->mode == TOG_VIEW_SPLIT_HRZN) {
6652 err = view_init_hsplit(view, begin_y);
6657 ref_view->focussed = 1;
6658 ref_view->mode = view->mode;
6659 ref_view->nlines = view->lines - begin_y;
6660 if (view_is_parent_view(view)) {
6661 view_transfer_size(ref_view, view);
6662 err = view_close_child(view);
6665 err = view_set_child(view, ref_view);
6668 view->focus_child = 1;
6670 *new_view = ref_view;
6676 if (s->tree == s->root)
6677 s->first_displayed_entry =
6678 got_object_tree_get_first_entry(s->tree);
6680 s->first_displayed_entry = NULL;
6684 int eos = view->nlines - 3;
6686 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6690 te = got_object_tree_get_last_entry(s->tree);
6691 for (n = 0; n < eos; n++) {
6693 if(s->tree != s->root) {
6694 s->first_displayed_entry = NULL;
6699 s->first_displayed_entry = te;
6700 te = got_tree_entry_get_prev(s->tree, te);
6703 s->selected = n - 1;
6709 if (s->selected > 0) {
6713 tree_scroll_up(s, 1);
6714 if (s->selected_entry == NULL ||
6715 (s->tree == s->root && s->selected_entry ==
6716 got_object_tree_get_first_entry(s->tree)))
6726 if (s->tree == s->root) {
6727 if (got_object_tree_get_first_entry(s->tree) ==
6728 s->first_displayed_entry)
6729 s->selected -= MIN(s->selected, nscroll);
6731 if (s->first_displayed_entry == NULL)
6732 s->selected -= MIN(s->selected, nscroll);
6734 tree_scroll_up(s, MAX(0, nscroll));
6735 if (s->selected_entry == NULL ||
6736 (s->tree == s->root && s->selected_entry ==
6737 got_object_tree_get_first_entry(s->tree)))
6743 if (s->selected < s->ndisplayed - 1) {
6747 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6749 /* can't scroll any further */
6753 tree_scroll_down(view, 1);
6763 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6765 /* can't scroll any further; move cursor down */
6766 if (s->selected < s->ndisplayed - 1)
6767 s->selected += MIN(nscroll,
6768 s->ndisplayed - s->selected - 1);
6773 tree_scroll_down(view, nscroll);
6778 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6779 struct tog_parent_tree *parent;
6780 /* user selected '..' */
6781 if (s->tree == s->root) {
6785 parent = TAILQ_FIRST(&s->parents);
6786 TAILQ_REMOVE(&s->parents, parent,
6788 got_object_tree_close(s->tree);
6789 s->tree = parent->tree;
6790 s->first_displayed_entry =
6791 parent->first_displayed_entry;
6793 parent->selected_entry;
6794 s->selected = parent->selected;
6795 if (s->selected > view->nlines - 3) {
6796 err = offset_selection_down(view);
6801 } else if (S_ISDIR(got_tree_entry_get_mode(
6802 s->selected_entry))) {
6803 struct got_tree_object *subtree;
6805 err = got_object_open_as_tree(&subtree, s->repo,
6806 got_tree_entry_get_id(s->selected_entry));
6809 err = tree_view_visit_subtree(s, subtree);
6811 got_object_tree_close(subtree);
6814 } else if (S_ISREG(got_tree_entry_get_mode(
6815 s->selected_entry))) {
6816 struct tog_view *blame_view;
6817 int begin_x = 0, begin_y = 0;
6819 if (view_is_parent_view(view))
6820 view_get_split(view, &begin_y, &begin_x);
6822 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6823 s->selected_entry, &s->parents,
6824 s->commit_id, s->repo);
6828 if (view_is_parent_view(view) &&
6829 view->mode == TOG_VIEW_SPLIT_HRZN) {
6830 err = view_init_hsplit(view, begin_y);
6837 blame_view->focussed = 1;
6838 blame_view->mode = view->mode;
6839 blame_view->nlines = view->lines - begin_y;
6840 if (view_is_parent_view(view)) {
6841 view_transfer_size(blame_view, view);
6842 err = view_close_child(view);
6845 err = view_set_child(view, blame_view);
6848 view->focus_child = 1;
6850 *new_view = blame_view;
6854 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6855 s->selected = view->nlines - 4;
6871 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6876 static const struct got_error *
6877 cmd_tree(int argc, char *argv[])
6879 const struct got_error *error;
6880 struct got_repository *repo = NULL;
6881 struct got_worktree *worktree = NULL;
6882 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6883 struct got_object_id *commit_id = NULL;
6884 struct got_commit_object *commit = NULL;
6885 const char *commit_id_arg = NULL;
6887 struct got_reference *ref = NULL;
6888 const char *head_ref_name = NULL;
6890 struct tog_view *view;
6891 int *pack_fds = NULL;
6893 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6896 commit_id_arg = optarg;
6899 repo_path = realpath(optarg, NULL);
6900 if (repo_path == NULL)
6901 return got_error_from_errno2("realpath",
6916 error = got_repo_pack_fds_open(&pack_fds);
6920 if (repo_path == NULL) {
6921 cwd = getcwd(NULL, 0);
6923 return got_error_from_errno("getcwd");
6924 error = got_worktree_open(&worktree, cwd);
6925 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6929 strdup(got_worktree_get_repo_path(worktree));
6931 repo_path = strdup(cwd);
6932 if (repo_path == NULL) {
6933 error = got_error_from_errno("strdup");
6938 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6942 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6949 error = apply_unveil(got_repo_get_path(repo), NULL);
6953 error = tog_load_refs(repo, 0);
6957 if (commit_id_arg == NULL) {
6958 error = got_repo_match_object_id(&commit_id, &label,
6959 worktree ? got_worktree_get_head_ref_name(worktree) :
6960 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6963 head_ref_name = label;
6965 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6967 head_ref_name = got_ref_get_name(ref);
6968 else if (error->code != GOT_ERR_NOT_REF)
6970 error = got_repo_match_object_id(&commit_id, NULL,
6971 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6976 error = got_object_open_as_commit(&commit, repo, commit_id);
6980 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6982 error = got_error_from_errno("view_open");
6985 error = open_tree_view(view, commit_id, head_ref_name, repo);
6988 if (!got_path_is_root_dir(in_repo_path)) {
6989 error = tree_view_walk_path(&view->state.tree, commit,
6996 /* Release work tree lock. */
6997 got_worktree_close(worktree);
7000 error = view_loop(view);
7009 const struct got_error *close_err = got_repo_close(repo);
7014 const struct got_error *pack_err =
7015 got_repo_pack_fds_close(pack_fds);
7023 static const struct got_error *
7024 ref_view_load_refs(struct tog_ref_view_state *s)
7026 struct got_reflist_entry *sre;
7027 struct tog_reflist_entry *re;
7030 TAILQ_FOREACH(sre, &tog_refs, entry) {
7031 if (strncmp(got_ref_get_name(sre->ref),
7032 "refs/got/", 9) == 0 &&
7033 strncmp(got_ref_get_name(sre->ref),
7034 "refs/got/backup/", 16) != 0)
7037 re = malloc(sizeof(*re));
7039 return got_error_from_errno("malloc");
7041 re->ref = got_ref_dup(sre->ref);
7042 if (re->ref == NULL)
7043 return got_error_from_errno("got_ref_dup");
7044 re->idx = s->nrefs++;
7045 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7048 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7053 ref_view_free_refs(struct tog_ref_view_state *s)
7055 struct tog_reflist_entry *re;
7057 while (!TAILQ_EMPTY(&s->refs)) {
7058 re = TAILQ_FIRST(&s->refs);
7059 TAILQ_REMOVE(&s->refs, re, entry);
7060 got_ref_close(re->ref);
7065 static const struct got_error *
7066 open_ref_view(struct tog_view *view, struct got_repository *repo)
7068 const struct got_error *err = NULL;
7069 struct tog_ref_view_state *s = &view->state.ref;
7071 s->selected_entry = 0;
7074 TAILQ_INIT(&s->refs);
7075 STAILQ_INIT(&s->colors);
7077 err = ref_view_load_refs(s);
7081 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7082 err = add_color(&s->colors, "^refs/heads/",
7083 TOG_COLOR_REFS_HEADS,
7084 get_color_value("TOG_COLOR_REFS_HEADS"));
7088 err = add_color(&s->colors, "^refs/tags/",
7089 TOG_COLOR_REFS_TAGS,
7090 get_color_value("TOG_COLOR_REFS_TAGS"));
7094 err = add_color(&s->colors, "^refs/remotes/",
7095 TOG_COLOR_REFS_REMOTES,
7096 get_color_value("TOG_COLOR_REFS_REMOTES"));
7100 err = add_color(&s->colors, "^refs/got/backup/",
7101 TOG_COLOR_REFS_BACKUP,
7102 get_color_value("TOG_COLOR_REFS_BACKUP"));
7107 view->show = show_ref_view;
7108 view->input = input_ref_view;
7109 view->close = close_ref_view;
7110 view->search_start = search_start_ref_view;
7111 view->search_next = search_next_ref_view;
7114 free_colors(&s->colors);
7118 static const struct got_error *
7119 close_ref_view(struct tog_view *view)
7121 struct tog_ref_view_state *s = &view->state.ref;
7123 ref_view_free_refs(s);
7124 free_colors(&s->colors);
7129 static const struct got_error *
7130 resolve_reflist_entry(struct got_object_id **commit_id,
7131 struct tog_reflist_entry *re, struct got_repository *repo)
7133 const struct got_error *err = NULL;
7134 struct got_object_id *obj_id;
7135 struct got_tag_object *tag = NULL;
7140 err = got_ref_resolve(&obj_id, repo, re->ref);
7144 err = got_object_get_type(&obj_type, repo, obj_id);
7149 case GOT_OBJ_TYPE_COMMIT:
7150 *commit_id = obj_id;
7152 case GOT_OBJ_TYPE_TAG:
7153 err = got_object_open_as_tag(&tag, repo, obj_id);
7157 err = got_object_get_type(&obj_type, repo,
7158 got_object_tag_get_object_id(tag));
7161 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7162 err = got_error(GOT_ERR_OBJ_TYPE);
7165 *commit_id = got_object_id_dup(
7166 got_object_tag_get_object_id(tag));
7167 if (*commit_id == NULL) {
7168 err = got_error_from_errno("got_object_id_dup");
7173 err = got_error(GOT_ERR_OBJ_TYPE);
7179 got_object_tag_close(tag);
7187 static const struct got_error *
7188 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7189 struct tog_reflist_entry *re, struct got_repository *repo)
7191 struct tog_view *log_view;
7192 const struct got_error *err = NULL;
7193 struct got_object_id *commit_id = NULL;
7197 err = resolve_reflist_entry(&commit_id, re, repo);
7199 if (err->code != GOT_ERR_OBJ_TYPE)
7205 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7206 if (log_view == NULL) {
7207 err = got_error_from_errno("view_open");
7211 err = open_log_view(log_view, commit_id, repo,
7212 got_ref_get_name(re->ref), "", 0);
7215 view_close(log_view);
7217 *new_view = log_view;
7223 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7225 struct tog_reflist_entry *re;
7228 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7231 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7232 while (i++ < maxscroll) {
7235 s->first_displayed_entry = re;
7236 re = TAILQ_PREV(re, tog_reflist_head, entry);
7240 static const struct got_error *
7241 ref_scroll_down(struct tog_view *view, int maxscroll)
7243 struct tog_ref_view_state *s = &view->state.ref;
7244 struct tog_reflist_entry *next, *last;
7247 if (s->first_displayed_entry)
7248 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7250 next = TAILQ_FIRST(&s->refs);
7252 last = s->last_displayed_entry;
7253 while (next && n++ < maxscroll) {
7255 last = TAILQ_NEXT(last, entry);
7256 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7257 s->first_displayed_entry = next;
7258 next = TAILQ_NEXT(next, entry);
7265 static const struct got_error *
7266 search_start_ref_view(struct tog_view *view)
7268 struct tog_ref_view_state *s = &view->state.ref;
7270 s->matched_entry = NULL;
7275 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7277 regmatch_t regmatch;
7279 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7283 static const struct got_error *
7284 search_next_ref_view(struct tog_view *view)
7286 struct tog_ref_view_state *s = &view->state.ref;
7287 struct tog_reflist_entry *re = NULL;
7289 if (!view->searching) {
7290 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7294 if (s->matched_entry) {
7295 if (view->searching == TOG_SEARCH_FORWARD) {
7296 if (s->selected_entry)
7297 re = TAILQ_NEXT(s->selected_entry, entry);
7299 re = TAILQ_PREV(s->selected_entry,
7300 tog_reflist_head, entry);
7302 if (s->selected_entry == NULL)
7303 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7305 re = TAILQ_PREV(s->selected_entry,
7306 tog_reflist_head, entry);
7309 if (s->selected_entry)
7310 re = s->selected_entry;
7311 else if (view->searching == TOG_SEARCH_FORWARD)
7312 re = TAILQ_FIRST(&s->refs);
7314 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7319 if (s->matched_entry == NULL) {
7320 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7323 if (view->searching == TOG_SEARCH_FORWARD)
7324 re = TAILQ_FIRST(&s->refs);
7326 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7329 if (match_reflist_entry(re, &view->regex)) {
7330 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7331 s->matched_entry = re;
7335 if (view->searching == TOG_SEARCH_FORWARD)
7336 re = TAILQ_NEXT(re, entry);
7338 re = TAILQ_PREV(re, tog_reflist_head, entry);
7341 if (s->matched_entry) {
7342 s->first_displayed_entry = s->matched_entry;
7349 static const struct got_error *
7350 show_ref_view(struct tog_view *view)
7352 const struct got_error *err = NULL;
7353 struct tog_ref_view_state *s = &view->state.ref;
7354 struct tog_reflist_entry *re;
7357 struct tog_color *tc;
7359 int limit = view->nlines;
7361 werase(view->window);
7364 if (view_is_hsplit_top(view))
7365 --limit; /* border */
7370 re = s->first_displayed_entry;
7372 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7374 return got_error_from_errno("asprintf");
7376 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7381 if (view_needs_focus_indication(view))
7382 wstandout(view->window);
7383 waddwstr(view->window, wline);
7384 if (view_needs_focus_indication(view))
7385 wstandend(view->window);
7390 if (width < view->ncols - 1)
7391 waddch(view->window, '\n');
7396 while (re && limit > 0) {
7398 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7401 struct got_commit_object *ci;
7402 struct got_tag_object *tag;
7403 struct got_object_id *id;
7407 err = got_ref_resolve(&id, s->repo, re->ref);
7410 err = got_object_open_as_tag(&tag, s->repo, id);
7412 if (err->code != GOT_ERR_OBJ_TYPE) {
7416 err = got_object_open_as_commit(&ci, s->repo,
7422 t = got_object_commit_get_committer_time(ci);
7423 got_object_commit_close(ci);
7425 t = got_object_tag_get_tagger_time(tag);
7426 got_object_tag_close(tag);
7429 if (gmtime_r(&t, &tm) == NULL)
7430 return got_error_from_errno("gmtime_r");
7431 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7432 return got_error(GOT_ERR_NO_SPACE);
7434 if (got_ref_is_symbolic(re->ref)) {
7435 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7436 ymd : "", got_ref_get_name(re->ref),
7437 got_ref_get_symref_target(re->ref)) == -1)
7438 return got_error_from_errno("asprintf");
7439 } else if (s->show_ids) {
7440 struct got_object_id *id;
7442 err = got_ref_resolve(&id, s->repo, re->ref);
7445 err = got_object_id_str(&id_str, id);
7450 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7451 got_ref_get_name(re->ref), id_str) == -1) {
7452 err = got_error_from_errno("asprintf");
7459 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7460 got_ref_get_name(re->ref)) == -1)
7461 return got_error_from_errno("asprintf");
7463 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7469 if (n == s->selected) {
7471 wstandout(view->window);
7472 s->selected_entry = re;
7474 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7476 wattr_on(view->window,
7477 COLOR_PAIR(tc->colorpair), NULL);
7478 waddwstr(view->window, wline);
7480 wattr_off(view->window,
7481 COLOR_PAIR(tc->colorpair), NULL);
7482 if (width < view->ncols - 1)
7483 waddch(view->window, '\n');
7484 if (n == s->selected && view->focussed)
7485 wstandend(view->window);
7491 s->last_displayed_entry = re;
7494 re = TAILQ_NEXT(re, entry);
7501 static const struct got_error *
7502 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7503 struct tog_reflist_entry *re, struct got_repository *repo)
7505 const struct got_error *err = NULL;
7506 struct got_object_id *commit_id = NULL;
7507 struct tog_view *tree_view;
7511 err = resolve_reflist_entry(&commit_id, re, repo);
7513 if (err->code != GOT_ERR_OBJ_TYPE)
7520 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7521 if (tree_view == NULL) {
7522 err = got_error_from_errno("view_open");
7526 err = open_tree_view(tree_view, commit_id,
7527 got_ref_get_name(re->ref), repo);
7531 *new_view = tree_view;
7536 static const struct got_error *
7537 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7539 const struct got_error *err = NULL;
7540 struct tog_ref_view_state *s = &view->state.ref;
7541 struct tog_view *log_view, *tree_view;
7542 struct tog_reflist_entry *re;
7543 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7547 s->show_ids = !s->show_ids;
7551 s->show_date = !s->show_date;
7555 s->sort_by_date = !s->sort_by_date;
7557 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7558 got_ref_cmp_by_commit_timestamp_descending :
7559 tog_ref_cmp_by_name, s->repo);
7562 got_reflist_object_id_map_free(tog_refs_idmap);
7563 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7564 &tog_refs, s->repo);
7567 ref_view_free_refs(s);
7568 err = ref_view_load_refs(s);
7573 if (!s->selected_entry)
7575 if (view_is_parent_view(view))
7576 view_get_split(view, &begin_y, &begin_x);
7578 err = log_ref_entry(&log_view, begin_y, begin_x,
7579 s->selected_entry, s->repo);
7583 if (view_is_parent_view(view) &&
7584 view->mode == TOG_VIEW_SPLIT_HRZN) {
7585 err = view_init_hsplit(view, begin_y);
7591 log_view->focussed = 1;
7592 log_view->mode = view->mode;
7593 log_view->nlines = view->lines - begin_y;
7594 if (view_is_parent_view(view)) {
7595 view_transfer_size(log_view, view);
7596 err = view_close_child(view);
7599 err = view_set_child(view, log_view);
7602 view->focus_child = 1;
7604 *new_view = log_view;
7608 if (!s->selected_entry)
7610 if (view_is_parent_view(view))
7611 view_get_split(view, &begin_y, &begin_x);
7612 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7613 s->selected_entry, s->repo);
7614 if (err || tree_view == NULL)
7616 if (view_is_parent_view(view) &&
7617 view->mode == TOG_VIEW_SPLIT_HRZN) {
7618 err = view_init_hsplit(view, begin_y);
7623 tree_view->focussed = 1;
7624 tree_view->mode = view->mode;
7625 tree_view->nlines = view->lines - begin_y;
7626 if (view_is_parent_view(view)) {
7627 view_transfer_size(tree_view, view);
7628 err = view_close_child(view);
7631 err = view_set_child(view, tree_view);
7634 view->focus_child = 1;
7636 *new_view = tree_view;
7642 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7646 int eos = view->nlines - 1;
7648 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7652 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7653 for (n = 0; n < eos; n++) {
7656 s->first_displayed_entry = re;
7657 re = TAILQ_PREV(re, tog_reflist_head, entry);
7660 s->selected = n - 1;
7666 if (s->selected > 0) {
7670 ref_scroll_up(s, 1);
7671 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7681 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7682 s->selected -= MIN(nscroll, s->selected);
7683 ref_scroll_up(s, MAX(0, nscroll));
7684 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7690 if (s->selected < s->ndisplayed - 1) {
7694 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7695 /* can't scroll any further */
7699 ref_scroll_down(view, 1);
7709 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7710 /* can't scroll any further; move cursor down */
7711 if (s->selected < s->ndisplayed - 1)
7712 s->selected += MIN(nscroll,
7713 s->ndisplayed - s->selected - 1);
7714 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7715 s->selected += s->ndisplayed - s->selected - 1;
7719 ref_scroll_down(view, nscroll);
7724 err = tog_load_refs(s->repo, s->sort_by_date);
7727 ref_view_free_refs(s);
7728 err = ref_view_load_refs(s);
7731 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7732 s->selected = view->nlines - 2;
7746 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7751 static const struct got_error *
7752 cmd_ref(int argc, char *argv[])
7754 const struct got_error *error;
7755 struct got_repository *repo = NULL;
7756 struct got_worktree *worktree = NULL;
7757 char *cwd = NULL, *repo_path = NULL;
7759 struct tog_view *view;
7760 int *pack_fds = NULL;
7762 while ((ch = getopt(argc, argv, "r:")) != -1) {
7765 repo_path = realpath(optarg, NULL);
7766 if (repo_path == NULL)
7767 return got_error_from_errno2("realpath",
7782 error = got_repo_pack_fds_open(&pack_fds);
7786 if (repo_path == NULL) {
7787 cwd = getcwd(NULL, 0);
7789 return got_error_from_errno("getcwd");
7790 error = got_worktree_open(&worktree, cwd);
7791 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7795 strdup(got_worktree_get_repo_path(worktree));
7797 repo_path = strdup(cwd);
7798 if (repo_path == NULL) {
7799 error = got_error_from_errno("strdup");
7804 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7810 error = apply_unveil(got_repo_get_path(repo), NULL);
7814 error = tog_load_refs(repo, 0);
7818 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7820 error = got_error_from_errno("view_open");
7824 error = open_ref_view(view, repo);
7829 /* Release work tree lock. */
7830 got_worktree_close(worktree);
7833 error = view_loop(view);
7838 const struct got_error *close_err = got_repo_close(repo);
7843 const struct got_error *pack_err =
7844 got_repo_pack_fds_close(pack_fds);
7853 * If view was scrolled down to move the selected line into view when opening a
7854 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7857 offset_selection_up(struct tog_view *view)
7859 switch (view->type) {
7860 case TOG_VIEW_BLAME: {
7861 struct tog_blame_view_state *s = &view->state.blame;
7862 if (s->first_displayed_line == 1) {
7863 s->selected_line = MAX(s->selected_line - view->offset,
7867 if (s->first_displayed_line > view->offset)
7868 s->first_displayed_line -= view->offset;
7870 s->first_displayed_line = 1;
7871 s->selected_line += view->offset;
7875 log_scroll_up(&view->state.log, view->offset);
7876 view->state.log.selected += view->offset;
7879 ref_scroll_up(&view->state.ref, view->offset);
7880 view->state.ref.selected += view->offset;
7883 tree_scroll_up(&view->state.tree, view->offset);
7884 view->state.tree.selected += view->offset;
7894 * If the selected line is in the section of screen covered by the bottom split,
7895 * scroll down offset lines to move it into view and index its new position.
7897 static const struct got_error *
7898 offset_selection_down(struct tog_view *view)
7900 const struct got_error *err = NULL;
7901 const struct got_error *(*scrolld)(struct tog_view *, int);
7902 int *selected = NULL;
7905 switch (view->type) {
7906 case TOG_VIEW_BLAME: {
7907 struct tog_blame_view_state *s = &view->state.blame;
7910 if (s->selected_line > view->nlines - header) {
7911 offset = abs(view->nlines - s->selected_line - header);
7912 s->first_displayed_line += offset;
7913 s->selected_line -= offset;
7914 view->offset = offset;
7918 case TOG_VIEW_LOG: {
7919 struct tog_log_view_state *s = &view->state.log;
7920 scrolld = &log_scroll_down;
7921 header = view_is_parent_view(view) ? 3 : 2;
7922 selected = &s->selected;
7925 case TOG_VIEW_REF: {
7926 struct tog_ref_view_state *s = &view->state.ref;
7927 scrolld = &ref_scroll_down;
7929 selected = &s->selected;
7932 case TOG_VIEW_TREE: {
7933 struct tog_tree_view_state *s = &view->state.tree;
7934 scrolld = &tree_scroll_down;
7936 selected = &s->selected;
7946 if (selected && *selected > view->nlines - header) {
7947 offset = abs(view->nlines - *selected - header);
7948 view->offset = offset;
7949 if (scrolld && offset) {
7950 err = scrolld(view, offset);
7951 *selected -= offset;
7959 list_commands(FILE *fp)
7963 fprintf(fp, "commands:");
7964 for (i = 0; i < nitems(tog_commands); i++) {
7965 const struct tog_cmd *cmd = &tog_commands[i];
7966 fprintf(fp, " %s", cmd->name);
7972 usage(int hflag, int status)
7974 FILE *fp = (status == 0) ? stdout : stderr;
7976 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7979 fprintf(fp, "lazy usage: %s path\n", getprogname());
7986 make_argv(int argc, ...)
7994 argv = calloc(argc, sizeof(char *));
7997 for (i = 0; i < argc; i++) {
7998 argv[i] = strdup(va_arg(ap, char *));
7999 if (argv[i] == NULL)
8008 * Try to convert 'tog path' into a 'tog log path' command.
8009 * The user could simply have mistyped the command rather than knowingly
8010 * provided a path. So check whether argv[0] can in fact be resolved
8011 * to a path in the HEAD commit and print a special error if not.
8012 * This hack is for mpi@ <3
8014 static const struct got_error *
8015 tog_log_with_path(int argc, char *argv[])
8017 const struct got_error *error = NULL, *close_err;
8018 const struct tog_cmd *cmd = NULL;
8019 struct got_repository *repo = NULL;
8020 struct got_worktree *worktree = NULL;
8021 struct got_object_id *commit_id = NULL, *id = NULL;
8022 struct got_commit_object *commit = NULL;
8023 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8024 char *commit_id_str = NULL, **cmd_argv = NULL;
8025 int *pack_fds = NULL;
8027 cwd = getcwd(NULL, 0);
8029 return got_error_from_errno("getcwd");
8031 error = got_repo_pack_fds_open(&pack_fds);
8035 error = got_worktree_open(&worktree, cwd);
8036 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8040 repo_path = strdup(got_worktree_get_repo_path(worktree));
8042 repo_path = strdup(cwd);
8043 if (repo_path == NULL) {
8044 error = got_error_from_errno("strdup");
8048 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8052 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8057 error = tog_load_refs(repo, 0);
8060 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8061 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8062 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8067 got_worktree_close(worktree);
8071 error = got_object_open_as_commit(&commit, repo, commit_id);
8075 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8077 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8079 fprintf(stderr, "%s: '%s' is no known command or path\n",
8080 getprogname(), argv[0]);
8085 close_err = got_repo_close(repo);
8090 error = got_object_id_str(&commit_id_str, commit_id);
8094 cmd = &tog_commands[0]; /* log */
8096 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8097 error = cmd->cmd_main(argc, cmd_argv);
8100 close_err = got_repo_close(repo);
8105 got_object_commit_close(commit);
8107 got_worktree_close(worktree);
8109 const struct got_error *pack_err =
8110 got_repo_pack_fds_close(pack_fds);
8115 free(commit_id_str);
8122 for (i = 0; i < argc; i++)
8131 main(int argc, char *argv[])
8133 const struct got_error *error = NULL;
8134 const struct tog_cmd *cmd = NULL;
8135 int ch, hflag = 0, Vflag = 0;
8136 char **cmd_argv = NULL;
8137 static const struct option longopts[] = {
8138 { "version", no_argument, NULL, 'V' },
8141 char *diff_algo_str = NULL;
8143 setlocale(LC_CTYPE, "");
8145 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8165 got_version_print_str();
8170 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8178 /* Build an argument vector which runs a default command. */
8179 cmd = &tog_commands[0];
8181 cmd_argv = make_argv(argc, cmd->name);
8185 /* Did the user specify a command? */
8186 for (i = 0; i < nitems(tog_commands); i++) {
8187 if (strncmp(tog_commands[i].name, argv[0],
8188 strlen(argv[0])) == 0) {
8189 cmd = &tog_commands[i];
8195 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8196 if (diff_algo_str) {
8197 if (strcasecmp(diff_algo_str, "patience") == 0)
8198 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8199 if (strcasecmp(diff_algo_str, "myers") == 0)
8200 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8206 /* No command specified; try log with a path */
8207 error = tog_log_with_path(argc, argv);
8212 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8219 for (i = 0; i < argc; i++)
8224 if (error && error->code != GOT_ERR_CANCELLED)
8225 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);