2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define CTRL(x) ((x) & 0x1f)
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
119 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
121 #define TOG_EOF_STRING "(END)"
123 struct commit_queue_entry {
124 TAILQ_ENTRY(commit_queue_entry) entry;
125 struct got_object_id *id;
126 struct got_commit_object *commit;
129 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 struct commit_queue {
132 struct commit_queue_head head;
136 STAILQ_ENTRY(tog_color) entry;
140 STAILQ_HEAD(tog_colors, tog_color);
142 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 static struct got_reflist_object_id_map *tog_refs_idmap;
144 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
146 static const struct got_error *
147 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 struct got_reference* re2)
150 const char *name1 = got_ref_get_name(re1);
151 const char *name2 = got_ref_get_name(re2);
152 int isbackup1, isbackup2;
154 /* Sort backup refs towards the bottom of the list. */
155 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 if (!isbackup1 && isbackup2) {
160 } else if (isbackup1 && !isbackup2) {
165 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
169 static const struct got_error *
170 tog_load_refs(struct got_repository *repo, int sort_by_date)
172 const struct got_error *err;
174 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
180 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
187 if (tog_refs_idmap) {
188 got_reflist_object_id_map_free(tog_refs_idmap);
189 tog_refs_idmap = NULL;
191 got_ref_list_free(&tog_refs);
194 static const struct got_error *
195 add_color(struct tog_colors *colors, const char *pattern,
196 int idx, short color)
198 const struct got_error *err = NULL;
199 struct tog_color *tc;
202 if (idx < 1 || idx > COLOR_PAIRS - 1)
205 init_pair(idx, color, -1);
207 tc = calloc(1, sizeof(*tc));
209 return got_error_from_errno("calloc");
210 regerr = regcomp(&tc->regex, pattern,
211 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
213 static char regerr_msg[512];
214 static char err_msg[512];
215 regerror(regerr, &tc->regex, regerr_msg,
217 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
219 err = got_error_msg(GOT_ERR_REGEX, err_msg);
224 STAILQ_INSERT_HEAD(colors, tc, entry);
229 free_colors(struct tog_colors *colors)
231 struct tog_color *tc;
233 while (!STAILQ_EMPTY(colors)) {
234 tc = STAILQ_FIRST(colors);
235 STAILQ_REMOVE_HEAD(colors, entry);
241 static struct tog_color *
242 get_color(struct tog_colors *colors, int colorpair)
244 struct tog_color *tc = NULL;
246 STAILQ_FOREACH(tc, colors, entry) {
247 if (tc->colorpair == colorpair)
255 default_color_value(const char *envvar)
257 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 return COLOR_MAGENTA;
259 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
261 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
263 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
265 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
271 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
273 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
275 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
277 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
279 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
281 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
285 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
292 get_color_value(const char *envvar)
294 const char *val = getenv(envvar);
297 return default_color_value(envvar);
299 if (strcasecmp(val, "black") == 0)
301 if (strcasecmp(val, "red") == 0)
303 if (strcasecmp(val, "green") == 0)
305 if (strcasecmp(val, "yellow") == 0)
307 if (strcasecmp(val, "blue") == 0)
309 if (strcasecmp(val, "magenta") == 0)
310 return COLOR_MAGENTA;
311 if (strcasecmp(val, "cyan") == 0)
313 if (strcasecmp(val, "white") == 0)
315 if (strcasecmp(val, "default") == 0)
318 return default_color_value(envvar);
322 struct tog_diff_view_state {
323 struct got_object_id *id1, *id2;
324 const char *label1, *label2;
327 int first_displayed_line;
328 int last_displayed_line;
331 int ignore_whitespace;
333 struct got_repository *repo;
334 struct tog_colors colors;
340 /* passed from log or blame view; may be NULL */
341 struct tog_view *parent_view;
344 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 static volatile sig_atomic_t tog_thread_error;
347 struct tog_log_thread_args {
348 pthread_cond_t need_commits;
349 pthread_cond_t commit_loaded;
352 struct got_commit_graph *graph;
353 struct commit_queue *commits;
354 const char *in_repo_path;
355 struct got_object_id *start_id;
356 struct got_repository *repo;
360 struct commit_queue_entry **first_displayed_entry;
361 struct commit_queue_entry **selected_entry;
363 int *search_next_done;
367 struct tog_log_view_state {
368 struct commit_queue commits;
369 struct commit_queue_entry *first_displayed_entry;
370 struct commit_queue_entry *last_displayed_entry;
371 struct commit_queue_entry *selected_entry;
376 struct got_repository *repo;
377 struct got_object_id *start_id;
380 struct tog_log_thread_args thread_args;
381 struct commit_queue_entry *matched_entry;
382 struct commit_queue_entry *search_entry;
383 struct tog_colors colors;
387 #define TOG_COLOR_DIFF_MINUS 1
388 #define TOG_COLOR_DIFF_PLUS 2
389 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 #define TOG_COLOR_DIFF_META 4
391 #define TOG_COLOR_TREE_SUBMODULE 5
392 #define TOG_COLOR_TREE_SYMLINK 6
393 #define TOG_COLOR_TREE_DIRECTORY 7
394 #define TOG_COLOR_TREE_EXECUTABLE 8
395 #define TOG_COLOR_COMMIT 9
396 #define TOG_COLOR_AUTHOR 10
397 #define TOG_COLOR_DATE 11
398 #define TOG_COLOR_REFS_HEADS 12
399 #define TOG_COLOR_REFS_TAGS 13
400 #define TOG_COLOR_REFS_REMOTES 14
401 #define TOG_COLOR_REFS_BACKUP 15
403 struct tog_blame_cb_args {
404 struct tog_blame_line *lines; /* one per line */
407 struct tog_view *view;
408 struct got_object_id *commit_id;
412 struct tog_blame_thread_args {
414 struct got_repository *repo;
415 struct tog_blame_cb_args *cb_args;
417 got_cancel_cb cancel_cb;
424 struct tog_blame_line *lines;
428 struct tog_blame_thread_args thread_args;
429 struct tog_blame_cb_args cb_args;
434 struct tog_blame_view_state {
435 int first_displayed_line;
436 int last_displayed_line;
438 int last_diffed_line;
442 struct got_object_id_queue blamed_commits;
443 struct got_object_qid *blamed_commit;
445 struct got_repository *repo;
446 struct got_object_id *commit_id;
447 struct got_object_id *id_to_log;
448 struct tog_blame blame;
450 struct tog_colors colors;
453 struct tog_parent_tree {
454 TAILQ_ENTRY(tog_parent_tree) entry;
455 struct got_tree_object *tree;
456 struct got_tree_entry *first_displayed_entry;
457 struct got_tree_entry *selected_entry;
461 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
463 struct tog_tree_view_state {
465 struct got_object_id *commit_id;/* commit which this tree belongs to */
466 struct got_tree_object *root; /* the commit's root tree entry */
467 struct got_tree_object *tree; /* currently displayed (sub-)tree */
468 struct got_tree_entry *first_displayed_entry;
469 struct got_tree_entry *last_displayed_entry;
470 struct got_tree_entry *selected_entry;
471 int ndisplayed, selected, show_ids;
472 struct tog_parent_trees parents; /* parent trees of current sub-tree */
474 struct got_repository *repo;
475 struct got_tree_entry *matched_entry;
476 struct tog_colors colors;
479 struct tog_reflist_entry {
480 TAILQ_ENTRY(tog_reflist_entry) entry;
481 struct got_reference *ref;
485 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
487 struct tog_ref_view_state {
488 struct tog_reflist_head refs;
489 struct tog_reflist_entry *first_displayed_entry;
490 struct tog_reflist_entry *last_displayed_entry;
491 struct tog_reflist_entry *selected_entry;
492 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
493 struct got_repository *repo;
494 struct tog_reflist_entry *matched_entry;
495 struct tog_colors colors;
499 * We implement two types of views: parent views and child views.
501 * The 'Tab' key switches focus between a parent view and its child view.
502 * Child views are shown side-by-side to their parent view, provided
503 * there is enough screen estate.
505 * When a new view is opened from within a parent view, this new view
506 * becomes a child view of the parent view, replacing any existing child.
508 * When a new view is opened from within a child view, this new view
509 * becomes a parent view which will obscure the views below until the
510 * user quits the new parent view by typing 'q'.
512 * This list of views contains parent views only.
513 * Child views are only pointed to by their parent view.
515 TAILQ_HEAD(tog_view_list_head, tog_view);
518 TAILQ_ENTRY(tog_view) entry;
521 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
522 int resized_y, resized_x; /* begin_y/x based on user resizing */
523 int maxx, x; /* max column and current start column */
524 int lines, cols; /* copies of LINES and COLS */
525 int nscrolled, offset; /* lines scrolled and hsplit line offset */
526 int ch, count; /* current keymap and count prefix */
527 int resized; /* set when in a resize event */
528 int focussed; /* Only set on one parent or child view at a time. */
530 struct tog_view *parent;
531 struct tog_view *child;
534 * This flag is initially set on parent views when a new child view
535 * is created. It gets toggled when the 'Tab' key switches focus
536 * between parent and child.
537 * The flag indicates whether focus should be passed on to our child
538 * view if this parent view gets picked for focus after another parent
539 * view was closed. This prevents child views from losing focus in such
544 enum tog_view_mode mode;
545 /* type-specific state */
546 enum tog_view_type type;
548 struct tog_diff_view_state diff;
549 struct tog_log_view_state log;
550 struct tog_blame_view_state blame;
551 struct tog_tree_view_state tree;
552 struct tog_ref_view_state ref;
555 const struct got_error *(*show)(struct tog_view *);
556 const struct got_error *(*input)(struct tog_view **,
557 struct tog_view *, int);
558 const struct got_error *(*reset)(struct tog_view *);
559 const struct got_error *(*resize)(struct tog_view *, int);
560 const struct got_error *(*close)(struct tog_view *);
562 const struct got_error *(*search_start)(struct tog_view *);
563 const struct got_error *(*search_next)(struct tog_view *);
566 #define TOG_SEARCH_FORWARD 1
567 #define TOG_SEARCH_BACKWARD 2
568 int search_next_done;
569 #define TOG_SEARCH_HAVE_MORE 1
570 #define TOG_SEARCH_NO_MORE 2
571 #define TOG_SEARCH_HAVE_NONE 3
576 static const struct got_error *open_diff_view(struct tog_view *,
577 struct got_object_id *, struct got_object_id *,
578 const char *, const char *, int, int, int, struct tog_view *,
579 struct got_repository *);
580 static const struct got_error *show_diff_view(struct tog_view *);
581 static const struct got_error *input_diff_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *reset_diff_view(struct tog_view *);
584 static const struct got_error* close_diff_view(struct tog_view *);
585 static const struct got_error *search_start_diff_view(struct tog_view *);
586 static const struct got_error *search_next_diff_view(struct tog_view *);
588 static const struct got_error *open_log_view(struct tog_view *,
589 struct got_object_id *, struct got_repository *,
590 const char *, const char *, int);
591 static const struct got_error * show_log_view(struct tog_view *);
592 static const struct got_error *input_log_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *resize_log_view(struct tog_view *, int);
595 static const struct got_error *close_log_view(struct tog_view *);
596 static const struct got_error *search_start_log_view(struct tog_view *);
597 static const struct got_error *search_next_log_view(struct tog_view *);
599 static const struct got_error *open_blame_view(struct tog_view *, char *,
600 struct got_object_id *, struct got_repository *);
601 static const struct got_error *show_blame_view(struct tog_view *);
602 static const struct got_error *input_blame_view(struct tog_view **,
603 struct tog_view *, int);
604 static const struct got_error *reset_blame_view(struct tog_view *);
605 static const struct got_error *close_blame_view(struct tog_view *);
606 static const struct got_error *search_start_blame_view(struct tog_view *);
607 static const struct got_error *search_next_blame_view(struct tog_view *);
609 static const struct got_error *open_tree_view(struct tog_view *,
610 struct got_object_id *, const char *, struct got_repository *);
611 static const struct got_error *show_tree_view(struct tog_view *);
612 static const struct got_error *input_tree_view(struct tog_view **,
613 struct tog_view *, int);
614 static const struct got_error *close_tree_view(struct tog_view *);
615 static const struct got_error *search_start_tree_view(struct tog_view *);
616 static const struct got_error *search_next_tree_view(struct tog_view *);
618 static const struct got_error *open_ref_view(struct tog_view *,
619 struct got_repository *);
620 static const struct got_error *show_ref_view(struct tog_view *);
621 static const struct got_error *input_ref_view(struct tog_view **,
622 struct tog_view *, int);
623 static const struct got_error *close_ref_view(struct tog_view *);
624 static const struct got_error *search_start_ref_view(struct tog_view *);
625 static const struct got_error *search_next_ref_view(struct tog_view *);
627 static volatile sig_atomic_t tog_sigwinch_received;
628 static volatile sig_atomic_t tog_sigpipe_received;
629 static volatile sig_atomic_t tog_sigcont_received;
630 static volatile sig_atomic_t tog_sigint_received;
631 static volatile sig_atomic_t tog_sigterm_received;
634 tog_sigwinch(int signo)
636 tog_sigwinch_received = 1;
640 tog_sigpipe(int signo)
642 tog_sigpipe_received = 1;
646 tog_sigcont(int signo)
648 tog_sigcont_received = 1;
652 tog_sigint(int signo)
654 tog_sigint_received = 1;
658 tog_sigterm(int signo)
660 tog_sigterm_received = 1;
664 tog_fatal_signal_received(void)
666 return (tog_sigpipe_received ||
667 tog_sigint_received || tog_sigint_received);
670 static const struct got_error *
671 view_close(struct tog_view *view)
673 const struct got_error *err = NULL, *child_err = NULL;
676 child_err = view_close(view->child);
680 err = view->close(view);
682 del_panel(view->panel);
684 delwin(view->window);
686 return err ? err : child_err;
689 static struct tog_view *
690 view_open(int nlines, int ncols, int begin_y, int begin_x,
691 enum tog_view_type type)
693 struct tog_view *view = calloc(1, sizeof(*view));
701 view->nlines = nlines ? nlines : LINES - begin_y;
702 view->ncols = ncols ? ncols : COLS - begin_x;
703 view->begin_y = begin_y;
704 view->begin_x = begin_x;
705 view->window = newwin(nlines, ncols, begin_y, begin_x);
706 if (view->window == NULL) {
710 view->panel = new_panel(view->window);
711 if (view->panel == NULL ||
712 set_panel_userptr(view->panel, view) != OK) {
717 keypad(view->window, TRUE);
722 view_split_begin_x(int begin_x)
724 if (begin_x > 0 || COLS < 120)
726 return (COLS - MAX(COLS / 2, 80));
729 /* XXX Stub till we decide what to do. */
731 view_split_begin_y(int lines)
733 return lines * HSPLIT_SCALE;
736 static const struct got_error *view_resize(struct tog_view *);
738 static const struct got_error *
739 view_splitscreen(struct tog_view *view)
741 const struct got_error *err = NULL;
743 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
744 if (view->resized_y && view->resized_y < view->lines)
745 view->begin_y = view->resized_y;
747 view->begin_y = view_split_begin_y(view->nlines);
749 } else if (!view->resized) {
750 if (view->resized_x && view->resized_x < view->cols - 1 &&
752 view->begin_x = view->resized_x;
754 view->begin_x = view_split_begin_x(0);
757 view->nlines = LINES - view->begin_y;
758 view->ncols = COLS - view->begin_x;
761 err = view_resize(view);
765 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
766 view->parent->nlines = view->begin_y;
768 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
769 return got_error_from_errno("mvwin");
774 static const struct got_error *
775 view_fullscreen(struct tog_view *view)
777 const struct got_error *err = NULL;
780 view->begin_y = view->resized ? view->begin_y : 0;
781 view->nlines = view->resized ? view->nlines : LINES;
785 err = view_resize(view);
789 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
790 return got_error_from_errno("mvwin");
796 view_is_parent_view(struct tog_view *view)
798 return view->parent == NULL;
802 view_is_splitscreen(struct tog_view *view)
804 return view->begin_x > 0 || view->begin_y > 0;
808 view_is_fullscreen(struct tog_view *view)
810 return view->nlines == LINES && view->ncols == COLS;
814 view_is_hsplit_top(struct tog_view *view)
816 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
817 view_is_splitscreen(view->child);
821 view_border(struct tog_view *view)
824 const struct tog_view *view_above;
827 return view_border(view->parent);
829 panel = panel_above(view->panel);
833 view_above = panel_userptr(panel);
834 if (view->mode == TOG_VIEW_SPLIT_HRZN)
835 mvwhline(view->window, view_above->begin_y - 1,
836 view->begin_x, got_locale_is_utf8() ?
837 ACS_HLINE : '-', view->ncols);
839 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
840 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
843 static const struct got_error *view_init_hsplit(struct tog_view *, int);
844 static const struct got_error *request_log_commits(struct tog_view *);
845 static const struct got_error *offset_selection_down(struct tog_view *);
846 static void offset_selection_up(struct tog_view *);
847 static void view_get_split(struct tog_view *, int *, int *);
849 static const struct got_error *
850 view_resize(struct tog_view *view)
852 const struct got_error *err = NULL;
853 int dif, nlines, ncols;
855 dif = LINES - view->lines; /* line difference */
857 if (view->lines > LINES)
858 nlines = view->nlines - (view->lines - LINES);
860 nlines = view->nlines + (LINES - view->lines);
861 if (view->cols > COLS)
862 ncols = view->ncols - (view->cols - COLS);
864 ncols = view->ncols + (COLS - view->cols);
867 int hs = view->child->begin_y;
869 if (!view_is_fullscreen(view))
870 view->child->begin_x = view_split_begin_x(view->begin_x);
871 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
872 view->child->begin_x == 0) {
875 view_fullscreen(view->child);
876 if (view->child->focussed)
877 show_panel(view->child->panel);
879 show_panel(view->panel);
881 ncols = view->child->begin_x;
883 view_splitscreen(view->child);
884 show_panel(view->child->panel);
887 * XXX This is ugly and needs to be moved into the above
888 * logic but "works" for now and my attempts at moving it
889 * break either 'tab' or 'F' key maps in horizontal splits.
892 err = view_splitscreen(view->child);
895 if (dif < 0) { /* top split decreased */
896 err = offset_selection_down(view);
903 show_panel(view->child->panel);
904 nlines = view->nlines;
906 } else if (view->parent == NULL)
909 if (view->resize && dif > 0) {
910 err = view->resize(view, dif);
915 if (wresize(view->window, nlines, ncols) == ERR)
916 return got_error_from_errno("wresize");
917 if (replace_panel(view->panel, view->window) == ERR)
918 return got_error_from_errno("replace_panel");
919 wclear(view->window);
921 view->nlines = nlines;
929 static const struct got_error *
930 resize_log_view(struct tog_view *view, int increase)
932 struct tog_log_view_state *s = &view->state.log;
933 const struct got_error *err = NULL;
936 if (s->selected_entry)
937 n = s->selected_entry->idx + view->lines - s->selected;
940 * Request commits to account for the increased
941 * height so we have enough to populate the view.
943 if (s->commits.ncommits < n) {
944 view->nscrolled = n - s->commits.ncommits + increase + 1;
945 err = request_log_commits(view);
952 view_adjust_offset(struct tog_view *view, int n)
957 if (view->parent && view->parent->offset) {
958 if (view->parent->offset + n >= 0)
959 view->parent->offset += n;
961 view->parent->offset = 0;
962 } else if (view->offset) {
963 if (view->offset - n >= 0)
970 static const struct got_error *
971 view_resize_split(struct tog_view *view, int resize)
973 const struct got_error *err = NULL;
974 struct tog_view *v = NULL;
981 if (!v->child || !view_is_splitscreen(v->child))
984 v->resized = v->child->resized = resize; /* lock for resize event */
986 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
987 if (v->child->resized_y)
988 v->child->begin_y = v->child->resized_y;
990 v->child->begin_y -= resize;
992 v->child->begin_y += resize;
993 if (v->child->begin_y < 3) {
995 v->child->begin_y = 3;
996 } else if (v->child->begin_y > LINES - 1) {
998 v->child->begin_y = LINES - 1;
1001 v->child->ncols = COLS;
1002 view_adjust_offset(view, resize);
1003 err = view_init_hsplit(v, v->child->begin_y);
1006 v->child->resized_y = v->child->begin_y;
1008 if (v->child->resized_x)
1009 v->child->begin_x = v->child->resized_x;
1011 v->child->begin_x -= resize;
1013 v->child->begin_x += resize;
1014 if (v->child->begin_x < 11) {
1016 v->child->begin_x = 11;
1017 } else if (v->child->begin_x > COLS - 1) {
1019 v->child->begin_x = COLS - 1;
1021 v->child->resized_x = v->child->begin_x;
1024 v->child->mode = v->mode;
1025 v->child->nlines = v->lines - v->child->begin_y;
1026 v->child->ncols = v->cols - v->child->begin_x;
1029 err = view_fullscreen(v);
1032 err = view_splitscreen(v->child);
1036 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1037 err = offset_selection_down(v->child);
1043 err = v->resize(v, 0);
1044 else if (v->child->resize)
1045 err = v->child->resize(v->child, 0);
1047 v->resized = v->child->resized = 0;
1053 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1055 struct tog_view *v = src->child ? src->child : src;
1057 dst->resized_x = v->resized_x;
1058 dst->resized_y = v->resized_y;
1061 static const struct got_error *
1062 view_close_child(struct tog_view *view)
1064 const struct got_error *err = NULL;
1066 if (view->child == NULL)
1069 err = view_close(view->child);
1074 static const struct got_error *
1075 view_set_child(struct tog_view *view, struct tog_view *child)
1077 const struct got_error *err = NULL;
1079 view->child = child;
1080 child->parent = view;
1082 err = view_resize(view);
1086 if (view->child->resized_x || view->child->resized_y)
1087 err = view_resize_split(view, 0);
1092 static const struct got_error *view_dispatch_request(struct tog_view **,
1093 struct tog_view *, enum tog_view_type, int, int);
1095 static const struct got_error *
1096 view_request_new(struct tog_view **requested, struct tog_view *view,
1097 enum tog_view_type request)
1099 struct tog_view *new_view = NULL;
1100 const struct got_error *err;
1105 if (view_is_parent_view(view))
1106 view_get_split(view, &y, &x);
1108 err = view_dispatch_request(&new_view, view, request, y, x);
1112 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1113 err = view_init_hsplit(view, y);
1119 new_view->focussed = 1;
1120 new_view->mode = view->mode;
1121 new_view->nlines = view->lines - y;
1123 if (view_is_parent_view(view)) {
1124 view_transfer_size(new_view, view);
1125 err = view_close_child(view);
1128 err = view_set_child(view, new_view);
1131 view->focus_child = 1;
1133 *requested = new_view;
1139 tog_resizeterm(void)
1142 struct winsize size;
1144 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1145 cols = 80; /* Default */
1149 lines = size.ws_row;
1151 resize_term(lines, cols);
1154 static const struct got_error *
1155 view_search_start(struct tog_view *view)
1157 const struct got_error *err = NULL;
1158 struct tog_view *v = view;
1162 if (view->search_started) {
1163 regfree(&view->regex);
1164 view->searching = 0;
1165 memset(&view->regmatch, 0, sizeof(view->regmatch));
1167 view->search_started = 0;
1169 if (view->nlines < 1)
1172 if (view_is_hsplit_top(view))
1175 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1176 wclrtoeol(v->window);
1178 nodelay(view->window, FALSE); /* block for search term input */
1181 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1182 wrefresh(v->window);
1185 nodelay(view->window, TRUE);
1189 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1190 err = view->search_start(view);
1192 regfree(&view->regex);
1195 view->search_started = 1;
1196 view->searching = TOG_SEARCH_FORWARD;
1197 view->search_next_done = 0;
1198 view->search_next(view);
1204 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1205 static const struct got_error *
1206 switch_split(struct tog_view *view)
1208 const struct got_error *err = NULL;
1209 struct tog_view *v = NULL;
1216 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1217 v->mode = TOG_VIEW_SPLIT_VERT;
1219 v->mode = TOG_VIEW_SPLIT_HRZN;
1223 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1224 v->mode = TOG_VIEW_SPLIT_NONE;
1226 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1227 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1228 v->child->begin_y = v->child->resized_y;
1229 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1230 v->child->begin_x = v->child->resized_x;
1233 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1235 v->child->ncols = COLS;
1236 v->child->nscrolled = LINES - v->child->nlines;
1238 err = view_init_hsplit(v, v->child->begin_y);
1242 v->child->mode = v->mode;
1243 v->child->nlines = v->lines - v->child->begin_y;
1246 err = view_fullscreen(v);
1249 err = view_splitscreen(v->child);
1253 if (v->mode == TOG_VIEW_SPLIT_NONE)
1254 v->mode = TOG_VIEW_SPLIT_VERT;
1255 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1256 err = offset_selection_down(v);
1257 err = offset_selection_down(v->child);
1259 offset_selection_up(v);
1260 offset_selection_up(v->child);
1263 err = v->resize(v, 0);
1264 else if (v->child->resize)
1265 err = v->child->resize(v->child, 0);
1271 * Compute view->count from numeric input. Assign total to view->count and
1272 * return first non-numeric key entered.
1275 get_compound_key(struct tog_view *view, int c)
1277 struct tog_view *v = view;
1280 if (view_is_hsplit_top(view))
1282 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1286 cbreak(); /* block for input */
1287 wmove(v->window, v->nlines - 1, 0);
1288 wclrtoeol(v->window);
1289 waddch(v->window, ':');
1292 x = getcurx(v->window);
1293 if (x != ERR && x < view->ncols) {
1294 waddch(v->window, c);
1295 wrefresh(v->window);
1299 * Don't overflow. Max valid request should be the greatest
1300 * between the longest and total lines; cap at 10 million.
1305 n = n * 10 + (c - '0');
1306 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1308 /* Massage excessive or inapplicable values at the input handler. */
1314 static const struct got_error *
1315 view_input(struct tog_view **new, int *done, struct tog_view *view,
1316 struct tog_view_list_head *views)
1318 const struct got_error *err = NULL;
1324 /* Clear "no matches" indicator. */
1325 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1326 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1327 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1331 if (view->searching && !view->search_next_done) {
1332 errcode = pthread_mutex_unlock(&tog_mutex);
1334 return got_error_set_errno(errcode,
1335 "pthread_mutex_unlock");
1337 errcode = pthread_mutex_lock(&tog_mutex);
1339 return got_error_set_errno(errcode,
1340 "pthread_mutex_lock");
1341 view->search_next(view);
1345 nodelay(view->window, FALSE);
1346 /* Allow threads to make progress while we are waiting for input. */
1347 errcode = pthread_mutex_unlock(&tog_mutex);
1349 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1350 /* If we have an unfinished count, let C-g or backspace abort. */
1351 if (view->count && --view->count) {
1353 nodelay(view->window, TRUE);
1354 ch = wgetch(view->window);
1355 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1360 ch = wgetch(view->window);
1361 if (ch >= '1' && ch <= '9')
1362 view->ch = ch = get_compound_key(view, ch);
1364 errcode = pthread_mutex_lock(&tog_mutex);
1366 return got_error_set_errno(errcode, "pthread_mutex_lock");
1367 nodelay(view->window, TRUE);
1369 if (tog_sigwinch_received || tog_sigcont_received) {
1371 tog_sigwinch_received = 0;
1372 tog_sigcont_received = 0;
1373 TAILQ_FOREACH(v, views, entry) {
1374 err = view_resize(v);
1377 err = v->input(new, v, KEY_RESIZE);
1381 err = view_resize(v->child);
1384 err = v->child->input(new, v->child,
1388 if (v->child->resized_x || v->child->resized_y) {
1389 err = view_resize_split(v, 0);
1402 view->child->focussed = 1;
1403 view->focus_child = 1;
1404 } else if (view->parent) {
1406 view->parent->focussed = 1;
1407 view->parent->focus_child = 0;
1408 if (!view_is_splitscreen(view)) {
1409 if (view->parent->resize) {
1410 err = view->parent->resize(view->parent,
1415 offset_selection_up(view->parent);
1416 err = view_fullscreen(view->parent);
1423 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1424 if (view->parent->resize) {
1425 /* might need more commits to fill fullscreen */
1426 err = view->parent->resize(view->parent, 0);
1430 offset_selection_up(view->parent);
1432 err = view->input(new, view, ch);
1440 if (view_is_parent_view(view)) {
1441 if (view->child == NULL)
1443 if (view_is_splitscreen(view->child)) {
1445 view->child->focussed = 1;
1446 err = view_fullscreen(view->child);
1448 err = view_splitscreen(view->child);
1450 err = view_resize_split(view, 0);
1454 err = view->child->input(new, view->child,
1457 if (view_is_splitscreen(view)) {
1458 view->parent->focussed = 0;
1460 err = view_fullscreen(view);
1462 err = view_splitscreen(view);
1463 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1464 err = view_resize(view->parent);
1466 err = view_resize_split(view, 0);
1470 err = view->input(new, view, KEY_RESIZE);
1475 err = view->resize(view, 0);
1480 err = offset_selection_down(view->parent);
1482 err = offset_selection_down(view);
1486 err = switch_split(view);
1489 err = view_resize_split(view, -1);
1492 err = view_resize_split(view, 1);
1498 if (view->search_start)
1499 view_search_start(view);
1501 err = view->input(new, view, ch);
1505 if (view->search_started && view->search_next) {
1506 view->searching = (ch == 'n' ?
1507 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1508 view->search_next_done = 0;
1509 view->search_next(view);
1511 err = view->input(new, view, ch);
1514 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1515 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1517 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1518 TAILQ_FOREACH(v, views, entry) {
1524 if (v->child && v->child->reset) {
1525 err = v->child->reset(v->child);
1532 err = view->input(new, view, ch);
1540 view_needs_focus_indication(struct tog_view *view)
1542 if (view_is_parent_view(view)) {
1543 if (view->child == NULL || view->child->focussed)
1545 if (!view_is_splitscreen(view->child))
1547 } else if (!view_is_splitscreen(view))
1550 return view->focussed;
1553 static const struct got_error *
1554 view_loop(struct tog_view *view)
1556 const struct got_error *err = NULL;
1557 struct tog_view_list_head views;
1558 struct tog_view *new_view;
1560 int fast_refresh = 10;
1561 int done = 0, errcode;
1563 mode = getenv("TOG_VIEW_SPLIT_MODE");
1564 if (!mode || !(*mode == 'h' || *mode == 'H'))
1565 view->mode = TOG_VIEW_SPLIT_VERT;
1567 view->mode = TOG_VIEW_SPLIT_HRZN;
1569 errcode = pthread_mutex_lock(&tog_mutex);
1571 return got_error_set_errno(errcode, "pthread_mutex_lock");
1574 TAILQ_INSERT_HEAD(&views, view, entry);
1577 err = view->show(view);
1582 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1583 !tog_fatal_signal_received()) {
1584 /* Refresh fast during initialization, then become slower. */
1585 if (fast_refresh && fast_refresh-- == 0)
1586 halfdelay(10); /* switch to once per second */
1588 err = view_input(&new_view, &done, view, &views);
1592 struct tog_view *v, *prev = NULL;
1594 if (view_is_parent_view(view))
1595 prev = TAILQ_PREV(view, tog_view_list_head,
1597 else if (view->parent)
1598 prev = view->parent;
1601 view->parent->child = NULL;
1602 view->parent->focus_child = 0;
1603 /* Restore fullscreen line height. */
1604 view->parent->nlines = view->parent->lines;
1605 err = view_resize(view->parent);
1608 /* Make resized splits persist. */
1609 view_transfer_size(view->parent, view);
1611 TAILQ_REMOVE(&views, view, entry);
1613 err = view_close(view);
1618 TAILQ_FOREACH(v, &views, entry) {
1622 if (view == NULL && new_view == NULL) {
1623 /* No view has focus. Try to pick one. */
1626 else if (!TAILQ_EMPTY(&views)) {
1627 view = TAILQ_LAST(&views,
1628 tog_view_list_head);
1631 if (view->focus_child) {
1632 view->child->focussed = 1;
1640 struct tog_view *v, *t;
1641 /* Only allow one parent view per type. */
1642 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1643 if (v->type != new_view->type)
1645 TAILQ_REMOVE(&views, v, entry);
1646 err = view_close(v);
1651 TAILQ_INSERT_TAIL(&views, new_view, entry);
1655 if (view_is_parent_view(view)) {
1656 if (view->child && view->child->focussed)
1659 if (view->parent && view->parent->focussed)
1660 view = view->parent;
1662 show_panel(view->panel);
1663 if (view->child && view_is_splitscreen(view->child))
1664 show_panel(view->child->panel);
1665 if (view->parent && view_is_splitscreen(view)) {
1666 err = view->parent->show(view->parent);
1670 err = view->show(view);
1674 err = view->child->show(view->child);
1683 while (!TAILQ_EMPTY(&views)) {
1684 const struct got_error *close_err;
1685 view = TAILQ_FIRST(&views);
1686 TAILQ_REMOVE(&views, view, entry);
1687 close_err = view_close(view);
1688 if (close_err && err == NULL)
1692 errcode = pthread_mutex_unlock(&tog_mutex);
1693 if (errcode && err == NULL)
1694 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1704 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1709 /* Create newly allocated wide-character string equivalent to a byte string. */
1710 static const struct got_error *
1711 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1714 const struct got_error *err = NULL;
1717 *wlen = mbstowcs(NULL, s, 0);
1718 if (*wlen == (size_t)-1) {
1720 if (errno != EILSEQ)
1721 return got_error_from_errno("mbstowcs");
1723 /* byte string invalid in current encoding; try to "fix" it */
1724 err = got_mbsavis(&vis, &vislen, s);
1727 *wlen = mbstowcs(NULL, vis, 0);
1728 if (*wlen == (size_t)-1) {
1729 err = got_error_from_errno("mbstowcs"); /* give up */
1734 *ws = calloc(*wlen + 1, sizeof(**ws));
1736 err = got_error_from_errno("calloc");
1740 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1741 err = got_error_from_errno("mbstowcs");
1752 static const struct got_error *
1753 expand_tab(char **ptr, const char *src)
1756 size_t len, n, idx = 0, sz = 0;
1759 n = len = strlen(src);
1760 dst = malloc(n + 1);
1762 return got_error_from_errno("malloc");
1764 while (idx < len && src[idx]) {
1765 const char c = src[idx];
1768 size_t nb = TABSIZE - sz % TABSIZE;
1771 p = realloc(dst, n + nb);
1774 return got_error_from_errno("realloc");
1779 memset(dst + sz, ' ', nb);
1782 dst[sz++] = src[idx];
1792 * Advance at most n columns from wline starting at offset off.
1793 * Return the index to the first character after the span operation.
1794 * Return the combined column width of all spanned wide character in
1798 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1800 int width, i, cols = 0;
1807 for (i = off; wline[i] != L'\0'; ++i) {
1808 if (wline[i] == L'\t')
1809 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1811 width = wcwidth(wline[i]);
1818 if (cols + width > n)
1828 * Format a line for display, ensuring that it won't overflow a width limit.
1829 * With scrolling, the width returned refers to the scrolled version of the
1830 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1832 static const struct got_error *
1833 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1834 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1836 const struct got_error *err = NULL;
1838 wchar_t *wline = NULL;
1847 err = expand_tab(&exstr, line);
1852 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1857 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1859 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1860 wline[wlen - 1] = L'\0';
1863 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1864 wline[wlen - 1] = L'\0';
1868 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1874 *scrollxp = scrollx;
1882 static const struct got_error*
1883 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1884 struct got_object_id *id, struct got_repository *repo)
1886 static const struct got_error *err = NULL;
1887 struct got_reflist_entry *re;
1893 TAILQ_FOREACH(re, refs, entry) {
1894 struct got_tag_object *tag = NULL;
1895 struct got_object_id *ref_id;
1898 name = got_ref_get_name(re->ref);
1899 if (strcmp(name, GOT_REF_HEAD) == 0)
1901 if (strncmp(name, "refs/", 5) == 0)
1903 if (strncmp(name, "got/", 4) == 0 &&
1904 strncmp(name, "got/backup/", 11) != 0)
1906 if (strncmp(name, "heads/", 6) == 0)
1908 if (strncmp(name, "remotes/", 8) == 0) {
1910 s = strstr(name, "/" GOT_REF_HEAD);
1911 if (s != NULL && s[strlen(s)] == '\0')
1914 err = got_ref_resolve(&ref_id, repo, re->ref);
1917 if (strncmp(name, "tags/", 5) == 0) {
1918 err = got_object_open_as_tag(&tag, repo, ref_id);
1920 if (err->code != GOT_ERR_OBJ_TYPE) {
1924 /* Ref points at something other than a tag. */
1929 cmp = got_object_id_cmp(tag ?
1930 got_object_tag_get_object_id(tag) : ref_id, id);
1933 got_object_tag_close(tag);
1937 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1938 s ? ", " : "", name) == -1) {
1939 err = got_error_from_errno("asprintf");
1950 static const struct got_error *
1951 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1956 smallerthan = strchr(author, '<');
1957 if (smallerthan && smallerthan[1] != '\0')
1958 author = smallerthan + 1;
1959 author[strcspn(author, "@>")] = '\0';
1960 return format_line(wauthor, author_width, NULL, author, 0, limit,
1964 static const struct got_error *
1965 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1966 struct got_object_id *id, const size_t date_display_cols,
1967 int author_display_cols)
1969 struct tog_log_view_state *s = &view->state.log;
1970 const struct got_error *err = NULL;
1971 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1972 char *logmsg0 = NULL, *logmsg = NULL;
1973 char *author = NULL;
1974 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1975 int author_width, logmsg_width;
1976 char *newline, *line = NULL;
1977 int col, limit, scrollx;
1978 const int avail = view->ncols;
1980 time_t committer_time;
1981 struct tog_color *tc;
1983 committer_time = got_object_commit_get_committer_time(commit);
1984 if (gmtime_r(&committer_time, &tm) == NULL)
1985 return got_error_from_errno("gmtime_r");
1986 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1987 return got_error(GOT_ERR_NO_SPACE);
1989 if (avail <= date_display_cols)
1990 limit = MIN(sizeof(datebuf) - 1, avail);
1992 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1993 tc = get_color(&s->colors, TOG_COLOR_DATE);
1995 wattr_on(view->window,
1996 COLOR_PAIR(tc->colorpair), NULL);
1997 waddnstr(view->window, datebuf, limit);
1999 wattr_off(view->window,
2000 COLOR_PAIR(tc->colorpair), NULL);
2007 err = got_object_id_str(&id_str, id);
2010 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2012 wattr_on(view->window,
2013 COLOR_PAIR(tc->colorpair), NULL);
2014 wprintw(view->window, "%.8s ", id_str);
2016 wattr_off(view->window,
2017 COLOR_PAIR(tc->colorpair), NULL);
2024 if (s->use_committer)
2025 author = strdup(got_object_commit_get_committer(commit));
2027 author = strdup(got_object_commit_get_author(commit));
2028 if (author == NULL) {
2029 err = got_error_from_errno("strdup");
2032 err = format_author(&wauthor, &author_width, author, avail - col, col);
2035 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2037 wattr_on(view->window,
2038 COLOR_PAIR(tc->colorpair), NULL);
2039 waddwstr(view->window, wauthor);
2041 wattr_off(view->window,
2042 COLOR_PAIR(tc->colorpair), NULL);
2043 col += author_width;
2044 while (col < avail && author_width < author_display_cols + 2) {
2045 waddch(view->window, ' ');
2052 err = got_object_commit_get_logmsg(&logmsg0, commit);
2056 while (*logmsg == '\n')
2058 newline = strchr(logmsg, '\n');
2061 limit = avail - col;
2062 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2063 limit--; /* for the border */
2064 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2068 waddwstr(view->window, &wlogmsg[scrollx]);
2069 col += MAX(logmsg_width, 0);
2070 while (col < avail) {
2071 waddch(view->window, ' ');
2083 static struct commit_queue_entry *
2084 alloc_commit_queue_entry(struct got_commit_object *commit,
2085 struct got_object_id *id)
2087 struct commit_queue_entry *entry;
2089 entry = calloc(1, sizeof(*entry));
2094 entry->commit = commit;
2099 pop_commit(struct commit_queue *commits)
2101 struct commit_queue_entry *entry;
2103 entry = TAILQ_FIRST(&commits->head);
2104 TAILQ_REMOVE(&commits->head, entry, entry);
2105 got_object_commit_close(entry->commit);
2106 commits->ncommits--;
2107 /* Don't free entry->id! It is owned by the commit graph. */
2112 free_commits(struct commit_queue *commits)
2114 while (!TAILQ_EMPTY(&commits->head))
2115 pop_commit(commits);
2118 static const struct got_error *
2119 match_commit(int *have_match, struct got_object_id *id,
2120 struct got_commit_object *commit, regex_t *regex)
2122 const struct got_error *err = NULL;
2123 regmatch_t regmatch;
2124 char *id_str = NULL, *logmsg = NULL;
2128 err = got_object_id_str(&id_str, id);
2132 err = got_object_commit_get_logmsg(&logmsg, commit);
2136 if (regexec(regex, got_object_commit_get_author(commit), 1,
2137 ®match, 0) == 0 ||
2138 regexec(regex, got_object_commit_get_committer(commit), 1,
2139 ®match, 0) == 0 ||
2140 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2141 regexec(regex, logmsg, 1, ®match, 0) == 0)
2149 static const struct got_error *
2150 queue_commits(struct tog_log_thread_args *a)
2152 const struct got_error *err = NULL;
2155 * We keep all commits open throughout the lifetime of the log
2156 * view in order to avoid having to re-fetch commits from disk
2157 * while updating the display.
2160 struct got_object_id *id;
2161 struct got_commit_object *commit;
2162 struct commit_queue_entry *entry;
2165 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2167 if (err || id == NULL)
2170 err = got_object_open_as_commit(&commit, a->repo, id);
2173 entry = alloc_commit_queue_entry(commit, id);
2174 if (entry == NULL) {
2175 err = got_error_from_errno("alloc_commit_queue_entry");
2179 errcode = pthread_mutex_lock(&tog_mutex);
2181 err = got_error_set_errno(errcode,
2182 "pthread_mutex_lock");
2186 entry->idx = a->commits->ncommits;
2187 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2188 a->commits->ncommits++;
2190 if (*a->searching == TOG_SEARCH_FORWARD &&
2191 !*a->search_next_done) {
2193 err = match_commit(&have_match, id, commit, a->regex);
2197 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2200 errcode = pthread_mutex_unlock(&tog_mutex);
2201 if (errcode && err == NULL)
2202 err = got_error_set_errno(errcode,
2203 "pthread_mutex_unlock");
2206 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2212 select_commit(struct tog_log_view_state *s)
2214 struct commit_queue_entry *entry;
2217 entry = s->first_displayed_entry;
2219 if (ncommits == s->selected) {
2220 s->selected_entry = entry;
2223 entry = TAILQ_NEXT(entry, entry);
2228 static const struct got_error *
2229 draw_commits(struct tog_view *view)
2231 const struct got_error *err = NULL;
2232 struct tog_log_view_state *s = &view->state.log;
2233 struct commit_queue_entry *entry = s->selected_entry;
2234 const int limit = view->nlines;
2236 int ncommits, author_cols = 4;
2237 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2238 char *refs_str = NULL;
2240 struct tog_color *tc;
2241 static const size_t date_display_cols = 12;
2243 if (s->selected_entry &&
2244 !(view->searching && view->search_next_done == 0)) {
2245 struct got_reflist_head *refs;
2246 err = got_object_id_str(&id_str, s->selected_entry->id);
2249 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2250 s->selected_entry->id);
2252 err = build_refs_str(&refs_str, refs,
2253 s->selected_entry->id, s->repo);
2259 if (s->thread_args.commits_needed == 0)
2260 halfdelay(10); /* disable fast refresh */
2262 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2263 if (asprintf(&ncommits_str, " [%d/%d] %s",
2264 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2265 (view->searching && !view->search_next_done) ?
2266 "searching..." : "loading...") == -1) {
2267 err = got_error_from_errno("asprintf");
2271 const char *search_str = NULL;
2273 if (view->searching) {
2274 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2275 search_str = "no more matches";
2276 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2277 search_str = "no matches found";
2278 else if (!view->search_next_done)
2279 search_str = "searching...";
2282 if (asprintf(&ncommits_str, " [%d/%d] %s",
2283 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2284 search_str ? search_str :
2285 (refs_str ? refs_str : "")) == -1) {
2286 err = got_error_from_errno("asprintf");
2291 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2292 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2293 "........................................",
2294 s->in_repo_path, ncommits_str) == -1) {
2295 err = got_error_from_errno("asprintf");
2299 } else if (asprintf(&header, "commit %s%s",
2300 id_str ? id_str : "........................................",
2301 ncommits_str) == -1) {
2302 err = got_error_from_errno("asprintf");
2306 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2310 werase(view->window);
2312 if (view_needs_focus_indication(view))
2313 wstandout(view->window);
2314 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2316 wattr_on(view->window,
2317 COLOR_PAIR(tc->colorpair), NULL);
2318 waddwstr(view->window, wline);
2320 wattr_off(view->window,
2321 COLOR_PAIR(tc->colorpair), NULL);
2322 while (width < view->ncols) {
2323 waddch(view->window, ' ');
2326 if (view_needs_focus_indication(view))
2327 wstandend(view->window);
2332 /* Grow author column size if necessary, and set view->maxx. */
2333 entry = s->first_displayed_entry;
2337 struct got_commit_object *c = entry->commit;
2338 char *author, *eol, *msg, *msg0;
2339 wchar_t *wauthor, *wmsg;
2341 if (ncommits >= limit - 1)
2343 if (s->use_committer)
2344 author = strdup(got_object_commit_get_committer(c));
2346 author = strdup(got_object_commit_get_author(c));
2347 if (author == NULL) {
2348 err = got_error_from_errno("strdup");
2351 err = format_author(&wauthor, &width, author, COLS,
2353 if (author_cols < width)
2354 author_cols = width;
2359 err = got_object_commit_get_logmsg(&msg0, c);
2363 while (*msg == '\n')
2365 if ((eol = strchr(msg, '\n')))
2367 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2368 date_display_cols + author_cols, 0);
2371 view->maxx = MAX(view->maxx, width);
2375 entry = TAILQ_NEXT(entry, entry);
2378 entry = s->first_displayed_entry;
2379 s->last_displayed_entry = s->first_displayed_entry;
2382 if (ncommits >= limit - 1)
2384 if (ncommits == s->selected)
2385 wstandout(view->window);
2386 err = draw_commit(view, entry->commit, entry->id,
2387 date_display_cols, author_cols);
2388 if (ncommits == s->selected)
2389 wstandend(view->window);
2393 s->last_displayed_entry = entry;
2394 entry = TAILQ_NEXT(entry, entry);
2407 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2409 struct commit_queue_entry *entry;
2412 entry = TAILQ_FIRST(&s->commits.head);
2413 if (s->first_displayed_entry == entry)
2416 entry = s->first_displayed_entry;
2417 while (entry && nscrolled < maxscroll) {
2418 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2420 s->first_displayed_entry = entry;
2426 static const struct got_error *
2427 trigger_log_thread(struct tog_view *view, int wait)
2429 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2432 halfdelay(1); /* fast refresh while loading commits */
2434 while (!ta->log_complete && !tog_thread_error &&
2435 (ta->commits_needed > 0 || ta->load_all)) {
2436 /* Wake the log thread. */
2437 errcode = pthread_cond_signal(&ta->need_commits);
2439 return got_error_set_errno(errcode,
2440 "pthread_cond_signal");
2443 * The mutex will be released while the view loop waits
2444 * in wgetch(), at which time the log thread will run.
2449 /* Display progress update in log view. */
2450 show_log_view(view);
2454 /* Wait right here while next commit is being loaded. */
2455 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2457 return got_error_set_errno(errcode,
2458 "pthread_cond_wait");
2460 /* Display progress update in log view. */
2461 show_log_view(view);
2469 static const struct got_error *
2470 request_log_commits(struct tog_view *view)
2472 struct tog_log_view_state *state = &view->state.log;
2473 const struct got_error *err = NULL;
2475 if (state->thread_args.log_complete)
2478 state->thread_args.commits_needed += view->nscrolled;
2479 err = trigger_log_thread(view, 1);
2480 view->nscrolled = 0;
2485 static const struct got_error *
2486 log_scroll_down(struct tog_view *view, int maxscroll)
2488 struct tog_log_view_state *s = &view->state.log;
2489 const struct got_error *err = NULL;
2490 struct commit_queue_entry *pentry;
2491 int nscrolled = 0, ncommits_needed;
2493 if (s->last_displayed_entry == NULL)
2496 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2497 if (s->commits.ncommits < ncommits_needed &&
2498 !s->thread_args.log_complete) {
2500 * Ask the log thread for required amount of commits.
2502 s->thread_args.commits_needed += maxscroll;
2503 err = trigger_log_thread(view, 1);
2509 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2510 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2513 s->last_displayed_entry = pentry ?
2514 pentry : s->last_displayed_entry;;
2516 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2519 s->first_displayed_entry = pentry;
2520 } while (++nscrolled < maxscroll);
2522 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2523 view->nscrolled += nscrolled;
2525 view->nscrolled = 0;
2530 static const struct got_error *
2531 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2532 struct got_commit_object *commit, struct got_object_id *commit_id,
2533 struct tog_view *log_view, struct got_repository *repo)
2535 const struct got_error *err;
2536 struct got_object_qid *parent_id;
2537 struct tog_view *diff_view;
2539 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2540 if (diff_view == NULL)
2541 return got_error_from_errno("view_open");
2543 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2544 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2545 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2547 *new_view = diff_view;
2551 static const struct got_error *
2552 tree_view_visit_subtree(struct tog_tree_view_state *s,
2553 struct got_tree_object *subtree)
2555 struct tog_parent_tree *parent;
2557 parent = calloc(1, sizeof(*parent));
2559 return got_error_from_errno("calloc");
2561 parent->tree = s->tree;
2562 parent->first_displayed_entry = s->first_displayed_entry;
2563 parent->selected_entry = s->selected_entry;
2564 parent->selected = s->selected;
2565 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2568 s->first_displayed_entry = NULL;
2572 static const struct got_error *
2573 tree_view_walk_path(struct tog_tree_view_state *s,
2574 struct got_commit_object *commit, const char *path)
2576 const struct got_error *err = NULL;
2577 struct got_tree_object *tree = NULL;
2579 char *slash, *subpath = NULL;
2581 /* Walk the path and open corresponding tree objects. */
2584 struct got_tree_entry *te;
2585 struct got_object_id *tree_id;
2591 /* Ensure the correct subtree entry is selected. */
2592 slash = strchr(p, '/');
2594 te_name = strdup(p);
2596 te_name = strndup(p, slash - p);
2597 if (te_name == NULL) {
2598 err = got_error_from_errno("strndup");
2601 te = got_object_tree_find_entry(s->tree, te_name);
2603 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2608 s->first_displayed_entry = s->selected_entry = te;
2610 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2611 break; /* jump to this file's entry */
2613 slash = strchr(p, '/');
2615 subpath = strndup(path, slash - path);
2617 subpath = strdup(path);
2618 if (subpath == NULL) {
2619 err = got_error_from_errno("strdup");
2623 err = got_object_id_by_path(&tree_id, s->repo, commit,
2628 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2633 err = tree_view_visit_subtree(s, tree);
2635 got_object_tree_close(tree);
2649 static const struct got_error *
2650 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2651 struct commit_queue_entry *entry, const char *path,
2652 const char *head_ref_name, struct got_repository *repo)
2654 const struct got_error *err = NULL;
2655 struct tog_tree_view_state *s;
2656 struct tog_view *tree_view;
2658 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2659 if (tree_view == NULL)
2660 return got_error_from_errno("view_open");
2662 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2665 s = &tree_view->state.tree;
2667 *new_view = tree_view;
2669 if (got_path_is_root_dir(path))
2672 return tree_view_walk_path(s, entry->commit, path);
2675 static const struct got_error *
2676 block_signals_used_by_main_thread(void)
2681 if (sigemptyset(&sigset) == -1)
2682 return got_error_from_errno("sigemptyset");
2684 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2685 if (sigaddset(&sigset, SIGWINCH) == -1)
2686 return got_error_from_errno("sigaddset");
2687 if (sigaddset(&sigset, SIGCONT) == -1)
2688 return got_error_from_errno("sigaddset");
2689 if (sigaddset(&sigset, SIGINT) == -1)
2690 return got_error_from_errno("sigaddset");
2691 if (sigaddset(&sigset, SIGTERM) == -1)
2692 return got_error_from_errno("sigaddset");
2694 /* ncurses handles SIGTSTP */
2695 if (sigaddset(&sigset, SIGTSTP) == -1)
2696 return got_error_from_errno("sigaddset");
2698 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2700 return got_error_set_errno(errcode, "pthread_sigmask");
2706 log_thread(void *arg)
2708 const struct got_error *err = NULL;
2710 struct tog_log_thread_args *a = arg;
2714 * Sync startup with main thread such that we begin our
2715 * work once view_input() has released the mutex.
2717 errcode = pthread_mutex_lock(&tog_mutex);
2719 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2723 err = block_signals_used_by_main_thread();
2725 pthread_mutex_unlock(&tog_mutex);
2729 while (!done && !err && !tog_fatal_signal_received()) {
2730 errcode = pthread_mutex_unlock(&tog_mutex);
2732 err = got_error_set_errno(errcode,
2733 "pthread_mutex_unlock");
2736 err = queue_commits(a);
2738 if (err->code != GOT_ERR_ITER_COMPLETED)
2742 } else if (a->commits_needed > 0 && !a->load_all)
2743 a->commits_needed--;
2745 errcode = pthread_mutex_lock(&tog_mutex);
2747 err = got_error_set_errno(errcode,
2748 "pthread_mutex_lock");
2750 } else if (*a->quit)
2752 else if (*a->first_displayed_entry == NULL) {
2753 *a->first_displayed_entry =
2754 TAILQ_FIRST(&a->commits->head);
2755 *a->selected_entry = *a->first_displayed_entry;
2758 errcode = pthread_cond_signal(&a->commit_loaded);
2760 err = got_error_set_errno(errcode,
2761 "pthread_cond_signal");
2762 pthread_mutex_unlock(&tog_mutex);
2767 a->commits_needed = 0;
2769 if (a->commits_needed == 0 && !a->load_all) {
2770 errcode = pthread_cond_wait(&a->need_commits,
2773 err = got_error_set_errno(errcode,
2774 "pthread_cond_wait");
2775 pthread_mutex_unlock(&tog_mutex);
2783 a->log_complete = 1;
2784 errcode = pthread_mutex_unlock(&tog_mutex);
2786 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2789 tog_thread_error = 1;
2790 pthread_cond_signal(&a->commit_loaded);
2795 static const struct got_error *
2796 stop_log_thread(struct tog_log_view_state *s)
2798 const struct got_error *err = NULL, *thread_err = NULL;
2803 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2805 return got_error_set_errno(errcode,
2806 "pthread_cond_signal");
2807 errcode = pthread_mutex_unlock(&tog_mutex);
2809 return got_error_set_errno(errcode,
2810 "pthread_mutex_unlock");
2811 errcode = pthread_join(s->thread, (void **)&thread_err);
2813 return got_error_set_errno(errcode, "pthread_join");
2814 errcode = pthread_mutex_lock(&tog_mutex);
2816 return got_error_set_errno(errcode,
2817 "pthread_mutex_lock");
2818 s->thread = 0; //NULL;
2821 if (s->thread_args.repo) {
2822 err = got_repo_close(s->thread_args.repo);
2823 s->thread_args.repo = NULL;
2826 if (s->thread_args.pack_fds) {
2827 const struct got_error *pack_err =
2828 got_repo_pack_fds_close(s->thread_args.pack_fds);
2831 s->thread_args.pack_fds = NULL;
2834 if (s->thread_args.graph) {
2835 got_commit_graph_close(s->thread_args.graph);
2836 s->thread_args.graph = NULL;
2839 return err ? err : thread_err;
2842 static const struct got_error *
2843 close_log_view(struct tog_view *view)
2845 const struct got_error *err = NULL;
2846 struct tog_log_view_state *s = &view->state.log;
2849 err = stop_log_thread(s);
2851 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2852 if (errcode && err == NULL)
2853 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2855 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2856 if (errcode && err == NULL)
2857 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2859 free_commits(&s->commits);
2860 free(s->in_repo_path);
2861 s->in_repo_path = NULL;
2864 free(s->head_ref_name);
2865 s->head_ref_name = NULL;
2869 static const struct got_error *
2870 search_start_log_view(struct tog_view *view)
2872 struct tog_log_view_state *s = &view->state.log;
2874 s->matched_entry = NULL;
2875 s->search_entry = NULL;
2879 static const struct got_error *
2880 search_next_log_view(struct tog_view *view)
2882 const struct got_error *err = NULL;
2883 struct tog_log_view_state *s = &view->state.log;
2884 struct commit_queue_entry *entry;
2886 /* Display progress update in log view. */
2887 show_log_view(view);
2891 if (s->search_entry) {
2893 errcode = pthread_mutex_unlock(&tog_mutex);
2895 return got_error_set_errno(errcode,
2896 "pthread_mutex_unlock");
2897 ch = wgetch(view->window);
2898 errcode = pthread_mutex_lock(&tog_mutex);
2900 return got_error_set_errno(errcode,
2901 "pthread_mutex_lock");
2902 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2903 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2906 if (view->searching == TOG_SEARCH_FORWARD)
2907 entry = TAILQ_NEXT(s->search_entry, entry);
2909 entry = TAILQ_PREV(s->search_entry,
2910 commit_queue_head, entry);
2911 } else if (s->matched_entry) {
2912 int matched_idx = s->matched_entry->idx;
2913 int selected_idx = s->selected_entry->idx;
2916 * If the user has moved the cursor after we hit a match,
2917 * the position from where we should continue searching
2918 * might have changed.
2920 if (view->searching == TOG_SEARCH_FORWARD) {
2921 if (matched_idx > selected_idx)
2922 entry = TAILQ_NEXT(s->selected_entry, entry);
2924 entry = TAILQ_NEXT(s->matched_entry, entry);
2926 if (matched_idx < selected_idx)
2927 entry = TAILQ_PREV(s->selected_entry,
2928 commit_queue_head, entry);
2930 entry = TAILQ_PREV(s->matched_entry,
2931 commit_queue_head, entry);
2934 entry = s->selected_entry;
2940 if (entry == NULL) {
2941 if (s->thread_args.log_complete ||
2942 view->searching == TOG_SEARCH_BACKWARD) {
2943 view->search_next_done =
2944 (s->matched_entry == NULL ?
2945 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2946 s->search_entry = NULL;
2950 * Poke the log thread for more commits and return,
2951 * allowing the main loop to make progress. Search
2952 * will resume at s->search_entry once we come back.
2954 s->thread_args.commits_needed++;
2955 return trigger_log_thread(view, 0);
2958 err = match_commit(&have_match, entry->id, entry->commit,
2963 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2964 s->matched_entry = entry;
2968 s->search_entry = entry;
2969 if (view->searching == TOG_SEARCH_FORWARD)
2970 entry = TAILQ_NEXT(entry, entry);
2972 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2975 if (s->matched_entry) {
2976 int cur = s->selected_entry->idx;
2977 while (cur < s->matched_entry->idx) {
2978 err = input_log_view(NULL, view, KEY_DOWN);
2983 while (cur > s->matched_entry->idx) {
2984 err = input_log_view(NULL, view, KEY_UP);
2991 s->search_entry = NULL;
2996 static const struct got_error *
2997 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2998 struct got_repository *repo, const char *head_ref_name,
2999 const char *in_repo_path, int log_branches)
3001 const struct got_error *err = NULL;
3002 struct tog_log_view_state *s = &view->state.log;
3003 struct got_repository *thread_repo = NULL;
3004 struct got_commit_graph *thread_graph = NULL;
3007 if (in_repo_path != s->in_repo_path) {
3008 free(s->in_repo_path);
3009 s->in_repo_path = strdup(in_repo_path);
3010 if (s->in_repo_path == NULL)
3011 return got_error_from_errno("strdup");
3014 /* The commit queue only contains commits being displayed. */
3015 TAILQ_INIT(&s->commits.head);
3016 s->commits.ncommits = 0;
3019 if (head_ref_name) {
3020 s->head_ref_name = strdup(head_ref_name);
3021 if (s->head_ref_name == NULL) {
3022 err = got_error_from_errno("strdup");
3026 s->start_id = got_object_id_dup(start_id);
3027 if (s->start_id == NULL) {
3028 err = got_error_from_errno("got_object_id_dup");
3031 s->log_branches = log_branches;
3033 STAILQ_INIT(&s->colors);
3034 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3035 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3036 get_color_value("TOG_COLOR_COMMIT"));
3039 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3040 get_color_value("TOG_COLOR_AUTHOR"));
3042 free_colors(&s->colors);
3045 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3046 get_color_value("TOG_COLOR_DATE"));
3048 free_colors(&s->colors);
3053 view->show = show_log_view;
3054 view->input = input_log_view;
3055 view->resize = resize_log_view;
3056 view->close = close_log_view;
3057 view->search_start = search_start_log_view;
3058 view->search_next = search_next_log_view;
3060 if (s->thread_args.pack_fds == NULL) {
3061 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3065 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3066 s->thread_args.pack_fds);
3069 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3073 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3074 s->repo, NULL, NULL);
3078 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3080 err = got_error_set_errno(errcode, "pthread_cond_init");
3083 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3085 err = got_error_set_errno(errcode, "pthread_cond_init");
3089 s->thread_args.commits_needed = view->nlines;
3090 s->thread_args.graph = thread_graph;
3091 s->thread_args.commits = &s->commits;
3092 s->thread_args.in_repo_path = s->in_repo_path;
3093 s->thread_args.start_id = s->start_id;
3094 s->thread_args.repo = thread_repo;
3095 s->thread_args.log_complete = 0;
3096 s->thread_args.quit = &s->quit;
3097 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3098 s->thread_args.selected_entry = &s->selected_entry;
3099 s->thread_args.searching = &view->searching;
3100 s->thread_args.search_next_done = &view->search_next_done;
3101 s->thread_args.regex = &view->regex;
3104 close_log_view(view);
3108 static const struct got_error *
3109 show_log_view(struct tog_view *view)
3111 const struct got_error *err;
3112 struct tog_log_view_state *s = &view->state.log;
3114 if (s->thread == 0) { //NULL) {
3115 int errcode = pthread_create(&s->thread, NULL, log_thread,
3118 return got_error_set_errno(errcode, "pthread_create");
3119 if (s->thread_args.commits_needed > 0) {
3120 err = trigger_log_thread(view, 1);
3126 return draw_commits(view);
3130 log_move_cursor_up(struct tog_view *view, int page, int home)
3132 struct tog_log_view_state *s = &view->state.log;
3134 if (s->selected_entry->idx == 0)
3136 if (s->first_displayed_entry == NULL)
3139 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3141 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3143 if (!page && !home && s->selected > 0)
3146 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3152 static const struct got_error *
3153 log_move_cursor_down(struct tog_view *view, int page)
3155 struct tog_log_view_state *s = &view->state.log;
3156 struct commit_queue_entry *first;
3157 const struct got_error *err = NULL;
3159 first = s->first_displayed_entry;
3160 if (first == NULL) {
3165 if (s->thread_args.log_complete &&
3166 s->selected_entry->idx >= s->commits.ncommits - 1)
3170 int eos = view->nlines - 2;
3172 if (view_is_hsplit_top(view))
3173 --eos; /* border consumes the last line */
3174 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3177 err = log_scroll_down(view, 1);
3178 } else if (s->thread_args.load_all) {
3179 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3180 s->selected += MIN(s->last_displayed_entry->idx -
3181 s->selected_entry->idx, page + 1);
3183 err = log_scroll_down(view, MIN(page,
3184 s->commits.ncommits - s->selected_entry->idx - 1));
3185 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3187 err = log_scroll_down(view, page);
3190 if (first == s->first_displayed_entry && s->selected <
3191 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3192 s->selected = MIN(s->commits.ncommits - 1, page);
3199 * We might necessarily overshoot in horizontal
3200 * splits; if so, select the last displayed commit.
3202 s->selected = MIN(s->selected,
3203 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3207 if (s->thread_args.log_complete &&
3208 s->selected_entry->idx == s->commits.ncommits - 1)
3215 view_get_split(struct tog_view *view, int *y, int *x)
3220 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3221 if (view->child && view->child->resized_y)
3222 *y = view->child->resized_y;
3223 else if (view->resized_y)
3224 *y = view->resized_y;
3226 *y = view_split_begin_y(view->lines);
3227 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3228 if (view->child && view->child->resized_x)
3229 *x = view->child->resized_x;
3230 else if (view->resized_x)
3231 *x = view->resized_x;
3233 *x = view_split_begin_x(view->begin_x);
3237 /* Split view horizontally at y and offset view->state->selected line. */
3238 static const struct got_error *
3239 view_init_hsplit(struct tog_view *view, int y)
3241 const struct got_error *err = NULL;
3245 err = view_resize(view);
3249 err = offset_selection_down(view);
3254 static const struct got_error *
3255 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3257 const struct got_error *err = NULL;
3258 struct tog_log_view_state *s = &view->state.log;
3259 struct commit_queue_entry *entry;
3260 int eos, n, nscroll;
3262 if (s->thread_args.load_all) {
3263 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3264 s->thread_args.load_all = 0;
3265 else if (s->thread_args.log_complete) {
3266 err = log_move_cursor_down(view, s->commits.ncommits);
3267 s->thread_args.load_all = 0;
3272 eos = nscroll = view->nlines - 1;
3273 if (view_is_hsplit_top(view))
3284 view->x = MAX(view->maxx - view->ncols / 2, 0);
3289 if (view->x + view->ncols / 2 < view->maxx)
3290 view->x += 2; /* move two columns right */
3296 view->x -= MIN(view->x, 2); /* move two columns back */
3305 log_move_cursor_up(view, 0, 0);
3309 log_move_cursor_up(view, 0, 1);
3319 log_move_cursor_up(view, nscroll, 0);
3326 err = log_move_cursor_down(view, 0);
3329 s->use_committer = !s->use_committer;
3333 /* We don't know yet how many commits, so we're forced to
3334 * traverse them all. */
3336 if (!s->thread_args.log_complete) {
3337 s->thread_args.load_all = 1;
3338 return trigger_log_thread(view, 0);
3342 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3343 for (n = 0; n < eos; n++) {
3346 s->first_displayed_entry = entry;
3347 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3350 s->selected = n - 1;
3362 err = log_move_cursor_down(view, nscroll);
3365 if (s->selected > view->nlines - 2)
3366 s->selected = view->nlines - 2;
3367 if (s->selected > s->commits.ncommits - 1)
3368 s->selected = s->commits.ncommits - 1;
3370 if (s->commits.ncommits < view->nlines - 1 &&
3371 !s->thread_args.log_complete) {
3372 s->thread_args.commits_needed += (view->nlines - 1) -
3373 s->commits.ncommits;
3374 err = trigger_log_thread(view, 1);
3380 if (s->selected_entry == NULL)
3382 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3386 if (s->selected_entry == NULL)
3388 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3394 if (ch == KEY_BACKSPACE &&
3395 got_path_is_root_dir(s->in_repo_path))
3397 err = stop_log_thread(s);
3400 if (ch == KEY_BACKSPACE) {
3402 err = got_path_dirname(&parent_path, s->in_repo_path);
3405 free(s->in_repo_path);
3406 s->in_repo_path = parent_path;
3407 s->thread_args.in_repo_path = s->in_repo_path;
3408 } else if (ch == CTRL('l')) {
3409 struct got_object_id *start_id;
3410 err = got_repo_match_object_id(&start_id, NULL,
3411 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3412 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3416 s->start_id = start_id;
3417 s->thread_args.start_id = s->start_id;
3419 s->log_branches = !s->log_branches;
3421 if (s->thread_args.pack_fds == NULL) {
3422 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3426 err = got_repo_open(&s->thread_args.repo,
3427 got_repo_get_path(s->repo), NULL,
3428 s->thread_args.pack_fds);
3432 err = tog_load_refs(s->repo, 0);
3435 err = got_commit_graph_open(&s->thread_args.graph,
3436 s->in_repo_path, !s->log_branches);
3439 err = got_commit_graph_iter_start(s->thread_args.graph,
3440 s->start_id, s->repo, NULL, NULL);
3443 free_commits(&s->commits);
3444 s->first_displayed_entry = NULL;
3445 s->last_displayed_entry = NULL;
3446 s->selected_entry = NULL;
3448 s->thread_args.log_complete = 0;
3450 s->thread_args.commits_needed = view->lines;
3451 s->matched_entry = NULL;
3452 s->search_entry = NULL;
3457 err = view_request_new(new_view, view, TOG_VIEW_REF);
3467 static const struct got_error *
3468 apply_unveil(const char *repo_path, const char *worktree_path)
3470 const struct got_error *error;
3473 if (unveil("gmon.out", "rwc") != 0)
3474 return got_error_from_errno2("unveil", "gmon.out");
3476 if (repo_path && unveil(repo_path, "r") != 0)
3477 return got_error_from_errno2("unveil", repo_path);
3479 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3480 return got_error_from_errno2("unveil", worktree_path);
3482 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3483 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3485 error = got_privsep_unveil_exec_helpers();
3489 if (unveil(NULL, NULL) != 0)
3490 return got_error_from_errno("unveil");
3499 * Override default signal handlers before starting ncurses.
3500 * This should prevent ncurses from installing its own
3501 * broken cleanup() signal handler.
3503 signal(SIGWINCH, tog_sigwinch);
3504 signal(SIGPIPE, tog_sigpipe);
3505 signal(SIGCONT, tog_sigcont);
3506 signal(SIGINT, tog_sigint);
3507 signal(SIGTERM, tog_sigterm);
3511 halfdelay(1); /* Do fast refresh while initial view is loading. */
3514 intrflush(stdscr, FALSE);
3515 keypad(stdscr, TRUE);
3517 if (getenv("TOG_COLORS") != NULL) {
3519 use_default_colors();
3523 static const struct got_error *
3524 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3525 struct got_repository *repo, struct got_worktree *worktree)
3527 const struct got_error *err = NULL;
3530 *in_repo_path = strdup("/");
3531 if (*in_repo_path == NULL)
3532 return got_error_from_errno("strdup");
3537 const char *prefix = got_worktree_get_path_prefix(worktree);
3540 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3543 if (asprintf(in_repo_path, "%s%s%s", prefix,
3544 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3546 err = got_error_from_errno("asprintf");
3547 *in_repo_path = NULL;
3551 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3556 static const struct got_error *
3557 cmd_log(int argc, char *argv[])
3559 const struct got_error *error;
3560 struct got_repository *repo = NULL;
3561 struct got_worktree *worktree = NULL;
3562 struct got_object_id *start_id = NULL;
3563 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3564 char *start_commit = NULL, *label = NULL;
3565 struct got_reference *ref = NULL;
3566 const char *head_ref_name = NULL;
3567 int ch, log_branches = 0;
3568 struct tog_view *view;
3569 int *pack_fds = NULL;
3571 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3577 start_commit = optarg;
3580 repo_path = realpath(optarg, NULL);
3581 if (repo_path == NULL)
3582 return got_error_from_errno2("realpath",
3597 error = got_repo_pack_fds_open(&pack_fds);
3601 if (repo_path == NULL) {
3602 cwd = getcwd(NULL, 0);
3604 return got_error_from_errno("getcwd");
3605 error = got_worktree_open(&worktree, cwd);
3606 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3610 strdup(got_worktree_get_repo_path(worktree));
3612 repo_path = strdup(cwd);
3613 if (repo_path == NULL) {
3614 error = got_error_from_errno("strdup");
3619 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3623 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3630 error = apply_unveil(got_repo_get_path(repo),
3631 worktree ? got_worktree_get_root_path(worktree) : NULL);
3635 /* already loaded by tog_log_with_path()? */
3636 if (TAILQ_EMPTY(&tog_refs)) {
3637 error = tog_load_refs(repo, 0);
3642 if (start_commit == NULL) {
3643 error = got_repo_match_object_id(&start_id, &label,
3644 worktree ? got_worktree_get_head_ref_name(worktree) :
3645 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3648 head_ref_name = label;
3650 error = got_ref_open(&ref, repo, start_commit, 0);
3652 head_ref_name = got_ref_get_name(ref);
3653 else if (error->code != GOT_ERR_NOT_REF)
3655 error = got_repo_match_object_id(&start_id, NULL,
3656 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3661 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3663 error = got_error_from_errno("view_open");
3666 error = open_log_view(view, start_id, repo, head_ref_name,
3667 in_repo_path, log_branches);
3671 /* Release work tree lock. */
3672 got_worktree_close(worktree);
3675 error = view_loop(view);
3685 const struct got_error *close_err = got_repo_close(repo);
3690 got_worktree_close(worktree);
3692 const struct got_error *pack_err =
3693 got_repo_pack_fds_close(pack_fds);
3705 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3706 "[-w] object1 object2\n", getprogname());
3711 match_line(const char *line, regex_t *regex, size_t nmatch,
3712 regmatch_t *regmatch)
3714 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3717 static struct tog_color *
3718 match_color(struct tog_colors *colors, const char *line)
3720 struct tog_color *tc = NULL;
3722 STAILQ_FOREACH(tc, colors, entry) {
3723 if (match_line(line, &tc->regex, 0, NULL))
3730 static const struct got_error *
3731 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3732 WINDOW *window, int skipcol, regmatch_t *regmatch)
3734 const struct got_error *err = NULL;
3736 wchar_t *wline = NULL;
3737 int rme, rms, n, width, scrollx;
3738 int width0 = 0, width1 = 0, width2 = 0;
3739 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3743 rms = regmatch->rm_so;
3744 rme = regmatch->rm_eo;
3746 err = expand_tab(&exstr, line);
3750 /* Split the line into 3 segments, according to match offsets. */
3751 seg0 = strndup(exstr, rms);
3753 err = got_error_from_errno("strndup");
3756 seg1 = strndup(exstr + rms, rme - rms);
3758 err = got_error_from_errno("strndup");
3761 seg2 = strdup(exstr + rme);
3763 err = got_error_from_errno("strndup");
3767 /* draw up to matched token if we haven't scrolled past it */
3768 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3772 n = MAX(width0 - skipcol, 0);
3775 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3776 wlimit, col_tab_align, 1);
3779 waddwstr(window, &wline[scrollx]);
3789 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3793 wlen = wcslen(wline);
3795 width = wcwidth(wline[i]);
3797 /* should not happen, tabs are expanded */
3798 err = got_error(GOT_ERR_RANGE);
3801 if (width0 + w + width > skipcol)
3806 /* draw (visible part of) matched token (if scrolled into it) */
3807 if (width1 - w > 0) {
3808 wattron(window, A_STANDOUT);
3809 waddwstr(window, &wline[i]);
3810 wattroff(window, A_STANDOUT);
3811 wlimit -= (width1 - w);
3812 *wtotal += (width1 - w);
3816 if (wlimit > 0) { /* draw rest of line */
3818 if (skipcol > width0 + width1) {
3819 err = format_line(&wline, &width2, &scrollx, seg2,
3820 skipcol - (width0 + width1), wlimit,
3824 waddwstr(window, &wline[scrollx]);
3826 err = format_line(&wline, &width2, NULL, seg2, 0,
3827 wlimit, col_tab_align, 1);
3830 waddwstr(window, wline);
3843 static const struct got_error *
3844 draw_file(struct tog_view *view, const char *header)
3846 struct tog_diff_view_state *s = &view->state.diff;
3847 regmatch_t *regmatch = &view->regmatch;
3848 const struct got_error *err;
3851 size_t linesize = 0;
3853 struct tog_color *tc;
3856 int max_lines = view->nlines;
3857 int nlines = s->nlines;
3860 line_offset = s->line_offsets[s->first_displayed_line - 1];
3861 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3862 return got_error_from_errno("fseek");
3864 werase(view->window);
3867 if (asprintf(&line, "[%d/%d] %s",
3868 s->first_displayed_line - 1 + s->selected_line, nlines,
3870 return got_error_from_errno("asprintf");
3871 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3877 if (view_needs_focus_indication(view))
3878 wstandout(view->window);
3879 waddwstr(view->window, wline);
3882 if (view_needs_focus_indication(view))
3883 wstandend(view->window);
3884 if (width <= view->ncols - 1)
3885 waddch(view->window, '\n');
3895 while (max_lines > 0 && nprinted < max_lines) {
3896 linelen = getline(&line, &linesize, s->f);
3897 if (linelen == -1) {
3903 return got_ferror(s->f, GOT_ERR_IO);
3906 /* Set view->maxx based on full line length. */
3907 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3913 view->maxx = MAX(view->maxx, width);
3917 tc = match_color(&s->colors, line);
3919 wattr_on(view->window,
3920 COLOR_PAIR(tc->colorpair), NULL);
3921 if (s->first_displayed_line + nprinted == s->matched_line &&
3922 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3923 err = add_matched_line(&width, line, view->ncols, 0,
3924 view->window, view->x, regmatch);
3931 err = format_line(&wline, &width, &skip, line,
3932 view->x, view->ncols, 0, view->x ? 1 : 0);
3937 waddwstr(view->window, &wline[skip]);
3942 wattr_off(view->window,
3943 COLOR_PAIR(tc->colorpair), NULL);
3944 if (width <= view->ncols - 1)
3945 waddch(view->window, '\n');
3950 s->last_displayed_line = s->first_displayed_line +
3953 s->last_displayed_line = s->first_displayed_line;
3958 while (nprinted < view->nlines) {
3959 waddch(view->window, '\n');
3963 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3969 wstandout(view->window);
3970 waddwstr(view->window, wline);
3973 wstandend(view->window);
3980 get_datestr(time_t *time, char *datebuf)
3982 struct tm mytm, *tm;
3985 tm = gmtime_r(time, &mytm);
3988 s = asctime_r(tm, datebuf);
3991 p = strchr(s, '\n');
3997 static const struct got_error *
3998 get_changed_paths(struct got_pathlist_head *paths,
3999 struct got_commit_object *commit, struct got_repository *repo)
4001 const struct got_error *err = NULL;
4002 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4003 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4004 struct got_object_qid *qid;
4006 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4008 struct got_commit_object *pcommit;
4009 err = got_object_open_as_commit(&pcommit, repo,
4014 tree_id1 = got_object_id_dup(
4015 got_object_commit_get_tree_id(pcommit));
4016 if (tree_id1 == NULL) {
4017 got_object_commit_close(pcommit);
4018 return got_error_from_errno("got_object_id_dup");
4020 got_object_commit_close(pcommit);
4025 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4030 tree_id2 = got_object_commit_get_tree_id(commit);
4031 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4035 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4036 got_diff_tree_collect_changed_paths, paths, 0);
4039 got_object_tree_close(tree1);
4041 got_object_tree_close(tree2);
4046 static const struct got_error *
4047 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4051 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4053 return got_error_from_errno("reallocarray");
4055 (*line_offsets)[*nlines] = off;
4060 static const struct got_error *
4061 write_commit_info(off_t **line_offsets, size_t *nlines,
4062 struct got_object_id *commit_id, struct got_reflist_head *refs,
4063 struct got_repository *repo, FILE *outfile)
4065 const struct got_error *err = NULL;
4066 char datebuf[26], *datestr;
4067 struct got_commit_object *commit;
4068 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4069 time_t committer_time;
4070 const char *author, *committer;
4071 char *refs_str = NULL;
4072 struct got_pathlist_head changed_paths;
4073 struct got_pathlist_entry *pe;
4077 TAILQ_INIT(&changed_paths);
4080 err = build_refs_str(&refs_str, refs, commit_id, repo);
4085 err = got_object_open_as_commit(&commit, repo, commit_id);
4089 err = got_object_id_str(&id_str, commit_id);
4091 err = got_error_from_errno("got_object_id_str");
4095 err = add_line_offset(line_offsets, nlines, 0);
4099 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4100 refs_str ? refs_str : "", refs_str ? ")" : "");
4102 err = got_error_from_errno("fprintf");
4106 err = add_line_offset(line_offsets, nlines, outoff);
4110 n = fprintf(outfile, "from: %s\n",
4111 got_object_commit_get_author(commit));
4113 err = got_error_from_errno("fprintf");
4117 err = add_line_offset(line_offsets, nlines, outoff);
4121 committer_time = got_object_commit_get_committer_time(commit);
4122 datestr = get_datestr(&committer_time, datebuf);
4124 n = fprintf(outfile, "date: %s UTC\n", datestr);
4126 err = got_error_from_errno("fprintf");
4130 err = add_line_offset(line_offsets, nlines, outoff);
4134 author = got_object_commit_get_author(commit);
4135 committer = got_object_commit_get_committer(commit);
4136 if (strcmp(author, committer) != 0) {
4137 n = fprintf(outfile, "via: %s\n", committer);
4139 err = got_error_from_errno("fprintf");
4143 err = add_line_offset(line_offsets, nlines, outoff);
4147 if (got_object_commit_get_nparents(commit) > 1) {
4148 const struct got_object_id_queue *parent_ids;
4149 struct got_object_qid *qid;
4151 parent_ids = got_object_commit_get_parent_ids(commit);
4152 STAILQ_FOREACH(qid, parent_ids, entry) {
4153 err = got_object_id_str(&id_str, &qid->id);
4156 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4158 err = got_error_from_errno("fprintf");
4162 err = add_line_offset(line_offsets, nlines, outoff);
4170 err = got_object_commit_get_logmsg(&logmsg, commit);
4174 while ((line = strsep(&s, "\n")) != NULL) {
4175 n = fprintf(outfile, "%s\n", line);
4177 err = got_error_from_errno("fprintf");
4181 err = add_line_offset(line_offsets, nlines, outoff);
4186 err = get_changed_paths(&changed_paths, commit, repo);
4189 TAILQ_FOREACH(pe, &changed_paths, entry) {
4190 struct got_diff_changed_path *cp = pe->data;
4191 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4193 err = got_error_from_errno("fprintf");
4197 err = add_line_offset(line_offsets, nlines, outoff);
4200 free((char *)pe->path);
4204 fputc('\n', outfile);
4206 err = add_line_offset(line_offsets, nlines, outoff);
4208 got_pathlist_free(&changed_paths);
4212 got_object_commit_close(commit);
4214 free(*line_offsets);
4215 *line_offsets = NULL;
4221 static const struct got_error *
4222 create_diff(struct tog_diff_view_state *s)
4224 const struct got_error *err = NULL;
4228 free(s->line_offsets);
4229 s->line_offsets = malloc(sizeof(off_t));
4230 if (s->line_offsets == NULL)
4231 return got_error_from_errno("malloc");
4236 err = got_error_from_errno("got_opentemp");
4239 if (s->f && fclose(s->f) == EOF) {
4240 err = got_error_from_errno("fclose");
4246 err = got_object_get_type(&obj_type, s->repo, s->id1);
4248 err = got_object_get_type(&obj_type, s->repo, s->id2);
4253 case GOT_OBJ_TYPE_BLOB:
4254 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4255 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4256 s->label1, s->label2, tog_diff_algo, s->diff_context,
4257 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4259 case GOT_OBJ_TYPE_TREE:
4260 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4261 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4262 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4263 s->force_text_diff, s->repo, s->f);
4265 case GOT_OBJ_TYPE_COMMIT: {
4266 const struct got_object_id_queue *parent_ids;
4267 struct got_object_qid *pid;
4268 struct got_commit_object *commit2;
4269 struct got_reflist_head *refs;
4271 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4274 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4275 /* Show commit info if we're diffing to a parent/root commit. */
4276 if (s->id1 == NULL) {
4277 err = write_commit_info(&s->line_offsets, &s->nlines,
4278 s->id2, refs, s->repo, s->f);
4282 parent_ids = got_object_commit_get_parent_ids(commit2);
4283 STAILQ_FOREACH(pid, parent_ids, entry) {
4284 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4285 err = write_commit_info(
4286 &s->line_offsets, &s->nlines,
4287 s->id2, refs, s->repo, s->f);
4294 got_object_commit_close(commit2);
4296 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4297 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4298 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4299 s->force_text_diff, s->repo, s->f);
4303 err = got_error(GOT_ERR_OBJ_TYPE);
4309 if (s->f && fflush(s->f) != 0 && err == NULL)
4310 err = got_error_from_errno("fflush");
4315 diff_view_indicate_progress(struct tog_view *view)
4317 mvwaddstr(view->window, 0, 0, "diffing...");
4322 static const struct got_error *
4323 search_start_diff_view(struct tog_view *view)
4325 struct tog_diff_view_state *s = &view->state.diff;
4327 s->matched_line = 0;
4331 static const struct got_error *
4332 search_next_diff_view(struct tog_view *view)
4334 struct tog_diff_view_state *s = &view->state.diff;
4335 const struct got_error *err = NULL;
4338 size_t linesize = 0;
4341 if (!view->searching) {
4342 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4346 if (s->matched_line) {
4347 if (view->searching == TOG_SEARCH_FORWARD)
4348 lineno = s->matched_line + 1;
4350 lineno = s->matched_line - 1;
4352 lineno = s->first_displayed_line;
4357 if (lineno <= 0 || lineno > s->nlines) {
4358 if (s->matched_line == 0) {
4359 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4363 if (view->searching == TOG_SEARCH_FORWARD)
4369 offset = s->line_offsets[lineno - 1];
4370 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4372 return got_error_from_errno("fseeko");
4374 linelen = getline(&line, &linesize, s->f);
4375 if (linelen != -1) {
4377 err = expand_tab(&exstr, line);
4380 if (match_line(exstr, &view->regex, 1,
4382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 s->matched_line = lineno;
4389 if (view->searching == TOG_SEARCH_FORWARD)
4396 if (s->matched_line) {
4397 s->first_displayed_line = s->matched_line;
4398 s->selected_line = 1;
4404 static const struct got_error *
4405 close_diff_view(struct tog_view *view)
4407 const struct got_error *err = NULL;
4408 struct tog_diff_view_state *s = &view->state.diff;
4414 if (s->f && fclose(s->f) == EOF)
4415 err = got_error_from_errno("fclose");
4417 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4418 err = got_error_from_errno("fclose");
4420 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4421 err = got_error_from_errno("fclose");
4423 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4424 err = got_error_from_errno("close");
4426 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4427 err = got_error_from_errno("close");
4429 free_colors(&s->colors);
4430 free(s->line_offsets);
4431 s->line_offsets = NULL;
4436 static const struct got_error *
4437 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4438 struct got_object_id *id2, const char *label1, const char *label2,
4439 int diff_context, int ignore_whitespace, int force_text_diff,
4440 struct tog_view *parent_view, struct got_repository *repo)
4442 const struct got_error *err;
4443 struct tog_diff_view_state *s = &view->state.diff;
4445 memset(s, 0, sizeof(*s));
4449 if (id1 != NULL && id2 != NULL) {
4451 err = got_object_get_type(&type1, repo, id1);
4454 err = got_object_get_type(&type2, repo, id2);
4459 return got_error(GOT_ERR_OBJ_TYPE);
4461 s->first_displayed_line = 1;
4462 s->last_displayed_line = view->nlines;
4463 s->selected_line = 1;
4471 s->id1 = got_object_id_dup(id1);
4473 return got_error_from_errno("got_object_id_dup");
4477 s->id2 = got_object_id_dup(id2);
4478 if (s->id2 == NULL) {
4479 err = got_error_from_errno("got_object_id_dup");
4483 s->f1 = got_opentemp();
4484 if (s->f1 == NULL) {
4485 err = got_error_from_errno("got_opentemp");
4489 s->f2 = got_opentemp();
4490 if (s->f2 == NULL) {
4491 err = got_error_from_errno("got_opentemp");
4495 s->fd1 = got_opentempfd();
4497 err = got_error_from_errno("got_opentempfd");
4501 s->fd2 = got_opentempfd();
4503 err = got_error_from_errno("got_opentempfd");
4507 s->first_displayed_line = 1;
4508 s->last_displayed_line = view->nlines;
4509 s->diff_context = diff_context;
4510 s->ignore_whitespace = ignore_whitespace;
4511 s->force_text_diff = force_text_diff;
4512 s->parent_view = parent_view;
4515 STAILQ_INIT(&s->colors);
4516 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4517 err = add_color(&s->colors,
4518 "^-", TOG_COLOR_DIFF_MINUS,
4519 get_color_value("TOG_COLOR_DIFF_MINUS"));
4522 err = add_color(&s->colors, "^\\+",
4523 TOG_COLOR_DIFF_PLUS,
4524 get_color_value("TOG_COLOR_DIFF_PLUS"));
4527 err = add_color(&s->colors,
4528 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4529 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4533 err = add_color(&s->colors,
4534 "^(commit [0-9a-f]|parent [0-9]|"
4535 "(blob|file|tree|commit) [-+] |"
4536 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4537 get_color_value("TOG_COLOR_DIFF_META"));
4541 err = add_color(&s->colors,
4542 "^(from|via): ", TOG_COLOR_AUTHOR,
4543 get_color_value("TOG_COLOR_AUTHOR"));
4547 err = add_color(&s->colors,
4548 "^date: ", TOG_COLOR_DATE,
4549 get_color_value("TOG_COLOR_DATE"));
4554 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4555 view_is_splitscreen(view))
4556 show_log_view(parent_view); /* draw border */
4557 diff_view_indicate_progress(view);
4559 err = create_diff(s);
4561 view->show = show_diff_view;
4562 view->input = input_diff_view;
4563 view->reset = reset_diff_view;
4564 view->close = close_diff_view;
4565 view->search_start = search_start_diff_view;
4566 view->search_next = search_next_diff_view;
4569 close_diff_view(view);
4573 static const struct got_error *
4574 show_diff_view(struct tog_view *view)
4576 const struct got_error *err;
4577 struct tog_diff_view_state *s = &view->state.diff;
4578 char *id_str1 = NULL, *id_str2, *header;
4579 const char *label1, *label2;
4582 err = got_object_id_str(&id_str1, s->id1);
4585 label1 = s->label1 ? : id_str1;
4587 label1 = "/dev/null";
4589 err = got_object_id_str(&id_str2, s->id2);
4592 label2 = s->label2 ? : id_str2;
4594 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4595 err = got_error_from_errno("asprintf");
4603 err = draw_file(view, header);
4608 static const struct got_error *
4609 set_selected_commit(struct tog_diff_view_state *s,
4610 struct commit_queue_entry *entry)
4612 const struct got_error *err;
4613 const struct got_object_id_queue *parent_ids;
4614 struct got_commit_object *selected_commit;
4615 struct got_object_qid *pid;
4618 s->id2 = got_object_id_dup(entry->id);
4620 return got_error_from_errno("got_object_id_dup");
4622 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4625 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4627 pid = STAILQ_FIRST(parent_ids);
4628 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4629 got_object_commit_close(selected_commit);
4633 static const struct got_error *
4634 reset_diff_view(struct tog_view *view)
4636 struct tog_diff_view_state *s = &view->state.diff;
4639 wclear(view->window);
4640 s->first_displayed_line = 1;
4641 s->last_displayed_line = view->nlines;
4642 s->matched_line = 0;
4643 diff_view_indicate_progress(view);
4644 return create_diff(s);
4647 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4649 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4652 static const struct got_error *
4653 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4655 const struct got_error *err = NULL;
4656 struct tog_diff_view_state *s = &view->state.diff;
4657 struct tog_log_view_state *ls;
4658 struct commit_queue_entry *old_selected_entry;
4660 size_t linesize = 0;
4662 int i, nscroll = view->nlines - 1, up = 0;
4669 view->x = MAX(view->maxx - view->ncols / 3, 0);
4674 if (view->x + view->ncols / 3 < view->maxx)
4675 view->x += 2; /* move two columns right */
4681 view->x -= MIN(view->x, 2); /* move two columns back */
4688 s->force_text_diff = !s->force_text_diff;
4690 s->ignore_whitespace = !s->ignore_whitespace;
4691 err = reset_diff_view(view);
4695 s->first_displayed_line = 1;
4704 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4710 if (s->first_displayed_line > 1)
4711 s->first_displayed_line--;
4722 if (s->first_displayed_line == 1) {
4727 while (i++ < nscroll && s->first_displayed_line > 1)
4728 s->first_displayed_line--;
4734 s->first_displayed_line++;
4751 while (!s->eof && i++ < nscroll) {
4752 linelen = getline(&line, &linesize, s->f);
4753 s->first_displayed_line++;
4754 if (linelen == -1) {
4758 err = got_ferror(s->f, GOT_ERR_IO);
4765 if (s->diff_context > 0) {
4767 s->matched_line = 0;
4768 diff_view_indicate_progress(view);
4769 err = create_diff(s);
4770 if (s->first_displayed_line + view->nlines - 1 >
4772 s->first_displayed_line = 1;
4773 s->last_displayed_line = view->nlines;
4779 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4781 s->matched_line = 0;
4782 diff_view_indicate_progress(view);
4783 err = create_diff(s);
4795 if (s->parent_view == NULL) {
4799 s->parent_view->count = view->count;
4801 if (s->parent_view->type == TOG_VIEW_LOG) {
4802 ls = &s->parent_view->state.log;
4803 old_selected_entry = ls->selected_entry;
4805 err = input_log_view(NULL, s->parent_view,
4806 up ? KEY_UP : KEY_DOWN);
4809 view->count = s->parent_view->count;
4811 if (old_selected_entry == ls->selected_entry)
4814 err = set_selected_commit(s, ls->selected_entry);
4817 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4818 struct tog_blame_view_state *bs;
4819 struct got_object_id *id, *prev_id;
4821 bs = &s->parent_view->state.blame;
4822 prev_id = get_annotation_for_line(bs->blame.lines,
4823 bs->blame.nlines, bs->last_diffed_line);
4825 err = input_blame_view(&view, s->parent_view,
4826 up ? KEY_UP : KEY_DOWN);
4829 view->count = s->parent_view->count;
4831 if (prev_id == NULL)
4833 id = get_selected_commit_id(bs->blame.lines,
4834 bs->blame.nlines, bs->first_displayed_line,
4839 if (!got_object_id_cmp(prev_id, id))
4842 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4846 s->first_displayed_line = 1;
4847 s->last_displayed_line = view->nlines;
4848 s->matched_line = 0;
4851 diff_view_indicate_progress(view);
4852 err = create_diff(s);
4862 static const struct got_error *
4863 cmd_diff(int argc, char *argv[])
4865 const struct got_error *error = NULL;
4866 struct got_repository *repo = NULL;
4867 struct got_worktree *worktree = NULL;
4868 struct got_object_id *id1 = NULL, *id2 = NULL;
4869 char *repo_path = NULL, *cwd = NULL;
4870 char *id_str1 = NULL, *id_str2 = NULL;
4871 char *label1 = NULL, *label2 = NULL;
4872 int diff_context = 3, ignore_whitespace = 0;
4873 int ch, force_text_diff = 0;
4875 struct tog_view *view;
4876 int *pack_fds = NULL;
4878 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4881 force_text_diff = 1;
4884 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4887 errx(1, "number of context lines is %s: %s",
4891 repo_path = realpath(optarg, NULL);
4892 if (repo_path == NULL)
4893 return got_error_from_errno2("realpath",
4895 got_path_strip_trailing_slashes(repo_path);
4898 ignore_whitespace = 1;
4910 usage_diff(); /* TODO show local worktree changes */
4911 } else if (argc == 2) {
4917 error = got_repo_pack_fds_open(&pack_fds);
4921 if (repo_path == NULL) {
4922 cwd = getcwd(NULL, 0);
4924 return got_error_from_errno("getcwd");
4925 error = got_worktree_open(&worktree, cwd);
4926 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4930 strdup(got_worktree_get_repo_path(worktree));
4932 repo_path = strdup(cwd);
4933 if (repo_path == NULL) {
4934 error = got_error_from_errno("strdup");
4939 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4945 error = apply_unveil(got_repo_get_path(repo), NULL);
4949 error = tog_load_refs(repo, 0);
4953 error = got_repo_match_object_id(&id1, &label1, id_str1,
4954 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4958 error = got_repo_match_object_id(&id2, &label2, id_str2,
4959 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4963 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4965 error = got_error_from_errno("view_open");
4968 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4969 ignore_whitespace, force_text_diff, NULL, repo);
4972 error = view_loop(view);
4979 const struct got_error *close_err = got_repo_close(repo);
4984 got_worktree_close(worktree);
4986 const struct got_error *pack_err =
4987 got_repo_pack_fds_close(pack_fds);
5000 "usage: %s blame [-c commit] [-r repository-path] path\n",
5005 struct tog_blame_line {
5007 struct got_object_id *id;
5010 static const struct got_error *
5011 draw_blame(struct tog_view *view)
5013 struct tog_blame_view_state *s = &view->state.blame;
5014 struct tog_blame *blame = &s->blame;
5015 regmatch_t *regmatch = &view->regmatch;
5016 const struct got_error *err;
5017 int lineno = 0, nprinted = 0;
5019 size_t linesize = 0;
5023 struct tog_blame_line *blame_line;
5024 struct got_object_id *prev_id = NULL;
5026 struct tog_color *tc;
5028 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5033 werase(view->window);
5035 if (asprintf(&line, "commit %s", id_str) == -1) {
5036 err = got_error_from_errno("asprintf");
5041 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5046 if (view_needs_focus_indication(view))
5047 wstandout(view->window);
5048 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5050 wattr_on(view->window,
5051 COLOR_PAIR(tc->colorpair), NULL);
5052 waddwstr(view->window, wline);
5054 wattr_off(view->window,
5055 COLOR_PAIR(tc->colorpair), NULL);
5056 if (view_needs_focus_indication(view))
5057 wstandend(view->window);
5060 if (width < view->ncols - 1)
5061 waddch(view->window, '\n');
5063 if (asprintf(&line, "[%d/%d] %s%s",
5064 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5065 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5067 return got_error_from_errno("asprintf");
5070 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5075 waddwstr(view->window, wline);
5078 if (width < view->ncols - 1)
5079 waddch(view->window, '\n');
5083 while (nprinted < view->nlines - 2) {
5084 linelen = getline(&line, &linesize, blame->f);
5085 if (linelen == -1) {
5086 if (feof(blame->f)) {
5091 return got_ferror(blame->f, GOT_ERR_IO);
5093 if (++lineno < s->first_displayed_line)
5096 /* Set view->maxx based on full line length. */
5097 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5104 view->maxx = MAX(view->maxx, width);
5106 if (nprinted == s->selected_line - 1)
5107 wstandout(view->window);
5109 if (blame->nlines > 0) {
5110 blame_line = &blame->lines[lineno - 1];
5111 if (blame_line->annotated && prev_id &&
5112 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5113 !(nprinted == s->selected_line - 1)) {
5114 waddstr(view->window, " ");
5115 } else if (blame_line->annotated) {
5117 err = got_object_id_str(&id_str,
5123 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5125 wattr_on(view->window,
5126 COLOR_PAIR(tc->colorpair), NULL);
5127 wprintw(view->window, "%.8s", id_str);
5129 wattr_off(view->window,
5130 COLOR_PAIR(tc->colorpair), NULL);
5132 prev_id = blame_line->id;
5134 waddstr(view->window, "........");
5138 waddstr(view->window, "........");
5142 if (nprinted == s->selected_line - 1)
5143 wstandend(view->window);
5144 waddstr(view->window, " ");
5146 if (view->ncols <= 9) {
5148 } else if (s->first_displayed_line + nprinted ==
5150 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5151 err = add_matched_line(&width, line, view->ncols - 9, 9,
5152 view->window, view->x, regmatch);
5160 err = format_line(&wline, &width, &skip, line,
5161 view->x, view->ncols - 9, 9, 1);
5166 waddwstr(view->window, &wline[skip]);
5172 if (width <= view->ncols - 1)
5173 waddch(view->window, '\n');
5174 if (++nprinted == 1)
5175 s->first_displayed_line = lineno;
5178 s->last_displayed_line = lineno;
5185 static const struct got_error *
5186 blame_cb(void *arg, int nlines, int lineno,
5187 struct got_commit_object *commit, struct got_object_id *id)
5189 const struct got_error *err = NULL;
5190 struct tog_blame_cb_args *a = arg;
5191 struct tog_blame_line *line;
5194 if (nlines != a->nlines ||
5195 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5196 return got_error(GOT_ERR_RANGE);
5198 errcode = pthread_mutex_lock(&tog_mutex);
5200 return got_error_set_errno(errcode, "pthread_mutex_lock");
5202 if (*a->quit) { /* user has quit the blame view */
5203 err = got_error(GOT_ERR_ITER_COMPLETED);
5208 goto done; /* no change in this commit */
5210 line = &a->lines[lineno - 1];
5211 if (line->annotated)
5214 line->id = got_object_id_dup(id);
5215 if (line->id == NULL) {
5216 err = got_error_from_errno("got_object_id_dup");
5219 line->annotated = 1;
5221 errcode = pthread_mutex_unlock(&tog_mutex);
5223 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5228 blame_thread(void *arg)
5230 const struct got_error *err, *close_err;
5231 struct tog_blame_thread_args *ta = arg;
5232 struct tog_blame_cb_args *a = ta->cb_args;
5233 int errcode, fd1 = -1, fd2 = -1;
5234 FILE *f1 = NULL, *f2 = NULL;
5236 fd1 = got_opentempfd();
5238 return (void *)got_error_from_errno("got_opentempfd");
5240 fd2 = got_opentempfd();
5242 err = got_error_from_errno("got_opentempfd");
5246 f1 = got_opentemp();
5248 err = (void *)got_error_from_errno("got_opentemp");
5251 f2 = got_opentemp();
5253 err = (void *)got_error_from_errno("got_opentemp");
5257 err = block_signals_used_by_main_thread();
5261 err = got_blame(ta->path, a->commit_id, ta->repo,
5262 tog_diff_algo, blame_cb, ta->cb_args,
5263 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5264 if (err && err->code == GOT_ERR_CANCELLED)
5267 errcode = pthread_mutex_lock(&tog_mutex);
5269 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5273 close_err = got_repo_close(ta->repo);
5279 errcode = pthread_mutex_unlock(&tog_mutex);
5280 if (errcode && err == NULL)
5281 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5284 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5285 err = got_error_from_errno("close");
5286 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5287 err = got_error_from_errno("close");
5288 if (f1 && fclose(f1) == EOF && err == NULL)
5289 err = got_error_from_errno("fclose");
5290 if (f2 && fclose(f2) == EOF && err == NULL)
5291 err = got_error_from_errno("fclose");
5296 static struct got_object_id *
5297 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5298 int first_displayed_line, int selected_line)
5300 struct tog_blame_line *line;
5305 line = &lines[first_displayed_line - 1 + selected_line - 1];
5306 if (!line->annotated)
5312 static struct got_object_id *
5313 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5316 struct tog_blame_line *line;
5318 if (nlines <= 0 || lineno >= nlines)
5321 line = &lines[lineno - 1];
5322 if (!line->annotated)
5328 static const struct got_error *
5329 stop_blame(struct tog_blame *blame)
5331 const struct got_error *err = NULL;
5334 if (blame->thread) {
5336 errcode = pthread_mutex_unlock(&tog_mutex);
5338 return got_error_set_errno(errcode,
5339 "pthread_mutex_unlock");
5340 errcode = pthread_join(blame->thread, (void **)&err);
5342 return got_error_set_errno(errcode, "pthread_join");
5343 errcode = pthread_mutex_lock(&tog_mutex);
5345 return got_error_set_errno(errcode,
5346 "pthread_mutex_lock");
5347 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5349 blame->thread = 0; //NULL;
5351 if (blame->thread_args.repo) {
5352 const struct got_error *close_err;
5353 close_err = got_repo_close(blame->thread_args.repo);
5356 blame->thread_args.repo = NULL;
5359 if (fclose(blame->f) == EOF && err == NULL)
5360 err = got_error_from_errno("fclose");
5364 for (i = 0; i < blame->nlines; i++)
5365 free(blame->lines[i].id);
5367 blame->lines = NULL;
5369 free(blame->cb_args.commit_id);
5370 blame->cb_args.commit_id = NULL;
5371 if (blame->pack_fds) {
5372 const struct got_error *pack_err =
5373 got_repo_pack_fds_close(blame->pack_fds);
5376 blame->pack_fds = NULL;
5381 static const struct got_error *
5382 cancel_blame_view(void *arg)
5384 const struct got_error *err = NULL;
5388 errcode = pthread_mutex_lock(&tog_mutex);
5390 return got_error_set_errno(errcode,
5391 "pthread_mutex_unlock");
5394 err = got_error(GOT_ERR_CANCELLED);
5396 errcode = pthread_mutex_unlock(&tog_mutex);
5398 return got_error_set_errno(errcode,
5399 "pthread_mutex_lock");
5404 static const struct got_error *
5405 run_blame(struct tog_view *view)
5407 struct tog_blame_view_state *s = &view->state.blame;
5408 struct tog_blame *blame = &s->blame;
5409 const struct got_error *err = NULL;
5410 struct got_commit_object *commit = NULL;
5411 struct got_blob_object *blob = NULL;
5412 struct got_repository *thread_repo = NULL;
5413 struct got_object_id *obj_id = NULL;
5414 int obj_type, fd = -1;
5415 int *pack_fds = NULL;
5417 err = got_object_open_as_commit(&commit, s->repo,
5418 &s->blamed_commit->id);
5422 fd = got_opentempfd();
5424 err = got_error_from_errno("got_opentempfd");
5428 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5432 err = got_object_get_type(&obj_type, s->repo, obj_id);
5436 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5437 err = got_error(GOT_ERR_OBJ_TYPE);
5441 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5444 blame->f = got_opentemp();
5445 if (blame->f == NULL) {
5446 err = got_error_from_errno("got_opentemp");
5449 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5450 &blame->line_offsets, blame->f, blob);
5453 if (blame->nlines == 0) {
5454 s->blame_complete = 1;
5458 /* Don't include \n at EOF in the blame line count. */
5459 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5462 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5463 if (blame->lines == NULL) {
5464 err = got_error_from_errno("calloc");
5468 err = got_repo_pack_fds_open(&pack_fds);
5471 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5476 blame->pack_fds = pack_fds;
5477 blame->cb_args.view = view;
5478 blame->cb_args.lines = blame->lines;
5479 blame->cb_args.nlines = blame->nlines;
5480 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5481 if (blame->cb_args.commit_id == NULL) {
5482 err = got_error_from_errno("got_object_id_dup");
5485 blame->cb_args.quit = &s->done;
5487 blame->thread_args.path = s->path;
5488 blame->thread_args.repo = thread_repo;
5489 blame->thread_args.cb_args = &blame->cb_args;
5490 blame->thread_args.complete = &s->blame_complete;
5491 blame->thread_args.cancel_cb = cancel_blame_view;
5492 blame->thread_args.cancel_arg = &s->done;
5493 s->blame_complete = 0;
5495 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5496 s->first_displayed_line = 1;
5497 s->last_displayed_line = view->nlines;
5498 s->selected_line = 1;
5500 s->matched_line = 0;
5504 got_object_commit_close(commit);
5505 if (fd != -1 && close(fd) == -1 && err == NULL)
5506 err = got_error_from_errno("close");
5508 got_object_blob_close(blob);
5515 static const struct got_error *
5516 open_blame_view(struct tog_view *view, char *path,
5517 struct got_object_id *commit_id, struct got_repository *repo)
5519 const struct got_error *err = NULL;
5520 struct tog_blame_view_state *s = &view->state.blame;
5522 STAILQ_INIT(&s->blamed_commits);
5524 s->path = strdup(path);
5525 if (s->path == NULL)
5526 return got_error_from_errno("strdup");
5528 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5534 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5535 s->first_displayed_line = 1;
5536 s->last_displayed_line = view->nlines;
5537 s->selected_line = 1;
5538 s->blame_complete = 0;
5540 s->commit_id = commit_id;
5541 memset(&s->blame, 0, sizeof(s->blame));
5543 STAILQ_INIT(&s->colors);
5544 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5545 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5546 get_color_value("TOG_COLOR_COMMIT"));
5551 view->show = show_blame_view;
5552 view->input = input_blame_view;
5553 view->reset = reset_blame_view;
5554 view->close = close_blame_view;
5555 view->search_start = search_start_blame_view;
5556 view->search_next = search_next_blame_view;
5558 return run_blame(view);
5561 static const struct got_error *
5562 close_blame_view(struct tog_view *view)
5564 const struct got_error *err = NULL;
5565 struct tog_blame_view_state *s = &view->state.blame;
5567 if (s->blame.thread)
5568 err = stop_blame(&s->blame);
5570 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5571 struct got_object_qid *blamed_commit;
5572 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5573 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5574 got_object_qid_free(blamed_commit);
5578 free_colors(&s->colors);
5582 static const struct got_error *
5583 search_start_blame_view(struct tog_view *view)
5585 struct tog_blame_view_state *s = &view->state.blame;
5587 s->matched_line = 0;
5591 static const struct got_error *
5592 search_next_blame_view(struct tog_view *view)
5594 struct tog_blame_view_state *s = &view->state.blame;
5595 const struct got_error *err = NULL;
5598 size_t linesize = 0;
5601 if (!view->searching) {
5602 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5606 if (s->matched_line) {
5607 if (view->searching == TOG_SEARCH_FORWARD)
5608 lineno = s->matched_line + 1;
5610 lineno = s->matched_line - 1;
5612 lineno = s->first_displayed_line - 1 + s->selected_line;
5617 if (lineno <= 0 || lineno > s->blame.nlines) {
5618 if (s->matched_line == 0) {
5619 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5623 if (view->searching == TOG_SEARCH_FORWARD)
5626 lineno = s->blame.nlines;
5629 offset = s->blame.line_offsets[lineno - 1];
5630 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5632 return got_error_from_errno("fseeko");
5634 linelen = getline(&line, &linesize, s->blame.f);
5635 if (linelen != -1) {
5637 err = expand_tab(&exstr, line);
5640 if (match_line(exstr, &view->regex, 1,
5642 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5643 s->matched_line = lineno;
5649 if (view->searching == TOG_SEARCH_FORWARD)
5656 if (s->matched_line) {
5657 s->first_displayed_line = s->matched_line;
5658 s->selected_line = 1;
5664 static const struct got_error *
5665 show_blame_view(struct tog_view *view)
5667 const struct got_error *err = NULL;
5668 struct tog_blame_view_state *s = &view->state.blame;
5671 if (s->blame.thread == 0 && !s->blame_complete) {
5672 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5673 &s->blame.thread_args);
5675 return got_error_set_errno(errcode, "pthread_create");
5677 halfdelay(1); /* fast refresh while annotating */
5680 if (s->blame_complete)
5681 halfdelay(10); /* disable fast refresh */
5683 err = draw_blame(view);
5689 static const struct got_error *
5690 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5691 struct got_repository *repo, struct got_object_id *id)
5693 struct tog_view *log_view;
5694 const struct got_error *err = NULL;
5698 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5699 if (log_view == NULL)
5700 return got_error_from_errno("view_open");
5702 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5704 view_close(log_view);
5706 *new_view = log_view;
5711 static const struct got_error *
5712 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5714 const struct got_error *err = NULL, *thread_err = NULL;
5715 struct tog_view *diff_view;
5716 struct tog_blame_view_state *s = &view->state.blame;
5717 int eos, nscroll, begin_y = 0, begin_x = 0;
5719 eos = nscroll = view->nlines - 2;
5720 if (view_is_hsplit_top(view))
5728 view->x = MAX(view->maxx - view->ncols / 3, 0);
5733 if (view->x + view->ncols / 3 < view->maxx)
5734 view->x += 2; /* move two columns right */
5740 view->x -= MIN(view->x, 2); /* move two columns back */
5749 s->selected_line = 1;
5750 s->first_displayed_line = 1;
5755 if (s->blame.nlines < eos) {
5756 s->selected_line = s->blame.nlines;
5757 s->first_displayed_line = 1;
5759 s->selected_line = eos;
5760 s->first_displayed_line = s->blame.nlines - (eos - 1);
5767 if (s->selected_line > 1)
5769 else if (s->selected_line == 1 &&
5770 s->first_displayed_line > 1)
5771 s->first_displayed_line--;
5782 if (s->first_displayed_line == 1) {
5783 if (view->count > 1)
5785 s->selected_line = MAX(1, s->selected_line - nscroll);
5789 if (s->first_displayed_line > nscroll)
5790 s->first_displayed_line -= nscroll;
5792 s->first_displayed_line = 1;
5797 if (s->selected_line < eos && s->first_displayed_line +
5798 s->selected_line <= s->blame.nlines)
5800 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5801 s->first_displayed_line++;
5807 struct got_object_id *id = NULL;
5810 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5811 s->first_displayed_line, s->selected_line);
5815 struct got_commit_object *commit, *pcommit;
5816 struct got_object_qid *pid;
5817 struct got_object_id *blob_id = NULL;
5819 err = got_object_open_as_commit(&commit,
5824 got_object_commit_get_parent_ids(commit));
5826 got_object_commit_close(commit);
5829 /* Check if path history ends here. */
5830 err = got_object_open_as_commit(&pcommit,
5834 err = got_object_id_by_path(&blob_id, s->repo,
5836 got_object_commit_close(pcommit);
5838 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5840 got_object_commit_close(commit);
5843 err = got_object_get_type(&obj_type, s->repo,
5846 /* Can't blame non-blob type objects. */
5847 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5848 got_object_commit_close(commit);
5851 err = got_object_qid_alloc(&s->blamed_commit,
5853 got_object_commit_close(commit);
5855 if (got_object_id_cmp(id,
5856 &s->blamed_commit->id) == 0)
5858 err = got_object_qid_alloc(&s->blamed_commit,
5864 thread_err = stop_blame(&s->blame);
5868 STAILQ_INSERT_HEAD(&s->blamed_commits,
5869 s->blamed_commit, entry);
5870 err = run_blame(view);
5876 struct got_object_qid *first;
5879 first = STAILQ_FIRST(&s->blamed_commits);
5880 if (!got_object_id_cmp(&first->id, s->commit_id))
5883 thread_err = stop_blame(&s->blame);
5887 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5888 got_object_qid_free(s->blamed_commit);
5890 STAILQ_FIRST(&s->blamed_commits);
5891 err = run_blame(view);
5898 s->id_to_log = get_selected_commit_id(s->blame.lines,
5899 s->blame.nlines, s->first_displayed_line, s->selected_line);
5901 err = view_request_new(new_view, view, TOG_VIEW_LOG);
5905 struct got_object_id *id = NULL;
5906 struct got_object_qid *pid;
5907 struct got_commit_object *commit = NULL;
5910 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5911 s->first_displayed_line, s->selected_line);
5914 err = got_object_open_as_commit(&commit, s->repo, id);
5917 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5919 /* traversed from diff view, release diff resources */
5920 err = close_diff_view(*new_view);
5923 diff_view = *new_view;
5925 if (view_is_parent_view(view))
5926 view_get_split(view, &begin_y, &begin_x);
5928 diff_view = view_open(0, 0, begin_y, begin_x,
5930 if (diff_view == NULL) {
5931 got_object_commit_close(commit);
5932 err = got_error_from_errno("view_open");
5936 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5937 id, NULL, NULL, 3, 0, 0, view, s->repo);
5938 got_object_commit_close(commit);
5940 view_close(diff_view);
5943 s->last_diffed_line = s->first_displayed_line - 1 +
5946 break; /* still open from active diff view */
5947 if (view_is_parent_view(view) &&
5948 view->mode == TOG_VIEW_SPLIT_HRZN) {
5949 err = view_init_hsplit(view, begin_y);
5955 diff_view->focussed = 1;
5956 diff_view->mode = view->mode;
5957 diff_view->nlines = view->lines - begin_y;
5958 if (view_is_parent_view(view)) {
5959 view_transfer_size(diff_view, view);
5960 err = view_close_child(view);
5963 err = view_set_child(view, diff_view);
5966 view->focus_child = 1;
5968 *new_view = diff_view;
5981 if (s->last_displayed_line >= s->blame.nlines &&
5982 s->selected_line >= MIN(s->blame.nlines,
5983 view->nlines - 2)) {
5987 if (s->last_displayed_line >= s->blame.nlines &&
5988 s->selected_line < view->nlines - 2) {
5990 MIN(nscroll, s->last_displayed_line -
5991 s->first_displayed_line - s->selected_line + 1);
5993 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5994 s->first_displayed_line += nscroll;
5996 s->first_displayed_line =
5997 s->blame.nlines - (view->nlines - 3);
6000 if (s->selected_line > view->nlines - 2) {
6001 s->selected_line = MIN(s->blame.nlines,
6009 return thread_err ? thread_err : err;
6012 static const struct got_error *
6013 reset_blame_view(struct tog_view *view)
6015 const struct got_error *err;
6016 struct tog_blame_view_state *s = &view->state.blame;
6020 err = stop_blame(&s->blame);
6024 return run_blame(view);
6027 static const struct got_error *
6028 cmd_blame(int argc, char *argv[])
6030 const struct got_error *error;
6031 struct got_repository *repo = NULL;
6032 struct got_worktree *worktree = NULL;
6033 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6034 char *link_target = NULL;
6035 struct got_object_id *commit_id = NULL;
6036 struct got_commit_object *commit = NULL;
6037 char *commit_id_str = NULL;
6039 struct tog_view *view;
6040 int *pack_fds = NULL;
6042 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6045 commit_id_str = optarg;
6048 repo_path = realpath(optarg, NULL);
6049 if (repo_path == NULL)
6050 return got_error_from_errno2("realpath",
6065 error = got_repo_pack_fds_open(&pack_fds);
6069 if (repo_path == NULL) {
6070 cwd = getcwd(NULL, 0);
6072 return got_error_from_errno("getcwd");
6073 error = got_worktree_open(&worktree, cwd);
6074 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6078 strdup(got_worktree_get_repo_path(worktree));
6080 repo_path = strdup(cwd);
6081 if (repo_path == NULL) {
6082 error = got_error_from_errno("strdup");
6087 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6091 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6098 error = apply_unveil(got_repo_get_path(repo), NULL);
6102 error = tog_load_refs(repo, 0);
6106 if (commit_id_str == NULL) {
6107 struct got_reference *head_ref;
6108 error = got_ref_open(&head_ref, repo, worktree ?
6109 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6112 error = got_ref_resolve(&commit_id, repo, head_ref);
6113 got_ref_close(head_ref);
6115 error = got_repo_match_object_id(&commit_id, NULL,
6116 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6121 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6123 error = got_error_from_errno("view_open");
6127 error = got_object_open_as_commit(&commit, repo, commit_id);
6131 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6136 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6141 /* Release work tree lock. */
6142 got_worktree_close(worktree);
6145 error = view_loop(view);
6153 got_object_commit_close(commit);
6155 got_worktree_close(worktree);
6157 const struct got_error *close_err = got_repo_close(repo);
6162 const struct got_error *pack_err =
6163 got_repo_pack_fds_close(pack_fds);
6171 static const struct got_error *
6172 draw_tree_entries(struct tog_view *view, const char *parent_path)
6174 struct tog_tree_view_state *s = &view->state.tree;
6175 const struct got_error *err = NULL;
6176 struct got_tree_entry *te;
6178 struct tog_color *tc;
6179 int width, n, i, nentries;
6180 int limit = view->nlines;
6183 if (view_is_hsplit_top(view))
6184 --limit; /* border */
6186 werase(view->window);
6191 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6195 if (view_needs_focus_indication(view))
6196 wstandout(view->window);
6197 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6199 wattr_on(view->window,
6200 COLOR_PAIR(tc->colorpair), NULL);
6201 waddwstr(view->window, wline);
6203 wattr_off(view->window,
6204 COLOR_PAIR(tc->colorpair), NULL);
6205 if (view_needs_focus_indication(view))
6206 wstandend(view->window);
6209 if (width < view->ncols - 1)
6210 waddch(view->window, '\n');
6213 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6217 waddwstr(view->window, wline);
6220 if (width < view->ncols - 1)
6221 waddch(view->window, '\n');
6224 waddch(view->window, '\n');
6228 if (s->first_displayed_entry == NULL) {
6229 te = got_object_tree_get_first_entry(s->tree);
6230 if (s->selected == 0) {
6232 wstandout(view->window);
6233 s->selected_entry = NULL;
6235 waddstr(view->window, " ..\n"); /* parent directory */
6236 if (s->selected == 0 && view->focussed)
6237 wstandend(view->window);
6244 te = s->first_displayed_entry;
6247 nentries = got_object_tree_get_nentries(s->tree);
6248 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6249 char *line = NULL, *id_str = NULL, *link_target = NULL;
6250 const char *modestr = "";
6253 te = got_object_tree_get_entry(s->tree, i);
6254 mode = got_tree_entry_get_mode(te);
6257 err = got_object_id_str(&id_str,
6258 got_tree_entry_get_id(te));
6260 return got_error_from_errno(
6261 "got_object_id_str");
6263 if (got_object_tree_entry_is_submodule(te))
6265 else if (S_ISLNK(mode)) {
6268 err = got_tree_entry_get_symlink_target(&link_target,
6274 for (i = 0; i < strlen(link_target); i++) {
6275 if (!isprint((unsigned char)link_target[i]))
6276 link_target[i] = '?';
6280 else if (S_ISDIR(mode))
6282 else if (mode & S_IXUSR)
6284 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6285 got_tree_entry_get_name(te), modestr,
6286 link_target ? " -> ": "",
6287 link_target ? link_target : "") == -1) {
6290 return got_error_from_errno("asprintf");
6294 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6300 if (n == s->selected) {
6302 wstandout(view->window);
6303 s->selected_entry = te;
6305 tc = match_color(&s->colors, line);
6307 wattr_on(view->window,
6308 COLOR_PAIR(tc->colorpair), NULL);
6309 waddwstr(view->window, wline);
6311 wattr_off(view->window,
6312 COLOR_PAIR(tc->colorpair), NULL);
6313 if (width < view->ncols - 1)
6314 waddch(view->window, '\n');
6315 if (n == s->selected && view->focussed)
6316 wstandend(view->window);
6322 s->last_displayed_entry = te;
6331 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6333 struct got_tree_entry *te;
6334 int isroot = s->tree == s->root;
6337 if (s->first_displayed_entry == NULL)
6340 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6341 while (i++ < maxscroll) {
6344 s->first_displayed_entry = NULL;
6347 s->first_displayed_entry = te;
6348 te = got_tree_entry_get_prev(s->tree, te);
6352 static const struct got_error *
6353 tree_scroll_down(struct tog_view *view, int maxscroll)
6355 struct tog_tree_view_state *s = &view->state.tree;
6356 struct got_tree_entry *next, *last;
6359 if (s->first_displayed_entry)
6360 next = got_tree_entry_get_next(s->tree,
6361 s->first_displayed_entry);
6363 next = got_object_tree_get_first_entry(s->tree);
6365 last = s->last_displayed_entry;
6366 while (next && n++ < maxscroll) {
6368 last = got_tree_entry_get_next(s->tree, last);
6369 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6370 s->first_displayed_entry = next;
6371 next = got_tree_entry_get_next(s->tree, next);
6378 static const struct got_error *
6379 tree_entry_path(char **path, struct tog_parent_trees *parents,
6380 struct got_tree_entry *te)
6382 const struct got_error *err = NULL;
6383 struct tog_parent_tree *pt;
6384 size_t len = 2; /* for leading slash and NUL */
6386 TAILQ_FOREACH(pt, parents, entry)
6387 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6390 len += strlen(got_tree_entry_get_name(te));
6392 *path = calloc(1, len);
6394 return got_error_from_errno("calloc");
6397 pt = TAILQ_LAST(parents, tog_parent_trees);
6399 const char *name = got_tree_entry_get_name(pt->selected_entry);
6400 if (strlcat(*path, name, len) >= len) {
6401 err = got_error(GOT_ERR_NO_SPACE);
6404 if (strlcat(*path, "/", len) >= len) {
6405 err = got_error(GOT_ERR_NO_SPACE);
6408 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6411 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6412 err = got_error(GOT_ERR_NO_SPACE);
6424 static const struct got_error *
6425 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6426 struct got_tree_entry *te, struct tog_parent_trees *parents,
6427 struct got_object_id *commit_id, struct got_repository *repo)
6429 const struct got_error *err = NULL;
6431 struct tog_view *blame_view;
6435 err = tree_entry_path(&path, parents, te);
6439 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6440 if (blame_view == NULL) {
6441 err = got_error_from_errno("view_open");
6445 err = open_blame_view(blame_view, path, commit_id, repo);
6447 if (err->code == GOT_ERR_CANCELLED)
6449 view_close(blame_view);
6451 *new_view = blame_view;
6457 static const struct got_error *
6458 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6459 struct tog_tree_view_state *s)
6461 struct tog_view *log_view;
6462 const struct got_error *err = NULL;
6467 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6468 if (log_view == NULL)
6469 return got_error_from_errno("view_open");
6471 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6475 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6478 view_close(log_view);
6480 *new_view = log_view;
6485 static const struct got_error *
6486 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6487 const char *head_ref_name, struct got_repository *repo)
6489 const struct got_error *err = NULL;
6490 char *commit_id_str = NULL;
6491 struct tog_tree_view_state *s = &view->state.tree;
6492 struct got_commit_object *commit = NULL;
6494 TAILQ_INIT(&s->parents);
6495 STAILQ_INIT(&s->colors);
6497 s->commit_id = got_object_id_dup(commit_id);
6498 if (s->commit_id == NULL)
6499 return got_error_from_errno("got_object_id_dup");
6501 err = got_object_open_as_commit(&commit, repo, commit_id);
6506 * The root is opened here and will be closed when the view is closed.
6507 * Any visited subtrees and their path-wise parents are opened and
6510 err = got_object_open_as_tree(&s->root, repo,
6511 got_object_commit_get_tree_id(commit));
6516 err = got_object_id_str(&commit_id_str, commit_id);
6520 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6521 err = got_error_from_errno("asprintf");
6525 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6526 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6527 if (head_ref_name) {
6528 s->head_ref_name = strdup(head_ref_name);
6529 if (s->head_ref_name == NULL) {
6530 err = got_error_from_errno("strdup");
6536 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6537 err = add_color(&s->colors, "\\$$",
6538 TOG_COLOR_TREE_SUBMODULE,
6539 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6542 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6543 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6546 err = add_color(&s->colors, "/$",
6547 TOG_COLOR_TREE_DIRECTORY,
6548 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6552 err = add_color(&s->colors, "\\*$",
6553 TOG_COLOR_TREE_EXECUTABLE,
6554 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6558 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6559 get_color_value("TOG_COLOR_COMMIT"));
6564 view->show = show_tree_view;
6565 view->input = input_tree_view;
6566 view->close = close_tree_view;
6567 view->search_start = search_start_tree_view;
6568 view->search_next = search_next_tree_view;
6570 free(commit_id_str);
6572 got_object_commit_close(commit);
6574 close_tree_view(view);
6578 static const struct got_error *
6579 close_tree_view(struct tog_view *view)
6581 struct tog_tree_view_state *s = &view->state.tree;
6583 free_colors(&s->colors);
6584 free(s->tree_label);
6585 s->tree_label = NULL;
6587 s->commit_id = NULL;
6588 free(s->head_ref_name);
6589 s->head_ref_name = NULL;
6590 while (!TAILQ_EMPTY(&s->parents)) {
6591 struct tog_parent_tree *parent;
6592 parent = TAILQ_FIRST(&s->parents);
6593 TAILQ_REMOVE(&s->parents, parent, entry);
6594 if (parent->tree != s->root)
6595 got_object_tree_close(parent->tree);
6599 if (s->tree != NULL && s->tree != s->root)
6600 got_object_tree_close(s->tree);
6602 got_object_tree_close(s->root);
6606 static const struct got_error *
6607 search_start_tree_view(struct tog_view *view)
6609 struct tog_tree_view_state *s = &view->state.tree;
6611 s->matched_entry = NULL;
6616 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6618 regmatch_t regmatch;
6620 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6624 static const struct got_error *
6625 search_next_tree_view(struct tog_view *view)
6627 struct tog_tree_view_state *s = &view->state.tree;
6628 struct got_tree_entry *te = NULL;
6630 if (!view->searching) {
6631 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6635 if (s->matched_entry) {
6636 if (view->searching == TOG_SEARCH_FORWARD) {
6637 if (s->selected_entry)
6638 te = got_tree_entry_get_next(s->tree,
6641 te = got_object_tree_get_first_entry(s->tree);
6643 if (s->selected_entry == NULL)
6644 te = got_object_tree_get_last_entry(s->tree);
6646 te = got_tree_entry_get_prev(s->tree,
6650 if (s->selected_entry)
6651 te = s->selected_entry;
6652 else if (view->searching == TOG_SEARCH_FORWARD)
6653 te = got_object_tree_get_first_entry(s->tree);
6655 te = got_object_tree_get_last_entry(s->tree);
6660 if (s->matched_entry == NULL) {
6661 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6664 if (view->searching == TOG_SEARCH_FORWARD)
6665 te = got_object_tree_get_first_entry(s->tree);
6667 te = got_object_tree_get_last_entry(s->tree);
6670 if (match_tree_entry(te, &view->regex)) {
6671 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6672 s->matched_entry = te;
6676 if (view->searching == TOG_SEARCH_FORWARD)
6677 te = got_tree_entry_get_next(s->tree, te);
6679 te = got_tree_entry_get_prev(s->tree, te);
6682 if (s->matched_entry) {
6683 s->first_displayed_entry = s->matched_entry;
6690 static const struct got_error *
6691 show_tree_view(struct tog_view *view)
6693 const struct got_error *err = NULL;
6694 struct tog_tree_view_state *s = &view->state.tree;
6697 err = tree_entry_path(&parent_path, &s->parents, NULL);
6701 err = draw_tree_entries(view, parent_path);
6708 static const struct got_error *
6709 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6711 const struct got_error *err = NULL;
6712 struct tog_tree_view_state *s = &view->state.tree;
6713 struct got_tree_entry *te;
6714 int n, nscroll = view->nlines - 3;
6718 s->show_ids = !s->show_ids;
6723 if (!s->selected_entry)
6725 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6729 err = view_request_new(new_view, view, TOG_VIEW_REF);
6735 if (s->tree == s->root)
6736 s->first_displayed_entry =
6737 got_object_tree_get_first_entry(s->tree);
6739 s->first_displayed_entry = NULL;
6743 int eos = view->nlines - 3;
6745 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6749 te = got_object_tree_get_last_entry(s->tree);
6750 for (n = 0; n < eos; n++) {
6752 if (s->tree != s->root) {
6753 s->first_displayed_entry = NULL;
6758 s->first_displayed_entry = te;
6759 te = got_tree_entry_get_prev(s->tree, te);
6762 s->selected = n - 1;
6768 if (s->selected > 0) {
6772 tree_scroll_up(s, 1);
6773 if (s->selected_entry == NULL ||
6774 (s->tree == s->root && s->selected_entry ==
6775 got_object_tree_get_first_entry(s->tree)))
6785 if (s->tree == s->root) {
6786 if (got_object_tree_get_first_entry(s->tree) ==
6787 s->first_displayed_entry)
6788 s->selected -= MIN(s->selected, nscroll);
6790 if (s->first_displayed_entry == NULL)
6791 s->selected -= MIN(s->selected, nscroll);
6793 tree_scroll_up(s, MAX(0, nscroll));
6794 if (s->selected_entry == NULL ||
6795 (s->tree == s->root && s->selected_entry ==
6796 got_object_tree_get_first_entry(s->tree)))
6802 if (s->selected < s->ndisplayed - 1) {
6806 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6808 /* can't scroll any further */
6812 tree_scroll_down(view, 1);
6822 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6824 /* can't scroll any further; move cursor down */
6825 if (s->selected < s->ndisplayed - 1)
6826 s->selected += MIN(nscroll,
6827 s->ndisplayed - s->selected - 1);
6832 tree_scroll_down(view, nscroll);
6837 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6838 struct tog_parent_tree *parent;
6839 /* user selected '..' */
6840 if (s->tree == s->root) {
6844 parent = TAILQ_FIRST(&s->parents);
6845 TAILQ_REMOVE(&s->parents, parent,
6847 got_object_tree_close(s->tree);
6848 s->tree = parent->tree;
6849 s->first_displayed_entry =
6850 parent->first_displayed_entry;
6852 parent->selected_entry;
6853 s->selected = parent->selected;
6854 if (s->selected > view->nlines - 3) {
6855 err = offset_selection_down(view);
6860 } else if (S_ISDIR(got_tree_entry_get_mode(
6861 s->selected_entry))) {
6862 struct got_tree_object *subtree;
6864 err = got_object_open_as_tree(&subtree, s->repo,
6865 got_tree_entry_get_id(s->selected_entry));
6868 err = tree_view_visit_subtree(s, subtree);
6870 got_object_tree_close(subtree);
6873 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
6874 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
6877 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6878 s->selected = view->nlines - 4;
6894 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6899 static const struct got_error *
6900 cmd_tree(int argc, char *argv[])
6902 const struct got_error *error;
6903 struct got_repository *repo = NULL;
6904 struct got_worktree *worktree = NULL;
6905 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6906 struct got_object_id *commit_id = NULL;
6907 struct got_commit_object *commit = NULL;
6908 const char *commit_id_arg = NULL;
6910 struct got_reference *ref = NULL;
6911 const char *head_ref_name = NULL;
6913 struct tog_view *view;
6914 int *pack_fds = NULL;
6916 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6919 commit_id_arg = optarg;
6922 repo_path = realpath(optarg, NULL);
6923 if (repo_path == NULL)
6924 return got_error_from_errno2("realpath",
6939 error = got_repo_pack_fds_open(&pack_fds);
6943 if (repo_path == NULL) {
6944 cwd = getcwd(NULL, 0);
6946 return got_error_from_errno("getcwd");
6947 error = got_worktree_open(&worktree, cwd);
6948 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6952 strdup(got_worktree_get_repo_path(worktree));
6954 repo_path = strdup(cwd);
6955 if (repo_path == NULL) {
6956 error = got_error_from_errno("strdup");
6961 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6965 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6972 error = apply_unveil(got_repo_get_path(repo), NULL);
6976 error = tog_load_refs(repo, 0);
6980 if (commit_id_arg == NULL) {
6981 error = got_repo_match_object_id(&commit_id, &label,
6982 worktree ? got_worktree_get_head_ref_name(worktree) :
6983 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6986 head_ref_name = label;
6988 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6990 head_ref_name = got_ref_get_name(ref);
6991 else if (error->code != GOT_ERR_NOT_REF)
6993 error = got_repo_match_object_id(&commit_id, NULL,
6994 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6999 error = got_object_open_as_commit(&commit, repo, commit_id);
7003 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7005 error = got_error_from_errno("view_open");
7008 error = open_tree_view(view, commit_id, head_ref_name, repo);
7011 if (!got_path_is_root_dir(in_repo_path)) {
7012 error = tree_view_walk_path(&view->state.tree, commit,
7019 /* Release work tree lock. */
7020 got_worktree_close(worktree);
7023 error = view_loop(view);
7032 const struct got_error *close_err = got_repo_close(repo);
7037 const struct got_error *pack_err =
7038 got_repo_pack_fds_close(pack_fds);
7046 static const struct got_error *
7047 ref_view_load_refs(struct tog_ref_view_state *s)
7049 struct got_reflist_entry *sre;
7050 struct tog_reflist_entry *re;
7053 TAILQ_FOREACH(sre, &tog_refs, entry) {
7054 if (strncmp(got_ref_get_name(sre->ref),
7055 "refs/got/", 9) == 0 &&
7056 strncmp(got_ref_get_name(sre->ref),
7057 "refs/got/backup/", 16) != 0)
7060 re = malloc(sizeof(*re));
7062 return got_error_from_errno("malloc");
7064 re->ref = got_ref_dup(sre->ref);
7065 if (re->ref == NULL)
7066 return got_error_from_errno("got_ref_dup");
7067 re->idx = s->nrefs++;
7068 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7071 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7076 ref_view_free_refs(struct tog_ref_view_state *s)
7078 struct tog_reflist_entry *re;
7080 while (!TAILQ_EMPTY(&s->refs)) {
7081 re = TAILQ_FIRST(&s->refs);
7082 TAILQ_REMOVE(&s->refs, re, entry);
7083 got_ref_close(re->ref);
7088 static const struct got_error *
7089 open_ref_view(struct tog_view *view, struct got_repository *repo)
7091 const struct got_error *err = NULL;
7092 struct tog_ref_view_state *s = &view->state.ref;
7094 s->selected_entry = 0;
7097 TAILQ_INIT(&s->refs);
7098 STAILQ_INIT(&s->colors);
7100 err = ref_view_load_refs(s);
7104 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7105 err = add_color(&s->colors, "^refs/heads/",
7106 TOG_COLOR_REFS_HEADS,
7107 get_color_value("TOG_COLOR_REFS_HEADS"));
7111 err = add_color(&s->colors, "^refs/tags/",
7112 TOG_COLOR_REFS_TAGS,
7113 get_color_value("TOG_COLOR_REFS_TAGS"));
7117 err = add_color(&s->colors, "^refs/remotes/",
7118 TOG_COLOR_REFS_REMOTES,
7119 get_color_value("TOG_COLOR_REFS_REMOTES"));
7123 err = add_color(&s->colors, "^refs/got/backup/",
7124 TOG_COLOR_REFS_BACKUP,
7125 get_color_value("TOG_COLOR_REFS_BACKUP"));
7130 view->show = show_ref_view;
7131 view->input = input_ref_view;
7132 view->close = close_ref_view;
7133 view->search_start = search_start_ref_view;
7134 view->search_next = search_next_ref_view;
7137 free_colors(&s->colors);
7141 static const struct got_error *
7142 close_ref_view(struct tog_view *view)
7144 struct tog_ref_view_state *s = &view->state.ref;
7146 ref_view_free_refs(s);
7147 free_colors(&s->colors);
7152 static const struct got_error *
7153 resolve_reflist_entry(struct got_object_id **commit_id,
7154 struct tog_reflist_entry *re, struct got_repository *repo)
7156 const struct got_error *err = NULL;
7157 struct got_object_id *obj_id;
7158 struct got_tag_object *tag = NULL;
7163 err = got_ref_resolve(&obj_id, repo, re->ref);
7167 err = got_object_get_type(&obj_type, repo, obj_id);
7172 case GOT_OBJ_TYPE_COMMIT:
7173 *commit_id = obj_id;
7175 case GOT_OBJ_TYPE_TAG:
7176 err = got_object_open_as_tag(&tag, repo, obj_id);
7180 err = got_object_get_type(&obj_type, repo,
7181 got_object_tag_get_object_id(tag));
7184 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7185 err = got_error(GOT_ERR_OBJ_TYPE);
7188 *commit_id = got_object_id_dup(
7189 got_object_tag_get_object_id(tag));
7190 if (*commit_id == NULL) {
7191 err = got_error_from_errno("got_object_id_dup");
7196 err = got_error(GOT_ERR_OBJ_TYPE);
7202 got_object_tag_close(tag);
7210 static const struct got_error *
7211 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7212 struct tog_reflist_entry *re, struct got_repository *repo)
7214 struct tog_view *log_view;
7215 const struct got_error *err = NULL;
7216 struct got_object_id *commit_id = NULL;
7220 err = resolve_reflist_entry(&commit_id, re, repo);
7222 if (err->code != GOT_ERR_OBJ_TYPE)
7228 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7229 if (log_view == NULL) {
7230 err = got_error_from_errno("view_open");
7234 err = open_log_view(log_view, commit_id, repo,
7235 got_ref_get_name(re->ref), "", 0);
7238 view_close(log_view);
7240 *new_view = log_view;
7246 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7248 struct tog_reflist_entry *re;
7251 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7254 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7255 while (i++ < maxscroll) {
7258 s->first_displayed_entry = re;
7259 re = TAILQ_PREV(re, tog_reflist_head, entry);
7263 static const struct got_error *
7264 ref_scroll_down(struct tog_view *view, int maxscroll)
7266 struct tog_ref_view_state *s = &view->state.ref;
7267 struct tog_reflist_entry *next, *last;
7270 if (s->first_displayed_entry)
7271 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7273 next = TAILQ_FIRST(&s->refs);
7275 last = s->last_displayed_entry;
7276 while (next && n++ < maxscroll) {
7278 last = TAILQ_NEXT(last, entry);
7279 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7280 s->first_displayed_entry = next;
7281 next = TAILQ_NEXT(next, entry);
7288 static const struct got_error *
7289 search_start_ref_view(struct tog_view *view)
7291 struct tog_ref_view_state *s = &view->state.ref;
7293 s->matched_entry = NULL;
7298 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7300 regmatch_t regmatch;
7302 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7306 static const struct got_error *
7307 search_next_ref_view(struct tog_view *view)
7309 struct tog_ref_view_state *s = &view->state.ref;
7310 struct tog_reflist_entry *re = NULL;
7312 if (!view->searching) {
7313 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7317 if (s->matched_entry) {
7318 if (view->searching == TOG_SEARCH_FORWARD) {
7319 if (s->selected_entry)
7320 re = TAILQ_NEXT(s->selected_entry, entry);
7322 re = TAILQ_PREV(s->selected_entry,
7323 tog_reflist_head, entry);
7325 if (s->selected_entry == NULL)
7326 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7328 re = TAILQ_PREV(s->selected_entry,
7329 tog_reflist_head, entry);
7332 if (s->selected_entry)
7333 re = s->selected_entry;
7334 else if (view->searching == TOG_SEARCH_FORWARD)
7335 re = TAILQ_FIRST(&s->refs);
7337 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7342 if (s->matched_entry == NULL) {
7343 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7346 if (view->searching == TOG_SEARCH_FORWARD)
7347 re = TAILQ_FIRST(&s->refs);
7349 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7352 if (match_reflist_entry(re, &view->regex)) {
7353 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7354 s->matched_entry = re;
7358 if (view->searching == TOG_SEARCH_FORWARD)
7359 re = TAILQ_NEXT(re, entry);
7361 re = TAILQ_PREV(re, tog_reflist_head, entry);
7364 if (s->matched_entry) {
7365 s->first_displayed_entry = s->matched_entry;
7372 static const struct got_error *
7373 show_ref_view(struct tog_view *view)
7375 const struct got_error *err = NULL;
7376 struct tog_ref_view_state *s = &view->state.ref;
7377 struct tog_reflist_entry *re;
7380 struct tog_color *tc;
7382 int limit = view->nlines;
7384 werase(view->window);
7387 if (view_is_hsplit_top(view))
7388 --limit; /* border */
7393 re = s->first_displayed_entry;
7395 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7397 return got_error_from_errno("asprintf");
7399 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7404 if (view_needs_focus_indication(view))
7405 wstandout(view->window);
7406 waddwstr(view->window, wline);
7407 if (view_needs_focus_indication(view))
7408 wstandend(view->window);
7413 if (width < view->ncols - 1)
7414 waddch(view->window, '\n');
7419 while (re && limit > 0) {
7421 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7424 struct got_commit_object *ci;
7425 struct got_tag_object *tag;
7426 struct got_object_id *id;
7430 err = got_ref_resolve(&id, s->repo, re->ref);
7433 err = got_object_open_as_tag(&tag, s->repo, id);
7435 if (err->code != GOT_ERR_OBJ_TYPE) {
7439 err = got_object_open_as_commit(&ci, s->repo,
7445 t = got_object_commit_get_committer_time(ci);
7446 got_object_commit_close(ci);
7448 t = got_object_tag_get_tagger_time(tag);
7449 got_object_tag_close(tag);
7452 if (gmtime_r(&t, &tm) == NULL)
7453 return got_error_from_errno("gmtime_r");
7454 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7455 return got_error(GOT_ERR_NO_SPACE);
7457 if (got_ref_is_symbolic(re->ref)) {
7458 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7459 ymd : "", got_ref_get_name(re->ref),
7460 got_ref_get_symref_target(re->ref)) == -1)
7461 return got_error_from_errno("asprintf");
7462 } else if (s->show_ids) {
7463 struct got_object_id *id;
7465 err = got_ref_resolve(&id, s->repo, re->ref);
7468 err = got_object_id_str(&id_str, id);
7473 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7474 got_ref_get_name(re->ref), id_str) == -1) {
7475 err = got_error_from_errno("asprintf");
7482 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7483 got_ref_get_name(re->ref)) == -1)
7484 return got_error_from_errno("asprintf");
7486 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7492 if (n == s->selected) {
7494 wstandout(view->window);
7495 s->selected_entry = re;
7497 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7499 wattr_on(view->window,
7500 COLOR_PAIR(tc->colorpair), NULL);
7501 waddwstr(view->window, wline);
7503 wattr_off(view->window,
7504 COLOR_PAIR(tc->colorpair), NULL);
7505 if (width < view->ncols - 1)
7506 waddch(view->window, '\n');
7507 if (n == s->selected && view->focussed)
7508 wstandend(view->window);
7514 s->last_displayed_entry = re;
7517 re = TAILQ_NEXT(re, entry);
7524 static const struct got_error *
7525 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7526 struct tog_reflist_entry *re, struct got_repository *repo)
7528 const struct got_error *err = NULL;
7529 struct got_object_id *commit_id = NULL;
7530 struct tog_view *tree_view;
7534 err = resolve_reflist_entry(&commit_id, re, repo);
7536 if (err->code != GOT_ERR_OBJ_TYPE)
7543 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7544 if (tree_view == NULL) {
7545 err = got_error_from_errno("view_open");
7549 err = open_tree_view(tree_view, commit_id,
7550 got_ref_get_name(re->ref), repo);
7554 *new_view = tree_view;
7559 static const struct got_error *
7560 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7562 const struct got_error *err = NULL;
7563 struct tog_ref_view_state *s = &view->state.ref;
7564 struct tog_reflist_entry *re;
7565 int n, nscroll = view->nlines - 1;
7569 s->show_ids = !s->show_ids;
7573 s->show_date = !s->show_date;
7577 s->sort_by_date = !s->sort_by_date;
7579 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7580 got_ref_cmp_by_commit_timestamp_descending :
7581 tog_ref_cmp_by_name, s->repo);
7584 got_reflist_object_id_map_free(tog_refs_idmap);
7585 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7586 &tog_refs, s->repo);
7589 ref_view_free_refs(s);
7590 err = ref_view_load_refs(s);
7595 if (!s->selected_entry)
7597 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7601 if (!s->selected_entry)
7603 err = view_request_new(new_view, view, TOG_VIEW_TREE);
7609 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7613 int eos = view->nlines - 1;
7615 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7619 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7620 for (n = 0; n < eos; n++) {
7623 s->first_displayed_entry = re;
7624 re = TAILQ_PREV(re, tog_reflist_head, entry);
7627 s->selected = n - 1;
7633 if (s->selected > 0) {
7637 ref_scroll_up(s, 1);
7638 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7648 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7649 s->selected -= MIN(nscroll, s->selected);
7650 ref_scroll_up(s, MAX(0, nscroll));
7651 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7657 if (s->selected < s->ndisplayed - 1) {
7661 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7662 /* can't scroll any further */
7666 ref_scroll_down(view, 1);
7676 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7677 /* can't scroll any further; move cursor down */
7678 if (s->selected < s->ndisplayed - 1)
7679 s->selected += MIN(nscroll,
7680 s->ndisplayed - s->selected - 1);
7681 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7682 s->selected += s->ndisplayed - s->selected - 1;
7686 ref_scroll_down(view, nscroll);
7691 err = tog_load_refs(s->repo, s->sort_by_date);
7694 ref_view_free_refs(s);
7695 err = ref_view_load_refs(s);
7698 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7699 s->selected = view->nlines - 2;
7713 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7718 static const struct got_error *
7719 cmd_ref(int argc, char *argv[])
7721 const struct got_error *error;
7722 struct got_repository *repo = NULL;
7723 struct got_worktree *worktree = NULL;
7724 char *cwd = NULL, *repo_path = NULL;
7726 struct tog_view *view;
7727 int *pack_fds = NULL;
7729 while ((ch = getopt(argc, argv, "r:")) != -1) {
7732 repo_path = realpath(optarg, NULL);
7733 if (repo_path == NULL)
7734 return got_error_from_errno2("realpath",
7749 error = got_repo_pack_fds_open(&pack_fds);
7753 if (repo_path == NULL) {
7754 cwd = getcwd(NULL, 0);
7756 return got_error_from_errno("getcwd");
7757 error = got_worktree_open(&worktree, cwd);
7758 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7762 strdup(got_worktree_get_repo_path(worktree));
7764 repo_path = strdup(cwd);
7765 if (repo_path == NULL) {
7766 error = got_error_from_errno("strdup");
7771 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7777 error = apply_unveil(got_repo_get_path(repo), NULL);
7781 error = tog_load_refs(repo, 0);
7785 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7787 error = got_error_from_errno("view_open");
7791 error = open_ref_view(view, repo);
7796 /* Release work tree lock. */
7797 got_worktree_close(worktree);
7800 error = view_loop(view);
7805 const struct got_error *close_err = got_repo_close(repo);
7810 const struct got_error *pack_err =
7811 got_repo_pack_fds_close(pack_fds);
7819 static const struct got_error *
7820 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
7821 enum tog_view_type request, int y, int x)
7823 const struct got_error *err = NULL;
7829 if (view->type == TOG_VIEW_LOG) {
7830 struct tog_log_view_state *s = &view->state.log;
7832 err = open_diff_view_for_commit(new_view, y, x,
7833 s->selected_entry->commit, s->selected_entry->id,
7836 return got_error_msg(GOT_ERR_NOT_IMPL,
7837 "parent/child view pair not supported");
7839 case TOG_VIEW_BLAME:
7840 if (view->type == TOG_VIEW_TREE) {
7841 struct tog_tree_view_state *s = &view->state.tree;
7843 err = blame_tree_entry(new_view, y, x,
7844 s->selected_entry, &s->parents, s->commit_id,
7847 return got_error_msg(GOT_ERR_NOT_IMPL,
7848 "parent/child view pair not supported");
7851 if (view->type == TOG_VIEW_BLAME)
7852 err = log_annotated_line(new_view, y, x,
7853 view->state.blame.repo, view->state.blame.id_to_log);
7854 else if (view->type == TOG_VIEW_TREE)
7855 err = log_selected_tree_entry(new_view, y, x,
7857 else if (view->type == TOG_VIEW_REF)
7858 err = log_ref_entry(new_view, y, x,
7859 view->state.ref.selected_entry,
7860 view->state.ref.repo);
7862 return got_error_msg(GOT_ERR_NOT_IMPL,
7863 "parent/child view pair not supported");
7866 if (view->type == TOG_VIEW_LOG)
7867 err = browse_commit_tree(new_view, y, x,
7868 view->state.log.selected_entry,
7869 view->state.log.in_repo_path,
7870 view->state.log.head_ref_name,
7871 view->state.log.repo);
7872 else if (view->type == TOG_VIEW_REF)
7873 err = browse_ref_tree(new_view, y, x,
7874 view->state.ref.selected_entry,
7875 view->state.ref.repo);
7877 return got_error_msg(GOT_ERR_NOT_IMPL,
7878 "parent/child view pair not supported");
7881 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
7882 if (*new_view == NULL)
7883 return got_error_from_errno("view_open");
7884 if (view->type == TOG_VIEW_LOG)
7885 err = open_ref_view(*new_view, view->state.log.repo);
7886 else if (view->type == TOG_VIEW_TREE)
7887 err = open_ref_view(*new_view, view->state.tree.repo);
7889 err = got_error_msg(GOT_ERR_NOT_IMPL,
7890 "parent/child view pair not supported");
7892 view_close(*new_view);
7895 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
7902 * If view was scrolled down to move the selected line into view when opening a
7903 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7906 offset_selection_up(struct tog_view *view)
7908 switch (view->type) {
7909 case TOG_VIEW_BLAME: {
7910 struct tog_blame_view_state *s = &view->state.blame;
7911 if (s->first_displayed_line == 1) {
7912 s->selected_line = MAX(s->selected_line - view->offset,
7916 if (s->first_displayed_line > view->offset)
7917 s->first_displayed_line -= view->offset;
7919 s->first_displayed_line = 1;
7920 s->selected_line += view->offset;
7924 log_scroll_up(&view->state.log, view->offset);
7925 view->state.log.selected += view->offset;
7928 ref_scroll_up(&view->state.ref, view->offset);
7929 view->state.ref.selected += view->offset;
7932 tree_scroll_up(&view->state.tree, view->offset);
7933 view->state.tree.selected += view->offset;
7943 * If the selected line is in the section of screen covered by the bottom split,
7944 * scroll down offset lines to move it into view and index its new position.
7946 static const struct got_error *
7947 offset_selection_down(struct tog_view *view)
7949 const struct got_error *err = NULL;
7950 const struct got_error *(*scrolld)(struct tog_view *, int);
7951 int *selected = NULL;
7954 switch (view->type) {
7955 case TOG_VIEW_BLAME: {
7956 struct tog_blame_view_state *s = &view->state.blame;
7959 if (s->selected_line > view->nlines - header) {
7960 offset = abs(view->nlines - s->selected_line - header);
7961 s->first_displayed_line += offset;
7962 s->selected_line -= offset;
7963 view->offset = offset;
7967 case TOG_VIEW_LOG: {
7968 struct tog_log_view_state *s = &view->state.log;
7969 scrolld = &log_scroll_down;
7970 header = view_is_parent_view(view) ? 3 : 2;
7971 selected = &s->selected;
7974 case TOG_VIEW_REF: {
7975 struct tog_ref_view_state *s = &view->state.ref;
7976 scrolld = &ref_scroll_down;
7978 selected = &s->selected;
7981 case TOG_VIEW_TREE: {
7982 struct tog_tree_view_state *s = &view->state.tree;
7983 scrolld = &tree_scroll_down;
7985 selected = &s->selected;
7995 if (selected && *selected > view->nlines - header) {
7996 offset = abs(view->nlines - *selected - header);
7997 view->offset = offset;
7998 if (scrolld && offset) {
7999 err = scrolld(view, offset);
8000 *selected -= offset;
8008 list_commands(FILE *fp)
8012 fprintf(fp, "commands:");
8013 for (i = 0; i < nitems(tog_commands); i++) {
8014 const struct tog_cmd *cmd = &tog_commands[i];
8015 fprintf(fp, " %s", cmd->name);
8021 usage(int hflag, int status)
8023 FILE *fp = (status == 0) ? stdout : stderr;
8025 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8028 fprintf(fp, "lazy usage: %s path\n", getprogname());
8035 make_argv(int argc, ...)
8043 argv = calloc(argc, sizeof(char *));
8046 for (i = 0; i < argc; i++) {
8047 argv[i] = strdup(va_arg(ap, char *));
8048 if (argv[i] == NULL)
8057 * Try to convert 'tog path' into a 'tog log path' command.
8058 * The user could simply have mistyped the command rather than knowingly
8059 * provided a path. So check whether argv[0] can in fact be resolved
8060 * to a path in the HEAD commit and print a special error if not.
8061 * This hack is for mpi@ <3
8063 static const struct got_error *
8064 tog_log_with_path(int argc, char *argv[])
8066 const struct got_error *error = NULL, *close_err;
8067 const struct tog_cmd *cmd = NULL;
8068 struct got_repository *repo = NULL;
8069 struct got_worktree *worktree = NULL;
8070 struct got_object_id *commit_id = NULL, *id = NULL;
8071 struct got_commit_object *commit = NULL;
8072 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8073 char *commit_id_str = NULL, **cmd_argv = NULL;
8074 int *pack_fds = NULL;
8076 cwd = getcwd(NULL, 0);
8078 return got_error_from_errno("getcwd");
8080 error = got_repo_pack_fds_open(&pack_fds);
8084 error = got_worktree_open(&worktree, cwd);
8085 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8089 repo_path = strdup(got_worktree_get_repo_path(worktree));
8091 repo_path = strdup(cwd);
8092 if (repo_path == NULL) {
8093 error = got_error_from_errno("strdup");
8097 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8101 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8106 error = tog_load_refs(repo, 0);
8109 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8110 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8111 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8116 got_worktree_close(worktree);
8120 error = got_object_open_as_commit(&commit, repo, commit_id);
8124 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8126 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8128 fprintf(stderr, "%s: '%s' is no known command or path\n",
8129 getprogname(), argv[0]);
8134 close_err = got_repo_close(repo);
8139 error = got_object_id_str(&commit_id_str, commit_id);
8143 cmd = &tog_commands[0]; /* log */
8145 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8146 error = cmd->cmd_main(argc, cmd_argv);
8149 close_err = got_repo_close(repo);
8154 got_object_commit_close(commit);
8156 got_worktree_close(worktree);
8158 const struct got_error *pack_err =
8159 got_repo_pack_fds_close(pack_fds);
8164 free(commit_id_str);
8171 for (i = 0; i < argc; i++)
8180 main(int argc, char *argv[])
8182 const struct got_error *error = NULL;
8183 const struct tog_cmd *cmd = NULL;
8184 int ch, hflag = 0, Vflag = 0;
8185 char **cmd_argv = NULL;
8186 static const struct option longopts[] = {
8187 { "version", no_argument, NULL, 'V' },
8190 char *diff_algo_str = NULL;
8192 setlocale(LC_CTYPE, "");
8194 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8214 got_version_print_str();
8219 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8227 /* Build an argument vector which runs a default command. */
8228 cmd = &tog_commands[0];
8230 cmd_argv = make_argv(argc, cmd->name);
8234 /* Did the user specify a command? */
8235 for (i = 0; i < nitems(tog_commands); i++) {
8236 if (strncmp(tog_commands[i].name, argv[0],
8237 strlen(argv[0])) == 0) {
8238 cmd = &tog_commands[i];
8244 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8245 if (diff_algo_str) {
8246 if (strcasecmp(diff_algo_str, "patience") == 0)
8247 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8248 if (strcasecmp(diff_algo_str, "myers") == 0)
8249 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8255 /* No command specified; try log with a path */
8256 error = tog_log_with_path(argc, argv);
8261 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8268 for (i = 0; i < argc; i++)
8273 if (error && error->code != GOT_ERR_CANCELLED)
8274 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);