2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
127 struct commit_queue_head head;
131 STAILQ_ENTRY(tog_color) entry;
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
155 } else if (isbackup1 && !isbackup2) {
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 STAILQ_INSERT_HEAD(colors, tc, entry);
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
296 if (strcasecmp(val, "red") == 0)
298 if (strcasecmp(val, "green") == 0)
300 if (strcasecmp(val, "yellow") == 0)
302 if (strcasecmp(val, "blue") == 0)
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
308 if (strcasecmp(val, "white") == 0)
310 if (strcasecmp(val, "default") == 0)
313 return default_color_value(envvar);
317 struct tog_diff_view_state {
318 struct got_object_id *id1, *id2;
319 const char *label1, *label2;
322 int first_displayed_line;
323 int last_displayed_line;
326 int ignore_whitespace;
328 struct got_repository *repo;
329 struct tog_colors colors;
335 /* passed from log or blame view; may be NULL */
336 struct tog_view *parent_view;
339 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 static volatile sig_atomic_t tog_thread_error;
342 struct tog_log_thread_args {
343 pthread_cond_t need_commits;
344 pthread_cond_t commit_loaded;
347 struct got_commit_graph *graph;
348 struct commit_queue *commits;
349 const char *in_repo_path;
350 struct got_object_id *start_id;
351 struct got_repository *repo;
355 struct commit_queue_entry **first_displayed_entry;
356 struct commit_queue_entry **selected_entry;
358 int *search_next_done;
362 struct tog_log_view_state {
363 struct commit_queue commits;
364 struct commit_queue_entry *first_displayed_entry;
365 struct commit_queue_entry *last_displayed_entry;
366 struct commit_queue_entry *selected_entry;
371 struct got_repository *repo;
372 struct got_object_id *start_id;
375 struct tog_log_thread_args thread_args;
376 struct commit_queue_entry *matched_entry;
377 struct commit_queue_entry *search_entry;
378 struct tog_colors colors;
381 #define TOG_COLOR_DIFF_MINUS 1
382 #define TOG_COLOR_DIFF_PLUS 2
383 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
384 #define TOG_COLOR_DIFF_META 4
385 #define TOG_COLOR_TREE_SUBMODULE 5
386 #define TOG_COLOR_TREE_SYMLINK 6
387 #define TOG_COLOR_TREE_DIRECTORY 7
388 #define TOG_COLOR_TREE_EXECUTABLE 8
389 #define TOG_COLOR_COMMIT 9
390 #define TOG_COLOR_AUTHOR 10
391 #define TOG_COLOR_DATE 11
392 #define TOG_COLOR_REFS_HEADS 12
393 #define TOG_COLOR_REFS_TAGS 13
394 #define TOG_COLOR_REFS_REMOTES 14
395 #define TOG_COLOR_REFS_BACKUP 15
397 struct tog_blame_cb_args {
398 struct tog_blame_line *lines; /* one per line */
401 struct tog_view *view;
402 struct got_object_id *commit_id;
406 struct tog_blame_thread_args {
408 struct got_repository *repo;
409 struct tog_blame_cb_args *cb_args;
411 got_cancel_cb cancel_cb;
418 struct tog_blame_line *lines;
422 struct tog_blame_thread_args thread_args;
423 struct tog_blame_cb_args cb_args;
428 struct tog_blame_view_state {
429 int first_displayed_line;
430 int last_displayed_line;
432 int last_diffed_line;
436 struct got_object_id_queue blamed_commits;
437 struct got_object_qid *blamed_commit;
439 struct got_repository *repo;
440 struct got_object_id *commit_id;
441 struct tog_blame blame;
443 struct tog_colors colors;
446 struct tog_parent_tree {
447 TAILQ_ENTRY(tog_parent_tree) entry;
448 struct got_tree_object *tree;
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *selected_entry;
454 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
456 struct tog_tree_view_state {
458 struct got_object_id *commit_id;/* commit which this tree belongs to */
459 struct got_tree_object *root; /* the commit's root tree entry */
460 struct got_tree_object *tree; /* currently displayed (sub-)tree */
461 struct got_tree_entry *first_displayed_entry;
462 struct got_tree_entry *last_displayed_entry;
463 struct got_tree_entry *selected_entry;
464 int ndisplayed, selected, show_ids;
465 struct tog_parent_trees parents; /* parent trees of current sub-tree */
467 struct got_repository *repo;
468 struct got_tree_entry *matched_entry;
469 struct tog_colors colors;
472 struct tog_reflist_entry {
473 TAILQ_ENTRY(tog_reflist_entry) entry;
474 struct got_reference *ref;
478 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
480 struct tog_ref_view_state {
481 struct tog_reflist_head refs;
482 struct tog_reflist_entry *first_displayed_entry;
483 struct tog_reflist_entry *last_displayed_entry;
484 struct tog_reflist_entry *selected_entry;
485 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
486 struct got_repository *repo;
487 struct tog_reflist_entry *matched_entry;
488 struct tog_colors colors;
492 * We implement two types of views: parent views and child views.
494 * The 'Tab' key switches focus between a parent view and its child view.
495 * Child views are shown side-by-side to their parent view, provided
496 * there is enough screen estate.
498 * When a new view is opened from within a parent view, this new view
499 * becomes a child view of the parent view, replacing any existing child.
501 * When a new view is opened from within a child view, this new view
502 * becomes a parent view which will obscure the views below until the
503 * user quits the new parent view by typing 'q'.
505 * This list of views contains parent views only.
506 * Child views are only pointed to by their parent view.
508 TAILQ_HEAD(tog_view_list_head, tog_view);
511 TAILQ_ENTRY(tog_view) entry;
514 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
515 int resized_y, resized_x; /* begin_y/x based on user resizing */
516 int maxx, x; /* max column and current start column */
517 int lines, cols; /* copies of LINES and COLS */
518 int nscrolled, offset; /* lines scrolled and hsplit line offset */
519 int ch, count; /* current keymap and count prefix */
520 int resize; /* set when in a resize event */
521 int focussed; /* Only set on one parent or child view at a time. */
523 struct tog_view *parent;
524 struct tog_view *child;
527 * This flag is initially set on parent views when a new child view
528 * is created. It gets toggled when the 'Tab' key switches focus
529 * between parent and child.
530 * The flag indicates whether focus should be passed on to our child
531 * view if this parent view gets picked for focus after another parent
532 * view was closed. This prevents child views from losing focus in such
537 enum tog_view_mode mode;
538 /* type-specific state */
539 enum tog_view_type type;
541 struct tog_diff_view_state diff;
542 struct tog_log_view_state log;
543 struct tog_blame_view_state blame;
544 struct tog_tree_view_state tree;
545 struct tog_ref_view_state ref;
548 const struct got_error *(*show)(struct tog_view *);
549 const struct got_error *(*input)(struct tog_view **,
550 struct tog_view *, int);
551 const struct got_error *(*reset)(struct tog_view *);
552 const struct got_error *(*close)(struct tog_view *);
554 const struct got_error *(*search_start)(struct tog_view *);
555 const struct got_error *(*search_next)(struct tog_view *);
558 #define TOG_SEARCH_FORWARD 1
559 #define TOG_SEARCH_BACKWARD 2
560 int search_next_done;
561 #define TOG_SEARCH_HAVE_MORE 1
562 #define TOG_SEARCH_NO_MORE 2
563 #define TOG_SEARCH_HAVE_NONE 3
568 static const struct got_error *open_diff_view(struct tog_view *,
569 struct got_object_id *, struct got_object_id *,
570 const char *, const char *, int, int, int, struct tog_view *,
571 struct got_repository *);
572 static const struct got_error *show_diff_view(struct tog_view *);
573 static const struct got_error *input_diff_view(struct tog_view **,
574 struct tog_view *, int);
575 static const struct got_error *reset_diff_view(struct tog_view *);
576 static const struct got_error* close_diff_view(struct tog_view *);
577 static const struct got_error *search_start_diff_view(struct tog_view *);
578 static const struct got_error *search_next_diff_view(struct tog_view *);
580 static const struct got_error *open_log_view(struct tog_view *,
581 struct got_object_id *, struct got_repository *,
582 const char *, const char *, int);
583 static const struct got_error * show_log_view(struct tog_view *);
584 static const struct got_error *input_log_view(struct tog_view **,
585 struct tog_view *, int);
586 static const struct got_error *close_log_view(struct tog_view *);
587 static const struct got_error *search_start_log_view(struct tog_view *);
588 static const struct got_error *search_next_log_view(struct tog_view *);
590 static const struct got_error *open_blame_view(struct tog_view *, char *,
591 struct got_object_id *, struct got_repository *);
592 static const struct got_error *show_blame_view(struct tog_view *);
593 static const struct got_error *input_blame_view(struct tog_view **,
594 struct tog_view *, int);
595 static const struct got_error *reset_blame_view(struct tog_view *);
596 static const struct got_error *close_blame_view(struct tog_view *);
597 static const struct got_error *search_start_blame_view(struct tog_view *);
598 static const struct got_error *search_next_blame_view(struct tog_view *);
600 static const struct got_error *open_tree_view(struct tog_view *,
601 struct got_object_id *, const char *, struct got_repository *);
602 static const struct got_error *show_tree_view(struct tog_view *);
603 static const struct got_error *input_tree_view(struct tog_view **,
604 struct tog_view *, int);
605 static const struct got_error *close_tree_view(struct tog_view *);
606 static const struct got_error *search_start_tree_view(struct tog_view *);
607 static const struct got_error *search_next_tree_view(struct tog_view *);
609 static const struct got_error *open_ref_view(struct tog_view *,
610 struct got_repository *);
611 static const struct got_error *show_ref_view(struct tog_view *);
612 static const struct got_error *input_ref_view(struct tog_view **,
613 struct tog_view *, int);
614 static const struct got_error *close_ref_view(struct tog_view *);
615 static const struct got_error *search_start_ref_view(struct tog_view *);
616 static const struct got_error *search_next_ref_view(struct tog_view *);
618 static volatile sig_atomic_t tog_sigwinch_received;
619 static volatile sig_atomic_t tog_sigpipe_received;
620 static volatile sig_atomic_t tog_sigcont_received;
621 static volatile sig_atomic_t tog_sigint_received;
622 static volatile sig_atomic_t tog_sigterm_received;
625 tog_sigwinch(int signo)
627 tog_sigwinch_received = 1;
631 tog_sigpipe(int signo)
633 tog_sigpipe_received = 1;
637 tog_sigcont(int signo)
639 tog_sigcont_received = 1;
643 tog_sigint(int signo)
645 tog_sigint_received = 1;
649 tog_sigterm(int signo)
651 tog_sigterm_received = 1;
655 tog_fatal_signal_received(void)
657 return (tog_sigpipe_received ||
658 tog_sigint_received || tog_sigint_received);
661 static const struct got_error *
662 view_close(struct tog_view *view)
664 const struct got_error *err = NULL, *child_err = NULL;
667 child_err = view_close(view->child);
671 err = view->close(view);
673 del_panel(view->panel);
675 delwin(view->window);
677 return err ? err : child_err;
680 static struct tog_view *
681 view_open(int nlines, int ncols, int begin_y, int begin_x,
682 enum tog_view_type type)
684 struct tog_view *view = calloc(1, sizeof(*view));
692 view->nlines = nlines ? nlines : LINES - begin_y;
693 view->ncols = ncols ? ncols : COLS - begin_x;
694 view->begin_y = begin_y;
695 view->begin_x = begin_x;
696 view->window = newwin(nlines, ncols, begin_y, begin_x);
697 if (view->window == NULL) {
701 view->panel = new_panel(view->window);
702 if (view->panel == NULL ||
703 set_panel_userptr(view->panel, view) != OK) {
708 keypad(view->window, TRUE);
713 view_split_begin_x(int begin_x)
715 if (begin_x > 0 || COLS < 120)
717 return (COLS - MAX(COLS / 2, 80));
720 /* XXX Stub till we decide what to do. */
722 view_split_begin_y(int lines)
724 return lines * HSPLIT_SCALE;
727 static const struct got_error *view_resize(struct tog_view *);
729 static const struct got_error *
730 view_splitscreen(struct tog_view *view)
732 const struct got_error *err = NULL;
734 if (!view->resize && view->mode == TOG_VIEW_SPLIT_HRZN) {
735 if (view->resized_y && view->resized_y < view->lines)
736 view->begin_y = view->resized_y;
738 view->begin_y = view_split_begin_y(view->nlines);
740 } else if (!view->resize) {
741 if (view->resized_x && view->resized_x < view->cols - 1 &&
743 view->begin_x = view->resized_x;
745 view->begin_x = view_split_begin_x(0);
748 view->nlines = LINES - view->begin_y;
749 view->ncols = COLS - view->begin_x;
752 err = view_resize(view);
756 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
757 view->parent->nlines = view->begin_y;
759 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
760 return got_error_from_errno("mvwin");
765 static const struct got_error *
766 view_fullscreen(struct tog_view *view)
768 const struct got_error *err = NULL;
771 view->begin_y = view->resize ? view->begin_y : 0;
772 view->nlines = view->resize ? view->nlines : LINES;
776 err = view_resize(view);
780 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
781 return got_error_from_errno("mvwin");
787 view_is_parent_view(struct tog_view *view)
789 return view->parent == NULL;
793 view_is_splitscreen(struct tog_view *view)
795 return view->begin_x > 0 || view->begin_y > 0;
799 view_is_fullscreen(struct tog_view *view)
801 return view->nlines == LINES && view->ncols == COLS;
805 view_is_hsplit_top(struct tog_view *view)
807 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
808 view_is_splitscreen(view->child);
812 view_border(struct tog_view *view)
815 const struct tog_view *view_above;
818 return view_border(view->parent);
820 panel = panel_above(view->panel);
824 view_above = panel_userptr(panel);
825 if (view->mode == TOG_VIEW_SPLIT_HRZN)
826 mvwhline(view->window, view_above->begin_y - 1,
827 view->begin_x, got_locale_is_utf8() ?
828 ACS_HLINE : '-', view->ncols);
830 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
831 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
834 static const struct got_error *view_init_hsplit(struct tog_view *, int);
835 static const struct got_error *request_log_commits(struct tog_view *);
836 static const struct got_error *offset_selection_down(struct tog_view *);
837 static void offset_selection_up(struct tog_view *);
838 static void view_get_split(struct tog_view *, int *, int *);
840 static const struct got_error *
841 view_resize(struct tog_view *view)
843 const struct got_error *err = NULL;
844 int dif, nlines, ncols;
846 dif = LINES - view->lines; /* line difference */
848 if (view->lines > LINES)
849 nlines = view->nlines - (view->lines - LINES);
851 nlines = view->nlines + (LINES - view->lines);
852 if (view->cols > COLS)
853 ncols = view->ncols - (view->cols - COLS);
855 ncols = view->ncols + (COLS - view->cols);
858 int hs = view->child->begin_y;
860 if (!view_is_fullscreen(view))
861 view->child->begin_x = view_split_begin_x(view->begin_x);
862 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
863 view->child->begin_x == 0) {
866 view_fullscreen(view->child);
867 if (view->child->focussed)
868 show_panel(view->child->panel);
870 show_panel(view->panel);
872 ncols = view->child->begin_x;
874 view_splitscreen(view->child);
875 show_panel(view->child->panel);
878 * Request commits if terminal height was increased in a log
879 * view so we have enough commits loaded to populate the view.
881 if (view->type == TOG_VIEW_LOG && dif > 0) {
882 struct tog_log_view_state *ts = &view->state.log;
884 if (ts->commits.ncommits < ts->selected_entry->idx +
885 view->lines - ts->selected) {
886 view->nscrolled = ts->selected_entry->idx +
887 view->lines - ts->selected -
888 ts->commits.ncommits + dif;
889 err = request_log_commits(view);
896 * XXX This is ugly and needs to be moved into the above
897 * logic but "works" for now and my attempts at moving it
898 * break either 'tab' or 'F' key maps in horizontal splits.
901 err = view_splitscreen(view->child);
904 if (dif < 0) { /* top split decreased */
905 err = offset_selection_down(view);
912 show_panel(view->child->panel);
913 nlines = view->nlines;
915 } else if (view->parent == NULL)
918 if (wresize(view->window, nlines, ncols) == ERR)
919 return got_error_from_errno("wresize");
920 if (replace_panel(view->panel, view->window) == ERR)
921 return got_error_from_errno("replace_panel");
922 wclear(view->window);
924 view->nlines = nlines;
933 view_adjust_offset(struct tog_view *view, int n)
938 if (view->parent && view->parent->offset) {
939 if (view->parent->offset + n >= 0)
940 view->parent->offset += n;
942 view->parent->offset = 0;
943 } else if (view->offset) {
944 if (view->offset - n >= 0)
951 static const struct got_error *
952 view_resize_split(struct tog_view *view, int resize)
954 const struct got_error *err = NULL;
955 struct tog_view *v = NULL;
962 if (!v->child || !view_is_splitscreen(v->child))
965 v->resize = v->child->resize = resize; /* lock for resize event */
967 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
968 if (v->child->resized_y)
969 v->child->begin_y = v->child->resized_y;
971 v->child->begin_y -= resize;
973 v->child->begin_y += resize;
974 if (v->child->begin_y < 3) {
976 v->child->begin_y = 3;
977 } else if (v->child->begin_y > LINES - 1) {
979 v->child->begin_y = LINES - 1;
982 v->child->ncols = COLS;
983 view_adjust_offset(view, resize);
984 err = view_init_hsplit(v, v->child->begin_y);
987 v->child->resized_y = v->child->begin_y;
989 if (v->child->resized_x)
990 v->child->begin_x = v->child->resized_x;
992 v->child->begin_x -= resize;
994 v->child->begin_x += resize;
995 if (v->child->begin_x < 11) {
997 v->child->begin_x = 11;
998 } else if (v->child->begin_x > COLS - 1) {
1000 v->child->begin_x = COLS - 1;
1002 v->child->resized_x = v->child->begin_x;
1005 v->child->mode = v->mode;
1006 v->child->nlines = v->lines - v->child->begin_y;
1007 v->child->ncols = v->cols - v->child->begin_x;
1010 err = view_fullscreen(v);
1013 err = view_splitscreen(v->child);
1017 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1018 err = offset_selection_down(v->child);
1023 if (v->type == TOG_VIEW_LOG)
1024 err = request_log_commits(v);
1025 else if (v->child->type == TOG_VIEW_LOG)
1026 err = request_log_commits(v->child);
1028 v->resize = v->child->resize = 0;
1034 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1036 struct tog_view *v = src->child ? src->child : src;
1038 dst->resized_x = v->resized_x;
1039 dst->resized_y = v->resized_y;
1042 static const struct got_error *
1043 view_close_child(struct tog_view *view)
1045 const struct got_error *err = NULL;
1047 if (view->child == NULL)
1050 err = view_close(view->child);
1055 static const struct got_error *
1056 view_set_child(struct tog_view *view, struct tog_view *child)
1058 const struct got_error *err = NULL;
1060 view->child = child;
1061 child->parent = view;
1063 err = view_resize(view);
1067 if (view->child->resized_x || view->child->resized_y)
1068 err = view_resize_split(view, 0);
1074 tog_resizeterm(void)
1077 struct winsize size;
1079 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1080 cols = 80; /* Default */
1084 lines = size.ws_row;
1086 resize_term(lines, cols);
1089 static const struct got_error *
1090 view_search_start(struct tog_view *view)
1092 const struct got_error *err = NULL;
1093 struct tog_view *v = view;
1097 if (view->search_started) {
1098 regfree(&view->regex);
1099 view->searching = 0;
1100 memset(&view->regmatch, 0, sizeof(view->regmatch));
1102 view->search_started = 0;
1104 if (view->nlines < 1)
1107 if (view_is_hsplit_top(view))
1110 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1111 wclrtoeol(v->window);
1113 nodelay(view->window, FALSE); /* block for search term input */
1116 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1117 wrefresh(v->window);
1120 nodelay(view->window, TRUE);
1124 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1125 err = view->search_start(view);
1127 regfree(&view->regex);
1130 view->search_started = 1;
1131 view->searching = TOG_SEARCH_FORWARD;
1132 view->search_next_done = 0;
1133 view->search_next(view);
1139 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1140 static const struct got_error *
1141 switch_split(struct tog_view *view)
1143 const struct got_error *err = NULL;
1144 struct tog_view *v = NULL;
1151 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1152 v->mode = TOG_VIEW_SPLIT_VERT;
1154 v->mode = TOG_VIEW_SPLIT_HRZN;
1158 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1159 v->mode = TOG_VIEW_SPLIT_NONE;
1161 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1162 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1163 v->child->begin_y = v->child->resized_y;
1164 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1165 v->child->begin_x = v->child->resized_x;
1168 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1170 v->child->ncols = COLS;
1171 v->child->nscrolled = LINES - v->child->nlines;
1173 err = view_init_hsplit(v, v->child->begin_y);
1177 v->child->mode = v->mode;
1178 v->child->nlines = v->lines - v->child->begin_y;
1181 err = view_fullscreen(v);
1184 err = view_splitscreen(v->child);
1188 if (v->mode == TOG_VIEW_SPLIT_NONE)
1189 v->mode = TOG_VIEW_SPLIT_VERT;
1190 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1191 err = offset_selection_down(v);
1192 err = offset_selection_down(v->child);
1194 offset_selection_up(v);
1195 offset_selection_up(v->child);
1197 if (v->type == TOG_VIEW_LOG)
1198 err = request_log_commits(v);
1199 else if (v->child->type == TOG_VIEW_LOG)
1200 err = request_log_commits(v->child);
1206 * Compute view->count from numeric input. Assign total to view->count and
1207 * return first non-numeric key entered.
1210 get_compound_key(struct tog_view *view, int c)
1212 struct tog_view *v = view;
1215 if (view_is_hsplit_top(view))
1217 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1221 cbreak(); /* block for input */
1222 wmove(v->window, v->nlines - 1, 0);
1223 wclrtoeol(v->window);
1224 waddch(v->window, ':');
1227 x = getcurx(v->window);
1228 if (x != ERR && x < view->ncols) {
1229 waddch(v->window, c);
1230 wrefresh(v->window);
1234 * Don't overflow. Max valid request should be the greatest
1235 * between the longest and total lines; cap at 10 million.
1240 n = n * 10 + (c - '0');
1241 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1243 /* Massage excessive or inapplicable values at the input handler. */
1249 static const struct got_error *
1250 view_input(struct tog_view **new, int *done, struct tog_view *view,
1251 struct tog_view_list_head *views)
1253 const struct got_error *err = NULL;
1259 /* Clear "no matches" indicator. */
1260 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1261 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1262 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1266 if (view->searching && !view->search_next_done) {
1267 errcode = pthread_mutex_unlock(&tog_mutex);
1269 return got_error_set_errno(errcode,
1270 "pthread_mutex_unlock");
1272 errcode = pthread_mutex_lock(&tog_mutex);
1274 return got_error_set_errno(errcode,
1275 "pthread_mutex_lock");
1276 view->search_next(view);
1280 nodelay(view->window, FALSE);
1281 /* Allow threads to make progress while we are waiting for input. */
1282 errcode = pthread_mutex_unlock(&tog_mutex);
1284 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1285 /* If we have an unfinished count, let C-g or backspace abort. */
1286 if (view->count && --view->count) {
1288 nodelay(view->window, TRUE);
1289 ch = wgetch(view->window);
1290 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1295 ch = wgetch(view->window);
1296 if (ch >= '1' && ch <= '9')
1297 view->ch = ch = get_compound_key(view, ch);
1299 errcode = pthread_mutex_lock(&tog_mutex);
1301 return got_error_set_errno(errcode, "pthread_mutex_lock");
1302 nodelay(view->window, TRUE);
1304 if (tog_sigwinch_received || tog_sigcont_received) {
1306 tog_sigwinch_received = 0;
1307 tog_sigcont_received = 0;
1308 TAILQ_FOREACH(v, views, entry) {
1309 err = view_resize(v);
1312 err = v->input(new, v, KEY_RESIZE);
1316 err = view_resize(v->child);
1319 err = v->child->input(new, v->child,
1323 if (v->child->resized_x || v->child->resized_y) {
1324 err = view_resize_split(v, 0);
1337 view->child->focussed = 1;
1338 view->focus_child = 1;
1339 } else if (view->parent) {
1341 view->parent->focussed = 1;
1342 view->parent->focus_child = 0;
1343 if (!view_is_splitscreen(view)) {
1344 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1345 view->parent->type == TOG_VIEW_LOG) {
1346 err = request_log_commits(view->parent);
1350 offset_selection_up(view->parent);
1351 err = view_fullscreen(view->parent);
1358 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1359 if (view->parent->type == TOG_VIEW_LOG) {
1360 /* might need more commits to fill fullscreen */
1361 err = request_log_commits(view->parent);
1365 offset_selection_up(view->parent);
1367 err = view->input(new, view, ch);
1375 if (view_is_parent_view(view)) {
1376 if (view->child == NULL)
1378 if (view_is_splitscreen(view->child)) {
1380 view->child->focussed = 1;
1381 err = view_fullscreen(view->child);
1383 err = view_splitscreen(view->child);
1385 err = view_resize_split(view, 0);
1389 err = view->child->input(new, view->child,
1392 if (view_is_splitscreen(view)) {
1393 view->parent->focussed = 0;
1395 err = view_fullscreen(view);
1397 err = view_splitscreen(view);
1398 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1399 err = view_resize(view->parent);
1401 err = view_resize_split(view, 0);
1405 err = view->input(new, view, KEY_RESIZE);
1409 if (view->type == TOG_VIEW_LOG) {
1410 err = request_log_commits(view);
1415 err = offset_selection_down(view->parent);
1417 err = offset_selection_down(view);
1421 err = switch_split(view);
1424 err = view_resize_split(view, -1);
1427 err = view_resize_split(view, 1);
1433 if (view->search_start)
1434 view_search_start(view);
1436 err = view->input(new, view, ch);
1440 if (view->search_started && view->search_next) {
1441 view->searching = (ch == 'n' ?
1442 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1443 view->search_next_done = 0;
1444 view->search_next(view);
1446 err = view->input(new, view, ch);
1449 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1450 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1452 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1453 TAILQ_FOREACH(v, views, entry) {
1459 if (v->child && v->child->reset) {
1460 err = v->child->reset(v->child);
1467 err = view->input(new, view, ch);
1475 view_needs_focus_indication(struct tog_view *view)
1477 if (view_is_parent_view(view)) {
1478 if (view->child == NULL || view->child->focussed)
1480 if (!view_is_splitscreen(view->child))
1482 } else if (!view_is_splitscreen(view))
1485 return view->focussed;
1488 static const struct got_error *
1489 view_loop(struct tog_view *view)
1491 const struct got_error *err = NULL;
1492 struct tog_view_list_head views;
1493 struct tog_view *new_view;
1495 int fast_refresh = 10;
1496 int done = 0, errcode;
1498 mode = getenv("TOG_VIEW_SPLIT_MODE");
1499 if (!mode || !(*mode == 'h' || *mode == 'H'))
1500 view->mode = TOG_VIEW_SPLIT_VERT;
1502 view->mode = TOG_VIEW_SPLIT_HRZN;
1504 errcode = pthread_mutex_lock(&tog_mutex);
1506 return got_error_set_errno(errcode, "pthread_mutex_lock");
1509 TAILQ_INSERT_HEAD(&views, view, entry);
1512 err = view->show(view);
1517 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1518 !tog_fatal_signal_received()) {
1519 /* Refresh fast during initialization, then become slower. */
1520 if (fast_refresh && fast_refresh-- == 0)
1521 halfdelay(10); /* switch to once per second */
1523 err = view_input(&new_view, &done, view, &views);
1527 struct tog_view *v, *prev = NULL;
1529 if (view_is_parent_view(view))
1530 prev = TAILQ_PREV(view, tog_view_list_head,
1532 else if (view->parent)
1533 prev = view->parent;
1536 view->parent->child = NULL;
1537 view->parent->focus_child = 0;
1538 /* Restore fullscreen line height. */
1539 view->parent->nlines = view->parent->lines;
1540 err = view_resize(view->parent);
1543 /* Make resized splits persist. */
1544 view_transfer_size(view->parent, view);
1546 TAILQ_REMOVE(&views, view, entry);
1548 err = view_close(view);
1553 TAILQ_FOREACH(v, &views, entry) {
1557 if (view == NULL && new_view == NULL) {
1558 /* No view has focus. Try to pick one. */
1561 else if (!TAILQ_EMPTY(&views)) {
1562 view = TAILQ_LAST(&views,
1563 tog_view_list_head);
1566 if (view->focus_child) {
1567 view->child->focussed = 1;
1575 struct tog_view *v, *t;
1576 /* Only allow one parent view per type. */
1577 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1578 if (v->type != new_view->type)
1580 TAILQ_REMOVE(&views, v, entry);
1581 err = view_close(v);
1586 TAILQ_INSERT_TAIL(&views, new_view, entry);
1590 if (view_is_parent_view(view)) {
1591 if (view->child && view->child->focussed)
1594 if (view->parent && view->parent->focussed)
1595 view = view->parent;
1597 show_panel(view->panel);
1598 if (view->child && view_is_splitscreen(view->child))
1599 show_panel(view->child->panel);
1600 if (view->parent && view_is_splitscreen(view)) {
1601 err = view->parent->show(view->parent);
1605 err = view->show(view);
1609 err = view->child->show(view->child);
1618 while (!TAILQ_EMPTY(&views)) {
1619 const struct got_error *close_err;
1620 view = TAILQ_FIRST(&views);
1621 TAILQ_REMOVE(&views, view, entry);
1622 close_err = view_close(view);
1623 if (close_err && err == NULL)
1627 errcode = pthread_mutex_unlock(&tog_mutex);
1628 if (errcode && err == NULL)
1629 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1639 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1644 /* Create newly allocated wide-character string equivalent to a byte string. */
1645 static const struct got_error *
1646 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1649 const struct got_error *err = NULL;
1652 *wlen = mbstowcs(NULL, s, 0);
1653 if (*wlen == (size_t)-1) {
1655 if (errno != EILSEQ)
1656 return got_error_from_errno("mbstowcs");
1658 /* byte string invalid in current encoding; try to "fix" it */
1659 err = got_mbsavis(&vis, &vislen, s);
1662 *wlen = mbstowcs(NULL, vis, 0);
1663 if (*wlen == (size_t)-1) {
1664 err = got_error_from_errno("mbstowcs"); /* give up */
1669 *ws = calloc(*wlen + 1, sizeof(**ws));
1671 err = got_error_from_errno("calloc");
1675 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1676 err = got_error_from_errno("mbstowcs");
1687 static const struct got_error *
1688 expand_tab(char **ptr, const char *src)
1691 size_t len, n, idx = 0, sz = 0;
1694 n = len = strlen(src);
1695 dst = malloc(n + 1);
1697 return got_error_from_errno("malloc");
1699 while (idx < len && src[idx]) {
1700 const char c = src[idx];
1703 size_t nb = TABSIZE - sz % TABSIZE;
1706 p = realloc(dst, n + nb);
1709 return got_error_from_errno("realloc");
1714 memset(dst + sz, ' ', nb);
1717 dst[sz++] = src[idx];
1727 * Advance at most n columns from wline starting at offset off.
1728 * Return the index to the first character after the span operation.
1729 * Return the combined column width of all spanned wide character in
1733 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1735 int width, i, cols = 0;
1742 for (i = off; wline[i] != L'\0'; ++i) {
1743 if (wline[i] == L'\t')
1744 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1746 width = wcwidth(wline[i]);
1753 if (cols + width > n)
1763 * Format a line for display, ensuring that it won't overflow a width limit.
1764 * With scrolling, the width returned refers to the scrolled version of the
1765 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1767 static const struct got_error *
1768 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1769 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1771 const struct got_error *err = NULL;
1773 wchar_t *wline = NULL;
1782 err = expand_tab(&exstr, line);
1787 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1792 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1794 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1795 wline[wlen - 1] = L'\0';
1798 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1799 wline[wlen - 1] = L'\0';
1803 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1809 *scrollxp = scrollx;
1817 static const struct got_error*
1818 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1819 struct got_object_id *id, struct got_repository *repo)
1821 static const struct got_error *err = NULL;
1822 struct got_reflist_entry *re;
1828 TAILQ_FOREACH(re, refs, entry) {
1829 struct got_tag_object *tag = NULL;
1830 struct got_object_id *ref_id;
1833 name = got_ref_get_name(re->ref);
1834 if (strcmp(name, GOT_REF_HEAD) == 0)
1836 if (strncmp(name, "refs/", 5) == 0)
1838 if (strncmp(name, "got/", 4) == 0 &&
1839 strncmp(name, "got/backup/", 11) != 0)
1841 if (strncmp(name, "heads/", 6) == 0)
1843 if (strncmp(name, "remotes/", 8) == 0) {
1845 s = strstr(name, "/" GOT_REF_HEAD);
1846 if (s != NULL && s[strlen(s)] == '\0')
1849 err = got_ref_resolve(&ref_id, repo, re->ref);
1852 if (strncmp(name, "tags/", 5) == 0) {
1853 err = got_object_open_as_tag(&tag, repo, ref_id);
1855 if (err->code != GOT_ERR_OBJ_TYPE) {
1859 /* Ref points at something other than a tag. */
1864 cmp = got_object_id_cmp(tag ?
1865 got_object_tag_get_object_id(tag) : ref_id, id);
1868 got_object_tag_close(tag);
1872 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1873 s ? ", " : "", name) == -1) {
1874 err = got_error_from_errno("asprintf");
1885 static const struct got_error *
1886 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1891 smallerthan = strchr(author, '<');
1892 if (smallerthan && smallerthan[1] != '\0')
1893 author = smallerthan + 1;
1894 author[strcspn(author, "@>")] = '\0';
1895 return format_line(wauthor, author_width, NULL, author, 0, limit,
1899 static const struct got_error *
1900 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1901 struct got_object_id *id, const size_t date_display_cols,
1902 int author_display_cols)
1904 struct tog_log_view_state *s = &view->state.log;
1905 const struct got_error *err = NULL;
1906 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1907 char *logmsg0 = NULL, *logmsg = NULL;
1908 char *author = NULL;
1909 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1910 int author_width, logmsg_width;
1911 char *newline, *line = NULL;
1912 int col, limit, scrollx;
1913 const int avail = view->ncols;
1915 time_t committer_time;
1916 struct tog_color *tc;
1918 committer_time = got_object_commit_get_committer_time(commit);
1919 if (gmtime_r(&committer_time, &tm) == NULL)
1920 return got_error_from_errno("gmtime_r");
1921 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1922 return got_error(GOT_ERR_NO_SPACE);
1924 if (avail <= date_display_cols)
1925 limit = MIN(sizeof(datebuf) - 1, avail);
1927 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1928 tc = get_color(&s->colors, TOG_COLOR_DATE);
1930 wattr_on(view->window,
1931 COLOR_PAIR(tc->colorpair), NULL);
1932 waddnstr(view->window, datebuf, limit);
1934 wattr_off(view->window,
1935 COLOR_PAIR(tc->colorpair), NULL);
1942 err = got_object_id_str(&id_str, id);
1945 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1947 wattr_on(view->window,
1948 COLOR_PAIR(tc->colorpair), NULL);
1949 wprintw(view->window, "%.8s ", id_str);
1951 wattr_off(view->window,
1952 COLOR_PAIR(tc->colorpair), NULL);
1959 author = strdup(got_object_commit_get_author(commit));
1960 if (author == NULL) {
1961 err = got_error_from_errno("strdup");
1964 err = format_author(&wauthor, &author_width, author, avail - col, col);
1967 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1969 wattr_on(view->window,
1970 COLOR_PAIR(tc->colorpair), NULL);
1971 waddwstr(view->window, wauthor);
1973 wattr_off(view->window,
1974 COLOR_PAIR(tc->colorpair), NULL);
1975 col += author_width;
1976 while (col < avail && author_width < author_display_cols + 2) {
1977 waddch(view->window, ' ');
1984 err = got_object_commit_get_logmsg(&logmsg0, commit);
1988 while (*logmsg == '\n')
1990 newline = strchr(logmsg, '\n');
1993 limit = avail - col;
1994 if (view->child && !view_is_hsplit_top(view) && limit > 0)
1995 limit--; /* for the border */
1996 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2000 waddwstr(view->window, &wlogmsg[scrollx]);
2001 col += MAX(logmsg_width, 0);
2002 while (col < avail) {
2003 waddch(view->window, ' ');
2015 static struct commit_queue_entry *
2016 alloc_commit_queue_entry(struct got_commit_object *commit,
2017 struct got_object_id *id)
2019 struct commit_queue_entry *entry;
2021 entry = calloc(1, sizeof(*entry));
2026 entry->commit = commit;
2031 pop_commit(struct commit_queue *commits)
2033 struct commit_queue_entry *entry;
2035 entry = TAILQ_FIRST(&commits->head);
2036 TAILQ_REMOVE(&commits->head, entry, entry);
2037 got_object_commit_close(entry->commit);
2038 commits->ncommits--;
2039 /* Don't free entry->id! It is owned by the commit graph. */
2044 free_commits(struct commit_queue *commits)
2046 while (!TAILQ_EMPTY(&commits->head))
2047 pop_commit(commits);
2050 static const struct got_error *
2051 match_commit(int *have_match, struct got_object_id *id,
2052 struct got_commit_object *commit, regex_t *regex)
2054 const struct got_error *err = NULL;
2055 regmatch_t regmatch;
2056 char *id_str = NULL, *logmsg = NULL;
2060 err = got_object_id_str(&id_str, id);
2064 err = got_object_commit_get_logmsg(&logmsg, commit);
2068 if (regexec(regex, got_object_commit_get_author(commit), 1,
2069 ®match, 0) == 0 ||
2070 regexec(regex, got_object_commit_get_committer(commit), 1,
2071 ®match, 0) == 0 ||
2072 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2073 regexec(regex, logmsg, 1, ®match, 0) == 0)
2081 static const struct got_error *
2082 queue_commits(struct tog_log_thread_args *a)
2084 const struct got_error *err = NULL;
2087 * We keep all commits open throughout the lifetime of the log
2088 * view in order to avoid having to re-fetch commits from disk
2089 * while updating the display.
2092 struct got_object_id *id;
2093 struct got_commit_object *commit;
2094 struct commit_queue_entry *entry;
2097 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2099 if (err || id == NULL)
2102 err = got_object_open_as_commit(&commit, a->repo, id);
2105 entry = alloc_commit_queue_entry(commit, id);
2106 if (entry == NULL) {
2107 err = got_error_from_errno("alloc_commit_queue_entry");
2111 errcode = pthread_mutex_lock(&tog_mutex);
2113 err = got_error_set_errno(errcode,
2114 "pthread_mutex_lock");
2118 entry->idx = a->commits->ncommits;
2119 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2120 a->commits->ncommits++;
2122 if (*a->searching == TOG_SEARCH_FORWARD &&
2123 !*a->search_next_done) {
2125 err = match_commit(&have_match, id, commit, a->regex);
2129 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2132 errcode = pthread_mutex_unlock(&tog_mutex);
2133 if (errcode && err == NULL)
2134 err = got_error_set_errno(errcode,
2135 "pthread_mutex_unlock");
2138 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2144 select_commit(struct tog_log_view_state *s)
2146 struct commit_queue_entry *entry;
2149 entry = s->first_displayed_entry;
2151 if (ncommits == s->selected) {
2152 s->selected_entry = entry;
2155 entry = TAILQ_NEXT(entry, entry);
2160 static const struct got_error *
2161 draw_commits(struct tog_view *view)
2163 const struct got_error *err = NULL;
2164 struct tog_log_view_state *s = &view->state.log;
2165 struct commit_queue_entry *entry = s->selected_entry;
2166 const int limit = view->nlines;
2168 int ncommits, author_cols = 4;
2169 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2170 char *refs_str = NULL;
2172 struct tog_color *tc;
2173 static const size_t date_display_cols = 12;
2175 if (s->selected_entry &&
2176 !(view->searching && view->search_next_done == 0)) {
2177 struct got_reflist_head *refs;
2178 err = got_object_id_str(&id_str, s->selected_entry->id);
2181 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2182 s->selected_entry->id);
2184 err = build_refs_str(&refs_str, refs,
2185 s->selected_entry->id, s->repo);
2191 if (s->thread_args.commits_needed == 0)
2192 halfdelay(10); /* disable fast refresh */
2194 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2195 if (asprintf(&ncommits_str, " [%d/%d] %s",
2196 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2197 (view->searching && !view->search_next_done) ?
2198 "searching..." : "loading...") == -1) {
2199 err = got_error_from_errno("asprintf");
2203 const char *search_str = NULL;
2205 if (view->searching) {
2206 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2207 search_str = "no more matches";
2208 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2209 search_str = "no matches found";
2210 else if (!view->search_next_done)
2211 search_str = "searching...";
2214 if (asprintf(&ncommits_str, " [%d/%d] %s",
2215 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2216 search_str ? search_str :
2217 (refs_str ? refs_str : "")) == -1) {
2218 err = got_error_from_errno("asprintf");
2223 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2224 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2225 "........................................",
2226 s->in_repo_path, ncommits_str) == -1) {
2227 err = got_error_from_errno("asprintf");
2231 } else if (asprintf(&header, "commit %s%s",
2232 id_str ? id_str : "........................................",
2233 ncommits_str) == -1) {
2234 err = got_error_from_errno("asprintf");
2238 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2242 werase(view->window);
2244 if (view_needs_focus_indication(view))
2245 wstandout(view->window);
2246 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2248 wattr_on(view->window,
2249 COLOR_PAIR(tc->colorpair), NULL);
2250 waddwstr(view->window, wline);
2252 wattr_off(view->window,
2253 COLOR_PAIR(tc->colorpair), NULL);
2254 while (width < view->ncols) {
2255 waddch(view->window, ' ');
2258 if (view_needs_focus_indication(view))
2259 wstandend(view->window);
2264 /* Grow author column size if necessary, and set view->maxx. */
2265 entry = s->first_displayed_entry;
2269 char *author, *eol, *msg, *msg0;
2270 wchar_t *wauthor, *wmsg;
2272 if (ncommits >= limit - 1)
2274 author = strdup(got_object_commit_get_author(entry->commit));
2275 if (author == NULL) {
2276 err = got_error_from_errno("strdup");
2279 err = format_author(&wauthor, &width, author, COLS,
2281 if (author_cols < width)
2282 author_cols = width;
2285 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2289 while (*msg == '\n')
2291 if ((eol = strchr(msg, '\n')))
2293 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2294 date_display_cols + author_cols, 0);
2297 view->maxx = MAX(view->maxx, width);
2301 entry = TAILQ_NEXT(entry, entry);
2304 entry = s->first_displayed_entry;
2305 s->last_displayed_entry = s->first_displayed_entry;
2308 if (ncommits >= limit - 1)
2310 if (ncommits == s->selected)
2311 wstandout(view->window);
2312 err = draw_commit(view, entry->commit, entry->id,
2313 date_display_cols, author_cols);
2314 if (ncommits == s->selected)
2315 wstandend(view->window);
2319 s->last_displayed_entry = entry;
2320 entry = TAILQ_NEXT(entry, entry);
2333 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2335 struct commit_queue_entry *entry;
2338 entry = TAILQ_FIRST(&s->commits.head);
2339 if (s->first_displayed_entry == entry)
2342 entry = s->first_displayed_entry;
2343 while (entry && nscrolled < maxscroll) {
2344 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2346 s->first_displayed_entry = entry;
2352 static const struct got_error *
2353 trigger_log_thread(struct tog_view *view, int wait)
2355 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2358 halfdelay(1); /* fast refresh while loading commits */
2360 while (!ta->log_complete && !tog_thread_error &&
2361 (ta->commits_needed > 0 || ta->load_all)) {
2362 /* Wake the log thread. */
2363 errcode = pthread_cond_signal(&ta->need_commits);
2365 return got_error_set_errno(errcode,
2366 "pthread_cond_signal");
2369 * The mutex will be released while the view loop waits
2370 * in wgetch(), at which time the log thread will run.
2375 /* Display progress update in log view. */
2376 show_log_view(view);
2380 /* Wait right here while next commit is being loaded. */
2381 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2383 return got_error_set_errno(errcode,
2384 "pthread_cond_wait");
2386 /* Display progress update in log view. */
2387 show_log_view(view);
2395 static const struct got_error *
2396 request_log_commits(struct tog_view *view)
2398 struct tog_log_view_state *state = &view->state.log;
2399 const struct got_error *err = NULL;
2401 state->thread_args.commits_needed = view->nscrolled;
2402 err = trigger_log_thread(view, 1);
2403 view->nscrolled = 0;
2408 static const struct got_error *
2409 log_scroll_down(struct tog_view *view, int maxscroll)
2411 struct tog_log_view_state *s = &view->state.log;
2412 const struct got_error *err = NULL;
2413 struct commit_queue_entry *pentry;
2414 int nscrolled = 0, ncommits_needed;
2416 if (s->last_displayed_entry == NULL)
2419 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2420 if (s->commits.ncommits < ncommits_needed &&
2421 !s->thread_args.log_complete) {
2423 * Ask the log thread for required amount of commits.
2425 s->thread_args.commits_needed += maxscroll;
2426 err = trigger_log_thread(view, 1);
2432 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2433 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2436 s->last_displayed_entry = pentry ?
2437 pentry : s->last_displayed_entry;;
2439 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2442 s->first_displayed_entry = pentry;
2443 } while (++nscrolled < maxscroll);
2445 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2446 view->nscrolled += nscrolled;
2448 view->nscrolled = 0;
2453 static const struct got_error *
2454 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2455 struct got_commit_object *commit, struct got_object_id *commit_id,
2456 struct tog_view *log_view, struct got_repository *repo)
2458 const struct got_error *err;
2459 struct got_object_qid *parent_id;
2460 struct tog_view *diff_view;
2462 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2463 if (diff_view == NULL)
2464 return got_error_from_errno("view_open");
2466 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2467 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2468 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2470 *new_view = diff_view;
2474 static const struct got_error *
2475 tree_view_visit_subtree(struct tog_tree_view_state *s,
2476 struct got_tree_object *subtree)
2478 struct tog_parent_tree *parent;
2480 parent = calloc(1, sizeof(*parent));
2482 return got_error_from_errno("calloc");
2484 parent->tree = s->tree;
2485 parent->first_displayed_entry = s->first_displayed_entry;
2486 parent->selected_entry = s->selected_entry;
2487 parent->selected = s->selected;
2488 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2491 s->first_displayed_entry = NULL;
2495 static const struct got_error *
2496 tree_view_walk_path(struct tog_tree_view_state *s,
2497 struct got_commit_object *commit, const char *path)
2499 const struct got_error *err = NULL;
2500 struct got_tree_object *tree = NULL;
2502 char *slash, *subpath = NULL;
2504 /* Walk the path and open corresponding tree objects. */
2507 struct got_tree_entry *te;
2508 struct got_object_id *tree_id;
2514 /* Ensure the correct subtree entry is selected. */
2515 slash = strchr(p, '/');
2517 te_name = strdup(p);
2519 te_name = strndup(p, slash - p);
2520 if (te_name == NULL) {
2521 err = got_error_from_errno("strndup");
2524 te = got_object_tree_find_entry(s->tree, te_name);
2526 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2531 s->first_displayed_entry = s->selected_entry = te;
2533 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2534 break; /* jump to this file's entry */
2536 slash = strchr(p, '/');
2538 subpath = strndup(path, slash - path);
2540 subpath = strdup(path);
2541 if (subpath == NULL) {
2542 err = got_error_from_errno("strdup");
2546 err = got_object_id_by_path(&tree_id, s->repo, commit,
2551 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2556 err = tree_view_visit_subtree(s, tree);
2558 got_object_tree_close(tree);
2572 static const struct got_error *
2573 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2574 struct commit_queue_entry *entry, const char *path,
2575 const char *head_ref_name, struct got_repository *repo)
2577 const struct got_error *err = NULL;
2578 struct tog_tree_view_state *s;
2579 struct tog_view *tree_view;
2581 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2582 if (tree_view == NULL)
2583 return got_error_from_errno("view_open");
2585 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2588 s = &tree_view->state.tree;
2590 *new_view = tree_view;
2592 if (got_path_is_root_dir(path))
2595 return tree_view_walk_path(s, entry->commit, path);
2598 static const struct got_error *
2599 block_signals_used_by_main_thread(void)
2604 if (sigemptyset(&sigset) == -1)
2605 return got_error_from_errno("sigemptyset");
2607 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2608 if (sigaddset(&sigset, SIGWINCH) == -1)
2609 return got_error_from_errno("sigaddset");
2610 if (sigaddset(&sigset, SIGCONT) == -1)
2611 return got_error_from_errno("sigaddset");
2612 if (sigaddset(&sigset, SIGINT) == -1)
2613 return got_error_from_errno("sigaddset");
2614 if (sigaddset(&sigset, SIGTERM) == -1)
2615 return got_error_from_errno("sigaddset");
2617 /* ncurses handles SIGTSTP */
2618 if (sigaddset(&sigset, SIGTSTP) == -1)
2619 return got_error_from_errno("sigaddset");
2621 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2623 return got_error_set_errno(errcode, "pthread_sigmask");
2629 log_thread(void *arg)
2631 const struct got_error *err = NULL;
2633 struct tog_log_thread_args *a = arg;
2637 * Sync startup with main thread such that we begin our
2638 * work once view_input() has released the mutex.
2640 errcode = pthread_mutex_lock(&tog_mutex);
2642 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2646 err = block_signals_used_by_main_thread();
2648 pthread_mutex_unlock(&tog_mutex);
2652 while (!done && !err && !tog_fatal_signal_received()) {
2653 errcode = pthread_mutex_unlock(&tog_mutex);
2655 err = got_error_set_errno(errcode,
2656 "pthread_mutex_unlock");
2659 err = queue_commits(a);
2661 if (err->code != GOT_ERR_ITER_COMPLETED)
2665 } else if (a->commits_needed > 0 && !a->load_all)
2666 a->commits_needed--;
2668 errcode = pthread_mutex_lock(&tog_mutex);
2670 err = got_error_set_errno(errcode,
2671 "pthread_mutex_lock");
2673 } else if (*a->quit)
2675 else if (*a->first_displayed_entry == NULL) {
2676 *a->first_displayed_entry =
2677 TAILQ_FIRST(&a->commits->head);
2678 *a->selected_entry = *a->first_displayed_entry;
2681 errcode = pthread_cond_signal(&a->commit_loaded);
2683 err = got_error_set_errno(errcode,
2684 "pthread_cond_signal");
2685 pthread_mutex_unlock(&tog_mutex);
2690 a->commits_needed = 0;
2692 if (a->commits_needed == 0 && !a->load_all) {
2693 errcode = pthread_cond_wait(&a->need_commits,
2696 err = got_error_set_errno(errcode,
2697 "pthread_cond_wait");
2698 pthread_mutex_unlock(&tog_mutex);
2706 a->log_complete = 1;
2707 errcode = pthread_mutex_unlock(&tog_mutex);
2709 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2712 tog_thread_error = 1;
2713 pthread_cond_signal(&a->commit_loaded);
2718 static const struct got_error *
2719 stop_log_thread(struct tog_log_view_state *s)
2721 const struct got_error *err = NULL, *thread_err = NULL;
2726 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2728 return got_error_set_errno(errcode,
2729 "pthread_cond_signal");
2730 errcode = pthread_mutex_unlock(&tog_mutex);
2732 return got_error_set_errno(errcode,
2733 "pthread_mutex_unlock");
2734 errcode = pthread_join(s->thread, (void **)&thread_err);
2736 return got_error_set_errno(errcode, "pthread_join");
2737 errcode = pthread_mutex_lock(&tog_mutex);
2739 return got_error_set_errno(errcode,
2740 "pthread_mutex_lock");
2744 if (s->thread_args.repo) {
2745 err = got_repo_close(s->thread_args.repo);
2746 s->thread_args.repo = NULL;
2749 if (s->thread_args.pack_fds) {
2750 const struct got_error *pack_err =
2751 got_repo_pack_fds_close(s->thread_args.pack_fds);
2754 s->thread_args.pack_fds = NULL;
2757 if (s->thread_args.graph) {
2758 got_commit_graph_close(s->thread_args.graph);
2759 s->thread_args.graph = NULL;
2762 return err ? err : thread_err;
2765 static const struct got_error *
2766 close_log_view(struct tog_view *view)
2768 const struct got_error *err = NULL;
2769 struct tog_log_view_state *s = &view->state.log;
2772 err = stop_log_thread(s);
2774 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2775 if (errcode && err == NULL)
2776 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2778 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2779 if (errcode && err == NULL)
2780 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2782 free_commits(&s->commits);
2783 free(s->in_repo_path);
2784 s->in_repo_path = NULL;
2787 free(s->head_ref_name);
2788 s->head_ref_name = NULL;
2792 static const struct got_error *
2793 search_start_log_view(struct tog_view *view)
2795 struct tog_log_view_state *s = &view->state.log;
2797 s->matched_entry = NULL;
2798 s->search_entry = NULL;
2802 static const struct got_error *
2803 search_next_log_view(struct tog_view *view)
2805 const struct got_error *err = NULL;
2806 struct tog_log_view_state *s = &view->state.log;
2807 struct commit_queue_entry *entry;
2809 /* Display progress update in log view. */
2810 show_log_view(view);
2814 if (s->search_entry) {
2816 errcode = pthread_mutex_unlock(&tog_mutex);
2818 return got_error_set_errno(errcode,
2819 "pthread_mutex_unlock");
2820 ch = wgetch(view->window);
2821 errcode = pthread_mutex_lock(&tog_mutex);
2823 return got_error_set_errno(errcode,
2824 "pthread_mutex_lock");
2825 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2826 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2829 if (view->searching == TOG_SEARCH_FORWARD)
2830 entry = TAILQ_NEXT(s->search_entry, entry);
2832 entry = TAILQ_PREV(s->search_entry,
2833 commit_queue_head, entry);
2834 } else if (s->matched_entry) {
2835 int matched_idx = s->matched_entry->idx;
2836 int selected_idx = s->selected_entry->idx;
2839 * If the user has moved the cursor after we hit a match,
2840 * the position from where we should continue searching
2841 * might have changed.
2843 if (view->searching == TOG_SEARCH_FORWARD) {
2844 if (matched_idx > selected_idx)
2845 entry = TAILQ_NEXT(s->selected_entry, entry);
2847 entry = TAILQ_NEXT(s->matched_entry, entry);
2849 if (matched_idx < selected_idx)
2850 entry = TAILQ_PREV(s->selected_entry,
2851 commit_queue_head, entry);
2853 entry = TAILQ_PREV(s->matched_entry,
2854 commit_queue_head, entry);
2857 entry = s->selected_entry;
2863 if (entry == NULL) {
2864 if (s->thread_args.log_complete ||
2865 view->searching == TOG_SEARCH_BACKWARD) {
2866 view->search_next_done =
2867 (s->matched_entry == NULL ?
2868 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2869 s->search_entry = NULL;
2873 * Poke the log thread for more commits and return,
2874 * allowing the main loop to make progress. Search
2875 * will resume at s->search_entry once we come back.
2877 s->thread_args.commits_needed++;
2878 return trigger_log_thread(view, 0);
2881 err = match_commit(&have_match, entry->id, entry->commit,
2886 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2887 s->matched_entry = entry;
2891 s->search_entry = entry;
2892 if (view->searching == TOG_SEARCH_FORWARD)
2893 entry = TAILQ_NEXT(entry, entry);
2895 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2898 if (s->matched_entry) {
2899 int cur = s->selected_entry->idx;
2900 while (cur < s->matched_entry->idx) {
2901 err = input_log_view(NULL, view, KEY_DOWN);
2906 while (cur > s->matched_entry->idx) {
2907 err = input_log_view(NULL, view, KEY_UP);
2914 s->search_entry = NULL;
2919 static const struct got_error *
2920 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2921 struct got_repository *repo, const char *head_ref_name,
2922 const char *in_repo_path, int log_branches)
2924 const struct got_error *err = NULL;
2925 struct tog_log_view_state *s = &view->state.log;
2926 struct got_repository *thread_repo = NULL;
2927 struct got_commit_graph *thread_graph = NULL;
2930 if (in_repo_path != s->in_repo_path) {
2931 free(s->in_repo_path);
2932 s->in_repo_path = strdup(in_repo_path);
2933 if (s->in_repo_path == NULL)
2934 return got_error_from_errno("strdup");
2937 /* The commit queue only contains commits being displayed. */
2938 TAILQ_INIT(&s->commits.head);
2939 s->commits.ncommits = 0;
2942 if (head_ref_name) {
2943 s->head_ref_name = strdup(head_ref_name);
2944 if (s->head_ref_name == NULL) {
2945 err = got_error_from_errno("strdup");
2949 s->start_id = got_object_id_dup(start_id);
2950 if (s->start_id == NULL) {
2951 err = got_error_from_errno("got_object_id_dup");
2954 s->log_branches = log_branches;
2956 STAILQ_INIT(&s->colors);
2957 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2958 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2959 get_color_value("TOG_COLOR_COMMIT"));
2962 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2963 get_color_value("TOG_COLOR_AUTHOR"));
2965 free_colors(&s->colors);
2968 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2969 get_color_value("TOG_COLOR_DATE"));
2971 free_colors(&s->colors);
2976 view->show = show_log_view;
2977 view->input = input_log_view;
2978 view->close = close_log_view;
2979 view->search_start = search_start_log_view;
2980 view->search_next = search_next_log_view;
2982 if (s->thread_args.pack_fds == NULL) {
2983 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2987 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2988 s->thread_args.pack_fds);
2991 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2995 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2996 s->repo, NULL, NULL);
3000 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3002 err = got_error_set_errno(errcode, "pthread_cond_init");
3005 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3007 err = got_error_set_errno(errcode, "pthread_cond_init");
3011 s->thread_args.commits_needed = view->nlines;
3012 s->thread_args.graph = thread_graph;
3013 s->thread_args.commits = &s->commits;
3014 s->thread_args.in_repo_path = s->in_repo_path;
3015 s->thread_args.start_id = s->start_id;
3016 s->thread_args.repo = thread_repo;
3017 s->thread_args.log_complete = 0;
3018 s->thread_args.quit = &s->quit;
3019 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3020 s->thread_args.selected_entry = &s->selected_entry;
3021 s->thread_args.searching = &view->searching;
3022 s->thread_args.search_next_done = &view->search_next_done;
3023 s->thread_args.regex = &view->regex;
3026 close_log_view(view);
3030 static const struct got_error *
3031 show_log_view(struct tog_view *view)
3033 const struct got_error *err;
3034 struct tog_log_view_state *s = &view->state.log;
3036 if (s->thread == NULL) {
3037 int errcode = pthread_create(&s->thread, NULL, log_thread,
3040 return got_error_set_errno(errcode, "pthread_create");
3041 if (s->thread_args.commits_needed > 0) {
3042 err = trigger_log_thread(view, 1);
3048 return draw_commits(view);
3052 log_move_cursor_up(struct tog_view *view, int page, int home)
3054 struct tog_log_view_state *s = &view->state.log;
3056 if (s->selected_entry->idx == 0)
3058 if (s->first_displayed_entry == NULL)
3061 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3063 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3065 if (!page && !home && s->selected > 0)
3068 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3074 static const struct got_error *
3075 log_move_cursor_down(struct tog_view *view, int page)
3077 struct tog_log_view_state *s = &view->state.log;
3078 struct commit_queue_entry *first;
3079 const struct got_error *err = NULL;
3081 first = s->first_displayed_entry;
3082 if (first == NULL) {
3087 if (s->thread_args.log_complete &&
3088 s->selected_entry->idx >= s->commits.ncommits - 1)
3092 int eos = view->nlines - 2;
3094 if (view_is_hsplit_top(view))
3095 --eos; /* border consumes the last line */
3096 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3099 err = log_scroll_down(view, 1);
3100 } else if (s->thread_args.load_all) {
3101 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3102 s->selected += MIN(s->last_displayed_entry->idx -
3103 s->selected_entry->idx, page + 1);
3105 err = log_scroll_down(view, MIN(page,
3106 s->commits.ncommits - s->selected_entry->idx - 1));
3107 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3109 err = log_scroll_down(view, page);
3112 if (first == s->first_displayed_entry && s->selected <
3113 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3114 s->selected = MIN(s->commits.ncommits - 1, page);
3121 * We might necessarily overshoot in horizontal
3122 * splits; if so, select the last displayed commit.
3124 s->selected = MIN(s->selected,
3125 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3129 if (s->thread_args.log_complete &&
3130 s->selected_entry->idx == s->commits.ncommits - 1)
3137 view_get_split(struct tog_view *view, int *y, int *x)
3142 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3143 if (view->child && view->child->resized_y)
3144 *y = view->child->resized_y;
3145 else if (view->resized_y)
3146 *y = view->resized_y;
3148 *y = view_split_begin_y(view->lines);
3149 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3150 if (view->child && view->child->resized_x)
3151 *x = view->child->resized_x;
3152 else if (view->resized_x)
3153 *x = view->resized_x;
3155 *x = view_split_begin_x(view->begin_x);
3159 /* Split view horizontally at y and offset view->state->selected line. */
3160 static const struct got_error *
3161 view_init_hsplit(struct tog_view *view, int y)
3163 const struct got_error *err = NULL;
3167 err = view_resize(view);
3171 err = offset_selection_down(view);
3176 static const struct got_error *
3177 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3179 const struct got_error *err = NULL;
3180 struct tog_log_view_state *s = &view->state.log;
3181 struct tog_view *diff_view = NULL, *tree_view = NULL;
3182 struct tog_view *ref_view = NULL;
3183 struct commit_queue_entry *entry;
3184 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3186 if (s->thread_args.load_all) {
3187 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3188 s->thread_args.load_all = 0;
3189 else if (s->thread_args.log_complete) {
3190 err = log_move_cursor_down(view, s->commits.ncommits);
3191 s->thread_args.load_all = 0;
3196 eos = nscroll = view->nlines - 1;
3197 if (view_is_hsplit_top(view))
3208 view->x = MAX(view->maxx - view->ncols / 2, 0);
3213 if (view->x + view->ncols / 2 < view->maxx)
3214 view->x += 2; /* move two columns right */
3220 view->x -= MIN(view->x, 2); /* move two columns back */
3229 log_move_cursor_up(view, 0, 0);
3233 log_move_cursor_up(view, 0, 1);
3243 log_move_cursor_up(view, nscroll, 0);
3250 err = log_move_cursor_down(view, 0);
3254 /* We don't know yet how many commits, so we're forced to
3255 * traverse them all. */
3257 if (!s->thread_args.log_complete) {
3258 s->thread_args.load_all = 1;
3259 return trigger_log_thread(view, 0);
3263 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3264 for (n = 0; n < eos; n++) {
3267 s->first_displayed_entry = entry;
3268 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3271 s->selected = n - 1;
3283 err = log_move_cursor_down(view, nscroll);
3286 if (s->selected > view->nlines - 2)
3287 s->selected = view->nlines - 2;
3288 if (s->selected > s->commits.ncommits - 1)
3289 s->selected = s->commits.ncommits - 1;
3291 if (s->commits.ncommits < view->nlines - 1 &&
3292 !s->thread_args.log_complete) {
3293 s->thread_args.commits_needed += (view->nlines - 1) -
3294 s->commits.ncommits;
3295 err = trigger_log_thread(view, 1);
3301 if (s->selected_entry == NULL)
3304 /* get dimensions--don't split till initialisation succeeds */
3305 if (view_is_parent_view(view))
3306 view_get_split(view, &begin_y, &begin_x);
3308 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3309 s->selected_entry->commit, s->selected_entry->id,
3314 if (view_is_parent_view(view) &&
3315 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3316 err = view_init_hsplit(view, begin_y);
3322 diff_view->focussed = 1;
3323 diff_view->mode = view->mode;
3324 diff_view->nlines = view->lines - begin_y;
3326 if (view_is_parent_view(view)) {
3327 view_transfer_size(diff_view, view);
3328 err = view_close_child(view);
3331 err = view_set_child(view, diff_view);
3334 view->focus_child = 1;
3336 *new_view = diff_view;
3340 if (s->selected_entry == NULL)
3342 if (view_is_parent_view(view))
3343 view_get_split(view, &begin_y, &begin_x);
3344 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3345 s->selected_entry, s->in_repo_path, s->head_ref_name,
3349 if (view_is_parent_view(view) &&
3350 view->mode == TOG_VIEW_SPLIT_HRZN) {
3351 err = view_init_hsplit(view, begin_y);
3356 tree_view->focussed = 1;
3357 tree_view->mode = view->mode;
3358 tree_view->nlines = view->lines - begin_y;
3359 if (view_is_parent_view(view)) {
3360 view_transfer_size(tree_view, view);
3361 err = view_close_child(view);
3364 err = view_set_child(view, tree_view);
3367 view->focus_child = 1;
3369 *new_view = tree_view;
3375 if (ch == KEY_BACKSPACE &&
3376 got_path_is_root_dir(s->in_repo_path))
3378 err = stop_log_thread(s);
3381 if (ch == KEY_BACKSPACE) {
3383 err = got_path_dirname(&parent_path, s->in_repo_path);
3386 free(s->in_repo_path);
3387 s->in_repo_path = parent_path;
3388 s->thread_args.in_repo_path = s->in_repo_path;
3389 } else if (ch == CTRL('l')) {
3390 struct got_object_id *start_id;
3391 err = got_repo_match_object_id(&start_id, NULL,
3392 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3393 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3397 s->start_id = start_id;
3398 s->thread_args.start_id = s->start_id;
3400 s->log_branches = !s->log_branches;
3402 if (s->thread_args.pack_fds == NULL) {
3403 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3407 err = got_repo_open(&s->thread_args.repo,
3408 got_repo_get_path(s->repo), NULL,
3409 s->thread_args.pack_fds);
3413 err = tog_load_refs(s->repo, 0);
3416 err = got_commit_graph_open(&s->thread_args.graph,
3417 s->in_repo_path, !s->log_branches);
3420 err = got_commit_graph_iter_start(s->thread_args.graph,
3421 s->start_id, s->repo, NULL, NULL);
3424 free_commits(&s->commits);
3425 s->first_displayed_entry = NULL;
3426 s->last_displayed_entry = NULL;
3427 s->selected_entry = NULL;
3429 s->thread_args.log_complete = 0;
3431 s->thread_args.commits_needed = view->lines;
3432 s->matched_entry = NULL;
3433 s->search_entry = NULL;
3437 if (view_is_parent_view(view))
3438 view_get_split(view, &begin_y, &begin_x);
3439 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3440 if (ref_view == NULL)
3441 return got_error_from_errno("view_open");
3442 err = open_ref_view(ref_view, s->repo);
3444 view_close(ref_view);
3447 if (view_is_parent_view(view) &&
3448 view->mode == TOG_VIEW_SPLIT_HRZN) {
3449 err = view_init_hsplit(view, begin_y);
3454 ref_view->focussed = 1;
3455 ref_view->mode = view->mode;
3456 ref_view->nlines = view->lines - begin_y;
3457 if (view_is_parent_view(view)) {
3458 view_transfer_size(ref_view, view);
3459 err = view_close_child(view);
3462 err = view_set_child(view, ref_view);
3465 view->focus_child = 1;
3467 *new_view = ref_view;
3477 static const struct got_error *
3478 apply_unveil(const char *repo_path, const char *worktree_path)
3480 const struct got_error *error;
3483 if (unveil("gmon.out", "rwc") != 0)
3484 return got_error_from_errno2("unveil", "gmon.out");
3486 if (repo_path && unveil(repo_path, "r") != 0)
3487 return got_error_from_errno2("unveil", repo_path);
3489 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3490 return got_error_from_errno2("unveil", worktree_path);
3492 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3493 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3495 error = got_privsep_unveil_exec_helpers();
3499 if (unveil(NULL, NULL) != 0)
3500 return got_error_from_errno("unveil");
3509 * Override default signal handlers before starting ncurses.
3510 * This should prevent ncurses from installing its own
3511 * broken cleanup() signal handler.
3513 signal(SIGWINCH, tog_sigwinch);
3514 signal(SIGPIPE, tog_sigpipe);
3515 signal(SIGCONT, tog_sigcont);
3516 signal(SIGINT, tog_sigint);
3517 signal(SIGTERM, tog_sigterm);
3521 halfdelay(1); /* Do fast refresh while initial view is loading. */
3524 intrflush(stdscr, FALSE);
3525 keypad(stdscr, TRUE);
3527 if (getenv("TOG_COLORS") != NULL) {
3529 use_default_colors();
3533 static const struct got_error *
3534 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3535 struct got_repository *repo, struct got_worktree *worktree)
3537 const struct got_error *err = NULL;
3540 *in_repo_path = strdup("/");
3541 if (*in_repo_path == NULL)
3542 return got_error_from_errno("strdup");
3547 const char *prefix = got_worktree_get_path_prefix(worktree);
3550 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3553 if (asprintf(in_repo_path, "%s%s%s", prefix,
3554 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3556 err = got_error_from_errno("asprintf");
3557 *in_repo_path = NULL;
3561 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3566 static const struct got_error *
3567 cmd_log(int argc, char *argv[])
3569 const struct got_error *error;
3570 struct got_repository *repo = NULL;
3571 struct got_worktree *worktree = NULL;
3572 struct got_object_id *start_id = NULL;
3573 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3574 char *start_commit = NULL, *label = NULL;
3575 struct got_reference *ref = NULL;
3576 const char *head_ref_name = NULL;
3577 int ch, log_branches = 0;
3578 struct tog_view *view;
3579 int *pack_fds = NULL;
3581 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3587 start_commit = optarg;
3590 repo_path = realpath(optarg, NULL);
3591 if (repo_path == NULL)
3592 return got_error_from_errno2("realpath",
3607 error = got_repo_pack_fds_open(&pack_fds);
3611 if (repo_path == NULL) {
3612 cwd = getcwd(NULL, 0);
3614 return got_error_from_errno("getcwd");
3615 error = got_worktree_open(&worktree, cwd);
3616 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3620 strdup(got_worktree_get_repo_path(worktree));
3622 repo_path = strdup(cwd);
3623 if (repo_path == NULL) {
3624 error = got_error_from_errno("strdup");
3629 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3633 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3640 error = apply_unveil(got_repo_get_path(repo),
3641 worktree ? got_worktree_get_root_path(worktree) : NULL);
3645 /* already loaded by tog_log_with_path()? */
3646 if (TAILQ_EMPTY(&tog_refs)) {
3647 error = tog_load_refs(repo, 0);
3652 if (start_commit == NULL) {
3653 error = got_repo_match_object_id(&start_id, &label,
3654 worktree ? got_worktree_get_head_ref_name(worktree) :
3655 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3658 head_ref_name = label;
3660 error = got_ref_open(&ref, repo, start_commit, 0);
3662 head_ref_name = got_ref_get_name(ref);
3663 else if (error->code != GOT_ERR_NOT_REF)
3665 error = got_repo_match_object_id(&start_id, NULL,
3666 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3671 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3673 error = got_error_from_errno("view_open");
3676 error = open_log_view(view, start_id, repo, head_ref_name,
3677 in_repo_path, log_branches);
3681 /* Release work tree lock. */
3682 got_worktree_close(worktree);
3685 error = view_loop(view);
3695 const struct got_error *close_err = got_repo_close(repo);
3700 got_worktree_close(worktree);
3702 const struct got_error *pack_err =
3703 got_repo_pack_fds_close(pack_fds);
3715 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3716 "[-w] object1 object2\n", getprogname());
3721 match_line(const char *line, regex_t *regex, size_t nmatch,
3722 regmatch_t *regmatch)
3724 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3727 static struct tog_color *
3728 match_color(struct tog_colors *colors, const char *line)
3730 struct tog_color *tc = NULL;
3732 STAILQ_FOREACH(tc, colors, entry) {
3733 if (match_line(line, &tc->regex, 0, NULL))
3740 static const struct got_error *
3741 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3742 WINDOW *window, int skipcol, regmatch_t *regmatch)
3744 const struct got_error *err = NULL;
3746 wchar_t *wline = NULL;
3747 int rme, rms, n, width, scrollx;
3748 int width0 = 0, width1 = 0, width2 = 0;
3749 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3753 rms = regmatch->rm_so;
3754 rme = regmatch->rm_eo;
3756 err = expand_tab(&exstr, line);
3760 /* Split the line into 3 segments, according to match offsets. */
3761 seg0 = strndup(exstr, rms);
3763 err = got_error_from_errno("strndup");
3766 seg1 = strndup(exstr + rms, rme - rms);
3768 err = got_error_from_errno("strndup");
3771 seg2 = strdup(exstr + rme);
3773 err = got_error_from_errno("strndup");
3777 /* draw up to matched token if we haven't scrolled past it */
3778 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3782 n = MAX(width0 - skipcol, 0);
3785 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3786 wlimit, col_tab_align, 1);
3789 waddwstr(window, &wline[scrollx]);
3799 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3803 wlen = wcslen(wline);
3805 width = wcwidth(wline[i]);
3807 /* should not happen, tabs are expanded */
3808 err = got_error(GOT_ERR_RANGE);
3811 if (width0 + w + width > skipcol)
3816 /* draw (visible part of) matched token (if scrolled into it) */
3817 if (width1 - w > 0) {
3818 wattron(window, A_STANDOUT);
3819 waddwstr(window, &wline[i]);
3820 wattroff(window, A_STANDOUT);
3821 wlimit -= (width1 - w);
3822 *wtotal += (width1 - w);
3826 if (wlimit > 0) { /* draw rest of line */
3828 if (skipcol > width0 + width1) {
3829 err = format_line(&wline, &width2, &scrollx, seg2,
3830 skipcol - (width0 + width1), wlimit,
3834 waddwstr(window, &wline[scrollx]);
3836 err = format_line(&wline, &width2, NULL, seg2, 0,
3837 wlimit, col_tab_align, 1);
3840 waddwstr(window, wline);
3853 static const struct got_error *
3854 draw_file(struct tog_view *view, const char *header)
3856 struct tog_diff_view_state *s = &view->state.diff;
3857 regmatch_t *regmatch = &view->regmatch;
3858 const struct got_error *err;
3861 size_t linesize = 0;
3863 struct tog_color *tc;
3866 int max_lines = view->nlines;
3867 int nlines = s->nlines;
3870 line_offset = s->line_offsets[s->first_displayed_line - 1];
3871 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3872 return got_error_from_errno("fseek");
3874 werase(view->window);
3877 if (asprintf(&line, "[%d/%d] %s",
3878 s->first_displayed_line - 1 + s->selected_line, nlines,
3880 return got_error_from_errno("asprintf");
3881 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3887 if (view_needs_focus_indication(view))
3888 wstandout(view->window);
3889 waddwstr(view->window, wline);
3892 if (view_needs_focus_indication(view))
3893 wstandend(view->window);
3894 if (width <= view->ncols - 1)
3895 waddch(view->window, '\n');
3905 while (max_lines > 0 && nprinted < max_lines) {
3906 linelen = getline(&line, &linesize, s->f);
3907 if (linelen == -1) {
3913 return got_ferror(s->f, GOT_ERR_IO);
3916 /* Set view->maxx based on full line length. */
3917 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3923 view->maxx = MAX(view->maxx, width);
3927 tc = match_color(&s->colors, line);
3929 wattr_on(view->window,
3930 COLOR_PAIR(tc->colorpair), NULL);
3931 if (s->first_displayed_line + nprinted == s->matched_line &&
3932 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3933 err = add_matched_line(&width, line, view->ncols, 0,
3934 view->window, view->x, regmatch);
3941 err = format_line(&wline, &width, &skip, line,
3942 view->x, view->ncols, 0, view->x ? 1 : 0);
3947 waddwstr(view->window, &wline[skip]);
3952 wattr_off(view->window,
3953 COLOR_PAIR(tc->colorpair), NULL);
3954 if (width <= view->ncols - 1)
3955 waddch(view->window, '\n');
3960 s->last_displayed_line = s->first_displayed_line +
3963 s->last_displayed_line = s->first_displayed_line;
3968 while (nprinted < view->nlines) {
3969 waddch(view->window, '\n');
3973 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3979 wstandout(view->window);
3980 waddwstr(view->window, wline);
3983 wstandend(view->window);
3990 get_datestr(time_t *time, char *datebuf)
3992 struct tm mytm, *tm;
3995 tm = gmtime_r(time, &mytm);
3998 s = asctime_r(tm, datebuf);
4001 p = strchr(s, '\n');
4007 static const struct got_error *
4008 get_changed_paths(struct got_pathlist_head *paths,
4009 struct got_commit_object *commit, struct got_repository *repo)
4011 const struct got_error *err = NULL;
4012 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4013 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4014 struct got_object_qid *qid;
4016 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4018 struct got_commit_object *pcommit;
4019 err = got_object_open_as_commit(&pcommit, repo,
4024 tree_id1 = got_object_id_dup(
4025 got_object_commit_get_tree_id(pcommit));
4026 if (tree_id1 == NULL) {
4027 got_object_commit_close(pcommit);
4028 return got_error_from_errno("got_object_id_dup");
4030 got_object_commit_close(pcommit);
4035 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4040 tree_id2 = got_object_commit_get_tree_id(commit);
4041 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4045 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4046 got_diff_tree_collect_changed_paths, paths, 0);
4049 got_object_tree_close(tree1);
4051 got_object_tree_close(tree2);
4056 static const struct got_error *
4057 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4061 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4063 return got_error_from_errno("reallocarray");
4065 (*line_offsets)[*nlines] = off;
4070 static const struct got_error *
4071 write_commit_info(off_t **line_offsets, size_t *nlines,
4072 struct got_object_id *commit_id, struct got_reflist_head *refs,
4073 struct got_repository *repo, FILE *outfile)
4075 const struct got_error *err = NULL;
4076 char datebuf[26], *datestr;
4077 struct got_commit_object *commit;
4078 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4079 time_t committer_time;
4080 const char *author, *committer;
4081 char *refs_str = NULL;
4082 struct got_pathlist_head changed_paths;
4083 struct got_pathlist_entry *pe;
4087 TAILQ_INIT(&changed_paths);
4090 err = build_refs_str(&refs_str, refs, commit_id, repo);
4095 err = got_object_open_as_commit(&commit, repo, commit_id);
4099 err = got_object_id_str(&id_str, commit_id);
4101 err = got_error_from_errno("got_object_id_str");
4105 err = add_line_offset(line_offsets, nlines, 0);
4109 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4110 refs_str ? refs_str : "", refs_str ? ")" : "");
4112 err = got_error_from_errno("fprintf");
4116 err = add_line_offset(line_offsets, nlines, outoff);
4120 n = fprintf(outfile, "from: %s\n",
4121 got_object_commit_get_author(commit));
4123 err = got_error_from_errno("fprintf");
4127 err = add_line_offset(line_offsets, nlines, outoff);
4131 committer_time = got_object_commit_get_committer_time(commit);
4132 datestr = get_datestr(&committer_time, datebuf);
4134 n = fprintf(outfile, "date: %s UTC\n", datestr);
4136 err = got_error_from_errno("fprintf");
4140 err = add_line_offset(line_offsets, nlines, outoff);
4144 author = got_object_commit_get_author(commit);
4145 committer = got_object_commit_get_committer(commit);
4146 if (strcmp(author, committer) != 0) {
4147 n = fprintf(outfile, "via: %s\n", committer);
4149 err = got_error_from_errno("fprintf");
4153 err = add_line_offset(line_offsets, nlines, outoff);
4157 if (got_object_commit_get_nparents(commit) > 1) {
4158 const struct got_object_id_queue *parent_ids;
4159 struct got_object_qid *qid;
4161 parent_ids = got_object_commit_get_parent_ids(commit);
4162 STAILQ_FOREACH(qid, parent_ids, entry) {
4163 err = got_object_id_str(&id_str, &qid->id);
4166 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4168 err = got_error_from_errno("fprintf");
4172 err = add_line_offset(line_offsets, nlines, outoff);
4180 err = got_object_commit_get_logmsg(&logmsg, commit);
4184 while ((line = strsep(&s, "\n")) != NULL) {
4185 n = fprintf(outfile, "%s\n", line);
4187 err = got_error_from_errno("fprintf");
4191 err = add_line_offset(line_offsets, nlines, outoff);
4196 err = get_changed_paths(&changed_paths, commit, repo);
4199 TAILQ_FOREACH(pe, &changed_paths, entry) {
4200 struct got_diff_changed_path *cp = pe->data;
4201 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4203 err = got_error_from_errno("fprintf");
4207 err = add_line_offset(line_offsets, nlines, outoff);
4210 free((char *)pe->path);
4214 fputc('\n', outfile);
4216 err = add_line_offset(line_offsets, nlines, outoff);
4218 got_pathlist_free(&changed_paths);
4222 got_object_commit_close(commit);
4224 free(*line_offsets);
4225 *line_offsets = NULL;
4231 static const struct got_error *
4232 create_diff(struct tog_diff_view_state *s)
4234 const struct got_error *err = NULL;
4238 free(s->line_offsets);
4239 s->line_offsets = malloc(sizeof(off_t));
4240 if (s->line_offsets == NULL)
4241 return got_error_from_errno("malloc");
4246 err = got_error_from_errno("got_opentemp");
4249 if (s->f && fclose(s->f) == EOF) {
4250 err = got_error_from_errno("fclose");
4256 err = got_object_get_type(&obj_type, s->repo, s->id1);
4258 err = got_object_get_type(&obj_type, s->repo, s->id2);
4263 case GOT_OBJ_TYPE_BLOB:
4264 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4265 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4266 s->label1, s->label2, tog_diff_algo, s->diff_context,
4267 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4269 case GOT_OBJ_TYPE_TREE:
4270 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4271 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4272 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4273 s->force_text_diff, s->repo, s->f);
4275 case GOT_OBJ_TYPE_COMMIT: {
4276 const struct got_object_id_queue *parent_ids;
4277 struct got_object_qid *pid;
4278 struct got_commit_object *commit2;
4279 struct got_reflist_head *refs;
4281 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4284 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4285 /* Show commit info if we're diffing to a parent/root commit. */
4286 if (s->id1 == NULL) {
4287 err = write_commit_info(&s->line_offsets, &s->nlines,
4288 s->id2, refs, s->repo, s->f);
4292 parent_ids = got_object_commit_get_parent_ids(commit2);
4293 STAILQ_FOREACH(pid, parent_ids, entry) {
4294 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4295 err = write_commit_info(
4296 &s->line_offsets, &s->nlines,
4297 s->id2, refs, s->repo, s->f);
4304 got_object_commit_close(commit2);
4306 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4307 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4308 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4309 s->force_text_diff, s->repo, s->f);
4313 err = got_error(GOT_ERR_OBJ_TYPE);
4319 if (s->f && fflush(s->f) != 0 && err == NULL)
4320 err = got_error_from_errno("fflush");
4325 diff_view_indicate_progress(struct tog_view *view)
4327 mvwaddstr(view->window, 0, 0, "diffing...");
4332 static const struct got_error *
4333 search_start_diff_view(struct tog_view *view)
4335 struct tog_diff_view_state *s = &view->state.diff;
4337 s->matched_line = 0;
4341 static const struct got_error *
4342 search_next_diff_view(struct tog_view *view)
4344 struct tog_diff_view_state *s = &view->state.diff;
4345 const struct got_error *err = NULL;
4348 size_t linesize = 0;
4351 if (!view->searching) {
4352 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4356 if (s->matched_line) {
4357 if (view->searching == TOG_SEARCH_FORWARD)
4358 lineno = s->matched_line + 1;
4360 lineno = s->matched_line - 1;
4362 lineno = s->first_displayed_line;
4367 if (lineno <= 0 || lineno > s->nlines) {
4368 if (s->matched_line == 0) {
4369 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4373 if (view->searching == TOG_SEARCH_FORWARD)
4379 offset = s->line_offsets[lineno - 1];
4380 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4382 return got_error_from_errno("fseeko");
4384 linelen = getline(&line, &linesize, s->f);
4385 if (linelen != -1) {
4387 err = expand_tab(&exstr, line);
4390 if (match_line(exstr, &view->regex, 1,
4392 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4393 s->matched_line = lineno;
4399 if (view->searching == TOG_SEARCH_FORWARD)
4406 if (s->matched_line) {
4407 s->first_displayed_line = s->matched_line;
4408 s->selected_line = 1;
4414 static const struct got_error *
4415 close_diff_view(struct tog_view *view)
4417 const struct got_error *err = NULL;
4418 struct tog_diff_view_state *s = &view->state.diff;
4424 if (s->f && fclose(s->f) == EOF)
4425 err = got_error_from_errno("fclose");
4427 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4428 err = got_error_from_errno("fclose");
4430 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4431 err = got_error_from_errno("fclose");
4433 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4434 err = got_error_from_errno("close");
4436 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4437 err = got_error_from_errno("close");
4439 free_colors(&s->colors);
4440 free(s->line_offsets);
4441 s->line_offsets = NULL;
4446 static const struct got_error *
4447 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4448 struct got_object_id *id2, const char *label1, const char *label2,
4449 int diff_context, int ignore_whitespace, int force_text_diff,
4450 struct tog_view *parent_view, struct got_repository *repo)
4452 const struct got_error *err;
4453 struct tog_diff_view_state *s = &view->state.diff;
4455 memset(s, 0, sizeof(*s));
4459 if (id1 != NULL && id2 != NULL) {
4461 err = got_object_get_type(&type1, repo, id1);
4464 err = got_object_get_type(&type2, repo, id2);
4469 return got_error(GOT_ERR_OBJ_TYPE);
4471 s->first_displayed_line = 1;
4472 s->last_displayed_line = view->nlines;
4473 s->selected_line = 1;
4481 s->id1 = got_object_id_dup(id1);
4483 return got_error_from_errno("got_object_id_dup");
4487 s->id2 = got_object_id_dup(id2);
4488 if (s->id2 == NULL) {
4489 err = got_error_from_errno("got_object_id_dup");
4493 s->f1 = got_opentemp();
4494 if (s->f1 == NULL) {
4495 err = got_error_from_errno("got_opentemp");
4499 s->f2 = got_opentemp();
4500 if (s->f2 == NULL) {
4501 err = got_error_from_errno("got_opentemp");
4505 s->fd1 = got_opentempfd();
4507 err = got_error_from_errno("got_opentempfd");
4511 s->fd2 = got_opentempfd();
4513 err = got_error_from_errno("got_opentempfd");
4517 s->first_displayed_line = 1;
4518 s->last_displayed_line = view->nlines;
4519 s->diff_context = diff_context;
4520 s->ignore_whitespace = ignore_whitespace;
4521 s->force_text_diff = force_text_diff;
4522 s->parent_view = parent_view;
4525 STAILQ_INIT(&s->colors);
4526 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4527 err = add_color(&s->colors,
4528 "^-", TOG_COLOR_DIFF_MINUS,
4529 get_color_value("TOG_COLOR_DIFF_MINUS"));
4532 err = add_color(&s->colors, "^\\+",
4533 TOG_COLOR_DIFF_PLUS,
4534 get_color_value("TOG_COLOR_DIFF_PLUS"));
4537 err = add_color(&s->colors,
4538 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4539 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4543 err = add_color(&s->colors,
4544 "^(commit [0-9a-f]|parent [0-9]|"
4545 "(blob|file|tree|commit) [-+] |"
4546 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4547 get_color_value("TOG_COLOR_DIFF_META"));
4551 err = add_color(&s->colors,
4552 "^(from|via): ", TOG_COLOR_AUTHOR,
4553 get_color_value("TOG_COLOR_AUTHOR"));
4557 err = add_color(&s->colors,
4558 "^date: ", TOG_COLOR_DATE,
4559 get_color_value("TOG_COLOR_DATE"));
4564 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4565 view_is_splitscreen(view))
4566 show_log_view(parent_view); /* draw border */
4567 diff_view_indicate_progress(view);
4569 err = create_diff(s);
4571 view->show = show_diff_view;
4572 view->input = input_diff_view;
4573 view->reset = reset_diff_view;
4574 view->close = close_diff_view;
4575 view->search_start = search_start_diff_view;
4576 view->search_next = search_next_diff_view;
4579 close_diff_view(view);
4583 static const struct got_error *
4584 show_diff_view(struct tog_view *view)
4586 const struct got_error *err;
4587 struct tog_diff_view_state *s = &view->state.diff;
4588 char *id_str1 = NULL, *id_str2, *header;
4589 const char *label1, *label2;
4592 err = got_object_id_str(&id_str1, s->id1);
4595 label1 = s->label1 ? : id_str1;
4597 label1 = "/dev/null";
4599 err = got_object_id_str(&id_str2, s->id2);
4602 label2 = s->label2 ? : id_str2;
4604 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4605 err = got_error_from_errno("asprintf");
4613 err = draw_file(view, header);
4618 static const struct got_error *
4619 set_selected_commit(struct tog_diff_view_state *s,
4620 struct commit_queue_entry *entry)
4622 const struct got_error *err;
4623 const struct got_object_id_queue *parent_ids;
4624 struct got_commit_object *selected_commit;
4625 struct got_object_qid *pid;
4628 s->id2 = got_object_id_dup(entry->id);
4630 return got_error_from_errno("got_object_id_dup");
4632 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4635 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4637 pid = STAILQ_FIRST(parent_ids);
4638 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4639 got_object_commit_close(selected_commit);
4643 static const struct got_error *
4644 reset_diff_view(struct tog_view *view)
4646 struct tog_diff_view_state *s = &view->state.diff;
4649 wclear(view->window);
4650 s->first_displayed_line = 1;
4651 s->last_displayed_line = view->nlines;
4652 s->matched_line = 0;
4653 diff_view_indicate_progress(view);
4654 return create_diff(s);
4657 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4659 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4662 static const struct got_error *
4663 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4665 const struct got_error *err = NULL;
4666 struct tog_diff_view_state *s = &view->state.diff;
4667 struct tog_log_view_state *ls;
4668 struct commit_queue_entry *old_selected_entry;
4670 size_t linesize = 0;
4672 int i, nscroll = view->nlines - 1, up = 0;
4679 view->x = MAX(view->maxx - view->ncols / 3, 0);
4684 if (view->x + view->ncols / 3 < view->maxx)
4685 view->x += 2; /* move two columns right */
4691 view->x -= MIN(view->x, 2); /* move two columns back */
4698 s->force_text_diff = !s->force_text_diff;
4700 s->ignore_whitespace = !s->ignore_whitespace;
4701 err = reset_diff_view(view);
4705 s->first_displayed_line = 1;
4714 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4720 if (s->first_displayed_line > 1)
4721 s->first_displayed_line--;
4732 if (s->first_displayed_line == 1) {
4737 while (i++ < nscroll && s->first_displayed_line > 1)
4738 s->first_displayed_line--;
4744 s->first_displayed_line++;
4761 while (!s->eof && i++ < nscroll) {
4762 linelen = getline(&line, &linesize, s->f);
4763 s->first_displayed_line++;
4764 if (linelen == -1) {
4768 err = got_ferror(s->f, GOT_ERR_IO);
4775 if (s->diff_context > 0) {
4777 s->matched_line = 0;
4778 diff_view_indicate_progress(view);
4779 err = create_diff(s);
4780 if (s->first_displayed_line + view->nlines - 1 >
4782 s->first_displayed_line = 1;
4783 s->last_displayed_line = view->nlines;
4789 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4791 s->matched_line = 0;
4792 diff_view_indicate_progress(view);
4793 err = create_diff(s);
4803 if (s->parent_view == NULL) {
4807 s->parent_view->count = view->count;
4809 if (s->parent_view->type == TOG_VIEW_LOG) {
4810 ls = &s->parent_view->state.log;
4811 old_selected_entry = ls->selected_entry;
4813 err = input_log_view(NULL, s->parent_view,
4814 up ? KEY_UP : KEY_DOWN);
4817 view->count = s->parent_view->count;
4819 if (old_selected_entry == ls->selected_entry)
4822 err = set_selected_commit(s, ls->selected_entry);
4825 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4826 struct tog_blame_view_state *bs;
4827 struct got_object_id *id, *prev_id;
4829 bs = &s->parent_view->state.blame;
4830 prev_id = get_annotation_for_line(bs->blame.lines,
4831 bs->blame.nlines, bs->last_diffed_line);
4833 err = input_blame_view(&view, s->parent_view,
4834 up ? KEY_UP : KEY_DOWN);
4837 view->count = s->parent_view->count;
4839 if (prev_id == NULL)
4841 id = get_selected_commit_id(bs->blame.lines,
4842 bs->blame.nlines, bs->first_displayed_line,
4847 if (!got_object_id_cmp(prev_id, id))
4850 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4854 s->first_displayed_line = 1;
4855 s->last_displayed_line = view->nlines;
4856 s->matched_line = 0;
4859 diff_view_indicate_progress(view);
4860 err = create_diff(s);
4870 static const struct got_error *
4871 cmd_diff(int argc, char *argv[])
4873 const struct got_error *error = NULL;
4874 struct got_repository *repo = NULL;
4875 struct got_worktree *worktree = NULL;
4876 struct got_object_id *id1 = NULL, *id2 = NULL;
4877 char *repo_path = NULL, *cwd = NULL;
4878 char *id_str1 = NULL, *id_str2 = NULL;
4879 char *label1 = NULL, *label2 = NULL;
4880 int diff_context = 3, ignore_whitespace = 0;
4881 int ch, force_text_diff = 0;
4883 struct tog_view *view;
4884 int *pack_fds = NULL;
4886 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4889 force_text_diff = 1;
4892 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4895 errx(1, "number of context lines is %s: %s",
4899 repo_path = realpath(optarg, NULL);
4900 if (repo_path == NULL)
4901 return got_error_from_errno2("realpath",
4903 got_path_strip_trailing_slashes(repo_path);
4906 ignore_whitespace = 1;
4918 usage_diff(); /* TODO show local worktree changes */
4919 } else if (argc == 2) {
4925 error = got_repo_pack_fds_open(&pack_fds);
4929 if (repo_path == NULL) {
4930 cwd = getcwd(NULL, 0);
4932 return got_error_from_errno("getcwd");
4933 error = got_worktree_open(&worktree, cwd);
4934 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4938 strdup(got_worktree_get_repo_path(worktree));
4940 repo_path = strdup(cwd);
4941 if (repo_path == NULL) {
4942 error = got_error_from_errno("strdup");
4947 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4953 error = apply_unveil(got_repo_get_path(repo), NULL);
4957 error = tog_load_refs(repo, 0);
4961 error = got_repo_match_object_id(&id1, &label1, id_str1,
4962 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4966 error = got_repo_match_object_id(&id2, &label2, id_str2,
4967 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4971 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4973 error = got_error_from_errno("view_open");
4976 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4977 ignore_whitespace, force_text_diff, NULL, repo);
4980 error = view_loop(view);
4987 const struct got_error *close_err = got_repo_close(repo);
4992 got_worktree_close(worktree);
4994 const struct got_error *pack_err =
4995 got_repo_pack_fds_close(pack_fds);
5008 "usage: %s blame [-c commit] [-r repository-path] path\n",
5013 struct tog_blame_line {
5015 struct got_object_id *id;
5018 static const struct got_error *
5019 draw_blame(struct tog_view *view)
5021 struct tog_blame_view_state *s = &view->state.blame;
5022 struct tog_blame *blame = &s->blame;
5023 regmatch_t *regmatch = &view->regmatch;
5024 const struct got_error *err;
5025 int lineno = 0, nprinted = 0;
5027 size_t linesize = 0;
5031 struct tog_blame_line *blame_line;
5032 struct got_object_id *prev_id = NULL;
5034 struct tog_color *tc;
5036 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5041 werase(view->window);
5043 if (asprintf(&line, "commit %s", id_str) == -1) {
5044 err = got_error_from_errno("asprintf");
5049 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5054 if (view_needs_focus_indication(view))
5055 wstandout(view->window);
5056 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5058 wattr_on(view->window,
5059 COLOR_PAIR(tc->colorpair), NULL);
5060 waddwstr(view->window, wline);
5062 wattr_off(view->window,
5063 COLOR_PAIR(tc->colorpair), NULL);
5064 if (view_needs_focus_indication(view))
5065 wstandend(view->window);
5068 if (width < view->ncols - 1)
5069 waddch(view->window, '\n');
5071 if (asprintf(&line, "[%d/%d] %s%s",
5072 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5073 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5075 return got_error_from_errno("asprintf");
5078 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5083 waddwstr(view->window, wline);
5086 if (width < view->ncols - 1)
5087 waddch(view->window, '\n');
5091 while (nprinted < view->nlines - 2) {
5092 linelen = getline(&line, &linesize, blame->f);
5093 if (linelen == -1) {
5094 if (feof(blame->f)) {
5099 return got_ferror(blame->f, GOT_ERR_IO);
5101 if (++lineno < s->first_displayed_line)
5104 /* Set view->maxx based on full line length. */
5105 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5112 view->maxx = MAX(view->maxx, width);
5114 if (nprinted == s->selected_line - 1)
5115 wstandout(view->window);
5117 if (blame->nlines > 0) {
5118 blame_line = &blame->lines[lineno - 1];
5119 if (blame_line->annotated && prev_id &&
5120 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5121 !(nprinted == s->selected_line - 1)) {
5122 waddstr(view->window, " ");
5123 } else if (blame_line->annotated) {
5125 err = got_object_id_str(&id_str,
5131 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5133 wattr_on(view->window,
5134 COLOR_PAIR(tc->colorpair), NULL);
5135 wprintw(view->window, "%.8s", id_str);
5137 wattr_off(view->window,
5138 COLOR_PAIR(tc->colorpair), NULL);
5140 prev_id = blame_line->id;
5142 waddstr(view->window, "........");
5146 waddstr(view->window, "........");
5150 if (nprinted == s->selected_line - 1)
5151 wstandend(view->window);
5152 waddstr(view->window, " ");
5154 if (view->ncols <= 9) {
5156 } else if (s->first_displayed_line + nprinted ==
5158 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5159 err = add_matched_line(&width, line, view->ncols - 9, 9,
5160 view->window, view->x, regmatch);
5168 err = format_line(&wline, &width, &skip, line,
5169 view->x, view->ncols - 9, 9, 1);
5174 waddwstr(view->window, &wline[skip]);
5180 if (width <= view->ncols - 1)
5181 waddch(view->window, '\n');
5182 if (++nprinted == 1)
5183 s->first_displayed_line = lineno;
5186 s->last_displayed_line = lineno;
5193 static const struct got_error *
5194 blame_cb(void *arg, int nlines, int lineno,
5195 struct got_commit_object *commit, struct got_object_id *id)
5197 const struct got_error *err = NULL;
5198 struct tog_blame_cb_args *a = arg;
5199 struct tog_blame_line *line;
5202 if (nlines != a->nlines ||
5203 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5204 return got_error(GOT_ERR_RANGE);
5206 errcode = pthread_mutex_lock(&tog_mutex);
5208 return got_error_set_errno(errcode, "pthread_mutex_lock");
5210 if (*a->quit) { /* user has quit the blame view */
5211 err = got_error(GOT_ERR_ITER_COMPLETED);
5216 goto done; /* no change in this commit */
5218 line = &a->lines[lineno - 1];
5219 if (line->annotated)
5222 line->id = got_object_id_dup(id);
5223 if (line->id == NULL) {
5224 err = got_error_from_errno("got_object_id_dup");
5227 line->annotated = 1;
5229 errcode = pthread_mutex_unlock(&tog_mutex);
5231 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5236 blame_thread(void *arg)
5238 const struct got_error *err, *close_err;
5239 struct tog_blame_thread_args *ta = arg;
5240 struct tog_blame_cb_args *a = ta->cb_args;
5241 int errcode, fd1 = -1, fd2 = -1;
5242 FILE *f1 = NULL, *f2 = NULL;
5244 fd1 = got_opentempfd();
5246 return (void *)got_error_from_errno("got_opentempfd");
5248 fd2 = got_opentempfd();
5250 err = got_error_from_errno("got_opentempfd");
5254 f1 = got_opentemp();
5256 err = (void *)got_error_from_errno("got_opentemp");
5259 f2 = got_opentemp();
5261 err = (void *)got_error_from_errno("got_opentemp");
5265 err = block_signals_used_by_main_thread();
5269 err = got_blame(ta->path, a->commit_id, ta->repo,
5270 tog_diff_algo, blame_cb, ta->cb_args,
5271 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5272 if (err && err->code == GOT_ERR_CANCELLED)
5275 errcode = pthread_mutex_lock(&tog_mutex);
5277 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5281 close_err = got_repo_close(ta->repo);
5287 errcode = pthread_mutex_unlock(&tog_mutex);
5288 if (errcode && err == NULL)
5289 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5292 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5293 err = got_error_from_errno("close");
5294 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5295 err = got_error_from_errno("close");
5296 if (f1 && fclose(f1) == EOF && err == NULL)
5297 err = got_error_from_errno("fclose");
5298 if (f2 && fclose(f2) == EOF && err == NULL)
5299 err = got_error_from_errno("fclose");
5304 static struct got_object_id *
5305 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5306 int first_displayed_line, int selected_line)
5308 struct tog_blame_line *line;
5313 line = &lines[first_displayed_line - 1 + selected_line - 1];
5314 if (!line->annotated)
5320 static struct got_object_id *
5321 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5324 struct tog_blame_line *line;
5326 if (nlines <= 0 || lineno >= nlines)
5329 line = &lines[lineno - 1];
5330 if (!line->annotated)
5336 static const struct got_error *
5337 stop_blame(struct tog_blame *blame)
5339 const struct got_error *err = NULL;
5342 if (blame->thread) {
5344 errcode = pthread_mutex_unlock(&tog_mutex);
5346 return got_error_set_errno(errcode,
5347 "pthread_mutex_unlock");
5348 errcode = pthread_join(blame->thread, (void **)&err);
5350 return got_error_set_errno(errcode, "pthread_join");
5351 errcode = pthread_mutex_lock(&tog_mutex);
5353 return got_error_set_errno(errcode,
5354 "pthread_mutex_lock");
5355 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5357 blame->thread = NULL;
5359 if (blame->thread_args.repo) {
5360 const struct got_error *close_err;
5361 close_err = got_repo_close(blame->thread_args.repo);
5364 blame->thread_args.repo = NULL;
5367 if (fclose(blame->f) == EOF && err == NULL)
5368 err = got_error_from_errno("fclose");
5372 for (i = 0; i < blame->nlines; i++)
5373 free(blame->lines[i].id);
5375 blame->lines = NULL;
5377 free(blame->cb_args.commit_id);
5378 blame->cb_args.commit_id = NULL;
5379 if (blame->pack_fds) {
5380 const struct got_error *pack_err =
5381 got_repo_pack_fds_close(blame->pack_fds);
5384 blame->pack_fds = NULL;
5389 static const struct got_error *
5390 cancel_blame_view(void *arg)
5392 const struct got_error *err = NULL;
5396 errcode = pthread_mutex_lock(&tog_mutex);
5398 return got_error_set_errno(errcode,
5399 "pthread_mutex_unlock");
5402 err = got_error(GOT_ERR_CANCELLED);
5404 errcode = pthread_mutex_unlock(&tog_mutex);
5406 return got_error_set_errno(errcode,
5407 "pthread_mutex_lock");
5412 static const struct got_error *
5413 run_blame(struct tog_view *view)
5415 struct tog_blame_view_state *s = &view->state.blame;
5416 struct tog_blame *blame = &s->blame;
5417 const struct got_error *err = NULL;
5418 struct got_commit_object *commit = NULL;
5419 struct got_blob_object *blob = NULL;
5420 struct got_repository *thread_repo = NULL;
5421 struct got_object_id *obj_id = NULL;
5422 int obj_type, fd = -1;
5423 int *pack_fds = NULL;
5425 err = got_object_open_as_commit(&commit, s->repo,
5426 &s->blamed_commit->id);
5430 fd = got_opentempfd();
5432 err = got_error_from_errno("got_opentempfd");
5436 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5440 err = got_object_get_type(&obj_type, s->repo, obj_id);
5444 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5445 err = got_error(GOT_ERR_OBJ_TYPE);
5449 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5452 blame->f = got_opentemp();
5453 if (blame->f == NULL) {
5454 err = got_error_from_errno("got_opentemp");
5457 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5458 &blame->line_offsets, blame->f, blob);
5461 if (blame->nlines == 0) {
5462 s->blame_complete = 1;
5466 /* Don't include \n at EOF in the blame line count. */
5467 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5470 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5471 if (blame->lines == NULL) {
5472 err = got_error_from_errno("calloc");
5476 err = got_repo_pack_fds_open(&pack_fds);
5479 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5484 blame->pack_fds = pack_fds;
5485 blame->cb_args.view = view;
5486 blame->cb_args.lines = blame->lines;
5487 blame->cb_args.nlines = blame->nlines;
5488 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5489 if (blame->cb_args.commit_id == NULL) {
5490 err = got_error_from_errno("got_object_id_dup");
5493 blame->cb_args.quit = &s->done;
5495 blame->thread_args.path = s->path;
5496 blame->thread_args.repo = thread_repo;
5497 blame->thread_args.cb_args = &blame->cb_args;
5498 blame->thread_args.complete = &s->blame_complete;
5499 blame->thread_args.cancel_cb = cancel_blame_view;
5500 blame->thread_args.cancel_arg = &s->done;
5501 s->blame_complete = 0;
5503 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5504 s->first_displayed_line = 1;
5505 s->last_displayed_line = view->nlines;
5506 s->selected_line = 1;
5508 s->matched_line = 0;
5512 got_object_commit_close(commit);
5513 if (fd != -1 && close(fd) == -1 && err == NULL)
5514 err = got_error_from_errno("close");
5516 got_object_blob_close(blob);
5523 static const struct got_error *
5524 open_blame_view(struct tog_view *view, char *path,
5525 struct got_object_id *commit_id, struct got_repository *repo)
5527 const struct got_error *err = NULL;
5528 struct tog_blame_view_state *s = &view->state.blame;
5530 STAILQ_INIT(&s->blamed_commits);
5532 s->path = strdup(path);
5533 if (s->path == NULL)
5534 return got_error_from_errno("strdup");
5536 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5542 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5543 s->first_displayed_line = 1;
5544 s->last_displayed_line = view->nlines;
5545 s->selected_line = 1;
5546 s->blame_complete = 0;
5548 s->commit_id = commit_id;
5549 memset(&s->blame, 0, sizeof(s->blame));
5551 STAILQ_INIT(&s->colors);
5552 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5553 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5554 get_color_value("TOG_COLOR_COMMIT"));
5559 view->show = show_blame_view;
5560 view->input = input_blame_view;
5561 view->reset = reset_blame_view;
5562 view->close = close_blame_view;
5563 view->search_start = search_start_blame_view;
5564 view->search_next = search_next_blame_view;
5566 return run_blame(view);
5569 static const struct got_error *
5570 close_blame_view(struct tog_view *view)
5572 const struct got_error *err = NULL;
5573 struct tog_blame_view_state *s = &view->state.blame;
5575 if (s->blame.thread)
5576 err = stop_blame(&s->blame);
5578 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5579 struct got_object_qid *blamed_commit;
5580 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5581 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5582 got_object_qid_free(blamed_commit);
5586 free_colors(&s->colors);
5590 static const struct got_error *
5591 search_start_blame_view(struct tog_view *view)
5593 struct tog_blame_view_state *s = &view->state.blame;
5595 s->matched_line = 0;
5599 static const struct got_error *
5600 search_next_blame_view(struct tog_view *view)
5602 struct tog_blame_view_state *s = &view->state.blame;
5603 const struct got_error *err = NULL;
5606 size_t linesize = 0;
5609 if (!view->searching) {
5610 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5614 if (s->matched_line) {
5615 if (view->searching == TOG_SEARCH_FORWARD)
5616 lineno = s->matched_line + 1;
5618 lineno = s->matched_line - 1;
5620 lineno = s->first_displayed_line - 1 + s->selected_line;
5625 if (lineno <= 0 || lineno > s->blame.nlines) {
5626 if (s->matched_line == 0) {
5627 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5631 if (view->searching == TOG_SEARCH_FORWARD)
5634 lineno = s->blame.nlines;
5637 offset = s->blame.line_offsets[lineno - 1];
5638 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5640 return got_error_from_errno("fseeko");
5642 linelen = getline(&line, &linesize, s->blame.f);
5643 if (linelen != -1) {
5645 err = expand_tab(&exstr, line);
5648 if (match_line(exstr, &view->regex, 1,
5650 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5651 s->matched_line = lineno;
5657 if (view->searching == TOG_SEARCH_FORWARD)
5664 if (s->matched_line) {
5665 s->first_displayed_line = s->matched_line;
5666 s->selected_line = 1;
5672 static const struct got_error *
5673 show_blame_view(struct tog_view *view)
5675 const struct got_error *err = NULL;
5676 struct tog_blame_view_state *s = &view->state.blame;
5679 if (s->blame.thread == NULL && !s->blame_complete) {
5680 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5681 &s->blame.thread_args);
5683 return got_error_set_errno(errcode, "pthread_create");
5685 halfdelay(1); /* fast refresh while annotating */
5688 if (s->blame_complete)
5689 halfdelay(10); /* disable fast refresh */
5691 err = draw_blame(view);
5697 static const struct got_error *
5698 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5700 const struct got_error *err = NULL, *thread_err = NULL;
5701 struct tog_view *diff_view;
5702 struct tog_blame_view_state *s = &view->state.blame;
5703 int eos, nscroll, begin_y = 0, begin_x = 0;
5705 eos = nscroll = view->nlines - 2;
5706 if (view_is_hsplit_top(view))
5714 view->x = MAX(view->maxx - view->ncols / 3, 0);
5719 if (view->x + view->ncols / 3 < view->maxx)
5720 view->x += 2; /* move two columns right */
5726 view->x -= MIN(view->x, 2); /* move two columns back */
5735 s->selected_line = 1;
5736 s->first_displayed_line = 1;
5741 if (s->blame.nlines < eos) {
5742 s->selected_line = s->blame.nlines;
5743 s->first_displayed_line = 1;
5745 s->selected_line = eos;
5746 s->first_displayed_line = s->blame.nlines - (eos - 1);
5753 if (s->selected_line > 1)
5755 else if (s->selected_line == 1 &&
5756 s->first_displayed_line > 1)
5757 s->first_displayed_line--;
5768 if (s->first_displayed_line == 1) {
5769 if (view->count > 1)
5771 s->selected_line = MAX(1, s->selected_line - nscroll);
5775 if (s->first_displayed_line > nscroll)
5776 s->first_displayed_line -= nscroll;
5778 s->first_displayed_line = 1;
5783 if (s->selected_line < eos && s->first_displayed_line +
5784 s->selected_line <= s->blame.nlines)
5786 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5787 s->first_displayed_line++;
5793 struct got_object_id *id = NULL;
5796 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5797 s->first_displayed_line, s->selected_line);
5801 struct got_commit_object *commit, *pcommit;
5802 struct got_object_qid *pid;
5803 struct got_object_id *blob_id = NULL;
5805 err = got_object_open_as_commit(&commit,
5810 got_object_commit_get_parent_ids(commit));
5812 got_object_commit_close(commit);
5815 /* Check if path history ends here. */
5816 err = got_object_open_as_commit(&pcommit,
5820 err = got_object_id_by_path(&blob_id, s->repo,
5822 got_object_commit_close(pcommit);
5824 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5826 got_object_commit_close(commit);
5829 err = got_object_get_type(&obj_type, s->repo,
5832 /* Can't blame non-blob type objects. */
5833 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5834 got_object_commit_close(commit);
5837 err = got_object_qid_alloc(&s->blamed_commit,
5839 got_object_commit_close(commit);
5841 if (got_object_id_cmp(id,
5842 &s->blamed_commit->id) == 0)
5844 err = got_object_qid_alloc(&s->blamed_commit,
5850 thread_err = stop_blame(&s->blame);
5854 STAILQ_INSERT_HEAD(&s->blamed_commits,
5855 s->blamed_commit, entry);
5856 err = run_blame(view);
5862 struct got_object_qid *first;
5865 first = STAILQ_FIRST(&s->blamed_commits);
5866 if (!got_object_id_cmp(&first->id, s->commit_id))
5869 thread_err = stop_blame(&s->blame);
5873 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5874 got_object_qid_free(s->blamed_commit);
5876 STAILQ_FIRST(&s->blamed_commits);
5877 err = run_blame(view);
5884 struct got_object_id *id = NULL;
5885 struct got_object_qid *pid;
5886 struct got_commit_object *commit = NULL;
5889 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5890 s->first_displayed_line, s->selected_line);
5893 err = got_object_open_as_commit(&commit, s->repo, id);
5896 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5898 /* traversed from diff view, release diff resources */
5899 err = close_diff_view(*new_view);
5902 diff_view = *new_view;
5904 if (view_is_parent_view(view))
5905 view_get_split(view, &begin_y, &begin_x);
5907 diff_view = view_open(0, 0, begin_y, begin_x,
5909 if (diff_view == NULL) {
5910 got_object_commit_close(commit);
5911 err = got_error_from_errno("view_open");
5915 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5916 id, NULL, NULL, 3, 0, 0, view, s->repo);
5917 got_object_commit_close(commit);
5919 view_close(diff_view);
5922 s->last_diffed_line = s->first_displayed_line - 1 +
5925 break; /* still open from active diff view */
5926 if (view_is_parent_view(view) &&
5927 view->mode == TOG_VIEW_SPLIT_HRZN) {
5928 err = view_init_hsplit(view, begin_y);
5934 diff_view->focussed = 1;
5935 diff_view->mode = view->mode;
5936 diff_view->nlines = view->lines - begin_y;
5937 if (view_is_parent_view(view)) {
5938 view_transfer_size(diff_view, view);
5939 err = view_close_child(view);
5942 err = view_set_child(view, diff_view);
5945 view->focus_child = 1;
5947 *new_view = diff_view;
5960 if (s->last_displayed_line >= s->blame.nlines &&
5961 s->selected_line >= MIN(s->blame.nlines,
5962 view->nlines - 2)) {
5966 if (s->last_displayed_line >= s->blame.nlines &&
5967 s->selected_line < view->nlines - 2) {
5969 MIN(nscroll, s->last_displayed_line -
5970 s->first_displayed_line - s->selected_line + 1);
5972 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5973 s->first_displayed_line += nscroll;
5975 s->first_displayed_line =
5976 s->blame.nlines - (view->nlines - 3);
5979 if (s->selected_line > view->nlines - 2) {
5980 s->selected_line = MIN(s->blame.nlines,
5988 return thread_err ? thread_err : err;
5991 static const struct got_error *
5992 reset_blame_view(struct tog_view *view)
5994 const struct got_error *err;
5995 struct tog_blame_view_state *s = &view->state.blame;
5999 err = stop_blame(&s->blame);
6003 return run_blame(view);
6006 static const struct got_error *
6007 cmd_blame(int argc, char *argv[])
6009 const struct got_error *error;
6010 struct got_repository *repo = NULL;
6011 struct got_worktree *worktree = NULL;
6012 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6013 char *link_target = NULL;
6014 struct got_object_id *commit_id = NULL;
6015 struct got_commit_object *commit = NULL;
6016 char *commit_id_str = NULL;
6018 struct tog_view *view;
6019 int *pack_fds = NULL;
6021 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6024 commit_id_str = optarg;
6027 repo_path = realpath(optarg, NULL);
6028 if (repo_path == NULL)
6029 return got_error_from_errno2("realpath",
6044 error = got_repo_pack_fds_open(&pack_fds);
6048 if (repo_path == NULL) {
6049 cwd = getcwd(NULL, 0);
6051 return got_error_from_errno("getcwd");
6052 error = got_worktree_open(&worktree, cwd);
6053 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6057 strdup(got_worktree_get_repo_path(worktree));
6059 repo_path = strdup(cwd);
6060 if (repo_path == NULL) {
6061 error = got_error_from_errno("strdup");
6066 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6070 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6077 error = apply_unveil(got_repo_get_path(repo), NULL);
6081 error = tog_load_refs(repo, 0);
6085 if (commit_id_str == NULL) {
6086 struct got_reference *head_ref;
6087 error = got_ref_open(&head_ref, repo, worktree ?
6088 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6091 error = got_ref_resolve(&commit_id, repo, head_ref);
6092 got_ref_close(head_ref);
6094 error = got_repo_match_object_id(&commit_id, NULL,
6095 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6100 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6102 error = got_error_from_errno("view_open");
6106 error = got_object_open_as_commit(&commit, repo, commit_id);
6110 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6115 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6120 /* Release work tree lock. */
6121 got_worktree_close(worktree);
6124 error = view_loop(view);
6132 got_object_commit_close(commit);
6134 got_worktree_close(worktree);
6136 const struct got_error *close_err = got_repo_close(repo);
6141 const struct got_error *pack_err =
6142 got_repo_pack_fds_close(pack_fds);
6150 static const struct got_error *
6151 draw_tree_entries(struct tog_view *view, const char *parent_path)
6153 struct tog_tree_view_state *s = &view->state.tree;
6154 const struct got_error *err = NULL;
6155 struct got_tree_entry *te;
6157 struct tog_color *tc;
6158 int width, n, i, nentries;
6159 int limit = view->nlines;
6162 if (view_is_hsplit_top(view))
6163 --limit; /* border */
6165 werase(view->window);
6170 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6174 if (view_needs_focus_indication(view))
6175 wstandout(view->window);
6176 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6178 wattr_on(view->window,
6179 COLOR_PAIR(tc->colorpair), NULL);
6180 waddwstr(view->window, wline);
6182 wattr_off(view->window,
6183 COLOR_PAIR(tc->colorpair), NULL);
6184 if (view_needs_focus_indication(view))
6185 wstandend(view->window);
6188 if (width < view->ncols - 1)
6189 waddch(view->window, '\n');
6192 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6196 waddwstr(view->window, wline);
6199 if (width < view->ncols - 1)
6200 waddch(view->window, '\n');
6203 waddch(view->window, '\n');
6207 if (s->first_displayed_entry == NULL) {
6208 te = got_object_tree_get_first_entry(s->tree);
6209 if (s->selected == 0) {
6211 wstandout(view->window);
6212 s->selected_entry = NULL;
6214 waddstr(view->window, " ..\n"); /* parent directory */
6215 if (s->selected == 0 && view->focussed)
6216 wstandend(view->window);
6223 te = s->first_displayed_entry;
6226 nentries = got_object_tree_get_nentries(s->tree);
6227 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6228 char *line = NULL, *id_str = NULL, *link_target = NULL;
6229 const char *modestr = "";
6232 te = got_object_tree_get_entry(s->tree, i);
6233 mode = got_tree_entry_get_mode(te);
6236 err = got_object_id_str(&id_str,
6237 got_tree_entry_get_id(te));
6239 return got_error_from_errno(
6240 "got_object_id_str");
6242 if (got_object_tree_entry_is_submodule(te))
6244 else if (S_ISLNK(mode)) {
6247 err = got_tree_entry_get_symlink_target(&link_target,
6253 for (i = 0; i < strlen(link_target); i++) {
6254 if (!isprint((unsigned char)link_target[i]))
6255 link_target[i] = '?';
6259 else if (S_ISDIR(mode))
6261 else if (mode & S_IXUSR)
6263 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6264 got_tree_entry_get_name(te), modestr,
6265 link_target ? " -> ": "",
6266 link_target ? link_target : "") == -1) {
6269 return got_error_from_errno("asprintf");
6273 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6279 if (n == s->selected) {
6281 wstandout(view->window);
6282 s->selected_entry = te;
6284 tc = match_color(&s->colors, line);
6286 wattr_on(view->window,
6287 COLOR_PAIR(tc->colorpair), NULL);
6288 waddwstr(view->window, wline);
6290 wattr_off(view->window,
6291 COLOR_PAIR(tc->colorpair), NULL);
6292 if (width < view->ncols - 1)
6293 waddch(view->window, '\n');
6294 if (n == s->selected && view->focussed)
6295 wstandend(view->window);
6301 s->last_displayed_entry = te;
6310 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6312 struct got_tree_entry *te;
6313 int isroot = s->tree == s->root;
6316 if (s->first_displayed_entry == NULL)
6319 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6320 while (i++ < maxscroll) {
6323 s->first_displayed_entry = NULL;
6326 s->first_displayed_entry = te;
6327 te = got_tree_entry_get_prev(s->tree, te);
6331 static const struct got_error *
6332 tree_scroll_down(struct tog_view *view, int maxscroll)
6334 struct tog_tree_view_state *s = &view->state.tree;
6335 struct got_tree_entry *next, *last;
6338 if (s->first_displayed_entry)
6339 next = got_tree_entry_get_next(s->tree,
6340 s->first_displayed_entry);
6342 next = got_object_tree_get_first_entry(s->tree);
6344 last = s->last_displayed_entry;
6345 while (next && n++ < maxscroll) {
6347 last = got_tree_entry_get_next(s->tree, last);
6348 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6349 s->first_displayed_entry = next;
6350 next = got_tree_entry_get_next(s->tree, next);
6357 static const struct got_error *
6358 tree_entry_path(char **path, struct tog_parent_trees *parents,
6359 struct got_tree_entry *te)
6361 const struct got_error *err = NULL;
6362 struct tog_parent_tree *pt;
6363 size_t len = 2; /* for leading slash and NUL */
6365 TAILQ_FOREACH(pt, parents, entry)
6366 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6369 len += strlen(got_tree_entry_get_name(te));
6371 *path = calloc(1, len);
6373 return got_error_from_errno("calloc");
6376 pt = TAILQ_LAST(parents, tog_parent_trees);
6378 const char *name = got_tree_entry_get_name(pt->selected_entry);
6379 if (strlcat(*path, name, len) >= len) {
6380 err = got_error(GOT_ERR_NO_SPACE);
6383 if (strlcat(*path, "/", len) >= len) {
6384 err = got_error(GOT_ERR_NO_SPACE);
6387 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6390 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6391 err = got_error(GOT_ERR_NO_SPACE);
6403 static const struct got_error *
6404 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6405 struct got_tree_entry *te, struct tog_parent_trees *parents,
6406 struct got_object_id *commit_id, struct got_repository *repo)
6408 const struct got_error *err = NULL;
6410 struct tog_view *blame_view;
6414 err = tree_entry_path(&path, parents, te);
6418 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6419 if (blame_view == NULL) {
6420 err = got_error_from_errno("view_open");
6424 err = open_blame_view(blame_view, path, commit_id, repo);
6426 if (err->code == GOT_ERR_CANCELLED)
6428 view_close(blame_view);
6430 *new_view = blame_view;
6436 static const struct got_error *
6437 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6438 struct tog_tree_view_state *s)
6440 struct tog_view *log_view;
6441 const struct got_error *err = NULL;
6446 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6447 if (log_view == NULL)
6448 return got_error_from_errno("view_open");
6450 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6454 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6457 view_close(log_view);
6459 *new_view = log_view;
6464 static const struct got_error *
6465 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6466 const char *head_ref_name, struct got_repository *repo)
6468 const struct got_error *err = NULL;
6469 char *commit_id_str = NULL;
6470 struct tog_tree_view_state *s = &view->state.tree;
6471 struct got_commit_object *commit = NULL;
6473 TAILQ_INIT(&s->parents);
6474 STAILQ_INIT(&s->colors);
6476 s->commit_id = got_object_id_dup(commit_id);
6477 if (s->commit_id == NULL)
6478 return got_error_from_errno("got_object_id_dup");
6480 err = got_object_open_as_commit(&commit, repo, commit_id);
6485 * The root is opened here and will be closed when the view is closed.
6486 * Any visited subtrees and their path-wise parents are opened and
6489 err = got_object_open_as_tree(&s->root, repo,
6490 got_object_commit_get_tree_id(commit));
6495 err = got_object_id_str(&commit_id_str, commit_id);
6499 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6500 err = got_error_from_errno("asprintf");
6504 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6505 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6506 if (head_ref_name) {
6507 s->head_ref_name = strdup(head_ref_name);
6508 if (s->head_ref_name == NULL) {
6509 err = got_error_from_errno("strdup");
6515 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6516 err = add_color(&s->colors, "\\$$",
6517 TOG_COLOR_TREE_SUBMODULE,
6518 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6521 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6522 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6525 err = add_color(&s->colors, "/$",
6526 TOG_COLOR_TREE_DIRECTORY,
6527 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6531 err = add_color(&s->colors, "\\*$",
6532 TOG_COLOR_TREE_EXECUTABLE,
6533 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6537 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6538 get_color_value("TOG_COLOR_COMMIT"));
6543 view->show = show_tree_view;
6544 view->input = input_tree_view;
6545 view->close = close_tree_view;
6546 view->search_start = search_start_tree_view;
6547 view->search_next = search_next_tree_view;
6549 free(commit_id_str);
6551 got_object_commit_close(commit);
6553 close_tree_view(view);
6557 static const struct got_error *
6558 close_tree_view(struct tog_view *view)
6560 struct tog_tree_view_state *s = &view->state.tree;
6562 free_colors(&s->colors);
6563 free(s->tree_label);
6564 s->tree_label = NULL;
6566 s->commit_id = NULL;
6567 free(s->head_ref_name);
6568 s->head_ref_name = NULL;
6569 while (!TAILQ_EMPTY(&s->parents)) {
6570 struct tog_parent_tree *parent;
6571 parent = TAILQ_FIRST(&s->parents);
6572 TAILQ_REMOVE(&s->parents, parent, entry);
6573 if (parent->tree != s->root)
6574 got_object_tree_close(parent->tree);
6578 if (s->tree != NULL && s->tree != s->root)
6579 got_object_tree_close(s->tree);
6581 got_object_tree_close(s->root);
6585 static const struct got_error *
6586 search_start_tree_view(struct tog_view *view)
6588 struct tog_tree_view_state *s = &view->state.tree;
6590 s->matched_entry = NULL;
6595 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6597 regmatch_t regmatch;
6599 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6603 static const struct got_error *
6604 search_next_tree_view(struct tog_view *view)
6606 struct tog_tree_view_state *s = &view->state.tree;
6607 struct got_tree_entry *te = NULL;
6609 if (!view->searching) {
6610 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6614 if (s->matched_entry) {
6615 if (view->searching == TOG_SEARCH_FORWARD) {
6616 if (s->selected_entry)
6617 te = got_tree_entry_get_next(s->tree,
6620 te = got_object_tree_get_first_entry(s->tree);
6622 if (s->selected_entry == NULL)
6623 te = got_object_tree_get_last_entry(s->tree);
6625 te = got_tree_entry_get_prev(s->tree,
6629 if (s->selected_entry)
6630 te = s->selected_entry;
6631 else if (view->searching == TOG_SEARCH_FORWARD)
6632 te = got_object_tree_get_first_entry(s->tree);
6634 te = got_object_tree_get_last_entry(s->tree);
6639 if (s->matched_entry == NULL) {
6640 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6643 if (view->searching == TOG_SEARCH_FORWARD)
6644 te = got_object_tree_get_first_entry(s->tree);
6646 te = got_object_tree_get_last_entry(s->tree);
6649 if (match_tree_entry(te, &view->regex)) {
6650 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6651 s->matched_entry = te;
6655 if (view->searching == TOG_SEARCH_FORWARD)
6656 te = got_tree_entry_get_next(s->tree, te);
6658 te = got_tree_entry_get_prev(s->tree, te);
6661 if (s->matched_entry) {
6662 s->first_displayed_entry = s->matched_entry;
6669 static const struct got_error *
6670 show_tree_view(struct tog_view *view)
6672 const struct got_error *err = NULL;
6673 struct tog_tree_view_state *s = &view->state.tree;
6676 err = tree_entry_path(&parent_path, &s->parents, NULL);
6680 err = draw_tree_entries(view, parent_path);
6687 static const struct got_error *
6688 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6690 const struct got_error *err = NULL;
6691 struct tog_tree_view_state *s = &view->state.tree;
6692 struct tog_view *log_view, *ref_view;
6693 struct got_tree_entry *te;
6694 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6698 s->show_ids = !s->show_ids;
6703 if (!s->selected_entry)
6705 if (view_is_parent_view(view))
6706 view_get_split(view, &begin_y, &begin_x);
6707 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6708 if (view_is_parent_view(view) &&
6709 view->mode == TOG_VIEW_SPLIT_HRZN) {
6710 err = view_init_hsplit(view, begin_y);
6715 log_view->focussed = 1;
6716 log_view->mode = view->mode;
6717 log_view->nlines = view->lines - begin_y;
6718 if (view_is_parent_view(view)) {
6719 view_transfer_size(log_view, view);
6720 err = view_close_child(view);
6723 err = view_set_child(view, log_view);
6726 view->focus_child = 1;
6728 *new_view = log_view;
6732 if (view_is_parent_view(view))
6733 view_get_split(view, &begin_y, &begin_x);
6734 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6735 if (ref_view == NULL)
6736 return got_error_from_errno("view_open");
6737 err = open_ref_view(ref_view, s->repo);
6739 view_close(ref_view);
6742 if (view_is_parent_view(view) &&
6743 view->mode == TOG_VIEW_SPLIT_HRZN) {
6744 err = view_init_hsplit(view, begin_y);
6749 ref_view->focussed = 1;
6750 ref_view->mode = view->mode;
6751 ref_view->nlines = view->lines - begin_y;
6752 if (view_is_parent_view(view)) {
6753 view_transfer_size(ref_view, view);
6754 err = view_close_child(view);
6757 err = view_set_child(view, ref_view);
6760 view->focus_child = 1;
6762 *new_view = ref_view;
6768 if (s->tree == s->root)
6769 s->first_displayed_entry =
6770 got_object_tree_get_first_entry(s->tree);
6772 s->first_displayed_entry = NULL;
6776 int eos = view->nlines - 3;
6778 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6782 te = got_object_tree_get_last_entry(s->tree);
6783 for (n = 0; n < eos; n++) {
6785 if(s->tree != s->root) {
6786 s->first_displayed_entry = NULL;
6791 s->first_displayed_entry = te;
6792 te = got_tree_entry_get_prev(s->tree, te);
6795 s->selected = n - 1;
6801 if (s->selected > 0) {
6805 tree_scroll_up(s, 1);
6806 if (s->selected_entry == NULL ||
6807 (s->tree == s->root && s->selected_entry ==
6808 got_object_tree_get_first_entry(s->tree)))
6818 if (s->tree == s->root) {
6819 if (got_object_tree_get_first_entry(s->tree) ==
6820 s->first_displayed_entry)
6821 s->selected -= MIN(s->selected, nscroll);
6823 if (s->first_displayed_entry == NULL)
6824 s->selected -= MIN(s->selected, nscroll);
6826 tree_scroll_up(s, MAX(0, nscroll));
6827 if (s->selected_entry == NULL ||
6828 (s->tree == s->root && s->selected_entry ==
6829 got_object_tree_get_first_entry(s->tree)))
6835 if (s->selected < s->ndisplayed - 1) {
6839 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6841 /* can't scroll any further */
6845 tree_scroll_down(view, 1);
6855 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6857 /* can't scroll any further; move cursor down */
6858 if (s->selected < s->ndisplayed - 1)
6859 s->selected += MIN(nscroll,
6860 s->ndisplayed - s->selected - 1);
6865 tree_scroll_down(view, nscroll);
6870 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6871 struct tog_parent_tree *parent;
6872 /* user selected '..' */
6873 if (s->tree == s->root) {
6877 parent = TAILQ_FIRST(&s->parents);
6878 TAILQ_REMOVE(&s->parents, parent,
6880 got_object_tree_close(s->tree);
6881 s->tree = parent->tree;
6882 s->first_displayed_entry =
6883 parent->first_displayed_entry;
6885 parent->selected_entry;
6886 s->selected = parent->selected;
6887 if (s->selected > view->nlines - 3) {
6888 err = offset_selection_down(view);
6893 } else if (S_ISDIR(got_tree_entry_get_mode(
6894 s->selected_entry))) {
6895 struct got_tree_object *subtree;
6897 err = got_object_open_as_tree(&subtree, s->repo,
6898 got_tree_entry_get_id(s->selected_entry));
6901 err = tree_view_visit_subtree(s, subtree);
6903 got_object_tree_close(subtree);
6906 } else if (S_ISREG(got_tree_entry_get_mode(
6907 s->selected_entry))) {
6908 struct tog_view *blame_view;
6909 int begin_x = 0, begin_y = 0;
6911 if (view_is_parent_view(view))
6912 view_get_split(view, &begin_y, &begin_x);
6914 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6915 s->selected_entry, &s->parents,
6916 s->commit_id, s->repo);
6920 if (view_is_parent_view(view) &&
6921 view->mode == TOG_VIEW_SPLIT_HRZN) {
6922 err = view_init_hsplit(view, begin_y);
6929 blame_view->focussed = 1;
6930 blame_view->mode = view->mode;
6931 blame_view->nlines = view->lines - begin_y;
6932 if (view_is_parent_view(view)) {
6933 view_transfer_size(blame_view, view);
6934 err = view_close_child(view);
6937 err = view_set_child(view, blame_view);
6940 view->focus_child = 1;
6942 *new_view = blame_view;
6946 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6947 s->selected = view->nlines - 4;
6963 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6968 static const struct got_error *
6969 cmd_tree(int argc, char *argv[])
6971 const struct got_error *error;
6972 struct got_repository *repo = NULL;
6973 struct got_worktree *worktree = NULL;
6974 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6975 struct got_object_id *commit_id = NULL;
6976 struct got_commit_object *commit = NULL;
6977 const char *commit_id_arg = NULL;
6979 struct got_reference *ref = NULL;
6980 const char *head_ref_name = NULL;
6982 struct tog_view *view;
6983 int *pack_fds = NULL;
6985 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6988 commit_id_arg = optarg;
6991 repo_path = realpath(optarg, NULL);
6992 if (repo_path == NULL)
6993 return got_error_from_errno2("realpath",
7008 error = got_repo_pack_fds_open(&pack_fds);
7012 if (repo_path == NULL) {
7013 cwd = getcwd(NULL, 0);
7015 return got_error_from_errno("getcwd");
7016 error = got_worktree_open(&worktree, cwd);
7017 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7021 strdup(got_worktree_get_repo_path(worktree));
7023 repo_path = strdup(cwd);
7024 if (repo_path == NULL) {
7025 error = got_error_from_errno("strdup");
7030 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7034 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7041 error = apply_unveil(got_repo_get_path(repo), NULL);
7045 error = tog_load_refs(repo, 0);
7049 if (commit_id_arg == NULL) {
7050 error = got_repo_match_object_id(&commit_id, &label,
7051 worktree ? got_worktree_get_head_ref_name(worktree) :
7052 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7055 head_ref_name = label;
7057 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7059 head_ref_name = got_ref_get_name(ref);
7060 else if (error->code != GOT_ERR_NOT_REF)
7062 error = got_repo_match_object_id(&commit_id, NULL,
7063 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7068 error = got_object_open_as_commit(&commit, repo, commit_id);
7072 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7074 error = got_error_from_errno("view_open");
7077 error = open_tree_view(view, commit_id, head_ref_name, repo);
7080 if (!got_path_is_root_dir(in_repo_path)) {
7081 error = tree_view_walk_path(&view->state.tree, commit,
7088 /* Release work tree lock. */
7089 got_worktree_close(worktree);
7092 error = view_loop(view);
7101 const struct got_error *close_err = got_repo_close(repo);
7106 const struct got_error *pack_err =
7107 got_repo_pack_fds_close(pack_fds);
7115 static const struct got_error *
7116 ref_view_load_refs(struct tog_ref_view_state *s)
7118 struct got_reflist_entry *sre;
7119 struct tog_reflist_entry *re;
7122 TAILQ_FOREACH(sre, &tog_refs, entry) {
7123 if (strncmp(got_ref_get_name(sre->ref),
7124 "refs/got/", 9) == 0 &&
7125 strncmp(got_ref_get_name(sre->ref),
7126 "refs/got/backup/", 16) != 0)
7129 re = malloc(sizeof(*re));
7131 return got_error_from_errno("malloc");
7133 re->ref = got_ref_dup(sre->ref);
7134 if (re->ref == NULL)
7135 return got_error_from_errno("got_ref_dup");
7136 re->idx = s->nrefs++;
7137 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7140 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7145 ref_view_free_refs(struct tog_ref_view_state *s)
7147 struct tog_reflist_entry *re;
7149 while (!TAILQ_EMPTY(&s->refs)) {
7150 re = TAILQ_FIRST(&s->refs);
7151 TAILQ_REMOVE(&s->refs, re, entry);
7152 got_ref_close(re->ref);
7157 static const struct got_error *
7158 open_ref_view(struct tog_view *view, struct got_repository *repo)
7160 const struct got_error *err = NULL;
7161 struct tog_ref_view_state *s = &view->state.ref;
7163 s->selected_entry = 0;
7166 TAILQ_INIT(&s->refs);
7167 STAILQ_INIT(&s->colors);
7169 err = ref_view_load_refs(s);
7173 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7174 err = add_color(&s->colors, "^refs/heads/",
7175 TOG_COLOR_REFS_HEADS,
7176 get_color_value("TOG_COLOR_REFS_HEADS"));
7180 err = add_color(&s->colors, "^refs/tags/",
7181 TOG_COLOR_REFS_TAGS,
7182 get_color_value("TOG_COLOR_REFS_TAGS"));
7186 err = add_color(&s->colors, "^refs/remotes/",
7187 TOG_COLOR_REFS_REMOTES,
7188 get_color_value("TOG_COLOR_REFS_REMOTES"));
7192 err = add_color(&s->colors, "^refs/got/backup/",
7193 TOG_COLOR_REFS_BACKUP,
7194 get_color_value("TOG_COLOR_REFS_BACKUP"));
7199 view->show = show_ref_view;
7200 view->input = input_ref_view;
7201 view->close = close_ref_view;
7202 view->search_start = search_start_ref_view;
7203 view->search_next = search_next_ref_view;
7206 free_colors(&s->colors);
7210 static const struct got_error *
7211 close_ref_view(struct tog_view *view)
7213 struct tog_ref_view_state *s = &view->state.ref;
7215 ref_view_free_refs(s);
7216 free_colors(&s->colors);
7221 static const struct got_error *
7222 resolve_reflist_entry(struct got_object_id **commit_id,
7223 struct tog_reflist_entry *re, struct got_repository *repo)
7225 const struct got_error *err = NULL;
7226 struct got_object_id *obj_id;
7227 struct got_tag_object *tag = NULL;
7232 err = got_ref_resolve(&obj_id, repo, re->ref);
7236 err = got_object_get_type(&obj_type, repo, obj_id);
7241 case GOT_OBJ_TYPE_COMMIT:
7242 *commit_id = obj_id;
7244 case GOT_OBJ_TYPE_TAG:
7245 err = got_object_open_as_tag(&tag, repo, obj_id);
7249 err = got_object_get_type(&obj_type, repo,
7250 got_object_tag_get_object_id(tag));
7253 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7254 err = got_error(GOT_ERR_OBJ_TYPE);
7257 *commit_id = got_object_id_dup(
7258 got_object_tag_get_object_id(tag));
7259 if (*commit_id == NULL) {
7260 err = got_error_from_errno("got_object_id_dup");
7265 err = got_error(GOT_ERR_OBJ_TYPE);
7271 got_object_tag_close(tag);
7279 static const struct got_error *
7280 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7281 struct tog_reflist_entry *re, struct got_repository *repo)
7283 struct tog_view *log_view;
7284 const struct got_error *err = NULL;
7285 struct got_object_id *commit_id = NULL;
7289 err = resolve_reflist_entry(&commit_id, re, repo);
7291 if (err->code != GOT_ERR_OBJ_TYPE)
7297 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7298 if (log_view == NULL) {
7299 err = got_error_from_errno("view_open");
7303 err = open_log_view(log_view, commit_id, repo,
7304 got_ref_get_name(re->ref), "", 0);
7307 view_close(log_view);
7309 *new_view = log_view;
7315 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7317 struct tog_reflist_entry *re;
7320 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7323 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7324 while (i++ < maxscroll) {
7327 s->first_displayed_entry = re;
7328 re = TAILQ_PREV(re, tog_reflist_head, entry);
7332 static const struct got_error *
7333 ref_scroll_down(struct tog_view *view, int maxscroll)
7335 struct tog_ref_view_state *s = &view->state.ref;
7336 struct tog_reflist_entry *next, *last;
7339 if (s->first_displayed_entry)
7340 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7342 next = TAILQ_FIRST(&s->refs);
7344 last = s->last_displayed_entry;
7345 while (next && n++ < maxscroll) {
7347 last = TAILQ_NEXT(last, entry);
7348 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7349 s->first_displayed_entry = next;
7350 next = TAILQ_NEXT(next, entry);
7357 static const struct got_error *
7358 search_start_ref_view(struct tog_view *view)
7360 struct tog_ref_view_state *s = &view->state.ref;
7362 s->matched_entry = NULL;
7367 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7369 regmatch_t regmatch;
7371 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7375 static const struct got_error *
7376 search_next_ref_view(struct tog_view *view)
7378 struct tog_ref_view_state *s = &view->state.ref;
7379 struct tog_reflist_entry *re = NULL;
7381 if (!view->searching) {
7382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7386 if (s->matched_entry) {
7387 if (view->searching == TOG_SEARCH_FORWARD) {
7388 if (s->selected_entry)
7389 re = TAILQ_NEXT(s->selected_entry, entry);
7391 re = TAILQ_PREV(s->selected_entry,
7392 tog_reflist_head, entry);
7394 if (s->selected_entry == NULL)
7395 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7397 re = TAILQ_PREV(s->selected_entry,
7398 tog_reflist_head, entry);
7401 if (s->selected_entry)
7402 re = s->selected_entry;
7403 else if (view->searching == TOG_SEARCH_FORWARD)
7404 re = TAILQ_FIRST(&s->refs);
7406 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7411 if (s->matched_entry == NULL) {
7412 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7415 if (view->searching == TOG_SEARCH_FORWARD)
7416 re = TAILQ_FIRST(&s->refs);
7418 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7421 if (match_reflist_entry(re, &view->regex)) {
7422 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7423 s->matched_entry = re;
7427 if (view->searching == TOG_SEARCH_FORWARD)
7428 re = TAILQ_NEXT(re, entry);
7430 re = TAILQ_PREV(re, tog_reflist_head, entry);
7433 if (s->matched_entry) {
7434 s->first_displayed_entry = s->matched_entry;
7441 static const struct got_error *
7442 show_ref_view(struct tog_view *view)
7444 const struct got_error *err = NULL;
7445 struct tog_ref_view_state *s = &view->state.ref;
7446 struct tog_reflist_entry *re;
7449 struct tog_color *tc;
7451 int limit = view->nlines;
7453 werase(view->window);
7456 if (view_is_hsplit_top(view))
7457 --limit; /* border */
7462 re = s->first_displayed_entry;
7464 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7466 return got_error_from_errno("asprintf");
7468 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7473 if (view_needs_focus_indication(view))
7474 wstandout(view->window);
7475 waddwstr(view->window, wline);
7476 if (view_needs_focus_indication(view))
7477 wstandend(view->window);
7482 if (width < view->ncols - 1)
7483 waddch(view->window, '\n');
7488 while (re && limit > 0) {
7490 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7493 struct got_commit_object *ci;
7494 struct got_tag_object *tag;
7495 struct got_object_id *id;
7499 err = got_ref_resolve(&id, s->repo, re->ref);
7502 err = got_object_open_as_tag(&tag, s->repo, id);
7504 if (err->code != GOT_ERR_OBJ_TYPE) {
7508 err = got_object_open_as_commit(&ci, s->repo,
7514 t = got_object_commit_get_committer_time(ci);
7515 got_object_commit_close(ci);
7517 t = got_object_tag_get_tagger_time(tag);
7518 got_object_tag_close(tag);
7521 if (gmtime_r(&t, &tm) == NULL)
7522 return got_error_from_errno("gmtime_r");
7523 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7524 return got_error(GOT_ERR_NO_SPACE);
7526 if (got_ref_is_symbolic(re->ref)) {
7527 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7528 ymd : "", got_ref_get_name(re->ref),
7529 got_ref_get_symref_target(re->ref)) == -1)
7530 return got_error_from_errno("asprintf");
7531 } else if (s->show_ids) {
7532 struct got_object_id *id;
7534 err = got_ref_resolve(&id, s->repo, re->ref);
7537 err = got_object_id_str(&id_str, id);
7542 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7543 got_ref_get_name(re->ref), id_str) == -1) {
7544 err = got_error_from_errno("asprintf");
7551 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7552 got_ref_get_name(re->ref)) == -1)
7553 return got_error_from_errno("asprintf");
7555 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7561 if (n == s->selected) {
7563 wstandout(view->window);
7564 s->selected_entry = re;
7566 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7568 wattr_on(view->window,
7569 COLOR_PAIR(tc->colorpair), NULL);
7570 waddwstr(view->window, wline);
7572 wattr_off(view->window,
7573 COLOR_PAIR(tc->colorpair), NULL);
7574 if (width < view->ncols - 1)
7575 waddch(view->window, '\n');
7576 if (n == s->selected && view->focussed)
7577 wstandend(view->window);
7583 s->last_displayed_entry = re;
7586 re = TAILQ_NEXT(re, entry);
7593 static const struct got_error *
7594 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7595 struct tog_reflist_entry *re, struct got_repository *repo)
7597 const struct got_error *err = NULL;
7598 struct got_object_id *commit_id = NULL;
7599 struct tog_view *tree_view;
7603 err = resolve_reflist_entry(&commit_id, re, repo);
7605 if (err->code != GOT_ERR_OBJ_TYPE)
7612 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7613 if (tree_view == NULL) {
7614 err = got_error_from_errno("view_open");
7618 err = open_tree_view(tree_view, commit_id,
7619 got_ref_get_name(re->ref), repo);
7623 *new_view = tree_view;
7628 static const struct got_error *
7629 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7631 const struct got_error *err = NULL;
7632 struct tog_ref_view_state *s = &view->state.ref;
7633 struct tog_view *log_view, *tree_view;
7634 struct tog_reflist_entry *re;
7635 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7639 s->show_ids = !s->show_ids;
7643 s->show_date = !s->show_date;
7647 s->sort_by_date = !s->sort_by_date;
7649 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7650 got_ref_cmp_by_commit_timestamp_descending :
7651 tog_ref_cmp_by_name, s->repo);
7654 got_reflist_object_id_map_free(tog_refs_idmap);
7655 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7656 &tog_refs, s->repo);
7659 ref_view_free_refs(s);
7660 err = ref_view_load_refs(s);
7665 if (!s->selected_entry)
7667 if (view_is_parent_view(view))
7668 view_get_split(view, &begin_y, &begin_x);
7670 err = log_ref_entry(&log_view, begin_y, begin_x,
7671 s->selected_entry, s->repo);
7675 if (view_is_parent_view(view) &&
7676 view->mode == TOG_VIEW_SPLIT_HRZN) {
7677 err = view_init_hsplit(view, begin_y);
7683 log_view->focussed = 1;
7684 log_view->mode = view->mode;
7685 log_view->nlines = view->lines - begin_y;
7686 if (view_is_parent_view(view)) {
7687 view_transfer_size(log_view, view);
7688 err = view_close_child(view);
7691 err = view_set_child(view, log_view);
7694 view->focus_child = 1;
7696 *new_view = log_view;
7700 if (!s->selected_entry)
7702 if (view_is_parent_view(view))
7703 view_get_split(view, &begin_y, &begin_x);
7704 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7705 s->selected_entry, s->repo);
7706 if (err || tree_view == NULL)
7708 if (view_is_parent_view(view) &&
7709 view->mode == TOG_VIEW_SPLIT_HRZN) {
7710 err = view_init_hsplit(view, begin_y);
7715 tree_view->focussed = 1;
7716 tree_view->mode = view->mode;
7717 tree_view->nlines = view->lines - begin_y;
7718 if (view_is_parent_view(view)) {
7719 view_transfer_size(tree_view, view);
7720 err = view_close_child(view);
7723 err = view_set_child(view, tree_view);
7726 view->focus_child = 1;
7728 *new_view = tree_view;
7734 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7738 int eos = view->nlines - 1;
7740 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7744 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7745 for (n = 0; n < eos; n++) {
7748 s->first_displayed_entry = re;
7749 re = TAILQ_PREV(re, tog_reflist_head, entry);
7752 s->selected = n - 1;
7758 if (s->selected > 0) {
7762 ref_scroll_up(s, 1);
7763 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7773 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7774 s->selected -= MIN(nscroll, s->selected);
7775 ref_scroll_up(s, MAX(0, nscroll));
7776 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7782 if (s->selected < s->ndisplayed - 1) {
7786 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7787 /* can't scroll any further */
7791 ref_scroll_down(view, 1);
7801 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7802 /* can't scroll any further; move cursor down */
7803 if (s->selected < s->ndisplayed - 1)
7804 s->selected += MIN(nscroll,
7805 s->ndisplayed - s->selected - 1);
7806 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7807 s->selected += s->ndisplayed - s->selected - 1;
7811 ref_scroll_down(view, nscroll);
7816 err = tog_load_refs(s->repo, s->sort_by_date);
7819 ref_view_free_refs(s);
7820 err = ref_view_load_refs(s);
7823 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7824 s->selected = view->nlines - 2;
7838 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7843 static const struct got_error *
7844 cmd_ref(int argc, char *argv[])
7846 const struct got_error *error;
7847 struct got_repository *repo = NULL;
7848 struct got_worktree *worktree = NULL;
7849 char *cwd = NULL, *repo_path = NULL;
7851 struct tog_view *view;
7852 int *pack_fds = NULL;
7854 while ((ch = getopt(argc, argv, "r:")) != -1) {
7857 repo_path = realpath(optarg, NULL);
7858 if (repo_path == NULL)
7859 return got_error_from_errno2("realpath",
7874 error = got_repo_pack_fds_open(&pack_fds);
7878 if (repo_path == NULL) {
7879 cwd = getcwd(NULL, 0);
7881 return got_error_from_errno("getcwd");
7882 error = got_worktree_open(&worktree, cwd);
7883 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7887 strdup(got_worktree_get_repo_path(worktree));
7889 repo_path = strdup(cwd);
7890 if (repo_path == NULL) {
7891 error = got_error_from_errno("strdup");
7896 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7902 error = apply_unveil(got_repo_get_path(repo), NULL);
7906 error = tog_load_refs(repo, 0);
7910 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7912 error = got_error_from_errno("view_open");
7916 error = open_ref_view(view, repo);
7921 /* Release work tree lock. */
7922 got_worktree_close(worktree);
7925 error = view_loop(view);
7930 const struct got_error *close_err = got_repo_close(repo);
7935 const struct got_error *pack_err =
7936 got_repo_pack_fds_close(pack_fds);
7945 * If view was scrolled down to move the selected line into view when opening a
7946 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7949 offset_selection_up(struct tog_view *view)
7951 switch (view->type) {
7952 case TOG_VIEW_BLAME: {
7953 struct tog_blame_view_state *s = &view->state.blame;
7954 if (s->first_displayed_line == 1) {
7955 s->selected_line = MAX(s->selected_line - view->offset,
7959 if (s->first_displayed_line > view->offset)
7960 s->first_displayed_line -= view->offset;
7962 s->first_displayed_line = 1;
7963 s->selected_line += view->offset;
7967 log_scroll_up(&view->state.log, view->offset);
7968 view->state.log.selected += view->offset;
7971 ref_scroll_up(&view->state.ref, view->offset);
7972 view->state.ref.selected += view->offset;
7975 tree_scroll_up(&view->state.tree, view->offset);
7976 view->state.tree.selected += view->offset;
7986 * If the selected line is in the section of screen covered by the bottom split,
7987 * scroll down offset lines to move it into view and index its new position.
7989 static const struct got_error *
7990 offset_selection_down(struct tog_view *view)
7992 const struct got_error *err = NULL;
7993 const struct got_error *(*scrolld)(struct tog_view *, int);
7994 int *selected = NULL;
7997 switch (view->type) {
7998 case TOG_VIEW_BLAME: {
7999 struct tog_blame_view_state *s = &view->state.blame;
8002 if (s->selected_line > view->nlines - header) {
8003 offset = abs(view->nlines - s->selected_line - header);
8004 s->first_displayed_line += offset;
8005 s->selected_line -= offset;
8006 view->offset = offset;
8010 case TOG_VIEW_LOG: {
8011 struct tog_log_view_state *s = &view->state.log;
8012 scrolld = &log_scroll_down;
8013 header = view_is_parent_view(view) ? 3 : 2;
8014 selected = &s->selected;
8017 case TOG_VIEW_REF: {
8018 struct tog_ref_view_state *s = &view->state.ref;
8019 scrolld = &ref_scroll_down;
8021 selected = &s->selected;
8024 case TOG_VIEW_TREE: {
8025 struct tog_tree_view_state *s = &view->state.tree;
8026 scrolld = &tree_scroll_down;
8028 selected = &s->selected;
8038 if (selected && *selected > view->nlines - header) {
8039 offset = abs(view->nlines - *selected - header);
8040 view->offset = offset;
8041 if (scrolld && offset) {
8042 err = scrolld(view, offset);
8043 *selected -= offset;
8051 list_commands(FILE *fp)
8055 fprintf(fp, "commands:");
8056 for (i = 0; i < nitems(tog_commands); i++) {
8057 const struct tog_cmd *cmd = &tog_commands[i];
8058 fprintf(fp, " %s", cmd->name);
8064 usage(int hflag, int status)
8066 FILE *fp = (status == 0) ? stdout : stderr;
8068 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8071 fprintf(fp, "lazy usage: %s path\n", getprogname());
8078 make_argv(int argc, ...)
8086 argv = calloc(argc, sizeof(char *));
8089 for (i = 0; i < argc; i++) {
8090 argv[i] = strdup(va_arg(ap, char *));
8091 if (argv[i] == NULL)
8100 * Try to convert 'tog path' into a 'tog log path' command.
8101 * The user could simply have mistyped the command rather than knowingly
8102 * provided a path. So check whether argv[0] can in fact be resolved
8103 * to a path in the HEAD commit and print a special error if not.
8104 * This hack is for mpi@ <3
8106 static const struct got_error *
8107 tog_log_with_path(int argc, char *argv[])
8109 const struct got_error *error = NULL, *close_err;
8110 const struct tog_cmd *cmd = NULL;
8111 struct got_repository *repo = NULL;
8112 struct got_worktree *worktree = NULL;
8113 struct got_object_id *commit_id = NULL, *id = NULL;
8114 struct got_commit_object *commit = NULL;
8115 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8116 char *commit_id_str = NULL, **cmd_argv = NULL;
8117 int *pack_fds = NULL;
8119 cwd = getcwd(NULL, 0);
8121 return got_error_from_errno("getcwd");
8123 error = got_repo_pack_fds_open(&pack_fds);
8127 error = got_worktree_open(&worktree, cwd);
8128 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8132 repo_path = strdup(got_worktree_get_repo_path(worktree));
8134 repo_path = strdup(cwd);
8135 if (repo_path == NULL) {
8136 error = got_error_from_errno("strdup");
8140 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8144 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8149 error = tog_load_refs(repo, 0);
8152 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8153 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8154 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8159 got_worktree_close(worktree);
8163 error = got_object_open_as_commit(&commit, repo, commit_id);
8167 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8169 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8171 fprintf(stderr, "%s: '%s' is no known command or path\n",
8172 getprogname(), argv[0]);
8177 close_err = got_repo_close(repo);
8182 error = got_object_id_str(&commit_id_str, commit_id);
8186 cmd = &tog_commands[0]; /* log */
8188 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8189 error = cmd->cmd_main(argc, cmd_argv);
8192 close_err = got_repo_close(repo);
8197 got_object_commit_close(commit);
8199 got_worktree_close(worktree);
8201 const struct got_error *pack_err =
8202 got_repo_pack_fds_close(pack_fds);
8207 free(commit_id_str);
8214 for (i = 0; i < argc; i++)
8223 main(int argc, char *argv[])
8225 const struct got_error *error = NULL;
8226 const struct tog_cmd *cmd = NULL;
8227 int ch, hflag = 0, Vflag = 0;
8228 char **cmd_argv = NULL;
8229 static const struct option longopts[] = {
8230 { "version", no_argument, NULL, 'V' },
8233 char *diff_algo_str = NULL;
8235 setlocale(LC_CTYPE, "");
8237 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8257 got_version_print_str();
8262 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8270 /* Build an argument vector which runs a default command. */
8271 cmd = &tog_commands[0];
8273 cmd_argv = make_argv(argc, cmd->name);
8277 /* Did the user specify a command? */
8278 for (i = 0; i < nitems(tog_commands); i++) {
8279 if (strncmp(tog_commands[i].name, argv[0],
8280 strlen(argv[0])) == 0) {
8281 cmd = &tog_commands[i];
8287 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8288 if (diff_algo_str) {
8289 if (strcasecmp(diff_algo_str, "patience") == 0)
8290 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8291 if (strcasecmp(diff_algo_str, "myers") == 0)
8292 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8298 /* No command specified; try log with a path */
8299 error = tog_log_with_path(argc, argv);
8304 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8311 for (i = 0; i < argc; i++)
8316 if (error && error->code != GOT_ERR_CANCELLED)
8317 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);