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);
1259 err = offset_selection_down(v->child);
1263 offset_selection_up(v);
1264 offset_selection_up(v->child);
1267 err = v->resize(v, 0);
1268 else if (v->child->resize)
1269 err = v->child->resize(v->child, 0);
1275 * Compute view->count from numeric input. Assign total to view->count and
1276 * return first non-numeric key entered.
1279 get_compound_key(struct tog_view *view, int c)
1281 struct tog_view *v = view;
1284 if (view_is_hsplit_top(view))
1286 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1290 cbreak(); /* block for input */
1291 wmove(v->window, v->nlines - 1, 0);
1292 wclrtoeol(v->window);
1293 waddch(v->window, ':');
1296 x = getcurx(v->window);
1297 if (x != ERR && x < view->ncols) {
1298 waddch(v->window, c);
1299 wrefresh(v->window);
1303 * Don't overflow. Max valid request should be the greatest
1304 * between the longest and total lines; cap at 10 million.
1309 n = n * 10 + (c - '0');
1310 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1312 /* Massage excessive or inapplicable values at the input handler. */
1318 static const struct got_error *
1319 view_input(struct tog_view **new, int *done, struct tog_view *view,
1320 struct tog_view_list_head *views)
1322 const struct got_error *err = NULL;
1328 /* Clear "no matches" indicator. */
1329 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1330 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1331 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1335 if (view->searching && !view->search_next_done) {
1336 errcode = pthread_mutex_unlock(&tog_mutex);
1338 return got_error_set_errno(errcode,
1339 "pthread_mutex_unlock");
1341 errcode = pthread_mutex_lock(&tog_mutex);
1343 return got_error_set_errno(errcode,
1344 "pthread_mutex_lock");
1345 view->search_next(view);
1349 nodelay(view->window, FALSE);
1350 /* Allow threads to make progress while we are waiting for input. */
1351 errcode = pthread_mutex_unlock(&tog_mutex);
1353 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1354 /* If we have an unfinished count, let C-g or backspace abort. */
1355 if (view->count && --view->count) {
1357 nodelay(view->window, TRUE);
1358 ch = wgetch(view->window);
1359 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1364 ch = wgetch(view->window);
1365 if (ch >= '1' && ch <= '9')
1366 view->ch = ch = get_compound_key(view, ch);
1368 errcode = pthread_mutex_lock(&tog_mutex);
1370 return got_error_set_errno(errcode, "pthread_mutex_lock");
1371 nodelay(view->window, TRUE);
1373 if (tog_sigwinch_received || tog_sigcont_received) {
1375 tog_sigwinch_received = 0;
1376 tog_sigcont_received = 0;
1377 TAILQ_FOREACH(v, views, entry) {
1378 err = view_resize(v);
1381 err = v->input(new, v, KEY_RESIZE);
1385 err = view_resize(v->child);
1388 err = v->child->input(new, v->child,
1392 if (v->child->resized_x || v->child->resized_y) {
1393 err = view_resize_split(v, 0);
1406 view->child->focussed = 1;
1407 view->focus_child = 1;
1408 } else if (view->parent) {
1410 view->parent->focussed = 1;
1411 view->parent->focus_child = 0;
1412 if (!view_is_splitscreen(view)) {
1413 if (view->parent->resize) {
1414 err = view->parent->resize(view->parent,
1419 offset_selection_up(view->parent);
1420 err = view_fullscreen(view->parent);
1427 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1428 if (view->parent->resize) {
1429 /* might need more commits to fill fullscreen */
1430 err = view->parent->resize(view->parent, 0);
1434 offset_selection_up(view->parent);
1436 err = view->input(new, view, ch);
1444 if (view_is_parent_view(view)) {
1445 if (view->child == NULL)
1447 if (view_is_splitscreen(view->child)) {
1449 view->child->focussed = 1;
1450 err = view_fullscreen(view->child);
1452 err = view_splitscreen(view->child);
1454 err = view_resize_split(view, 0);
1458 err = view->child->input(new, view->child,
1461 if (view_is_splitscreen(view)) {
1462 view->parent->focussed = 0;
1464 err = view_fullscreen(view);
1466 err = view_splitscreen(view);
1467 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1468 err = view_resize(view->parent);
1470 err = view_resize_split(view, 0);
1474 err = view->input(new, view, KEY_RESIZE);
1479 err = view->resize(view, 0);
1484 err = offset_selection_down(view->parent);
1486 err = offset_selection_down(view);
1490 err = switch_split(view);
1493 err = view_resize_split(view, -1);
1496 err = view_resize_split(view, 1);
1502 if (view->search_start)
1503 view_search_start(view);
1505 err = view->input(new, view, ch);
1509 if (view->search_started && view->search_next) {
1510 view->searching = (ch == 'n' ?
1511 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1512 view->search_next_done = 0;
1513 view->search_next(view);
1515 err = view->input(new, view, ch);
1518 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1519 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1521 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1522 TAILQ_FOREACH(v, views, entry) {
1528 if (v->child && v->child->reset) {
1529 err = v->child->reset(v->child);
1536 err = view->input(new, view, ch);
1544 view_needs_focus_indication(struct tog_view *view)
1546 if (view_is_parent_view(view)) {
1547 if (view->child == NULL || view->child->focussed)
1549 if (!view_is_splitscreen(view->child))
1551 } else if (!view_is_splitscreen(view))
1554 return view->focussed;
1557 static const struct got_error *
1558 view_loop(struct tog_view *view)
1560 const struct got_error *err = NULL;
1561 struct tog_view_list_head views;
1562 struct tog_view *new_view;
1564 int fast_refresh = 10;
1565 int done = 0, errcode;
1567 mode = getenv("TOG_VIEW_SPLIT_MODE");
1568 if (!mode || !(*mode == 'h' || *mode == 'H'))
1569 view->mode = TOG_VIEW_SPLIT_VERT;
1571 view->mode = TOG_VIEW_SPLIT_HRZN;
1573 errcode = pthread_mutex_lock(&tog_mutex);
1575 return got_error_set_errno(errcode, "pthread_mutex_lock");
1578 TAILQ_INSERT_HEAD(&views, view, entry);
1581 err = view->show(view);
1586 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1587 !tog_fatal_signal_received()) {
1588 /* Refresh fast during initialization, then become slower. */
1589 if (fast_refresh && fast_refresh-- == 0)
1590 halfdelay(10); /* switch to once per second */
1592 err = view_input(&new_view, &done, view, &views);
1596 struct tog_view *v, *prev = NULL;
1598 if (view_is_parent_view(view))
1599 prev = TAILQ_PREV(view, tog_view_list_head,
1601 else if (view->parent)
1602 prev = view->parent;
1605 view->parent->child = NULL;
1606 view->parent->focus_child = 0;
1607 /* Restore fullscreen line height. */
1608 view->parent->nlines = view->parent->lines;
1609 err = view_resize(view->parent);
1612 /* Make resized splits persist. */
1613 view_transfer_size(view->parent, view);
1615 TAILQ_REMOVE(&views, view, entry);
1617 err = view_close(view);
1622 TAILQ_FOREACH(v, &views, entry) {
1626 if (view == NULL && new_view == NULL) {
1627 /* No view has focus. Try to pick one. */
1630 else if (!TAILQ_EMPTY(&views)) {
1631 view = TAILQ_LAST(&views,
1632 tog_view_list_head);
1635 if (view->focus_child) {
1636 view->child->focussed = 1;
1644 struct tog_view *v, *t;
1645 /* Only allow one parent view per type. */
1646 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1647 if (v->type != new_view->type)
1649 TAILQ_REMOVE(&views, v, entry);
1650 err = view_close(v);
1655 TAILQ_INSERT_TAIL(&views, new_view, entry);
1659 if (view_is_parent_view(view)) {
1660 if (view->child && view->child->focussed)
1663 if (view->parent && view->parent->focussed)
1664 view = view->parent;
1666 show_panel(view->panel);
1667 if (view->child && view_is_splitscreen(view->child))
1668 show_panel(view->child->panel);
1669 if (view->parent && view_is_splitscreen(view)) {
1670 err = view->parent->show(view->parent);
1674 err = view->show(view);
1678 err = view->child->show(view->child);
1687 while (!TAILQ_EMPTY(&views)) {
1688 const struct got_error *close_err;
1689 view = TAILQ_FIRST(&views);
1690 TAILQ_REMOVE(&views, view, entry);
1691 close_err = view_close(view);
1692 if (close_err && err == NULL)
1696 errcode = pthread_mutex_unlock(&tog_mutex);
1697 if (errcode && err == NULL)
1698 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1708 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1713 /* Create newly allocated wide-character string equivalent to a byte string. */
1714 static const struct got_error *
1715 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1718 const struct got_error *err = NULL;
1721 *wlen = mbstowcs(NULL, s, 0);
1722 if (*wlen == (size_t)-1) {
1724 if (errno != EILSEQ)
1725 return got_error_from_errno("mbstowcs");
1727 /* byte string invalid in current encoding; try to "fix" it */
1728 err = got_mbsavis(&vis, &vislen, s);
1731 *wlen = mbstowcs(NULL, vis, 0);
1732 if (*wlen == (size_t)-1) {
1733 err = got_error_from_errno("mbstowcs"); /* give up */
1738 *ws = calloc(*wlen + 1, sizeof(**ws));
1740 err = got_error_from_errno("calloc");
1744 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1745 err = got_error_from_errno("mbstowcs");
1756 static const struct got_error *
1757 expand_tab(char **ptr, const char *src)
1760 size_t len, n, idx = 0, sz = 0;
1763 n = len = strlen(src);
1764 dst = malloc(n + 1);
1766 return got_error_from_errno("malloc");
1768 while (idx < len && src[idx]) {
1769 const char c = src[idx];
1772 size_t nb = TABSIZE - sz % TABSIZE;
1775 p = realloc(dst, n + nb);
1778 return got_error_from_errno("realloc");
1783 memset(dst + sz, ' ', nb);
1786 dst[sz++] = src[idx];
1796 * Advance at most n columns from wline starting at offset off.
1797 * Return the index to the first character after the span operation.
1798 * Return the combined column width of all spanned wide character in
1802 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1804 int width, i, cols = 0;
1811 for (i = off; wline[i] != L'\0'; ++i) {
1812 if (wline[i] == L'\t')
1813 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1815 width = wcwidth(wline[i]);
1822 if (cols + width > n)
1832 * Format a line for display, ensuring that it won't overflow a width limit.
1833 * With scrolling, the width returned refers to the scrolled version of the
1834 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1836 static const struct got_error *
1837 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1838 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1840 const struct got_error *err = NULL;
1842 wchar_t *wline = NULL;
1851 err = expand_tab(&exstr, line);
1856 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1861 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1863 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1864 wline[wlen - 1] = L'\0';
1867 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1868 wline[wlen - 1] = L'\0';
1872 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1878 *scrollxp = scrollx;
1886 static const struct got_error*
1887 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1888 struct got_object_id *id, struct got_repository *repo)
1890 static const struct got_error *err = NULL;
1891 struct got_reflist_entry *re;
1897 TAILQ_FOREACH(re, refs, entry) {
1898 struct got_tag_object *tag = NULL;
1899 struct got_object_id *ref_id;
1902 name = got_ref_get_name(re->ref);
1903 if (strcmp(name, GOT_REF_HEAD) == 0)
1905 if (strncmp(name, "refs/", 5) == 0)
1907 if (strncmp(name, "got/", 4) == 0 &&
1908 strncmp(name, "got/backup/", 11) != 0)
1910 if (strncmp(name, "heads/", 6) == 0)
1912 if (strncmp(name, "remotes/", 8) == 0) {
1914 s = strstr(name, "/" GOT_REF_HEAD);
1915 if (s != NULL && s[strlen(s)] == '\0')
1918 err = got_ref_resolve(&ref_id, repo, re->ref);
1921 if (strncmp(name, "tags/", 5) == 0) {
1922 err = got_object_open_as_tag(&tag, repo, ref_id);
1924 if (err->code != GOT_ERR_OBJ_TYPE) {
1928 /* Ref points at something other than a tag. */
1933 cmp = got_object_id_cmp(tag ?
1934 got_object_tag_get_object_id(tag) : ref_id, id);
1937 got_object_tag_close(tag);
1941 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1942 s ? ", " : "", name) == -1) {
1943 err = got_error_from_errno("asprintf");
1954 static const struct got_error *
1955 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1960 smallerthan = strchr(author, '<');
1961 if (smallerthan && smallerthan[1] != '\0')
1962 author = smallerthan + 1;
1963 author[strcspn(author, "@>")] = '\0';
1964 return format_line(wauthor, author_width, NULL, author, 0, limit,
1968 static const struct got_error *
1969 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1970 struct got_object_id *id, const size_t date_display_cols,
1971 int author_display_cols)
1973 struct tog_log_view_state *s = &view->state.log;
1974 const struct got_error *err = NULL;
1975 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1976 char *logmsg0 = NULL, *logmsg = NULL;
1977 char *author = NULL;
1978 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1979 int author_width, logmsg_width;
1980 char *newline, *line = NULL;
1981 int col, limit, scrollx;
1982 const int avail = view->ncols;
1984 time_t committer_time;
1985 struct tog_color *tc;
1987 committer_time = got_object_commit_get_committer_time(commit);
1988 if (gmtime_r(&committer_time, &tm) == NULL)
1989 return got_error_from_errno("gmtime_r");
1990 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1991 return got_error(GOT_ERR_NO_SPACE);
1993 if (avail <= date_display_cols)
1994 limit = MIN(sizeof(datebuf) - 1, avail);
1996 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1997 tc = get_color(&s->colors, TOG_COLOR_DATE);
1999 wattr_on(view->window,
2000 COLOR_PAIR(tc->colorpair), NULL);
2001 waddnstr(view->window, datebuf, limit);
2003 wattr_off(view->window,
2004 COLOR_PAIR(tc->colorpair), NULL);
2011 err = got_object_id_str(&id_str, id);
2014 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2016 wattr_on(view->window,
2017 COLOR_PAIR(tc->colorpair), NULL);
2018 wprintw(view->window, "%.8s ", id_str);
2020 wattr_off(view->window,
2021 COLOR_PAIR(tc->colorpair), NULL);
2028 if (s->use_committer)
2029 author = strdup(got_object_commit_get_committer(commit));
2031 author = strdup(got_object_commit_get_author(commit));
2032 if (author == NULL) {
2033 err = got_error_from_errno("strdup");
2036 err = format_author(&wauthor, &author_width, author, avail - col, col);
2039 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2041 wattr_on(view->window,
2042 COLOR_PAIR(tc->colorpair), NULL);
2043 waddwstr(view->window, wauthor);
2045 wattr_off(view->window,
2046 COLOR_PAIR(tc->colorpair), NULL);
2047 col += author_width;
2048 while (col < avail && author_width < author_display_cols + 2) {
2049 waddch(view->window, ' ');
2056 err = got_object_commit_get_logmsg(&logmsg0, commit);
2060 while (*logmsg == '\n')
2062 newline = strchr(logmsg, '\n');
2065 limit = avail - col;
2066 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2067 limit--; /* for the border */
2068 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2072 waddwstr(view->window, &wlogmsg[scrollx]);
2073 col += MAX(logmsg_width, 0);
2074 while (col < avail) {
2075 waddch(view->window, ' ');
2087 static struct commit_queue_entry *
2088 alloc_commit_queue_entry(struct got_commit_object *commit,
2089 struct got_object_id *id)
2091 struct commit_queue_entry *entry;
2093 entry = calloc(1, sizeof(*entry));
2098 entry->commit = commit;
2103 pop_commit(struct commit_queue *commits)
2105 struct commit_queue_entry *entry;
2107 entry = TAILQ_FIRST(&commits->head);
2108 TAILQ_REMOVE(&commits->head, entry, entry);
2109 got_object_commit_close(entry->commit);
2110 commits->ncommits--;
2111 /* Don't free entry->id! It is owned by the commit graph. */
2116 free_commits(struct commit_queue *commits)
2118 while (!TAILQ_EMPTY(&commits->head))
2119 pop_commit(commits);
2122 static const struct got_error *
2123 match_commit(int *have_match, struct got_object_id *id,
2124 struct got_commit_object *commit, regex_t *regex)
2126 const struct got_error *err = NULL;
2127 regmatch_t regmatch;
2128 char *id_str = NULL, *logmsg = NULL;
2132 err = got_object_id_str(&id_str, id);
2136 err = got_object_commit_get_logmsg(&logmsg, commit);
2140 if (regexec(regex, got_object_commit_get_author(commit), 1,
2141 ®match, 0) == 0 ||
2142 regexec(regex, got_object_commit_get_committer(commit), 1,
2143 ®match, 0) == 0 ||
2144 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2145 regexec(regex, logmsg, 1, ®match, 0) == 0)
2153 static const struct got_error *
2154 queue_commits(struct tog_log_thread_args *a)
2156 const struct got_error *err = NULL;
2159 * We keep all commits open throughout the lifetime of the log
2160 * view in order to avoid having to re-fetch commits from disk
2161 * while updating the display.
2164 struct got_object_id *id;
2165 struct got_commit_object *commit;
2166 struct commit_queue_entry *entry;
2169 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2171 if (err || id == NULL)
2174 err = got_object_open_as_commit(&commit, a->repo, id);
2177 entry = alloc_commit_queue_entry(commit, id);
2178 if (entry == NULL) {
2179 err = got_error_from_errno("alloc_commit_queue_entry");
2183 errcode = pthread_mutex_lock(&tog_mutex);
2185 err = got_error_set_errno(errcode,
2186 "pthread_mutex_lock");
2190 entry->idx = a->commits->ncommits;
2191 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2192 a->commits->ncommits++;
2194 if (*a->searching == TOG_SEARCH_FORWARD &&
2195 !*a->search_next_done) {
2197 err = match_commit(&have_match, id, commit, a->regex);
2201 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2204 errcode = pthread_mutex_unlock(&tog_mutex);
2205 if (errcode && err == NULL)
2206 err = got_error_set_errno(errcode,
2207 "pthread_mutex_unlock");
2210 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2216 select_commit(struct tog_log_view_state *s)
2218 struct commit_queue_entry *entry;
2221 entry = s->first_displayed_entry;
2223 if (ncommits == s->selected) {
2224 s->selected_entry = entry;
2227 entry = TAILQ_NEXT(entry, entry);
2232 static const struct got_error *
2233 draw_commits(struct tog_view *view)
2235 const struct got_error *err = NULL;
2236 struct tog_log_view_state *s = &view->state.log;
2237 struct commit_queue_entry *entry = s->selected_entry;
2238 const int limit = view->nlines;
2240 int ncommits, author_cols = 4;
2241 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2242 char *refs_str = NULL;
2244 struct tog_color *tc;
2245 static const size_t date_display_cols = 12;
2247 if (s->selected_entry &&
2248 !(view->searching && view->search_next_done == 0)) {
2249 struct got_reflist_head *refs;
2250 err = got_object_id_str(&id_str, s->selected_entry->id);
2253 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2254 s->selected_entry->id);
2256 err = build_refs_str(&refs_str, refs,
2257 s->selected_entry->id, s->repo);
2263 if (s->thread_args.commits_needed == 0)
2264 halfdelay(10); /* disable fast refresh */
2266 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2267 if (asprintf(&ncommits_str, " [%d/%d] %s",
2268 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2269 (view->searching && !view->search_next_done) ?
2270 "searching..." : "loading...") == -1) {
2271 err = got_error_from_errno("asprintf");
2275 const char *search_str = NULL;
2277 if (view->searching) {
2278 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2279 search_str = "no more matches";
2280 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2281 search_str = "no matches found";
2282 else if (!view->search_next_done)
2283 search_str = "searching...";
2286 if (asprintf(&ncommits_str, " [%d/%d] %s",
2287 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2288 search_str ? search_str :
2289 (refs_str ? refs_str : "")) == -1) {
2290 err = got_error_from_errno("asprintf");
2295 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2296 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2297 "........................................",
2298 s->in_repo_path, ncommits_str) == -1) {
2299 err = got_error_from_errno("asprintf");
2303 } else if (asprintf(&header, "commit %s%s",
2304 id_str ? id_str : "........................................",
2305 ncommits_str) == -1) {
2306 err = got_error_from_errno("asprintf");
2310 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2314 werase(view->window);
2316 if (view_needs_focus_indication(view))
2317 wstandout(view->window);
2318 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2320 wattr_on(view->window,
2321 COLOR_PAIR(tc->colorpair), NULL);
2322 waddwstr(view->window, wline);
2324 wattr_off(view->window,
2325 COLOR_PAIR(tc->colorpair), NULL);
2326 while (width < view->ncols) {
2327 waddch(view->window, ' ');
2330 if (view_needs_focus_indication(view))
2331 wstandend(view->window);
2336 /* Grow author column size if necessary, and set view->maxx. */
2337 entry = s->first_displayed_entry;
2341 struct got_commit_object *c = entry->commit;
2342 char *author, *eol, *msg, *msg0;
2343 wchar_t *wauthor, *wmsg;
2345 if (ncommits >= limit - 1)
2347 if (s->use_committer)
2348 author = strdup(got_object_commit_get_committer(c));
2350 author = strdup(got_object_commit_get_author(c));
2351 if (author == NULL) {
2352 err = got_error_from_errno("strdup");
2355 err = format_author(&wauthor, &width, author, COLS,
2357 if (author_cols < width)
2358 author_cols = width;
2363 err = got_object_commit_get_logmsg(&msg0, c);
2367 while (*msg == '\n')
2369 if ((eol = strchr(msg, '\n')))
2371 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2372 date_display_cols + author_cols, 0);
2375 view->maxx = MAX(view->maxx, width);
2379 entry = TAILQ_NEXT(entry, entry);
2382 entry = s->first_displayed_entry;
2383 s->last_displayed_entry = s->first_displayed_entry;
2386 if (ncommits >= limit - 1)
2388 if (ncommits == s->selected)
2389 wstandout(view->window);
2390 err = draw_commit(view, entry->commit, entry->id,
2391 date_display_cols, author_cols);
2392 if (ncommits == s->selected)
2393 wstandend(view->window);
2397 s->last_displayed_entry = entry;
2398 entry = TAILQ_NEXT(entry, entry);
2411 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2413 struct commit_queue_entry *entry;
2416 entry = TAILQ_FIRST(&s->commits.head);
2417 if (s->first_displayed_entry == entry)
2420 entry = s->first_displayed_entry;
2421 while (entry && nscrolled < maxscroll) {
2422 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2424 s->first_displayed_entry = entry;
2430 static const struct got_error *
2431 trigger_log_thread(struct tog_view *view, int wait)
2433 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2436 halfdelay(1); /* fast refresh while loading commits */
2438 while (!ta->log_complete && !tog_thread_error &&
2439 (ta->commits_needed > 0 || ta->load_all)) {
2440 /* Wake the log thread. */
2441 errcode = pthread_cond_signal(&ta->need_commits);
2443 return got_error_set_errno(errcode,
2444 "pthread_cond_signal");
2447 * The mutex will be released while the view loop waits
2448 * in wgetch(), at which time the log thread will run.
2453 /* Display progress update in log view. */
2454 show_log_view(view);
2458 /* Wait right here while next commit is being loaded. */
2459 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2461 return got_error_set_errno(errcode,
2462 "pthread_cond_wait");
2464 /* Display progress update in log view. */
2465 show_log_view(view);
2473 static const struct got_error *
2474 request_log_commits(struct tog_view *view)
2476 struct tog_log_view_state *state = &view->state.log;
2477 const struct got_error *err = NULL;
2479 if (state->thread_args.log_complete)
2482 state->thread_args.commits_needed += view->nscrolled;
2483 err = trigger_log_thread(view, 1);
2484 view->nscrolled = 0;
2489 static const struct got_error *
2490 log_scroll_down(struct tog_view *view, int maxscroll)
2492 struct tog_log_view_state *s = &view->state.log;
2493 const struct got_error *err = NULL;
2494 struct commit_queue_entry *pentry;
2495 int nscrolled = 0, ncommits_needed;
2497 if (s->last_displayed_entry == NULL)
2500 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2501 if (s->commits.ncommits < ncommits_needed &&
2502 !s->thread_args.log_complete) {
2504 * Ask the log thread for required amount of commits.
2506 s->thread_args.commits_needed += maxscroll;
2507 err = trigger_log_thread(view, 1);
2513 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2514 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2517 s->last_displayed_entry = pentry ?
2518 pentry : s->last_displayed_entry;;
2520 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2523 s->first_displayed_entry = pentry;
2524 } while (++nscrolled < maxscroll);
2526 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2527 view->nscrolled += nscrolled;
2529 view->nscrolled = 0;
2534 static const struct got_error *
2535 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2536 struct got_commit_object *commit, struct got_object_id *commit_id,
2537 struct tog_view *log_view, struct got_repository *repo)
2539 const struct got_error *err;
2540 struct got_object_qid *parent_id;
2541 struct tog_view *diff_view;
2543 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2544 if (diff_view == NULL)
2545 return got_error_from_errno("view_open");
2547 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2548 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2549 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2551 *new_view = diff_view;
2555 static const struct got_error *
2556 tree_view_visit_subtree(struct tog_tree_view_state *s,
2557 struct got_tree_object *subtree)
2559 struct tog_parent_tree *parent;
2561 parent = calloc(1, sizeof(*parent));
2563 return got_error_from_errno("calloc");
2565 parent->tree = s->tree;
2566 parent->first_displayed_entry = s->first_displayed_entry;
2567 parent->selected_entry = s->selected_entry;
2568 parent->selected = s->selected;
2569 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2572 s->first_displayed_entry = NULL;
2576 static const struct got_error *
2577 tree_view_walk_path(struct tog_tree_view_state *s,
2578 struct got_commit_object *commit, const char *path)
2580 const struct got_error *err = NULL;
2581 struct got_tree_object *tree = NULL;
2583 char *slash, *subpath = NULL;
2585 /* Walk the path and open corresponding tree objects. */
2588 struct got_tree_entry *te;
2589 struct got_object_id *tree_id;
2595 /* Ensure the correct subtree entry is selected. */
2596 slash = strchr(p, '/');
2598 te_name = strdup(p);
2600 te_name = strndup(p, slash - p);
2601 if (te_name == NULL) {
2602 err = got_error_from_errno("strndup");
2605 te = got_object_tree_find_entry(s->tree, te_name);
2607 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2612 s->first_displayed_entry = s->selected_entry = te;
2614 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2615 break; /* jump to this file's entry */
2617 slash = strchr(p, '/');
2619 subpath = strndup(path, slash - path);
2621 subpath = strdup(path);
2622 if (subpath == NULL) {
2623 err = got_error_from_errno("strdup");
2627 err = got_object_id_by_path(&tree_id, s->repo, commit,
2632 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2637 err = tree_view_visit_subtree(s, tree);
2639 got_object_tree_close(tree);
2653 static const struct got_error *
2654 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2655 struct commit_queue_entry *entry, const char *path,
2656 const char *head_ref_name, struct got_repository *repo)
2658 const struct got_error *err = NULL;
2659 struct tog_tree_view_state *s;
2660 struct tog_view *tree_view;
2662 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2663 if (tree_view == NULL)
2664 return got_error_from_errno("view_open");
2666 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2669 s = &tree_view->state.tree;
2671 *new_view = tree_view;
2673 if (got_path_is_root_dir(path))
2676 return tree_view_walk_path(s, entry->commit, path);
2679 static const struct got_error *
2680 block_signals_used_by_main_thread(void)
2685 if (sigemptyset(&sigset) == -1)
2686 return got_error_from_errno("sigemptyset");
2688 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2689 if (sigaddset(&sigset, SIGWINCH) == -1)
2690 return got_error_from_errno("sigaddset");
2691 if (sigaddset(&sigset, SIGCONT) == -1)
2692 return got_error_from_errno("sigaddset");
2693 if (sigaddset(&sigset, SIGINT) == -1)
2694 return got_error_from_errno("sigaddset");
2695 if (sigaddset(&sigset, SIGTERM) == -1)
2696 return got_error_from_errno("sigaddset");
2698 /* ncurses handles SIGTSTP */
2699 if (sigaddset(&sigset, SIGTSTP) == -1)
2700 return got_error_from_errno("sigaddset");
2702 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2704 return got_error_set_errno(errcode, "pthread_sigmask");
2710 log_thread(void *arg)
2712 const struct got_error *err = NULL;
2714 struct tog_log_thread_args *a = arg;
2718 * Sync startup with main thread such that we begin our
2719 * work once view_input() has released the mutex.
2721 errcode = pthread_mutex_lock(&tog_mutex);
2723 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2727 err = block_signals_used_by_main_thread();
2729 pthread_mutex_unlock(&tog_mutex);
2733 while (!done && !err && !tog_fatal_signal_received()) {
2734 errcode = pthread_mutex_unlock(&tog_mutex);
2736 err = got_error_set_errno(errcode,
2737 "pthread_mutex_unlock");
2740 err = queue_commits(a);
2742 if (err->code != GOT_ERR_ITER_COMPLETED)
2746 } else if (a->commits_needed > 0 && !a->load_all)
2747 a->commits_needed--;
2749 errcode = pthread_mutex_lock(&tog_mutex);
2751 err = got_error_set_errno(errcode,
2752 "pthread_mutex_lock");
2754 } else if (*a->quit)
2756 else if (*a->first_displayed_entry == NULL) {
2757 *a->first_displayed_entry =
2758 TAILQ_FIRST(&a->commits->head);
2759 *a->selected_entry = *a->first_displayed_entry;
2762 errcode = pthread_cond_signal(&a->commit_loaded);
2764 err = got_error_set_errno(errcode,
2765 "pthread_cond_signal");
2766 pthread_mutex_unlock(&tog_mutex);
2771 a->commits_needed = 0;
2773 if (a->commits_needed == 0 && !a->load_all) {
2774 errcode = pthread_cond_wait(&a->need_commits,
2777 err = got_error_set_errno(errcode,
2778 "pthread_cond_wait");
2779 pthread_mutex_unlock(&tog_mutex);
2787 a->log_complete = 1;
2788 errcode = pthread_mutex_unlock(&tog_mutex);
2790 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2793 tog_thread_error = 1;
2794 pthread_cond_signal(&a->commit_loaded);
2799 static const struct got_error *
2800 stop_log_thread(struct tog_log_view_state *s)
2802 const struct got_error *err = NULL, *thread_err = NULL;
2807 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2809 return got_error_set_errno(errcode,
2810 "pthread_cond_signal");
2811 errcode = pthread_mutex_unlock(&tog_mutex);
2813 return got_error_set_errno(errcode,
2814 "pthread_mutex_unlock");
2815 errcode = pthread_join(s->thread, (void **)&thread_err);
2817 return got_error_set_errno(errcode, "pthread_join");
2818 errcode = pthread_mutex_lock(&tog_mutex);
2820 return got_error_set_errno(errcode,
2821 "pthread_mutex_lock");
2822 s->thread = 0; //NULL;
2825 if (s->thread_args.repo) {
2826 err = got_repo_close(s->thread_args.repo);
2827 s->thread_args.repo = NULL;
2830 if (s->thread_args.pack_fds) {
2831 const struct got_error *pack_err =
2832 got_repo_pack_fds_close(s->thread_args.pack_fds);
2835 s->thread_args.pack_fds = NULL;
2838 if (s->thread_args.graph) {
2839 got_commit_graph_close(s->thread_args.graph);
2840 s->thread_args.graph = NULL;
2843 return err ? err : thread_err;
2846 static const struct got_error *
2847 close_log_view(struct tog_view *view)
2849 const struct got_error *err = NULL;
2850 struct tog_log_view_state *s = &view->state.log;
2853 err = stop_log_thread(s);
2855 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2856 if (errcode && err == NULL)
2857 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2859 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2860 if (errcode && err == NULL)
2861 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2863 free_commits(&s->commits);
2864 free(s->in_repo_path);
2865 s->in_repo_path = NULL;
2868 free(s->head_ref_name);
2869 s->head_ref_name = NULL;
2873 static const struct got_error *
2874 search_start_log_view(struct tog_view *view)
2876 struct tog_log_view_state *s = &view->state.log;
2878 s->matched_entry = NULL;
2879 s->search_entry = NULL;
2883 static const struct got_error *
2884 search_next_log_view(struct tog_view *view)
2886 const struct got_error *err = NULL;
2887 struct tog_log_view_state *s = &view->state.log;
2888 struct commit_queue_entry *entry;
2890 /* Display progress update in log view. */
2891 show_log_view(view);
2895 if (s->search_entry) {
2897 errcode = pthread_mutex_unlock(&tog_mutex);
2899 return got_error_set_errno(errcode,
2900 "pthread_mutex_unlock");
2901 ch = wgetch(view->window);
2902 errcode = pthread_mutex_lock(&tog_mutex);
2904 return got_error_set_errno(errcode,
2905 "pthread_mutex_lock");
2906 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2910 if (view->searching == TOG_SEARCH_FORWARD)
2911 entry = TAILQ_NEXT(s->search_entry, entry);
2913 entry = TAILQ_PREV(s->search_entry,
2914 commit_queue_head, entry);
2915 } else if (s->matched_entry) {
2916 int matched_idx = s->matched_entry->idx;
2917 int selected_idx = s->selected_entry->idx;
2920 * If the user has moved the cursor after we hit a match,
2921 * the position from where we should continue searching
2922 * might have changed.
2924 if (view->searching == TOG_SEARCH_FORWARD) {
2925 if (matched_idx > selected_idx)
2926 entry = TAILQ_NEXT(s->selected_entry, entry);
2928 entry = TAILQ_NEXT(s->matched_entry, entry);
2930 if (matched_idx < selected_idx)
2931 entry = TAILQ_PREV(s->selected_entry,
2932 commit_queue_head, entry);
2934 entry = TAILQ_PREV(s->matched_entry,
2935 commit_queue_head, entry);
2938 entry = s->selected_entry;
2944 if (entry == NULL) {
2945 if (s->thread_args.log_complete ||
2946 view->searching == TOG_SEARCH_BACKWARD) {
2947 view->search_next_done =
2948 (s->matched_entry == NULL ?
2949 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2950 s->search_entry = NULL;
2954 * Poke the log thread for more commits and return,
2955 * allowing the main loop to make progress. Search
2956 * will resume at s->search_entry once we come back.
2958 s->thread_args.commits_needed++;
2959 return trigger_log_thread(view, 0);
2962 err = match_commit(&have_match, entry->id, entry->commit,
2967 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2968 s->matched_entry = entry;
2972 s->search_entry = entry;
2973 if (view->searching == TOG_SEARCH_FORWARD)
2974 entry = TAILQ_NEXT(entry, entry);
2976 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2979 if (s->matched_entry) {
2980 int cur = s->selected_entry->idx;
2981 while (cur < s->matched_entry->idx) {
2982 err = input_log_view(NULL, view, KEY_DOWN);
2987 while (cur > s->matched_entry->idx) {
2988 err = input_log_view(NULL, view, KEY_UP);
2995 s->search_entry = NULL;
3000 static const struct got_error *
3001 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3002 struct got_repository *repo, const char *head_ref_name,
3003 const char *in_repo_path, int log_branches)
3005 const struct got_error *err = NULL;
3006 struct tog_log_view_state *s = &view->state.log;
3007 struct got_repository *thread_repo = NULL;
3008 struct got_commit_graph *thread_graph = NULL;
3011 if (in_repo_path != s->in_repo_path) {
3012 free(s->in_repo_path);
3013 s->in_repo_path = strdup(in_repo_path);
3014 if (s->in_repo_path == NULL)
3015 return got_error_from_errno("strdup");
3018 /* The commit queue only contains commits being displayed. */
3019 TAILQ_INIT(&s->commits.head);
3020 s->commits.ncommits = 0;
3023 if (head_ref_name) {
3024 s->head_ref_name = strdup(head_ref_name);
3025 if (s->head_ref_name == NULL) {
3026 err = got_error_from_errno("strdup");
3030 s->start_id = got_object_id_dup(start_id);
3031 if (s->start_id == NULL) {
3032 err = got_error_from_errno("got_object_id_dup");
3035 s->log_branches = log_branches;
3037 STAILQ_INIT(&s->colors);
3038 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3039 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3040 get_color_value("TOG_COLOR_COMMIT"));
3043 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3044 get_color_value("TOG_COLOR_AUTHOR"));
3046 free_colors(&s->colors);
3049 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3050 get_color_value("TOG_COLOR_DATE"));
3052 free_colors(&s->colors);
3057 view->show = show_log_view;
3058 view->input = input_log_view;
3059 view->resize = resize_log_view;
3060 view->close = close_log_view;
3061 view->search_start = search_start_log_view;
3062 view->search_next = search_next_log_view;
3064 if (s->thread_args.pack_fds == NULL) {
3065 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3069 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3070 s->thread_args.pack_fds);
3073 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3077 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3078 s->repo, NULL, NULL);
3082 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3084 err = got_error_set_errno(errcode, "pthread_cond_init");
3087 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3089 err = got_error_set_errno(errcode, "pthread_cond_init");
3093 s->thread_args.commits_needed = view->nlines;
3094 s->thread_args.graph = thread_graph;
3095 s->thread_args.commits = &s->commits;
3096 s->thread_args.in_repo_path = s->in_repo_path;
3097 s->thread_args.start_id = s->start_id;
3098 s->thread_args.repo = thread_repo;
3099 s->thread_args.log_complete = 0;
3100 s->thread_args.quit = &s->quit;
3101 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3102 s->thread_args.selected_entry = &s->selected_entry;
3103 s->thread_args.searching = &view->searching;
3104 s->thread_args.search_next_done = &view->search_next_done;
3105 s->thread_args.regex = &view->regex;
3108 close_log_view(view);
3112 static const struct got_error *
3113 show_log_view(struct tog_view *view)
3115 const struct got_error *err;
3116 struct tog_log_view_state *s = &view->state.log;
3118 if (s->thread == 0) { //NULL) {
3119 int errcode = pthread_create(&s->thread, NULL, log_thread,
3122 return got_error_set_errno(errcode, "pthread_create");
3123 if (s->thread_args.commits_needed > 0) {
3124 err = trigger_log_thread(view, 1);
3130 return draw_commits(view);
3134 log_move_cursor_up(struct tog_view *view, int page, int home)
3136 struct tog_log_view_state *s = &view->state.log;
3138 if (s->selected_entry->idx == 0)
3140 if (s->first_displayed_entry == NULL)
3143 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3145 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3147 if (!page && !home && s->selected > 0)
3150 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3156 static const struct got_error *
3157 log_move_cursor_down(struct tog_view *view, int page)
3159 struct tog_log_view_state *s = &view->state.log;
3160 struct commit_queue_entry *first;
3161 const struct got_error *err = NULL;
3163 first = s->first_displayed_entry;
3164 if (first == NULL) {
3169 if (s->thread_args.log_complete &&
3170 s->selected_entry->idx >= s->commits.ncommits - 1)
3174 int eos = view->nlines - 2;
3176 if (view_is_hsplit_top(view))
3177 --eos; /* border consumes the last line */
3178 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3181 err = log_scroll_down(view, 1);
3182 } else if (s->thread_args.load_all) {
3183 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3184 s->selected += MIN(s->last_displayed_entry->idx -
3185 s->selected_entry->idx, page + 1);
3187 err = log_scroll_down(view, MIN(page,
3188 s->commits.ncommits - s->selected_entry->idx - 1));
3189 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3191 err = log_scroll_down(view, page);
3194 if (first == s->first_displayed_entry && s->selected <
3195 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3196 s->selected = MIN(s->commits.ncommits - 1, page);
3203 * We might necessarily overshoot in horizontal
3204 * splits; if so, select the last displayed commit.
3206 s->selected = MIN(s->selected,
3207 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3211 if (s->thread_args.log_complete &&
3212 s->selected_entry->idx == s->commits.ncommits - 1)
3219 view_get_split(struct tog_view *view, int *y, int *x)
3224 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3225 if (view->child && view->child->resized_y)
3226 *y = view->child->resized_y;
3227 else if (view->resized_y)
3228 *y = view->resized_y;
3230 *y = view_split_begin_y(view->lines);
3231 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3232 if (view->child && view->child->resized_x)
3233 *x = view->child->resized_x;
3234 else if (view->resized_x)
3235 *x = view->resized_x;
3237 *x = view_split_begin_x(view->begin_x);
3241 /* Split view horizontally at y and offset view->state->selected line. */
3242 static const struct got_error *
3243 view_init_hsplit(struct tog_view *view, int y)
3245 const struct got_error *err = NULL;
3249 err = view_resize(view);
3253 err = offset_selection_down(view);
3258 static const struct got_error *
3259 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3261 const struct got_error *err = NULL;
3262 struct tog_log_view_state *s = &view->state.log;
3263 struct commit_queue_entry *entry;
3264 int eos, n, nscroll;
3266 if (s->thread_args.load_all) {
3267 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3268 s->thread_args.load_all = 0;
3269 else if (s->thread_args.log_complete) {
3270 err = log_move_cursor_down(view, s->commits.ncommits);
3271 s->thread_args.load_all = 0;
3276 eos = nscroll = view->nlines - 1;
3277 if (view_is_hsplit_top(view))
3288 view->x = MAX(view->maxx - view->ncols / 2, 0);
3293 if (view->x + view->ncols / 2 < view->maxx)
3294 view->x += 2; /* move two columns right */
3300 view->x -= MIN(view->x, 2); /* move two columns back */
3309 log_move_cursor_up(view, 0, 0);
3313 log_move_cursor_up(view, 0, 1);
3323 log_move_cursor_up(view, nscroll, 0);
3330 err = log_move_cursor_down(view, 0);
3333 s->use_committer = !s->use_committer;
3337 /* We don't know yet how many commits, so we're forced to
3338 * traverse them all. */
3340 if (!s->thread_args.log_complete) {
3341 s->thread_args.load_all = 1;
3342 return trigger_log_thread(view, 0);
3346 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3347 for (n = 0; n < eos; n++) {
3350 s->first_displayed_entry = entry;
3351 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3354 s->selected = n - 1;
3366 err = log_move_cursor_down(view, nscroll);
3369 if (s->selected > view->nlines - 2)
3370 s->selected = view->nlines - 2;
3371 if (s->selected > s->commits.ncommits - 1)
3372 s->selected = s->commits.ncommits - 1;
3374 if (s->commits.ncommits < view->nlines - 1 &&
3375 !s->thread_args.log_complete) {
3376 s->thread_args.commits_needed += (view->nlines - 1) -
3377 s->commits.ncommits;
3378 err = trigger_log_thread(view, 1);
3384 if (s->selected_entry == NULL)
3386 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3390 if (s->selected_entry == NULL)
3392 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3398 if (ch == KEY_BACKSPACE &&
3399 got_path_is_root_dir(s->in_repo_path))
3401 err = stop_log_thread(s);
3404 if (ch == KEY_BACKSPACE) {
3406 err = got_path_dirname(&parent_path, s->in_repo_path);
3409 free(s->in_repo_path);
3410 s->in_repo_path = parent_path;
3411 s->thread_args.in_repo_path = s->in_repo_path;
3412 } else if (ch == CTRL('l')) {
3413 struct got_object_id *start_id;
3414 err = got_repo_match_object_id(&start_id, NULL,
3415 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3416 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3420 s->start_id = start_id;
3421 s->thread_args.start_id = s->start_id;
3423 s->log_branches = !s->log_branches;
3425 if (s->thread_args.pack_fds == NULL) {
3426 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3430 err = got_repo_open(&s->thread_args.repo,
3431 got_repo_get_path(s->repo), NULL,
3432 s->thread_args.pack_fds);
3436 err = tog_load_refs(s->repo, 0);
3439 err = got_commit_graph_open(&s->thread_args.graph,
3440 s->in_repo_path, !s->log_branches);
3443 err = got_commit_graph_iter_start(s->thread_args.graph,
3444 s->start_id, s->repo, NULL, NULL);
3447 free_commits(&s->commits);
3448 s->first_displayed_entry = NULL;
3449 s->last_displayed_entry = NULL;
3450 s->selected_entry = NULL;
3452 s->thread_args.log_complete = 0;
3454 s->thread_args.commits_needed = view->lines;
3455 s->matched_entry = NULL;
3456 s->search_entry = NULL;
3461 err = view_request_new(new_view, view, TOG_VIEW_REF);
3471 static const struct got_error *
3472 apply_unveil(const char *repo_path, const char *worktree_path)
3474 const struct got_error *error;
3477 if (unveil("gmon.out", "rwc") != 0)
3478 return got_error_from_errno2("unveil", "gmon.out");
3480 if (repo_path && unveil(repo_path, "r") != 0)
3481 return got_error_from_errno2("unveil", repo_path);
3483 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3484 return got_error_from_errno2("unveil", worktree_path);
3486 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3487 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3489 error = got_privsep_unveil_exec_helpers();
3493 if (unveil(NULL, NULL) != 0)
3494 return got_error_from_errno("unveil");
3503 * Override default signal handlers before starting ncurses.
3504 * This should prevent ncurses from installing its own
3505 * broken cleanup() signal handler.
3507 signal(SIGWINCH, tog_sigwinch);
3508 signal(SIGPIPE, tog_sigpipe);
3509 signal(SIGCONT, tog_sigcont);
3510 signal(SIGINT, tog_sigint);
3511 signal(SIGTERM, tog_sigterm);
3515 halfdelay(1); /* Do fast refresh while initial view is loading. */
3518 intrflush(stdscr, FALSE);
3519 keypad(stdscr, TRUE);
3521 if (getenv("TOG_COLORS") != NULL) {
3523 use_default_colors();
3527 static const struct got_error *
3528 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3529 struct got_repository *repo, struct got_worktree *worktree)
3531 const struct got_error *err = NULL;
3534 *in_repo_path = strdup("/");
3535 if (*in_repo_path == NULL)
3536 return got_error_from_errno("strdup");
3541 const char *prefix = got_worktree_get_path_prefix(worktree);
3544 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3547 if (asprintf(in_repo_path, "%s%s%s", prefix,
3548 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3550 err = got_error_from_errno("asprintf");
3551 *in_repo_path = NULL;
3555 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3560 static const struct got_error *
3561 cmd_log(int argc, char *argv[])
3563 const struct got_error *error;
3564 struct got_repository *repo = NULL;
3565 struct got_worktree *worktree = NULL;
3566 struct got_object_id *start_id = NULL;
3567 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3568 char *start_commit = NULL, *label = NULL;
3569 struct got_reference *ref = NULL;
3570 const char *head_ref_name = NULL;
3571 int ch, log_branches = 0;
3572 struct tog_view *view;
3573 int *pack_fds = NULL;
3575 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3581 start_commit = optarg;
3584 repo_path = realpath(optarg, NULL);
3585 if (repo_path == NULL)
3586 return got_error_from_errno2("realpath",
3601 error = got_repo_pack_fds_open(&pack_fds);
3605 if (repo_path == NULL) {
3606 cwd = getcwd(NULL, 0);
3608 return got_error_from_errno("getcwd");
3609 error = got_worktree_open(&worktree, cwd);
3610 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3614 strdup(got_worktree_get_repo_path(worktree));
3616 repo_path = strdup(cwd);
3617 if (repo_path == NULL) {
3618 error = got_error_from_errno("strdup");
3623 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3627 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3634 error = apply_unveil(got_repo_get_path(repo),
3635 worktree ? got_worktree_get_root_path(worktree) : NULL);
3639 /* already loaded by tog_log_with_path()? */
3640 if (TAILQ_EMPTY(&tog_refs)) {
3641 error = tog_load_refs(repo, 0);
3646 if (start_commit == NULL) {
3647 error = got_repo_match_object_id(&start_id, &label,
3648 worktree ? got_worktree_get_head_ref_name(worktree) :
3649 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3652 head_ref_name = label;
3654 error = got_ref_open(&ref, repo, start_commit, 0);
3656 head_ref_name = got_ref_get_name(ref);
3657 else if (error->code != GOT_ERR_NOT_REF)
3659 error = got_repo_match_object_id(&start_id, NULL,
3660 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3665 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3667 error = got_error_from_errno("view_open");
3670 error = open_log_view(view, start_id, repo, head_ref_name,
3671 in_repo_path, log_branches);
3675 /* Release work tree lock. */
3676 got_worktree_close(worktree);
3679 error = view_loop(view);
3689 const struct got_error *close_err = got_repo_close(repo);
3694 got_worktree_close(worktree);
3696 const struct got_error *pack_err =
3697 got_repo_pack_fds_close(pack_fds);
3709 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3710 "[-w] object1 object2\n", getprogname());
3715 match_line(const char *line, regex_t *regex, size_t nmatch,
3716 regmatch_t *regmatch)
3718 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3721 static struct tog_color *
3722 match_color(struct tog_colors *colors, const char *line)
3724 struct tog_color *tc = NULL;
3726 STAILQ_FOREACH(tc, colors, entry) {
3727 if (match_line(line, &tc->regex, 0, NULL))
3734 static const struct got_error *
3735 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3736 WINDOW *window, int skipcol, regmatch_t *regmatch)
3738 const struct got_error *err = NULL;
3740 wchar_t *wline = NULL;
3741 int rme, rms, n, width, scrollx;
3742 int width0 = 0, width1 = 0, width2 = 0;
3743 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3747 rms = regmatch->rm_so;
3748 rme = regmatch->rm_eo;
3750 err = expand_tab(&exstr, line);
3754 /* Split the line into 3 segments, according to match offsets. */
3755 seg0 = strndup(exstr, rms);
3757 err = got_error_from_errno("strndup");
3760 seg1 = strndup(exstr + rms, rme - rms);
3762 err = got_error_from_errno("strndup");
3765 seg2 = strdup(exstr + rme);
3767 err = got_error_from_errno("strndup");
3771 /* draw up to matched token if we haven't scrolled past it */
3772 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3776 n = MAX(width0 - skipcol, 0);
3779 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3780 wlimit, col_tab_align, 1);
3783 waddwstr(window, &wline[scrollx]);
3793 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3797 wlen = wcslen(wline);
3799 width = wcwidth(wline[i]);
3801 /* should not happen, tabs are expanded */
3802 err = got_error(GOT_ERR_RANGE);
3805 if (width0 + w + width > skipcol)
3810 /* draw (visible part of) matched token (if scrolled into it) */
3811 if (width1 - w > 0) {
3812 wattron(window, A_STANDOUT);
3813 waddwstr(window, &wline[i]);
3814 wattroff(window, A_STANDOUT);
3815 wlimit -= (width1 - w);
3816 *wtotal += (width1 - w);
3820 if (wlimit > 0) { /* draw rest of line */
3822 if (skipcol > width0 + width1) {
3823 err = format_line(&wline, &width2, &scrollx, seg2,
3824 skipcol - (width0 + width1), wlimit,
3828 waddwstr(window, &wline[scrollx]);
3830 err = format_line(&wline, &width2, NULL, seg2, 0,
3831 wlimit, col_tab_align, 1);
3834 waddwstr(window, wline);
3847 static const struct got_error *
3848 draw_file(struct tog_view *view, const char *header)
3850 struct tog_diff_view_state *s = &view->state.diff;
3851 regmatch_t *regmatch = &view->regmatch;
3852 const struct got_error *err;
3855 size_t linesize = 0;
3857 struct tog_color *tc;
3860 int max_lines = view->nlines;
3861 int nlines = s->nlines;
3864 line_offset = s->line_offsets[s->first_displayed_line - 1];
3865 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3866 return got_error_from_errno("fseek");
3868 werase(view->window);
3871 if (asprintf(&line, "[%d/%d] %s",
3872 s->first_displayed_line - 1 + s->selected_line, nlines,
3874 return got_error_from_errno("asprintf");
3875 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3881 if (view_needs_focus_indication(view))
3882 wstandout(view->window);
3883 waddwstr(view->window, wline);
3886 if (view_needs_focus_indication(view))
3887 wstandend(view->window);
3888 if (width <= view->ncols - 1)
3889 waddch(view->window, '\n');
3899 while (max_lines > 0 && nprinted < max_lines) {
3900 linelen = getline(&line, &linesize, s->f);
3901 if (linelen == -1) {
3907 return got_ferror(s->f, GOT_ERR_IO);
3910 /* Set view->maxx based on full line length. */
3911 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3917 view->maxx = MAX(view->maxx, width);
3921 tc = match_color(&s->colors, line);
3923 wattr_on(view->window,
3924 COLOR_PAIR(tc->colorpair), NULL);
3925 if (s->first_displayed_line + nprinted == s->matched_line &&
3926 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3927 err = add_matched_line(&width, line, view->ncols, 0,
3928 view->window, view->x, regmatch);
3935 err = format_line(&wline, &width, &skip, line,
3936 view->x, view->ncols, 0, view->x ? 1 : 0);
3941 waddwstr(view->window, &wline[skip]);
3946 wattr_off(view->window,
3947 COLOR_PAIR(tc->colorpair), NULL);
3948 if (width <= view->ncols - 1)
3949 waddch(view->window, '\n');
3954 s->last_displayed_line = s->first_displayed_line +
3957 s->last_displayed_line = s->first_displayed_line;
3962 while (nprinted < view->nlines) {
3963 waddch(view->window, '\n');
3967 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3973 wstandout(view->window);
3974 waddwstr(view->window, wline);
3977 wstandend(view->window);
3984 get_datestr(time_t *time, char *datebuf)
3986 struct tm mytm, *tm;
3989 tm = gmtime_r(time, &mytm);
3992 s = asctime_r(tm, datebuf);
3995 p = strchr(s, '\n');
4001 static const struct got_error *
4002 get_changed_paths(struct got_pathlist_head *paths,
4003 struct got_commit_object *commit, struct got_repository *repo)
4005 const struct got_error *err = NULL;
4006 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4007 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4008 struct got_object_qid *qid;
4010 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4012 struct got_commit_object *pcommit;
4013 err = got_object_open_as_commit(&pcommit, repo,
4018 tree_id1 = got_object_id_dup(
4019 got_object_commit_get_tree_id(pcommit));
4020 if (tree_id1 == NULL) {
4021 got_object_commit_close(pcommit);
4022 return got_error_from_errno("got_object_id_dup");
4024 got_object_commit_close(pcommit);
4029 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4034 tree_id2 = got_object_commit_get_tree_id(commit);
4035 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4039 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4040 got_diff_tree_collect_changed_paths, paths, 0);
4043 got_object_tree_close(tree1);
4045 got_object_tree_close(tree2);
4050 static const struct got_error *
4051 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4055 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4057 return got_error_from_errno("reallocarray");
4059 (*line_offsets)[*nlines] = off;
4064 static const struct got_error *
4065 write_commit_info(off_t **line_offsets, size_t *nlines,
4066 struct got_object_id *commit_id, struct got_reflist_head *refs,
4067 struct got_repository *repo, FILE *outfile)
4069 const struct got_error *err = NULL;
4070 char datebuf[26], *datestr;
4071 struct got_commit_object *commit;
4072 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4073 time_t committer_time;
4074 const char *author, *committer;
4075 char *refs_str = NULL;
4076 struct got_pathlist_head changed_paths;
4077 struct got_pathlist_entry *pe;
4081 TAILQ_INIT(&changed_paths);
4084 err = build_refs_str(&refs_str, refs, commit_id, repo);
4089 err = got_object_open_as_commit(&commit, repo, commit_id);
4093 err = got_object_id_str(&id_str, commit_id);
4095 err = got_error_from_errno("got_object_id_str");
4099 err = add_line_offset(line_offsets, nlines, 0);
4103 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4104 refs_str ? refs_str : "", refs_str ? ")" : "");
4106 err = got_error_from_errno("fprintf");
4110 err = add_line_offset(line_offsets, nlines, outoff);
4114 n = fprintf(outfile, "from: %s\n",
4115 got_object_commit_get_author(commit));
4117 err = got_error_from_errno("fprintf");
4121 err = add_line_offset(line_offsets, nlines, outoff);
4125 committer_time = got_object_commit_get_committer_time(commit);
4126 datestr = get_datestr(&committer_time, datebuf);
4128 n = fprintf(outfile, "date: %s UTC\n", datestr);
4130 err = got_error_from_errno("fprintf");
4134 err = add_line_offset(line_offsets, nlines, outoff);
4138 author = got_object_commit_get_author(commit);
4139 committer = got_object_commit_get_committer(commit);
4140 if (strcmp(author, committer) != 0) {
4141 n = fprintf(outfile, "via: %s\n", committer);
4143 err = got_error_from_errno("fprintf");
4147 err = add_line_offset(line_offsets, nlines, outoff);
4151 if (got_object_commit_get_nparents(commit) > 1) {
4152 const struct got_object_id_queue *parent_ids;
4153 struct got_object_qid *qid;
4155 parent_ids = got_object_commit_get_parent_ids(commit);
4156 STAILQ_FOREACH(qid, parent_ids, entry) {
4157 err = got_object_id_str(&id_str, &qid->id);
4160 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4162 err = got_error_from_errno("fprintf");
4166 err = add_line_offset(line_offsets, nlines, outoff);
4174 err = got_object_commit_get_logmsg(&logmsg, commit);
4178 while ((line = strsep(&s, "\n")) != NULL) {
4179 n = fprintf(outfile, "%s\n", line);
4181 err = got_error_from_errno("fprintf");
4185 err = add_line_offset(line_offsets, nlines, outoff);
4190 err = get_changed_paths(&changed_paths, commit, repo);
4193 TAILQ_FOREACH(pe, &changed_paths, entry) {
4194 struct got_diff_changed_path *cp = pe->data;
4195 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4197 err = got_error_from_errno("fprintf");
4201 err = add_line_offset(line_offsets, nlines, outoff);
4204 free((char *)pe->path);
4208 fputc('\n', outfile);
4210 err = add_line_offset(line_offsets, nlines, outoff);
4212 got_pathlist_free(&changed_paths);
4216 got_object_commit_close(commit);
4218 free(*line_offsets);
4219 *line_offsets = NULL;
4225 static const struct got_error *
4226 create_diff(struct tog_diff_view_state *s)
4228 const struct got_error *err = NULL;
4232 free(s->line_offsets);
4233 s->line_offsets = malloc(sizeof(off_t));
4234 if (s->line_offsets == NULL)
4235 return got_error_from_errno("malloc");
4240 err = got_error_from_errno("got_opentemp");
4243 if (s->f && fclose(s->f) == EOF) {
4244 err = got_error_from_errno("fclose");
4250 err = got_object_get_type(&obj_type, s->repo, s->id1);
4252 err = got_object_get_type(&obj_type, s->repo, s->id2);
4257 case GOT_OBJ_TYPE_BLOB:
4258 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4259 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4260 s->label1, s->label2, tog_diff_algo, s->diff_context,
4261 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4263 case GOT_OBJ_TYPE_TREE:
4264 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4265 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4266 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4267 s->force_text_diff, s->repo, s->f);
4269 case GOT_OBJ_TYPE_COMMIT: {
4270 const struct got_object_id_queue *parent_ids;
4271 struct got_object_qid *pid;
4272 struct got_commit_object *commit2;
4273 struct got_reflist_head *refs;
4275 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4278 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4279 /* Show commit info if we're diffing to a parent/root commit. */
4280 if (s->id1 == NULL) {
4281 err = write_commit_info(&s->line_offsets, &s->nlines,
4282 s->id2, refs, s->repo, s->f);
4286 parent_ids = got_object_commit_get_parent_ids(commit2);
4287 STAILQ_FOREACH(pid, parent_ids, entry) {
4288 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4289 err = write_commit_info(
4290 &s->line_offsets, &s->nlines,
4291 s->id2, refs, s->repo, s->f);
4298 got_object_commit_close(commit2);
4300 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4301 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4302 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4303 s->force_text_diff, s->repo, s->f);
4307 err = got_error(GOT_ERR_OBJ_TYPE);
4313 if (s->f && fflush(s->f) != 0 && err == NULL)
4314 err = got_error_from_errno("fflush");
4319 diff_view_indicate_progress(struct tog_view *view)
4321 mvwaddstr(view->window, 0, 0, "diffing...");
4326 static const struct got_error *
4327 search_start_diff_view(struct tog_view *view)
4329 struct tog_diff_view_state *s = &view->state.diff;
4331 s->matched_line = 0;
4335 static const struct got_error *
4336 search_next_diff_view(struct tog_view *view)
4338 struct tog_diff_view_state *s = &view->state.diff;
4339 const struct got_error *err = NULL;
4342 size_t linesize = 0;
4345 if (!view->searching) {
4346 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4350 if (s->matched_line) {
4351 if (view->searching == TOG_SEARCH_FORWARD)
4352 lineno = s->matched_line + 1;
4354 lineno = s->matched_line - 1;
4356 lineno = s->first_displayed_line;
4361 if (lineno <= 0 || lineno > s->nlines) {
4362 if (s->matched_line == 0) {
4363 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4367 if (view->searching == TOG_SEARCH_FORWARD)
4373 offset = s->line_offsets[lineno - 1];
4374 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4376 return got_error_from_errno("fseeko");
4378 linelen = getline(&line, &linesize, s->f);
4379 if (linelen != -1) {
4381 err = expand_tab(&exstr, line);
4384 if (match_line(exstr, &view->regex, 1,
4386 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4387 s->matched_line = lineno;
4393 if (view->searching == TOG_SEARCH_FORWARD)
4400 if (s->matched_line) {
4401 s->first_displayed_line = s->matched_line;
4402 s->selected_line = 1;
4408 static const struct got_error *
4409 close_diff_view(struct tog_view *view)
4411 const struct got_error *err = NULL;
4412 struct tog_diff_view_state *s = &view->state.diff;
4418 if (s->f && fclose(s->f) == EOF)
4419 err = got_error_from_errno("fclose");
4421 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4422 err = got_error_from_errno("fclose");
4424 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4425 err = got_error_from_errno("fclose");
4427 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4428 err = got_error_from_errno("close");
4430 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4431 err = got_error_from_errno("close");
4433 free_colors(&s->colors);
4434 free(s->line_offsets);
4435 s->line_offsets = NULL;
4440 static const struct got_error *
4441 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4442 struct got_object_id *id2, const char *label1, const char *label2,
4443 int diff_context, int ignore_whitespace, int force_text_diff,
4444 struct tog_view *parent_view, struct got_repository *repo)
4446 const struct got_error *err;
4447 struct tog_diff_view_state *s = &view->state.diff;
4449 memset(s, 0, sizeof(*s));
4453 if (id1 != NULL && id2 != NULL) {
4455 err = got_object_get_type(&type1, repo, id1);
4458 err = got_object_get_type(&type2, repo, id2);
4463 return got_error(GOT_ERR_OBJ_TYPE);
4465 s->first_displayed_line = 1;
4466 s->last_displayed_line = view->nlines;
4467 s->selected_line = 1;
4475 s->id1 = got_object_id_dup(id1);
4477 return got_error_from_errno("got_object_id_dup");
4481 s->id2 = got_object_id_dup(id2);
4482 if (s->id2 == NULL) {
4483 err = got_error_from_errno("got_object_id_dup");
4487 s->f1 = got_opentemp();
4488 if (s->f1 == NULL) {
4489 err = got_error_from_errno("got_opentemp");
4493 s->f2 = got_opentemp();
4494 if (s->f2 == NULL) {
4495 err = got_error_from_errno("got_opentemp");
4499 s->fd1 = got_opentempfd();
4501 err = got_error_from_errno("got_opentempfd");
4505 s->fd2 = got_opentempfd();
4507 err = got_error_from_errno("got_opentempfd");
4511 s->first_displayed_line = 1;
4512 s->last_displayed_line = view->nlines;
4513 s->diff_context = diff_context;
4514 s->ignore_whitespace = ignore_whitespace;
4515 s->force_text_diff = force_text_diff;
4516 s->parent_view = parent_view;
4519 STAILQ_INIT(&s->colors);
4520 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4521 err = add_color(&s->colors,
4522 "^-", TOG_COLOR_DIFF_MINUS,
4523 get_color_value("TOG_COLOR_DIFF_MINUS"));
4526 err = add_color(&s->colors, "^\\+",
4527 TOG_COLOR_DIFF_PLUS,
4528 get_color_value("TOG_COLOR_DIFF_PLUS"));
4531 err = add_color(&s->colors,
4532 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4533 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4537 err = add_color(&s->colors,
4538 "^(commit [0-9a-f]|parent [0-9]|"
4539 "(blob|file|tree|commit) [-+] |"
4540 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4541 get_color_value("TOG_COLOR_DIFF_META"));
4545 err = add_color(&s->colors,
4546 "^(from|via): ", TOG_COLOR_AUTHOR,
4547 get_color_value("TOG_COLOR_AUTHOR"));
4551 err = add_color(&s->colors,
4552 "^date: ", TOG_COLOR_DATE,
4553 get_color_value("TOG_COLOR_DATE"));
4558 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4559 view_is_splitscreen(view))
4560 show_log_view(parent_view); /* draw border */
4561 diff_view_indicate_progress(view);
4563 err = create_diff(s);
4565 view->show = show_diff_view;
4566 view->input = input_diff_view;
4567 view->reset = reset_diff_view;
4568 view->close = close_diff_view;
4569 view->search_start = search_start_diff_view;
4570 view->search_next = search_next_diff_view;
4573 close_diff_view(view);
4577 static const struct got_error *
4578 show_diff_view(struct tog_view *view)
4580 const struct got_error *err;
4581 struct tog_diff_view_state *s = &view->state.diff;
4582 char *id_str1 = NULL, *id_str2, *header;
4583 const char *label1, *label2;
4586 err = got_object_id_str(&id_str1, s->id1);
4589 label1 = s->label1 ? : id_str1;
4591 label1 = "/dev/null";
4593 err = got_object_id_str(&id_str2, s->id2);
4596 label2 = s->label2 ? : id_str2;
4598 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4599 err = got_error_from_errno("asprintf");
4607 err = draw_file(view, header);
4612 static const struct got_error *
4613 set_selected_commit(struct tog_diff_view_state *s,
4614 struct commit_queue_entry *entry)
4616 const struct got_error *err;
4617 const struct got_object_id_queue *parent_ids;
4618 struct got_commit_object *selected_commit;
4619 struct got_object_qid *pid;
4622 s->id2 = got_object_id_dup(entry->id);
4624 return got_error_from_errno("got_object_id_dup");
4626 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4629 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4631 pid = STAILQ_FIRST(parent_ids);
4632 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4633 got_object_commit_close(selected_commit);
4637 static const struct got_error *
4638 reset_diff_view(struct tog_view *view)
4640 struct tog_diff_view_state *s = &view->state.diff;
4643 wclear(view->window);
4644 s->first_displayed_line = 1;
4645 s->last_displayed_line = view->nlines;
4646 s->matched_line = 0;
4647 diff_view_indicate_progress(view);
4648 return create_diff(s);
4651 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4653 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4656 static const struct got_error *
4657 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4659 const struct got_error *err = NULL;
4660 struct tog_diff_view_state *s = &view->state.diff;
4661 struct tog_log_view_state *ls;
4662 struct commit_queue_entry *old_selected_entry;
4664 size_t linesize = 0;
4666 int i, nscroll = view->nlines - 1, up = 0;
4673 view->x = MAX(view->maxx - view->ncols / 3, 0);
4678 if (view->x + view->ncols / 3 < view->maxx)
4679 view->x += 2; /* move two columns right */
4685 view->x -= MIN(view->x, 2); /* move two columns back */
4692 s->force_text_diff = !s->force_text_diff;
4694 s->ignore_whitespace = !s->ignore_whitespace;
4695 err = reset_diff_view(view);
4699 s->first_displayed_line = 1;
4708 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4714 if (s->first_displayed_line > 1)
4715 s->first_displayed_line--;
4726 if (s->first_displayed_line == 1) {
4731 while (i++ < nscroll && s->first_displayed_line > 1)
4732 s->first_displayed_line--;
4738 s->first_displayed_line++;
4755 while (!s->eof && i++ < nscroll) {
4756 linelen = getline(&line, &linesize, s->f);
4757 s->first_displayed_line++;
4758 if (linelen == -1) {
4762 err = got_ferror(s->f, GOT_ERR_IO);
4769 if (s->diff_context > 0) {
4771 s->matched_line = 0;
4772 diff_view_indicate_progress(view);
4773 err = create_diff(s);
4774 if (s->first_displayed_line + view->nlines - 1 >
4776 s->first_displayed_line = 1;
4777 s->last_displayed_line = view->nlines;
4783 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4785 s->matched_line = 0;
4786 diff_view_indicate_progress(view);
4787 err = create_diff(s);
4799 if (s->parent_view == NULL) {
4803 s->parent_view->count = view->count;
4805 if (s->parent_view->type == TOG_VIEW_LOG) {
4806 ls = &s->parent_view->state.log;
4807 old_selected_entry = ls->selected_entry;
4809 err = input_log_view(NULL, s->parent_view,
4810 up ? KEY_UP : KEY_DOWN);
4813 view->count = s->parent_view->count;
4815 if (old_selected_entry == ls->selected_entry)
4818 err = set_selected_commit(s, ls->selected_entry);
4821 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4822 struct tog_blame_view_state *bs;
4823 struct got_object_id *id, *prev_id;
4825 bs = &s->parent_view->state.blame;
4826 prev_id = get_annotation_for_line(bs->blame.lines,
4827 bs->blame.nlines, bs->last_diffed_line);
4829 err = input_blame_view(&view, s->parent_view,
4830 up ? KEY_UP : KEY_DOWN);
4833 view->count = s->parent_view->count;
4835 if (prev_id == NULL)
4837 id = get_selected_commit_id(bs->blame.lines,
4838 bs->blame.nlines, bs->first_displayed_line,
4843 if (!got_object_id_cmp(prev_id, id))
4846 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4850 s->first_displayed_line = 1;
4851 s->last_displayed_line = view->nlines;
4852 s->matched_line = 0;
4855 diff_view_indicate_progress(view);
4856 err = create_diff(s);
4866 static const struct got_error *
4867 cmd_diff(int argc, char *argv[])
4869 const struct got_error *error = NULL;
4870 struct got_repository *repo = NULL;
4871 struct got_worktree *worktree = NULL;
4872 struct got_object_id *id1 = NULL, *id2 = NULL;
4873 char *repo_path = NULL, *cwd = NULL;
4874 char *id_str1 = NULL, *id_str2 = NULL;
4875 char *label1 = NULL, *label2 = NULL;
4876 int diff_context = 3, ignore_whitespace = 0;
4877 int ch, force_text_diff = 0;
4879 struct tog_view *view;
4880 int *pack_fds = NULL;
4882 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4885 force_text_diff = 1;
4888 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4891 errx(1, "number of context lines is %s: %s",
4895 repo_path = realpath(optarg, NULL);
4896 if (repo_path == NULL)
4897 return got_error_from_errno2("realpath",
4899 got_path_strip_trailing_slashes(repo_path);
4902 ignore_whitespace = 1;
4914 usage_diff(); /* TODO show local worktree changes */
4915 } else if (argc == 2) {
4921 error = got_repo_pack_fds_open(&pack_fds);
4925 if (repo_path == NULL) {
4926 cwd = getcwd(NULL, 0);
4928 return got_error_from_errno("getcwd");
4929 error = got_worktree_open(&worktree, cwd);
4930 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4934 strdup(got_worktree_get_repo_path(worktree));
4936 repo_path = strdup(cwd);
4937 if (repo_path == NULL) {
4938 error = got_error_from_errno("strdup");
4943 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4949 error = apply_unveil(got_repo_get_path(repo), NULL);
4953 error = tog_load_refs(repo, 0);
4957 error = got_repo_match_object_id(&id1, &label1, id_str1,
4958 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4962 error = got_repo_match_object_id(&id2, &label2, id_str2,
4963 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4967 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4969 error = got_error_from_errno("view_open");
4972 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4973 ignore_whitespace, force_text_diff, NULL, repo);
4976 error = view_loop(view);
4983 const struct got_error *close_err = got_repo_close(repo);
4988 got_worktree_close(worktree);
4990 const struct got_error *pack_err =
4991 got_repo_pack_fds_close(pack_fds);
5004 "usage: %s blame [-c commit] [-r repository-path] path\n",
5009 struct tog_blame_line {
5011 struct got_object_id *id;
5014 static const struct got_error *
5015 draw_blame(struct tog_view *view)
5017 struct tog_blame_view_state *s = &view->state.blame;
5018 struct tog_blame *blame = &s->blame;
5019 regmatch_t *regmatch = &view->regmatch;
5020 const struct got_error *err;
5021 int lineno = 0, nprinted = 0;
5023 size_t linesize = 0;
5027 struct tog_blame_line *blame_line;
5028 struct got_object_id *prev_id = NULL;
5030 struct tog_color *tc;
5032 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5037 werase(view->window);
5039 if (asprintf(&line, "commit %s", id_str) == -1) {
5040 err = got_error_from_errno("asprintf");
5045 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5050 if (view_needs_focus_indication(view))
5051 wstandout(view->window);
5052 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5054 wattr_on(view->window,
5055 COLOR_PAIR(tc->colorpair), NULL);
5056 waddwstr(view->window, wline);
5058 wattr_off(view->window,
5059 COLOR_PAIR(tc->colorpair), NULL);
5060 if (view_needs_focus_indication(view))
5061 wstandend(view->window);
5064 if (width < view->ncols - 1)
5065 waddch(view->window, '\n');
5067 if (asprintf(&line, "[%d/%d] %s%s",
5068 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5069 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5071 return got_error_from_errno("asprintf");
5074 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5079 waddwstr(view->window, wline);
5082 if (width < view->ncols - 1)
5083 waddch(view->window, '\n');
5087 while (nprinted < view->nlines - 2) {
5088 linelen = getline(&line, &linesize, blame->f);
5089 if (linelen == -1) {
5090 if (feof(blame->f)) {
5095 return got_ferror(blame->f, GOT_ERR_IO);
5097 if (++lineno < s->first_displayed_line)
5100 /* Set view->maxx based on full line length. */
5101 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5108 view->maxx = MAX(view->maxx, width);
5110 if (nprinted == s->selected_line - 1)
5111 wstandout(view->window);
5113 if (blame->nlines > 0) {
5114 blame_line = &blame->lines[lineno - 1];
5115 if (blame_line->annotated && prev_id &&
5116 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5117 !(nprinted == s->selected_line - 1)) {
5118 waddstr(view->window, " ");
5119 } else if (blame_line->annotated) {
5121 err = got_object_id_str(&id_str,
5127 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5129 wattr_on(view->window,
5130 COLOR_PAIR(tc->colorpair), NULL);
5131 wprintw(view->window, "%.8s", id_str);
5133 wattr_off(view->window,
5134 COLOR_PAIR(tc->colorpair), NULL);
5136 prev_id = blame_line->id;
5138 waddstr(view->window, "........");
5142 waddstr(view->window, "........");
5146 if (nprinted == s->selected_line - 1)
5147 wstandend(view->window);
5148 waddstr(view->window, " ");
5150 if (view->ncols <= 9) {
5152 } else if (s->first_displayed_line + nprinted ==
5154 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5155 err = add_matched_line(&width, line, view->ncols - 9, 9,
5156 view->window, view->x, regmatch);
5164 err = format_line(&wline, &width, &skip, line,
5165 view->x, view->ncols - 9, 9, 1);
5170 waddwstr(view->window, &wline[skip]);
5176 if (width <= view->ncols - 1)
5177 waddch(view->window, '\n');
5178 if (++nprinted == 1)
5179 s->first_displayed_line = lineno;
5182 s->last_displayed_line = lineno;
5189 static const struct got_error *
5190 blame_cb(void *arg, int nlines, int lineno,
5191 struct got_commit_object *commit, struct got_object_id *id)
5193 const struct got_error *err = NULL;
5194 struct tog_blame_cb_args *a = arg;
5195 struct tog_blame_line *line;
5198 if (nlines != a->nlines ||
5199 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5200 return got_error(GOT_ERR_RANGE);
5202 errcode = pthread_mutex_lock(&tog_mutex);
5204 return got_error_set_errno(errcode, "pthread_mutex_lock");
5206 if (*a->quit) { /* user has quit the blame view */
5207 err = got_error(GOT_ERR_ITER_COMPLETED);
5212 goto done; /* no change in this commit */
5214 line = &a->lines[lineno - 1];
5215 if (line->annotated)
5218 line->id = got_object_id_dup(id);
5219 if (line->id == NULL) {
5220 err = got_error_from_errno("got_object_id_dup");
5223 line->annotated = 1;
5225 errcode = pthread_mutex_unlock(&tog_mutex);
5227 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5232 blame_thread(void *arg)
5234 const struct got_error *err, *close_err;
5235 struct tog_blame_thread_args *ta = arg;
5236 struct tog_blame_cb_args *a = ta->cb_args;
5237 int errcode, fd1 = -1, fd2 = -1;
5238 FILE *f1 = NULL, *f2 = NULL;
5240 fd1 = got_opentempfd();
5242 return (void *)got_error_from_errno("got_opentempfd");
5244 fd2 = got_opentempfd();
5246 err = got_error_from_errno("got_opentempfd");
5250 f1 = got_opentemp();
5252 err = (void *)got_error_from_errno("got_opentemp");
5255 f2 = got_opentemp();
5257 err = (void *)got_error_from_errno("got_opentemp");
5261 err = block_signals_used_by_main_thread();
5265 err = got_blame(ta->path, a->commit_id, ta->repo,
5266 tog_diff_algo, blame_cb, ta->cb_args,
5267 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5268 if (err && err->code == GOT_ERR_CANCELLED)
5271 errcode = pthread_mutex_lock(&tog_mutex);
5273 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5277 close_err = got_repo_close(ta->repo);
5283 errcode = pthread_mutex_unlock(&tog_mutex);
5284 if (errcode && err == NULL)
5285 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5288 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5289 err = got_error_from_errno("close");
5290 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5291 err = got_error_from_errno("close");
5292 if (f1 && fclose(f1) == EOF && err == NULL)
5293 err = got_error_from_errno("fclose");
5294 if (f2 && fclose(f2) == EOF && err == NULL)
5295 err = got_error_from_errno("fclose");
5300 static struct got_object_id *
5301 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5302 int first_displayed_line, int selected_line)
5304 struct tog_blame_line *line;
5309 line = &lines[first_displayed_line - 1 + selected_line - 1];
5310 if (!line->annotated)
5316 static struct got_object_id *
5317 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5320 struct tog_blame_line *line;
5322 if (nlines <= 0 || lineno >= nlines)
5325 line = &lines[lineno - 1];
5326 if (!line->annotated)
5332 static const struct got_error *
5333 stop_blame(struct tog_blame *blame)
5335 const struct got_error *err = NULL;
5338 if (blame->thread) {
5340 errcode = pthread_mutex_unlock(&tog_mutex);
5342 return got_error_set_errno(errcode,
5343 "pthread_mutex_unlock");
5344 errcode = pthread_join(blame->thread, (void **)&err);
5346 return got_error_set_errno(errcode, "pthread_join");
5347 errcode = pthread_mutex_lock(&tog_mutex);
5349 return got_error_set_errno(errcode,
5350 "pthread_mutex_lock");
5351 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5353 blame->thread = 0; //NULL;
5355 if (blame->thread_args.repo) {
5356 const struct got_error *close_err;
5357 close_err = got_repo_close(blame->thread_args.repo);
5360 blame->thread_args.repo = NULL;
5363 if (fclose(blame->f) == EOF && err == NULL)
5364 err = got_error_from_errno("fclose");
5368 for (i = 0; i < blame->nlines; i++)
5369 free(blame->lines[i].id);
5371 blame->lines = NULL;
5373 free(blame->cb_args.commit_id);
5374 blame->cb_args.commit_id = NULL;
5375 if (blame->pack_fds) {
5376 const struct got_error *pack_err =
5377 got_repo_pack_fds_close(blame->pack_fds);
5380 blame->pack_fds = NULL;
5385 static const struct got_error *
5386 cancel_blame_view(void *arg)
5388 const struct got_error *err = NULL;
5392 errcode = pthread_mutex_lock(&tog_mutex);
5394 return got_error_set_errno(errcode,
5395 "pthread_mutex_unlock");
5398 err = got_error(GOT_ERR_CANCELLED);
5400 errcode = pthread_mutex_unlock(&tog_mutex);
5402 return got_error_set_errno(errcode,
5403 "pthread_mutex_lock");
5408 static const struct got_error *
5409 run_blame(struct tog_view *view)
5411 struct tog_blame_view_state *s = &view->state.blame;
5412 struct tog_blame *blame = &s->blame;
5413 const struct got_error *err = NULL;
5414 struct got_commit_object *commit = NULL;
5415 struct got_blob_object *blob = NULL;
5416 struct got_repository *thread_repo = NULL;
5417 struct got_object_id *obj_id = NULL;
5418 int obj_type, fd = -1;
5419 int *pack_fds = NULL;
5421 err = got_object_open_as_commit(&commit, s->repo,
5422 &s->blamed_commit->id);
5426 fd = got_opentempfd();
5428 err = got_error_from_errno("got_opentempfd");
5432 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5436 err = got_object_get_type(&obj_type, s->repo, obj_id);
5440 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5441 err = got_error(GOT_ERR_OBJ_TYPE);
5445 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5448 blame->f = got_opentemp();
5449 if (blame->f == NULL) {
5450 err = got_error_from_errno("got_opentemp");
5453 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5454 &blame->line_offsets, blame->f, blob);
5457 if (blame->nlines == 0) {
5458 s->blame_complete = 1;
5462 /* Don't include \n at EOF in the blame line count. */
5463 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5466 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5467 if (blame->lines == NULL) {
5468 err = got_error_from_errno("calloc");
5472 err = got_repo_pack_fds_open(&pack_fds);
5475 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5480 blame->pack_fds = pack_fds;
5481 blame->cb_args.view = view;
5482 blame->cb_args.lines = blame->lines;
5483 blame->cb_args.nlines = blame->nlines;
5484 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5485 if (blame->cb_args.commit_id == NULL) {
5486 err = got_error_from_errno("got_object_id_dup");
5489 blame->cb_args.quit = &s->done;
5491 blame->thread_args.path = s->path;
5492 blame->thread_args.repo = thread_repo;
5493 blame->thread_args.cb_args = &blame->cb_args;
5494 blame->thread_args.complete = &s->blame_complete;
5495 blame->thread_args.cancel_cb = cancel_blame_view;
5496 blame->thread_args.cancel_arg = &s->done;
5497 s->blame_complete = 0;
5499 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5500 s->first_displayed_line = 1;
5501 s->last_displayed_line = view->nlines;
5502 s->selected_line = 1;
5504 s->matched_line = 0;
5508 got_object_commit_close(commit);
5509 if (fd != -1 && close(fd) == -1 && err == NULL)
5510 err = got_error_from_errno("close");
5512 got_object_blob_close(blob);
5519 static const struct got_error *
5520 open_blame_view(struct tog_view *view, char *path,
5521 struct got_object_id *commit_id, struct got_repository *repo)
5523 const struct got_error *err = NULL;
5524 struct tog_blame_view_state *s = &view->state.blame;
5526 STAILQ_INIT(&s->blamed_commits);
5528 s->path = strdup(path);
5529 if (s->path == NULL)
5530 return got_error_from_errno("strdup");
5532 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5538 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5539 s->first_displayed_line = 1;
5540 s->last_displayed_line = view->nlines;
5541 s->selected_line = 1;
5542 s->blame_complete = 0;
5544 s->commit_id = commit_id;
5545 memset(&s->blame, 0, sizeof(s->blame));
5547 STAILQ_INIT(&s->colors);
5548 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5549 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5550 get_color_value("TOG_COLOR_COMMIT"));
5555 view->show = show_blame_view;
5556 view->input = input_blame_view;
5557 view->reset = reset_blame_view;
5558 view->close = close_blame_view;
5559 view->search_start = search_start_blame_view;
5560 view->search_next = search_next_blame_view;
5562 return run_blame(view);
5565 static const struct got_error *
5566 close_blame_view(struct tog_view *view)
5568 const struct got_error *err = NULL;
5569 struct tog_blame_view_state *s = &view->state.blame;
5571 if (s->blame.thread)
5572 err = stop_blame(&s->blame);
5574 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5575 struct got_object_qid *blamed_commit;
5576 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5577 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5578 got_object_qid_free(blamed_commit);
5582 free_colors(&s->colors);
5586 static const struct got_error *
5587 search_start_blame_view(struct tog_view *view)
5589 struct tog_blame_view_state *s = &view->state.blame;
5591 s->matched_line = 0;
5595 static const struct got_error *
5596 search_next_blame_view(struct tog_view *view)
5598 struct tog_blame_view_state *s = &view->state.blame;
5599 const struct got_error *err = NULL;
5602 size_t linesize = 0;
5605 if (!view->searching) {
5606 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5610 if (s->matched_line) {
5611 if (view->searching == TOG_SEARCH_FORWARD)
5612 lineno = s->matched_line + 1;
5614 lineno = s->matched_line - 1;
5616 lineno = s->first_displayed_line - 1 + s->selected_line;
5621 if (lineno <= 0 || lineno > s->blame.nlines) {
5622 if (s->matched_line == 0) {
5623 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5627 if (view->searching == TOG_SEARCH_FORWARD)
5630 lineno = s->blame.nlines;
5633 offset = s->blame.line_offsets[lineno - 1];
5634 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5636 return got_error_from_errno("fseeko");
5638 linelen = getline(&line, &linesize, s->blame.f);
5639 if (linelen != -1) {
5641 err = expand_tab(&exstr, line);
5644 if (match_line(exstr, &view->regex, 1,
5646 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5647 s->matched_line = lineno;
5653 if (view->searching == TOG_SEARCH_FORWARD)
5660 if (s->matched_line) {
5661 s->first_displayed_line = s->matched_line;
5662 s->selected_line = 1;
5668 static const struct got_error *
5669 show_blame_view(struct tog_view *view)
5671 const struct got_error *err = NULL;
5672 struct tog_blame_view_state *s = &view->state.blame;
5675 if (s->blame.thread == 0 && !s->blame_complete) {
5676 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5677 &s->blame.thread_args);
5679 return got_error_set_errno(errcode, "pthread_create");
5681 halfdelay(1); /* fast refresh while annotating */
5684 if (s->blame_complete)
5685 halfdelay(10); /* disable fast refresh */
5687 err = draw_blame(view);
5693 static const struct got_error *
5694 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5695 struct got_repository *repo, struct got_object_id *id)
5697 struct tog_view *log_view;
5698 const struct got_error *err = NULL;
5702 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5703 if (log_view == NULL)
5704 return got_error_from_errno("view_open");
5706 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5708 view_close(log_view);
5710 *new_view = log_view;
5715 static const struct got_error *
5716 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5718 const struct got_error *err = NULL, *thread_err = NULL;
5719 struct tog_view *diff_view;
5720 struct tog_blame_view_state *s = &view->state.blame;
5721 int eos, nscroll, begin_y = 0, begin_x = 0;
5723 eos = nscroll = view->nlines - 2;
5724 if (view_is_hsplit_top(view))
5732 view->x = MAX(view->maxx - view->ncols / 3, 0);
5737 if (view->x + view->ncols / 3 < view->maxx)
5738 view->x += 2; /* move two columns right */
5744 view->x -= MIN(view->x, 2); /* move two columns back */
5753 s->selected_line = 1;
5754 s->first_displayed_line = 1;
5759 if (s->blame.nlines < eos) {
5760 s->selected_line = s->blame.nlines;
5761 s->first_displayed_line = 1;
5763 s->selected_line = eos;
5764 s->first_displayed_line = s->blame.nlines - (eos - 1);
5771 if (s->selected_line > 1)
5773 else if (s->selected_line == 1 &&
5774 s->first_displayed_line > 1)
5775 s->first_displayed_line--;
5786 if (s->first_displayed_line == 1) {
5787 if (view->count > 1)
5789 s->selected_line = MAX(1, s->selected_line - nscroll);
5793 if (s->first_displayed_line > nscroll)
5794 s->first_displayed_line -= nscroll;
5796 s->first_displayed_line = 1;
5801 if (s->selected_line < eos && s->first_displayed_line +
5802 s->selected_line <= s->blame.nlines)
5804 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5805 s->first_displayed_line++;
5811 struct got_object_id *id = NULL;
5814 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5815 s->first_displayed_line, s->selected_line);
5819 struct got_commit_object *commit, *pcommit;
5820 struct got_object_qid *pid;
5821 struct got_object_id *blob_id = NULL;
5823 err = got_object_open_as_commit(&commit,
5828 got_object_commit_get_parent_ids(commit));
5830 got_object_commit_close(commit);
5833 /* Check if path history ends here. */
5834 err = got_object_open_as_commit(&pcommit,
5838 err = got_object_id_by_path(&blob_id, s->repo,
5840 got_object_commit_close(pcommit);
5842 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5844 got_object_commit_close(commit);
5847 err = got_object_get_type(&obj_type, s->repo,
5850 /* Can't blame non-blob type objects. */
5851 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5852 got_object_commit_close(commit);
5855 err = got_object_qid_alloc(&s->blamed_commit,
5857 got_object_commit_close(commit);
5859 if (got_object_id_cmp(id,
5860 &s->blamed_commit->id) == 0)
5862 err = got_object_qid_alloc(&s->blamed_commit,
5868 thread_err = stop_blame(&s->blame);
5872 STAILQ_INSERT_HEAD(&s->blamed_commits,
5873 s->blamed_commit, entry);
5874 err = run_blame(view);
5880 struct got_object_qid *first;
5883 first = STAILQ_FIRST(&s->blamed_commits);
5884 if (!got_object_id_cmp(&first->id, s->commit_id))
5887 thread_err = stop_blame(&s->blame);
5891 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5892 got_object_qid_free(s->blamed_commit);
5894 STAILQ_FIRST(&s->blamed_commits);
5895 err = run_blame(view);
5902 s->id_to_log = get_selected_commit_id(s->blame.lines,
5903 s->blame.nlines, s->first_displayed_line, s->selected_line);
5905 err = view_request_new(new_view, view, TOG_VIEW_LOG);
5909 struct got_object_id *id = NULL;
5910 struct got_object_qid *pid;
5911 struct got_commit_object *commit = NULL;
5914 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5915 s->first_displayed_line, s->selected_line);
5918 err = got_object_open_as_commit(&commit, s->repo, id);
5921 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5923 /* traversed from diff view, release diff resources */
5924 err = close_diff_view(*new_view);
5927 diff_view = *new_view;
5929 if (view_is_parent_view(view))
5930 view_get_split(view, &begin_y, &begin_x);
5932 diff_view = view_open(0, 0, begin_y, begin_x,
5934 if (diff_view == NULL) {
5935 got_object_commit_close(commit);
5936 err = got_error_from_errno("view_open");
5940 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5941 id, NULL, NULL, 3, 0, 0, view, s->repo);
5942 got_object_commit_close(commit);
5944 view_close(diff_view);
5947 s->last_diffed_line = s->first_displayed_line - 1 +
5950 break; /* still open from active diff view */
5951 if (view_is_parent_view(view) &&
5952 view->mode == TOG_VIEW_SPLIT_HRZN) {
5953 err = view_init_hsplit(view, begin_y);
5959 diff_view->focussed = 1;
5960 diff_view->mode = view->mode;
5961 diff_view->nlines = view->lines - begin_y;
5962 if (view_is_parent_view(view)) {
5963 view_transfer_size(diff_view, view);
5964 err = view_close_child(view);
5967 err = view_set_child(view, diff_view);
5970 view->focus_child = 1;
5972 *new_view = diff_view;
5985 if (s->last_displayed_line >= s->blame.nlines &&
5986 s->selected_line >= MIN(s->blame.nlines,
5987 view->nlines - 2)) {
5991 if (s->last_displayed_line >= s->blame.nlines &&
5992 s->selected_line < view->nlines - 2) {
5994 MIN(nscroll, s->last_displayed_line -
5995 s->first_displayed_line - s->selected_line + 1);
5997 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5998 s->first_displayed_line += nscroll;
6000 s->first_displayed_line =
6001 s->blame.nlines - (view->nlines - 3);
6004 if (s->selected_line > view->nlines - 2) {
6005 s->selected_line = MIN(s->blame.nlines,
6013 return thread_err ? thread_err : err;
6016 static const struct got_error *
6017 reset_blame_view(struct tog_view *view)
6019 const struct got_error *err;
6020 struct tog_blame_view_state *s = &view->state.blame;
6024 err = stop_blame(&s->blame);
6028 return run_blame(view);
6031 static const struct got_error *
6032 cmd_blame(int argc, char *argv[])
6034 const struct got_error *error;
6035 struct got_repository *repo = NULL;
6036 struct got_worktree *worktree = NULL;
6037 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6038 char *link_target = NULL;
6039 struct got_object_id *commit_id = NULL;
6040 struct got_commit_object *commit = NULL;
6041 char *commit_id_str = NULL;
6043 struct tog_view *view;
6044 int *pack_fds = NULL;
6046 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6049 commit_id_str = optarg;
6052 repo_path = realpath(optarg, NULL);
6053 if (repo_path == NULL)
6054 return got_error_from_errno2("realpath",
6069 error = got_repo_pack_fds_open(&pack_fds);
6073 if (repo_path == NULL) {
6074 cwd = getcwd(NULL, 0);
6076 return got_error_from_errno("getcwd");
6077 error = got_worktree_open(&worktree, cwd);
6078 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6082 strdup(got_worktree_get_repo_path(worktree));
6084 repo_path = strdup(cwd);
6085 if (repo_path == NULL) {
6086 error = got_error_from_errno("strdup");
6091 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6095 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6102 error = apply_unveil(got_repo_get_path(repo), NULL);
6106 error = tog_load_refs(repo, 0);
6110 if (commit_id_str == NULL) {
6111 struct got_reference *head_ref;
6112 error = got_ref_open(&head_ref, repo, worktree ?
6113 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6116 error = got_ref_resolve(&commit_id, repo, head_ref);
6117 got_ref_close(head_ref);
6119 error = got_repo_match_object_id(&commit_id, NULL,
6120 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6125 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6127 error = got_error_from_errno("view_open");
6131 error = got_object_open_as_commit(&commit, repo, commit_id);
6135 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6140 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6145 /* Release work tree lock. */
6146 got_worktree_close(worktree);
6149 error = view_loop(view);
6157 got_object_commit_close(commit);
6159 got_worktree_close(worktree);
6161 const struct got_error *close_err = got_repo_close(repo);
6166 const struct got_error *pack_err =
6167 got_repo_pack_fds_close(pack_fds);
6175 static const struct got_error *
6176 draw_tree_entries(struct tog_view *view, const char *parent_path)
6178 struct tog_tree_view_state *s = &view->state.tree;
6179 const struct got_error *err = NULL;
6180 struct got_tree_entry *te;
6182 struct tog_color *tc;
6183 int width, n, i, nentries;
6184 int limit = view->nlines;
6187 if (view_is_hsplit_top(view))
6188 --limit; /* border */
6190 werase(view->window);
6195 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6199 if (view_needs_focus_indication(view))
6200 wstandout(view->window);
6201 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6203 wattr_on(view->window,
6204 COLOR_PAIR(tc->colorpair), NULL);
6205 waddwstr(view->window, wline);
6207 wattr_off(view->window,
6208 COLOR_PAIR(tc->colorpair), NULL);
6209 if (view_needs_focus_indication(view))
6210 wstandend(view->window);
6213 if (width < view->ncols - 1)
6214 waddch(view->window, '\n');
6217 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6221 waddwstr(view->window, wline);
6224 if (width < view->ncols - 1)
6225 waddch(view->window, '\n');
6228 waddch(view->window, '\n');
6232 if (s->first_displayed_entry == NULL) {
6233 te = got_object_tree_get_first_entry(s->tree);
6234 if (s->selected == 0) {
6236 wstandout(view->window);
6237 s->selected_entry = NULL;
6239 waddstr(view->window, " ..\n"); /* parent directory */
6240 if (s->selected == 0 && view->focussed)
6241 wstandend(view->window);
6248 te = s->first_displayed_entry;
6251 nentries = got_object_tree_get_nentries(s->tree);
6252 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6253 char *line = NULL, *id_str = NULL, *link_target = NULL;
6254 const char *modestr = "";
6257 te = got_object_tree_get_entry(s->tree, i);
6258 mode = got_tree_entry_get_mode(te);
6261 err = got_object_id_str(&id_str,
6262 got_tree_entry_get_id(te));
6264 return got_error_from_errno(
6265 "got_object_id_str");
6267 if (got_object_tree_entry_is_submodule(te))
6269 else if (S_ISLNK(mode)) {
6272 err = got_tree_entry_get_symlink_target(&link_target,
6278 for (i = 0; i < strlen(link_target); i++) {
6279 if (!isprint((unsigned char)link_target[i]))
6280 link_target[i] = '?';
6284 else if (S_ISDIR(mode))
6286 else if (mode & S_IXUSR)
6288 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6289 got_tree_entry_get_name(te), modestr,
6290 link_target ? " -> ": "",
6291 link_target ? link_target : "") == -1) {
6294 return got_error_from_errno("asprintf");
6298 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6304 if (n == s->selected) {
6306 wstandout(view->window);
6307 s->selected_entry = te;
6309 tc = match_color(&s->colors, line);
6311 wattr_on(view->window,
6312 COLOR_PAIR(tc->colorpair), NULL);
6313 waddwstr(view->window, wline);
6315 wattr_off(view->window,
6316 COLOR_PAIR(tc->colorpair), NULL);
6317 if (width < view->ncols - 1)
6318 waddch(view->window, '\n');
6319 if (n == s->selected && view->focussed)
6320 wstandend(view->window);
6326 s->last_displayed_entry = te;
6335 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6337 struct got_tree_entry *te;
6338 int isroot = s->tree == s->root;
6341 if (s->first_displayed_entry == NULL)
6344 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6345 while (i++ < maxscroll) {
6348 s->first_displayed_entry = NULL;
6351 s->first_displayed_entry = te;
6352 te = got_tree_entry_get_prev(s->tree, te);
6356 static const struct got_error *
6357 tree_scroll_down(struct tog_view *view, int maxscroll)
6359 struct tog_tree_view_state *s = &view->state.tree;
6360 struct got_tree_entry *next, *last;
6363 if (s->first_displayed_entry)
6364 next = got_tree_entry_get_next(s->tree,
6365 s->first_displayed_entry);
6367 next = got_object_tree_get_first_entry(s->tree);
6369 last = s->last_displayed_entry;
6370 while (next && n++ < maxscroll) {
6372 last = got_tree_entry_get_next(s->tree, last);
6373 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6374 s->first_displayed_entry = next;
6375 next = got_tree_entry_get_next(s->tree, next);
6382 static const struct got_error *
6383 tree_entry_path(char **path, struct tog_parent_trees *parents,
6384 struct got_tree_entry *te)
6386 const struct got_error *err = NULL;
6387 struct tog_parent_tree *pt;
6388 size_t len = 2; /* for leading slash and NUL */
6390 TAILQ_FOREACH(pt, parents, entry)
6391 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6394 len += strlen(got_tree_entry_get_name(te));
6396 *path = calloc(1, len);
6398 return got_error_from_errno("calloc");
6401 pt = TAILQ_LAST(parents, tog_parent_trees);
6403 const char *name = got_tree_entry_get_name(pt->selected_entry);
6404 if (strlcat(*path, name, len) >= len) {
6405 err = got_error(GOT_ERR_NO_SPACE);
6408 if (strlcat(*path, "/", len) >= len) {
6409 err = got_error(GOT_ERR_NO_SPACE);
6412 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6415 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6416 err = got_error(GOT_ERR_NO_SPACE);
6428 static const struct got_error *
6429 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6430 struct got_tree_entry *te, struct tog_parent_trees *parents,
6431 struct got_object_id *commit_id, struct got_repository *repo)
6433 const struct got_error *err = NULL;
6435 struct tog_view *blame_view;
6439 err = tree_entry_path(&path, parents, te);
6443 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6444 if (blame_view == NULL) {
6445 err = got_error_from_errno("view_open");
6449 err = open_blame_view(blame_view, path, commit_id, repo);
6451 if (err->code == GOT_ERR_CANCELLED)
6453 view_close(blame_view);
6455 *new_view = blame_view;
6461 static const struct got_error *
6462 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6463 struct tog_tree_view_state *s)
6465 struct tog_view *log_view;
6466 const struct got_error *err = NULL;
6471 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6472 if (log_view == NULL)
6473 return got_error_from_errno("view_open");
6475 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6479 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6482 view_close(log_view);
6484 *new_view = log_view;
6489 static const struct got_error *
6490 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6491 const char *head_ref_name, struct got_repository *repo)
6493 const struct got_error *err = NULL;
6494 char *commit_id_str = NULL;
6495 struct tog_tree_view_state *s = &view->state.tree;
6496 struct got_commit_object *commit = NULL;
6498 TAILQ_INIT(&s->parents);
6499 STAILQ_INIT(&s->colors);
6501 s->commit_id = got_object_id_dup(commit_id);
6502 if (s->commit_id == NULL)
6503 return got_error_from_errno("got_object_id_dup");
6505 err = got_object_open_as_commit(&commit, repo, commit_id);
6510 * The root is opened here and will be closed when the view is closed.
6511 * Any visited subtrees and their path-wise parents are opened and
6514 err = got_object_open_as_tree(&s->root, repo,
6515 got_object_commit_get_tree_id(commit));
6520 err = got_object_id_str(&commit_id_str, commit_id);
6524 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6525 err = got_error_from_errno("asprintf");
6529 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6530 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6531 if (head_ref_name) {
6532 s->head_ref_name = strdup(head_ref_name);
6533 if (s->head_ref_name == NULL) {
6534 err = got_error_from_errno("strdup");
6540 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6541 err = add_color(&s->colors, "\\$$",
6542 TOG_COLOR_TREE_SUBMODULE,
6543 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6546 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6547 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6550 err = add_color(&s->colors, "/$",
6551 TOG_COLOR_TREE_DIRECTORY,
6552 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6556 err = add_color(&s->colors, "\\*$",
6557 TOG_COLOR_TREE_EXECUTABLE,
6558 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6562 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6563 get_color_value("TOG_COLOR_COMMIT"));
6568 view->show = show_tree_view;
6569 view->input = input_tree_view;
6570 view->close = close_tree_view;
6571 view->search_start = search_start_tree_view;
6572 view->search_next = search_next_tree_view;
6574 free(commit_id_str);
6576 got_object_commit_close(commit);
6578 close_tree_view(view);
6582 static const struct got_error *
6583 close_tree_view(struct tog_view *view)
6585 struct tog_tree_view_state *s = &view->state.tree;
6587 free_colors(&s->colors);
6588 free(s->tree_label);
6589 s->tree_label = NULL;
6591 s->commit_id = NULL;
6592 free(s->head_ref_name);
6593 s->head_ref_name = NULL;
6594 while (!TAILQ_EMPTY(&s->parents)) {
6595 struct tog_parent_tree *parent;
6596 parent = TAILQ_FIRST(&s->parents);
6597 TAILQ_REMOVE(&s->parents, parent, entry);
6598 if (parent->tree != s->root)
6599 got_object_tree_close(parent->tree);
6603 if (s->tree != NULL && s->tree != s->root)
6604 got_object_tree_close(s->tree);
6606 got_object_tree_close(s->root);
6610 static const struct got_error *
6611 search_start_tree_view(struct tog_view *view)
6613 struct tog_tree_view_state *s = &view->state.tree;
6615 s->matched_entry = NULL;
6620 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6622 regmatch_t regmatch;
6624 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6628 static const struct got_error *
6629 search_next_tree_view(struct tog_view *view)
6631 struct tog_tree_view_state *s = &view->state.tree;
6632 struct got_tree_entry *te = NULL;
6634 if (!view->searching) {
6635 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6639 if (s->matched_entry) {
6640 if (view->searching == TOG_SEARCH_FORWARD) {
6641 if (s->selected_entry)
6642 te = got_tree_entry_get_next(s->tree,
6645 te = got_object_tree_get_first_entry(s->tree);
6647 if (s->selected_entry == NULL)
6648 te = got_object_tree_get_last_entry(s->tree);
6650 te = got_tree_entry_get_prev(s->tree,
6654 if (s->selected_entry)
6655 te = s->selected_entry;
6656 else if (view->searching == TOG_SEARCH_FORWARD)
6657 te = got_object_tree_get_first_entry(s->tree);
6659 te = got_object_tree_get_last_entry(s->tree);
6664 if (s->matched_entry == NULL) {
6665 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6668 if (view->searching == TOG_SEARCH_FORWARD)
6669 te = got_object_tree_get_first_entry(s->tree);
6671 te = got_object_tree_get_last_entry(s->tree);
6674 if (match_tree_entry(te, &view->regex)) {
6675 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6676 s->matched_entry = te;
6680 if (view->searching == TOG_SEARCH_FORWARD)
6681 te = got_tree_entry_get_next(s->tree, te);
6683 te = got_tree_entry_get_prev(s->tree, te);
6686 if (s->matched_entry) {
6687 s->first_displayed_entry = s->matched_entry;
6694 static const struct got_error *
6695 show_tree_view(struct tog_view *view)
6697 const struct got_error *err = NULL;
6698 struct tog_tree_view_state *s = &view->state.tree;
6701 err = tree_entry_path(&parent_path, &s->parents, NULL);
6705 err = draw_tree_entries(view, parent_path);
6712 static const struct got_error *
6713 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6715 const struct got_error *err = NULL;
6716 struct tog_tree_view_state *s = &view->state.tree;
6717 struct got_tree_entry *te;
6718 int n, nscroll = view->nlines - 3;
6722 s->show_ids = !s->show_ids;
6727 if (!s->selected_entry)
6729 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6733 err = view_request_new(new_view, view, TOG_VIEW_REF);
6739 if (s->tree == s->root)
6740 s->first_displayed_entry =
6741 got_object_tree_get_first_entry(s->tree);
6743 s->first_displayed_entry = NULL;
6747 int eos = view->nlines - 3;
6749 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6753 te = got_object_tree_get_last_entry(s->tree);
6754 for (n = 0; n < eos; n++) {
6756 if (s->tree != s->root) {
6757 s->first_displayed_entry = NULL;
6762 s->first_displayed_entry = te;
6763 te = got_tree_entry_get_prev(s->tree, te);
6766 s->selected = n - 1;
6772 if (s->selected > 0) {
6776 tree_scroll_up(s, 1);
6777 if (s->selected_entry == NULL ||
6778 (s->tree == s->root && s->selected_entry ==
6779 got_object_tree_get_first_entry(s->tree)))
6789 if (s->tree == s->root) {
6790 if (got_object_tree_get_first_entry(s->tree) ==
6791 s->first_displayed_entry)
6792 s->selected -= MIN(s->selected, nscroll);
6794 if (s->first_displayed_entry == NULL)
6795 s->selected -= MIN(s->selected, nscroll);
6797 tree_scroll_up(s, MAX(0, nscroll));
6798 if (s->selected_entry == NULL ||
6799 (s->tree == s->root && s->selected_entry ==
6800 got_object_tree_get_first_entry(s->tree)))
6806 if (s->selected < s->ndisplayed - 1) {
6810 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6812 /* can't scroll any further */
6816 tree_scroll_down(view, 1);
6826 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6828 /* can't scroll any further; move cursor down */
6829 if (s->selected < s->ndisplayed - 1)
6830 s->selected += MIN(nscroll,
6831 s->ndisplayed - s->selected - 1);
6836 tree_scroll_down(view, nscroll);
6841 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6842 struct tog_parent_tree *parent;
6843 /* user selected '..' */
6844 if (s->tree == s->root) {
6848 parent = TAILQ_FIRST(&s->parents);
6849 TAILQ_REMOVE(&s->parents, parent,
6851 got_object_tree_close(s->tree);
6852 s->tree = parent->tree;
6853 s->first_displayed_entry =
6854 parent->first_displayed_entry;
6856 parent->selected_entry;
6857 s->selected = parent->selected;
6858 if (s->selected > view->nlines - 3) {
6859 err = offset_selection_down(view);
6864 } else if (S_ISDIR(got_tree_entry_get_mode(
6865 s->selected_entry))) {
6866 struct got_tree_object *subtree;
6868 err = got_object_open_as_tree(&subtree, s->repo,
6869 got_tree_entry_get_id(s->selected_entry));
6872 err = tree_view_visit_subtree(s, subtree);
6874 got_object_tree_close(subtree);
6877 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
6878 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
6881 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6882 s->selected = view->nlines - 4;
6898 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6903 static const struct got_error *
6904 cmd_tree(int argc, char *argv[])
6906 const struct got_error *error;
6907 struct got_repository *repo = NULL;
6908 struct got_worktree *worktree = NULL;
6909 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6910 struct got_object_id *commit_id = NULL;
6911 struct got_commit_object *commit = NULL;
6912 const char *commit_id_arg = NULL;
6914 struct got_reference *ref = NULL;
6915 const char *head_ref_name = NULL;
6917 struct tog_view *view;
6918 int *pack_fds = NULL;
6920 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6923 commit_id_arg = optarg;
6926 repo_path = realpath(optarg, NULL);
6927 if (repo_path == NULL)
6928 return got_error_from_errno2("realpath",
6943 error = got_repo_pack_fds_open(&pack_fds);
6947 if (repo_path == NULL) {
6948 cwd = getcwd(NULL, 0);
6950 return got_error_from_errno("getcwd");
6951 error = got_worktree_open(&worktree, cwd);
6952 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6956 strdup(got_worktree_get_repo_path(worktree));
6958 repo_path = strdup(cwd);
6959 if (repo_path == NULL) {
6960 error = got_error_from_errno("strdup");
6965 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6969 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6976 error = apply_unveil(got_repo_get_path(repo), NULL);
6980 error = tog_load_refs(repo, 0);
6984 if (commit_id_arg == NULL) {
6985 error = got_repo_match_object_id(&commit_id, &label,
6986 worktree ? got_worktree_get_head_ref_name(worktree) :
6987 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6990 head_ref_name = label;
6992 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6994 head_ref_name = got_ref_get_name(ref);
6995 else if (error->code != GOT_ERR_NOT_REF)
6997 error = got_repo_match_object_id(&commit_id, NULL,
6998 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7003 error = got_object_open_as_commit(&commit, repo, commit_id);
7007 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7009 error = got_error_from_errno("view_open");
7012 error = open_tree_view(view, commit_id, head_ref_name, repo);
7015 if (!got_path_is_root_dir(in_repo_path)) {
7016 error = tree_view_walk_path(&view->state.tree, commit,
7023 /* Release work tree lock. */
7024 got_worktree_close(worktree);
7027 error = view_loop(view);
7036 const struct got_error *close_err = got_repo_close(repo);
7041 const struct got_error *pack_err =
7042 got_repo_pack_fds_close(pack_fds);
7050 static const struct got_error *
7051 ref_view_load_refs(struct tog_ref_view_state *s)
7053 struct got_reflist_entry *sre;
7054 struct tog_reflist_entry *re;
7057 TAILQ_FOREACH(sre, &tog_refs, entry) {
7058 if (strncmp(got_ref_get_name(sre->ref),
7059 "refs/got/", 9) == 0 &&
7060 strncmp(got_ref_get_name(sre->ref),
7061 "refs/got/backup/", 16) != 0)
7064 re = malloc(sizeof(*re));
7066 return got_error_from_errno("malloc");
7068 re->ref = got_ref_dup(sre->ref);
7069 if (re->ref == NULL)
7070 return got_error_from_errno("got_ref_dup");
7071 re->idx = s->nrefs++;
7072 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7075 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7080 ref_view_free_refs(struct tog_ref_view_state *s)
7082 struct tog_reflist_entry *re;
7084 while (!TAILQ_EMPTY(&s->refs)) {
7085 re = TAILQ_FIRST(&s->refs);
7086 TAILQ_REMOVE(&s->refs, re, entry);
7087 got_ref_close(re->ref);
7092 static const struct got_error *
7093 open_ref_view(struct tog_view *view, struct got_repository *repo)
7095 const struct got_error *err = NULL;
7096 struct tog_ref_view_state *s = &view->state.ref;
7098 s->selected_entry = 0;
7101 TAILQ_INIT(&s->refs);
7102 STAILQ_INIT(&s->colors);
7104 err = ref_view_load_refs(s);
7108 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7109 err = add_color(&s->colors, "^refs/heads/",
7110 TOG_COLOR_REFS_HEADS,
7111 get_color_value("TOG_COLOR_REFS_HEADS"));
7115 err = add_color(&s->colors, "^refs/tags/",
7116 TOG_COLOR_REFS_TAGS,
7117 get_color_value("TOG_COLOR_REFS_TAGS"));
7121 err = add_color(&s->colors, "^refs/remotes/",
7122 TOG_COLOR_REFS_REMOTES,
7123 get_color_value("TOG_COLOR_REFS_REMOTES"));
7127 err = add_color(&s->colors, "^refs/got/backup/",
7128 TOG_COLOR_REFS_BACKUP,
7129 get_color_value("TOG_COLOR_REFS_BACKUP"));
7134 view->show = show_ref_view;
7135 view->input = input_ref_view;
7136 view->close = close_ref_view;
7137 view->search_start = search_start_ref_view;
7138 view->search_next = search_next_ref_view;
7141 free_colors(&s->colors);
7145 static const struct got_error *
7146 close_ref_view(struct tog_view *view)
7148 struct tog_ref_view_state *s = &view->state.ref;
7150 ref_view_free_refs(s);
7151 free_colors(&s->colors);
7156 static const struct got_error *
7157 resolve_reflist_entry(struct got_object_id **commit_id,
7158 struct tog_reflist_entry *re, struct got_repository *repo)
7160 const struct got_error *err = NULL;
7161 struct got_object_id *obj_id;
7162 struct got_tag_object *tag = NULL;
7167 err = got_ref_resolve(&obj_id, repo, re->ref);
7171 err = got_object_get_type(&obj_type, repo, obj_id);
7176 case GOT_OBJ_TYPE_COMMIT:
7177 *commit_id = obj_id;
7179 case GOT_OBJ_TYPE_TAG:
7180 err = got_object_open_as_tag(&tag, repo, obj_id);
7184 err = got_object_get_type(&obj_type, repo,
7185 got_object_tag_get_object_id(tag));
7188 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7189 err = got_error(GOT_ERR_OBJ_TYPE);
7192 *commit_id = got_object_id_dup(
7193 got_object_tag_get_object_id(tag));
7194 if (*commit_id == NULL) {
7195 err = got_error_from_errno("got_object_id_dup");
7200 err = got_error(GOT_ERR_OBJ_TYPE);
7206 got_object_tag_close(tag);
7214 static const struct got_error *
7215 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7216 struct tog_reflist_entry *re, struct got_repository *repo)
7218 struct tog_view *log_view;
7219 const struct got_error *err = NULL;
7220 struct got_object_id *commit_id = NULL;
7224 err = resolve_reflist_entry(&commit_id, re, repo);
7226 if (err->code != GOT_ERR_OBJ_TYPE)
7232 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7233 if (log_view == NULL) {
7234 err = got_error_from_errno("view_open");
7238 err = open_log_view(log_view, commit_id, repo,
7239 got_ref_get_name(re->ref), "", 0);
7242 view_close(log_view);
7244 *new_view = log_view;
7250 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7252 struct tog_reflist_entry *re;
7255 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7258 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7259 while (i++ < maxscroll) {
7262 s->first_displayed_entry = re;
7263 re = TAILQ_PREV(re, tog_reflist_head, entry);
7267 static const struct got_error *
7268 ref_scroll_down(struct tog_view *view, int maxscroll)
7270 struct tog_ref_view_state *s = &view->state.ref;
7271 struct tog_reflist_entry *next, *last;
7274 if (s->first_displayed_entry)
7275 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7277 next = TAILQ_FIRST(&s->refs);
7279 last = s->last_displayed_entry;
7280 while (next && n++ < maxscroll) {
7282 last = TAILQ_NEXT(last, entry);
7283 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7284 s->first_displayed_entry = next;
7285 next = TAILQ_NEXT(next, entry);
7292 static const struct got_error *
7293 search_start_ref_view(struct tog_view *view)
7295 struct tog_ref_view_state *s = &view->state.ref;
7297 s->matched_entry = NULL;
7302 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7304 regmatch_t regmatch;
7306 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7310 static const struct got_error *
7311 search_next_ref_view(struct tog_view *view)
7313 struct tog_ref_view_state *s = &view->state.ref;
7314 struct tog_reflist_entry *re = NULL;
7316 if (!view->searching) {
7317 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7321 if (s->matched_entry) {
7322 if (view->searching == TOG_SEARCH_FORWARD) {
7323 if (s->selected_entry)
7324 re = TAILQ_NEXT(s->selected_entry, entry);
7326 re = TAILQ_PREV(s->selected_entry,
7327 tog_reflist_head, entry);
7329 if (s->selected_entry == NULL)
7330 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7332 re = TAILQ_PREV(s->selected_entry,
7333 tog_reflist_head, entry);
7336 if (s->selected_entry)
7337 re = s->selected_entry;
7338 else if (view->searching == TOG_SEARCH_FORWARD)
7339 re = TAILQ_FIRST(&s->refs);
7341 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7346 if (s->matched_entry == NULL) {
7347 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7350 if (view->searching == TOG_SEARCH_FORWARD)
7351 re = TAILQ_FIRST(&s->refs);
7353 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7356 if (match_reflist_entry(re, &view->regex)) {
7357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7358 s->matched_entry = re;
7362 if (view->searching == TOG_SEARCH_FORWARD)
7363 re = TAILQ_NEXT(re, entry);
7365 re = TAILQ_PREV(re, tog_reflist_head, entry);
7368 if (s->matched_entry) {
7369 s->first_displayed_entry = s->matched_entry;
7376 static const struct got_error *
7377 show_ref_view(struct tog_view *view)
7379 const struct got_error *err = NULL;
7380 struct tog_ref_view_state *s = &view->state.ref;
7381 struct tog_reflist_entry *re;
7384 struct tog_color *tc;
7386 int limit = view->nlines;
7388 werase(view->window);
7391 if (view_is_hsplit_top(view))
7392 --limit; /* border */
7397 re = s->first_displayed_entry;
7399 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7401 return got_error_from_errno("asprintf");
7403 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7408 if (view_needs_focus_indication(view))
7409 wstandout(view->window);
7410 waddwstr(view->window, wline);
7411 if (view_needs_focus_indication(view))
7412 wstandend(view->window);
7417 if (width < view->ncols - 1)
7418 waddch(view->window, '\n');
7423 while (re && limit > 0) {
7425 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7428 struct got_commit_object *ci;
7429 struct got_tag_object *tag;
7430 struct got_object_id *id;
7434 err = got_ref_resolve(&id, s->repo, re->ref);
7437 err = got_object_open_as_tag(&tag, s->repo, id);
7439 if (err->code != GOT_ERR_OBJ_TYPE) {
7443 err = got_object_open_as_commit(&ci, s->repo,
7449 t = got_object_commit_get_committer_time(ci);
7450 got_object_commit_close(ci);
7452 t = got_object_tag_get_tagger_time(tag);
7453 got_object_tag_close(tag);
7456 if (gmtime_r(&t, &tm) == NULL)
7457 return got_error_from_errno("gmtime_r");
7458 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7459 return got_error(GOT_ERR_NO_SPACE);
7461 if (got_ref_is_symbolic(re->ref)) {
7462 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7463 ymd : "", got_ref_get_name(re->ref),
7464 got_ref_get_symref_target(re->ref)) == -1)
7465 return got_error_from_errno("asprintf");
7466 } else if (s->show_ids) {
7467 struct got_object_id *id;
7469 err = got_ref_resolve(&id, s->repo, re->ref);
7472 err = got_object_id_str(&id_str, id);
7477 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7478 got_ref_get_name(re->ref), id_str) == -1) {
7479 err = got_error_from_errno("asprintf");
7486 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7487 got_ref_get_name(re->ref)) == -1)
7488 return got_error_from_errno("asprintf");
7490 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7496 if (n == s->selected) {
7498 wstandout(view->window);
7499 s->selected_entry = re;
7501 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7503 wattr_on(view->window,
7504 COLOR_PAIR(tc->colorpair), NULL);
7505 waddwstr(view->window, wline);
7507 wattr_off(view->window,
7508 COLOR_PAIR(tc->colorpair), NULL);
7509 if (width < view->ncols - 1)
7510 waddch(view->window, '\n');
7511 if (n == s->selected && view->focussed)
7512 wstandend(view->window);
7518 s->last_displayed_entry = re;
7521 re = TAILQ_NEXT(re, entry);
7528 static const struct got_error *
7529 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7530 struct tog_reflist_entry *re, struct got_repository *repo)
7532 const struct got_error *err = NULL;
7533 struct got_object_id *commit_id = NULL;
7534 struct tog_view *tree_view;
7538 err = resolve_reflist_entry(&commit_id, re, repo);
7540 if (err->code != GOT_ERR_OBJ_TYPE)
7547 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7548 if (tree_view == NULL) {
7549 err = got_error_from_errno("view_open");
7553 err = open_tree_view(tree_view, commit_id,
7554 got_ref_get_name(re->ref), repo);
7558 *new_view = tree_view;
7563 static const struct got_error *
7564 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7566 const struct got_error *err = NULL;
7567 struct tog_ref_view_state *s = &view->state.ref;
7568 struct tog_reflist_entry *re;
7569 int n, nscroll = view->nlines - 1;
7573 s->show_ids = !s->show_ids;
7577 s->show_date = !s->show_date;
7581 s->sort_by_date = !s->sort_by_date;
7583 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7584 got_ref_cmp_by_commit_timestamp_descending :
7585 tog_ref_cmp_by_name, s->repo);
7588 got_reflist_object_id_map_free(tog_refs_idmap);
7589 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7590 &tog_refs, s->repo);
7593 ref_view_free_refs(s);
7594 err = ref_view_load_refs(s);
7599 if (!s->selected_entry)
7601 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7605 if (!s->selected_entry)
7607 err = view_request_new(new_view, view, TOG_VIEW_TREE);
7613 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7617 int eos = view->nlines - 1;
7619 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7623 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7624 for (n = 0; n < eos; n++) {
7627 s->first_displayed_entry = re;
7628 re = TAILQ_PREV(re, tog_reflist_head, entry);
7631 s->selected = n - 1;
7637 if (s->selected > 0) {
7641 ref_scroll_up(s, 1);
7642 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7652 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7653 s->selected -= MIN(nscroll, s->selected);
7654 ref_scroll_up(s, MAX(0, nscroll));
7655 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7661 if (s->selected < s->ndisplayed - 1) {
7665 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7666 /* can't scroll any further */
7670 ref_scroll_down(view, 1);
7680 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7681 /* can't scroll any further; move cursor down */
7682 if (s->selected < s->ndisplayed - 1)
7683 s->selected += MIN(nscroll,
7684 s->ndisplayed - s->selected - 1);
7685 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7686 s->selected += s->ndisplayed - s->selected - 1;
7690 ref_scroll_down(view, nscroll);
7695 err = tog_load_refs(s->repo, s->sort_by_date);
7698 ref_view_free_refs(s);
7699 err = ref_view_load_refs(s);
7702 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7703 s->selected = view->nlines - 2;
7717 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7722 static const struct got_error *
7723 cmd_ref(int argc, char *argv[])
7725 const struct got_error *error;
7726 struct got_repository *repo = NULL;
7727 struct got_worktree *worktree = NULL;
7728 char *cwd = NULL, *repo_path = NULL;
7730 struct tog_view *view;
7731 int *pack_fds = NULL;
7733 while ((ch = getopt(argc, argv, "r:")) != -1) {
7736 repo_path = realpath(optarg, NULL);
7737 if (repo_path == NULL)
7738 return got_error_from_errno2("realpath",
7753 error = got_repo_pack_fds_open(&pack_fds);
7757 if (repo_path == NULL) {
7758 cwd = getcwd(NULL, 0);
7760 return got_error_from_errno("getcwd");
7761 error = got_worktree_open(&worktree, cwd);
7762 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7766 strdup(got_worktree_get_repo_path(worktree));
7768 repo_path = strdup(cwd);
7769 if (repo_path == NULL) {
7770 error = got_error_from_errno("strdup");
7775 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7781 error = apply_unveil(got_repo_get_path(repo), NULL);
7785 error = tog_load_refs(repo, 0);
7789 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7791 error = got_error_from_errno("view_open");
7795 error = open_ref_view(view, repo);
7800 /* Release work tree lock. */
7801 got_worktree_close(worktree);
7804 error = view_loop(view);
7809 const struct got_error *close_err = got_repo_close(repo);
7814 const struct got_error *pack_err =
7815 got_repo_pack_fds_close(pack_fds);
7823 static const struct got_error *
7824 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
7825 enum tog_view_type request, int y, int x)
7827 const struct got_error *err = NULL;
7833 if (view->type == TOG_VIEW_LOG) {
7834 struct tog_log_view_state *s = &view->state.log;
7836 err = open_diff_view_for_commit(new_view, y, x,
7837 s->selected_entry->commit, s->selected_entry->id,
7840 return got_error_msg(GOT_ERR_NOT_IMPL,
7841 "parent/child view pair not supported");
7843 case TOG_VIEW_BLAME:
7844 if (view->type == TOG_VIEW_TREE) {
7845 struct tog_tree_view_state *s = &view->state.tree;
7847 err = blame_tree_entry(new_view, y, x,
7848 s->selected_entry, &s->parents, s->commit_id,
7851 return got_error_msg(GOT_ERR_NOT_IMPL,
7852 "parent/child view pair not supported");
7855 if (view->type == TOG_VIEW_BLAME)
7856 err = log_annotated_line(new_view, y, x,
7857 view->state.blame.repo, view->state.blame.id_to_log);
7858 else if (view->type == TOG_VIEW_TREE)
7859 err = log_selected_tree_entry(new_view, y, x,
7861 else if (view->type == TOG_VIEW_REF)
7862 err = log_ref_entry(new_view, y, x,
7863 view->state.ref.selected_entry,
7864 view->state.ref.repo);
7866 return got_error_msg(GOT_ERR_NOT_IMPL,
7867 "parent/child view pair not supported");
7870 if (view->type == TOG_VIEW_LOG)
7871 err = browse_commit_tree(new_view, y, x,
7872 view->state.log.selected_entry,
7873 view->state.log.in_repo_path,
7874 view->state.log.head_ref_name,
7875 view->state.log.repo);
7876 else if (view->type == TOG_VIEW_REF)
7877 err = browse_ref_tree(new_view, y, x,
7878 view->state.ref.selected_entry,
7879 view->state.ref.repo);
7881 return got_error_msg(GOT_ERR_NOT_IMPL,
7882 "parent/child view pair not supported");
7885 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
7886 if (*new_view == NULL)
7887 return got_error_from_errno("view_open");
7888 if (view->type == TOG_VIEW_LOG)
7889 err = open_ref_view(*new_view, view->state.log.repo);
7890 else if (view->type == TOG_VIEW_TREE)
7891 err = open_ref_view(*new_view, view->state.tree.repo);
7893 err = got_error_msg(GOT_ERR_NOT_IMPL,
7894 "parent/child view pair not supported");
7896 view_close(*new_view);
7899 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
7906 * If view was scrolled down to move the selected line into view when opening a
7907 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7910 offset_selection_up(struct tog_view *view)
7912 switch (view->type) {
7913 case TOG_VIEW_BLAME: {
7914 struct tog_blame_view_state *s = &view->state.blame;
7915 if (s->first_displayed_line == 1) {
7916 s->selected_line = MAX(s->selected_line - view->offset,
7920 if (s->first_displayed_line > view->offset)
7921 s->first_displayed_line -= view->offset;
7923 s->first_displayed_line = 1;
7924 s->selected_line += view->offset;
7928 log_scroll_up(&view->state.log, view->offset);
7929 view->state.log.selected += view->offset;
7932 ref_scroll_up(&view->state.ref, view->offset);
7933 view->state.ref.selected += view->offset;
7936 tree_scroll_up(&view->state.tree, view->offset);
7937 view->state.tree.selected += view->offset;
7947 * If the selected line is in the section of screen covered by the bottom split,
7948 * scroll down offset lines to move it into view and index its new position.
7950 static const struct got_error *
7951 offset_selection_down(struct tog_view *view)
7953 const struct got_error *err = NULL;
7954 const struct got_error *(*scrolld)(struct tog_view *, int);
7955 int *selected = NULL;
7958 switch (view->type) {
7959 case TOG_VIEW_BLAME: {
7960 struct tog_blame_view_state *s = &view->state.blame;
7963 if (s->selected_line > view->nlines - header) {
7964 offset = abs(view->nlines - s->selected_line - header);
7965 s->first_displayed_line += offset;
7966 s->selected_line -= offset;
7967 view->offset = offset;
7971 case TOG_VIEW_LOG: {
7972 struct tog_log_view_state *s = &view->state.log;
7973 scrolld = &log_scroll_down;
7974 header = view_is_parent_view(view) ? 3 : 2;
7975 selected = &s->selected;
7978 case TOG_VIEW_REF: {
7979 struct tog_ref_view_state *s = &view->state.ref;
7980 scrolld = &ref_scroll_down;
7982 selected = &s->selected;
7985 case TOG_VIEW_TREE: {
7986 struct tog_tree_view_state *s = &view->state.tree;
7987 scrolld = &tree_scroll_down;
7989 selected = &s->selected;
7999 if (selected && *selected > view->nlines - header) {
8000 offset = abs(view->nlines - *selected - header);
8001 view->offset = offset;
8002 if (scrolld && offset) {
8003 err = scrolld(view, offset);
8004 *selected -= offset;
8012 list_commands(FILE *fp)
8016 fprintf(fp, "commands:");
8017 for (i = 0; i < nitems(tog_commands); i++) {
8018 const struct tog_cmd *cmd = &tog_commands[i];
8019 fprintf(fp, " %s", cmd->name);
8025 usage(int hflag, int status)
8027 FILE *fp = (status == 0) ? stdout : stderr;
8029 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8032 fprintf(fp, "lazy usage: %s path\n", getprogname());
8039 make_argv(int argc, ...)
8047 argv = calloc(argc, sizeof(char *));
8050 for (i = 0; i < argc; i++) {
8051 argv[i] = strdup(va_arg(ap, char *));
8052 if (argv[i] == NULL)
8061 * Try to convert 'tog path' into a 'tog log path' command.
8062 * The user could simply have mistyped the command rather than knowingly
8063 * provided a path. So check whether argv[0] can in fact be resolved
8064 * to a path in the HEAD commit and print a special error if not.
8065 * This hack is for mpi@ <3
8067 static const struct got_error *
8068 tog_log_with_path(int argc, char *argv[])
8070 const struct got_error *error = NULL, *close_err;
8071 const struct tog_cmd *cmd = NULL;
8072 struct got_repository *repo = NULL;
8073 struct got_worktree *worktree = NULL;
8074 struct got_object_id *commit_id = NULL, *id = NULL;
8075 struct got_commit_object *commit = NULL;
8076 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8077 char *commit_id_str = NULL, **cmd_argv = NULL;
8078 int *pack_fds = NULL;
8080 cwd = getcwd(NULL, 0);
8082 return got_error_from_errno("getcwd");
8084 error = got_repo_pack_fds_open(&pack_fds);
8088 error = got_worktree_open(&worktree, cwd);
8089 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8093 repo_path = strdup(got_worktree_get_repo_path(worktree));
8095 repo_path = strdup(cwd);
8096 if (repo_path == NULL) {
8097 error = got_error_from_errno("strdup");
8101 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8105 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8110 error = tog_load_refs(repo, 0);
8113 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8114 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8115 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8120 got_worktree_close(worktree);
8124 error = got_object_open_as_commit(&commit, repo, commit_id);
8128 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8130 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8132 fprintf(stderr, "%s: '%s' is no known command or path\n",
8133 getprogname(), argv[0]);
8138 error = got_object_id_str(&commit_id_str, commit_id);
8142 cmd = &tog_commands[0]; /* log */
8144 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8145 error = cmd->cmd_main(argc, cmd_argv);
8148 close_err = got_repo_close(repo);
8153 got_object_commit_close(commit);
8155 got_worktree_close(worktree);
8157 const struct got_error *pack_err =
8158 got_repo_pack_fds_close(pack_fds);
8163 free(commit_id_str);
8170 for (i = 0; i < argc; i++)
8179 main(int argc, char *argv[])
8181 const struct got_error *error = NULL;
8182 const struct tog_cmd *cmd = NULL;
8183 int ch, hflag = 0, Vflag = 0;
8184 char **cmd_argv = NULL;
8185 static const struct option longopts[] = {
8186 { "version", no_argument, NULL, 'V' },
8189 char *diff_algo_str = NULL;
8191 setlocale(LC_CTYPE, "");
8193 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8213 got_version_print_str();
8218 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8226 /* Build an argument vector which runs a default command. */
8227 cmd = &tog_commands[0];
8229 cmd_argv = make_argv(argc, cmd->name);
8233 /* Did the user specify a command? */
8234 for (i = 0; i < nitems(tog_commands); i++) {
8235 if (strncmp(tog_commands[i].name, argv[0],
8236 strlen(argv[0])) == 0) {
8237 cmd = &tog_commands[i];
8243 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8244 if (diff_algo_str) {
8245 if (strcasecmp(diff_algo_str, "patience") == 0)
8246 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8247 if (strcasecmp(diff_algo_str, "myers") == 0)
8248 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8254 /* No command specified; try log with a path */
8255 error = tog_log_with_path(argc, argv);
8260 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8267 for (i = 0; i < argc; i++)
8272 if (error && error->code != GOT_ERR_CANCELLED)
8273 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);