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 && v->child->type == TOG_VIEW_LOG)
991 v->child->nscrolled = y - v->child->begin_y;
992 else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
993 v->nscrolled = v->child->begin_y - y;
995 if (v->child->resized_x)
996 v->child->begin_x = v->child->resized_x;
998 v->child->begin_x -= resize;
1000 v->child->begin_x += resize;
1001 if (v->child->begin_x < 11) {
1003 v->child->begin_x = 11;
1004 } else if (v->child->begin_x > COLS - 1) {
1006 v->child->begin_x = COLS - 1;
1008 v->child->resized_x = v->child->begin_x;
1011 v->child->mode = v->mode;
1012 v->child->nlines = v->lines - v->child->begin_y;
1013 v->child->ncols = v->cols - v->child->begin_x;
1016 err = view_fullscreen(v);
1019 err = view_splitscreen(v->child);
1023 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1024 err = offset_selection_down(v->child);
1030 err = request_log_commits(v);
1031 else if (v->child->nscrolled)
1032 err = request_log_commits(v->child);
1034 v->resize = v->child->resize = 0;
1040 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1042 struct tog_view *v = src->child ? src->child : src;
1044 dst->resized_x = v->resized_x;
1045 dst->resized_y = v->resized_y;
1048 static const struct got_error *
1049 view_close_child(struct tog_view *view)
1051 const struct got_error *err = NULL;
1053 if (view->child == NULL)
1056 err = view_close(view->child);
1061 static const struct got_error *
1062 view_set_child(struct tog_view *view, struct tog_view *child)
1064 const struct got_error *err = NULL;
1066 view->child = child;
1067 child->parent = view;
1069 err = view_resize(view);
1073 if (view->child->resized_x || view->child->resized_y)
1074 err = view_resize_split(view, 0);
1080 tog_resizeterm(void)
1083 struct winsize size;
1085 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1086 cols = 80; /* Default */
1090 lines = size.ws_row;
1092 resize_term(lines, cols);
1095 static const struct got_error *
1096 view_search_start(struct tog_view *view)
1098 const struct got_error *err = NULL;
1099 struct tog_view *v = view;
1103 if (view->search_started) {
1104 regfree(&view->regex);
1105 view->searching = 0;
1106 memset(&view->regmatch, 0, sizeof(view->regmatch));
1108 view->search_started = 0;
1110 if (view->nlines < 1)
1113 if (view_is_hsplit_top(view))
1116 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1117 wclrtoeol(v->window);
1119 nodelay(view->window, FALSE); /* block for search term input */
1122 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1123 wrefresh(v->window);
1126 nodelay(view->window, TRUE);
1130 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1131 err = view->search_start(view);
1133 regfree(&view->regex);
1136 view->search_started = 1;
1137 view->searching = TOG_SEARCH_FORWARD;
1138 view->search_next_done = 0;
1139 view->search_next(view);
1145 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1146 static const struct got_error *
1147 switch_split(struct tog_view *view)
1149 const struct got_error *err = NULL;
1150 struct tog_view *v = NULL;
1157 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1158 v->mode = TOG_VIEW_SPLIT_VERT;
1160 v->mode = TOG_VIEW_SPLIT_HRZN;
1164 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1165 v->mode = TOG_VIEW_SPLIT_NONE;
1167 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1168 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1169 v->child->begin_y = v->child->resized_y;
1170 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1171 v->child->begin_x = v->child->resized_x;
1174 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1176 v->child->ncols = COLS;
1177 v->child->nscrolled = LINES - v->child->nlines;
1179 err = view_init_hsplit(v, v->child->begin_y);
1183 v->child->mode = v->mode;
1184 v->child->nlines = v->lines - v->child->begin_y;
1187 err = view_fullscreen(v);
1190 err = view_splitscreen(v->child);
1194 if (v->mode == TOG_VIEW_SPLIT_NONE)
1195 v->mode = TOG_VIEW_SPLIT_VERT;
1196 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1197 err = offset_selection_down(v);
1198 err = offset_selection_down(v->child);
1200 offset_selection_up(v);
1201 offset_selection_up(v->child);
1203 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1204 err = request_log_commits(v);
1205 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1206 err = request_log_commits(v->child);
1212 * Compute view->count from numeric input. Assign total to view->count and
1213 * return first non-numeric key entered.
1216 get_compound_key(struct tog_view *view, int c)
1218 struct tog_view *v = view;
1221 if (view_is_hsplit_top(view))
1223 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1227 cbreak(); /* block for input */
1228 wmove(v->window, v->nlines - 1, 0);
1229 wclrtoeol(v->window);
1230 waddch(v->window, ':');
1233 x = getcurx(v->window);
1234 if (x != ERR && x < view->ncols) {
1235 waddch(v->window, c);
1236 wrefresh(v->window);
1240 * Don't overflow. Max valid request should be the greatest
1241 * between the longest and total lines; cap at 10 million.
1246 n = n * 10 + (c - '0');
1247 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1249 /* Massage excessive or inapplicable values at the input handler. */
1255 static const struct got_error *
1256 view_input(struct tog_view **new, int *done, struct tog_view *view,
1257 struct tog_view_list_head *views)
1259 const struct got_error *err = NULL;
1265 /* Clear "no matches" indicator. */
1266 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1267 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1268 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1272 if (view->searching && !view->search_next_done) {
1273 errcode = pthread_mutex_unlock(&tog_mutex);
1275 return got_error_set_errno(errcode,
1276 "pthread_mutex_unlock");
1278 errcode = pthread_mutex_lock(&tog_mutex);
1280 return got_error_set_errno(errcode,
1281 "pthread_mutex_lock");
1282 view->search_next(view);
1286 nodelay(view->window, FALSE);
1287 /* Allow threads to make progress while we are waiting for input. */
1288 errcode = pthread_mutex_unlock(&tog_mutex);
1290 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1291 /* If we have an unfinished count, let C-g or backspace abort. */
1292 if (view->count && --view->count) {
1294 nodelay(view->window, TRUE);
1295 ch = wgetch(view->window);
1296 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1301 ch = wgetch(view->window);
1302 if (ch >= '1' && ch <= '9')
1303 view->ch = ch = get_compound_key(view, ch);
1305 errcode = pthread_mutex_lock(&tog_mutex);
1307 return got_error_set_errno(errcode, "pthread_mutex_lock");
1308 nodelay(view->window, TRUE);
1310 if (tog_sigwinch_received || tog_sigcont_received) {
1312 tog_sigwinch_received = 0;
1313 tog_sigcont_received = 0;
1314 TAILQ_FOREACH(v, views, entry) {
1315 err = view_resize(v);
1318 err = v->input(new, v, KEY_RESIZE);
1322 err = view_resize(v->child);
1325 err = v->child->input(new, v->child,
1329 if (v->child->resized_x || v->child->resized_y) {
1330 err = view_resize_split(v, 0);
1343 view->child->focussed = 1;
1344 view->focus_child = 1;
1345 } else if (view->parent) {
1347 view->parent->focussed = 1;
1348 view->parent->focus_child = 0;
1349 if (!view_is_splitscreen(view)) {
1350 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1351 view->parent->type == TOG_VIEW_LOG) {
1352 err = request_log_commits(view->parent);
1356 offset_selection_up(view->parent);
1357 err = view_fullscreen(view->parent);
1364 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1365 if (view->parent->type == TOG_VIEW_LOG) {
1366 /* might need more commits to fill fullscreen */
1367 err = request_log_commits(view->parent);
1371 offset_selection_up(view->parent);
1373 err = view->input(new, view, ch);
1381 if (view_is_parent_view(view)) {
1382 if (view->child == NULL)
1384 if (view_is_splitscreen(view->child)) {
1386 view->child->focussed = 1;
1387 err = view_fullscreen(view->child);
1389 err = view_splitscreen(view->child);
1391 err = view_resize_split(view, 0);
1395 err = view->child->input(new, view->child,
1398 if (view_is_splitscreen(view)) {
1399 view->parent->focussed = 0;
1401 err = view_fullscreen(view);
1403 err = view_splitscreen(view);
1404 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1405 err = view_resize(view->parent);
1407 err = view_resize_split(view, 0);
1411 err = view->input(new, view, KEY_RESIZE);
1415 if (view->type == TOG_VIEW_LOG) {
1416 err = request_log_commits(view);
1421 err = offset_selection_down(view->parent);
1423 err = offset_selection_down(view);
1427 err = switch_split(view);
1430 err = view_resize_split(view, -1);
1433 err = view_resize_split(view, 1);
1439 if (view->search_start)
1440 view_search_start(view);
1442 err = view->input(new, view, ch);
1446 if (view->search_started && view->search_next) {
1447 view->searching = (ch == 'n' ?
1448 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1449 view->search_next_done = 0;
1450 view->search_next(view);
1452 err = view->input(new, view, ch);
1455 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1456 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1458 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1459 TAILQ_FOREACH(v, views, entry) {
1465 if (v->child && v->child->reset) {
1466 err = v->child->reset(v->child);
1473 err = view->input(new, view, ch);
1481 view_needs_focus_indication(struct tog_view *view)
1483 if (view_is_parent_view(view)) {
1484 if (view->child == NULL || view->child->focussed)
1486 if (!view_is_splitscreen(view->child))
1488 } else if (!view_is_splitscreen(view))
1491 return view->focussed;
1494 static const struct got_error *
1495 view_loop(struct tog_view *view)
1497 const struct got_error *err = NULL;
1498 struct tog_view_list_head views;
1499 struct tog_view *new_view;
1501 int fast_refresh = 10;
1502 int done = 0, errcode;
1504 mode = getenv("TOG_VIEW_SPLIT_MODE");
1505 if (!mode || !(*mode == 'h' || *mode == 'H'))
1506 view->mode = TOG_VIEW_SPLIT_VERT;
1508 view->mode = TOG_VIEW_SPLIT_HRZN;
1510 errcode = pthread_mutex_lock(&tog_mutex);
1512 return got_error_set_errno(errcode, "pthread_mutex_lock");
1515 TAILQ_INSERT_HEAD(&views, view, entry);
1518 err = view->show(view);
1523 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1524 !tog_fatal_signal_received()) {
1525 /* Refresh fast during initialization, then become slower. */
1526 if (fast_refresh && fast_refresh-- == 0)
1527 halfdelay(10); /* switch to once per second */
1529 err = view_input(&new_view, &done, view, &views);
1533 struct tog_view *v, *prev = NULL;
1535 if (view_is_parent_view(view))
1536 prev = TAILQ_PREV(view, tog_view_list_head,
1538 else if (view->parent)
1539 prev = view->parent;
1542 view->parent->child = NULL;
1543 view->parent->focus_child = 0;
1544 /* Restore fullscreen line height. */
1545 view->parent->nlines = view->parent->lines;
1546 err = view_resize(view->parent);
1549 /* Make resized splits persist. */
1550 view_transfer_size(view->parent, view);
1552 TAILQ_REMOVE(&views, view, entry);
1554 err = view_close(view);
1559 TAILQ_FOREACH(v, &views, entry) {
1563 if (view == NULL && new_view == NULL) {
1564 /* No view has focus. Try to pick one. */
1567 else if (!TAILQ_EMPTY(&views)) {
1568 view = TAILQ_LAST(&views,
1569 tog_view_list_head);
1572 if (view->focus_child) {
1573 view->child->focussed = 1;
1581 struct tog_view *v, *t;
1582 /* Only allow one parent view per type. */
1583 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1584 if (v->type != new_view->type)
1586 TAILQ_REMOVE(&views, v, entry);
1587 err = view_close(v);
1592 TAILQ_INSERT_TAIL(&views, new_view, entry);
1596 if (view_is_parent_view(view)) {
1597 if (view->child && view->child->focussed)
1600 if (view->parent && view->parent->focussed)
1601 view = view->parent;
1603 show_panel(view->panel);
1604 if (view->child && view_is_splitscreen(view->child))
1605 show_panel(view->child->panel);
1606 if (view->parent && view_is_splitscreen(view)) {
1607 err = view->parent->show(view->parent);
1611 err = view->show(view);
1615 err = view->child->show(view->child);
1624 while (!TAILQ_EMPTY(&views)) {
1625 const struct got_error *close_err;
1626 view = TAILQ_FIRST(&views);
1627 TAILQ_REMOVE(&views, view, entry);
1628 close_err = view_close(view);
1629 if (close_err && err == NULL)
1633 errcode = pthread_mutex_unlock(&tog_mutex);
1634 if (errcode && err == NULL)
1635 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1645 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1650 /* Create newly allocated wide-character string equivalent to a byte string. */
1651 static const struct got_error *
1652 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1655 const struct got_error *err = NULL;
1658 *wlen = mbstowcs(NULL, s, 0);
1659 if (*wlen == (size_t)-1) {
1661 if (errno != EILSEQ)
1662 return got_error_from_errno("mbstowcs");
1664 /* byte string invalid in current encoding; try to "fix" it */
1665 err = got_mbsavis(&vis, &vislen, s);
1668 *wlen = mbstowcs(NULL, vis, 0);
1669 if (*wlen == (size_t)-1) {
1670 err = got_error_from_errno("mbstowcs"); /* give up */
1675 *ws = calloc(*wlen + 1, sizeof(**ws));
1677 err = got_error_from_errno("calloc");
1681 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1682 err = got_error_from_errno("mbstowcs");
1693 static const struct got_error *
1694 expand_tab(char **ptr, const char *src)
1697 size_t len, n, idx = 0, sz = 0;
1700 n = len = strlen(src);
1701 dst = malloc(n + 1);
1703 return got_error_from_errno("malloc");
1705 while (idx < len && src[idx]) {
1706 const char c = src[idx];
1709 size_t nb = TABSIZE - sz % TABSIZE;
1712 p = realloc(dst, n + nb);
1715 return got_error_from_errno("realloc");
1720 memset(dst + sz, ' ', nb);
1723 dst[sz++] = src[idx];
1733 * Advance at most n columns from wline starting at offset off.
1734 * Return the index to the first character after the span operation.
1735 * Return the combined column width of all spanned wide character in
1739 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1741 int width, i, cols = 0;
1748 for (i = off; wline[i] != L'\0'; ++i) {
1749 if (wline[i] == L'\t')
1750 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1752 width = wcwidth(wline[i]);
1759 if (cols + width > n)
1769 * Format a line for display, ensuring that it won't overflow a width limit.
1770 * With scrolling, the width returned refers to the scrolled version of the
1771 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1773 static const struct got_error *
1774 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1775 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1777 const struct got_error *err = NULL;
1779 wchar_t *wline = NULL;
1788 err = expand_tab(&exstr, line);
1793 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1798 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1800 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1801 wline[wlen - 1] = L'\0';
1804 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1805 wline[wlen - 1] = L'\0';
1809 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1815 *scrollxp = scrollx;
1823 static const struct got_error*
1824 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1825 struct got_object_id *id, struct got_repository *repo)
1827 static const struct got_error *err = NULL;
1828 struct got_reflist_entry *re;
1834 TAILQ_FOREACH(re, refs, entry) {
1835 struct got_tag_object *tag = NULL;
1836 struct got_object_id *ref_id;
1839 name = got_ref_get_name(re->ref);
1840 if (strcmp(name, GOT_REF_HEAD) == 0)
1842 if (strncmp(name, "refs/", 5) == 0)
1844 if (strncmp(name, "got/", 4) == 0 &&
1845 strncmp(name, "got/backup/", 11) != 0)
1847 if (strncmp(name, "heads/", 6) == 0)
1849 if (strncmp(name, "remotes/", 8) == 0) {
1851 s = strstr(name, "/" GOT_REF_HEAD);
1852 if (s != NULL && s[strlen(s)] == '\0')
1855 err = got_ref_resolve(&ref_id, repo, re->ref);
1858 if (strncmp(name, "tags/", 5) == 0) {
1859 err = got_object_open_as_tag(&tag, repo, ref_id);
1861 if (err->code != GOT_ERR_OBJ_TYPE) {
1865 /* Ref points at something other than a tag. */
1870 cmp = got_object_id_cmp(tag ?
1871 got_object_tag_get_object_id(tag) : ref_id, id);
1874 got_object_tag_close(tag);
1878 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1879 s ? ", " : "", name) == -1) {
1880 err = got_error_from_errno("asprintf");
1891 static const struct got_error *
1892 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1897 smallerthan = strchr(author, '<');
1898 if (smallerthan && smallerthan[1] != '\0')
1899 author = smallerthan + 1;
1900 author[strcspn(author, "@>")] = '\0';
1901 return format_line(wauthor, author_width, NULL, author, 0, limit,
1905 static const struct got_error *
1906 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1907 struct got_object_id *id, const size_t date_display_cols,
1908 int author_display_cols)
1910 struct tog_log_view_state *s = &view->state.log;
1911 const struct got_error *err = NULL;
1912 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1913 char *logmsg0 = NULL, *logmsg = NULL;
1914 char *author = NULL;
1915 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1916 int author_width, logmsg_width;
1917 char *newline, *line = NULL;
1918 int col, limit, scrollx;
1919 const int avail = view->ncols;
1921 time_t committer_time;
1922 struct tog_color *tc;
1924 committer_time = got_object_commit_get_committer_time(commit);
1925 if (gmtime_r(&committer_time, &tm) == NULL)
1926 return got_error_from_errno("gmtime_r");
1927 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1928 return got_error(GOT_ERR_NO_SPACE);
1930 if (avail <= date_display_cols)
1931 limit = MIN(sizeof(datebuf) - 1, avail);
1933 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1934 tc = get_color(&s->colors, TOG_COLOR_DATE);
1936 wattr_on(view->window,
1937 COLOR_PAIR(tc->colorpair), NULL);
1938 waddnstr(view->window, datebuf, limit);
1940 wattr_off(view->window,
1941 COLOR_PAIR(tc->colorpair), NULL);
1948 err = got_object_id_str(&id_str, id);
1951 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1953 wattr_on(view->window,
1954 COLOR_PAIR(tc->colorpair), NULL);
1955 wprintw(view->window, "%.8s ", id_str);
1957 wattr_off(view->window,
1958 COLOR_PAIR(tc->colorpair), NULL);
1965 author = strdup(got_object_commit_get_author(commit));
1966 if (author == NULL) {
1967 err = got_error_from_errno("strdup");
1970 err = format_author(&wauthor, &author_width, author, avail - col, col);
1973 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1975 wattr_on(view->window,
1976 COLOR_PAIR(tc->colorpair), NULL);
1977 waddwstr(view->window, wauthor);
1979 wattr_off(view->window,
1980 COLOR_PAIR(tc->colorpair), NULL);
1981 col += author_width;
1982 while (col < avail && author_width < author_display_cols + 2) {
1983 waddch(view->window, ' ');
1990 err = got_object_commit_get_logmsg(&logmsg0, commit);
1994 while (*logmsg == '\n')
1996 newline = strchr(logmsg, '\n');
1999 limit = avail - col;
2000 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2001 limit--; /* for the border */
2002 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2006 waddwstr(view->window, &wlogmsg[scrollx]);
2007 col += MAX(logmsg_width, 0);
2008 while (col < avail) {
2009 waddch(view->window, ' ');
2021 static struct commit_queue_entry *
2022 alloc_commit_queue_entry(struct got_commit_object *commit,
2023 struct got_object_id *id)
2025 struct commit_queue_entry *entry;
2027 entry = calloc(1, sizeof(*entry));
2032 entry->commit = commit;
2037 pop_commit(struct commit_queue *commits)
2039 struct commit_queue_entry *entry;
2041 entry = TAILQ_FIRST(&commits->head);
2042 TAILQ_REMOVE(&commits->head, entry, entry);
2043 got_object_commit_close(entry->commit);
2044 commits->ncommits--;
2045 /* Don't free entry->id! It is owned by the commit graph. */
2050 free_commits(struct commit_queue *commits)
2052 while (!TAILQ_EMPTY(&commits->head))
2053 pop_commit(commits);
2056 static const struct got_error *
2057 match_commit(int *have_match, struct got_object_id *id,
2058 struct got_commit_object *commit, regex_t *regex)
2060 const struct got_error *err = NULL;
2061 regmatch_t regmatch;
2062 char *id_str = NULL, *logmsg = NULL;
2066 err = got_object_id_str(&id_str, id);
2070 err = got_object_commit_get_logmsg(&logmsg, commit);
2074 if (regexec(regex, got_object_commit_get_author(commit), 1,
2075 ®match, 0) == 0 ||
2076 regexec(regex, got_object_commit_get_committer(commit), 1,
2077 ®match, 0) == 0 ||
2078 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2079 regexec(regex, logmsg, 1, ®match, 0) == 0)
2087 static const struct got_error *
2088 queue_commits(struct tog_log_thread_args *a)
2090 const struct got_error *err = NULL;
2093 * We keep all commits open throughout the lifetime of the log
2094 * view in order to avoid having to re-fetch commits from disk
2095 * while updating the display.
2098 struct got_object_id *id;
2099 struct got_commit_object *commit;
2100 struct commit_queue_entry *entry;
2103 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2105 if (err || id == NULL)
2108 err = got_object_open_as_commit(&commit, a->repo, id);
2111 entry = alloc_commit_queue_entry(commit, id);
2112 if (entry == NULL) {
2113 err = got_error_from_errno("alloc_commit_queue_entry");
2117 errcode = pthread_mutex_lock(&tog_mutex);
2119 err = got_error_set_errno(errcode,
2120 "pthread_mutex_lock");
2124 entry->idx = a->commits->ncommits;
2125 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2126 a->commits->ncommits++;
2128 if (*a->searching == TOG_SEARCH_FORWARD &&
2129 !*a->search_next_done) {
2131 err = match_commit(&have_match, id, commit, a->regex);
2135 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2138 errcode = pthread_mutex_unlock(&tog_mutex);
2139 if (errcode && err == NULL)
2140 err = got_error_set_errno(errcode,
2141 "pthread_mutex_unlock");
2144 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2150 select_commit(struct tog_log_view_state *s)
2152 struct commit_queue_entry *entry;
2155 entry = s->first_displayed_entry;
2157 if (ncommits == s->selected) {
2158 s->selected_entry = entry;
2161 entry = TAILQ_NEXT(entry, entry);
2166 static const struct got_error *
2167 draw_commits(struct tog_view *view)
2169 const struct got_error *err = NULL;
2170 struct tog_log_view_state *s = &view->state.log;
2171 struct commit_queue_entry *entry = s->selected_entry;
2172 const int limit = view->nlines;
2174 int ncommits, author_cols = 4;
2175 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2176 char *refs_str = NULL;
2178 struct tog_color *tc;
2179 static const size_t date_display_cols = 12;
2181 if (s->selected_entry &&
2182 !(view->searching && view->search_next_done == 0)) {
2183 struct got_reflist_head *refs;
2184 err = got_object_id_str(&id_str, s->selected_entry->id);
2187 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2188 s->selected_entry->id);
2190 err = build_refs_str(&refs_str, refs,
2191 s->selected_entry->id, s->repo);
2197 if (s->thread_args.commits_needed == 0)
2198 halfdelay(10); /* disable fast refresh */
2200 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2201 if (asprintf(&ncommits_str, " [%d/%d] %s",
2202 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2203 (view->searching && !view->search_next_done) ?
2204 "searching..." : "loading...") == -1) {
2205 err = got_error_from_errno("asprintf");
2209 const char *search_str = NULL;
2211 if (view->searching) {
2212 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2213 search_str = "no more matches";
2214 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2215 search_str = "no matches found";
2216 else if (!view->search_next_done)
2217 search_str = "searching...";
2220 if (asprintf(&ncommits_str, " [%d/%d] %s",
2221 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2222 search_str ? search_str :
2223 (refs_str ? refs_str : "")) == -1) {
2224 err = got_error_from_errno("asprintf");
2229 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2230 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2231 "........................................",
2232 s->in_repo_path, ncommits_str) == -1) {
2233 err = got_error_from_errno("asprintf");
2237 } else if (asprintf(&header, "commit %s%s",
2238 id_str ? id_str : "........................................",
2239 ncommits_str) == -1) {
2240 err = got_error_from_errno("asprintf");
2244 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2248 werase(view->window);
2250 if (view_needs_focus_indication(view))
2251 wstandout(view->window);
2252 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2254 wattr_on(view->window,
2255 COLOR_PAIR(tc->colorpair), NULL);
2256 waddwstr(view->window, wline);
2258 wattr_off(view->window,
2259 COLOR_PAIR(tc->colorpair), NULL);
2260 while (width < view->ncols) {
2261 waddch(view->window, ' ');
2264 if (view_needs_focus_indication(view))
2265 wstandend(view->window);
2270 /* Grow author column size if necessary, and set view->maxx. */
2271 entry = s->first_displayed_entry;
2275 char *author, *eol, *msg, *msg0;
2276 wchar_t *wauthor, *wmsg;
2278 if (ncommits >= limit - 1)
2280 author = strdup(got_object_commit_get_author(entry->commit));
2281 if (author == NULL) {
2282 err = got_error_from_errno("strdup");
2285 err = format_author(&wauthor, &width, author, COLS,
2287 if (author_cols < width)
2288 author_cols = width;
2291 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2295 while (*msg == '\n')
2297 if ((eol = strchr(msg, '\n')))
2299 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2300 date_display_cols + author_cols, 0);
2303 view->maxx = MAX(view->maxx, width);
2307 entry = TAILQ_NEXT(entry, entry);
2310 entry = s->first_displayed_entry;
2311 s->last_displayed_entry = s->first_displayed_entry;
2314 if (ncommits >= limit - 1)
2316 if (ncommits == s->selected)
2317 wstandout(view->window);
2318 err = draw_commit(view, entry->commit, entry->id,
2319 date_display_cols, author_cols);
2320 if (ncommits == s->selected)
2321 wstandend(view->window);
2325 s->last_displayed_entry = entry;
2326 entry = TAILQ_NEXT(entry, entry);
2339 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2341 struct commit_queue_entry *entry;
2344 entry = TAILQ_FIRST(&s->commits.head);
2345 if (s->first_displayed_entry == entry)
2348 entry = s->first_displayed_entry;
2349 while (entry && nscrolled < maxscroll) {
2350 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2352 s->first_displayed_entry = entry;
2358 static const struct got_error *
2359 trigger_log_thread(struct tog_view *view, int wait)
2361 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2364 halfdelay(1); /* fast refresh while loading commits */
2366 while (!ta->log_complete && !tog_thread_error &&
2367 (ta->commits_needed > 0 || ta->load_all)) {
2368 /* Wake the log thread. */
2369 errcode = pthread_cond_signal(&ta->need_commits);
2371 return got_error_set_errno(errcode,
2372 "pthread_cond_signal");
2375 * The mutex will be released while the view loop waits
2376 * in wgetch(), at which time the log thread will run.
2381 /* Display progress update in log view. */
2382 show_log_view(view);
2386 /* Wait right here while next commit is being loaded. */
2387 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2389 return got_error_set_errno(errcode,
2390 "pthread_cond_wait");
2392 /* Display progress update in log view. */
2393 show_log_view(view);
2401 static const struct got_error *
2402 request_log_commits(struct tog_view *view)
2404 struct tog_log_view_state *state = &view->state.log;
2405 const struct got_error *err = NULL;
2407 if (state->thread_args.log_complete)
2410 state->thread_args.commits_needed += view->nscrolled;
2411 err = trigger_log_thread(view, 1);
2412 view->nscrolled = 0;
2417 static const struct got_error *
2418 log_scroll_down(struct tog_view *view, int maxscroll)
2420 struct tog_log_view_state *s = &view->state.log;
2421 const struct got_error *err = NULL;
2422 struct commit_queue_entry *pentry;
2423 int nscrolled = 0, ncommits_needed;
2425 if (s->last_displayed_entry == NULL)
2428 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2429 if (s->commits.ncommits < ncommits_needed &&
2430 !s->thread_args.log_complete) {
2432 * Ask the log thread for required amount of commits.
2434 s->thread_args.commits_needed += maxscroll;
2435 err = trigger_log_thread(view, 1);
2441 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2442 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2445 s->last_displayed_entry = pentry ?
2446 pentry : s->last_displayed_entry;;
2448 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2451 s->first_displayed_entry = pentry;
2452 } while (++nscrolled < maxscroll);
2454 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2455 view->nscrolled += nscrolled;
2457 view->nscrolled = 0;
2462 static const struct got_error *
2463 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2464 struct got_commit_object *commit, struct got_object_id *commit_id,
2465 struct tog_view *log_view, struct got_repository *repo)
2467 const struct got_error *err;
2468 struct got_object_qid *parent_id;
2469 struct tog_view *diff_view;
2471 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2472 if (diff_view == NULL)
2473 return got_error_from_errno("view_open");
2475 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2476 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2477 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2479 *new_view = diff_view;
2483 static const struct got_error *
2484 tree_view_visit_subtree(struct tog_tree_view_state *s,
2485 struct got_tree_object *subtree)
2487 struct tog_parent_tree *parent;
2489 parent = calloc(1, sizeof(*parent));
2491 return got_error_from_errno("calloc");
2493 parent->tree = s->tree;
2494 parent->first_displayed_entry = s->first_displayed_entry;
2495 parent->selected_entry = s->selected_entry;
2496 parent->selected = s->selected;
2497 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2500 s->first_displayed_entry = NULL;
2504 static const struct got_error *
2505 tree_view_walk_path(struct tog_tree_view_state *s,
2506 struct got_commit_object *commit, const char *path)
2508 const struct got_error *err = NULL;
2509 struct got_tree_object *tree = NULL;
2511 char *slash, *subpath = NULL;
2513 /* Walk the path and open corresponding tree objects. */
2516 struct got_tree_entry *te;
2517 struct got_object_id *tree_id;
2523 /* Ensure the correct subtree entry is selected. */
2524 slash = strchr(p, '/');
2526 te_name = strdup(p);
2528 te_name = strndup(p, slash - p);
2529 if (te_name == NULL) {
2530 err = got_error_from_errno("strndup");
2533 te = got_object_tree_find_entry(s->tree, te_name);
2535 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2540 s->first_displayed_entry = s->selected_entry = te;
2542 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2543 break; /* jump to this file's entry */
2545 slash = strchr(p, '/');
2547 subpath = strndup(path, slash - path);
2549 subpath = strdup(path);
2550 if (subpath == NULL) {
2551 err = got_error_from_errno("strdup");
2555 err = got_object_id_by_path(&tree_id, s->repo, commit,
2560 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2565 err = tree_view_visit_subtree(s, tree);
2567 got_object_tree_close(tree);
2581 static const struct got_error *
2582 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2583 struct commit_queue_entry *entry, const char *path,
2584 const char *head_ref_name, struct got_repository *repo)
2586 const struct got_error *err = NULL;
2587 struct tog_tree_view_state *s;
2588 struct tog_view *tree_view;
2590 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2591 if (tree_view == NULL)
2592 return got_error_from_errno("view_open");
2594 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2597 s = &tree_view->state.tree;
2599 *new_view = tree_view;
2601 if (got_path_is_root_dir(path))
2604 return tree_view_walk_path(s, entry->commit, path);
2607 static const struct got_error *
2608 block_signals_used_by_main_thread(void)
2613 if (sigemptyset(&sigset) == -1)
2614 return got_error_from_errno("sigemptyset");
2616 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2617 if (sigaddset(&sigset, SIGWINCH) == -1)
2618 return got_error_from_errno("sigaddset");
2619 if (sigaddset(&sigset, SIGCONT) == -1)
2620 return got_error_from_errno("sigaddset");
2621 if (sigaddset(&sigset, SIGINT) == -1)
2622 return got_error_from_errno("sigaddset");
2623 if (sigaddset(&sigset, SIGTERM) == -1)
2624 return got_error_from_errno("sigaddset");
2626 /* ncurses handles SIGTSTP */
2627 if (sigaddset(&sigset, SIGTSTP) == -1)
2628 return got_error_from_errno("sigaddset");
2630 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2632 return got_error_set_errno(errcode, "pthread_sigmask");
2638 log_thread(void *arg)
2640 const struct got_error *err = NULL;
2642 struct tog_log_thread_args *a = arg;
2646 * Sync startup with main thread such that we begin our
2647 * work once view_input() has released the mutex.
2649 errcode = pthread_mutex_lock(&tog_mutex);
2651 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2655 err = block_signals_used_by_main_thread();
2657 pthread_mutex_unlock(&tog_mutex);
2661 while (!done && !err && !tog_fatal_signal_received()) {
2662 errcode = pthread_mutex_unlock(&tog_mutex);
2664 err = got_error_set_errno(errcode,
2665 "pthread_mutex_unlock");
2668 err = queue_commits(a);
2670 if (err->code != GOT_ERR_ITER_COMPLETED)
2674 } else if (a->commits_needed > 0 && !a->load_all)
2675 a->commits_needed--;
2677 errcode = pthread_mutex_lock(&tog_mutex);
2679 err = got_error_set_errno(errcode,
2680 "pthread_mutex_lock");
2682 } else if (*a->quit)
2684 else if (*a->first_displayed_entry == NULL) {
2685 *a->first_displayed_entry =
2686 TAILQ_FIRST(&a->commits->head);
2687 *a->selected_entry = *a->first_displayed_entry;
2690 errcode = pthread_cond_signal(&a->commit_loaded);
2692 err = got_error_set_errno(errcode,
2693 "pthread_cond_signal");
2694 pthread_mutex_unlock(&tog_mutex);
2699 a->commits_needed = 0;
2701 if (a->commits_needed == 0 && !a->load_all) {
2702 errcode = pthread_cond_wait(&a->need_commits,
2705 err = got_error_set_errno(errcode,
2706 "pthread_cond_wait");
2707 pthread_mutex_unlock(&tog_mutex);
2715 a->log_complete = 1;
2716 errcode = pthread_mutex_unlock(&tog_mutex);
2718 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2721 tog_thread_error = 1;
2722 pthread_cond_signal(&a->commit_loaded);
2727 static const struct got_error *
2728 stop_log_thread(struct tog_log_view_state *s)
2730 const struct got_error *err = NULL, *thread_err = NULL;
2735 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2737 return got_error_set_errno(errcode,
2738 "pthread_cond_signal");
2739 errcode = pthread_mutex_unlock(&tog_mutex);
2741 return got_error_set_errno(errcode,
2742 "pthread_mutex_unlock");
2743 errcode = pthread_join(s->thread, (void **)&thread_err);
2745 return got_error_set_errno(errcode, "pthread_join");
2746 errcode = pthread_mutex_lock(&tog_mutex);
2748 return got_error_set_errno(errcode,
2749 "pthread_mutex_lock");
2753 if (s->thread_args.repo) {
2754 err = got_repo_close(s->thread_args.repo);
2755 s->thread_args.repo = NULL;
2758 if (s->thread_args.pack_fds) {
2759 const struct got_error *pack_err =
2760 got_repo_pack_fds_close(s->thread_args.pack_fds);
2763 s->thread_args.pack_fds = NULL;
2766 if (s->thread_args.graph) {
2767 got_commit_graph_close(s->thread_args.graph);
2768 s->thread_args.graph = NULL;
2771 return err ? err : thread_err;
2774 static const struct got_error *
2775 close_log_view(struct tog_view *view)
2777 const struct got_error *err = NULL;
2778 struct tog_log_view_state *s = &view->state.log;
2781 err = stop_log_thread(s);
2783 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2784 if (errcode && err == NULL)
2785 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2787 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2788 if (errcode && err == NULL)
2789 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2791 free_commits(&s->commits);
2792 free(s->in_repo_path);
2793 s->in_repo_path = NULL;
2796 free(s->head_ref_name);
2797 s->head_ref_name = NULL;
2801 static const struct got_error *
2802 search_start_log_view(struct tog_view *view)
2804 struct tog_log_view_state *s = &view->state.log;
2806 s->matched_entry = NULL;
2807 s->search_entry = NULL;
2811 static const struct got_error *
2812 search_next_log_view(struct tog_view *view)
2814 const struct got_error *err = NULL;
2815 struct tog_log_view_state *s = &view->state.log;
2816 struct commit_queue_entry *entry;
2818 /* Display progress update in log view. */
2819 show_log_view(view);
2823 if (s->search_entry) {
2825 errcode = pthread_mutex_unlock(&tog_mutex);
2827 return got_error_set_errno(errcode,
2828 "pthread_mutex_unlock");
2829 ch = wgetch(view->window);
2830 errcode = pthread_mutex_lock(&tog_mutex);
2832 return got_error_set_errno(errcode,
2833 "pthread_mutex_lock");
2834 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2835 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2838 if (view->searching == TOG_SEARCH_FORWARD)
2839 entry = TAILQ_NEXT(s->search_entry, entry);
2841 entry = TAILQ_PREV(s->search_entry,
2842 commit_queue_head, entry);
2843 } else if (s->matched_entry) {
2844 int matched_idx = s->matched_entry->idx;
2845 int selected_idx = s->selected_entry->idx;
2848 * If the user has moved the cursor after we hit a match,
2849 * the position from where we should continue searching
2850 * might have changed.
2852 if (view->searching == TOG_SEARCH_FORWARD) {
2853 if (matched_idx > selected_idx)
2854 entry = TAILQ_NEXT(s->selected_entry, entry);
2856 entry = TAILQ_NEXT(s->matched_entry, entry);
2858 if (matched_idx < selected_idx)
2859 entry = TAILQ_PREV(s->selected_entry,
2860 commit_queue_head, entry);
2862 entry = TAILQ_PREV(s->matched_entry,
2863 commit_queue_head, entry);
2866 entry = s->selected_entry;
2872 if (entry == NULL) {
2873 if (s->thread_args.log_complete ||
2874 view->searching == TOG_SEARCH_BACKWARD) {
2875 view->search_next_done =
2876 (s->matched_entry == NULL ?
2877 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2878 s->search_entry = NULL;
2882 * Poke the log thread for more commits and return,
2883 * allowing the main loop to make progress. Search
2884 * will resume at s->search_entry once we come back.
2886 s->thread_args.commits_needed++;
2887 return trigger_log_thread(view, 0);
2890 err = match_commit(&have_match, entry->id, entry->commit,
2895 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2896 s->matched_entry = entry;
2900 s->search_entry = entry;
2901 if (view->searching == TOG_SEARCH_FORWARD)
2902 entry = TAILQ_NEXT(entry, entry);
2904 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2907 if (s->matched_entry) {
2908 int cur = s->selected_entry->idx;
2909 while (cur < s->matched_entry->idx) {
2910 err = input_log_view(NULL, view, KEY_DOWN);
2915 while (cur > s->matched_entry->idx) {
2916 err = input_log_view(NULL, view, KEY_UP);
2923 s->search_entry = NULL;
2928 static const struct got_error *
2929 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2930 struct got_repository *repo, const char *head_ref_name,
2931 const char *in_repo_path, int log_branches)
2933 const struct got_error *err = NULL;
2934 struct tog_log_view_state *s = &view->state.log;
2935 struct got_repository *thread_repo = NULL;
2936 struct got_commit_graph *thread_graph = NULL;
2939 if (in_repo_path != s->in_repo_path) {
2940 free(s->in_repo_path);
2941 s->in_repo_path = strdup(in_repo_path);
2942 if (s->in_repo_path == NULL)
2943 return got_error_from_errno("strdup");
2946 /* The commit queue only contains commits being displayed. */
2947 TAILQ_INIT(&s->commits.head);
2948 s->commits.ncommits = 0;
2951 if (head_ref_name) {
2952 s->head_ref_name = strdup(head_ref_name);
2953 if (s->head_ref_name == NULL) {
2954 err = got_error_from_errno("strdup");
2958 s->start_id = got_object_id_dup(start_id);
2959 if (s->start_id == NULL) {
2960 err = got_error_from_errno("got_object_id_dup");
2963 s->log_branches = log_branches;
2965 STAILQ_INIT(&s->colors);
2966 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2967 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2968 get_color_value("TOG_COLOR_COMMIT"));
2971 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2972 get_color_value("TOG_COLOR_AUTHOR"));
2974 free_colors(&s->colors);
2977 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2978 get_color_value("TOG_COLOR_DATE"));
2980 free_colors(&s->colors);
2985 view->show = show_log_view;
2986 view->input = input_log_view;
2987 view->close = close_log_view;
2988 view->search_start = search_start_log_view;
2989 view->search_next = search_next_log_view;
2991 if (s->thread_args.pack_fds == NULL) {
2992 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2996 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2997 s->thread_args.pack_fds);
3000 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3004 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3005 s->repo, NULL, NULL);
3009 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3011 err = got_error_set_errno(errcode, "pthread_cond_init");
3014 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3016 err = got_error_set_errno(errcode, "pthread_cond_init");
3020 s->thread_args.commits_needed = view->nlines;
3021 s->thread_args.graph = thread_graph;
3022 s->thread_args.commits = &s->commits;
3023 s->thread_args.in_repo_path = s->in_repo_path;
3024 s->thread_args.start_id = s->start_id;
3025 s->thread_args.repo = thread_repo;
3026 s->thread_args.log_complete = 0;
3027 s->thread_args.quit = &s->quit;
3028 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3029 s->thread_args.selected_entry = &s->selected_entry;
3030 s->thread_args.searching = &view->searching;
3031 s->thread_args.search_next_done = &view->search_next_done;
3032 s->thread_args.regex = &view->regex;
3035 close_log_view(view);
3039 static const struct got_error *
3040 show_log_view(struct tog_view *view)
3042 const struct got_error *err;
3043 struct tog_log_view_state *s = &view->state.log;
3045 if (s->thread == NULL) {
3046 int errcode = pthread_create(&s->thread, NULL, log_thread,
3049 return got_error_set_errno(errcode, "pthread_create");
3050 if (s->thread_args.commits_needed > 0) {
3051 err = trigger_log_thread(view, 1);
3057 return draw_commits(view);
3061 log_move_cursor_up(struct tog_view *view, int page, int home)
3063 struct tog_log_view_state *s = &view->state.log;
3065 if (s->selected_entry->idx == 0)
3067 if (s->first_displayed_entry == NULL)
3070 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3072 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3074 if (!page && !home && s->selected > 0)
3077 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3083 static const struct got_error *
3084 log_move_cursor_down(struct tog_view *view, int page)
3086 struct tog_log_view_state *s = &view->state.log;
3087 struct commit_queue_entry *first;
3088 const struct got_error *err = NULL;
3090 first = s->first_displayed_entry;
3091 if (first == NULL) {
3096 if (s->thread_args.log_complete &&
3097 s->selected_entry->idx >= s->commits.ncommits - 1)
3101 int eos = view->nlines - 2;
3103 if (view_is_hsplit_top(view))
3104 --eos; /* border consumes the last line */
3105 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3108 err = log_scroll_down(view, 1);
3109 } else if (s->thread_args.load_all) {
3110 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3111 s->selected += MIN(s->last_displayed_entry->idx -
3112 s->selected_entry->idx, page + 1);
3114 err = log_scroll_down(view, MIN(page,
3115 s->commits.ncommits - s->selected_entry->idx - 1));
3116 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3118 err = log_scroll_down(view, page);
3121 if (first == s->first_displayed_entry && s->selected <
3122 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3123 s->selected = MIN(s->commits.ncommits - 1, page);
3130 * We might necessarily overshoot in horizontal
3131 * splits; if so, select the last displayed commit.
3133 s->selected = MIN(s->selected,
3134 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3138 if (s->thread_args.log_complete &&
3139 s->selected_entry->idx == s->commits.ncommits - 1)
3146 view_get_split(struct tog_view *view, int *y, int *x)
3151 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3152 if (view->child && view->child->resized_y)
3153 *y = view->child->resized_y;
3154 else if (view->resized_y)
3155 *y = view->resized_y;
3157 *y = view_split_begin_y(view->lines);
3158 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3159 if (view->child && view->child->resized_x)
3160 *x = view->child->resized_x;
3161 else if (view->resized_x)
3162 *x = view->resized_x;
3164 *x = view_split_begin_x(view->begin_x);
3168 /* Split view horizontally at y and offset view->state->selected line. */
3169 static const struct got_error *
3170 view_init_hsplit(struct tog_view *view, int y)
3172 const struct got_error *err = NULL;
3176 err = view_resize(view);
3180 err = offset_selection_down(view);
3185 static const struct got_error *
3186 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3188 const struct got_error *err = NULL;
3189 struct tog_log_view_state *s = &view->state.log;
3190 struct tog_view *diff_view = NULL, *tree_view = NULL;
3191 struct tog_view *ref_view = NULL;
3192 struct commit_queue_entry *entry;
3193 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3195 if (s->thread_args.load_all) {
3196 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3197 s->thread_args.load_all = 0;
3198 else if (s->thread_args.log_complete) {
3199 err = log_move_cursor_down(view, s->commits.ncommits);
3200 s->thread_args.load_all = 0;
3205 eos = nscroll = view->nlines - 1;
3206 if (view_is_hsplit_top(view))
3217 view->x = MAX(view->maxx - view->ncols / 2, 0);
3222 if (view->x + view->ncols / 2 < view->maxx)
3223 view->x += 2; /* move two columns right */
3229 view->x -= MIN(view->x, 2); /* move two columns back */
3238 log_move_cursor_up(view, 0, 0);
3242 log_move_cursor_up(view, 0, 1);
3252 log_move_cursor_up(view, nscroll, 0);
3259 err = log_move_cursor_down(view, 0);
3263 /* We don't know yet how many commits, so we're forced to
3264 * traverse them all. */
3266 if (!s->thread_args.log_complete) {
3267 s->thread_args.load_all = 1;
3268 return trigger_log_thread(view, 0);
3272 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3273 for (n = 0; n < eos; n++) {
3276 s->first_displayed_entry = entry;
3277 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3280 s->selected = n - 1;
3292 err = log_move_cursor_down(view, nscroll);
3295 if (s->selected > view->nlines - 2)
3296 s->selected = view->nlines - 2;
3297 if (s->selected > s->commits.ncommits - 1)
3298 s->selected = s->commits.ncommits - 1;
3300 if (s->commits.ncommits < view->nlines - 1 &&
3301 !s->thread_args.log_complete) {
3302 s->thread_args.commits_needed += (view->nlines - 1) -
3303 s->commits.ncommits;
3304 err = trigger_log_thread(view, 1);
3310 if (s->selected_entry == NULL)
3313 /* get dimensions--don't split till initialisation succeeds */
3314 if (view_is_parent_view(view))
3315 view_get_split(view, &begin_y, &begin_x);
3317 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3318 s->selected_entry->commit, s->selected_entry->id,
3323 if (view_is_parent_view(view) &&
3324 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3325 err = view_init_hsplit(view, begin_y);
3331 diff_view->focussed = 1;
3332 diff_view->mode = view->mode;
3333 diff_view->nlines = view->lines - begin_y;
3335 if (view_is_parent_view(view)) {
3336 view_transfer_size(diff_view, view);
3337 err = view_close_child(view);
3340 err = view_set_child(view, diff_view);
3343 view->focus_child = 1;
3345 *new_view = diff_view;
3349 if (s->selected_entry == NULL)
3351 if (view_is_parent_view(view))
3352 view_get_split(view, &begin_y, &begin_x);
3353 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3354 s->selected_entry, s->in_repo_path, s->head_ref_name,
3358 if (view_is_parent_view(view) &&
3359 view->mode == TOG_VIEW_SPLIT_HRZN) {
3360 err = view_init_hsplit(view, begin_y);
3365 tree_view->focussed = 1;
3366 tree_view->mode = view->mode;
3367 tree_view->nlines = view->lines - begin_y;
3368 if (view_is_parent_view(view)) {
3369 view_transfer_size(tree_view, view);
3370 err = view_close_child(view);
3373 err = view_set_child(view, tree_view);
3376 view->focus_child = 1;
3378 *new_view = tree_view;
3384 if (ch == KEY_BACKSPACE &&
3385 got_path_is_root_dir(s->in_repo_path))
3387 err = stop_log_thread(s);
3390 if (ch == KEY_BACKSPACE) {
3392 err = got_path_dirname(&parent_path, s->in_repo_path);
3395 free(s->in_repo_path);
3396 s->in_repo_path = parent_path;
3397 s->thread_args.in_repo_path = s->in_repo_path;
3398 } else if (ch == CTRL('l')) {
3399 struct got_object_id *start_id;
3400 err = got_repo_match_object_id(&start_id, NULL,
3401 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3402 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3406 s->start_id = start_id;
3407 s->thread_args.start_id = s->start_id;
3409 s->log_branches = !s->log_branches;
3411 if (s->thread_args.pack_fds == NULL) {
3412 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3416 err = got_repo_open(&s->thread_args.repo,
3417 got_repo_get_path(s->repo), NULL,
3418 s->thread_args.pack_fds);
3422 err = tog_load_refs(s->repo, 0);
3425 err = got_commit_graph_open(&s->thread_args.graph,
3426 s->in_repo_path, !s->log_branches);
3429 err = got_commit_graph_iter_start(s->thread_args.graph,
3430 s->start_id, s->repo, NULL, NULL);
3433 free_commits(&s->commits);
3434 s->first_displayed_entry = NULL;
3435 s->last_displayed_entry = NULL;
3436 s->selected_entry = NULL;
3438 s->thread_args.log_complete = 0;
3440 s->thread_args.commits_needed = view->lines;
3441 s->matched_entry = NULL;
3442 s->search_entry = NULL;
3446 if (view_is_parent_view(view))
3447 view_get_split(view, &begin_y, &begin_x);
3448 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3449 if (ref_view == NULL)
3450 return got_error_from_errno("view_open");
3451 err = open_ref_view(ref_view, s->repo);
3453 view_close(ref_view);
3456 if (view_is_parent_view(view) &&
3457 view->mode == TOG_VIEW_SPLIT_HRZN) {
3458 err = view_init_hsplit(view, begin_y);
3463 ref_view->focussed = 1;
3464 ref_view->mode = view->mode;
3465 ref_view->nlines = view->lines - begin_y;
3466 if (view_is_parent_view(view)) {
3467 view_transfer_size(ref_view, view);
3468 err = view_close_child(view);
3471 err = view_set_child(view, ref_view);
3474 view->focus_child = 1;
3476 *new_view = ref_view;
3486 static const struct got_error *
3487 apply_unveil(const char *repo_path, const char *worktree_path)
3489 const struct got_error *error;
3492 if (unveil("gmon.out", "rwc") != 0)
3493 return got_error_from_errno2("unveil", "gmon.out");
3495 if (repo_path && unveil(repo_path, "r") != 0)
3496 return got_error_from_errno2("unveil", repo_path);
3498 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3499 return got_error_from_errno2("unveil", worktree_path);
3501 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3502 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3504 error = got_privsep_unveil_exec_helpers();
3508 if (unveil(NULL, NULL) != 0)
3509 return got_error_from_errno("unveil");
3518 * Override default signal handlers before starting ncurses.
3519 * This should prevent ncurses from installing its own
3520 * broken cleanup() signal handler.
3522 signal(SIGWINCH, tog_sigwinch);
3523 signal(SIGPIPE, tog_sigpipe);
3524 signal(SIGCONT, tog_sigcont);
3525 signal(SIGINT, tog_sigint);
3526 signal(SIGTERM, tog_sigterm);
3530 halfdelay(1); /* Do fast refresh while initial view is loading. */
3533 intrflush(stdscr, FALSE);
3534 keypad(stdscr, TRUE);
3536 if (getenv("TOG_COLORS") != NULL) {
3538 use_default_colors();
3542 static const struct got_error *
3543 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3544 struct got_repository *repo, struct got_worktree *worktree)
3546 const struct got_error *err = NULL;
3549 *in_repo_path = strdup("/");
3550 if (*in_repo_path == NULL)
3551 return got_error_from_errno("strdup");
3556 const char *prefix = got_worktree_get_path_prefix(worktree);
3559 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3562 if (asprintf(in_repo_path, "%s%s%s", prefix,
3563 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3565 err = got_error_from_errno("asprintf");
3566 *in_repo_path = NULL;
3570 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3575 static const struct got_error *
3576 cmd_log(int argc, char *argv[])
3578 const struct got_error *error;
3579 struct got_repository *repo = NULL;
3580 struct got_worktree *worktree = NULL;
3581 struct got_object_id *start_id = NULL;
3582 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3583 char *start_commit = NULL, *label = NULL;
3584 struct got_reference *ref = NULL;
3585 const char *head_ref_name = NULL;
3586 int ch, log_branches = 0;
3587 struct tog_view *view;
3588 int *pack_fds = NULL;
3590 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3596 start_commit = optarg;
3599 repo_path = realpath(optarg, NULL);
3600 if (repo_path == NULL)
3601 return got_error_from_errno2("realpath",
3616 error = got_repo_pack_fds_open(&pack_fds);
3620 if (repo_path == NULL) {
3621 cwd = getcwd(NULL, 0);
3623 return got_error_from_errno("getcwd");
3624 error = got_worktree_open(&worktree, cwd);
3625 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3629 strdup(got_worktree_get_repo_path(worktree));
3631 repo_path = strdup(cwd);
3632 if (repo_path == NULL) {
3633 error = got_error_from_errno("strdup");
3638 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3642 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3649 error = apply_unveil(got_repo_get_path(repo),
3650 worktree ? got_worktree_get_root_path(worktree) : NULL);
3654 /* already loaded by tog_log_with_path()? */
3655 if (TAILQ_EMPTY(&tog_refs)) {
3656 error = tog_load_refs(repo, 0);
3661 if (start_commit == NULL) {
3662 error = got_repo_match_object_id(&start_id, &label,
3663 worktree ? got_worktree_get_head_ref_name(worktree) :
3664 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3667 head_ref_name = label;
3669 error = got_ref_open(&ref, repo, start_commit, 0);
3671 head_ref_name = got_ref_get_name(ref);
3672 else if (error->code != GOT_ERR_NOT_REF)
3674 error = got_repo_match_object_id(&start_id, NULL,
3675 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3680 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3682 error = got_error_from_errno("view_open");
3685 error = open_log_view(view, start_id, repo, head_ref_name,
3686 in_repo_path, log_branches);
3690 /* Release work tree lock. */
3691 got_worktree_close(worktree);
3694 error = view_loop(view);
3704 const struct got_error *close_err = got_repo_close(repo);
3709 got_worktree_close(worktree);
3711 const struct got_error *pack_err =
3712 got_repo_pack_fds_close(pack_fds);
3724 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3725 "[-w] object1 object2\n", getprogname());
3730 match_line(const char *line, regex_t *regex, size_t nmatch,
3731 regmatch_t *regmatch)
3733 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3736 static struct tog_color *
3737 match_color(struct tog_colors *colors, const char *line)
3739 struct tog_color *tc = NULL;
3741 STAILQ_FOREACH(tc, colors, entry) {
3742 if (match_line(line, &tc->regex, 0, NULL))
3749 static const struct got_error *
3750 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3751 WINDOW *window, int skipcol, regmatch_t *regmatch)
3753 const struct got_error *err = NULL;
3755 wchar_t *wline = NULL;
3756 int rme, rms, n, width, scrollx;
3757 int width0 = 0, width1 = 0, width2 = 0;
3758 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3762 rms = regmatch->rm_so;
3763 rme = regmatch->rm_eo;
3765 err = expand_tab(&exstr, line);
3769 /* Split the line into 3 segments, according to match offsets. */
3770 seg0 = strndup(exstr, rms);
3772 err = got_error_from_errno("strndup");
3775 seg1 = strndup(exstr + rms, rme - rms);
3777 err = got_error_from_errno("strndup");
3780 seg2 = strdup(exstr + rme);
3782 err = got_error_from_errno("strndup");
3786 /* draw up to matched token if we haven't scrolled past it */
3787 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3791 n = MAX(width0 - skipcol, 0);
3794 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3795 wlimit, col_tab_align, 1);
3798 waddwstr(window, &wline[scrollx]);
3808 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3812 wlen = wcslen(wline);
3814 width = wcwidth(wline[i]);
3816 /* should not happen, tabs are expanded */
3817 err = got_error(GOT_ERR_RANGE);
3820 if (width0 + w + width > skipcol)
3825 /* draw (visible part of) matched token (if scrolled into it) */
3826 if (width1 - w > 0) {
3827 wattron(window, A_STANDOUT);
3828 waddwstr(window, &wline[i]);
3829 wattroff(window, A_STANDOUT);
3830 wlimit -= (width1 - w);
3831 *wtotal += (width1 - w);
3835 if (wlimit > 0) { /* draw rest of line */
3837 if (skipcol > width0 + width1) {
3838 err = format_line(&wline, &width2, &scrollx, seg2,
3839 skipcol - (width0 + width1), wlimit,
3843 waddwstr(window, &wline[scrollx]);
3845 err = format_line(&wline, &width2, NULL, seg2, 0,
3846 wlimit, col_tab_align, 1);
3849 waddwstr(window, wline);
3862 static const struct got_error *
3863 draw_file(struct tog_view *view, const char *header)
3865 struct tog_diff_view_state *s = &view->state.diff;
3866 regmatch_t *regmatch = &view->regmatch;
3867 const struct got_error *err;
3870 size_t linesize = 0;
3872 struct tog_color *tc;
3875 int max_lines = view->nlines;
3876 int nlines = s->nlines;
3879 line_offset = s->line_offsets[s->first_displayed_line - 1];
3880 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3881 return got_error_from_errno("fseek");
3883 werase(view->window);
3886 if (asprintf(&line, "[%d/%d] %s",
3887 s->first_displayed_line - 1 + s->selected_line, nlines,
3889 return got_error_from_errno("asprintf");
3890 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3896 if (view_needs_focus_indication(view))
3897 wstandout(view->window);
3898 waddwstr(view->window, wline);
3901 if (view_needs_focus_indication(view))
3902 wstandend(view->window);
3903 if (width <= view->ncols - 1)
3904 waddch(view->window, '\n');
3914 while (max_lines > 0 && nprinted < max_lines) {
3915 linelen = getline(&line, &linesize, s->f);
3916 if (linelen == -1) {
3922 return got_ferror(s->f, GOT_ERR_IO);
3925 /* Set view->maxx based on full line length. */
3926 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3932 view->maxx = MAX(view->maxx, width);
3936 tc = match_color(&s->colors, line);
3938 wattr_on(view->window,
3939 COLOR_PAIR(tc->colorpair), NULL);
3940 if (s->first_displayed_line + nprinted == s->matched_line &&
3941 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3942 err = add_matched_line(&width, line, view->ncols, 0,
3943 view->window, view->x, regmatch);
3950 err = format_line(&wline, &width, &skip, line,
3951 view->x, view->ncols, 0, view->x ? 1 : 0);
3956 waddwstr(view->window, &wline[skip]);
3961 wattr_off(view->window,
3962 COLOR_PAIR(tc->colorpair), NULL);
3963 if (width <= view->ncols - 1)
3964 waddch(view->window, '\n');
3969 s->last_displayed_line = s->first_displayed_line +
3972 s->last_displayed_line = s->first_displayed_line;
3977 while (nprinted < view->nlines) {
3978 waddch(view->window, '\n');
3982 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3988 wstandout(view->window);
3989 waddwstr(view->window, wline);
3992 wstandend(view->window);
3999 get_datestr(time_t *time, char *datebuf)
4001 struct tm mytm, *tm;
4004 tm = gmtime_r(time, &mytm);
4007 s = asctime_r(tm, datebuf);
4010 p = strchr(s, '\n');
4016 static const struct got_error *
4017 get_changed_paths(struct got_pathlist_head *paths,
4018 struct got_commit_object *commit, struct got_repository *repo)
4020 const struct got_error *err = NULL;
4021 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4022 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4023 struct got_object_qid *qid;
4025 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4027 struct got_commit_object *pcommit;
4028 err = got_object_open_as_commit(&pcommit, repo,
4033 tree_id1 = got_object_id_dup(
4034 got_object_commit_get_tree_id(pcommit));
4035 if (tree_id1 == NULL) {
4036 got_object_commit_close(pcommit);
4037 return got_error_from_errno("got_object_id_dup");
4039 got_object_commit_close(pcommit);
4044 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4049 tree_id2 = got_object_commit_get_tree_id(commit);
4050 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4054 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4055 got_diff_tree_collect_changed_paths, paths, 0);
4058 got_object_tree_close(tree1);
4060 got_object_tree_close(tree2);
4065 static const struct got_error *
4066 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4070 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4072 return got_error_from_errno("reallocarray");
4074 (*line_offsets)[*nlines] = off;
4079 static const struct got_error *
4080 write_commit_info(off_t **line_offsets, size_t *nlines,
4081 struct got_object_id *commit_id, struct got_reflist_head *refs,
4082 struct got_repository *repo, FILE *outfile)
4084 const struct got_error *err = NULL;
4085 char datebuf[26], *datestr;
4086 struct got_commit_object *commit;
4087 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4088 time_t committer_time;
4089 const char *author, *committer;
4090 char *refs_str = NULL;
4091 struct got_pathlist_head changed_paths;
4092 struct got_pathlist_entry *pe;
4096 TAILQ_INIT(&changed_paths);
4099 err = build_refs_str(&refs_str, refs, commit_id, repo);
4104 err = got_object_open_as_commit(&commit, repo, commit_id);
4108 err = got_object_id_str(&id_str, commit_id);
4110 err = got_error_from_errno("got_object_id_str");
4114 err = add_line_offset(line_offsets, nlines, 0);
4118 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4119 refs_str ? refs_str : "", refs_str ? ")" : "");
4121 err = got_error_from_errno("fprintf");
4125 err = add_line_offset(line_offsets, nlines, outoff);
4129 n = fprintf(outfile, "from: %s\n",
4130 got_object_commit_get_author(commit));
4132 err = got_error_from_errno("fprintf");
4136 err = add_line_offset(line_offsets, nlines, outoff);
4140 committer_time = got_object_commit_get_committer_time(commit);
4141 datestr = get_datestr(&committer_time, datebuf);
4143 n = fprintf(outfile, "date: %s UTC\n", datestr);
4145 err = got_error_from_errno("fprintf");
4149 err = add_line_offset(line_offsets, nlines, outoff);
4153 author = got_object_commit_get_author(commit);
4154 committer = got_object_commit_get_committer(commit);
4155 if (strcmp(author, committer) != 0) {
4156 n = fprintf(outfile, "via: %s\n", committer);
4158 err = got_error_from_errno("fprintf");
4162 err = add_line_offset(line_offsets, nlines, outoff);
4166 if (got_object_commit_get_nparents(commit) > 1) {
4167 const struct got_object_id_queue *parent_ids;
4168 struct got_object_qid *qid;
4170 parent_ids = got_object_commit_get_parent_ids(commit);
4171 STAILQ_FOREACH(qid, parent_ids, entry) {
4172 err = got_object_id_str(&id_str, &qid->id);
4175 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4177 err = got_error_from_errno("fprintf");
4181 err = add_line_offset(line_offsets, nlines, outoff);
4189 err = got_object_commit_get_logmsg(&logmsg, commit);
4193 while ((line = strsep(&s, "\n")) != NULL) {
4194 n = fprintf(outfile, "%s\n", line);
4196 err = got_error_from_errno("fprintf");
4200 err = add_line_offset(line_offsets, nlines, outoff);
4205 err = get_changed_paths(&changed_paths, commit, repo);
4208 TAILQ_FOREACH(pe, &changed_paths, entry) {
4209 struct got_diff_changed_path *cp = pe->data;
4210 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4212 err = got_error_from_errno("fprintf");
4216 err = add_line_offset(line_offsets, nlines, outoff);
4219 free((char *)pe->path);
4223 fputc('\n', outfile);
4225 err = add_line_offset(line_offsets, nlines, outoff);
4227 got_pathlist_free(&changed_paths);
4231 got_object_commit_close(commit);
4233 free(*line_offsets);
4234 *line_offsets = NULL;
4240 static const struct got_error *
4241 create_diff(struct tog_diff_view_state *s)
4243 const struct got_error *err = NULL;
4247 free(s->line_offsets);
4248 s->line_offsets = malloc(sizeof(off_t));
4249 if (s->line_offsets == NULL)
4250 return got_error_from_errno("malloc");
4255 err = got_error_from_errno("got_opentemp");
4258 if (s->f && fclose(s->f) == EOF) {
4259 err = got_error_from_errno("fclose");
4265 err = got_object_get_type(&obj_type, s->repo, s->id1);
4267 err = got_object_get_type(&obj_type, s->repo, s->id2);
4272 case GOT_OBJ_TYPE_BLOB:
4273 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4274 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4275 s->label1, s->label2, tog_diff_algo, s->diff_context,
4276 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4278 case GOT_OBJ_TYPE_TREE:
4279 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4280 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4281 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4282 s->force_text_diff, s->repo, s->f);
4284 case GOT_OBJ_TYPE_COMMIT: {
4285 const struct got_object_id_queue *parent_ids;
4286 struct got_object_qid *pid;
4287 struct got_commit_object *commit2;
4288 struct got_reflist_head *refs;
4290 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4293 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4294 /* Show commit info if we're diffing to a parent/root commit. */
4295 if (s->id1 == NULL) {
4296 err = write_commit_info(&s->line_offsets, &s->nlines,
4297 s->id2, refs, s->repo, s->f);
4301 parent_ids = got_object_commit_get_parent_ids(commit2);
4302 STAILQ_FOREACH(pid, parent_ids, entry) {
4303 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4304 err = write_commit_info(
4305 &s->line_offsets, &s->nlines,
4306 s->id2, refs, s->repo, s->f);
4313 got_object_commit_close(commit2);
4315 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4316 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4317 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4318 s->force_text_diff, s->repo, s->f);
4322 err = got_error(GOT_ERR_OBJ_TYPE);
4328 if (s->f && fflush(s->f) != 0 && err == NULL)
4329 err = got_error_from_errno("fflush");
4334 diff_view_indicate_progress(struct tog_view *view)
4336 mvwaddstr(view->window, 0, 0, "diffing...");
4341 static const struct got_error *
4342 search_start_diff_view(struct tog_view *view)
4344 struct tog_diff_view_state *s = &view->state.diff;
4346 s->matched_line = 0;
4350 static const struct got_error *
4351 search_next_diff_view(struct tog_view *view)
4353 struct tog_diff_view_state *s = &view->state.diff;
4354 const struct got_error *err = NULL;
4357 size_t linesize = 0;
4360 if (!view->searching) {
4361 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4365 if (s->matched_line) {
4366 if (view->searching == TOG_SEARCH_FORWARD)
4367 lineno = s->matched_line + 1;
4369 lineno = s->matched_line - 1;
4371 lineno = s->first_displayed_line;
4376 if (lineno <= 0 || lineno > s->nlines) {
4377 if (s->matched_line == 0) {
4378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4382 if (view->searching == TOG_SEARCH_FORWARD)
4388 offset = s->line_offsets[lineno - 1];
4389 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4391 return got_error_from_errno("fseeko");
4393 linelen = getline(&line, &linesize, s->f);
4394 if (linelen != -1) {
4396 err = expand_tab(&exstr, line);
4399 if (match_line(exstr, &view->regex, 1,
4401 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4402 s->matched_line = lineno;
4408 if (view->searching == TOG_SEARCH_FORWARD)
4415 if (s->matched_line) {
4416 s->first_displayed_line = s->matched_line;
4417 s->selected_line = 1;
4423 static const struct got_error *
4424 close_diff_view(struct tog_view *view)
4426 const struct got_error *err = NULL;
4427 struct tog_diff_view_state *s = &view->state.diff;
4433 if (s->f && fclose(s->f) == EOF)
4434 err = got_error_from_errno("fclose");
4436 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4437 err = got_error_from_errno("fclose");
4439 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4440 err = got_error_from_errno("fclose");
4442 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4443 err = got_error_from_errno("close");
4445 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4446 err = got_error_from_errno("close");
4448 free_colors(&s->colors);
4449 free(s->line_offsets);
4450 s->line_offsets = NULL;
4455 static const struct got_error *
4456 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4457 struct got_object_id *id2, const char *label1, const char *label2,
4458 int diff_context, int ignore_whitespace, int force_text_diff,
4459 struct tog_view *parent_view, struct got_repository *repo)
4461 const struct got_error *err;
4462 struct tog_diff_view_state *s = &view->state.diff;
4464 memset(s, 0, sizeof(*s));
4468 if (id1 != NULL && id2 != NULL) {
4470 err = got_object_get_type(&type1, repo, id1);
4473 err = got_object_get_type(&type2, repo, id2);
4478 return got_error(GOT_ERR_OBJ_TYPE);
4480 s->first_displayed_line = 1;
4481 s->last_displayed_line = view->nlines;
4482 s->selected_line = 1;
4490 s->id1 = got_object_id_dup(id1);
4492 return got_error_from_errno("got_object_id_dup");
4496 s->id2 = got_object_id_dup(id2);
4497 if (s->id2 == NULL) {
4498 err = got_error_from_errno("got_object_id_dup");
4502 s->f1 = got_opentemp();
4503 if (s->f1 == NULL) {
4504 err = got_error_from_errno("got_opentemp");
4508 s->f2 = got_opentemp();
4509 if (s->f2 == NULL) {
4510 err = got_error_from_errno("got_opentemp");
4514 s->fd1 = got_opentempfd();
4516 err = got_error_from_errno("got_opentempfd");
4520 s->fd2 = got_opentempfd();
4522 err = got_error_from_errno("got_opentempfd");
4526 s->first_displayed_line = 1;
4527 s->last_displayed_line = view->nlines;
4528 s->diff_context = diff_context;
4529 s->ignore_whitespace = ignore_whitespace;
4530 s->force_text_diff = force_text_diff;
4531 s->parent_view = parent_view;
4534 STAILQ_INIT(&s->colors);
4535 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4536 err = add_color(&s->colors,
4537 "^-", TOG_COLOR_DIFF_MINUS,
4538 get_color_value("TOG_COLOR_DIFF_MINUS"));
4541 err = add_color(&s->colors, "^\\+",
4542 TOG_COLOR_DIFF_PLUS,
4543 get_color_value("TOG_COLOR_DIFF_PLUS"));
4546 err = add_color(&s->colors,
4547 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4548 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4552 err = add_color(&s->colors,
4553 "^(commit [0-9a-f]|parent [0-9]|"
4554 "(blob|file|tree|commit) [-+] |"
4555 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4556 get_color_value("TOG_COLOR_DIFF_META"));
4560 err = add_color(&s->colors,
4561 "^(from|via): ", TOG_COLOR_AUTHOR,
4562 get_color_value("TOG_COLOR_AUTHOR"));
4566 err = add_color(&s->colors,
4567 "^date: ", TOG_COLOR_DATE,
4568 get_color_value("TOG_COLOR_DATE"));
4573 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4574 view_is_splitscreen(view))
4575 show_log_view(parent_view); /* draw border */
4576 diff_view_indicate_progress(view);
4578 err = create_diff(s);
4580 view->show = show_diff_view;
4581 view->input = input_diff_view;
4582 view->reset = reset_diff_view;
4583 view->close = close_diff_view;
4584 view->search_start = search_start_diff_view;
4585 view->search_next = search_next_diff_view;
4588 close_diff_view(view);
4592 static const struct got_error *
4593 show_diff_view(struct tog_view *view)
4595 const struct got_error *err;
4596 struct tog_diff_view_state *s = &view->state.diff;
4597 char *id_str1 = NULL, *id_str2, *header;
4598 const char *label1, *label2;
4601 err = got_object_id_str(&id_str1, s->id1);
4604 label1 = s->label1 ? : id_str1;
4606 label1 = "/dev/null";
4608 err = got_object_id_str(&id_str2, s->id2);
4611 label2 = s->label2 ? : id_str2;
4613 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4614 err = got_error_from_errno("asprintf");
4622 err = draw_file(view, header);
4627 static const struct got_error *
4628 set_selected_commit(struct tog_diff_view_state *s,
4629 struct commit_queue_entry *entry)
4631 const struct got_error *err;
4632 const struct got_object_id_queue *parent_ids;
4633 struct got_commit_object *selected_commit;
4634 struct got_object_qid *pid;
4637 s->id2 = got_object_id_dup(entry->id);
4639 return got_error_from_errno("got_object_id_dup");
4641 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4644 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4646 pid = STAILQ_FIRST(parent_ids);
4647 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4648 got_object_commit_close(selected_commit);
4652 static const struct got_error *
4653 reset_diff_view(struct tog_view *view)
4655 struct tog_diff_view_state *s = &view->state.diff;
4658 wclear(view->window);
4659 s->first_displayed_line = 1;
4660 s->last_displayed_line = view->nlines;
4661 s->matched_line = 0;
4662 diff_view_indicate_progress(view);
4663 return create_diff(s);
4666 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4668 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4671 static const struct got_error *
4672 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4674 const struct got_error *err = NULL;
4675 struct tog_diff_view_state *s = &view->state.diff;
4676 struct tog_log_view_state *ls;
4677 struct commit_queue_entry *old_selected_entry;
4679 size_t linesize = 0;
4681 int i, nscroll = view->nlines - 1, up = 0;
4688 view->x = MAX(view->maxx - view->ncols / 3, 0);
4693 if (view->x + view->ncols / 3 < view->maxx)
4694 view->x += 2; /* move two columns right */
4700 view->x -= MIN(view->x, 2); /* move two columns back */
4707 s->force_text_diff = !s->force_text_diff;
4709 s->ignore_whitespace = !s->ignore_whitespace;
4710 err = reset_diff_view(view);
4714 s->first_displayed_line = 1;
4723 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4729 if (s->first_displayed_line > 1)
4730 s->first_displayed_line--;
4741 if (s->first_displayed_line == 1) {
4746 while (i++ < nscroll && s->first_displayed_line > 1)
4747 s->first_displayed_line--;
4753 s->first_displayed_line++;
4770 while (!s->eof && i++ < nscroll) {
4771 linelen = getline(&line, &linesize, s->f);
4772 s->first_displayed_line++;
4773 if (linelen == -1) {
4777 err = got_ferror(s->f, GOT_ERR_IO);
4784 if (s->diff_context > 0) {
4786 s->matched_line = 0;
4787 diff_view_indicate_progress(view);
4788 err = create_diff(s);
4789 if (s->first_displayed_line + view->nlines - 1 >
4791 s->first_displayed_line = 1;
4792 s->last_displayed_line = view->nlines;
4798 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4800 s->matched_line = 0;
4801 diff_view_indicate_progress(view);
4802 err = create_diff(s);
4812 if (s->parent_view == NULL) {
4816 s->parent_view->count = view->count;
4818 if (s->parent_view->type == TOG_VIEW_LOG) {
4819 ls = &s->parent_view->state.log;
4820 old_selected_entry = ls->selected_entry;
4822 err = input_log_view(NULL, s->parent_view,
4823 up ? KEY_UP : KEY_DOWN);
4826 view->count = s->parent_view->count;
4828 if (old_selected_entry == ls->selected_entry)
4831 err = set_selected_commit(s, ls->selected_entry);
4834 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4835 struct tog_blame_view_state *bs;
4836 struct got_object_id *id, *prev_id;
4838 bs = &s->parent_view->state.blame;
4839 prev_id = get_annotation_for_line(bs->blame.lines,
4840 bs->blame.nlines, bs->last_diffed_line);
4842 err = input_blame_view(&view, s->parent_view,
4843 up ? KEY_UP : KEY_DOWN);
4846 view->count = s->parent_view->count;
4848 if (prev_id == NULL)
4850 id = get_selected_commit_id(bs->blame.lines,
4851 bs->blame.nlines, bs->first_displayed_line,
4856 if (!got_object_id_cmp(prev_id, id))
4859 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4863 s->first_displayed_line = 1;
4864 s->last_displayed_line = view->nlines;
4865 s->matched_line = 0;
4868 diff_view_indicate_progress(view);
4869 err = create_diff(s);
4879 static const struct got_error *
4880 cmd_diff(int argc, char *argv[])
4882 const struct got_error *error = NULL;
4883 struct got_repository *repo = NULL;
4884 struct got_worktree *worktree = NULL;
4885 struct got_object_id *id1 = NULL, *id2 = NULL;
4886 char *repo_path = NULL, *cwd = NULL;
4887 char *id_str1 = NULL, *id_str2 = NULL;
4888 char *label1 = NULL, *label2 = NULL;
4889 int diff_context = 3, ignore_whitespace = 0;
4890 int ch, force_text_diff = 0;
4892 struct tog_view *view;
4893 int *pack_fds = NULL;
4895 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4898 force_text_diff = 1;
4901 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4904 errx(1, "number of context lines is %s: %s",
4908 repo_path = realpath(optarg, NULL);
4909 if (repo_path == NULL)
4910 return got_error_from_errno2("realpath",
4912 got_path_strip_trailing_slashes(repo_path);
4915 ignore_whitespace = 1;
4927 usage_diff(); /* TODO show local worktree changes */
4928 } else if (argc == 2) {
4934 error = got_repo_pack_fds_open(&pack_fds);
4938 if (repo_path == NULL) {
4939 cwd = getcwd(NULL, 0);
4941 return got_error_from_errno("getcwd");
4942 error = got_worktree_open(&worktree, cwd);
4943 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4947 strdup(got_worktree_get_repo_path(worktree));
4949 repo_path = strdup(cwd);
4950 if (repo_path == NULL) {
4951 error = got_error_from_errno("strdup");
4956 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4962 error = apply_unveil(got_repo_get_path(repo), NULL);
4966 error = tog_load_refs(repo, 0);
4970 error = got_repo_match_object_id(&id1, &label1, id_str1,
4971 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4975 error = got_repo_match_object_id(&id2, &label2, id_str2,
4976 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4980 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4982 error = got_error_from_errno("view_open");
4985 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4986 ignore_whitespace, force_text_diff, NULL, repo);
4989 error = view_loop(view);
4996 const struct got_error *close_err = got_repo_close(repo);
5001 got_worktree_close(worktree);
5003 const struct got_error *pack_err =
5004 got_repo_pack_fds_close(pack_fds);
5017 "usage: %s blame [-c commit] [-r repository-path] path\n",
5022 struct tog_blame_line {
5024 struct got_object_id *id;
5027 static const struct got_error *
5028 draw_blame(struct tog_view *view)
5030 struct tog_blame_view_state *s = &view->state.blame;
5031 struct tog_blame *blame = &s->blame;
5032 regmatch_t *regmatch = &view->regmatch;
5033 const struct got_error *err;
5034 int lineno = 0, nprinted = 0;
5036 size_t linesize = 0;
5040 struct tog_blame_line *blame_line;
5041 struct got_object_id *prev_id = NULL;
5043 struct tog_color *tc;
5045 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5050 werase(view->window);
5052 if (asprintf(&line, "commit %s", id_str) == -1) {
5053 err = got_error_from_errno("asprintf");
5058 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5063 if (view_needs_focus_indication(view))
5064 wstandout(view->window);
5065 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5067 wattr_on(view->window,
5068 COLOR_PAIR(tc->colorpair), NULL);
5069 waddwstr(view->window, wline);
5071 wattr_off(view->window,
5072 COLOR_PAIR(tc->colorpair), NULL);
5073 if (view_needs_focus_indication(view))
5074 wstandend(view->window);
5077 if (width < view->ncols - 1)
5078 waddch(view->window, '\n');
5080 if (asprintf(&line, "[%d/%d] %s%s",
5081 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5082 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5084 return got_error_from_errno("asprintf");
5087 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5092 waddwstr(view->window, wline);
5095 if (width < view->ncols - 1)
5096 waddch(view->window, '\n');
5100 while (nprinted < view->nlines - 2) {
5101 linelen = getline(&line, &linesize, blame->f);
5102 if (linelen == -1) {
5103 if (feof(blame->f)) {
5108 return got_ferror(blame->f, GOT_ERR_IO);
5110 if (++lineno < s->first_displayed_line)
5113 /* Set view->maxx based on full line length. */
5114 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5121 view->maxx = MAX(view->maxx, width);
5123 if (nprinted == s->selected_line - 1)
5124 wstandout(view->window);
5126 if (blame->nlines > 0) {
5127 blame_line = &blame->lines[lineno - 1];
5128 if (blame_line->annotated && prev_id &&
5129 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5130 !(nprinted == s->selected_line - 1)) {
5131 waddstr(view->window, " ");
5132 } else if (blame_line->annotated) {
5134 err = got_object_id_str(&id_str,
5140 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5142 wattr_on(view->window,
5143 COLOR_PAIR(tc->colorpair), NULL);
5144 wprintw(view->window, "%.8s", id_str);
5146 wattr_off(view->window,
5147 COLOR_PAIR(tc->colorpair), NULL);
5149 prev_id = blame_line->id;
5151 waddstr(view->window, "........");
5155 waddstr(view->window, "........");
5159 if (nprinted == s->selected_line - 1)
5160 wstandend(view->window);
5161 waddstr(view->window, " ");
5163 if (view->ncols <= 9) {
5165 } else if (s->first_displayed_line + nprinted ==
5167 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5168 err = add_matched_line(&width, line, view->ncols - 9, 9,
5169 view->window, view->x, regmatch);
5177 err = format_line(&wline, &width, &skip, line,
5178 view->x, view->ncols - 9, 9, 1);
5183 waddwstr(view->window, &wline[skip]);
5189 if (width <= view->ncols - 1)
5190 waddch(view->window, '\n');
5191 if (++nprinted == 1)
5192 s->first_displayed_line = lineno;
5195 s->last_displayed_line = lineno;
5202 static const struct got_error *
5203 blame_cb(void *arg, int nlines, int lineno,
5204 struct got_commit_object *commit, struct got_object_id *id)
5206 const struct got_error *err = NULL;
5207 struct tog_blame_cb_args *a = arg;
5208 struct tog_blame_line *line;
5211 if (nlines != a->nlines ||
5212 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5213 return got_error(GOT_ERR_RANGE);
5215 errcode = pthread_mutex_lock(&tog_mutex);
5217 return got_error_set_errno(errcode, "pthread_mutex_lock");
5219 if (*a->quit) { /* user has quit the blame view */
5220 err = got_error(GOT_ERR_ITER_COMPLETED);
5225 goto done; /* no change in this commit */
5227 line = &a->lines[lineno - 1];
5228 if (line->annotated)
5231 line->id = got_object_id_dup(id);
5232 if (line->id == NULL) {
5233 err = got_error_from_errno("got_object_id_dup");
5236 line->annotated = 1;
5238 errcode = pthread_mutex_unlock(&tog_mutex);
5240 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5245 blame_thread(void *arg)
5247 const struct got_error *err, *close_err;
5248 struct tog_blame_thread_args *ta = arg;
5249 struct tog_blame_cb_args *a = ta->cb_args;
5250 int errcode, fd1 = -1, fd2 = -1;
5251 FILE *f1 = NULL, *f2 = NULL;
5253 fd1 = got_opentempfd();
5255 return (void *)got_error_from_errno("got_opentempfd");
5257 fd2 = got_opentempfd();
5259 err = got_error_from_errno("got_opentempfd");
5263 f1 = got_opentemp();
5265 err = (void *)got_error_from_errno("got_opentemp");
5268 f2 = got_opentemp();
5270 err = (void *)got_error_from_errno("got_opentemp");
5274 err = block_signals_used_by_main_thread();
5278 err = got_blame(ta->path, a->commit_id, ta->repo,
5279 tog_diff_algo, blame_cb, ta->cb_args,
5280 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5281 if (err && err->code == GOT_ERR_CANCELLED)
5284 errcode = pthread_mutex_lock(&tog_mutex);
5286 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5290 close_err = got_repo_close(ta->repo);
5296 errcode = pthread_mutex_unlock(&tog_mutex);
5297 if (errcode && err == NULL)
5298 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5301 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5302 err = got_error_from_errno("close");
5303 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5304 err = got_error_from_errno("close");
5305 if (f1 && fclose(f1) == EOF && err == NULL)
5306 err = got_error_from_errno("fclose");
5307 if (f2 && fclose(f2) == EOF && err == NULL)
5308 err = got_error_from_errno("fclose");
5313 static struct got_object_id *
5314 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5315 int first_displayed_line, int selected_line)
5317 struct tog_blame_line *line;
5322 line = &lines[first_displayed_line - 1 + selected_line - 1];
5323 if (!line->annotated)
5329 static struct got_object_id *
5330 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5333 struct tog_blame_line *line;
5335 if (nlines <= 0 || lineno >= nlines)
5338 line = &lines[lineno - 1];
5339 if (!line->annotated)
5345 static const struct got_error *
5346 stop_blame(struct tog_blame *blame)
5348 const struct got_error *err = NULL;
5351 if (blame->thread) {
5353 errcode = pthread_mutex_unlock(&tog_mutex);
5355 return got_error_set_errno(errcode,
5356 "pthread_mutex_unlock");
5357 errcode = pthread_join(blame->thread, (void **)&err);
5359 return got_error_set_errno(errcode, "pthread_join");
5360 errcode = pthread_mutex_lock(&tog_mutex);
5362 return got_error_set_errno(errcode,
5363 "pthread_mutex_lock");
5364 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5366 blame->thread = NULL;
5368 if (blame->thread_args.repo) {
5369 const struct got_error *close_err;
5370 close_err = got_repo_close(blame->thread_args.repo);
5373 blame->thread_args.repo = NULL;
5376 if (fclose(blame->f) == EOF && err == NULL)
5377 err = got_error_from_errno("fclose");
5381 for (i = 0; i < blame->nlines; i++)
5382 free(blame->lines[i].id);
5384 blame->lines = NULL;
5386 free(blame->cb_args.commit_id);
5387 blame->cb_args.commit_id = NULL;
5388 if (blame->pack_fds) {
5389 const struct got_error *pack_err =
5390 got_repo_pack_fds_close(blame->pack_fds);
5393 blame->pack_fds = NULL;
5398 static const struct got_error *
5399 cancel_blame_view(void *arg)
5401 const struct got_error *err = NULL;
5405 errcode = pthread_mutex_lock(&tog_mutex);
5407 return got_error_set_errno(errcode,
5408 "pthread_mutex_unlock");
5411 err = got_error(GOT_ERR_CANCELLED);
5413 errcode = pthread_mutex_unlock(&tog_mutex);
5415 return got_error_set_errno(errcode,
5416 "pthread_mutex_lock");
5421 static const struct got_error *
5422 run_blame(struct tog_view *view)
5424 struct tog_blame_view_state *s = &view->state.blame;
5425 struct tog_blame *blame = &s->blame;
5426 const struct got_error *err = NULL;
5427 struct got_commit_object *commit = NULL;
5428 struct got_blob_object *blob = NULL;
5429 struct got_repository *thread_repo = NULL;
5430 struct got_object_id *obj_id = NULL;
5431 int obj_type, fd = -1;
5432 int *pack_fds = NULL;
5434 err = got_object_open_as_commit(&commit, s->repo,
5435 &s->blamed_commit->id);
5439 fd = got_opentempfd();
5441 err = got_error_from_errno("got_opentempfd");
5445 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5449 err = got_object_get_type(&obj_type, s->repo, obj_id);
5453 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5454 err = got_error(GOT_ERR_OBJ_TYPE);
5458 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5461 blame->f = got_opentemp();
5462 if (blame->f == NULL) {
5463 err = got_error_from_errno("got_opentemp");
5466 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5467 &blame->line_offsets, blame->f, blob);
5470 if (blame->nlines == 0) {
5471 s->blame_complete = 1;
5475 /* Don't include \n at EOF in the blame line count. */
5476 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5479 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5480 if (blame->lines == NULL) {
5481 err = got_error_from_errno("calloc");
5485 err = got_repo_pack_fds_open(&pack_fds);
5488 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5493 blame->pack_fds = pack_fds;
5494 blame->cb_args.view = view;
5495 blame->cb_args.lines = blame->lines;
5496 blame->cb_args.nlines = blame->nlines;
5497 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5498 if (blame->cb_args.commit_id == NULL) {
5499 err = got_error_from_errno("got_object_id_dup");
5502 blame->cb_args.quit = &s->done;
5504 blame->thread_args.path = s->path;
5505 blame->thread_args.repo = thread_repo;
5506 blame->thread_args.cb_args = &blame->cb_args;
5507 blame->thread_args.complete = &s->blame_complete;
5508 blame->thread_args.cancel_cb = cancel_blame_view;
5509 blame->thread_args.cancel_arg = &s->done;
5510 s->blame_complete = 0;
5512 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5513 s->first_displayed_line = 1;
5514 s->last_displayed_line = view->nlines;
5515 s->selected_line = 1;
5517 s->matched_line = 0;
5521 got_object_commit_close(commit);
5522 if (fd != -1 && close(fd) == -1 && err == NULL)
5523 err = got_error_from_errno("close");
5525 got_object_blob_close(blob);
5532 static const struct got_error *
5533 open_blame_view(struct tog_view *view, char *path,
5534 struct got_object_id *commit_id, struct got_repository *repo)
5536 const struct got_error *err = NULL;
5537 struct tog_blame_view_state *s = &view->state.blame;
5539 STAILQ_INIT(&s->blamed_commits);
5541 s->path = strdup(path);
5542 if (s->path == NULL)
5543 return got_error_from_errno("strdup");
5545 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5551 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5552 s->first_displayed_line = 1;
5553 s->last_displayed_line = view->nlines;
5554 s->selected_line = 1;
5555 s->blame_complete = 0;
5557 s->commit_id = commit_id;
5558 memset(&s->blame, 0, sizeof(s->blame));
5560 STAILQ_INIT(&s->colors);
5561 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5562 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5563 get_color_value("TOG_COLOR_COMMIT"));
5568 view->show = show_blame_view;
5569 view->input = input_blame_view;
5570 view->reset = reset_blame_view;
5571 view->close = close_blame_view;
5572 view->search_start = search_start_blame_view;
5573 view->search_next = search_next_blame_view;
5575 return run_blame(view);
5578 static const struct got_error *
5579 close_blame_view(struct tog_view *view)
5581 const struct got_error *err = NULL;
5582 struct tog_blame_view_state *s = &view->state.blame;
5584 if (s->blame.thread)
5585 err = stop_blame(&s->blame);
5587 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5588 struct got_object_qid *blamed_commit;
5589 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5590 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5591 got_object_qid_free(blamed_commit);
5595 free_colors(&s->colors);
5599 static const struct got_error *
5600 search_start_blame_view(struct tog_view *view)
5602 struct tog_blame_view_state *s = &view->state.blame;
5604 s->matched_line = 0;
5608 static const struct got_error *
5609 search_next_blame_view(struct tog_view *view)
5611 struct tog_blame_view_state *s = &view->state.blame;
5612 const struct got_error *err = NULL;
5615 size_t linesize = 0;
5618 if (!view->searching) {
5619 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5623 if (s->matched_line) {
5624 if (view->searching == TOG_SEARCH_FORWARD)
5625 lineno = s->matched_line + 1;
5627 lineno = s->matched_line - 1;
5629 lineno = s->first_displayed_line - 1 + s->selected_line;
5634 if (lineno <= 0 || lineno > s->blame.nlines) {
5635 if (s->matched_line == 0) {
5636 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5640 if (view->searching == TOG_SEARCH_FORWARD)
5643 lineno = s->blame.nlines;
5646 offset = s->blame.line_offsets[lineno - 1];
5647 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5649 return got_error_from_errno("fseeko");
5651 linelen = getline(&line, &linesize, s->blame.f);
5652 if (linelen != -1) {
5654 err = expand_tab(&exstr, line);
5657 if (match_line(exstr, &view->regex, 1,
5659 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5660 s->matched_line = lineno;
5666 if (view->searching == TOG_SEARCH_FORWARD)
5673 if (s->matched_line) {
5674 s->first_displayed_line = s->matched_line;
5675 s->selected_line = 1;
5681 static const struct got_error *
5682 show_blame_view(struct tog_view *view)
5684 const struct got_error *err = NULL;
5685 struct tog_blame_view_state *s = &view->state.blame;
5688 if (s->blame.thread == NULL && !s->blame_complete) {
5689 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5690 &s->blame.thread_args);
5692 return got_error_set_errno(errcode, "pthread_create");
5694 halfdelay(1); /* fast refresh while annotating */
5697 if (s->blame_complete)
5698 halfdelay(10); /* disable fast refresh */
5700 err = draw_blame(view);
5706 static const struct got_error *
5707 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5709 const struct got_error *err = NULL, *thread_err = NULL;
5710 struct tog_view *diff_view;
5711 struct tog_blame_view_state *s = &view->state.blame;
5712 int eos, nscroll, begin_y = 0, begin_x = 0;
5714 eos = nscroll = view->nlines - 2;
5715 if (view_is_hsplit_top(view))
5723 view->x = MAX(view->maxx - view->ncols / 3, 0);
5728 if (view->x + view->ncols / 3 < view->maxx)
5729 view->x += 2; /* move two columns right */
5735 view->x -= MIN(view->x, 2); /* move two columns back */
5744 s->selected_line = 1;
5745 s->first_displayed_line = 1;
5750 if (s->blame.nlines < eos) {
5751 s->selected_line = s->blame.nlines;
5752 s->first_displayed_line = 1;
5754 s->selected_line = eos;
5755 s->first_displayed_line = s->blame.nlines - (eos - 1);
5762 if (s->selected_line > 1)
5764 else if (s->selected_line == 1 &&
5765 s->first_displayed_line > 1)
5766 s->first_displayed_line--;
5777 if (s->first_displayed_line == 1) {
5778 if (view->count > 1)
5780 s->selected_line = MAX(1, s->selected_line - nscroll);
5784 if (s->first_displayed_line > nscroll)
5785 s->first_displayed_line -= nscroll;
5787 s->first_displayed_line = 1;
5792 if (s->selected_line < eos && s->first_displayed_line +
5793 s->selected_line <= s->blame.nlines)
5795 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5796 s->first_displayed_line++;
5802 struct got_object_id *id = NULL;
5805 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5806 s->first_displayed_line, s->selected_line);
5810 struct got_commit_object *commit, *pcommit;
5811 struct got_object_qid *pid;
5812 struct got_object_id *blob_id = NULL;
5814 err = got_object_open_as_commit(&commit,
5819 got_object_commit_get_parent_ids(commit));
5821 got_object_commit_close(commit);
5824 /* Check if path history ends here. */
5825 err = got_object_open_as_commit(&pcommit,
5829 err = got_object_id_by_path(&blob_id, s->repo,
5831 got_object_commit_close(pcommit);
5833 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5835 got_object_commit_close(commit);
5838 err = got_object_get_type(&obj_type, s->repo,
5841 /* Can't blame non-blob type objects. */
5842 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5843 got_object_commit_close(commit);
5846 err = got_object_qid_alloc(&s->blamed_commit,
5848 got_object_commit_close(commit);
5850 if (got_object_id_cmp(id,
5851 &s->blamed_commit->id) == 0)
5853 err = got_object_qid_alloc(&s->blamed_commit,
5859 thread_err = stop_blame(&s->blame);
5863 STAILQ_INSERT_HEAD(&s->blamed_commits,
5864 s->blamed_commit, entry);
5865 err = run_blame(view);
5871 struct got_object_qid *first;
5874 first = STAILQ_FIRST(&s->blamed_commits);
5875 if (!got_object_id_cmp(&first->id, s->commit_id))
5878 thread_err = stop_blame(&s->blame);
5882 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5883 got_object_qid_free(s->blamed_commit);
5885 STAILQ_FIRST(&s->blamed_commits);
5886 err = run_blame(view);
5893 struct got_object_id *id = NULL;
5894 struct got_object_qid *pid;
5895 struct got_commit_object *commit = NULL;
5898 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5899 s->first_displayed_line, s->selected_line);
5902 err = got_object_open_as_commit(&commit, s->repo, id);
5905 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5907 /* traversed from diff view, release diff resources */
5908 err = close_diff_view(*new_view);
5911 diff_view = *new_view;
5913 if (view_is_parent_view(view))
5914 view_get_split(view, &begin_y, &begin_x);
5916 diff_view = view_open(0, 0, begin_y, begin_x,
5918 if (diff_view == NULL) {
5919 got_object_commit_close(commit);
5920 err = got_error_from_errno("view_open");
5924 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5925 id, NULL, NULL, 3, 0, 0, view, s->repo);
5926 got_object_commit_close(commit);
5928 view_close(diff_view);
5931 s->last_diffed_line = s->first_displayed_line - 1 +
5934 break; /* still open from active diff view */
5935 if (view_is_parent_view(view) &&
5936 view->mode == TOG_VIEW_SPLIT_HRZN) {
5937 err = view_init_hsplit(view, begin_y);
5943 diff_view->focussed = 1;
5944 diff_view->mode = view->mode;
5945 diff_view->nlines = view->lines - begin_y;
5946 if (view_is_parent_view(view)) {
5947 view_transfer_size(diff_view, view);
5948 err = view_close_child(view);
5951 err = view_set_child(view, diff_view);
5954 view->focus_child = 1;
5956 *new_view = diff_view;
5969 if (s->last_displayed_line >= s->blame.nlines &&
5970 s->selected_line >= MIN(s->blame.nlines,
5971 view->nlines - 2)) {
5975 if (s->last_displayed_line >= s->blame.nlines &&
5976 s->selected_line < view->nlines - 2) {
5978 MIN(nscroll, s->last_displayed_line -
5979 s->first_displayed_line - s->selected_line + 1);
5981 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5982 s->first_displayed_line += nscroll;
5984 s->first_displayed_line =
5985 s->blame.nlines - (view->nlines - 3);
5988 if (s->selected_line > view->nlines - 2) {
5989 s->selected_line = MIN(s->blame.nlines,
5997 return thread_err ? thread_err : err;
6000 static const struct got_error *
6001 reset_blame_view(struct tog_view *view)
6003 const struct got_error *err;
6004 struct tog_blame_view_state *s = &view->state.blame;
6008 err = stop_blame(&s->blame);
6012 return run_blame(view);
6015 static const struct got_error *
6016 cmd_blame(int argc, char *argv[])
6018 const struct got_error *error;
6019 struct got_repository *repo = NULL;
6020 struct got_worktree *worktree = NULL;
6021 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6022 char *link_target = NULL;
6023 struct got_object_id *commit_id = NULL;
6024 struct got_commit_object *commit = NULL;
6025 char *commit_id_str = NULL;
6027 struct tog_view *view;
6028 int *pack_fds = NULL;
6030 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6033 commit_id_str = optarg;
6036 repo_path = realpath(optarg, NULL);
6037 if (repo_path == NULL)
6038 return got_error_from_errno2("realpath",
6053 error = got_repo_pack_fds_open(&pack_fds);
6057 if (repo_path == NULL) {
6058 cwd = getcwd(NULL, 0);
6060 return got_error_from_errno("getcwd");
6061 error = got_worktree_open(&worktree, cwd);
6062 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6066 strdup(got_worktree_get_repo_path(worktree));
6068 repo_path = strdup(cwd);
6069 if (repo_path == NULL) {
6070 error = got_error_from_errno("strdup");
6075 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6079 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6086 error = apply_unveil(got_repo_get_path(repo), NULL);
6090 error = tog_load_refs(repo, 0);
6094 if (commit_id_str == NULL) {
6095 struct got_reference *head_ref;
6096 error = got_ref_open(&head_ref, repo, worktree ?
6097 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6100 error = got_ref_resolve(&commit_id, repo, head_ref);
6101 got_ref_close(head_ref);
6103 error = got_repo_match_object_id(&commit_id, NULL,
6104 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6109 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6111 error = got_error_from_errno("view_open");
6115 error = got_object_open_as_commit(&commit, repo, commit_id);
6119 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6124 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6129 /* Release work tree lock. */
6130 got_worktree_close(worktree);
6133 error = view_loop(view);
6141 got_object_commit_close(commit);
6143 got_worktree_close(worktree);
6145 const struct got_error *close_err = got_repo_close(repo);
6150 const struct got_error *pack_err =
6151 got_repo_pack_fds_close(pack_fds);
6159 static const struct got_error *
6160 draw_tree_entries(struct tog_view *view, const char *parent_path)
6162 struct tog_tree_view_state *s = &view->state.tree;
6163 const struct got_error *err = NULL;
6164 struct got_tree_entry *te;
6166 struct tog_color *tc;
6167 int width, n, i, nentries;
6168 int limit = view->nlines;
6171 if (view_is_hsplit_top(view))
6172 --limit; /* border */
6174 werase(view->window);
6179 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6183 if (view_needs_focus_indication(view))
6184 wstandout(view->window);
6185 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6187 wattr_on(view->window,
6188 COLOR_PAIR(tc->colorpair), NULL);
6189 waddwstr(view->window, wline);
6191 wattr_off(view->window,
6192 COLOR_PAIR(tc->colorpair), NULL);
6193 if (view_needs_focus_indication(view))
6194 wstandend(view->window);
6197 if (width < view->ncols - 1)
6198 waddch(view->window, '\n');
6201 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6205 waddwstr(view->window, wline);
6208 if (width < view->ncols - 1)
6209 waddch(view->window, '\n');
6212 waddch(view->window, '\n');
6216 if (s->first_displayed_entry == NULL) {
6217 te = got_object_tree_get_first_entry(s->tree);
6218 if (s->selected == 0) {
6220 wstandout(view->window);
6221 s->selected_entry = NULL;
6223 waddstr(view->window, " ..\n"); /* parent directory */
6224 if (s->selected == 0 && view->focussed)
6225 wstandend(view->window);
6232 te = s->first_displayed_entry;
6235 nentries = got_object_tree_get_nentries(s->tree);
6236 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6237 char *line = NULL, *id_str = NULL, *link_target = NULL;
6238 const char *modestr = "";
6241 te = got_object_tree_get_entry(s->tree, i);
6242 mode = got_tree_entry_get_mode(te);
6245 err = got_object_id_str(&id_str,
6246 got_tree_entry_get_id(te));
6248 return got_error_from_errno(
6249 "got_object_id_str");
6251 if (got_object_tree_entry_is_submodule(te))
6253 else if (S_ISLNK(mode)) {
6256 err = got_tree_entry_get_symlink_target(&link_target,
6262 for (i = 0; i < strlen(link_target); i++) {
6263 if (!isprint((unsigned char)link_target[i]))
6264 link_target[i] = '?';
6268 else if (S_ISDIR(mode))
6270 else if (mode & S_IXUSR)
6272 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6273 got_tree_entry_get_name(te), modestr,
6274 link_target ? " -> ": "",
6275 link_target ? link_target : "") == -1) {
6278 return got_error_from_errno("asprintf");
6282 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6288 if (n == s->selected) {
6290 wstandout(view->window);
6291 s->selected_entry = te;
6293 tc = match_color(&s->colors, line);
6295 wattr_on(view->window,
6296 COLOR_PAIR(tc->colorpair), NULL);
6297 waddwstr(view->window, wline);
6299 wattr_off(view->window,
6300 COLOR_PAIR(tc->colorpair), NULL);
6301 if (width < view->ncols - 1)
6302 waddch(view->window, '\n');
6303 if (n == s->selected && view->focussed)
6304 wstandend(view->window);
6310 s->last_displayed_entry = te;
6319 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6321 struct got_tree_entry *te;
6322 int isroot = s->tree == s->root;
6325 if (s->first_displayed_entry == NULL)
6328 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6329 while (i++ < maxscroll) {
6332 s->first_displayed_entry = NULL;
6335 s->first_displayed_entry = te;
6336 te = got_tree_entry_get_prev(s->tree, te);
6340 static const struct got_error *
6341 tree_scroll_down(struct tog_view *view, int maxscroll)
6343 struct tog_tree_view_state *s = &view->state.tree;
6344 struct got_tree_entry *next, *last;
6347 if (s->first_displayed_entry)
6348 next = got_tree_entry_get_next(s->tree,
6349 s->first_displayed_entry);
6351 next = got_object_tree_get_first_entry(s->tree);
6353 last = s->last_displayed_entry;
6354 while (next && n++ < maxscroll) {
6356 last = got_tree_entry_get_next(s->tree, last);
6357 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6358 s->first_displayed_entry = next;
6359 next = got_tree_entry_get_next(s->tree, next);
6366 static const struct got_error *
6367 tree_entry_path(char **path, struct tog_parent_trees *parents,
6368 struct got_tree_entry *te)
6370 const struct got_error *err = NULL;
6371 struct tog_parent_tree *pt;
6372 size_t len = 2; /* for leading slash and NUL */
6374 TAILQ_FOREACH(pt, parents, entry)
6375 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6378 len += strlen(got_tree_entry_get_name(te));
6380 *path = calloc(1, len);
6382 return got_error_from_errno("calloc");
6385 pt = TAILQ_LAST(parents, tog_parent_trees);
6387 const char *name = got_tree_entry_get_name(pt->selected_entry);
6388 if (strlcat(*path, name, len) >= len) {
6389 err = got_error(GOT_ERR_NO_SPACE);
6392 if (strlcat(*path, "/", len) >= len) {
6393 err = got_error(GOT_ERR_NO_SPACE);
6396 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6399 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6400 err = got_error(GOT_ERR_NO_SPACE);
6412 static const struct got_error *
6413 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6414 struct got_tree_entry *te, struct tog_parent_trees *parents,
6415 struct got_object_id *commit_id, struct got_repository *repo)
6417 const struct got_error *err = NULL;
6419 struct tog_view *blame_view;
6423 err = tree_entry_path(&path, parents, te);
6427 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6428 if (blame_view == NULL) {
6429 err = got_error_from_errno("view_open");
6433 err = open_blame_view(blame_view, path, commit_id, repo);
6435 if (err->code == GOT_ERR_CANCELLED)
6437 view_close(blame_view);
6439 *new_view = blame_view;
6445 static const struct got_error *
6446 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6447 struct tog_tree_view_state *s)
6449 struct tog_view *log_view;
6450 const struct got_error *err = NULL;
6455 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6456 if (log_view == NULL)
6457 return got_error_from_errno("view_open");
6459 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6463 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6466 view_close(log_view);
6468 *new_view = log_view;
6473 static const struct got_error *
6474 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6475 const char *head_ref_name, struct got_repository *repo)
6477 const struct got_error *err = NULL;
6478 char *commit_id_str = NULL;
6479 struct tog_tree_view_state *s = &view->state.tree;
6480 struct got_commit_object *commit = NULL;
6482 TAILQ_INIT(&s->parents);
6483 STAILQ_INIT(&s->colors);
6485 s->commit_id = got_object_id_dup(commit_id);
6486 if (s->commit_id == NULL)
6487 return got_error_from_errno("got_object_id_dup");
6489 err = got_object_open_as_commit(&commit, repo, commit_id);
6494 * The root is opened here and will be closed when the view is closed.
6495 * Any visited subtrees and their path-wise parents are opened and
6498 err = got_object_open_as_tree(&s->root, repo,
6499 got_object_commit_get_tree_id(commit));
6504 err = got_object_id_str(&commit_id_str, commit_id);
6508 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6509 err = got_error_from_errno("asprintf");
6513 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6514 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6515 if (head_ref_name) {
6516 s->head_ref_name = strdup(head_ref_name);
6517 if (s->head_ref_name == NULL) {
6518 err = got_error_from_errno("strdup");
6524 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6525 err = add_color(&s->colors, "\\$$",
6526 TOG_COLOR_TREE_SUBMODULE,
6527 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6530 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6531 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6534 err = add_color(&s->colors, "/$",
6535 TOG_COLOR_TREE_DIRECTORY,
6536 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6540 err = add_color(&s->colors, "\\*$",
6541 TOG_COLOR_TREE_EXECUTABLE,
6542 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6546 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6547 get_color_value("TOG_COLOR_COMMIT"));
6552 view->show = show_tree_view;
6553 view->input = input_tree_view;
6554 view->close = close_tree_view;
6555 view->search_start = search_start_tree_view;
6556 view->search_next = search_next_tree_view;
6558 free(commit_id_str);
6560 got_object_commit_close(commit);
6562 close_tree_view(view);
6566 static const struct got_error *
6567 close_tree_view(struct tog_view *view)
6569 struct tog_tree_view_state *s = &view->state.tree;
6571 free_colors(&s->colors);
6572 free(s->tree_label);
6573 s->tree_label = NULL;
6575 s->commit_id = NULL;
6576 free(s->head_ref_name);
6577 s->head_ref_name = NULL;
6578 while (!TAILQ_EMPTY(&s->parents)) {
6579 struct tog_parent_tree *parent;
6580 parent = TAILQ_FIRST(&s->parents);
6581 TAILQ_REMOVE(&s->parents, parent, entry);
6582 if (parent->tree != s->root)
6583 got_object_tree_close(parent->tree);
6587 if (s->tree != NULL && s->tree != s->root)
6588 got_object_tree_close(s->tree);
6590 got_object_tree_close(s->root);
6594 static const struct got_error *
6595 search_start_tree_view(struct tog_view *view)
6597 struct tog_tree_view_state *s = &view->state.tree;
6599 s->matched_entry = NULL;
6604 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6606 regmatch_t regmatch;
6608 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6612 static const struct got_error *
6613 search_next_tree_view(struct tog_view *view)
6615 struct tog_tree_view_state *s = &view->state.tree;
6616 struct got_tree_entry *te = NULL;
6618 if (!view->searching) {
6619 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6623 if (s->matched_entry) {
6624 if (view->searching == TOG_SEARCH_FORWARD) {
6625 if (s->selected_entry)
6626 te = got_tree_entry_get_next(s->tree,
6629 te = got_object_tree_get_first_entry(s->tree);
6631 if (s->selected_entry == NULL)
6632 te = got_object_tree_get_last_entry(s->tree);
6634 te = got_tree_entry_get_prev(s->tree,
6638 if (s->selected_entry)
6639 te = s->selected_entry;
6640 else if (view->searching == TOG_SEARCH_FORWARD)
6641 te = got_object_tree_get_first_entry(s->tree);
6643 te = got_object_tree_get_last_entry(s->tree);
6648 if (s->matched_entry == NULL) {
6649 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6652 if (view->searching == TOG_SEARCH_FORWARD)
6653 te = got_object_tree_get_first_entry(s->tree);
6655 te = got_object_tree_get_last_entry(s->tree);
6658 if (match_tree_entry(te, &view->regex)) {
6659 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6660 s->matched_entry = te;
6664 if (view->searching == TOG_SEARCH_FORWARD)
6665 te = got_tree_entry_get_next(s->tree, te);
6667 te = got_tree_entry_get_prev(s->tree, te);
6670 if (s->matched_entry) {
6671 s->first_displayed_entry = s->matched_entry;
6678 static const struct got_error *
6679 show_tree_view(struct tog_view *view)
6681 const struct got_error *err = NULL;
6682 struct tog_tree_view_state *s = &view->state.tree;
6685 err = tree_entry_path(&parent_path, &s->parents, NULL);
6689 err = draw_tree_entries(view, parent_path);
6696 static const struct got_error *
6697 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6699 const struct got_error *err = NULL;
6700 struct tog_tree_view_state *s = &view->state.tree;
6701 struct tog_view *log_view, *ref_view;
6702 struct got_tree_entry *te;
6703 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6707 s->show_ids = !s->show_ids;
6712 if (!s->selected_entry)
6714 if (view_is_parent_view(view))
6715 view_get_split(view, &begin_y, &begin_x);
6716 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6717 if (view_is_parent_view(view) &&
6718 view->mode == TOG_VIEW_SPLIT_HRZN) {
6719 err = view_init_hsplit(view, begin_y);
6724 log_view->focussed = 1;
6725 log_view->mode = view->mode;
6726 log_view->nlines = view->lines - begin_y;
6727 if (view_is_parent_view(view)) {
6728 view_transfer_size(log_view, view);
6729 err = view_close_child(view);
6732 err = view_set_child(view, log_view);
6735 view->focus_child = 1;
6737 *new_view = log_view;
6741 if (view_is_parent_view(view))
6742 view_get_split(view, &begin_y, &begin_x);
6743 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6744 if (ref_view == NULL)
6745 return got_error_from_errno("view_open");
6746 err = open_ref_view(ref_view, s->repo);
6748 view_close(ref_view);
6751 if (view_is_parent_view(view) &&
6752 view->mode == TOG_VIEW_SPLIT_HRZN) {
6753 err = view_init_hsplit(view, begin_y);
6758 ref_view->focussed = 1;
6759 ref_view->mode = view->mode;
6760 ref_view->nlines = view->lines - begin_y;
6761 if (view_is_parent_view(view)) {
6762 view_transfer_size(ref_view, view);
6763 err = view_close_child(view);
6766 err = view_set_child(view, ref_view);
6769 view->focus_child = 1;
6771 *new_view = ref_view;
6777 if (s->tree == s->root)
6778 s->first_displayed_entry =
6779 got_object_tree_get_first_entry(s->tree);
6781 s->first_displayed_entry = NULL;
6785 int eos = view->nlines - 3;
6787 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6791 te = got_object_tree_get_last_entry(s->tree);
6792 for (n = 0; n < eos; n++) {
6794 if(s->tree != s->root) {
6795 s->first_displayed_entry = NULL;
6800 s->first_displayed_entry = te;
6801 te = got_tree_entry_get_prev(s->tree, te);
6804 s->selected = n - 1;
6810 if (s->selected > 0) {
6814 tree_scroll_up(s, 1);
6815 if (s->selected_entry == NULL ||
6816 (s->tree == s->root && s->selected_entry ==
6817 got_object_tree_get_first_entry(s->tree)))
6827 if (s->tree == s->root) {
6828 if (got_object_tree_get_first_entry(s->tree) ==
6829 s->first_displayed_entry)
6830 s->selected -= MIN(s->selected, nscroll);
6832 if (s->first_displayed_entry == NULL)
6833 s->selected -= MIN(s->selected, nscroll);
6835 tree_scroll_up(s, MAX(0, nscroll));
6836 if (s->selected_entry == NULL ||
6837 (s->tree == s->root && s->selected_entry ==
6838 got_object_tree_get_first_entry(s->tree)))
6844 if (s->selected < s->ndisplayed - 1) {
6848 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6850 /* can't scroll any further */
6854 tree_scroll_down(view, 1);
6864 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6866 /* can't scroll any further; move cursor down */
6867 if (s->selected < s->ndisplayed - 1)
6868 s->selected += MIN(nscroll,
6869 s->ndisplayed - s->selected - 1);
6874 tree_scroll_down(view, nscroll);
6879 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6880 struct tog_parent_tree *parent;
6881 /* user selected '..' */
6882 if (s->tree == s->root) {
6886 parent = TAILQ_FIRST(&s->parents);
6887 TAILQ_REMOVE(&s->parents, parent,
6889 got_object_tree_close(s->tree);
6890 s->tree = parent->tree;
6891 s->first_displayed_entry =
6892 parent->first_displayed_entry;
6894 parent->selected_entry;
6895 s->selected = parent->selected;
6896 if (s->selected > view->nlines - 3) {
6897 err = offset_selection_down(view);
6902 } else if (S_ISDIR(got_tree_entry_get_mode(
6903 s->selected_entry))) {
6904 struct got_tree_object *subtree;
6906 err = got_object_open_as_tree(&subtree, s->repo,
6907 got_tree_entry_get_id(s->selected_entry));
6910 err = tree_view_visit_subtree(s, subtree);
6912 got_object_tree_close(subtree);
6915 } else if (S_ISREG(got_tree_entry_get_mode(
6916 s->selected_entry))) {
6917 struct tog_view *blame_view;
6918 int begin_x = 0, begin_y = 0;
6920 if (view_is_parent_view(view))
6921 view_get_split(view, &begin_y, &begin_x);
6923 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6924 s->selected_entry, &s->parents,
6925 s->commit_id, s->repo);
6929 if (view_is_parent_view(view) &&
6930 view->mode == TOG_VIEW_SPLIT_HRZN) {
6931 err = view_init_hsplit(view, begin_y);
6938 blame_view->focussed = 1;
6939 blame_view->mode = view->mode;
6940 blame_view->nlines = view->lines - begin_y;
6941 if (view_is_parent_view(view)) {
6942 view_transfer_size(blame_view, view);
6943 err = view_close_child(view);
6946 err = view_set_child(view, blame_view);
6949 view->focus_child = 1;
6951 *new_view = blame_view;
6955 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6956 s->selected = view->nlines - 4;
6972 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6977 static const struct got_error *
6978 cmd_tree(int argc, char *argv[])
6980 const struct got_error *error;
6981 struct got_repository *repo = NULL;
6982 struct got_worktree *worktree = NULL;
6983 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6984 struct got_object_id *commit_id = NULL;
6985 struct got_commit_object *commit = NULL;
6986 const char *commit_id_arg = NULL;
6988 struct got_reference *ref = NULL;
6989 const char *head_ref_name = NULL;
6991 struct tog_view *view;
6992 int *pack_fds = NULL;
6994 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6997 commit_id_arg = optarg;
7000 repo_path = realpath(optarg, NULL);
7001 if (repo_path == NULL)
7002 return got_error_from_errno2("realpath",
7017 error = got_repo_pack_fds_open(&pack_fds);
7021 if (repo_path == NULL) {
7022 cwd = getcwd(NULL, 0);
7024 return got_error_from_errno("getcwd");
7025 error = got_worktree_open(&worktree, cwd);
7026 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7030 strdup(got_worktree_get_repo_path(worktree));
7032 repo_path = strdup(cwd);
7033 if (repo_path == NULL) {
7034 error = got_error_from_errno("strdup");
7039 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7043 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7050 error = apply_unveil(got_repo_get_path(repo), NULL);
7054 error = tog_load_refs(repo, 0);
7058 if (commit_id_arg == NULL) {
7059 error = got_repo_match_object_id(&commit_id, &label,
7060 worktree ? got_worktree_get_head_ref_name(worktree) :
7061 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7064 head_ref_name = label;
7066 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7068 head_ref_name = got_ref_get_name(ref);
7069 else if (error->code != GOT_ERR_NOT_REF)
7071 error = got_repo_match_object_id(&commit_id, NULL,
7072 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7077 error = got_object_open_as_commit(&commit, repo, commit_id);
7081 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7083 error = got_error_from_errno("view_open");
7086 error = open_tree_view(view, commit_id, head_ref_name, repo);
7089 if (!got_path_is_root_dir(in_repo_path)) {
7090 error = tree_view_walk_path(&view->state.tree, commit,
7097 /* Release work tree lock. */
7098 got_worktree_close(worktree);
7101 error = view_loop(view);
7110 const struct got_error *close_err = got_repo_close(repo);
7115 const struct got_error *pack_err =
7116 got_repo_pack_fds_close(pack_fds);
7124 static const struct got_error *
7125 ref_view_load_refs(struct tog_ref_view_state *s)
7127 struct got_reflist_entry *sre;
7128 struct tog_reflist_entry *re;
7131 TAILQ_FOREACH(sre, &tog_refs, entry) {
7132 if (strncmp(got_ref_get_name(sre->ref),
7133 "refs/got/", 9) == 0 &&
7134 strncmp(got_ref_get_name(sre->ref),
7135 "refs/got/backup/", 16) != 0)
7138 re = malloc(sizeof(*re));
7140 return got_error_from_errno("malloc");
7142 re->ref = got_ref_dup(sre->ref);
7143 if (re->ref == NULL)
7144 return got_error_from_errno("got_ref_dup");
7145 re->idx = s->nrefs++;
7146 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7149 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7154 ref_view_free_refs(struct tog_ref_view_state *s)
7156 struct tog_reflist_entry *re;
7158 while (!TAILQ_EMPTY(&s->refs)) {
7159 re = TAILQ_FIRST(&s->refs);
7160 TAILQ_REMOVE(&s->refs, re, entry);
7161 got_ref_close(re->ref);
7166 static const struct got_error *
7167 open_ref_view(struct tog_view *view, struct got_repository *repo)
7169 const struct got_error *err = NULL;
7170 struct tog_ref_view_state *s = &view->state.ref;
7172 s->selected_entry = 0;
7175 TAILQ_INIT(&s->refs);
7176 STAILQ_INIT(&s->colors);
7178 err = ref_view_load_refs(s);
7182 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7183 err = add_color(&s->colors, "^refs/heads/",
7184 TOG_COLOR_REFS_HEADS,
7185 get_color_value("TOG_COLOR_REFS_HEADS"));
7189 err = add_color(&s->colors, "^refs/tags/",
7190 TOG_COLOR_REFS_TAGS,
7191 get_color_value("TOG_COLOR_REFS_TAGS"));
7195 err = add_color(&s->colors, "^refs/remotes/",
7196 TOG_COLOR_REFS_REMOTES,
7197 get_color_value("TOG_COLOR_REFS_REMOTES"));
7201 err = add_color(&s->colors, "^refs/got/backup/",
7202 TOG_COLOR_REFS_BACKUP,
7203 get_color_value("TOG_COLOR_REFS_BACKUP"));
7208 view->show = show_ref_view;
7209 view->input = input_ref_view;
7210 view->close = close_ref_view;
7211 view->search_start = search_start_ref_view;
7212 view->search_next = search_next_ref_view;
7215 free_colors(&s->colors);
7219 static const struct got_error *
7220 close_ref_view(struct tog_view *view)
7222 struct tog_ref_view_state *s = &view->state.ref;
7224 ref_view_free_refs(s);
7225 free_colors(&s->colors);
7230 static const struct got_error *
7231 resolve_reflist_entry(struct got_object_id **commit_id,
7232 struct tog_reflist_entry *re, struct got_repository *repo)
7234 const struct got_error *err = NULL;
7235 struct got_object_id *obj_id;
7236 struct got_tag_object *tag = NULL;
7241 err = got_ref_resolve(&obj_id, repo, re->ref);
7245 err = got_object_get_type(&obj_type, repo, obj_id);
7250 case GOT_OBJ_TYPE_COMMIT:
7251 *commit_id = obj_id;
7253 case GOT_OBJ_TYPE_TAG:
7254 err = got_object_open_as_tag(&tag, repo, obj_id);
7258 err = got_object_get_type(&obj_type, repo,
7259 got_object_tag_get_object_id(tag));
7262 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7263 err = got_error(GOT_ERR_OBJ_TYPE);
7266 *commit_id = got_object_id_dup(
7267 got_object_tag_get_object_id(tag));
7268 if (*commit_id == NULL) {
7269 err = got_error_from_errno("got_object_id_dup");
7274 err = got_error(GOT_ERR_OBJ_TYPE);
7280 got_object_tag_close(tag);
7288 static const struct got_error *
7289 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7290 struct tog_reflist_entry *re, struct got_repository *repo)
7292 struct tog_view *log_view;
7293 const struct got_error *err = NULL;
7294 struct got_object_id *commit_id = NULL;
7298 err = resolve_reflist_entry(&commit_id, re, repo);
7300 if (err->code != GOT_ERR_OBJ_TYPE)
7306 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7307 if (log_view == NULL) {
7308 err = got_error_from_errno("view_open");
7312 err = open_log_view(log_view, commit_id, repo,
7313 got_ref_get_name(re->ref), "", 0);
7316 view_close(log_view);
7318 *new_view = log_view;
7324 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7326 struct tog_reflist_entry *re;
7329 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7332 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7333 while (i++ < maxscroll) {
7336 s->first_displayed_entry = re;
7337 re = TAILQ_PREV(re, tog_reflist_head, entry);
7341 static const struct got_error *
7342 ref_scroll_down(struct tog_view *view, int maxscroll)
7344 struct tog_ref_view_state *s = &view->state.ref;
7345 struct tog_reflist_entry *next, *last;
7348 if (s->first_displayed_entry)
7349 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7351 next = TAILQ_FIRST(&s->refs);
7353 last = s->last_displayed_entry;
7354 while (next && n++ < maxscroll) {
7356 last = TAILQ_NEXT(last, entry);
7357 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7358 s->first_displayed_entry = next;
7359 next = TAILQ_NEXT(next, entry);
7366 static const struct got_error *
7367 search_start_ref_view(struct tog_view *view)
7369 struct tog_ref_view_state *s = &view->state.ref;
7371 s->matched_entry = NULL;
7376 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7378 regmatch_t regmatch;
7380 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7384 static const struct got_error *
7385 search_next_ref_view(struct tog_view *view)
7387 struct tog_ref_view_state *s = &view->state.ref;
7388 struct tog_reflist_entry *re = NULL;
7390 if (!view->searching) {
7391 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7395 if (s->matched_entry) {
7396 if (view->searching == TOG_SEARCH_FORWARD) {
7397 if (s->selected_entry)
7398 re = TAILQ_NEXT(s->selected_entry, entry);
7400 re = TAILQ_PREV(s->selected_entry,
7401 tog_reflist_head, entry);
7403 if (s->selected_entry == NULL)
7404 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7406 re = TAILQ_PREV(s->selected_entry,
7407 tog_reflist_head, entry);
7410 if (s->selected_entry)
7411 re = s->selected_entry;
7412 else if (view->searching == TOG_SEARCH_FORWARD)
7413 re = TAILQ_FIRST(&s->refs);
7415 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7420 if (s->matched_entry == NULL) {
7421 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7424 if (view->searching == TOG_SEARCH_FORWARD)
7425 re = TAILQ_FIRST(&s->refs);
7427 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7430 if (match_reflist_entry(re, &view->regex)) {
7431 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7432 s->matched_entry = re;
7436 if (view->searching == TOG_SEARCH_FORWARD)
7437 re = TAILQ_NEXT(re, entry);
7439 re = TAILQ_PREV(re, tog_reflist_head, entry);
7442 if (s->matched_entry) {
7443 s->first_displayed_entry = s->matched_entry;
7450 static const struct got_error *
7451 show_ref_view(struct tog_view *view)
7453 const struct got_error *err = NULL;
7454 struct tog_ref_view_state *s = &view->state.ref;
7455 struct tog_reflist_entry *re;
7458 struct tog_color *tc;
7460 int limit = view->nlines;
7462 werase(view->window);
7465 if (view_is_hsplit_top(view))
7466 --limit; /* border */
7471 re = s->first_displayed_entry;
7473 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7475 return got_error_from_errno("asprintf");
7477 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7482 if (view_needs_focus_indication(view))
7483 wstandout(view->window);
7484 waddwstr(view->window, wline);
7485 if (view_needs_focus_indication(view))
7486 wstandend(view->window);
7491 if (width < view->ncols - 1)
7492 waddch(view->window, '\n');
7497 while (re && limit > 0) {
7499 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7502 struct got_commit_object *ci;
7503 struct got_tag_object *tag;
7504 struct got_object_id *id;
7508 err = got_ref_resolve(&id, s->repo, re->ref);
7511 err = got_object_open_as_tag(&tag, s->repo, id);
7513 if (err->code != GOT_ERR_OBJ_TYPE) {
7517 err = got_object_open_as_commit(&ci, s->repo,
7523 t = got_object_commit_get_committer_time(ci);
7524 got_object_commit_close(ci);
7526 t = got_object_tag_get_tagger_time(tag);
7527 got_object_tag_close(tag);
7530 if (gmtime_r(&t, &tm) == NULL)
7531 return got_error_from_errno("gmtime_r");
7532 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7533 return got_error(GOT_ERR_NO_SPACE);
7535 if (got_ref_is_symbolic(re->ref)) {
7536 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7537 ymd : "", got_ref_get_name(re->ref),
7538 got_ref_get_symref_target(re->ref)) == -1)
7539 return got_error_from_errno("asprintf");
7540 } else if (s->show_ids) {
7541 struct got_object_id *id;
7543 err = got_ref_resolve(&id, s->repo, re->ref);
7546 err = got_object_id_str(&id_str, id);
7551 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7552 got_ref_get_name(re->ref), id_str) == -1) {
7553 err = got_error_from_errno("asprintf");
7560 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7561 got_ref_get_name(re->ref)) == -1)
7562 return got_error_from_errno("asprintf");
7564 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7570 if (n == s->selected) {
7572 wstandout(view->window);
7573 s->selected_entry = re;
7575 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7577 wattr_on(view->window,
7578 COLOR_PAIR(tc->colorpair), NULL);
7579 waddwstr(view->window, wline);
7581 wattr_off(view->window,
7582 COLOR_PAIR(tc->colorpair), NULL);
7583 if (width < view->ncols - 1)
7584 waddch(view->window, '\n');
7585 if (n == s->selected && view->focussed)
7586 wstandend(view->window);
7592 s->last_displayed_entry = re;
7595 re = TAILQ_NEXT(re, entry);
7602 static const struct got_error *
7603 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7604 struct tog_reflist_entry *re, struct got_repository *repo)
7606 const struct got_error *err = NULL;
7607 struct got_object_id *commit_id = NULL;
7608 struct tog_view *tree_view;
7612 err = resolve_reflist_entry(&commit_id, re, repo);
7614 if (err->code != GOT_ERR_OBJ_TYPE)
7621 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7622 if (tree_view == NULL) {
7623 err = got_error_from_errno("view_open");
7627 err = open_tree_view(tree_view, commit_id,
7628 got_ref_get_name(re->ref), repo);
7632 *new_view = tree_view;
7637 static const struct got_error *
7638 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7640 const struct got_error *err = NULL;
7641 struct tog_ref_view_state *s = &view->state.ref;
7642 struct tog_view *log_view, *tree_view;
7643 struct tog_reflist_entry *re;
7644 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7648 s->show_ids = !s->show_ids;
7652 s->show_date = !s->show_date;
7656 s->sort_by_date = !s->sort_by_date;
7658 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7659 got_ref_cmp_by_commit_timestamp_descending :
7660 tog_ref_cmp_by_name, s->repo);
7663 got_reflist_object_id_map_free(tog_refs_idmap);
7664 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7665 &tog_refs, s->repo);
7668 ref_view_free_refs(s);
7669 err = ref_view_load_refs(s);
7674 if (!s->selected_entry)
7676 if (view_is_parent_view(view))
7677 view_get_split(view, &begin_y, &begin_x);
7679 err = log_ref_entry(&log_view, begin_y, begin_x,
7680 s->selected_entry, s->repo);
7684 if (view_is_parent_view(view) &&
7685 view->mode == TOG_VIEW_SPLIT_HRZN) {
7686 err = view_init_hsplit(view, begin_y);
7692 log_view->focussed = 1;
7693 log_view->mode = view->mode;
7694 log_view->nlines = view->lines - begin_y;
7695 if (view_is_parent_view(view)) {
7696 view_transfer_size(log_view, view);
7697 err = view_close_child(view);
7700 err = view_set_child(view, log_view);
7703 view->focus_child = 1;
7705 *new_view = log_view;
7709 if (!s->selected_entry)
7711 if (view_is_parent_view(view))
7712 view_get_split(view, &begin_y, &begin_x);
7713 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7714 s->selected_entry, s->repo);
7715 if (err || tree_view == NULL)
7717 if (view_is_parent_view(view) &&
7718 view->mode == TOG_VIEW_SPLIT_HRZN) {
7719 err = view_init_hsplit(view, begin_y);
7724 tree_view->focussed = 1;
7725 tree_view->mode = view->mode;
7726 tree_view->nlines = view->lines - begin_y;
7727 if (view_is_parent_view(view)) {
7728 view_transfer_size(tree_view, view);
7729 err = view_close_child(view);
7732 err = view_set_child(view, tree_view);
7735 view->focus_child = 1;
7737 *new_view = tree_view;
7743 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7747 int eos = view->nlines - 1;
7749 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7753 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7754 for (n = 0; n < eos; n++) {
7757 s->first_displayed_entry = re;
7758 re = TAILQ_PREV(re, tog_reflist_head, entry);
7761 s->selected = n - 1;
7767 if (s->selected > 0) {
7771 ref_scroll_up(s, 1);
7772 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7782 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7783 s->selected -= MIN(nscroll, s->selected);
7784 ref_scroll_up(s, MAX(0, nscroll));
7785 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7791 if (s->selected < s->ndisplayed - 1) {
7795 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7796 /* can't scroll any further */
7800 ref_scroll_down(view, 1);
7810 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7811 /* can't scroll any further; move cursor down */
7812 if (s->selected < s->ndisplayed - 1)
7813 s->selected += MIN(nscroll,
7814 s->ndisplayed - s->selected - 1);
7815 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7816 s->selected += s->ndisplayed - s->selected - 1;
7820 ref_scroll_down(view, nscroll);
7825 err = tog_load_refs(s->repo, s->sort_by_date);
7828 ref_view_free_refs(s);
7829 err = ref_view_load_refs(s);
7832 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7833 s->selected = view->nlines - 2;
7847 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7852 static const struct got_error *
7853 cmd_ref(int argc, char *argv[])
7855 const struct got_error *error;
7856 struct got_repository *repo = NULL;
7857 struct got_worktree *worktree = NULL;
7858 char *cwd = NULL, *repo_path = NULL;
7860 struct tog_view *view;
7861 int *pack_fds = NULL;
7863 while ((ch = getopt(argc, argv, "r:")) != -1) {
7866 repo_path = realpath(optarg, NULL);
7867 if (repo_path == NULL)
7868 return got_error_from_errno2("realpath",
7883 error = got_repo_pack_fds_open(&pack_fds);
7887 if (repo_path == NULL) {
7888 cwd = getcwd(NULL, 0);
7890 return got_error_from_errno("getcwd");
7891 error = got_worktree_open(&worktree, cwd);
7892 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7896 strdup(got_worktree_get_repo_path(worktree));
7898 repo_path = strdup(cwd);
7899 if (repo_path == NULL) {
7900 error = got_error_from_errno("strdup");
7905 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7911 error = apply_unveil(got_repo_get_path(repo), NULL);
7915 error = tog_load_refs(repo, 0);
7919 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7921 error = got_error_from_errno("view_open");
7925 error = open_ref_view(view, repo);
7930 /* Release work tree lock. */
7931 got_worktree_close(worktree);
7934 error = view_loop(view);
7939 const struct got_error *close_err = got_repo_close(repo);
7944 const struct got_error *pack_err =
7945 got_repo_pack_fds_close(pack_fds);
7954 * If view was scrolled down to move the selected line into view when opening a
7955 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7958 offset_selection_up(struct tog_view *view)
7960 switch (view->type) {
7961 case TOG_VIEW_BLAME: {
7962 struct tog_blame_view_state *s = &view->state.blame;
7963 if (s->first_displayed_line == 1) {
7964 s->selected_line = MAX(s->selected_line - view->offset,
7968 if (s->first_displayed_line > view->offset)
7969 s->first_displayed_line -= view->offset;
7971 s->first_displayed_line = 1;
7972 s->selected_line += view->offset;
7976 log_scroll_up(&view->state.log, view->offset);
7977 view->state.log.selected += view->offset;
7980 ref_scroll_up(&view->state.ref, view->offset);
7981 view->state.ref.selected += view->offset;
7984 tree_scroll_up(&view->state.tree, view->offset);
7985 view->state.tree.selected += view->offset;
7995 * If the selected line is in the section of screen covered by the bottom split,
7996 * scroll down offset lines to move it into view and index its new position.
7998 static const struct got_error *
7999 offset_selection_down(struct tog_view *view)
8001 const struct got_error *err = NULL;
8002 const struct got_error *(*scrolld)(struct tog_view *, int);
8003 int *selected = NULL;
8006 switch (view->type) {
8007 case TOG_VIEW_BLAME: {
8008 struct tog_blame_view_state *s = &view->state.blame;
8011 if (s->selected_line > view->nlines - header) {
8012 offset = abs(view->nlines - s->selected_line - header);
8013 s->first_displayed_line += offset;
8014 s->selected_line -= offset;
8015 view->offset = offset;
8019 case TOG_VIEW_LOG: {
8020 struct tog_log_view_state *s = &view->state.log;
8021 scrolld = &log_scroll_down;
8022 header = view_is_parent_view(view) ? 3 : 2;
8023 selected = &s->selected;
8026 case TOG_VIEW_REF: {
8027 struct tog_ref_view_state *s = &view->state.ref;
8028 scrolld = &ref_scroll_down;
8030 selected = &s->selected;
8033 case TOG_VIEW_TREE: {
8034 struct tog_tree_view_state *s = &view->state.tree;
8035 scrolld = &tree_scroll_down;
8037 selected = &s->selected;
8047 if (selected && *selected > view->nlines - header) {
8048 offset = abs(view->nlines - *selected - header);
8049 view->offset = offset;
8050 if (scrolld && offset) {
8051 err = scrolld(view, offset);
8052 *selected -= offset;
8060 list_commands(FILE *fp)
8064 fprintf(fp, "commands:");
8065 for (i = 0; i < nitems(tog_commands); i++) {
8066 const struct tog_cmd *cmd = &tog_commands[i];
8067 fprintf(fp, " %s", cmd->name);
8073 usage(int hflag, int status)
8075 FILE *fp = (status == 0) ? stdout : stderr;
8077 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8080 fprintf(fp, "lazy usage: %s path\n", getprogname());
8087 make_argv(int argc, ...)
8095 argv = calloc(argc, sizeof(char *));
8098 for (i = 0; i < argc; i++) {
8099 argv[i] = strdup(va_arg(ap, char *));
8100 if (argv[i] == NULL)
8109 * Try to convert 'tog path' into a 'tog log path' command.
8110 * The user could simply have mistyped the command rather than knowingly
8111 * provided a path. So check whether argv[0] can in fact be resolved
8112 * to a path in the HEAD commit and print a special error if not.
8113 * This hack is for mpi@ <3
8115 static const struct got_error *
8116 tog_log_with_path(int argc, char *argv[])
8118 const struct got_error *error = NULL, *close_err;
8119 const struct tog_cmd *cmd = NULL;
8120 struct got_repository *repo = NULL;
8121 struct got_worktree *worktree = NULL;
8122 struct got_object_id *commit_id = NULL, *id = NULL;
8123 struct got_commit_object *commit = NULL;
8124 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8125 char *commit_id_str = NULL, **cmd_argv = NULL;
8126 int *pack_fds = NULL;
8128 cwd = getcwd(NULL, 0);