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 int y = v->child->begin_y;
970 if (v->child->resized_y)
971 v->child->begin_y = v->child->resized_y;
973 v->child->begin_y -= resize;
975 v->child->begin_y += resize;
976 if (v->child->begin_y < 3) {
978 v->child->begin_y = 3;
979 } else if (v->child->begin_y > LINES - 1) {
981 v->child->begin_y = LINES - 1;
984 v->child->ncols = COLS;
985 view_adjust_offset(view, resize);
986 err = view_init_hsplit(v, v->child->begin_y);
989 v->child->resized_y = v->child->begin_y;
990 if (y > v->child->begin_y) /* split increased */
991 v->child->nscrolled = y - v->child->begin_y;
993 if (v->child->resized_x)
994 v->child->begin_x = v->child->resized_x;
996 v->child->begin_x -= resize;
998 v->child->begin_x += resize;
999 if (v->child->begin_x < 11) {
1001 v->child->begin_x = 11;
1002 } else if (v->child->begin_x > COLS - 1) {
1004 v->child->begin_x = COLS - 1;
1006 v->child->resized_x = v->child->begin_x;
1009 v->child->mode = v->mode;
1010 v->child->nlines = v->lines - v->child->begin_y;
1011 v->child->ncols = v->cols - v->child->begin_x;
1014 err = view_fullscreen(v);
1017 err = view_splitscreen(v->child);
1021 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1022 err = offset_selection_down(v->child);
1027 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1028 err = request_log_commits(v);
1029 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1030 err = request_log_commits(v->child);
1032 v->resize = v->child->resize = 0;
1038 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1040 struct tog_view *v = src->child ? src->child : src;
1042 dst->resized_x = v->resized_x;
1043 dst->resized_y = v->resized_y;
1046 static const struct got_error *
1047 view_close_child(struct tog_view *view)
1049 const struct got_error *err = NULL;
1051 if (view->child == NULL)
1054 err = view_close(view->child);
1059 static const struct got_error *
1060 view_set_child(struct tog_view *view, struct tog_view *child)
1062 const struct got_error *err = NULL;
1064 view->child = child;
1065 child->parent = view;
1067 err = view_resize(view);
1071 if (view->child->resized_x || view->child->resized_y)
1072 err = view_resize_split(view, 0);
1078 tog_resizeterm(void)
1081 struct winsize size;
1083 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1084 cols = 80; /* Default */
1088 lines = size.ws_row;
1090 resize_term(lines, cols);
1093 static const struct got_error *
1094 view_search_start(struct tog_view *view)
1096 const struct got_error *err = NULL;
1097 struct tog_view *v = view;
1101 if (view->search_started) {
1102 regfree(&view->regex);
1103 view->searching = 0;
1104 memset(&view->regmatch, 0, sizeof(view->regmatch));
1106 view->search_started = 0;
1108 if (view->nlines < 1)
1111 if (view_is_hsplit_top(view))
1114 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1115 wclrtoeol(v->window);
1117 nodelay(view->window, FALSE); /* block for search term input */
1120 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1121 wrefresh(v->window);
1124 nodelay(view->window, TRUE);
1128 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1129 err = view->search_start(view);
1131 regfree(&view->regex);
1134 view->search_started = 1;
1135 view->searching = TOG_SEARCH_FORWARD;
1136 view->search_next_done = 0;
1137 view->search_next(view);
1143 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1144 static const struct got_error *
1145 switch_split(struct tog_view *view)
1147 const struct got_error *err = NULL;
1148 struct tog_view *v = NULL;
1155 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1156 v->mode = TOG_VIEW_SPLIT_VERT;
1158 v->mode = TOG_VIEW_SPLIT_HRZN;
1162 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1163 v->mode = TOG_VIEW_SPLIT_NONE;
1165 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1166 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1167 v->child->begin_y = v->child->resized_y;
1168 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1169 v->child->begin_x = v->child->resized_x;
1172 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1174 v->child->ncols = COLS;
1175 v->child->nscrolled = LINES - v->child->nlines;
1177 err = view_init_hsplit(v, v->child->begin_y);
1181 v->child->mode = v->mode;
1182 v->child->nlines = v->lines - v->child->begin_y;
1185 err = view_fullscreen(v);
1188 err = view_splitscreen(v->child);
1192 if (v->mode == TOG_VIEW_SPLIT_NONE)
1193 v->mode = TOG_VIEW_SPLIT_VERT;
1194 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1195 err = offset_selection_down(v);
1196 err = offset_selection_down(v->child);
1198 offset_selection_up(v);
1199 offset_selection_up(v->child);
1201 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1202 err = request_log_commits(v);
1203 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1204 err = request_log_commits(v->child);
1210 * Compute view->count from numeric input. Assign total to view->count and
1211 * return first non-numeric key entered.
1214 get_compound_key(struct tog_view *view, int c)
1216 struct tog_view *v = view;
1219 if (view_is_hsplit_top(view))
1221 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1225 cbreak(); /* block for input */
1226 wmove(v->window, v->nlines - 1, 0);
1227 wclrtoeol(v->window);
1228 waddch(v->window, ':');
1231 x = getcurx(v->window);
1232 if (x != ERR && x < view->ncols) {
1233 waddch(v->window, c);
1234 wrefresh(v->window);
1238 * Don't overflow. Max valid request should be the greatest
1239 * between the longest and total lines; cap at 10 million.
1244 n = n * 10 + (c - '0');
1245 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1247 /* Massage excessive or inapplicable values at the input handler. */
1253 static const struct got_error *
1254 view_input(struct tog_view **new, int *done, struct tog_view *view,
1255 struct tog_view_list_head *views)
1257 const struct got_error *err = NULL;
1263 /* Clear "no matches" indicator. */
1264 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1265 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1266 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1270 if (view->searching && !view->search_next_done) {
1271 errcode = pthread_mutex_unlock(&tog_mutex);
1273 return got_error_set_errno(errcode,
1274 "pthread_mutex_unlock");
1276 errcode = pthread_mutex_lock(&tog_mutex);
1278 return got_error_set_errno(errcode,
1279 "pthread_mutex_lock");
1280 view->search_next(view);
1284 nodelay(view->window, FALSE);
1285 /* Allow threads to make progress while we are waiting for input. */
1286 errcode = pthread_mutex_unlock(&tog_mutex);
1288 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1289 /* If we have an unfinished count, let C-g or backspace abort. */
1290 if (view->count && --view->count) {
1292 nodelay(view->window, TRUE);
1293 ch = wgetch(view->window);
1294 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1299 ch = wgetch(view->window);
1300 if (ch >= '1' && ch <= '9')
1301 view->ch = ch = get_compound_key(view, ch);
1303 errcode = pthread_mutex_lock(&tog_mutex);
1305 return got_error_set_errno(errcode, "pthread_mutex_lock");
1306 nodelay(view->window, TRUE);
1308 if (tog_sigwinch_received || tog_sigcont_received) {
1310 tog_sigwinch_received = 0;
1311 tog_sigcont_received = 0;
1312 TAILQ_FOREACH(v, views, entry) {
1313 err = view_resize(v);
1316 err = v->input(new, v, KEY_RESIZE);
1320 err = view_resize(v->child);
1323 err = v->child->input(new, v->child,
1327 if (v->child->resized_x || v->child->resized_y) {
1328 err = view_resize_split(v, 0);
1341 view->child->focussed = 1;
1342 view->focus_child = 1;
1343 } else if (view->parent) {
1345 view->parent->focussed = 1;
1346 view->parent->focus_child = 0;
1347 if (!view_is_splitscreen(view)) {
1348 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1349 view->parent->type == TOG_VIEW_LOG) {
1350 err = request_log_commits(view->parent);
1354 offset_selection_up(view->parent);
1355 err = view_fullscreen(view->parent);
1362 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1363 if (view->parent->type == TOG_VIEW_LOG) {
1364 /* might need more commits to fill fullscreen */
1365 err = request_log_commits(view->parent);
1369 offset_selection_up(view->parent);
1371 err = view->input(new, view, ch);
1379 if (view_is_parent_view(view)) {
1380 if (view->child == NULL)
1382 if (view_is_splitscreen(view->child)) {
1384 view->child->focussed = 1;
1385 err = view_fullscreen(view->child);
1387 err = view_splitscreen(view->child);
1389 err = view_resize_split(view, 0);
1393 err = view->child->input(new, view->child,
1396 if (view_is_splitscreen(view)) {
1397 view->parent->focussed = 0;
1399 err = view_fullscreen(view);
1401 err = view_splitscreen(view);
1402 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1403 err = view_resize(view->parent);
1405 err = view_resize_split(view, 0);
1409 err = view->input(new, view, KEY_RESIZE);
1413 if (view->type == TOG_VIEW_LOG) {
1414 err = request_log_commits(view);
1419 err = offset_selection_down(view->parent);
1421 err = offset_selection_down(view);
1425 err = switch_split(view);
1428 err = view_resize_split(view, -1);
1431 err = view_resize_split(view, 1);
1437 if (view->search_start)
1438 view_search_start(view);
1440 err = view->input(new, view, ch);
1444 if (view->search_started && view->search_next) {
1445 view->searching = (ch == 'n' ?
1446 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1447 view->search_next_done = 0;
1448 view->search_next(view);
1450 err = view->input(new, view, ch);
1453 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1454 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1456 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1457 TAILQ_FOREACH(v, views, entry) {
1463 if (v->child && v->child->reset) {
1464 err = v->child->reset(v->child);
1471 err = view->input(new, view, ch);
1479 view_needs_focus_indication(struct tog_view *view)
1481 if (view_is_parent_view(view)) {
1482 if (view->child == NULL || view->child->focussed)
1484 if (!view_is_splitscreen(view->child))
1486 } else if (!view_is_splitscreen(view))
1489 return view->focussed;
1492 static const struct got_error *
1493 view_loop(struct tog_view *view)
1495 const struct got_error *err = NULL;
1496 struct tog_view_list_head views;
1497 struct tog_view *new_view;
1499 int fast_refresh = 10;
1500 int done = 0, errcode;
1502 mode = getenv("TOG_VIEW_SPLIT_MODE");
1503 if (!mode || !(*mode == 'h' || *mode == 'H'))
1504 view->mode = TOG_VIEW_SPLIT_VERT;
1506 view->mode = TOG_VIEW_SPLIT_HRZN;
1508 errcode = pthread_mutex_lock(&tog_mutex);
1510 return got_error_set_errno(errcode, "pthread_mutex_lock");
1513 TAILQ_INSERT_HEAD(&views, view, entry);
1516 err = view->show(view);
1521 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1522 !tog_fatal_signal_received()) {
1523 /* Refresh fast during initialization, then become slower. */
1524 if (fast_refresh && fast_refresh-- == 0)
1525 halfdelay(10); /* switch to once per second */
1527 err = view_input(&new_view, &done, view, &views);
1531 struct tog_view *v, *prev = NULL;
1533 if (view_is_parent_view(view))
1534 prev = TAILQ_PREV(view, tog_view_list_head,
1536 else if (view->parent)
1537 prev = view->parent;
1540 view->parent->child = NULL;
1541 view->parent->focus_child = 0;
1542 /* Restore fullscreen line height. */
1543 view->parent->nlines = view->parent->lines;
1544 err = view_resize(view->parent);
1547 /* Make resized splits persist. */
1548 view_transfer_size(view->parent, view);
1550 TAILQ_REMOVE(&views, view, entry);
1552 err = view_close(view);
1557 TAILQ_FOREACH(v, &views, entry) {
1561 if (view == NULL && new_view == NULL) {
1562 /* No view has focus. Try to pick one. */
1565 else if (!TAILQ_EMPTY(&views)) {
1566 view = TAILQ_LAST(&views,
1567 tog_view_list_head);
1570 if (view->focus_child) {
1571 view->child->focussed = 1;
1579 struct tog_view *v, *t;
1580 /* Only allow one parent view per type. */
1581 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1582 if (v->type != new_view->type)
1584 TAILQ_REMOVE(&views, v, entry);
1585 err = view_close(v);
1590 TAILQ_INSERT_TAIL(&views, new_view, entry);
1594 if (view_is_parent_view(view)) {
1595 if (view->child && view->child->focussed)
1598 if (view->parent && view->parent->focussed)
1599 view = view->parent;
1601 show_panel(view->panel);
1602 if (view->child && view_is_splitscreen(view->child))
1603 show_panel(view->child->panel);
1604 if (view->parent && view_is_splitscreen(view)) {
1605 err = view->parent->show(view->parent);
1609 err = view->show(view);
1613 err = view->child->show(view->child);
1622 while (!TAILQ_EMPTY(&views)) {
1623 const struct got_error *close_err;
1624 view = TAILQ_FIRST(&views);
1625 TAILQ_REMOVE(&views, view, entry);
1626 close_err = view_close(view);
1627 if (close_err && err == NULL)
1631 errcode = pthread_mutex_unlock(&tog_mutex);
1632 if (errcode && err == NULL)
1633 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1643 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1648 /* Create newly allocated wide-character string equivalent to a byte string. */
1649 static const struct got_error *
1650 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1653 const struct got_error *err = NULL;
1656 *wlen = mbstowcs(NULL, s, 0);
1657 if (*wlen == (size_t)-1) {
1659 if (errno != EILSEQ)
1660 return got_error_from_errno("mbstowcs");
1662 /* byte string invalid in current encoding; try to "fix" it */
1663 err = got_mbsavis(&vis, &vislen, s);
1666 *wlen = mbstowcs(NULL, vis, 0);
1667 if (*wlen == (size_t)-1) {
1668 err = got_error_from_errno("mbstowcs"); /* give up */
1673 *ws = calloc(*wlen + 1, sizeof(**ws));
1675 err = got_error_from_errno("calloc");
1679 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1680 err = got_error_from_errno("mbstowcs");
1691 static const struct got_error *
1692 expand_tab(char **ptr, const char *src)
1695 size_t len, n, idx = 0, sz = 0;
1698 n = len = strlen(src);
1699 dst = malloc(n + 1);
1701 return got_error_from_errno("malloc");
1703 while (idx < len && src[idx]) {
1704 const char c = src[idx];
1707 size_t nb = TABSIZE - sz % TABSIZE;
1710 p = realloc(dst, n + nb);
1713 return got_error_from_errno("realloc");
1718 memset(dst + sz, ' ', nb);
1721 dst[sz++] = src[idx];
1731 * Advance at most n columns from wline starting at offset off.
1732 * Return the index to the first character after the span operation.
1733 * Return the combined column width of all spanned wide character in
1737 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1739 int width, i, cols = 0;
1746 for (i = off; wline[i] != L'\0'; ++i) {
1747 if (wline[i] == L'\t')
1748 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1750 width = wcwidth(wline[i]);
1757 if (cols + width > n)
1767 * Format a line for display, ensuring that it won't overflow a width limit.
1768 * With scrolling, the width returned refers to the scrolled version of the
1769 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1771 static const struct got_error *
1772 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1773 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1775 const struct got_error *err = NULL;
1777 wchar_t *wline = NULL;
1786 err = expand_tab(&exstr, line);
1791 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1796 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1798 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1799 wline[wlen - 1] = L'\0';
1802 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1803 wline[wlen - 1] = L'\0';
1807 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1813 *scrollxp = scrollx;
1821 static const struct got_error*
1822 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1823 struct got_object_id *id, struct got_repository *repo)
1825 static const struct got_error *err = NULL;
1826 struct got_reflist_entry *re;
1832 TAILQ_FOREACH(re, refs, entry) {
1833 struct got_tag_object *tag = NULL;
1834 struct got_object_id *ref_id;
1837 name = got_ref_get_name(re->ref);
1838 if (strcmp(name, GOT_REF_HEAD) == 0)
1840 if (strncmp(name, "refs/", 5) == 0)
1842 if (strncmp(name, "got/", 4) == 0 &&
1843 strncmp(name, "got/backup/", 11) != 0)
1845 if (strncmp(name, "heads/", 6) == 0)
1847 if (strncmp(name, "remotes/", 8) == 0) {
1849 s = strstr(name, "/" GOT_REF_HEAD);
1850 if (s != NULL && s[strlen(s)] == '\0')
1853 err = got_ref_resolve(&ref_id, repo, re->ref);
1856 if (strncmp(name, "tags/", 5) == 0) {
1857 err = got_object_open_as_tag(&tag, repo, ref_id);
1859 if (err->code != GOT_ERR_OBJ_TYPE) {
1863 /* Ref points at something other than a tag. */
1868 cmp = got_object_id_cmp(tag ?
1869 got_object_tag_get_object_id(tag) : ref_id, id);
1872 got_object_tag_close(tag);
1876 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1877 s ? ", " : "", name) == -1) {
1878 err = got_error_from_errno("asprintf");
1889 static const struct got_error *
1890 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1895 smallerthan = strchr(author, '<');
1896 if (smallerthan && smallerthan[1] != '\0')
1897 author = smallerthan + 1;
1898 author[strcspn(author, "@>")] = '\0';
1899 return format_line(wauthor, author_width, NULL, author, 0, limit,
1903 static const struct got_error *
1904 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1905 struct got_object_id *id, const size_t date_display_cols,
1906 int author_display_cols)
1908 struct tog_log_view_state *s = &view->state.log;
1909 const struct got_error *err = NULL;
1910 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1911 char *logmsg0 = NULL, *logmsg = NULL;
1912 char *author = NULL;
1913 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1914 int author_width, logmsg_width;
1915 char *newline, *line = NULL;
1916 int col, limit, scrollx;
1917 const int avail = view->ncols;
1919 time_t committer_time;
1920 struct tog_color *tc;
1922 committer_time = got_object_commit_get_committer_time(commit);
1923 if (gmtime_r(&committer_time, &tm) == NULL)
1924 return got_error_from_errno("gmtime_r");
1925 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1926 return got_error(GOT_ERR_NO_SPACE);
1928 if (avail <= date_display_cols)
1929 limit = MIN(sizeof(datebuf) - 1, avail);
1931 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1932 tc = get_color(&s->colors, TOG_COLOR_DATE);
1934 wattr_on(view->window,
1935 COLOR_PAIR(tc->colorpair), NULL);
1936 waddnstr(view->window, datebuf, limit);
1938 wattr_off(view->window,
1939 COLOR_PAIR(tc->colorpair), NULL);
1946 err = got_object_id_str(&id_str, id);
1949 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1951 wattr_on(view->window,
1952 COLOR_PAIR(tc->colorpair), NULL);
1953 wprintw(view->window, "%.8s ", id_str);
1955 wattr_off(view->window,
1956 COLOR_PAIR(tc->colorpair), NULL);
1963 author = strdup(got_object_commit_get_author(commit));
1964 if (author == NULL) {
1965 err = got_error_from_errno("strdup");
1968 err = format_author(&wauthor, &author_width, author, avail - col, col);
1971 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1973 wattr_on(view->window,
1974 COLOR_PAIR(tc->colorpair), NULL);
1975 waddwstr(view->window, wauthor);
1977 wattr_off(view->window,
1978 COLOR_PAIR(tc->colorpair), NULL);
1979 col += author_width;
1980 while (col < avail && author_width < author_display_cols + 2) {
1981 waddch(view->window, ' ');
1988 err = got_object_commit_get_logmsg(&logmsg0, commit);
1992 while (*logmsg == '\n')
1994 newline = strchr(logmsg, '\n');
1997 limit = avail - col;
1998 if (view->child && !view_is_hsplit_top(view) && limit > 0)
1999 limit--; /* for the border */
2000 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2004 waddwstr(view->window, &wlogmsg[scrollx]);
2005 col += MAX(logmsg_width, 0);
2006 while (col < avail) {
2007 waddch(view->window, ' ');
2019 static struct commit_queue_entry *
2020 alloc_commit_queue_entry(struct got_commit_object *commit,
2021 struct got_object_id *id)
2023 struct commit_queue_entry *entry;
2025 entry = calloc(1, sizeof(*entry));
2030 entry->commit = commit;
2035 pop_commit(struct commit_queue *commits)
2037 struct commit_queue_entry *entry;
2039 entry = TAILQ_FIRST(&commits->head);
2040 TAILQ_REMOVE(&commits->head, entry, entry);
2041 got_object_commit_close(entry->commit);
2042 commits->ncommits--;
2043 /* Don't free entry->id! It is owned by the commit graph. */
2048 free_commits(struct commit_queue *commits)
2050 while (!TAILQ_EMPTY(&commits->head))
2051 pop_commit(commits);
2054 static const struct got_error *
2055 match_commit(int *have_match, struct got_object_id *id,
2056 struct got_commit_object *commit, regex_t *regex)
2058 const struct got_error *err = NULL;
2059 regmatch_t regmatch;
2060 char *id_str = NULL, *logmsg = NULL;
2064 err = got_object_id_str(&id_str, id);
2068 err = got_object_commit_get_logmsg(&logmsg, commit);
2072 if (regexec(regex, got_object_commit_get_author(commit), 1,
2073 ®match, 0) == 0 ||
2074 regexec(regex, got_object_commit_get_committer(commit), 1,
2075 ®match, 0) == 0 ||
2076 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2077 regexec(regex, logmsg, 1, ®match, 0) == 0)
2085 static const struct got_error *
2086 queue_commits(struct tog_log_thread_args *a)
2088 const struct got_error *err = NULL;
2091 * We keep all commits open throughout the lifetime of the log
2092 * view in order to avoid having to re-fetch commits from disk
2093 * while updating the display.
2096 struct got_object_id *id;
2097 struct got_commit_object *commit;
2098 struct commit_queue_entry *entry;
2101 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2103 if (err || id == NULL)
2106 err = got_object_open_as_commit(&commit, a->repo, id);
2109 entry = alloc_commit_queue_entry(commit, id);
2110 if (entry == NULL) {
2111 err = got_error_from_errno("alloc_commit_queue_entry");
2115 errcode = pthread_mutex_lock(&tog_mutex);
2117 err = got_error_set_errno(errcode,
2118 "pthread_mutex_lock");
2122 entry->idx = a->commits->ncommits;
2123 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2124 a->commits->ncommits++;
2126 if (*a->searching == TOG_SEARCH_FORWARD &&
2127 !*a->search_next_done) {
2129 err = match_commit(&have_match, id, commit, a->regex);
2133 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2136 errcode = pthread_mutex_unlock(&tog_mutex);
2137 if (errcode && err == NULL)
2138 err = got_error_set_errno(errcode,
2139 "pthread_mutex_unlock");
2142 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2148 select_commit(struct tog_log_view_state *s)
2150 struct commit_queue_entry *entry;
2153 entry = s->first_displayed_entry;
2155 if (ncommits == s->selected) {
2156 s->selected_entry = entry;
2159 entry = TAILQ_NEXT(entry, entry);
2164 static const struct got_error *
2165 draw_commits(struct tog_view *view)
2167 const struct got_error *err = NULL;
2168 struct tog_log_view_state *s = &view->state.log;
2169 struct commit_queue_entry *entry = s->selected_entry;
2170 const int limit = view->nlines;
2172 int ncommits, author_cols = 4;
2173 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2174 char *refs_str = NULL;
2176 struct tog_color *tc;
2177 static const size_t date_display_cols = 12;
2179 if (s->selected_entry &&
2180 !(view->searching && view->search_next_done == 0)) {
2181 struct got_reflist_head *refs;
2182 err = got_object_id_str(&id_str, s->selected_entry->id);
2185 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2186 s->selected_entry->id);
2188 err = build_refs_str(&refs_str, refs,
2189 s->selected_entry->id, s->repo);
2195 if (s->thread_args.commits_needed == 0)
2196 halfdelay(10); /* disable fast refresh */
2198 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2199 if (asprintf(&ncommits_str, " [%d/%d] %s",
2200 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2201 (view->searching && !view->search_next_done) ?
2202 "searching..." : "loading...") == -1) {
2203 err = got_error_from_errno("asprintf");
2207 const char *search_str = NULL;
2209 if (view->searching) {
2210 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2211 search_str = "no more matches";
2212 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2213 search_str = "no matches found";
2214 else if (!view->search_next_done)
2215 search_str = "searching...";
2218 if (asprintf(&ncommits_str, " [%d/%d] %s",
2219 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2220 search_str ? search_str :
2221 (refs_str ? refs_str : "")) == -1) {
2222 err = got_error_from_errno("asprintf");
2227 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2228 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2229 "........................................",
2230 s->in_repo_path, ncommits_str) == -1) {
2231 err = got_error_from_errno("asprintf");
2235 } else if (asprintf(&header, "commit %s%s",
2236 id_str ? id_str : "........................................",
2237 ncommits_str) == -1) {
2238 err = got_error_from_errno("asprintf");
2242 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2246 werase(view->window);
2248 if (view_needs_focus_indication(view))
2249 wstandout(view->window);
2250 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2252 wattr_on(view->window,
2253 COLOR_PAIR(tc->colorpair), NULL);
2254 waddwstr(view->window, wline);
2256 wattr_off(view->window,
2257 COLOR_PAIR(tc->colorpair), NULL);
2258 while (width < view->ncols) {
2259 waddch(view->window, ' ');
2262 if (view_needs_focus_indication(view))
2263 wstandend(view->window);
2268 /* Grow author column size if necessary, and set view->maxx. */
2269 entry = s->first_displayed_entry;
2273 char *author, *eol, *msg, *msg0;
2274 wchar_t *wauthor, *wmsg;
2276 if (ncommits >= limit - 1)
2278 author = strdup(got_object_commit_get_author(entry->commit));
2279 if (author == NULL) {
2280 err = got_error_from_errno("strdup");
2283 err = format_author(&wauthor, &width, author, COLS,
2285 if (author_cols < width)
2286 author_cols = width;
2289 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2293 while (*msg == '\n')
2295 if ((eol = strchr(msg, '\n')))
2297 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2298 date_display_cols + author_cols, 0);
2301 view->maxx = MAX(view->maxx, width);
2305 entry = TAILQ_NEXT(entry, entry);
2308 entry = s->first_displayed_entry;
2309 s->last_displayed_entry = s->first_displayed_entry;
2312 if (ncommits >= limit - 1)
2314 if (ncommits == s->selected)
2315 wstandout(view->window);
2316 err = draw_commit(view, entry->commit, entry->id,
2317 date_display_cols, author_cols);
2318 if (ncommits == s->selected)
2319 wstandend(view->window);
2323 s->last_displayed_entry = entry;
2324 entry = TAILQ_NEXT(entry, entry);
2337 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2339 struct commit_queue_entry *entry;
2342 entry = TAILQ_FIRST(&s->commits.head);
2343 if (s->first_displayed_entry == entry)
2346 entry = s->first_displayed_entry;
2347 while (entry && nscrolled < maxscroll) {
2348 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2350 s->first_displayed_entry = entry;
2356 static const struct got_error *
2357 trigger_log_thread(struct tog_view *view, int wait)
2359 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2362 halfdelay(1); /* fast refresh while loading commits */
2364 while (!ta->log_complete && !tog_thread_error &&
2365 (ta->commits_needed > 0 || ta->load_all)) {
2366 /* Wake the log thread. */
2367 errcode = pthread_cond_signal(&ta->need_commits);
2369 return got_error_set_errno(errcode,
2370 "pthread_cond_signal");
2373 * The mutex will be released while the view loop waits
2374 * in wgetch(), at which time the log thread will run.
2379 /* Display progress update in log view. */
2380 show_log_view(view);
2384 /* Wait right here while next commit is being loaded. */
2385 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2387 return got_error_set_errno(errcode,
2388 "pthread_cond_wait");
2390 /* Display progress update in log view. */
2391 show_log_view(view);
2399 static const struct got_error *
2400 request_log_commits(struct tog_view *view)
2402 struct tog_log_view_state *state = &view->state.log;
2403 const struct got_error *err = NULL;
2405 state->thread_args.commits_needed += view->nscrolled;
2406 err = trigger_log_thread(view, 1);
2407 view->nscrolled = 0;
2412 static const struct got_error *
2413 log_scroll_down(struct tog_view *view, int maxscroll)
2415 struct tog_log_view_state *s = &view->state.log;
2416 const struct got_error *err = NULL;
2417 struct commit_queue_entry *pentry;
2418 int nscrolled = 0, ncommits_needed;
2420 if (s->last_displayed_entry == NULL)
2423 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2424 if (s->commits.ncommits < ncommits_needed &&
2425 !s->thread_args.log_complete) {
2427 * Ask the log thread for required amount of commits.
2429 s->thread_args.commits_needed += maxscroll;
2430 err = trigger_log_thread(view, 1);
2436 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2437 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2440 s->last_displayed_entry = pentry ?
2441 pentry : s->last_displayed_entry;;
2443 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2446 s->first_displayed_entry = pentry;
2447 } while (++nscrolled < maxscroll);
2449 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2450 view->nscrolled += nscrolled;
2452 view->nscrolled = 0;
2457 static const struct got_error *
2458 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2459 struct got_commit_object *commit, struct got_object_id *commit_id,
2460 struct tog_view *log_view, struct got_repository *repo)
2462 const struct got_error *err;
2463 struct got_object_qid *parent_id;
2464 struct tog_view *diff_view;
2466 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2467 if (diff_view == NULL)
2468 return got_error_from_errno("view_open");
2470 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2471 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2472 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2474 *new_view = diff_view;
2478 static const struct got_error *
2479 tree_view_visit_subtree(struct tog_tree_view_state *s,
2480 struct got_tree_object *subtree)
2482 struct tog_parent_tree *parent;
2484 parent = calloc(1, sizeof(*parent));
2486 return got_error_from_errno("calloc");
2488 parent->tree = s->tree;
2489 parent->first_displayed_entry = s->first_displayed_entry;
2490 parent->selected_entry = s->selected_entry;
2491 parent->selected = s->selected;
2492 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2495 s->first_displayed_entry = NULL;
2499 static const struct got_error *
2500 tree_view_walk_path(struct tog_tree_view_state *s,
2501 struct got_commit_object *commit, const char *path)
2503 const struct got_error *err = NULL;
2504 struct got_tree_object *tree = NULL;
2506 char *slash, *subpath = NULL;
2508 /* Walk the path and open corresponding tree objects. */
2511 struct got_tree_entry *te;
2512 struct got_object_id *tree_id;
2518 /* Ensure the correct subtree entry is selected. */
2519 slash = strchr(p, '/');
2521 te_name = strdup(p);
2523 te_name = strndup(p, slash - p);
2524 if (te_name == NULL) {
2525 err = got_error_from_errno("strndup");
2528 te = got_object_tree_find_entry(s->tree, te_name);
2530 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2535 s->first_displayed_entry = s->selected_entry = te;
2537 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2538 break; /* jump to this file's entry */
2540 slash = strchr(p, '/');
2542 subpath = strndup(path, slash - path);
2544 subpath = strdup(path);
2545 if (subpath == NULL) {
2546 err = got_error_from_errno("strdup");
2550 err = got_object_id_by_path(&tree_id, s->repo, commit,
2555 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2560 err = tree_view_visit_subtree(s, tree);
2562 got_object_tree_close(tree);
2576 static const struct got_error *
2577 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2578 struct commit_queue_entry *entry, const char *path,
2579 const char *head_ref_name, struct got_repository *repo)
2581 const struct got_error *err = NULL;
2582 struct tog_tree_view_state *s;
2583 struct tog_view *tree_view;
2585 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2586 if (tree_view == NULL)
2587 return got_error_from_errno("view_open");
2589 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2592 s = &tree_view->state.tree;
2594 *new_view = tree_view;
2596 if (got_path_is_root_dir(path))
2599 return tree_view_walk_path(s, entry->commit, path);
2602 static const struct got_error *
2603 block_signals_used_by_main_thread(void)
2608 if (sigemptyset(&sigset) == -1)
2609 return got_error_from_errno("sigemptyset");
2611 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2612 if (sigaddset(&sigset, SIGWINCH) == -1)
2613 return got_error_from_errno("sigaddset");
2614 if (sigaddset(&sigset, SIGCONT) == -1)
2615 return got_error_from_errno("sigaddset");
2616 if (sigaddset(&sigset, SIGINT) == -1)
2617 return got_error_from_errno("sigaddset");
2618 if (sigaddset(&sigset, SIGTERM) == -1)
2619 return got_error_from_errno("sigaddset");
2621 /* ncurses handles SIGTSTP */
2622 if (sigaddset(&sigset, SIGTSTP) == -1)
2623 return got_error_from_errno("sigaddset");
2625 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2627 return got_error_set_errno(errcode, "pthread_sigmask");
2633 log_thread(void *arg)
2635 const struct got_error *err = NULL;
2637 struct tog_log_thread_args *a = arg;
2641 * Sync startup with main thread such that we begin our
2642 * work once view_input() has released the mutex.
2644 errcode = pthread_mutex_lock(&tog_mutex);
2646 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2650 err = block_signals_used_by_main_thread();
2652 pthread_mutex_unlock(&tog_mutex);
2656 while (!done && !err && !tog_fatal_signal_received()) {
2657 errcode = pthread_mutex_unlock(&tog_mutex);
2659 err = got_error_set_errno(errcode,
2660 "pthread_mutex_unlock");
2663 err = queue_commits(a);
2665 if (err->code != GOT_ERR_ITER_COMPLETED)
2669 } else if (a->commits_needed > 0 && !a->load_all)
2670 a->commits_needed--;
2672 errcode = pthread_mutex_lock(&tog_mutex);
2674 err = got_error_set_errno(errcode,
2675 "pthread_mutex_lock");
2677 } else if (*a->quit)
2679 else if (*a->first_displayed_entry == NULL) {
2680 *a->first_displayed_entry =
2681 TAILQ_FIRST(&a->commits->head);
2682 *a->selected_entry = *a->first_displayed_entry;
2685 errcode = pthread_cond_signal(&a->commit_loaded);
2687 err = got_error_set_errno(errcode,
2688 "pthread_cond_signal");
2689 pthread_mutex_unlock(&tog_mutex);
2694 a->commits_needed = 0;
2696 if (a->commits_needed == 0 && !a->load_all) {
2697 errcode = pthread_cond_wait(&a->need_commits,
2700 err = got_error_set_errno(errcode,
2701 "pthread_cond_wait");
2702 pthread_mutex_unlock(&tog_mutex);
2710 a->log_complete = 1;
2711 errcode = pthread_mutex_unlock(&tog_mutex);
2713 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2716 tog_thread_error = 1;
2717 pthread_cond_signal(&a->commit_loaded);
2722 static const struct got_error *
2723 stop_log_thread(struct tog_log_view_state *s)
2725 const struct got_error *err = NULL, *thread_err = NULL;
2730 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2732 return got_error_set_errno(errcode,
2733 "pthread_cond_signal");
2734 errcode = pthread_mutex_unlock(&tog_mutex);
2736 return got_error_set_errno(errcode,
2737 "pthread_mutex_unlock");
2738 errcode = pthread_join(s->thread, (void **)&thread_err);
2740 return got_error_set_errno(errcode, "pthread_join");
2741 errcode = pthread_mutex_lock(&tog_mutex);
2743 return got_error_set_errno(errcode,
2744 "pthread_mutex_lock");
2748 if (s->thread_args.repo) {
2749 err = got_repo_close(s->thread_args.repo);
2750 s->thread_args.repo = NULL;
2753 if (s->thread_args.pack_fds) {
2754 const struct got_error *pack_err =
2755 got_repo_pack_fds_close(s->thread_args.pack_fds);
2758 s->thread_args.pack_fds = NULL;
2761 if (s->thread_args.graph) {
2762 got_commit_graph_close(s->thread_args.graph);
2763 s->thread_args.graph = NULL;
2766 return err ? err : thread_err;
2769 static const struct got_error *
2770 close_log_view(struct tog_view *view)
2772 const struct got_error *err = NULL;
2773 struct tog_log_view_state *s = &view->state.log;
2776 err = stop_log_thread(s);
2778 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2779 if (errcode && err == NULL)
2780 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2782 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2783 if (errcode && err == NULL)
2784 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2786 free_commits(&s->commits);
2787 free(s->in_repo_path);
2788 s->in_repo_path = NULL;
2791 free(s->head_ref_name);
2792 s->head_ref_name = NULL;
2796 static const struct got_error *
2797 search_start_log_view(struct tog_view *view)
2799 struct tog_log_view_state *s = &view->state.log;
2801 s->matched_entry = NULL;
2802 s->search_entry = NULL;
2806 static const struct got_error *
2807 search_next_log_view(struct tog_view *view)
2809 const struct got_error *err = NULL;
2810 struct tog_log_view_state *s = &view->state.log;
2811 struct commit_queue_entry *entry;
2813 /* Display progress update in log view. */
2814 show_log_view(view);
2818 if (s->search_entry) {
2820 errcode = pthread_mutex_unlock(&tog_mutex);
2822 return got_error_set_errno(errcode,
2823 "pthread_mutex_unlock");
2824 ch = wgetch(view->window);
2825 errcode = pthread_mutex_lock(&tog_mutex);
2827 return got_error_set_errno(errcode,
2828 "pthread_mutex_lock");
2829 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2830 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2833 if (view->searching == TOG_SEARCH_FORWARD)
2834 entry = TAILQ_NEXT(s->search_entry, entry);
2836 entry = TAILQ_PREV(s->search_entry,
2837 commit_queue_head, entry);
2838 } else if (s->matched_entry) {
2839 int matched_idx = s->matched_entry->idx;
2840 int selected_idx = s->selected_entry->idx;
2843 * If the user has moved the cursor after we hit a match,
2844 * the position from where we should continue searching
2845 * might have changed.
2847 if (view->searching == TOG_SEARCH_FORWARD) {
2848 if (matched_idx > selected_idx)
2849 entry = TAILQ_NEXT(s->selected_entry, entry);
2851 entry = TAILQ_NEXT(s->matched_entry, entry);
2853 if (matched_idx < selected_idx)
2854 entry = TAILQ_PREV(s->selected_entry,
2855 commit_queue_head, entry);
2857 entry = TAILQ_PREV(s->matched_entry,
2858 commit_queue_head, entry);
2861 entry = s->selected_entry;
2867 if (entry == NULL) {
2868 if (s->thread_args.log_complete ||
2869 view->searching == TOG_SEARCH_BACKWARD) {
2870 view->search_next_done =
2871 (s->matched_entry == NULL ?
2872 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2873 s->search_entry = NULL;
2877 * Poke the log thread for more commits and return,
2878 * allowing the main loop to make progress. Search
2879 * will resume at s->search_entry once we come back.
2881 s->thread_args.commits_needed++;
2882 return trigger_log_thread(view, 0);
2885 err = match_commit(&have_match, entry->id, entry->commit,
2890 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2891 s->matched_entry = entry;
2895 s->search_entry = entry;
2896 if (view->searching == TOG_SEARCH_FORWARD)
2897 entry = TAILQ_NEXT(entry, entry);
2899 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2902 if (s->matched_entry) {
2903 int cur = s->selected_entry->idx;
2904 while (cur < s->matched_entry->idx) {
2905 err = input_log_view(NULL, view, KEY_DOWN);
2910 while (cur > s->matched_entry->idx) {
2911 err = input_log_view(NULL, view, KEY_UP);
2918 s->search_entry = NULL;
2923 static const struct got_error *
2924 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2925 struct got_repository *repo, const char *head_ref_name,
2926 const char *in_repo_path, int log_branches)
2928 const struct got_error *err = NULL;
2929 struct tog_log_view_state *s = &view->state.log;
2930 struct got_repository *thread_repo = NULL;
2931 struct got_commit_graph *thread_graph = NULL;
2934 if (in_repo_path != s->in_repo_path) {
2935 free(s->in_repo_path);
2936 s->in_repo_path = strdup(in_repo_path);
2937 if (s->in_repo_path == NULL)
2938 return got_error_from_errno("strdup");
2941 /* The commit queue only contains commits being displayed. */
2942 TAILQ_INIT(&s->commits.head);
2943 s->commits.ncommits = 0;
2946 if (head_ref_name) {
2947 s->head_ref_name = strdup(head_ref_name);
2948 if (s->head_ref_name == NULL) {
2949 err = got_error_from_errno("strdup");
2953 s->start_id = got_object_id_dup(start_id);
2954 if (s->start_id == NULL) {
2955 err = got_error_from_errno("got_object_id_dup");
2958 s->log_branches = log_branches;
2960 STAILQ_INIT(&s->colors);
2961 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2962 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2963 get_color_value("TOG_COLOR_COMMIT"));
2966 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2967 get_color_value("TOG_COLOR_AUTHOR"));
2969 free_colors(&s->colors);
2972 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2973 get_color_value("TOG_COLOR_DATE"));
2975 free_colors(&s->colors);
2980 view->show = show_log_view;
2981 view->input = input_log_view;
2982 view->close = close_log_view;
2983 view->search_start = search_start_log_view;
2984 view->search_next = search_next_log_view;
2986 if (s->thread_args.pack_fds == NULL) {
2987 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2991 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2992 s->thread_args.pack_fds);
2995 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2999 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3000 s->repo, NULL, NULL);
3004 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3006 err = got_error_set_errno(errcode, "pthread_cond_init");
3009 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3011 err = got_error_set_errno(errcode, "pthread_cond_init");
3015 s->thread_args.commits_needed = view->nlines;
3016 s->thread_args.graph = thread_graph;
3017 s->thread_args.commits = &s->commits;
3018 s->thread_args.in_repo_path = s->in_repo_path;
3019 s->thread_args.start_id = s->start_id;
3020 s->thread_args.repo = thread_repo;
3021 s->thread_args.log_complete = 0;
3022 s->thread_args.quit = &s->quit;
3023 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3024 s->thread_args.selected_entry = &s->selected_entry;
3025 s->thread_args.searching = &view->searching;
3026 s->thread_args.search_next_done = &view->search_next_done;
3027 s->thread_args.regex = &view->regex;
3030 close_log_view(view);
3034 static const struct got_error *
3035 show_log_view(struct tog_view *view)
3037 const struct got_error *err;
3038 struct tog_log_view_state *s = &view->state.log;
3040 if (s->thread == NULL) {
3041 int errcode = pthread_create(&s->thread, NULL, log_thread,
3044 return got_error_set_errno(errcode, "pthread_create");
3045 if (s->thread_args.commits_needed > 0) {
3046 err = trigger_log_thread(view, 1);
3052 return draw_commits(view);
3056 log_move_cursor_up(struct tog_view *view, int page, int home)
3058 struct tog_log_view_state *s = &view->state.log;
3060 if (s->selected_entry->idx == 0)
3062 if (s->first_displayed_entry == NULL)
3065 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3067 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3069 if (!page && !home && s->selected > 0)
3072 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3078 static const struct got_error *
3079 log_move_cursor_down(struct tog_view *view, int page)
3081 struct tog_log_view_state *s = &view->state.log;
3082 struct commit_queue_entry *first;
3083 const struct got_error *err = NULL;
3085 first = s->first_displayed_entry;
3086 if (first == NULL) {
3091 if (s->thread_args.log_complete &&
3092 s->selected_entry->idx >= s->commits.ncommits - 1)
3096 int eos = view->nlines - 2;
3098 if (view_is_hsplit_top(view))
3099 --eos; /* border consumes the last line */
3100 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3103 err = log_scroll_down(view, 1);
3104 } else if (s->thread_args.load_all) {
3105 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3106 s->selected += MIN(s->last_displayed_entry->idx -
3107 s->selected_entry->idx, page + 1);
3109 err = log_scroll_down(view, MIN(page,
3110 s->commits.ncommits - s->selected_entry->idx - 1));
3111 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3113 err = log_scroll_down(view, page);
3116 if (first == s->first_displayed_entry && s->selected <
3117 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3118 s->selected = MIN(s->commits.ncommits - 1, page);
3125 * We might necessarily overshoot in horizontal
3126 * splits; if so, select the last displayed commit.
3128 s->selected = MIN(s->selected,
3129 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3133 if (s->thread_args.log_complete &&
3134 s->selected_entry->idx == s->commits.ncommits - 1)
3141 view_get_split(struct tog_view *view, int *y, int *x)
3146 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3147 if (view->child && view->child->resized_y)
3148 *y = view->child->resized_y;
3149 else if (view->resized_y)
3150 *y = view->resized_y;
3152 *y = view_split_begin_y(view->lines);
3153 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3154 if (view->child && view->child->resized_x)
3155 *x = view->child->resized_x;
3156 else if (view->resized_x)
3157 *x = view->resized_x;
3159 *x = view_split_begin_x(view->begin_x);
3163 /* Split view horizontally at y and offset view->state->selected line. */
3164 static const struct got_error *
3165 view_init_hsplit(struct tog_view *view, int y)
3167 const struct got_error *err = NULL;
3171 err = view_resize(view);
3175 err = offset_selection_down(view);
3180 static const struct got_error *
3181 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3183 const struct got_error *err = NULL;
3184 struct tog_log_view_state *s = &view->state.log;
3185 struct tog_view *diff_view = NULL, *tree_view = NULL;
3186 struct tog_view *ref_view = NULL;
3187 struct commit_queue_entry *entry;
3188 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3190 if (s->thread_args.load_all) {
3191 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3192 s->thread_args.load_all = 0;
3193 else if (s->thread_args.log_complete) {
3194 err = log_move_cursor_down(view, s->commits.ncommits);
3195 s->thread_args.load_all = 0;
3200 eos = nscroll = view->nlines - 1;
3201 if (view_is_hsplit_top(view))
3212 view->x = MAX(view->maxx - view->ncols / 2, 0);
3217 if (view->x + view->ncols / 2 < view->maxx)
3218 view->x += 2; /* move two columns right */
3224 view->x -= MIN(view->x, 2); /* move two columns back */
3233 log_move_cursor_up(view, 0, 0);
3237 log_move_cursor_up(view, 0, 1);
3247 log_move_cursor_up(view, nscroll, 0);
3254 err = log_move_cursor_down(view, 0);
3258 /* We don't know yet how many commits, so we're forced to
3259 * traverse them all. */
3261 if (!s->thread_args.log_complete) {
3262 s->thread_args.load_all = 1;
3263 return trigger_log_thread(view, 0);
3267 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3268 for (n = 0; n < eos; n++) {
3271 s->first_displayed_entry = entry;
3272 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3275 s->selected = n - 1;
3287 err = log_move_cursor_down(view, nscroll);
3290 if (s->selected > view->nlines - 2)
3291 s->selected = view->nlines - 2;
3292 if (s->selected > s->commits.ncommits - 1)
3293 s->selected = s->commits.ncommits - 1;
3295 if (s->commits.ncommits < view->nlines - 1 &&
3296 !s->thread_args.log_complete) {
3297 s->thread_args.commits_needed += (view->nlines - 1) -
3298 s->commits.ncommits;
3299 err = trigger_log_thread(view, 1);
3305 if (s->selected_entry == NULL)
3308 /* get dimensions--don't split till initialisation succeeds */
3309 if (view_is_parent_view(view))
3310 view_get_split(view, &begin_y, &begin_x);
3312 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3313 s->selected_entry->commit, s->selected_entry->id,
3318 if (view_is_parent_view(view) &&
3319 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3320 err = view_init_hsplit(view, begin_y);
3326 diff_view->focussed = 1;
3327 diff_view->mode = view->mode;
3328 diff_view->nlines = view->lines - begin_y;
3330 if (view_is_parent_view(view)) {
3331 view_transfer_size(diff_view, view);
3332 err = view_close_child(view);
3335 err = view_set_child(view, diff_view);
3338 view->focus_child = 1;
3340 *new_view = diff_view;
3344 if (s->selected_entry == NULL)
3346 if (view_is_parent_view(view))
3347 view_get_split(view, &begin_y, &begin_x);
3348 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3349 s->selected_entry, s->in_repo_path, s->head_ref_name,
3353 if (view_is_parent_view(view) &&
3354 view->mode == TOG_VIEW_SPLIT_HRZN) {
3355 err = view_init_hsplit(view, begin_y);
3360 tree_view->focussed = 1;
3361 tree_view->mode = view->mode;
3362 tree_view->nlines = view->lines - begin_y;
3363 if (view_is_parent_view(view)) {
3364 view_transfer_size(tree_view, view);
3365 err = view_close_child(view);
3368 err = view_set_child(view, tree_view);
3371 view->focus_child = 1;
3373 *new_view = tree_view;
3379 if (ch == KEY_BACKSPACE &&
3380 got_path_is_root_dir(s->in_repo_path))
3382 err = stop_log_thread(s);
3385 if (ch == KEY_BACKSPACE) {
3387 err = got_path_dirname(&parent_path, s->in_repo_path);
3390 free(s->in_repo_path);
3391 s->in_repo_path = parent_path;
3392 s->thread_args.in_repo_path = s->in_repo_path;
3393 } else if (ch == CTRL('l')) {
3394 struct got_object_id *start_id;
3395 err = got_repo_match_object_id(&start_id, NULL,
3396 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3397 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3401 s->start_id = start_id;
3402 s->thread_args.start_id = s->start_id;
3404 s->log_branches = !s->log_branches;
3406 if (s->thread_args.pack_fds == NULL) {
3407 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3411 err = got_repo_open(&s->thread_args.repo,
3412 got_repo_get_path(s->repo), NULL,
3413 s->thread_args.pack_fds);
3417 err = tog_load_refs(s->repo, 0);
3420 err = got_commit_graph_open(&s->thread_args.graph,
3421 s->in_repo_path, !s->log_branches);
3424 err = got_commit_graph_iter_start(s->thread_args.graph,
3425 s->start_id, s->repo, NULL, NULL);
3428 free_commits(&s->commits);
3429 s->first_displayed_entry = NULL;
3430 s->last_displayed_entry = NULL;
3431 s->selected_entry = NULL;
3433 s->thread_args.log_complete = 0;
3435 s->thread_args.commits_needed = view->lines;
3436 s->matched_entry = NULL;
3437 s->search_entry = NULL;
3441 if (view_is_parent_view(view))
3442 view_get_split(view, &begin_y, &begin_x);
3443 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3444 if (ref_view == NULL)
3445 return got_error_from_errno("view_open");
3446 err = open_ref_view(ref_view, s->repo);
3448 view_close(ref_view);
3451 if (view_is_parent_view(view) &&
3452 view->mode == TOG_VIEW_SPLIT_HRZN) {
3453 err = view_init_hsplit(view, begin_y);
3458 ref_view->focussed = 1;
3459 ref_view->mode = view->mode;
3460 ref_view->nlines = view->lines - begin_y;
3461 if (view_is_parent_view(view)) {
3462 view_transfer_size(ref_view, view);
3463 err = view_close_child(view);
3466 err = view_set_child(view, ref_view);
3469 view->focus_child = 1;
3471 *new_view = ref_view;
3481 static const struct got_error *
3482 apply_unveil(const char *repo_path, const char *worktree_path)
3484 const struct got_error *error;
3487 if (unveil("gmon.out", "rwc") != 0)
3488 return got_error_from_errno2("unveil", "gmon.out");
3490 if (repo_path && unveil(repo_path, "r") != 0)
3491 return got_error_from_errno2("unveil", repo_path);
3493 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3494 return got_error_from_errno2("unveil", worktree_path);
3496 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3497 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3499 error = got_privsep_unveil_exec_helpers();
3503 if (unveil(NULL, NULL) != 0)
3504 return got_error_from_errno("unveil");
3513 * Override default signal handlers before starting ncurses.
3514 * This should prevent ncurses from installing its own
3515 * broken cleanup() signal handler.
3517 signal(SIGWINCH, tog_sigwinch);
3518 signal(SIGPIPE, tog_sigpipe);
3519 signal(SIGCONT, tog_sigcont);
3520 signal(SIGINT, tog_sigint);
3521 signal(SIGTERM, tog_sigterm);
3525 halfdelay(1); /* Do fast refresh while initial view is loading. */
3528 intrflush(stdscr, FALSE);
3529 keypad(stdscr, TRUE);
3531 if (getenv("TOG_COLORS") != NULL) {
3533 use_default_colors();
3537 static const struct got_error *
3538 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3539 struct got_repository *repo, struct got_worktree *worktree)
3541 const struct got_error *err = NULL;
3544 *in_repo_path = strdup("/");
3545 if (*in_repo_path == NULL)
3546 return got_error_from_errno("strdup");
3551 const char *prefix = got_worktree_get_path_prefix(worktree);
3554 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3557 if (asprintf(in_repo_path, "%s%s%s", prefix,
3558 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3560 err = got_error_from_errno("asprintf");
3561 *in_repo_path = NULL;
3565 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3570 static const struct got_error *
3571 cmd_log(int argc, char *argv[])
3573 const struct got_error *error;
3574 struct got_repository *repo = NULL;
3575 struct got_worktree *worktree = NULL;
3576 struct got_object_id *start_id = NULL;
3577 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3578 char *start_commit = NULL, *label = NULL;
3579 struct got_reference *ref = NULL;
3580 const char *head_ref_name = NULL;
3581 int ch, log_branches = 0;
3582 struct tog_view *view;
3583 int *pack_fds = NULL;
3585 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3591 start_commit = optarg;
3594 repo_path = realpath(optarg, NULL);
3595 if (repo_path == NULL)
3596 return got_error_from_errno2("realpath",
3611 error = got_repo_pack_fds_open(&pack_fds);
3615 if (repo_path == NULL) {
3616 cwd = getcwd(NULL, 0);
3618 return got_error_from_errno("getcwd");
3619 error = got_worktree_open(&worktree, cwd);
3620 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3624 strdup(got_worktree_get_repo_path(worktree));
3626 repo_path = strdup(cwd);
3627 if (repo_path == NULL) {
3628 error = got_error_from_errno("strdup");
3633 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3637 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3644 error = apply_unveil(got_repo_get_path(repo),
3645 worktree ? got_worktree_get_root_path(worktree) : NULL);
3649 /* already loaded by tog_log_with_path()? */
3650 if (TAILQ_EMPTY(&tog_refs)) {
3651 error = tog_load_refs(repo, 0);
3656 if (start_commit == NULL) {
3657 error = got_repo_match_object_id(&start_id, &label,
3658 worktree ? got_worktree_get_head_ref_name(worktree) :
3659 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3662 head_ref_name = label;
3664 error = got_ref_open(&ref, repo, start_commit, 0);
3666 head_ref_name = got_ref_get_name(ref);
3667 else if (error->code != GOT_ERR_NOT_REF)
3669 error = got_repo_match_object_id(&start_id, NULL,
3670 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3675 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3677 error = got_error_from_errno("view_open");
3680 error = open_log_view(view, start_id, repo, head_ref_name,
3681 in_repo_path, log_branches);
3685 /* Release work tree lock. */
3686 got_worktree_close(worktree);
3689 error = view_loop(view);
3699 const struct got_error *close_err = got_repo_close(repo);
3704 got_worktree_close(worktree);
3706 const struct got_error *pack_err =
3707 got_repo_pack_fds_close(pack_fds);
3719 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3720 "[-w] object1 object2\n", getprogname());
3725 match_line(const char *line, regex_t *regex, size_t nmatch,
3726 regmatch_t *regmatch)
3728 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3731 static struct tog_color *
3732 match_color(struct tog_colors *colors, const char *line)
3734 struct tog_color *tc = NULL;
3736 STAILQ_FOREACH(tc, colors, entry) {
3737 if (match_line(line, &tc->regex, 0, NULL))
3744 static const struct got_error *
3745 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3746 WINDOW *window, int skipcol, regmatch_t *regmatch)
3748 const struct got_error *err = NULL;
3750 wchar_t *wline = NULL;
3751 int rme, rms, n, width, scrollx;
3752 int width0 = 0, width1 = 0, width2 = 0;
3753 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3757 rms = regmatch->rm_so;
3758 rme = regmatch->rm_eo;
3760 err = expand_tab(&exstr, line);
3764 /* Split the line into 3 segments, according to match offsets. */
3765 seg0 = strndup(exstr, rms);
3767 err = got_error_from_errno("strndup");
3770 seg1 = strndup(exstr + rms, rme - rms);
3772 err = got_error_from_errno("strndup");
3775 seg2 = strdup(exstr + rme);
3777 err = got_error_from_errno("strndup");
3781 /* draw up to matched token if we haven't scrolled past it */
3782 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3786 n = MAX(width0 - skipcol, 0);
3789 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3790 wlimit, col_tab_align, 1);
3793 waddwstr(window, &wline[scrollx]);
3803 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3807 wlen = wcslen(wline);
3809 width = wcwidth(wline[i]);
3811 /* should not happen, tabs are expanded */
3812 err = got_error(GOT_ERR_RANGE);
3815 if (width0 + w + width > skipcol)
3820 /* draw (visible part of) matched token (if scrolled into it) */
3821 if (width1 - w > 0) {
3822 wattron(window, A_STANDOUT);
3823 waddwstr(window, &wline[i]);
3824 wattroff(window, A_STANDOUT);
3825 wlimit -= (width1 - w);
3826 *wtotal += (width1 - w);
3830 if (wlimit > 0) { /* draw rest of line */
3832 if (skipcol > width0 + width1) {
3833 err = format_line(&wline, &width2, &scrollx, seg2,
3834 skipcol - (width0 + width1), wlimit,
3838 waddwstr(window, &wline[scrollx]);
3840 err = format_line(&wline, &width2, NULL, seg2, 0,
3841 wlimit, col_tab_align, 1);
3844 waddwstr(window, wline);
3857 static const struct got_error *
3858 draw_file(struct tog_view *view, const char *header)
3860 struct tog_diff_view_state *s = &view->state.diff;
3861 regmatch_t *regmatch = &view->regmatch;
3862 const struct got_error *err;
3865 size_t linesize = 0;
3867 struct tog_color *tc;
3870 int max_lines = view->nlines;
3871 int nlines = s->nlines;
3874 line_offset = s->line_offsets[s->first_displayed_line - 1];
3875 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3876 return got_error_from_errno("fseek");
3878 werase(view->window);
3881 if (asprintf(&line, "[%d/%d] %s",
3882 s->first_displayed_line - 1 + s->selected_line, nlines,
3884 return got_error_from_errno("asprintf");
3885 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3891 if (view_needs_focus_indication(view))
3892 wstandout(view->window);
3893 waddwstr(view->window, wline);
3896 if (view_needs_focus_indication(view))
3897 wstandend(view->window);
3898 if (width <= view->ncols - 1)
3899 waddch(view->window, '\n');
3909 while (max_lines > 0 && nprinted < max_lines) {
3910 linelen = getline(&line, &linesize, s->f);
3911 if (linelen == -1) {
3917 return got_ferror(s->f, GOT_ERR_IO);
3920 /* Set view->maxx based on full line length. */
3921 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3927 view->maxx = MAX(view->maxx, width);
3931 tc = match_color(&s->colors, line);
3933 wattr_on(view->window,
3934 COLOR_PAIR(tc->colorpair), NULL);
3935 if (s->first_displayed_line + nprinted == s->matched_line &&
3936 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3937 err = add_matched_line(&width, line, view->ncols, 0,
3938 view->window, view->x, regmatch);
3945 err = format_line(&wline, &width, &skip, line,
3946 view->x, view->ncols, 0, view->x ? 1 : 0);
3951 waddwstr(view->window, &wline[skip]);
3956 wattr_off(view->window,
3957 COLOR_PAIR(tc->colorpair), NULL);
3958 if (width <= view->ncols - 1)
3959 waddch(view->window, '\n');
3964 s->last_displayed_line = s->first_displayed_line +
3967 s->last_displayed_line = s->first_displayed_line;
3972 while (nprinted < view->nlines) {
3973 waddch(view->window, '\n');
3977 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3983 wstandout(view->window);
3984 waddwstr(view->window, wline);
3987 wstandend(view->window);
3994 get_datestr(time_t *time, char *datebuf)
3996 struct tm mytm, *tm;
3999 tm = gmtime_r(time, &mytm);
4002 s = asctime_r(tm, datebuf);
4005 p = strchr(s, '\n');
4011 static const struct got_error *
4012 get_changed_paths(struct got_pathlist_head *paths,
4013 struct got_commit_object *commit, struct got_repository *repo)
4015 const struct got_error *err = NULL;
4016 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4017 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4018 struct got_object_qid *qid;
4020 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4022 struct got_commit_object *pcommit;
4023 err = got_object_open_as_commit(&pcommit, repo,
4028 tree_id1 = got_object_id_dup(
4029 got_object_commit_get_tree_id(pcommit));
4030 if (tree_id1 == NULL) {
4031 got_object_commit_close(pcommit);
4032 return got_error_from_errno("got_object_id_dup");
4034 got_object_commit_close(pcommit);
4039 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4044 tree_id2 = got_object_commit_get_tree_id(commit);
4045 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4049 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4050 got_diff_tree_collect_changed_paths, paths, 0);
4053 got_object_tree_close(tree1);
4055 got_object_tree_close(tree2);
4060 static const struct got_error *
4061 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4065 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4067 return got_error_from_errno("reallocarray");
4069 (*line_offsets)[*nlines] = off;
4074 static const struct got_error *
4075 write_commit_info(off_t **line_offsets, size_t *nlines,
4076 struct got_object_id *commit_id, struct got_reflist_head *refs,
4077 struct got_repository *repo, FILE *outfile)
4079 const struct got_error *err = NULL;
4080 char datebuf[26], *datestr;
4081 struct got_commit_object *commit;
4082 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4083 time_t committer_time;
4084 const char *author, *committer;
4085 char *refs_str = NULL;
4086 struct got_pathlist_head changed_paths;
4087 struct got_pathlist_entry *pe;
4091 TAILQ_INIT(&changed_paths);
4094 err = build_refs_str(&refs_str, refs, commit_id, repo);
4099 err = got_object_open_as_commit(&commit, repo, commit_id);
4103 err = got_object_id_str(&id_str, commit_id);
4105 err = got_error_from_errno("got_object_id_str");
4109 err = add_line_offset(line_offsets, nlines, 0);
4113 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4114 refs_str ? refs_str : "", refs_str ? ")" : "");
4116 err = got_error_from_errno("fprintf");
4120 err = add_line_offset(line_offsets, nlines, outoff);
4124 n = fprintf(outfile, "from: %s\n",
4125 got_object_commit_get_author(commit));
4127 err = got_error_from_errno("fprintf");
4131 err = add_line_offset(line_offsets, nlines, outoff);
4135 committer_time = got_object_commit_get_committer_time(commit);
4136 datestr = get_datestr(&committer_time, datebuf);
4138 n = fprintf(outfile, "date: %s UTC\n", datestr);
4140 err = got_error_from_errno("fprintf");
4144 err = add_line_offset(line_offsets, nlines, outoff);
4148 author = got_object_commit_get_author(commit);
4149 committer = got_object_commit_get_committer(commit);
4150 if (strcmp(author, committer) != 0) {
4151 n = fprintf(outfile, "via: %s\n", committer);
4153 err = got_error_from_errno("fprintf");
4157 err = add_line_offset(line_offsets, nlines, outoff);
4161 if (got_object_commit_get_nparents(commit) > 1) {
4162 const struct got_object_id_queue *parent_ids;
4163 struct got_object_qid *qid;
4165 parent_ids = got_object_commit_get_parent_ids(commit);
4166 STAILQ_FOREACH(qid, parent_ids, entry) {
4167 err = got_object_id_str(&id_str, &qid->id);
4170 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4172 err = got_error_from_errno("fprintf");
4176 err = add_line_offset(line_offsets, nlines, outoff);
4184 err = got_object_commit_get_logmsg(&logmsg, commit);
4188 while ((line = strsep(&s, "\n")) != NULL) {
4189 n = fprintf(outfile, "%s\n", line);
4191 err = got_error_from_errno("fprintf");
4195 err = add_line_offset(line_offsets, nlines, outoff);
4200 err = get_changed_paths(&changed_paths, commit, repo);
4203 TAILQ_FOREACH(pe, &changed_paths, entry) {
4204 struct got_diff_changed_path *cp = pe->data;
4205 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4207 err = got_error_from_errno("fprintf");
4211 err = add_line_offset(line_offsets, nlines, outoff);
4214 free((char *)pe->path);
4218 fputc('\n', outfile);
4220 err = add_line_offset(line_offsets, nlines, outoff);
4222 got_pathlist_free(&changed_paths);
4226 got_object_commit_close(commit);
4228 free(*line_offsets);
4229 *line_offsets = NULL;
4235 static const struct got_error *
4236 create_diff(struct tog_diff_view_state *s)
4238 const struct got_error *err = NULL;
4242 free(s->line_offsets);
4243 s->line_offsets = malloc(sizeof(off_t));
4244 if (s->line_offsets == NULL)
4245 return got_error_from_errno("malloc");
4250 err = got_error_from_errno("got_opentemp");
4253 if (s->f && fclose(s->f) == EOF) {
4254 err = got_error_from_errno("fclose");
4260 err = got_object_get_type(&obj_type, s->repo, s->id1);
4262 err = got_object_get_type(&obj_type, s->repo, s->id2);
4267 case GOT_OBJ_TYPE_BLOB:
4268 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4269 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4270 s->label1, s->label2, tog_diff_algo, s->diff_context,
4271 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4273 case GOT_OBJ_TYPE_TREE:
4274 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4275 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4276 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4277 s->force_text_diff, s->repo, s->f);
4279 case GOT_OBJ_TYPE_COMMIT: {
4280 const struct got_object_id_queue *parent_ids;
4281 struct got_object_qid *pid;
4282 struct got_commit_object *commit2;
4283 struct got_reflist_head *refs;
4285 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4288 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4289 /* Show commit info if we're diffing to a parent/root commit. */
4290 if (s->id1 == NULL) {
4291 err = write_commit_info(&s->line_offsets, &s->nlines,
4292 s->id2, refs, s->repo, s->f);
4296 parent_ids = got_object_commit_get_parent_ids(commit2);
4297 STAILQ_FOREACH(pid, parent_ids, entry) {
4298 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4299 err = write_commit_info(
4300 &s->line_offsets, &s->nlines,
4301 s->id2, refs, s->repo, s->f);
4308 got_object_commit_close(commit2);
4310 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4311 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4312 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4313 s->force_text_diff, s->repo, s->f);
4317 err = got_error(GOT_ERR_OBJ_TYPE);
4323 if (s->f && fflush(s->f) != 0 && err == NULL)
4324 err = got_error_from_errno("fflush");
4329 diff_view_indicate_progress(struct tog_view *view)
4331 mvwaddstr(view->window, 0, 0, "diffing...");
4336 static const struct got_error *
4337 search_start_diff_view(struct tog_view *view)
4339 struct tog_diff_view_state *s = &view->state.diff;
4341 s->matched_line = 0;
4345 static const struct got_error *
4346 search_next_diff_view(struct tog_view *view)
4348 struct tog_diff_view_state *s = &view->state.diff;
4349 const struct got_error *err = NULL;
4352 size_t linesize = 0;
4355 if (!view->searching) {
4356 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4360 if (s->matched_line) {
4361 if (view->searching == TOG_SEARCH_FORWARD)
4362 lineno = s->matched_line + 1;
4364 lineno = s->matched_line - 1;
4366 lineno = s->first_displayed_line;
4371 if (lineno <= 0 || lineno > s->nlines) {
4372 if (s->matched_line == 0) {
4373 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4377 if (view->searching == TOG_SEARCH_FORWARD)
4383 offset = s->line_offsets[lineno - 1];
4384 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4386 return got_error_from_errno("fseeko");
4388 linelen = getline(&line, &linesize, s->f);
4389 if (linelen != -1) {
4391 err = expand_tab(&exstr, line);
4394 if (match_line(exstr, &view->regex, 1,
4396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4397 s->matched_line = lineno;
4403 if (view->searching == TOG_SEARCH_FORWARD)
4410 if (s->matched_line) {
4411 s->first_displayed_line = s->matched_line;
4412 s->selected_line = 1;
4418 static const struct got_error *
4419 close_diff_view(struct tog_view *view)
4421 const struct got_error *err = NULL;
4422 struct tog_diff_view_state *s = &view->state.diff;
4428 if (s->f && fclose(s->f) == EOF)
4429 err = got_error_from_errno("fclose");
4431 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4432 err = got_error_from_errno("fclose");
4434 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4435 err = got_error_from_errno("fclose");
4437 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4438 err = got_error_from_errno("close");
4440 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4441 err = got_error_from_errno("close");
4443 free_colors(&s->colors);
4444 free(s->line_offsets);
4445 s->line_offsets = NULL;
4450 static const struct got_error *
4451 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4452 struct got_object_id *id2, const char *label1, const char *label2,
4453 int diff_context, int ignore_whitespace, int force_text_diff,
4454 struct tog_view *parent_view, struct got_repository *repo)
4456 const struct got_error *err;
4457 struct tog_diff_view_state *s = &view->state.diff;
4459 memset(s, 0, sizeof(*s));
4463 if (id1 != NULL && id2 != NULL) {
4465 err = got_object_get_type(&type1, repo, id1);
4468 err = got_object_get_type(&type2, repo, id2);
4473 return got_error(GOT_ERR_OBJ_TYPE);
4475 s->first_displayed_line = 1;
4476 s->last_displayed_line = view->nlines;
4477 s->selected_line = 1;
4485 s->id1 = got_object_id_dup(id1);
4487 return got_error_from_errno("got_object_id_dup");
4491 s->id2 = got_object_id_dup(id2);
4492 if (s->id2 == NULL) {
4493 err = got_error_from_errno("got_object_id_dup");
4497 s->f1 = got_opentemp();
4498 if (s->f1 == NULL) {
4499 err = got_error_from_errno("got_opentemp");
4503 s->f2 = got_opentemp();
4504 if (s->f2 == NULL) {
4505 err = got_error_from_errno("got_opentemp");
4509 s->fd1 = got_opentempfd();
4511 err = got_error_from_errno("got_opentempfd");
4515 s->fd2 = got_opentempfd();
4517 err = got_error_from_errno("got_opentempfd");
4521 s->first_displayed_line = 1;
4522 s->last_displayed_line = view->nlines;
4523 s->diff_context = diff_context;
4524 s->ignore_whitespace = ignore_whitespace;
4525 s->force_text_diff = force_text_diff;
4526 s->parent_view = parent_view;
4529 STAILQ_INIT(&s->colors);
4530 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4531 err = add_color(&s->colors,
4532 "^-", TOG_COLOR_DIFF_MINUS,
4533 get_color_value("TOG_COLOR_DIFF_MINUS"));
4536 err = add_color(&s->colors, "^\\+",
4537 TOG_COLOR_DIFF_PLUS,
4538 get_color_value("TOG_COLOR_DIFF_PLUS"));
4541 err = add_color(&s->colors,
4542 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4543 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4547 err = add_color(&s->colors,
4548 "^(commit [0-9a-f]|parent [0-9]|"
4549 "(blob|file|tree|commit) [-+] |"
4550 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4551 get_color_value("TOG_COLOR_DIFF_META"));
4555 err = add_color(&s->colors,
4556 "^(from|via): ", TOG_COLOR_AUTHOR,
4557 get_color_value("TOG_COLOR_AUTHOR"));
4561 err = add_color(&s->colors,
4562 "^date: ", TOG_COLOR_DATE,
4563 get_color_value("TOG_COLOR_DATE"));
4568 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4569 view_is_splitscreen(view))
4570 show_log_view(parent_view); /* draw border */
4571 diff_view_indicate_progress(view);
4573 err = create_diff(s);
4575 view->show = show_diff_view;
4576 view->input = input_diff_view;
4577 view->reset = reset_diff_view;
4578 view->close = close_diff_view;
4579 view->search_start = search_start_diff_view;
4580 view->search_next = search_next_diff_view;
4583 close_diff_view(view);
4587 static const struct got_error *
4588 show_diff_view(struct tog_view *view)
4590 const struct got_error *err;
4591 struct tog_diff_view_state *s = &view->state.diff;
4592 char *id_str1 = NULL, *id_str2, *header;
4593 const char *label1, *label2;
4596 err = got_object_id_str(&id_str1, s->id1);
4599 label1 = s->label1 ? : id_str1;
4601 label1 = "/dev/null";
4603 err = got_object_id_str(&id_str2, s->id2);
4606 label2 = s->label2 ? : id_str2;
4608 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4609 err = got_error_from_errno("asprintf");
4617 err = draw_file(view, header);
4622 static const struct got_error *
4623 set_selected_commit(struct tog_diff_view_state *s,
4624 struct commit_queue_entry *entry)
4626 const struct got_error *err;
4627 const struct got_object_id_queue *parent_ids;
4628 struct got_commit_object *selected_commit;
4629 struct got_object_qid *pid;
4632 s->id2 = got_object_id_dup(entry->id);
4634 return got_error_from_errno("got_object_id_dup");
4636 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4639 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4641 pid = STAILQ_FIRST(parent_ids);
4642 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4643 got_object_commit_close(selected_commit);
4647 static const struct got_error *
4648 reset_diff_view(struct tog_view *view)
4650 struct tog_diff_view_state *s = &view->state.diff;
4653 wclear(view->window);
4654 s->first_displayed_line = 1;
4655 s->last_displayed_line = view->nlines;
4656 s->matched_line = 0;
4657 diff_view_indicate_progress(view);
4658 return create_diff(s);
4661 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4663 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4666 static const struct got_error *
4667 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4669 const struct got_error *err = NULL;
4670 struct tog_diff_view_state *s = &view->state.diff;
4671 struct tog_log_view_state *ls;
4672 struct commit_queue_entry *old_selected_entry;
4674 size_t linesize = 0;
4676 int i, nscroll = view->nlines - 1, up = 0;
4683 view->x = MAX(view->maxx - view->ncols / 3, 0);
4688 if (view->x + view->ncols / 3 < view->maxx)
4689 view->x += 2; /* move two columns right */
4695 view->x -= MIN(view->x, 2); /* move two columns back */
4702 s->force_text_diff = !s->force_text_diff;
4704 s->ignore_whitespace = !s->ignore_whitespace;
4705 err = reset_diff_view(view);
4709 s->first_displayed_line = 1;
4718 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4724 if (s->first_displayed_line > 1)
4725 s->first_displayed_line--;
4736 if (s->first_displayed_line == 1) {
4741 while (i++ < nscroll && s->first_displayed_line > 1)
4742 s->first_displayed_line--;
4748 s->first_displayed_line++;
4765 while (!s->eof && i++ < nscroll) {
4766 linelen = getline(&line, &linesize, s->f);
4767 s->first_displayed_line++;
4768 if (linelen == -1) {
4772 err = got_ferror(s->f, GOT_ERR_IO);
4779 if (s->diff_context > 0) {
4781 s->matched_line = 0;
4782 diff_view_indicate_progress(view);
4783 err = create_diff(s);
4784 if (s->first_displayed_line + view->nlines - 1 >
4786 s->first_displayed_line = 1;
4787 s->last_displayed_line = view->nlines;
4793 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4795 s->matched_line = 0;
4796 diff_view_indicate_progress(view);
4797 err = create_diff(s);
4807 if (s->parent_view == NULL) {
4811 s->parent_view->count = view->count;
4813 if (s->parent_view->type == TOG_VIEW_LOG) {
4814 ls = &s->parent_view->state.log;
4815 old_selected_entry = ls->selected_entry;
4817 err = input_log_view(NULL, s->parent_view,
4818 up ? KEY_UP : KEY_DOWN);
4821 view->count = s->parent_view->count;
4823 if (old_selected_entry == ls->selected_entry)
4826 err = set_selected_commit(s, ls->selected_entry);
4829 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4830 struct tog_blame_view_state *bs;
4831 struct got_object_id *id, *prev_id;
4833 bs = &s->parent_view->state.blame;
4834 prev_id = get_annotation_for_line(bs->blame.lines,
4835 bs->blame.nlines, bs->last_diffed_line);
4837 err = input_blame_view(&view, s->parent_view,
4838 up ? KEY_UP : KEY_DOWN);
4841 view->count = s->parent_view->count;
4843 if (prev_id == NULL)
4845 id = get_selected_commit_id(bs->blame.lines,
4846 bs->blame.nlines, bs->first_displayed_line,
4851 if (!got_object_id_cmp(prev_id, id))
4854 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4858 s->first_displayed_line = 1;
4859 s->last_displayed_line = view->nlines;
4860 s->matched_line = 0;
4863 diff_view_indicate_progress(view);
4864 err = create_diff(s);
4874 static const struct got_error *
4875 cmd_diff(int argc, char *argv[])
4877 const struct got_error *error = NULL;
4878 struct got_repository *repo = NULL;
4879 struct got_worktree *worktree = NULL;
4880 struct got_object_id *id1 = NULL, *id2 = NULL;
4881 char *repo_path = NULL, *cwd = NULL;
4882 char *id_str1 = NULL, *id_str2 = NULL;
4883 char *label1 = NULL, *label2 = NULL;
4884 int diff_context = 3, ignore_whitespace = 0;
4885 int ch, force_text_diff = 0;
4887 struct tog_view *view;
4888 int *pack_fds = NULL;
4890 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4893 force_text_diff = 1;
4896 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4899 errx(1, "number of context lines is %s: %s",
4903 repo_path = realpath(optarg, NULL);
4904 if (repo_path == NULL)
4905 return got_error_from_errno2("realpath",
4907 got_path_strip_trailing_slashes(repo_path);
4910 ignore_whitespace = 1;
4922 usage_diff(); /* TODO show local worktree changes */
4923 } else if (argc == 2) {
4929 error = got_repo_pack_fds_open(&pack_fds);
4933 if (repo_path == NULL) {
4934 cwd = getcwd(NULL, 0);
4936 return got_error_from_errno("getcwd");
4937 error = got_worktree_open(&worktree, cwd);
4938 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4942 strdup(got_worktree_get_repo_path(worktree));
4944 repo_path = strdup(cwd);
4945 if (repo_path == NULL) {
4946 error = got_error_from_errno("strdup");
4951 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4957 error = apply_unveil(got_repo_get_path(repo), NULL);
4961 error = tog_load_refs(repo, 0);
4965 error = got_repo_match_object_id(&id1, &label1, id_str1,
4966 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4970 error = got_repo_match_object_id(&id2, &label2, id_str2,
4971 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4975 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4977 error = got_error_from_errno("view_open");
4980 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4981 ignore_whitespace, force_text_diff, NULL, repo);
4984 error = view_loop(view);
4991 const struct got_error *close_err = got_repo_close(repo);
4996 got_worktree_close(worktree);
4998 const struct got_error *pack_err =
4999 got_repo_pack_fds_close(pack_fds);
5012 "usage: %s blame [-c commit] [-r repository-path] path\n",
5017 struct tog_blame_line {
5019 struct got_object_id *id;
5022 static const struct got_error *
5023 draw_blame(struct tog_view *view)
5025 struct tog_blame_view_state *s = &view->state.blame;
5026 struct tog_blame *blame = &s->blame;
5027 regmatch_t *regmatch = &view->regmatch;
5028 const struct got_error *err;
5029 int lineno = 0, nprinted = 0;
5031 size_t linesize = 0;
5035 struct tog_blame_line *blame_line;
5036 struct got_object_id *prev_id = NULL;
5038 struct tog_color *tc;
5040 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5045 werase(view->window);
5047 if (asprintf(&line, "commit %s", id_str) == -1) {
5048 err = got_error_from_errno("asprintf");
5053 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5058 if (view_needs_focus_indication(view))
5059 wstandout(view->window);
5060 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5062 wattr_on(view->window,
5063 COLOR_PAIR(tc->colorpair), NULL);
5064 waddwstr(view->window, wline);
5066 wattr_off(view->window,
5067 COLOR_PAIR(tc->colorpair), NULL);
5068 if (view_needs_focus_indication(view))
5069 wstandend(view->window);
5072 if (width < view->ncols - 1)
5073 waddch(view->window, '\n');
5075 if (asprintf(&line, "[%d/%d] %s%s",
5076 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5077 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5079 return got_error_from_errno("asprintf");
5082 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5087 waddwstr(view->window, wline);
5090 if (width < view->ncols - 1)
5091 waddch(view->window, '\n');
5095 while (nprinted < view->nlines - 2) {
5096 linelen = getline(&line, &linesize, blame->f);
5097 if (linelen == -1) {
5098 if (feof(blame->f)) {
5103 return got_ferror(blame->f, GOT_ERR_IO);
5105 if (++lineno < s->first_displayed_line)
5108 /* Set view->maxx based on full line length. */
5109 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5116 view->maxx = MAX(view->maxx, width);
5118 if (nprinted == s->selected_line - 1)
5119 wstandout(view->window);
5121 if (blame->nlines > 0) {
5122 blame_line = &blame->lines[lineno - 1];
5123 if (blame_line->annotated && prev_id &&
5124 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5125 !(nprinted == s->selected_line - 1)) {
5126 waddstr(view->window, " ");
5127 } else if (blame_line->annotated) {
5129 err = got_object_id_str(&id_str,
5135 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5137 wattr_on(view->window,
5138 COLOR_PAIR(tc->colorpair), NULL);
5139 wprintw(view->window, "%.8s", id_str);
5141 wattr_off(view->window,
5142 COLOR_PAIR(tc->colorpair), NULL);
5144 prev_id = blame_line->id;
5146 waddstr(view->window, "........");
5150 waddstr(view->window, "........");
5154 if (nprinted == s->selected_line - 1)
5155 wstandend(view->window);
5156 waddstr(view->window, " ");
5158 if (view->ncols <= 9) {
5160 } else if (s->first_displayed_line + nprinted ==
5162 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5163 err = add_matched_line(&width, line, view->ncols - 9, 9,
5164 view->window, view->x, regmatch);
5172 err = format_line(&wline, &width, &skip, line,
5173 view->x, view->ncols - 9, 9, 1);
5178 waddwstr(view->window, &wline[skip]);
5184 if (width <= view->ncols - 1)
5185 waddch(view->window, '\n');
5186 if (++nprinted == 1)
5187 s->first_displayed_line = lineno;
5190 s->last_displayed_line = lineno;
5197 static const struct got_error *
5198 blame_cb(void *arg, int nlines, int lineno,
5199 struct got_commit_object *commit, struct got_object_id *id)
5201 const struct got_error *err = NULL;
5202 struct tog_blame_cb_args *a = arg;
5203 struct tog_blame_line *line;
5206 if (nlines != a->nlines ||
5207 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5208 return got_error(GOT_ERR_RANGE);
5210 errcode = pthread_mutex_lock(&tog_mutex);
5212 return got_error_set_errno(errcode, "pthread_mutex_lock");
5214 if (*a->quit) { /* user has quit the blame view */
5215 err = got_error(GOT_ERR_ITER_COMPLETED);
5220 goto done; /* no change in this commit */
5222 line = &a->lines[lineno - 1];
5223 if (line->annotated)
5226 line->id = got_object_id_dup(id);
5227 if (line->id == NULL) {
5228 err = got_error_from_errno("got_object_id_dup");
5231 line->annotated = 1;
5233 errcode = pthread_mutex_unlock(&tog_mutex);
5235 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5240 blame_thread(void *arg)
5242 const struct got_error *err, *close_err;
5243 struct tog_blame_thread_args *ta = arg;
5244 struct tog_blame_cb_args *a = ta->cb_args;
5245 int errcode, fd1 = -1, fd2 = -1;
5246 FILE *f1 = NULL, *f2 = NULL;
5248 fd1 = got_opentempfd();
5250 return (void *)got_error_from_errno("got_opentempfd");
5252 fd2 = got_opentempfd();
5254 err = got_error_from_errno("got_opentempfd");
5258 f1 = got_opentemp();
5260 err = (void *)got_error_from_errno("got_opentemp");
5263 f2 = got_opentemp();
5265 err = (void *)got_error_from_errno("got_opentemp");
5269 err = block_signals_used_by_main_thread();
5273 err = got_blame(ta->path, a->commit_id, ta->repo,
5274 tog_diff_algo, blame_cb, ta->cb_args,
5275 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5276 if (err && err->code == GOT_ERR_CANCELLED)
5279 errcode = pthread_mutex_lock(&tog_mutex);
5281 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5285 close_err = got_repo_close(ta->repo);
5291 errcode = pthread_mutex_unlock(&tog_mutex);
5292 if (errcode && err == NULL)
5293 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5296 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5297 err = got_error_from_errno("close");
5298 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5299 err = got_error_from_errno("close");
5300 if (f1 && fclose(f1) == EOF && err == NULL)
5301 err = got_error_from_errno("fclose");
5302 if (f2 && fclose(f2) == EOF && err == NULL)
5303 err = got_error_from_errno("fclose");
5308 static struct got_object_id *
5309 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5310 int first_displayed_line, int selected_line)
5312 struct tog_blame_line *line;
5317 line = &lines[first_displayed_line - 1 + selected_line - 1];
5318 if (!line->annotated)
5324 static struct got_object_id *
5325 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5328 struct tog_blame_line *line;
5330 if (nlines <= 0 || lineno >= nlines)
5333 line = &lines[lineno - 1];
5334 if (!line->annotated)
5340 static const struct got_error *
5341 stop_blame(struct tog_blame *blame)
5343 const struct got_error *err = NULL;
5346 if (blame->thread) {
5348 errcode = pthread_mutex_unlock(&tog_mutex);
5350 return got_error_set_errno(errcode,
5351 "pthread_mutex_unlock");
5352 errcode = pthread_join(blame->thread, (void **)&err);
5354 return got_error_set_errno(errcode, "pthread_join");
5355 errcode = pthread_mutex_lock(&tog_mutex);
5357 return got_error_set_errno(errcode,
5358 "pthread_mutex_lock");
5359 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5361 blame->thread = NULL;
5363 if (blame->thread_args.repo) {
5364 const struct got_error *close_err;
5365 close_err = got_repo_close(blame->thread_args.repo);
5368 blame->thread_args.repo = NULL;
5371 if (fclose(blame->f) == EOF && err == NULL)
5372 err = got_error_from_errno("fclose");
5376 for (i = 0; i < blame->nlines; i++)
5377 free(blame->lines[i].id);
5379 blame->lines = NULL;
5381 free(blame->cb_args.commit_id);
5382 blame->cb_args.commit_id = NULL;
5383 if (blame->pack_fds) {
5384 const struct got_error *pack_err =
5385 got_repo_pack_fds_close(blame->pack_fds);
5388 blame->pack_fds = NULL;
5393 static const struct got_error *
5394 cancel_blame_view(void *arg)
5396 const struct got_error *err = NULL;
5400 errcode = pthread_mutex_lock(&tog_mutex);
5402 return got_error_set_errno(errcode,
5403 "pthread_mutex_unlock");
5406 err = got_error(GOT_ERR_CANCELLED);
5408 errcode = pthread_mutex_unlock(&tog_mutex);
5410 return got_error_set_errno(errcode,
5411 "pthread_mutex_lock");
5416 static const struct got_error *
5417 run_blame(struct tog_view *view)
5419 struct tog_blame_view_state *s = &view->state.blame;
5420 struct tog_blame *blame = &s->blame;
5421 const struct got_error *err = NULL;
5422 struct got_commit_object *commit = NULL;
5423 struct got_blob_object *blob = NULL;
5424 struct got_repository *thread_repo = NULL;
5425 struct got_object_id *obj_id = NULL;
5426 int obj_type, fd = -1;
5427 int *pack_fds = NULL;
5429 err = got_object_open_as_commit(&commit, s->repo,
5430 &s->blamed_commit->id);
5434 fd = got_opentempfd();
5436 err = got_error_from_errno("got_opentempfd");
5440 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5444 err = got_object_get_type(&obj_type, s->repo, obj_id);
5448 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5449 err = got_error(GOT_ERR_OBJ_TYPE);
5453 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5456 blame->f = got_opentemp();
5457 if (blame->f == NULL) {
5458 err = got_error_from_errno("got_opentemp");
5461 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5462 &blame->line_offsets, blame->f, blob);
5465 if (blame->nlines == 0) {
5466 s->blame_complete = 1;
5470 /* Don't include \n at EOF in the blame line count. */
5471 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5474 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5475 if (blame->lines == NULL) {
5476 err = got_error_from_errno("calloc");
5480 err = got_repo_pack_fds_open(&pack_fds);
5483 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5488 blame->pack_fds = pack_fds;
5489 blame->cb_args.view = view;
5490 blame->cb_args.lines = blame->lines;
5491 blame->cb_args.nlines = blame->nlines;
5492 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5493 if (blame->cb_args.commit_id == NULL) {
5494 err = got_error_from_errno("got_object_id_dup");
5497 blame->cb_args.quit = &s->done;
5499 blame->thread_args.path = s->path;
5500 blame->thread_args.repo = thread_repo;
5501 blame->thread_args.cb_args = &blame->cb_args;
5502 blame->thread_args.complete = &s->blame_complete;
5503 blame->thread_args.cancel_cb = cancel_blame_view;
5504 blame->thread_args.cancel_arg = &s->done;
5505 s->blame_complete = 0;
5507 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5508 s->first_displayed_line = 1;
5509 s->last_displayed_line = view->nlines;
5510 s->selected_line = 1;
5512 s->matched_line = 0;
5516 got_object_commit_close(commit);
5517 if (fd != -1 && close(fd) == -1 && err == NULL)
5518 err = got_error_from_errno("close");
5520 got_object_blob_close(blob);
5527 static const struct got_error *
5528 open_blame_view(struct tog_view *view, char *path,
5529 struct got_object_id *commit_id, struct got_repository *repo)
5531 const struct got_error *err = NULL;
5532 struct tog_blame_view_state *s = &view->state.blame;
5534 STAILQ_INIT(&s->blamed_commits);
5536 s->path = strdup(path);
5537 if (s->path == NULL)
5538 return got_error_from_errno("strdup");
5540 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5546 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5547 s->first_displayed_line = 1;
5548 s->last_displayed_line = view->nlines;
5549 s->selected_line = 1;
5550 s->blame_complete = 0;
5552 s->commit_id = commit_id;
5553 memset(&s->blame, 0, sizeof(s->blame));
5555 STAILQ_INIT(&s->colors);
5556 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5557 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5558 get_color_value("TOG_COLOR_COMMIT"));
5563 view->show = show_blame_view;
5564 view->input = input_blame_view;
5565 view->reset = reset_blame_view;
5566 view->close = close_blame_view;
5567 view->search_start = search_start_blame_view;
5568 view->search_next = search_next_blame_view;
5570 return run_blame(view);
5573 static const struct got_error *
5574 close_blame_view(struct tog_view *view)
5576 const struct got_error *err = NULL;
5577 struct tog_blame_view_state *s = &view->state.blame;
5579 if (s->blame.thread)
5580 err = stop_blame(&s->blame);
5582 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5583 struct got_object_qid *blamed_commit;
5584 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5585 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5586 got_object_qid_free(blamed_commit);
5590 free_colors(&s->colors);
5594 static const struct got_error *
5595 search_start_blame_view(struct tog_view *view)
5597 struct tog_blame_view_state *s = &view->state.blame;
5599 s->matched_line = 0;
5603 static const struct got_error *
5604 search_next_blame_view(struct tog_view *view)
5606 struct tog_blame_view_state *s = &view->state.blame;
5607 const struct got_error *err = NULL;
5610 size_t linesize = 0;
5613 if (!view->searching) {
5614 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5618 if (s->matched_line) {
5619 if (view->searching == TOG_SEARCH_FORWARD)
5620 lineno = s->matched_line + 1;
5622 lineno = s->matched_line - 1;
5624 lineno = s->first_displayed_line - 1 + s->selected_line;
5629 if (lineno <= 0 || lineno > s->blame.nlines) {
5630 if (s->matched_line == 0) {
5631 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5635 if (view->searching == TOG_SEARCH_FORWARD)
5638 lineno = s->blame.nlines;
5641 offset = s->blame.line_offsets[lineno - 1];
5642 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5644 return got_error_from_errno("fseeko");
5646 linelen = getline(&line, &linesize, s->blame.f);
5647 if (linelen != -1) {
5649 err = expand_tab(&exstr, line);
5652 if (match_line(exstr, &view->regex, 1,
5654 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5655 s->matched_line = lineno;
5661 if (view->searching == TOG_SEARCH_FORWARD)
5668 if (s->matched_line) {
5669 s->first_displayed_line = s->matched_line;
5670 s->selected_line = 1;
5676 static const struct got_error *
5677 show_blame_view(struct tog_view *view)
5679 const struct got_error *err = NULL;
5680 struct tog_blame_view_state *s = &view->state.blame;
5683 if (s->blame.thread == NULL && !s->blame_complete) {
5684 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5685 &s->blame.thread_args);
5687 return got_error_set_errno(errcode, "pthread_create");
5689 halfdelay(1); /* fast refresh while annotating */
5692 if (s->blame_complete)
5693 halfdelay(10); /* disable fast refresh */
5695 err = draw_blame(view);
5701 static const struct got_error *
5702 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5704 const struct got_error *err = NULL, *thread_err = NULL;
5705 struct tog_view *diff_view;
5706 struct tog_blame_view_state *s = &view->state.blame;
5707 int eos, nscroll, begin_y = 0, begin_x = 0;
5709 eos = nscroll = view->nlines - 2;
5710 if (view_is_hsplit_top(view))
5718 view->x = MAX(view->maxx - view->ncols / 3, 0);
5723 if (view->x + view->ncols / 3 < view->maxx)
5724 view->x += 2; /* move two columns right */
5730 view->x -= MIN(view->x, 2); /* move two columns back */
5739 s->selected_line = 1;
5740 s->first_displayed_line = 1;
5745 if (s->blame.nlines < eos) {
5746 s->selected_line = s->blame.nlines;
5747 s->first_displayed_line = 1;
5749 s->selected_line = eos;
5750 s->first_displayed_line = s->blame.nlines - (eos - 1);
5757 if (s->selected_line > 1)
5759 else if (s->selected_line == 1 &&
5760 s->first_displayed_line > 1)
5761 s->first_displayed_line--;
5772 if (s->first_displayed_line == 1) {
5773 if (view->count > 1)
5775 s->selected_line = MAX(1, s->selected_line - nscroll);
5779 if (s->first_displayed_line > nscroll)
5780 s->first_displayed_line -= nscroll;
5782 s->first_displayed_line = 1;
5787 if (s->selected_line < eos && s->first_displayed_line +
5788 s->selected_line <= s->blame.nlines)
5790 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5791 s->first_displayed_line++;
5797 struct got_object_id *id = NULL;
5800 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5801 s->first_displayed_line, s->selected_line);
5805 struct got_commit_object *commit, *pcommit;
5806 struct got_object_qid *pid;
5807 struct got_object_id *blob_id = NULL;
5809 err = got_object_open_as_commit(&commit,
5814 got_object_commit_get_parent_ids(commit));
5816 got_object_commit_close(commit);
5819 /* Check if path history ends here. */
5820 err = got_object_open_as_commit(&pcommit,
5824 err = got_object_id_by_path(&blob_id, s->repo,
5826 got_object_commit_close(pcommit);
5828 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5830 got_object_commit_close(commit);
5833 err = got_object_get_type(&obj_type, s->repo,
5836 /* Can't blame non-blob type objects. */
5837 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5838 got_object_commit_close(commit);
5841 err = got_object_qid_alloc(&s->blamed_commit,
5843 got_object_commit_close(commit);
5845 if (got_object_id_cmp(id,
5846 &s->blamed_commit->id) == 0)
5848 err = got_object_qid_alloc(&s->blamed_commit,
5854 thread_err = stop_blame(&s->blame);
5858 STAILQ_INSERT_HEAD(&s->blamed_commits,
5859 s->blamed_commit, entry);
5860 err = run_blame(view);
5866 struct got_object_qid *first;
5869 first = STAILQ_FIRST(&s->blamed_commits);
5870 if (!got_object_id_cmp(&first->id, s->commit_id))
5873 thread_err = stop_blame(&s->blame);
5877 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5878 got_object_qid_free(s->blamed_commit);
5880 STAILQ_FIRST(&s->blamed_commits);
5881 err = run_blame(view);
5888 struct got_object_id *id = NULL;
5889 struct got_object_qid *pid;
5890 struct got_commit_object *commit = NULL;
5893 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5894 s->first_displayed_line, s->selected_line);
5897 err = got_object_open_as_commit(&commit, s->repo, id);
5900 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5902 /* traversed from diff view, release diff resources */
5903 err = close_diff_view(*new_view);
5906 diff_view = *new_view;
5908 if (view_is_parent_view(view))
5909 view_get_split(view, &begin_y, &begin_x);
5911 diff_view = view_open(0, 0, begin_y, begin_x,
5913 if (diff_view == NULL) {
5914 got_object_commit_close(commit);
5915 err = got_error_from_errno("view_open");
5919 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5920 id, NULL, NULL, 3, 0, 0, view, s->repo);
5921 got_object_commit_close(commit);
5923 view_close(diff_view);
5926 s->last_diffed_line = s->first_displayed_line - 1 +
5929 break; /* still open from active diff view */
5930 if (view_is_parent_view(view) &&
5931 view->mode == TOG_VIEW_SPLIT_HRZN) {
5932 err = view_init_hsplit(view, begin_y);
5938 diff_view->focussed = 1;
5939 diff_view->mode = view->mode;
5940 diff_view->nlines = view->lines - begin_y;
5941 if (view_is_parent_view(view)) {
5942 view_transfer_size(diff_view, view);
5943 err = view_close_child(view);
5946 err = view_set_child(view, diff_view);
5949 view->focus_child = 1;
5951 *new_view = diff_view;
5964 if (s->last_displayed_line >= s->blame.nlines &&
5965 s->selected_line >= MIN(s->blame.nlines,
5966 view->nlines - 2)) {
5970 if (s->last_displayed_line >= s->blame.nlines &&
5971 s->selected_line < view->nlines - 2) {
5973 MIN(nscroll, s->last_displayed_line -
5974 s->first_displayed_line - s->selected_line + 1);
5976 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5977 s->first_displayed_line += nscroll;
5979 s->first_displayed_line =
5980 s->blame.nlines - (view->nlines - 3);
5983 if (s->selected_line > view->nlines - 2) {
5984 s->selected_line = MIN(s->blame.nlines,
5992 return thread_err ? thread_err : err;
5995 static const struct got_error *
5996 reset_blame_view(struct tog_view *view)
5998 const struct got_error *err;
5999 struct tog_blame_view_state *s = &view->state.blame;
6003 err = stop_blame(&s->blame);
6007 return run_blame(view);
6010 static const struct got_error *
6011 cmd_blame(int argc, char *argv[])
6013 const struct got_error *error;
6014 struct got_repository *repo = NULL;
6015 struct got_worktree *worktree = NULL;
6016 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6017 char *link_target = NULL;
6018 struct got_object_id *commit_id = NULL;
6019 struct got_commit_object *commit = NULL;
6020 char *commit_id_str = NULL;
6022 struct tog_view *view;
6023 int *pack_fds = NULL;
6025 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6028 commit_id_str = optarg;
6031 repo_path = realpath(optarg, NULL);
6032 if (repo_path == NULL)
6033 return got_error_from_errno2("realpath",
6048 error = got_repo_pack_fds_open(&pack_fds);
6052 if (repo_path == NULL) {
6053 cwd = getcwd(NULL, 0);
6055 return got_error_from_errno("getcwd");
6056 error = got_worktree_open(&worktree, cwd);
6057 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6061 strdup(got_worktree_get_repo_path(worktree));
6063 repo_path = strdup(cwd);
6064 if (repo_path == NULL) {
6065 error = got_error_from_errno("strdup");
6070 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6074 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6081 error = apply_unveil(got_repo_get_path(repo), NULL);
6085 error = tog_load_refs(repo, 0);
6089 if (commit_id_str == NULL) {
6090 struct got_reference *head_ref;
6091 error = got_ref_open(&head_ref, repo, worktree ?
6092 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6095 error = got_ref_resolve(&commit_id, repo, head_ref);
6096 got_ref_close(head_ref);
6098 error = got_repo_match_object_id(&commit_id, NULL,
6099 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6104 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6106 error = got_error_from_errno("view_open");
6110 error = got_object_open_as_commit(&commit, repo, commit_id);
6114 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6119 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6124 /* Release work tree lock. */
6125 got_worktree_close(worktree);
6128 error = view_loop(view);
6136 got_object_commit_close(commit);
6138 got_worktree_close(worktree);
6140 const struct got_error *close_err = got_repo_close(repo);
6145 const struct got_error *pack_err =
6146 got_repo_pack_fds_close(pack_fds);
6154 static const struct got_error *
6155 draw_tree_entries(struct tog_view *view, const char *parent_path)
6157 struct tog_tree_view_state *s = &view->state.tree;
6158 const struct got_error *err = NULL;
6159 struct got_tree_entry *te;
6161 struct tog_color *tc;
6162 int width, n, i, nentries;
6163 int limit = view->nlines;
6166 if (view_is_hsplit_top(view))
6167 --limit; /* border */
6169 werase(view->window);
6174 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6178 if (view_needs_focus_indication(view))
6179 wstandout(view->window);
6180 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6182 wattr_on(view->window,
6183 COLOR_PAIR(tc->colorpair), NULL);
6184 waddwstr(view->window, wline);
6186 wattr_off(view->window,
6187 COLOR_PAIR(tc->colorpair), NULL);
6188 if (view_needs_focus_indication(view))
6189 wstandend(view->window);
6192 if (width < view->ncols - 1)
6193 waddch(view->window, '\n');
6196 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6200 waddwstr(view->window, wline);
6203 if (width < view->ncols - 1)
6204 waddch(view->window, '\n');
6207 waddch(view->window, '\n');
6211 if (s->first_displayed_entry == NULL) {
6212 te = got_object_tree_get_first_entry(s->tree);
6213 if (s->selected == 0) {
6215 wstandout(view->window);
6216 s->selected_entry = NULL;
6218 waddstr(view->window, " ..\n"); /* parent directory */
6219 if (s->selected == 0 && view->focussed)
6220 wstandend(view->window);
6227 te = s->first_displayed_entry;
6230 nentries = got_object_tree_get_nentries(s->tree);
6231 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6232 char *line = NULL, *id_str = NULL, *link_target = NULL;
6233 const char *modestr = "";
6236 te = got_object_tree_get_entry(s->tree, i);
6237 mode = got_tree_entry_get_mode(te);
6240 err = got_object_id_str(&id_str,
6241 got_tree_entry_get_id(te));
6243 return got_error_from_errno(
6244 "got_object_id_str");
6246 if (got_object_tree_entry_is_submodule(te))
6248 else if (S_ISLNK(mode)) {
6251 err = got_tree_entry_get_symlink_target(&link_target,
6257 for (i = 0; i < strlen(link_target); i++) {
6258 if (!isprint((unsigned char)link_target[i]))
6259 link_target[i] = '?';
6263 else if (S_ISDIR(mode))
6265 else if (mode & S_IXUSR)
6267 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6268 got_tree_entry_get_name(te), modestr,
6269 link_target ? " -> ": "",
6270 link_target ? link_target : "") == -1) {
6273 return got_error_from_errno("asprintf");
6277 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6283 if (n == s->selected) {
6285 wstandout(view->window);
6286 s->selected_entry = te;
6288 tc = match_color(&s->colors, line);
6290 wattr_on(view->window,
6291 COLOR_PAIR(tc->colorpair), NULL);
6292 waddwstr(view->window, wline);
6294 wattr_off(view->window,
6295 COLOR_PAIR(tc->colorpair), NULL);
6296 if (width < view->ncols - 1)
6297 waddch(view->window, '\n');
6298 if (n == s->selected && view->focussed)
6299 wstandend(view->window);
6305 s->last_displayed_entry = te;
6314 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6316 struct got_tree_entry *te;
6317 int isroot = s->tree == s->root;
6320 if (s->first_displayed_entry == NULL)
6323 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6324 while (i++ < maxscroll) {
6327 s->first_displayed_entry = NULL;
6330 s->first_displayed_entry = te;
6331 te = got_tree_entry_get_prev(s->tree, te);
6335 static const struct got_error *
6336 tree_scroll_down(struct tog_view *view, int maxscroll)
6338 struct tog_tree_view_state *s = &view->state.tree;
6339 struct got_tree_entry *next, *last;
6342 if (s->first_displayed_entry)
6343 next = got_tree_entry_get_next(s->tree,
6344 s->first_displayed_entry);
6346 next = got_object_tree_get_first_entry(s->tree);
6348 last = s->last_displayed_entry;
6349 while (next && n++ < maxscroll) {
6351 last = got_tree_entry_get_next(s->tree, last);
6352 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6353 s->first_displayed_entry = next;
6354 next = got_tree_entry_get_next(s->tree, next);
6361 static const struct got_error *
6362 tree_entry_path(char **path, struct tog_parent_trees *parents,
6363 struct got_tree_entry *te)
6365 const struct got_error *err = NULL;
6366 struct tog_parent_tree *pt;
6367 size_t len = 2; /* for leading slash and NUL */
6369 TAILQ_FOREACH(pt, parents, entry)
6370 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6373 len += strlen(got_tree_entry_get_name(te));
6375 *path = calloc(1, len);
6377 return got_error_from_errno("calloc");
6380 pt = TAILQ_LAST(parents, tog_parent_trees);
6382 const char *name = got_tree_entry_get_name(pt->selected_entry);
6383 if (strlcat(*path, name, len) >= len) {
6384 err = got_error(GOT_ERR_NO_SPACE);
6387 if (strlcat(*path, "/", len) >= len) {
6388 err = got_error(GOT_ERR_NO_SPACE);
6391 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6394 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6395 err = got_error(GOT_ERR_NO_SPACE);
6407 static const struct got_error *
6408 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6409 struct got_tree_entry *te, struct tog_parent_trees *parents,
6410 struct got_object_id *commit_id, struct got_repository *repo)
6412 const struct got_error *err = NULL;
6414 struct tog_view *blame_view;
6418 err = tree_entry_path(&path, parents, te);
6422 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6423 if (blame_view == NULL) {
6424 err = got_error_from_errno("view_open");
6428 err = open_blame_view(blame_view, path, commit_id, repo);
6430 if (err->code == GOT_ERR_CANCELLED)
6432 view_close(blame_view);
6434 *new_view = blame_view;
6440 static const struct got_error *
6441 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6442 struct tog_tree_view_state *s)
6444 struct tog_view *log_view;
6445 const struct got_error *err = NULL;
6450 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6451 if (log_view == NULL)
6452 return got_error_from_errno("view_open");
6454 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6458 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6461 view_close(log_view);
6463 *new_view = log_view;
6468 static const struct got_error *
6469 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6470 const char *head_ref_name, struct got_repository *repo)
6472 const struct got_error *err = NULL;
6473 char *commit_id_str = NULL;
6474 struct tog_tree_view_state *s = &view->state.tree;
6475 struct got_commit_object *commit = NULL;
6477 TAILQ_INIT(&s->parents);
6478 STAILQ_INIT(&s->colors);
6480 s->commit_id = got_object_id_dup(commit_id);
6481 if (s->commit_id == NULL)
6482 return got_error_from_errno("got_object_id_dup");
6484 err = got_object_open_as_commit(&commit, repo, commit_id);
6489 * The root is opened here and will be closed when the view is closed.
6490 * Any visited subtrees and their path-wise parents are opened and
6493 err = got_object_open_as_tree(&s->root, repo,
6494 got_object_commit_get_tree_id(commit));
6499 err = got_object_id_str(&commit_id_str, commit_id);
6503 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6504 err = got_error_from_errno("asprintf");
6508 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6509 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6510 if (head_ref_name) {
6511 s->head_ref_name = strdup(head_ref_name);
6512 if (s->head_ref_name == NULL) {
6513 err = got_error_from_errno("strdup");
6519 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6520 err = add_color(&s->colors, "\\$$",
6521 TOG_COLOR_TREE_SUBMODULE,
6522 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6525 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6526 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6529 err = add_color(&s->colors, "/$",
6530 TOG_COLOR_TREE_DIRECTORY,
6531 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6535 err = add_color(&s->colors, "\\*$",
6536 TOG_COLOR_TREE_EXECUTABLE,
6537 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6541 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6542 get_color_value("TOG_COLOR_COMMIT"));
6547 view->show = show_tree_view;
6548 view->input = input_tree_view;
6549 view->close = close_tree_view;
6550 view->search_start = search_start_tree_view;
6551 view->search_next = search_next_tree_view;
6553 free(commit_id_str);
6555 got_object_commit_close(commit);
6557 close_tree_view(view);
6561 static const struct got_error *
6562 close_tree_view(struct tog_view *view)
6564 struct tog_tree_view_state *s = &view->state.tree;
6566 free_colors(&s->colors);
6567 free(s->tree_label);
6568 s->tree_label = NULL;
6570 s->commit_id = NULL;
6571 free(s->head_ref_name);
6572 s->head_ref_name = NULL;
6573 while (!TAILQ_EMPTY(&s->parents)) {
6574 struct tog_parent_tree *parent;
6575 parent = TAILQ_FIRST(&s->parents);
6576 TAILQ_REMOVE(&s->parents, parent, entry);
6577 if (parent->tree != s->root)
6578 got_object_tree_close(parent->tree);
6582 if (s->tree != NULL && s->tree != s->root)
6583 got_object_tree_close(s->tree);
6585 got_object_tree_close(s->root);
6589 static const struct got_error *
6590 search_start_tree_view(struct tog_view *view)
6592 struct tog_tree_view_state *s = &view->state.tree;
6594 s->matched_entry = NULL;
6599 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6601 regmatch_t regmatch;
6603 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6607 static const struct got_error *
6608 search_next_tree_view(struct tog_view *view)
6610 struct tog_tree_view_state *s = &view->state.tree;
6611 struct got_tree_entry *te = NULL;
6613 if (!view->searching) {
6614 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6618 if (s->matched_entry) {
6619 if (view->searching == TOG_SEARCH_FORWARD) {
6620 if (s->selected_entry)
6621 te = got_tree_entry_get_next(s->tree,
6624 te = got_object_tree_get_first_entry(s->tree);
6626 if (s->selected_entry == NULL)
6627 te = got_object_tree_get_last_entry(s->tree);
6629 te = got_tree_entry_get_prev(s->tree,
6633 if (s->selected_entry)
6634 te = s->selected_entry;
6635 else if (view->searching == TOG_SEARCH_FORWARD)
6636 te = got_object_tree_get_first_entry(s->tree);
6638 te = got_object_tree_get_last_entry(s->tree);
6643 if (s->matched_entry == NULL) {
6644 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6647 if (view->searching == TOG_SEARCH_FORWARD)
6648 te = got_object_tree_get_first_entry(s->tree);
6650 te = got_object_tree_get_last_entry(s->tree);
6653 if (match_tree_entry(te, &view->regex)) {
6654 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6655 s->matched_entry = te;
6659 if (view->searching == TOG_SEARCH_FORWARD)
6660 te = got_tree_entry_get_next(s->tree, te);
6662 te = got_tree_entry_get_prev(s->tree, te);
6665 if (s->matched_entry) {
6666 s->first_displayed_entry = s->matched_entry;
6673 static const struct got_error *
6674 show_tree_view(struct tog_view *view)
6676 const struct got_error *err = NULL;
6677 struct tog_tree_view_state *s = &view->state.tree;
6680 err = tree_entry_path(&parent_path, &s->parents, NULL);
6684 err = draw_tree_entries(view, parent_path);
6691 static const struct got_error *
6692 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6694 const struct got_error *err = NULL;
6695 struct tog_tree_view_state *s = &view->state.tree;
6696 struct tog_view *log_view, *ref_view;
6697 struct got_tree_entry *te;
6698 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6702 s->show_ids = !s->show_ids;
6707 if (!s->selected_entry)
6709 if (view_is_parent_view(view))
6710 view_get_split(view, &begin_y, &begin_x);
6711 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6712 if (view_is_parent_view(view) &&
6713 view->mode == TOG_VIEW_SPLIT_HRZN) {
6714 err = view_init_hsplit(view, begin_y);
6719 log_view->focussed = 1;
6720 log_view->mode = view->mode;
6721 log_view->nlines = view->lines - begin_y;
6722 if (view_is_parent_view(view)) {
6723 view_transfer_size(log_view, view);
6724 err = view_close_child(view);
6727 err = view_set_child(view, log_view);
6730 view->focus_child = 1;
6732 *new_view = log_view;
6736 if (view_is_parent_view(view))
6737 view_get_split(view, &begin_y, &begin_x);
6738 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6739 if (ref_view == NULL)
6740 return got_error_from_errno("view_open");
6741 err = open_ref_view(ref_view, s->repo);
6743 view_close(ref_view);
6746 if (view_is_parent_view(view) &&
6747 view->mode == TOG_VIEW_SPLIT_HRZN) {
6748 err = view_init_hsplit(view, begin_y);
6753 ref_view->focussed = 1;
6754 ref_view->mode = view->mode;
6755 ref_view->nlines = view->lines - begin_y;
6756 if (view_is_parent_view(view)) {
6757 view_transfer_size(ref_view, view);
6758 err = view_close_child(view);
6761 err = view_set_child(view, ref_view);
6764 view->focus_child = 1;
6766 *new_view = ref_view;
6772 if (s->tree == s->root)
6773 s->first_displayed_entry =
6774 got_object_tree_get_first_entry(s->tree);
6776 s->first_displayed_entry = NULL;
6780 int eos = view->nlines - 3;
6782 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6786 te = got_object_tree_get_last_entry(s->tree);
6787 for (n = 0; n < eos; n++) {
6789 if(s->tree != s->root) {
6790 s->first_displayed_entry = NULL;
6795 s->first_displayed_entry = te;
6796 te = got_tree_entry_get_prev(s->tree, te);
6799 s->selected = n - 1;
6805 if (s->selected > 0) {
6809 tree_scroll_up(s, 1);
6810 if (s->selected_entry == NULL ||
6811 (s->tree == s->root && s->selected_entry ==
6812 got_object_tree_get_first_entry(s->tree)))
6822 if (s->tree == s->root) {
6823 if (got_object_tree_get_first_entry(s->tree) ==
6824 s->first_displayed_entry)
6825 s->selected -= MIN(s->selected, nscroll);
6827 if (s->first_displayed_entry == NULL)
6828 s->selected -= MIN(s->selected, nscroll);
6830 tree_scroll_up(s, MAX(0, nscroll));
6831 if (s->selected_entry == NULL ||
6832 (s->tree == s->root && s->selected_entry ==
6833 got_object_tree_get_first_entry(s->tree)))
6839 if (s->selected < s->ndisplayed - 1) {
6843 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6845 /* can't scroll any further */
6849 tree_scroll_down(view, 1);
6859 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6861 /* can't scroll any further; move cursor down */
6862 if (s->selected < s->ndisplayed - 1)
6863 s->selected += MIN(nscroll,
6864 s->ndisplayed - s->selected - 1);
6869 tree_scroll_down(view, nscroll);
6874 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6875 struct tog_parent_tree *parent;
6876 /* user selected '..' */
6877 if (s->tree == s->root) {
6881 parent = TAILQ_FIRST(&s->parents);
6882 TAILQ_REMOVE(&s->parents, parent,
6884 got_object_tree_close(s->tree);
6885 s->tree = parent->tree;
6886 s->first_displayed_entry =
6887 parent->first_displayed_entry;
6889 parent->selected_entry;
6890 s->selected = parent->selected;
6891 if (s->selected > view->nlines - 3) {
6892 err = offset_selection_down(view);
6897 } else if (S_ISDIR(got_tree_entry_get_mode(
6898 s->selected_entry))) {
6899 struct got_tree_object *subtree;
6901 err = got_object_open_as_tree(&subtree, s->repo,
6902 got_tree_entry_get_id(s->selected_entry));
6905 err = tree_view_visit_subtree(s, subtree);
6907 got_object_tree_close(subtree);
6910 } else if (S_ISREG(got_tree_entry_get_mode(
6911 s->selected_entry))) {
6912 struct tog_view *blame_view;
6913 int begin_x = 0, begin_y = 0;
6915 if (view_is_parent_view(view))
6916 view_get_split(view, &begin_y, &begin_x);
6918 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6919 s->selected_entry, &s->parents,
6920 s->commit_id, s->repo);
6924 if (view_is_parent_view(view) &&
6925 view->mode == TOG_VIEW_SPLIT_HRZN) {
6926 err = view_init_hsplit(view, begin_y);
6933 blame_view->focussed = 1;
6934 blame_view->mode = view->mode;
6935 blame_view->nlines = view->lines - begin_y;
6936 if (view_is_parent_view(view)) {
6937 view_transfer_size(blame_view, view);
6938 err = view_close_child(view);
6941 err = view_set_child(view, blame_view);
6944 view->focus_child = 1;
6946 *new_view = blame_view;
6950 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6951 s->selected = view->nlines - 4;
6967 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6972 static const struct got_error *
6973 cmd_tree(int argc, char *argv[])
6975 const struct got_error *error;
6976 struct got_repository *repo = NULL;
6977 struct got_worktree *worktree = NULL;
6978 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6979 struct got_object_id *commit_id = NULL;
6980 struct got_commit_object *commit = NULL;
6981 const char *commit_id_arg = NULL;
6983 struct got_reference *ref = NULL;
6984 const char *head_ref_name = NULL;
6986 struct tog_view *view;
6987 int *pack_fds = NULL;
6989 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6992 commit_id_arg = optarg;
6995 repo_path = realpath(optarg, NULL);
6996 if (repo_path == NULL)
6997 return got_error_from_errno2("realpath",
7012 error = got_repo_pack_fds_open(&pack_fds);
7016 if (repo_path == NULL) {
7017 cwd = getcwd(NULL, 0);
7019 return got_error_from_errno("getcwd");
7020 error = got_worktree_open(&worktree, cwd);
7021 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7025 strdup(got_worktree_get_repo_path(worktree));
7027 repo_path = strdup(cwd);
7028 if (repo_path == NULL) {
7029 error = got_error_from_errno("strdup");
7034 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7038 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7045 error = apply_unveil(got_repo_get_path(repo), NULL);
7049 error = tog_load_refs(repo, 0);
7053 if (commit_id_arg == NULL) {
7054 error = got_repo_match_object_id(&commit_id, &label,
7055 worktree ? got_worktree_get_head_ref_name(worktree) :
7056 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7059 head_ref_name = label;
7061 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7063 head_ref_name = got_ref_get_name(ref);
7064 else if (error->code != GOT_ERR_NOT_REF)
7066 error = got_repo_match_object_id(&commit_id, NULL,
7067 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7072 error = got_object_open_as_commit(&commit, repo, commit_id);
7076 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7078 error = got_error_from_errno("view_open");
7081 error = open_tree_view(view, commit_id, head_ref_name, repo);
7084 if (!got_path_is_root_dir(in_repo_path)) {
7085 error = tree_view_walk_path(&view->state.tree, commit,
7092 /* Release work tree lock. */
7093 got_worktree_close(worktree);
7096 error = view_loop(view);
7105 const struct got_error *close_err = got_repo_close(repo);
7110 const struct got_error *pack_err =
7111 got_repo_pack_fds_close(pack_fds);
7119 static const struct got_error *
7120 ref_view_load_refs(struct tog_ref_view_state *s)
7122 struct got_reflist_entry *sre;
7123 struct tog_reflist_entry *re;
7126 TAILQ_FOREACH(sre, &tog_refs, entry) {
7127 if (strncmp(got_ref_get_name(sre->ref),
7128 "refs/got/", 9) == 0 &&
7129 strncmp(got_ref_get_name(sre->ref),
7130 "refs/got/backup/", 16) != 0)
7133 re = malloc(sizeof(*re));
7135 return got_error_from_errno("malloc");
7137 re->ref = got_ref_dup(sre->ref);
7138 if (re->ref == NULL)
7139 return got_error_from_errno("got_ref_dup");
7140 re->idx = s->nrefs++;
7141 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7144 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7149 ref_view_free_refs(struct tog_ref_view_state *s)
7151 struct tog_reflist_entry *re;
7153 while (!TAILQ_EMPTY(&s->refs)) {
7154 re = TAILQ_FIRST(&s->refs);
7155 TAILQ_REMOVE(&s->refs, re, entry);
7156 got_ref_close(re->ref);
7161 static const struct got_error *
7162 open_ref_view(struct tog_view *view, struct got_repository *repo)
7164 const struct got_error *err = NULL;
7165 struct tog_ref_view_state *s = &view->state.ref;
7167 s->selected_entry = 0;
7170 TAILQ_INIT(&s->refs);
7171 STAILQ_INIT(&s->colors);
7173 err = ref_view_load_refs(s);
7177 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7178 err = add_color(&s->colors, "^refs/heads/",
7179 TOG_COLOR_REFS_HEADS,
7180 get_color_value("TOG_COLOR_REFS_HEADS"));
7184 err = add_color(&s->colors, "^refs/tags/",
7185 TOG_COLOR_REFS_TAGS,
7186 get_color_value("TOG_COLOR_REFS_TAGS"));
7190 err = add_color(&s->colors, "^refs/remotes/",
7191 TOG_COLOR_REFS_REMOTES,
7192 get_color_value("TOG_COLOR_REFS_REMOTES"));
7196 err = add_color(&s->colors, "^refs/got/backup/",
7197 TOG_COLOR_REFS_BACKUP,
7198 get_color_value("TOG_COLOR_REFS_BACKUP"));
7203 view->show = show_ref_view;
7204 view->input = input_ref_view;
7205 view->close = close_ref_view;
7206 view->search_start = search_start_ref_view;
7207 view->search_next = search_next_ref_view;
7210 free_colors(&s->colors);
7214 static const struct got_error *
7215 close_ref_view(struct tog_view *view)
7217 struct tog_ref_view_state *s = &view->state.ref;
7219 ref_view_free_refs(s);
7220 free_colors(&s->colors);
7225 static const struct got_error *
7226 resolve_reflist_entry(struct got_object_id **commit_id,
7227 struct tog_reflist_entry *re, struct got_repository *repo)
7229 const struct got_error *err = NULL;
7230 struct got_object_id *obj_id;
7231 struct got_tag_object *tag = NULL;
7236 err = got_ref_resolve(&obj_id, repo, re->ref);
7240 err = got_object_get_type(&obj_type, repo, obj_id);
7245 case GOT_OBJ_TYPE_COMMIT:
7246 *commit_id = obj_id;
7248 case GOT_OBJ_TYPE_TAG:
7249 err = got_object_open_as_tag(&tag, repo, obj_id);
7253 err = got_object_get_type(&obj_type, repo,
7254 got_object_tag_get_object_id(tag));
7257 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7258 err = got_error(GOT_ERR_OBJ_TYPE);
7261 *commit_id = got_object_id_dup(
7262 got_object_tag_get_object_id(tag));
7263 if (*commit_id == NULL) {
7264 err = got_error_from_errno("got_object_id_dup");
7269 err = got_error(GOT_ERR_OBJ_TYPE);
7275 got_object_tag_close(tag);
7283 static const struct got_error *
7284 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7285 struct tog_reflist_entry *re, struct got_repository *repo)
7287 struct tog_view *log_view;
7288 const struct got_error *err = NULL;
7289 struct got_object_id *commit_id = NULL;
7293 err = resolve_reflist_entry(&commit_id, re, repo);
7295 if (err->code != GOT_ERR_OBJ_TYPE)
7301 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7302 if (log_view == NULL) {
7303 err = got_error_from_errno("view_open");
7307 err = open_log_view(log_view, commit_id, repo,
7308 got_ref_get_name(re->ref), "", 0);
7311 view_close(log_view);
7313 *new_view = log_view;
7319 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7321 struct tog_reflist_entry *re;
7324 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7327 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7328 while (i++ < maxscroll) {
7331 s->first_displayed_entry = re;
7332 re = TAILQ_PREV(re, tog_reflist_head, entry);
7336 static const struct got_error *
7337 ref_scroll_down(struct tog_view *view, int maxscroll)
7339 struct tog_ref_view_state *s = &view->state.ref;
7340 struct tog_reflist_entry *next, *last;
7343 if (s->first_displayed_entry)
7344 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7346 next = TAILQ_FIRST(&s->refs);
7348 last = s->last_displayed_entry;
7349 while (next && n++ < maxscroll) {
7351 last = TAILQ_NEXT(last, entry);
7352 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7353 s->first_displayed_entry = next;
7354 next = TAILQ_NEXT(next, entry);
7361 static const struct got_error *
7362 search_start_ref_view(struct tog_view *view)
7364 struct tog_ref_view_state *s = &view->state.ref;
7366 s->matched_entry = NULL;
7371 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7373 regmatch_t regmatch;
7375 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7379 static const struct got_error *
7380 search_next_ref_view(struct tog_view *view)
7382 struct tog_ref_view_state *s = &view->state.ref;
7383 struct tog_reflist_entry *re = NULL;
7385 if (!view->searching) {
7386 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7390 if (s->matched_entry) {
7391 if (view->searching == TOG_SEARCH_FORWARD) {
7392 if (s->selected_entry)
7393 re = TAILQ_NEXT(s->selected_entry, entry);
7395 re = TAILQ_PREV(s->selected_entry,
7396 tog_reflist_head, entry);
7398 if (s->selected_entry == NULL)
7399 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7401 re = TAILQ_PREV(s->selected_entry,
7402 tog_reflist_head, entry);
7405 if (s->selected_entry)
7406 re = s->selected_entry;
7407 else if (view->searching == TOG_SEARCH_FORWARD)
7408 re = TAILQ_FIRST(&s->refs);
7410 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7415 if (s->matched_entry == NULL) {
7416 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7419 if (view->searching == TOG_SEARCH_FORWARD)
7420 re = TAILQ_FIRST(&s->refs);
7422 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7425 if (match_reflist_entry(re, &view->regex)) {
7426 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7427 s->matched_entry = re;
7431 if (view->searching == TOG_SEARCH_FORWARD)
7432 re = TAILQ_NEXT(re, entry);
7434 re = TAILQ_PREV(re, tog_reflist_head, entry);
7437 if (s->matched_entry) {
7438 s->first_displayed_entry = s->matched_entry;
7445 static const struct got_error *
7446 show_ref_view(struct tog_view *view)
7448 const struct got_error *err = NULL;
7449 struct tog_ref_view_state *s = &view->state.ref;
7450 struct tog_reflist_entry *re;
7453 struct tog_color *tc;
7455 int limit = view->nlines;
7457 werase(view->window);
7460 if (view_is_hsplit_top(view))
7461 --limit; /* border */
7466 re = s->first_displayed_entry;
7468 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7470 return got_error_from_errno("asprintf");
7472 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7477 if (view_needs_focus_indication(view))
7478 wstandout(view->window);
7479 waddwstr(view->window, wline);
7480 if (view_needs_focus_indication(view))
7481 wstandend(view->window);
7486 if (width < view->ncols - 1)
7487 waddch(view->window, '\n');
7492 while (re && limit > 0) {
7494 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7497 struct got_commit_object *ci;
7498 struct got_tag_object *tag;
7499 struct got_object_id *id;
7503 err = got_ref_resolve(&id, s->repo, re->ref);
7506 err = got_object_open_as_tag(&tag, s->repo, id);
7508 if (err->code != GOT_ERR_OBJ_TYPE) {
7512 err = got_object_open_as_commit(&ci, s->repo,
7518 t = got_object_commit_get_committer_time(ci);
7519 got_object_commit_close(ci);
7521 t = got_object_tag_get_tagger_time(tag);
7522 got_object_tag_close(tag);
7525 if (gmtime_r(&t, &tm) == NULL)
7526 return got_error_from_errno("gmtime_r");
7527 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7528 return got_error(GOT_ERR_NO_SPACE);
7530 if (got_ref_is_symbolic(re->ref)) {
7531 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7532 ymd : "", got_ref_get_name(re->ref),
7533 got_ref_get_symref_target(re->ref)) == -1)
7534 return got_error_from_errno("asprintf");
7535 } else if (s->show_ids) {
7536 struct got_object_id *id;
7538 err = got_ref_resolve(&id, s->repo, re->ref);
7541 err = got_object_id_str(&id_str, id);
7546 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7547 got_ref_get_name(re->ref), id_str) == -1) {
7548 err = got_error_from_errno("asprintf");
7555 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7556 got_ref_get_name(re->ref)) == -1)
7557 return got_error_from_errno("asprintf");
7559 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7565 if (n == s->selected) {
7567 wstandout(view->window);
7568 s->selected_entry = re;
7570 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7572 wattr_on(view->window,
7573 COLOR_PAIR(tc->colorpair), NULL);
7574 waddwstr(view->window, wline);
7576 wattr_off(view->window,
7577 COLOR_PAIR(tc->colorpair), NULL);
7578 if (width < view->ncols - 1)
7579 waddch(view->window, '\n');
7580 if (n == s->selected && view->focussed)
7581 wstandend(view->window);
7587 s->last_displayed_entry = re;
7590 re = TAILQ_NEXT(re, entry);
7597 static const struct got_error *
7598 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7599 struct tog_reflist_entry *re, struct got_repository *repo)
7601 const struct got_error *err = NULL;
7602 struct got_object_id *commit_id = NULL;
7603 struct tog_view *tree_view;
7607 err = resolve_reflist_entry(&commit_id, re, repo);
7609 if (err->code != GOT_ERR_OBJ_TYPE)
7616 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7617 if (tree_view == NULL) {
7618 err = got_error_from_errno("view_open");
7622 err = open_tree_view(tree_view, commit_id,
7623 got_ref_get_name(re->ref), repo);
7627 *new_view = tree_view;
7632 static const struct got_error *
7633 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7635 const struct got_error *err = NULL;
7636 struct tog_ref_view_state *s = &view->state.ref;
7637 struct tog_view *log_view, *tree_view;
7638 struct tog_reflist_entry *re;
7639 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7643 s->show_ids = !s->show_ids;
7647 s->show_date = !s->show_date;
7651 s->sort_by_date = !s->sort_by_date;
7653 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7654 got_ref_cmp_by_commit_timestamp_descending :
7655 tog_ref_cmp_by_name, s->repo);
7658 got_reflist_object_id_map_free(tog_refs_idmap);
7659 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7660 &tog_refs, s->repo);
7663 ref_view_free_refs(s);
7664 err = ref_view_load_refs(s);
7669 if (!s->selected_entry)
7671 if (view_is_parent_view(view))
7672 view_get_split(view, &begin_y, &begin_x);
7674 err = log_ref_entry(&log_view, begin_y, begin_x,
7675 s->selected_entry, s->repo);
7679 if (view_is_parent_view(view) &&
7680 view->mode == TOG_VIEW_SPLIT_HRZN) {
7681 err = view_init_hsplit(view, begin_y);
7687 log_view->focussed = 1;
7688 log_view->mode = view->mode;
7689 log_view->nlines = view->lines - begin_y;
7690 if (view_is_parent_view(view)) {
7691 view_transfer_size(log_view, view);
7692 err = view_close_child(view);
7695 err = view_set_child(view, log_view);
7698 view->focus_child = 1;
7700 *new_view = log_view;
7704 if (!s->selected_entry)
7706 if (view_is_parent_view(view))
7707 view_get_split(view, &begin_y, &begin_x);
7708 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7709 s->selected_entry, s->repo);
7710 if (err || tree_view == NULL)
7712 if (view_is_parent_view(view) &&
7713 view->mode == TOG_VIEW_SPLIT_HRZN) {
7714 err = view_init_hsplit(view, begin_y);
7719 tree_view->focussed = 1;
7720 tree_view->mode = view->mode;
7721 tree_view->nlines = view->lines - begin_y;
7722 if (view_is_parent_view(view)) {
7723 view_transfer_size(tree_view, view);
7724 err = view_close_child(view);
7727 err = view_set_child(view, tree_view);
7730 view->focus_child = 1;
7732 *new_view = tree_view;
7738 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7742 int eos = view->nlines - 1;
7744 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7748 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7749 for (n = 0; n < eos; n++) {
7752 s->first_displayed_entry = re;
7753 re = TAILQ_PREV(re, tog_reflist_head, entry);
7756 s->selected = n - 1;
7762 if (s->selected > 0) {
7766 ref_scroll_up(s, 1);
7767 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7777 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7778 s->selected -= MIN(nscroll, s->selected);
7779 ref_scroll_up(s, MAX(0, nscroll));
7780 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7786 if (s->selected < s->ndisplayed - 1) {
7790 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7791 /* can't scroll any further */
7795 ref_scroll_down(view, 1);
7805 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7806 /* can't scroll any further; move cursor down */
7807 if (s->selected < s->ndisplayed - 1)
7808 s->selected += MIN(nscroll,
7809 s->ndisplayed - s->selected - 1);
7810 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7811 s->selected += s->ndisplayed - s->selected - 1;
7815 ref_scroll_down(view, nscroll);
7820 err = tog_load_refs(s->repo, s->sort_by_date);
7823 ref_view_free_refs(s);
7824 err = ref_view_load_refs(s);
7827 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7828 s->selected = view->nlines - 2;
7842 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7847 static const struct got_error *
7848 cmd_ref(int argc, char *argv[])
7850 const struct got_error *error;
7851 struct got_repository *repo = NULL;
7852 struct got_worktree *worktree = NULL;
7853 char *cwd = NULL, *repo_path = NULL;
7855 struct tog_view *view;
7856 int *pack_fds = NULL;
7858 while ((ch = getopt(argc, argv, "r:")) != -1) {
7861 repo_path = realpath(optarg, NULL);
7862 if (repo_path == NULL)
7863 return got_error_from_errno2("realpath",
7878 error = got_repo_pack_fds_open(&pack_fds);
7882 if (repo_path == NULL) {
7883 cwd = getcwd(NULL, 0);
7885 return got_error_from_errno("getcwd");
7886 error = got_worktree_open(&worktree, cwd);
7887 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7891 strdup(got_worktree_get_repo_path(worktree));
7893 repo_path = strdup(cwd);
7894 if (repo_path == NULL) {
7895 error = got_error_from_errno("strdup");
7900 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7906 error = apply_unveil(got_repo_get_path(repo), NULL);
7910 error = tog_load_refs(repo, 0);
7914 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7916 error = got_error_from_errno("view_open");
7920 error = open_ref_view(view, repo);
7925 /* Release work tree lock. */
7926 got_worktree_close(worktree);
7929 error = view_loop(view);
7934 const struct got_error *close_err = got_repo_close(repo);
7939 const struct got_error *pack_err =
7940 got_repo_pack_fds_close(pack_fds);
7949 * If view was scrolled down to move the selected line into view when opening a
7950 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7953 offset_selection_up(struct tog_view *view)
7955 switch (view->type) {
7956 case TOG_VIEW_BLAME: {
7957 struct tog_blame_view_state *s = &view->state.blame;
7958 if (s->first_displayed_line == 1) {
7959 s->selected_line = MAX(s->selected_line - view->offset,
7963 if (s->first_displayed_line > view->offset)
7964 s->first_displayed_line -= view->offset;
7966 s->first_displayed_line = 1;
7967 s->selected_line += view->offset;
7971 log_scroll_up(&view->state.log, view->offset);
7972 view->state.log.selected += view->offset;
7975 ref_scroll_up(&view->state.ref, view->offset);
7976 view->state.ref.selected += view->offset;
7979 tree_scroll_up(&view->state.tree, view->offset);
7980 view->state.tree.selected += view->offset;
7990 * If the selected line is in the section of screen covered by the bottom split,
7991 * scroll down offset lines to move it into view and index its new position.
7993 static const struct got_error *
7994 offset_selection_down(struct tog_view *view)
7996 const struct got_error *err = NULL;
7997 const struct got_error *(*scrolld)(struct tog_view *, int);
7998 int *selected = NULL;
8001 switch (view->type) {
8002 case TOG_VIEW_BLAME: {
8003 struct tog_blame_view_state *s = &view->state.blame;
8006 if (s->selected_line > view->nlines - header) {
8007 offset = abs(view->nlines - s->selected_line - header);
8008 s->first_displayed_line += offset;
8009 s->selected_line -= offset;
8010 view->offset = offset;
8014 case TOG_VIEW_LOG: {
8015 struct tog_log_view_state *s = &view->state.log;
8016 scrolld = &log_scroll_down;
8017 header = view_is_parent_view(view) ? 3 : 2;
8018 selected = &s->selected;
8021 case TOG_VIEW_REF: {
8022 struct tog_ref_view_state *s = &view->state.ref;
8023 scrolld = &ref_scroll_down;
8025 selected = &s->selected;
8028 case TOG_VIEW_TREE: {
8029 struct tog_tree_view_state *s = &view->state.tree;
8030 scrolld = &tree_scroll_down;
8032 selected = &s->selected;
8042 if (selected && *selected > view->nlines - header) {
8043 offset = abs(view->nlines - *selected - header);
8044 view->offset = offset;
8045 if (scrolld && offset) {
8046 err = scrolld(view, offset);
8047 *selected -= offset;
8055 list_commands(FILE *fp)
8059 fprintf(fp, "commands:");
8060 for (i = 0; i < nitems(tog_commands); i++) {
8061 const struct tog_cmd *cmd = &tog_commands[i];
8062 fprintf(fp, " %s", cmd->name);
8068 usage(int hflag, int status)
8070 FILE *fp = (status == 0) ? stdout : stderr;
8072 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8075 fprintf(fp, "lazy usage: %s path\n", getprogname());
8082 make_argv(int argc, ...)
8090 argv = calloc(argc, sizeof(char *));
8093 for (i = 0; i < argc; i++) {
8094 argv[i] = strdup(va_arg(ap, char *));
8095 if (argv[i] == NULL)
8104 * Try to convert 'tog path' into a 'tog log path' command.
8105 * The user could simply have mistyped the command rather than knowingly
8106 * provided a path. So check whether argv[0] can in fact be resolved
8107 * to a path in the HEAD commit and print a special error if not.
8108 * This hack is for mpi@ <3
8110 static const struct got_error *
8111 tog_log_with_path(int argc, char *argv[])
8113 const struct got_error *error = NULL, *close_err;
8114 const struct tog_cmd *cmd = NULL;
8115 struct got_repository *repo = NULL;
8116 struct got_worktree *worktree = NULL;
8117 struct got_object_id *commit_id = NULL, *id = NULL;
8118 struct got_commit_object *commit = NULL;
8119 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8120 char *commit_id_str = NULL, **cmd_argv = NULL;
8121 int *pack_fds = NULL;
8123 cwd = getcwd(NULL, 0);
8125 return got_error_from_errno("getcwd");
8127 error = got_repo_pack_fds_open(&pack_fds);
8131 error = got_worktree_open(&worktree, cwd);
8132 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8136 repo_path = strdup(got_worktree_get_repo_path(worktree));
8138 repo_path = strdup(cwd);
8139 if (repo_path == NULL) {
8140 error = got_error_from_errno("strdup");
8144 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8148 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8153 error = tog_load_refs(repo, 0);
8156 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8157 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8158 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8163 got_worktree_close(worktree);
8167 error = got_object_open_as_commit(&commit, repo, commit_id);
8171 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8173 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8175 fprintf(stderr, "%s: '%s' is no known command or path\n",
8176 getprogname(), argv[0]);
8181 close_err = got_repo_close(repo);
8186 error = got_object_id_str(&commit_id_str, commit_id);
8190 cmd = &tog_commands[0]; /* log */
8192 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8193 error = cmd->cmd_main(argc, cmd_argv);
8196 close_err = got_repo_close(repo);
8201 got_object_commit_close(commit);
8203 got_worktree_close(worktree);
8205 const struct got_error *pack_err =
8206 got_repo_pack_fds_close(pack_fds);
8211 free(commit_id_str);
8218 for (i = 0; i < argc; i++)
8227 main(int argc, char *argv[])
8229 const struct got_error *error = NULL;
8230 const struct tog_cmd *cmd = NULL;
8231 int ch, hflag = 0, Vflag = 0;
8232 char **cmd_argv = NULL;
8233 static const struct option longopts[] = {
8234 { "version", no_argument, NULL, 'V' },
8237 char *diff_algo_str = NULL;
8239 setlocale(LC_CTYPE, "");
8241 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8261 got_version_print_str();
8266 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8274 /* Build an argument vector which runs a default command. */
8275 cmd = &tog_commands[0];
8277 cmd_argv = make_argv(argc, cmd->name);
8281 /* Did the user specify a command? */
8282 for (i = 0; i < nitems(tog_commands); i++) {
8283 if (strncmp(tog_commands[i].name, argv[0],
8284 strlen(argv[0])) == 0) {
8285 cmd = &tog_commands[i];
8291 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8292 if (diff_algo_str) {
8293 if (strcasecmp(diff_algo_str, "patience") == 0)
8294 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8295 if (strcasecmp(diff_algo_str, "myers") == 0)
8296 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8302 /* No command specified; try log with a path */
8303 error = tog_log_with_path(argc, argv);
8308 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8315 for (i = 0; i < argc; i++)
8320 if (error && error->code != GOT_ERR_CANCELLED)
8321 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);