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.
18 #include <sys/ioctl.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
52 #include "got_opentemp.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
59 #include "got_worktree.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #define CTRL(x) ((x) & 0x1f)
74 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
83 __dead static void usage(int, int);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_ref(void);
90 static const struct got_error* cmd_log(int, char *[]);
91 static const struct got_error* cmd_diff(int, char *[]);
92 static const struct got_error* cmd_blame(int, char *[]);
93 static const struct got_error* cmd_tree(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
96 static const struct tog_cmd tog_commands[] = {
97 { "log", cmd_log, usage_log },
98 { "diff", cmd_diff, usage_diff },
99 { "blame", cmd_blame, usage_blame },
100 { "tree", cmd_tree, usage_tree },
101 { "ref", cmd_ref, usage_ref },
118 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 #define TOG_EOF_STRING "(END)"
122 struct commit_queue_entry {
123 TAILQ_ENTRY(commit_queue_entry) entry;
124 struct got_object_id *id;
125 struct got_commit_object *commit;
128 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
129 struct commit_queue {
131 struct commit_queue_head head;
135 STAILQ_ENTRY(tog_color) entry;
139 STAILQ_HEAD(tog_colors, tog_color);
141 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
142 static struct got_reflist_object_id_map *tog_refs_idmap;
143 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 static const struct got_error *
146 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
147 struct got_reference* re2)
149 const char *name1 = got_ref_get_name(re1);
150 const char *name2 = got_ref_get_name(re2);
151 int isbackup1, isbackup2;
153 /* Sort backup refs towards the bottom of the list. */
154 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
155 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
156 if (!isbackup1 && isbackup2) {
159 } else if (isbackup1 && !isbackup2) {
164 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
168 static const struct got_error *
169 tog_load_refs(struct got_repository *repo, int sort_by_date)
171 const struct got_error *err;
173 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
174 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
179 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
186 if (tog_refs_idmap) {
187 got_reflist_object_id_map_free(tog_refs_idmap);
188 tog_refs_idmap = NULL;
190 got_ref_list_free(&tog_refs);
193 static const struct got_error *
194 add_color(struct tog_colors *colors, const char *pattern,
195 int idx, short color)
197 const struct got_error *err = NULL;
198 struct tog_color *tc;
201 if (idx < 1 || idx > COLOR_PAIRS - 1)
204 init_pair(idx, color, -1);
206 tc = calloc(1, sizeof(*tc));
208 return got_error_from_errno("calloc");
209 regerr = regcomp(&tc->regex, pattern,
210 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 static char regerr_msg[512];
213 static char err_msg[512];
214 regerror(regerr, &tc->regex, regerr_msg,
216 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 err = got_error_msg(GOT_ERR_REGEX, err_msg);
223 STAILQ_INSERT_HEAD(colors, tc, entry);
228 free_colors(struct tog_colors *colors)
230 struct tog_color *tc;
232 while (!STAILQ_EMPTY(colors)) {
233 tc = STAILQ_FIRST(colors);
234 STAILQ_REMOVE_HEAD(colors, entry);
240 static struct tog_color *
241 get_color(struct tog_colors *colors, int colorpair)
243 struct tog_color *tc = NULL;
245 STAILQ_FOREACH(tc, colors, entry) {
246 if (tc->colorpair == colorpair)
254 default_color_value(const char *envvar)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
257 return COLOR_MAGENTA;
258 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
265 return COLOR_MAGENTA;
266 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
291 get_color_value(const char *envvar)
293 const char *val = getenv(envvar);
296 return default_color_value(envvar);
298 if (strcasecmp(val, "black") == 0)
300 if (strcasecmp(val, "red") == 0)
302 if (strcasecmp(val, "green") == 0)
304 if (strcasecmp(val, "yellow") == 0)
306 if (strcasecmp(val, "blue") == 0)
308 if (strcasecmp(val, "magenta") == 0)
309 return COLOR_MAGENTA;
310 if (strcasecmp(val, "cyan") == 0)
312 if (strcasecmp(val, "white") == 0)
314 if (strcasecmp(val, "default") == 0)
317 return default_color_value(envvar);
321 struct tog_diff_view_state {
322 struct got_object_id *id1, *id2;
323 const char *label1, *label2;
326 int first_displayed_line;
327 int last_displayed_line;
330 int ignore_whitespace;
332 struct got_repository *repo;
333 struct tog_colors colors;
339 /* passed from log or blame view; may be NULL */
340 struct tog_view *parent_view;
343 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 static volatile sig_atomic_t tog_thread_error;
346 struct tog_log_thread_args {
347 pthread_cond_t need_commits;
348 pthread_cond_t commit_loaded;
351 struct got_commit_graph *graph;
352 struct commit_queue *commits;
353 const char *in_repo_path;
354 struct got_object_id *start_id;
355 struct got_repository *repo;
359 struct commit_queue_entry **first_displayed_entry;
360 struct commit_queue_entry **selected_entry;
362 int *search_next_done;
366 struct tog_log_view_state {
367 struct commit_queue commits;
368 struct commit_queue_entry *first_displayed_entry;
369 struct commit_queue_entry *last_displayed_entry;
370 struct commit_queue_entry *selected_entry;
375 struct got_repository *repo;
376 struct got_object_id *start_id;
379 struct tog_log_thread_args thread_args;
380 struct commit_queue_entry *matched_entry;
381 struct commit_queue_entry *search_entry;
382 struct tog_colors colors;
385 #define TOG_COLOR_DIFF_MINUS 1
386 #define TOG_COLOR_DIFF_PLUS 2
387 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
388 #define TOG_COLOR_DIFF_META 4
389 #define TOG_COLOR_TREE_SUBMODULE 5
390 #define TOG_COLOR_TREE_SYMLINK 6
391 #define TOG_COLOR_TREE_DIRECTORY 7
392 #define TOG_COLOR_TREE_EXECUTABLE 8
393 #define TOG_COLOR_COMMIT 9
394 #define TOG_COLOR_AUTHOR 10
395 #define TOG_COLOR_DATE 11
396 #define TOG_COLOR_REFS_HEADS 12
397 #define TOG_COLOR_REFS_TAGS 13
398 #define TOG_COLOR_REFS_REMOTES 14
399 #define TOG_COLOR_REFS_BACKUP 15
401 struct tog_blame_cb_args {
402 struct tog_blame_line *lines; /* one per line */
405 struct tog_view *view;
406 struct got_object_id *commit_id;
410 struct tog_blame_thread_args {
412 struct got_repository *repo;
413 struct tog_blame_cb_args *cb_args;
415 got_cancel_cb cancel_cb;
422 struct tog_blame_line *lines;
426 struct tog_blame_thread_args thread_args;
427 struct tog_blame_cb_args cb_args;
432 struct tog_blame_view_state {
433 int first_displayed_line;
434 int last_displayed_line;
436 int last_diffed_line;
440 struct got_object_id_queue blamed_commits;
441 struct got_object_qid *blamed_commit;
443 struct got_repository *repo;
444 struct got_object_id *commit_id;
445 struct tog_blame blame;
447 struct tog_colors colors;
450 struct tog_parent_tree {
451 TAILQ_ENTRY(tog_parent_tree) entry;
452 struct got_tree_object *tree;
453 struct got_tree_entry *first_displayed_entry;
454 struct got_tree_entry *selected_entry;
458 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
460 struct tog_tree_view_state {
462 struct got_object_id *commit_id;/* commit which this tree belongs to */
463 struct got_tree_object *root; /* the commit's root tree entry */
464 struct got_tree_object *tree; /* currently displayed (sub-)tree */
465 struct got_tree_entry *first_displayed_entry;
466 struct got_tree_entry *last_displayed_entry;
467 struct got_tree_entry *selected_entry;
468 int ndisplayed, selected, show_ids;
469 struct tog_parent_trees parents; /* parent trees of current sub-tree */
471 struct got_repository *repo;
472 struct got_tree_entry *matched_entry;
473 struct tog_colors colors;
476 struct tog_reflist_entry {
477 TAILQ_ENTRY(tog_reflist_entry) entry;
478 struct got_reference *ref;
482 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
484 struct tog_ref_view_state {
485 struct tog_reflist_head refs;
486 struct tog_reflist_entry *first_displayed_entry;
487 struct tog_reflist_entry *last_displayed_entry;
488 struct tog_reflist_entry *selected_entry;
489 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
490 struct got_repository *repo;
491 struct tog_reflist_entry *matched_entry;
492 struct tog_colors colors;
496 * We implement two types of views: parent views and child views.
498 * The 'Tab' key switches focus between a parent view and its child view.
499 * Child views are shown side-by-side to their parent view, provided
500 * there is enough screen estate.
502 * When a new view is opened from within a parent view, this new view
503 * becomes a child view of the parent view, replacing any existing child.
505 * When a new view is opened from within a child view, this new view
506 * becomes a parent view which will obscure the views below until the
507 * user quits the new parent view by typing 'q'.
509 * This list of views contains parent views only.
510 * Child views are only pointed to by their parent view.
512 TAILQ_HEAD(tog_view_list_head, tog_view);
515 TAILQ_ENTRY(tog_view) entry;
518 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
519 int resized_y, resized_x; /* begin_y/x based on user resizing */
520 int maxx, x; /* max column and current start column */
521 int lines, cols; /* copies of LINES and COLS */
522 int nscrolled, offset; /* lines scrolled and hsplit line offset */
523 int ch, count; /* current keymap and count prefix */
524 int resize; /* set when in a resize event */
525 int focussed; /* Only set on one parent or child view at a time. */
527 struct tog_view *parent;
528 struct tog_view *child;
531 * This flag is initially set on parent views when a new child view
532 * is created. It gets toggled when the 'Tab' key switches focus
533 * between parent and child.
534 * The flag indicates whether focus should be passed on to our child
535 * view if this parent view gets picked for focus after another parent
536 * view was closed. This prevents child views from losing focus in such
541 enum tog_view_mode mode;
542 /* type-specific state */
543 enum tog_view_type type;
545 struct tog_diff_view_state diff;
546 struct tog_log_view_state log;
547 struct tog_blame_view_state blame;
548 struct tog_tree_view_state tree;
549 struct tog_ref_view_state ref;
552 const struct got_error *(*show)(struct tog_view *);
553 const struct got_error *(*input)(struct tog_view **,
554 struct tog_view *, int);
555 const struct got_error *(*reset)(struct tog_view *);
556 const struct got_error *(*close)(struct tog_view *);
558 const struct got_error *(*search_start)(struct tog_view *);
559 const struct got_error *(*search_next)(struct tog_view *);
562 #define TOG_SEARCH_FORWARD 1
563 #define TOG_SEARCH_BACKWARD 2
564 int search_next_done;
565 #define TOG_SEARCH_HAVE_MORE 1
566 #define TOG_SEARCH_NO_MORE 2
567 #define TOG_SEARCH_HAVE_NONE 3
572 static const struct got_error *open_diff_view(struct tog_view *,
573 struct got_object_id *, struct got_object_id *,
574 const char *, const char *, int, int, int, struct tog_view *,
575 struct got_repository *);
576 static const struct got_error *show_diff_view(struct tog_view *);
577 static const struct got_error *input_diff_view(struct tog_view **,
578 struct tog_view *, int);
579 static const struct got_error *reset_diff_view(struct tog_view *);
580 static const struct got_error* close_diff_view(struct tog_view *);
581 static const struct got_error *search_start_diff_view(struct tog_view *);
582 static const struct got_error *search_next_diff_view(struct tog_view *);
584 static const struct got_error *open_log_view(struct tog_view *,
585 struct got_object_id *, struct got_repository *,
586 const char *, const char *, int);
587 static const struct got_error * show_log_view(struct tog_view *);
588 static const struct got_error *input_log_view(struct tog_view **,
589 struct tog_view *, int);
590 static const struct got_error *close_log_view(struct tog_view *);
591 static const struct got_error *search_start_log_view(struct tog_view *);
592 static const struct got_error *search_next_log_view(struct tog_view *);
594 static const struct got_error *open_blame_view(struct tog_view *, char *,
595 struct got_object_id *, struct got_repository *);
596 static const struct got_error *show_blame_view(struct tog_view *);
597 static const struct got_error *input_blame_view(struct tog_view **,
598 struct tog_view *, int);
599 static const struct got_error *reset_blame_view(struct tog_view *);
600 static const struct got_error *close_blame_view(struct tog_view *);
601 static const struct got_error *search_start_blame_view(struct tog_view *);
602 static const struct got_error *search_next_blame_view(struct tog_view *);
604 static const struct got_error *open_tree_view(struct tog_view *,
605 struct got_object_id *, const char *, struct got_repository *);
606 static const struct got_error *show_tree_view(struct tog_view *);
607 static const struct got_error *input_tree_view(struct tog_view **,
608 struct tog_view *, int);
609 static const struct got_error *close_tree_view(struct tog_view *);
610 static const struct got_error *search_start_tree_view(struct tog_view *);
611 static const struct got_error *search_next_tree_view(struct tog_view *);
613 static const struct got_error *open_ref_view(struct tog_view *,
614 struct got_repository *);
615 static const struct got_error *show_ref_view(struct tog_view *);
616 static const struct got_error *input_ref_view(struct tog_view **,
617 struct tog_view *, int);
618 static const struct got_error *close_ref_view(struct tog_view *);
619 static const struct got_error *search_start_ref_view(struct tog_view *);
620 static const struct got_error *search_next_ref_view(struct tog_view *);
622 static volatile sig_atomic_t tog_sigwinch_received;
623 static volatile sig_atomic_t tog_sigpipe_received;
624 static volatile sig_atomic_t tog_sigcont_received;
625 static volatile sig_atomic_t tog_sigint_received;
626 static volatile sig_atomic_t tog_sigterm_received;
629 tog_sigwinch(int signo)
631 tog_sigwinch_received = 1;
635 tog_sigpipe(int signo)
637 tog_sigpipe_received = 1;
641 tog_sigcont(int signo)
643 tog_sigcont_received = 1;
647 tog_sigint(int signo)
649 tog_sigint_received = 1;
653 tog_sigterm(int signo)
655 tog_sigterm_received = 1;
659 tog_fatal_signal_received(void)
661 return (tog_sigpipe_received ||
662 tog_sigint_received || tog_sigint_received);
665 static const struct got_error *
666 view_close(struct tog_view *view)
668 const struct got_error *err = NULL, *child_err = NULL;
671 child_err = view_close(view->child);
675 err = view->close(view);
677 del_panel(view->panel);
679 delwin(view->window);
681 return err ? err : child_err;
684 static struct tog_view *
685 view_open(int nlines, int ncols, int begin_y, int begin_x,
686 enum tog_view_type type)
688 struct tog_view *view = calloc(1, sizeof(*view));
696 view->nlines = nlines ? nlines : LINES - begin_y;
697 view->ncols = ncols ? ncols : COLS - begin_x;
698 view->begin_y = begin_y;
699 view->begin_x = begin_x;
700 view->window = newwin(nlines, ncols, begin_y, begin_x);
701 if (view->window == NULL) {
705 view->panel = new_panel(view->window);
706 if (view->panel == NULL ||
707 set_panel_userptr(view->panel, view) != OK) {
712 keypad(view->window, TRUE);
717 view_split_begin_x(int begin_x)
719 if (begin_x > 0 || COLS < 120)
721 return (COLS - MAX(COLS / 2, 80));
724 /* XXX Stub till we decide what to do. */
726 view_split_begin_y(int lines)
728 return lines * HSPLIT_SCALE;
731 static const struct got_error *view_resize(struct tog_view *);
733 static const struct got_error *
734 view_splitscreen(struct tog_view *view)
736 const struct got_error *err = NULL;
738 if (!view->resize && view->mode == TOG_VIEW_SPLIT_HRZN) {
739 if (view->resized_y && view->resized_y < view->lines)
740 view->begin_y = view->resized_y;
742 view->begin_y = view_split_begin_y(view->nlines);
744 } else if (!view->resize) {
745 if (view->resized_x && view->resized_x < view->cols - 1 &&
747 view->begin_x = view->resized_x;
749 view->begin_x = view_split_begin_x(0);
752 view->nlines = LINES - view->begin_y;
753 view->ncols = COLS - view->begin_x;
756 err = view_resize(view);
760 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
761 view->parent->nlines = view->begin_y;
763 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
764 return got_error_from_errno("mvwin");
769 static const struct got_error *
770 view_fullscreen(struct tog_view *view)
772 const struct got_error *err = NULL;
775 view->begin_y = view->resize ? view->begin_y : 0;
776 view->nlines = view->resize ? view->nlines : LINES;
780 err = view_resize(view);
784 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
785 return got_error_from_errno("mvwin");
791 view_is_parent_view(struct tog_view *view)
793 return view->parent == NULL;
797 view_is_splitscreen(struct tog_view *view)
799 return view->begin_x > 0 || view->begin_y > 0;
803 view_is_fullscreen(struct tog_view *view)
805 return view->nlines == LINES && view->ncols == COLS;
809 view_is_hsplit_top(struct tog_view *view)
811 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
812 view_is_splitscreen(view->child);
816 view_border(struct tog_view *view)
819 const struct tog_view *view_above;
822 return view_border(view->parent);
824 panel = panel_above(view->panel);
828 view_above = panel_userptr(panel);
829 if (view->mode == TOG_VIEW_SPLIT_HRZN)
830 mvwhline(view->window, view_above->begin_y - 1,
831 view->begin_x, got_locale_is_utf8() ?
832 ACS_HLINE : '-', view->ncols);
834 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
835 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
838 static const struct got_error *view_init_hsplit(struct tog_view *, int);
839 static const struct got_error *request_log_commits(struct tog_view *);
840 static const struct got_error *offset_selection_down(struct tog_view *);
841 static void offset_selection_up(struct tog_view *);
842 static void view_get_split(struct tog_view *, int *, int *);
844 static const struct got_error *
845 view_resize(struct tog_view *view)
847 const struct got_error *err = NULL;
848 int dif, nlines, ncols;
850 dif = LINES - view->lines; /* line difference */
852 if (view->lines > LINES)
853 nlines = view->nlines - (view->lines - LINES);
855 nlines = view->nlines + (LINES - view->lines);
856 if (view->cols > COLS)
857 ncols = view->ncols - (view->cols - COLS);
859 ncols = view->ncols + (COLS - view->cols);
862 int hs = view->child->begin_y;
864 if (!view_is_fullscreen(view))
865 view->child->begin_x = view_split_begin_x(view->begin_x);
866 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
867 view->child->begin_x == 0) {
870 view_fullscreen(view->child);
871 if (view->child->focussed)
872 show_panel(view->child->panel);
874 show_panel(view->panel);
876 ncols = view->child->begin_x;
878 view_splitscreen(view->child);
879 show_panel(view->child->panel);
882 * Request commits if terminal height was increased in a log
883 * view so we have enough commits loaded to populate the view.
885 if (view->type == TOG_VIEW_LOG && dif > 0) {
886 struct tog_log_view_state *ts = &view->state.log;
888 if (ts->commits.ncommits < ts->selected_entry->idx +
889 view->lines - ts->selected) {
890 view->nscrolled = ts->selected_entry->idx +
891 view->lines - ts->selected -
892 ts->commits.ncommits + dif;
893 err = request_log_commits(view);
900 * XXX This is ugly and needs to be moved into the above
901 * logic but "works" for now and my attempts at moving it
902 * break either 'tab' or 'F' key maps in horizontal splits.
905 err = view_splitscreen(view->child);
908 if (dif < 0) { /* top split decreased */
909 err = offset_selection_down(view);
916 show_panel(view->child->panel);
917 nlines = view->nlines;
919 } else if (view->parent == NULL)
922 if (wresize(view->window, nlines, ncols) == ERR)
923 return got_error_from_errno("wresize");
924 if (replace_panel(view->panel, view->window) == ERR)
925 return got_error_from_errno("replace_panel");
926 wclear(view->window);
928 view->nlines = nlines;
936 static const struct got_error *
937 view_resize_split(struct tog_view *view, int resize)
939 const struct got_error *err = NULL;
940 struct tog_view *v = NULL;
947 if (!v->child || !view_is_splitscreen(v->child))
950 v->resize = v->child->resize = resize; /* lock for resize event */
952 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
953 if (v->child->resized_y)
954 v->child->begin_y = v->child->resized_y;
956 v->child->begin_y -= resize;
958 v->child->begin_y += resize;
959 if (v->child->begin_y < 3) {
961 v->child->begin_y = 3;
962 } else if (v->child->begin_y > LINES - 1) {
964 v->child->begin_y = LINES - 1;
967 v->child->ncols = COLS;
968 err = view_init_hsplit(v, v->child->begin_y);
971 v->child->resized_y = v->child->begin_y;
973 if (v->child->resized_x)
974 v->child->begin_x = v->child->resized_x;
976 v->child->begin_x -= resize;
978 v->child->begin_x += resize;
979 if (v->child->begin_x < 11) {
981 v->child->begin_x = 11;
982 } else if (v->child->begin_x > COLS - 1) {
984 v->child->begin_x = COLS - 1;
986 v->child->resized_x = v->child->begin_x;
989 v->child->mode = v->mode;
990 v->child->nlines = v->lines - v->child->begin_y;
991 v->child->ncols = v->cols - v->child->begin_x;
994 err = view_fullscreen(v);
997 err = view_splitscreen(v->child);
1001 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1002 err = offset_selection_down(v->child);
1007 if (v->type == TOG_VIEW_LOG)
1008 err = request_log_commits(v);
1009 else if (v->child->type == TOG_VIEW_LOG)
1010 err = request_log_commits(v->child);
1012 v->resize = v->child->resize = 0;
1018 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1020 struct tog_view *v = src->child ? src->child : src;
1022 dst->resized_x = v->resized_x;
1023 dst->resized_y = v->resized_y;
1026 static const struct got_error *
1027 view_close_child(struct tog_view *view)
1029 const struct got_error *err = NULL;
1031 if (view->child == NULL)
1034 err = view_close(view->child);
1039 static const struct got_error *
1040 view_set_child(struct tog_view *view, struct tog_view *child)
1042 const struct got_error *err = NULL;
1044 view->child = child;
1045 child->parent = view;
1047 err = view_resize(view);
1051 if (view->child->resized_x || view->child->resized_y)
1052 err = view_resize_split(view, 0);
1058 tog_resizeterm(void)
1061 struct winsize size;
1063 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1064 cols = 80; /* Default */
1068 lines = size.ws_row;
1070 resize_term(lines, cols);
1073 static const struct got_error *
1074 view_search_start(struct tog_view *view)
1076 const struct got_error *err = NULL;
1077 struct tog_view *v = view;
1081 if (view->search_started) {
1082 regfree(&view->regex);
1083 view->searching = 0;
1084 memset(&view->regmatch, 0, sizeof(view->regmatch));
1086 view->search_started = 0;
1088 if (view->nlines < 1)
1091 if (view_is_hsplit_top(view))
1094 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1095 wclrtoeol(v->window);
1097 nodelay(view->window, FALSE); /* block for search term input */
1100 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1101 wrefresh(v->window);
1104 nodelay(view->window, TRUE);
1108 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1109 err = view->search_start(view);
1111 regfree(&view->regex);
1114 view->search_started = 1;
1115 view->searching = TOG_SEARCH_FORWARD;
1116 view->search_next_done = 0;
1117 view->search_next(view);
1123 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1124 static const struct got_error *
1125 switch_split(struct tog_view *view)
1127 const struct got_error *err = NULL;
1128 struct tog_view *v = NULL;
1135 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1136 v->mode = TOG_VIEW_SPLIT_VERT;
1138 v->mode = TOG_VIEW_SPLIT_HRZN;
1142 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1143 v->mode = TOG_VIEW_SPLIT_NONE;
1145 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1146 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1147 v->child->begin_y = v->child->resized_y;
1148 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1149 v->child->begin_x = v->child->resized_x;
1152 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1154 v->child->ncols = COLS;
1155 v->child->nscrolled = LINES - v->child->nlines;
1157 err = view_init_hsplit(v, v->child->begin_y);
1161 v->child->mode = v->mode;
1162 v->child->nlines = v->lines - v->child->begin_y;
1165 err = view_fullscreen(v);
1168 err = view_splitscreen(v->child);
1172 if (v->mode == TOG_VIEW_SPLIT_NONE)
1173 v->mode = TOG_VIEW_SPLIT_VERT;
1174 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1175 err = offset_selection_down(v);
1176 err = offset_selection_down(v->child);
1178 offset_selection_up(v);
1179 offset_selection_up(v->child);
1181 if (v->type == TOG_VIEW_LOG)
1182 err = request_log_commits(v);
1183 else if (v->child->type == TOG_VIEW_LOG)
1184 err = request_log_commits(v->child);
1190 * Compute view->count from numeric input. Assign total to view->count and
1191 * return first non-numeric key entered.
1194 get_compound_key(struct tog_view *view, int c)
1196 struct tog_view *v = view;
1199 if (view_is_hsplit_top(view))
1201 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1205 cbreak(); /* block for input */
1206 wmove(v->window, v->nlines - 1, 0);
1207 wclrtoeol(v->window);
1208 waddch(v->window, ':');
1211 x = getcurx(v->window);
1212 if (x != ERR && x < view->ncols) {
1213 waddch(v->window, c);
1214 wrefresh(v->window);
1218 * Don't overflow. Max valid request should be the greatest
1219 * between the longest and total lines; cap at 10 million.
1224 n = n * 10 + (c - '0');
1225 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1227 /* Massage excessive or inapplicable values at the input handler. */
1233 static const struct got_error *
1234 view_input(struct tog_view **new, int *done, struct tog_view *view,
1235 struct tog_view_list_head *views)
1237 const struct got_error *err = NULL;
1243 /* Clear "no matches" indicator. */
1244 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1245 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1246 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1250 if (view->searching && !view->search_next_done) {
1251 errcode = pthread_mutex_unlock(&tog_mutex);
1253 return got_error_set_errno(errcode,
1254 "pthread_mutex_unlock");
1256 errcode = pthread_mutex_lock(&tog_mutex);
1258 return got_error_set_errno(errcode,
1259 "pthread_mutex_lock");
1260 view->search_next(view);
1264 nodelay(view->window, FALSE);
1265 /* Allow threads to make progress while we are waiting for input. */
1266 errcode = pthread_mutex_unlock(&tog_mutex);
1268 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1269 /* If we have an unfinished count, let C-g or backspace abort. */
1270 if (view->count && --view->count) {
1272 nodelay(view->window, TRUE);
1273 ch = wgetch(view->window);
1274 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1279 ch = wgetch(view->window);
1280 if (ch >= '1' && ch <= '9')
1281 view->ch = ch = get_compound_key(view, ch);
1283 errcode = pthread_mutex_lock(&tog_mutex);
1285 return got_error_set_errno(errcode, "pthread_mutex_lock");
1286 nodelay(view->window, TRUE);
1288 if (tog_sigwinch_received || tog_sigcont_received) {
1290 tog_sigwinch_received = 0;
1291 tog_sigcont_received = 0;
1292 TAILQ_FOREACH(v, views, entry) {
1293 err = view_resize(v);
1296 err = v->input(new, v, KEY_RESIZE);
1300 err = view_resize(v->child);
1303 err = v->child->input(new, v->child,
1307 if (v->child->resized_x || v->child->resized_y) {
1308 err = view_resize_split(v, 0);
1321 view->child->focussed = 1;
1322 view->focus_child = 1;
1323 } else if (view->parent) {
1325 view->parent->focussed = 1;
1326 view->parent->focus_child = 0;
1327 if (!view_is_splitscreen(view)) {
1328 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1329 view->parent->type == TOG_VIEW_LOG) {
1330 err = request_log_commits(view->parent);
1334 offset_selection_up(view->parent);
1335 err = view_fullscreen(view->parent);
1342 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1343 if (view->parent->type == TOG_VIEW_LOG) {
1344 /* might need more commits to fill fullscreen */
1345 err = request_log_commits(view->parent);
1349 offset_selection_up(view->parent);
1351 err = view->input(new, view, ch);
1359 if (view_is_parent_view(view)) {
1360 if (view->child == NULL)
1362 if (view_is_splitscreen(view->child)) {
1364 view->child->focussed = 1;
1365 err = view_fullscreen(view->child);
1367 err = view_splitscreen(view->child);
1369 err = view_resize_split(view, 0);
1373 err = view->child->input(new, view->child,
1376 if (view_is_splitscreen(view)) {
1377 view->parent->focussed = 0;
1379 err = view_fullscreen(view);
1381 err = view_splitscreen(view);
1382 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1383 err = view_resize(view->parent);
1385 err = view_resize_split(view, 0);
1389 err = view->input(new, view, KEY_RESIZE);
1393 if (view->type == TOG_VIEW_LOG) {
1394 err = request_log_commits(view);
1399 err = offset_selection_down(view->parent);
1401 err = offset_selection_down(view);
1405 err = switch_split(view);
1408 err = view_resize_split(view, -1);
1411 err = view_resize_split(view, 1);
1417 if (view->search_start)
1418 view_search_start(view);
1420 err = view->input(new, view, ch);
1424 if (view->search_started && view->search_next) {
1425 view->searching = (ch == 'n' ?
1426 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1427 view->search_next_done = 0;
1428 view->search_next(view);
1430 err = view->input(new, view, ch);
1433 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1434 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1436 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1437 TAILQ_FOREACH(v, views, entry) {
1443 if (v->child && v->child->reset) {
1444 err = v->child->reset(v->child);
1451 err = view->input(new, view, ch);
1459 view_needs_focus_indication(struct tog_view *view)
1461 if (view_is_parent_view(view)) {
1462 if (view->child == NULL || view->child->focussed)
1464 if (!view_is_splitscreen(view->child))
1466 } else if (!view_is_splitscreen(view))
1469 return view->focussed;
1472 static const struct got_error *
1473 view_loop(struct tog_view *view)
1475 const struct got_error *err = NULL;
1476 struct tog_view_list_head views;
1477 struct tog_view *new_view;
1479 int fast_refresh = 10;
1480 int done = 0, errcode;
1482 mode = getenv("TOG_VIEW_SPLIT_MODE");
1483 if (!mode || !(*mode == 'h' || *mode == 'H'))
1484 view->mode = TOG_VIEW_SPLIT_VERT;
1486 view->mode = TOG_VIEW_SPLIT_HRZN;
1488 errcode = pthread_mutex_lock(&tog_mutex);
1490 return got_error_set_errno(errcode, "pthread_mutex_lock");
1493 TAILQ_INSERT_HEAD(&views, view, entry);
1496 err = view->show(view);
1501 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1502 !tog_fatal_signal_received()) {
1503 /* Refresh fast during initialization, then become slower. */
1504 if (fast_refresh && fast_refresh-- == 0)
1505 halfdelay(10); /* switch to once per second */
1507 err = view_input(&new_view, &done, view, &views);
1511 struct tog_view *v, *prev = NULL;
1513 if (view_is_parent_view(view))
1514 prev = TAILQ_PREV(view, tog_view_list_head,
1516 else if (view->parent)
1517 prev = view->parent;
1520 view->parent->child = NULL;
1521 view->parent->focus_child = 0;
1522 /* Restore fullscreen line height. */
1523 view->parent->nlines = view->parent->lines;
1524 err = view_resize(view->parent);
1527 /* Make resized splits persist. */
1528 view_transfer_size(view->parent, view);
1530 TAILQ_REMOVE(&views, view, entry);
1532 err = view_close(view);
1537 TAILQ_FOREACH(v, &views, entry) {
1541 if (view == NULL && new_view == NULL) {
1542 /* No view has focus. Try to pick one. */
1545 else if (!TAILQ_EMPTY(&views)) {
1546 view = TAILQ_LAST(&views,
1547 tog_view_list_head);
1550 if (view->focus_child) {
1551 view->child->focussed = 1;
1559 struct tog_view *v, *t;
1560 /* Only allow one parent view per type. */
1561 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1562 if (v->type != new_view->type)
1564 TAILQ_REMOVE(&views, v, entry);
1565 err = view_close(v);
1570 TAILQ_INSERT_TAIL(&views, new_view, entry);
1574 if (view_is_parent_view(view)) {
1575 if (view->child && view->child->focussed)
1578 if (view->parent && view->parent->focussed)
1579 view = view->parent;
1581 show_panel(view->panel);
1582 if (view->child && view_is_splitscreen(view->child))
1583 show_panel(view->child->panel);
1584 if (view->parent && view_is_splitscreen(view)) {
1585 err = view->parent->show(view->parent);
1589 err = view->show(view);
1593 err = view->child->show(view->child);
1602 while (!TAILQ_EMPTY(&views)) {
1603 const struct got_error *close_err;
1604 view = TAILQ_FIRST(&views);
1605 TAILQ_REMOVE(&views, view, entry);
1606 close_err = view_close(view);
1607 if (close_err && err == NULL)
1611 errcode = pthread_mutex_unlock(&tog_mutex);
1612 if (errcode && err == NULL)
1613 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1623 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1628 /* Create newly allocated wide-character string equivalent to a byte string. */
1629 static const struct got_error *
1630 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1633 const struct got_error *err = NULL;
1636 *wlen = mbstowcs(NULL, s, 0);
1637 if (*wlen == (size_t)-1) {
1639 if (errno != EILSEQ)
1640 return got_error_from_errno("mbstowcs");
1642 /* byte string invalid in current encoding; try to "fix" it */
1643 err = got_mbsavis(&vis, &vislen, s);
1646 *wlen = mbstowcs(NULL, vis, 0);
1647 if (*wlen == (size_t)-1) {
1648 err = got_error_from_errno("mbstowcs"); /* give up */
1653 *ws = calloc(*wlen + 1, sizeof(**ws));
1655 err = got_error_from_errno("calloc");
1659 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1660 err = got_error_from_errno("mbstowcs");
1671 static const struct got_error *
1672 expand_tab(char **ptr, const char *src)
1675 size_t len, n, idx = 0, sz = 0;
1678 n = len = strlen(src);
1679 dst = malloc(n + 1);
1681 return got_error_from_errno("malloc");
1683 while (idx < len && src[idx]) {
1684 const char c = src[idx];
1687 size_t nb = TABSIZE - sz % TABSIZE;
1690 p = realloc(dst, n + nb);
1693 return got_error_from_errno("realloc");
1698 memset(dst + sz, ' ', nb);
1701 dst[sz++] = src[idx];
1711 * Advance at most n columns from wline starting at offset off.
1712 * Return the index to the first character after the span operation.
1713 * Return the combined column width of all spanned wide character in
1717 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1719 int width, i, cols = 0;
1726 for (i = off; wline[i] != L'\0'; ++i) {
1727 if (wline[i] == L'\t')
1728 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1730 width = wcwidth(wline[i]);
1737 if (cols + width > n)
1747 * Format a line for display, ensuring that it won't overflow a width limit.
1748 * With scrolling, the width returned refers to the scrolled version of the
1749 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1751 static const struct got_error *
1752 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1753 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1755 const struct got_error *err = NULL;
1757 wchar_t *wline = NULL;
1766 err = expand_tab(&exstr, line);
1771 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1776 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1778 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1779 wline[wlen - 1] = L'\0';
1782 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1783 wline[wlen - 1] = L'\0';
1787 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1793 *scrollxp = scrollx;
1801 static const struct got_error*
1802 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1803 struct got_object_id *id, struct got_repository *repo)
1805 static const struct got_error *err = NULL;
1806 struct got_reflist_entry *re;
1812 TAILQ_FOREACH(re, refs, entry) {
1813 struct got_tag_object *tag = NULL;
1814 struct got_object_id *ref_id;
1817 name = got_ref_get_name(re->ref);
1818 if (strcmp(name, GOT_REF_HEAD) == 0)
1820 if (strncmp(name, "refs/", 5) == 0)
1822 if (strncmp(name, "got/", 4) == 0 &&
1823 strncmp(name, "got/backup/", 11) != 0)
1825 if (strncmp(name, "heads/", 6) == 0)
1827 if (strncmp(name, "remotes/", 8) == 0) {
1829 s = strstr(name, "/" GOT_REF_HEAD);
1830 if (s != NULL && s[strlen(s)] == '\0')
1833 err = got_ref_resolve(&ref_id, repo, re->ref);
1836 if (strncmp(name, "tags/", 5) == 0) {
1837 err = got_object_open_as_tag(&tag, repo, ref_id);
1839 if (err->code != GOT_ERR_OBJ_TYPE) {
1843 /* Ref points at something other than a tag. */
1848 cmp = got_object_id_cmp(tag ?
1849 got_object_tag_get_object_id(tag) : ref_id, id);
1852 got_object_tag_close(tag);
1856 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1857 s ? ", " : "", name) == -1) {
1858 err = got_error_from_errno("asprintf");
1869 static const struct got_error *
1870 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1875 smallerthan = strchr(author, '<');
1876 if (smallerthan && smallerthan[1] != '\0')
1877 author = smallerthan + 1;
1878 author[strcspn(author, "@>")] = '\0';
1879 return format_line(wauthor, author_width, NULL, author, 0, limit,
1883 static const struct got_error *
1884 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1885 struct got_object_id *id, const size_t date_display_cols,
1886 int author_display_cols)
1888 struct tog_log_view_state *s = &view->state.log;
1889 const struct got_error *err = NULL;
1890 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1891 char *logmsg0 = NULL, *logmsg = NULL;
1892 char *author = NULL;
1893 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1894 int author_width, logmsg_width;
1895 char *newline, *line = NULL;
1896 int col, limit, scrollx;
1897 const int avail = view->ncols;
1899 time_t committer_time;
1900 struct tog_color *tc;
1902 committer_time = got_object_commit_get_committer_time(commit);
1903 if (gmtime_r(&committer_time, &tm) == NULL)
1904 return got_error_from_errno("gmtime_r");
1905 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1906 return got_error(GOT_ERR_NO_SPACE);
1908 if (avail <= date_display_cols)
1909 limit = MIN(sizeof(datebuf) - 1, avail);
1911 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1912 tc = get_color(&s->colors, TOG_COLOR_DATE);
1914 wattr_on(view->window,
1915 COLOR_PAIR(tc->colorpair), NULL);
1916 waddnstr(view->window, datebuf, limit);
1918 wattr_off(view->window,
1919 COLOR_PAIR(tc->colorpair), NULL);
1926 err = got_object_id_str(&id_str, id);
1929 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1931 wattr_on(view->window,
1932 COLOR_PAIR(tc->colorpair), NULL);
1933 wprintw(view->window, "%.8s ", id_str);
1935 wattr_off(view->window,
1936 COLOR_PAIR(tc->colorpair), NULL);
1943 author = strdup(got_object_commit_get_author(commit));
1944 if (author == NULL) {
1945 err = got_error_from_errno("strdup");
1948 err = format_author(&wauthor, &author_width, author, avail - col, col);
1951 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1953 wattr_on(view->window,
1954 COLOR_PAIR(tc->colorpair), NULL);
1955 waddwstr(view->window, wauthor);
1957 wattr_off(view->window,
1958 COLOR_PAIR(tc->colorpair), NULL);
1959 col += author_width;
1960 while (col < avail && author_width < author_display_cols + 2) {
1961 waddch(view->window, ' ');
1968 err = got_object_commit_get_logmsg(&logmsg0, commit);
1972 while (*logmsg == '\n')
1974 newline = strchr(logmsg, '\n');
1977 limit = avail - col;
1978 if (view->child && !view_is_hsplit_top(view) && limit > 0)
1979 limit--; /* for the border */
1980 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1984 waddwstr(view->window, &wlogmsg[scrollx]);
1985 col += MAX(logmsg_width, 0);
1986 while (col < avail) {
1987 waddch(view->window, ' ');
1999 static struct commit_queue_entry *
2000 alloc_commit_queue_entry(struct got_commit_object *commit,
2001 struct got_object_id *id)
2003 struct commit_queue_entry *entry;
2005 entry = calloc(1, sizeof(*entry));
2010 entry->commit = commit;
2015 pop_commit(struct commit_queue *commits)
2017 struct commit_queue_entry *entry;
2019 entry = TAILQ_FIRST(&commits->head);
2020 TAILQ_REMOVE(&commits->head, entry, entry);
2021 got_object_commit_close(entry->commit);
2022 commits->ncommits--;
2023 /* Don't free entry->id! It is owned by the commit graph. */
2028 free_commits(struct commit_queue *commits)
2030 while (!TAILQ_EMPTY(&commits->head))
2031 pop_commit(commits);
2034 static const struct got_error *
2035 match_commit(int *have_match, struct got_object_id *id,
2036 struct got_commit_object *commit, regex_t *regex)
2038 const struct got_error *err = NULL;
2039 regmatch_t regmatch;
2040 char *id_str = NULL, *logmsg = NULL;
2044 err = got_object_id_str(&id_str, id);
2048 err = got_object_commit_get_logmsg(&logmsg, commit);
2052 if (regexec(regex, got_object_commit_get_author(commit), 1,
2053 ®match, 0) == 0 ||
2054 regexec(regex, got_object_commit_get_committer(commit), 1,
2055 ®match, 0) == 0 ||
2056 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2057 regexec(regex, logmsg, 1, ®match, 0) == 0)
2065 static const struct got_error *
2066 queue_commits(struct tog_log_thread_args *a)
2068 const struct got_error *err = NULL;
2071 * We keep all commits open throughout the lifetime of the log
2072 * view in order to avoid having to re-fetch commits from disk
2073 * while updating the display.
2076 struct got_object_id *id;
2077 struct got_commit_object *commit;
2078 struct commit_queue_entry *entry;
2081 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2083 if (err || id == NULL)
2086 err = got_object_open_as_commit(&commit, a->repo, id);
2089 entry = alloc_commit_queue_entry(commit, id);
2090 if (entry == NULL) {
2091 err = got_error_from_errno("alloc_commit_queue_entry");
2095 errcode = pthread_mutex_lock(&tog_mutex);
2097 err = got_error_set_errno(errcode,
2098 "pthread_mutex_lock");
2102 entry->idx = a->commits->ncommits;
2103 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2104 a->commits->ncommits++;
2106 if (*a->searching == TOG_SEARCH_FORWARD &&
2107 !*a->search_next_done) {
2109 err = match_commit(&have_match, id, commit, a->regex);
2113 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2116 errcode = pthread_mutex_unlock(&tog_mutex);
2117 if (errcode && err == NULL)
2118 err = got_error_set_errno(errcode,
2119 "pthread_mutex_unlock");
2122 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2128 select_commit(struct tog_log_view_state *s)
2130 struct commit_queue_entry *entry;
2133 entry = s->first_displayed_entry;
2135 if (ncommits == s->selected) {
2136 s->selected_entry = entry;
2139 entry = TAILQ_NEXT(entry, entry);
2144 static const struct got_error *
2145 draw_commits(struct tog_view *view)
2147 const struct got_error *err = NULL;
2148 struct tog_log_view_state *s = &view->state.log;
2149 struct commit_queue_entry *entry = s->selected_entry;
2150 const int limit = view->nlines;
2152 int ncommits, author_cols = 4;
2153 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2154 char *refs_str = NULL;
2156 struct tog_color *tc;
2157 static const size_t date_display_cols = 12;
2159 if (s->selected_entry &&
2160 !(view->searching && view->search_next_done == 0)) {
2161 struct got_reflist_head *refs;
2162 err = got_object_id_str(&id_str, s->selected_entry->id);
2165 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2166 s->selected_entry->id);
2168 err = build_refs_str(&refs_str, refs,
2169 s->selected_entry->id, s->repo);
2175 if (s->thread_args.commits_needed == 0)
2176 halfdelay(10); /* disable fast refresh */
2178 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2179 if (asprintf(&ncommits_str, " [%d/%d] %s",
2180 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2181 (view->searching && !view->search_next_done) ?
2182 "searching..." : "loading...") == -1) {
2183 err = got_error_from_errno("asprintf");
2187 const char *search_str = NULL;
2189 if (view->searching) {
2190 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2191 search_str = "no more matches";
2192 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2193 search_str = "no matches found";
2194 else if (!view->search_next_done)
2195 search_str = "searching...";
2198 if (asprintf(&ncommits_str, " [%d/%d] %s",
2199 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2200 search_str ? search_str :
2201 (refs_str ? refs_str : "")) == -1) {
2202 err = got_error_from_errno("asprintf");
2207 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2208 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2209 "........................................",
2210 s->in_repo_path, ncommits_str) == -1) {
2211 err = got_error_from_errno("asprintf");
2215 } else if (asprintf(&header, "commit %s%s",
2216 id_str ? id_str : "........................................",
2217 ncommits_str) == -1) {
2218 err = got_error_from_errno("asprintf");
2222 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2226 werase(view->window);
2228 if (view_needs_focus_indication(view))
2229 wstandout(view->window);
2230 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2232 wattr_on(view->window,
2233 COLOR_PAIR(tc->colorpair), NULL);
2234 waddwstr(view->window, wline);
2236 wattr_off(view->window,
2237 COLOR_PAIR(tc->colorpair), NULL);
2238 while (width < view->ncols) {
2239 waddch(view->window, ' ');
2242 if (view_needs_focus_indication(view))
2243 wstandend(view->window);
2248 /* Grow author column size if necessary, and set view->maxx. */
2249 entry = s->first_displayed_entry;
2253 char *author, *eol, *msg, *msg0;
2254 wchar_t *wauthor, *wmsg;
2256 if (ncommits >= limit - 1)
2258 author = strdup(got_object_commit_get_author(entry->commit));
2259 if (author == NULL) {
2260 err = got_error_from_errno("strdup");
2263 err = format_author(&wauthor, &width, author, COLS,
2265 if (author_cols < width)
2266 author_cols = width;
2269 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2273 while (*msg == '\n')
2275 if ((eol = strchr(msg, '\n')))
2277 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2278 date_display_cols + author_cols, 0);
2281 view->maxx = MAX(view->maxx, width);
2285 entry = TAILQ_NEXT(entry, entry);
2288 entry = s->first_displayed_entry;
2289 s->last_displayed_entry = s->first_displayed_entry;
2292 if (ncommits >= limit - 1)
2294 if (ncommits == s->selected)
2295 wstandout(view->window);
2296 err = draw_commit(view, entry->commit, entry->id,
2297 date_display_cols, author_cols);
2298 if (ncommits == s->selected)
2299 wstandend(view->window);
2303 s->last_displayed_entry = entry;
2304 entry = TAILQ_NEXT(entry, entry);
2317 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2319 struct commit_queue_entry *entry;
2322 entry = TAILQ_FIRST(&s->commits.head);
2323 if (s->first_displayed_entry == entry)
2326 entry = s->first_displayed_entry;
2327 while (entry && nscrolled < maxscroll) {
2328 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2330 s->first_displayed_entry = entry;
2336 static const struct got_error *
2337 trigger_log_thread(struct tog_view *view, int wait)
2339 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2342 halfdelay(1); /* fast refresh while loading commits */
2344 while (!ta->log_complete && !tog_thread_error &&
2345 (ta->commits_needed > 0 || ta->load_all)) {
2346 /* Wake the log thread. */
2347 errcode = pthread_cond_signal(&ta->need_commits);
2349 return got_error_set_errno(errcode,
2350 "pthread_cond_signal");
2353 * The mutex will be released while the view loop waits
2354 * in wgetch(), at which time the log thread will run.
2359 /* Display progress update in log view. */
2360 show_log_view(view);
2364 /* Wait right here while next commit is being loaded. */
2365 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2367 return got_error_set_errno(errcode,
2368 "pthread_cond_wait");
2370 /* Display progress update in log view. */
2371 show_log_view(view);
2379 static const struct got_error *
2380 request_log_commits(struct tog_view *view)
2382 struct tog_log_view_state *state = &view->state.log;
2383 const struct got_error *err = NULL;
2385 state->thread_args.commits_needed = view->nscrolled;
2386 err = trigger_log_thread(view, 1);
2387 view->nscrolled = 0;
2392 static const struct got_error *
2393 log_scroll_down(struct tog_view *view, int maxscroll)
2395 struct tog_log_view_state *s = &view->state.log;
2396 const struct got_error *err = NULL;
2397 struct commit_queue_entry *pentry;
2398 int nscrolled = 0, ncommits_needed;
2400 if (s->last_displayed_entry == NULL)
2403 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2404 if (s->commits.ncommits < ncommits_needed &&
2405 !s->thread_args.log_complete) {
2407 * Ask the log thread for required amount of commits.
2409 s->thread_args.commits_needed += maxscroll;
2410 err = trigger_log_thread(view, 1);
2416 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2417 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2420 s->last_displayed_entry = pentry ?
2421 pentry : s->last_displayed_entry;;
2423 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2426 s->first_displayed_entry = pentry;
2427 } while (++nscrolled < maxscroll);
2429 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2430 view->nscrolled += nscrolled;
2432 view->nscrolled = 0;
2437 static const struct got_error *
2438 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2439 struct got_commit_object *commit, struct got_object_id *commit_id,
2440 struct tog_view *log_view, struct got_repository *repo)
2442 const struct got_error *err;
2443 struct got_object_qid *parent_id;
2444 struct tog_view *diff_view;
2446 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2447 if (diff_view == NULL)
2448 return got_error_from_errno("view_open");
2450 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2451 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2452 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2454 *new_view = diff_view;
2458 static const struct got_error *
2459 tree_view_visit_subtree(struct tog_tree_view_state *s,
2460 struct got_tree_object *subtree)
2462 struct tog_parent_tree *parent;
2464 parent = calloc(1, sizeof(*parent));
2466 return got_error_from_errno("calloc");
2468 parent->tree = s->tree;
2469 parent->first_displayed_entry = s->first_displayed_entry;
2470 parent->selected_entry = s->selected_entry;
2471 parent->selected = s->selected;
2472 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2475 s->first_displayed_entry = NULL;
2479 static const struct got_error *
2480 tree_view_walk_path(struct tog_tree_view_state *s,
2481 struct got_commit_object *commit, const char *path)
2483 const struct got_error *err = NULL;
2484 struct got_tree_object *tree = NULL;
2486 char *slash, *subpath = NULL;
2488 /* Walk the path and open corresponding tree objects. */
2491 struct got_tree_entry *te;
2492 struct got_object_id *tree_id;
2498 /* Ensure the correct subtree entry is selected. */
2499 slash = strchr(p, '/');
2501 te_name = strdup(p);
2503 te_name = strndup(p, slash - p);
2504 if (te_name == NULL) {
2505 err = got_error_from_errno("strndup");
2508 te = got_object_tree_find_entry(s->tree, te_name);
2510 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2515 s->first_displayed_entry = s->selected_entry = te;
2517 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2518 break; /* jump to this file's entry */
2520 slash = strchr(p, '/');
2522 subpath = strndup(path, slash - path);
2524 subpath = strdup(path);
2525 if (subpath == NULL) {
2526 err = got_error_from_errno("strdup");
2530 err = got_object_id_by_path(&tree_id, s->repo, commit,
2535 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2540 err = tree_view_visit_subtree(s, tree);
2542 got_object_tree_close(tree);
2556 static const struct got_error *
2557 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2558 struct commit_queue_entry *entry, const char *path,
2559 const char *head_ref_name, struct got_repository *repo)
2561 const struct got_error *err = NULL;
2562 struct tog_tree_view_state *s;
2563 struct tog_view *tree_view;
2565 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2566 if (tree_view == NULL)
2567 return got_error_from_errno("view_open");
2569 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2572 s = &tree_view->state.tree;
2574 *new_view = tree_view;
2576 if (got_path_is_root_dir(path))
2579 return tree_view_walk_path(s, entry->commit, path);
2582 static const struct got_error *
2583 block_signals_used_by_main_thread(void)
2588 if (sigemptyset(&sigset) == -1)
2589 return got_error_from_errno("sigemptyset");
2591 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2592 if (sigaddset(&sigset, SIGWINCH) == -1)
2593 return got_error_from_errno("sigaddset");
2594 if (sigaddset(&sigset, SIGCONT) == -1)
2595 return got_error_from_errno("sigaddset");
2596 if (sigaddset(&sigset, SIGINT) == -1)
2597 return got_error_from_errno("sigaddset");
2598 if (sigaddset(&sigset, SIGTERM) == -1)
2599 return got_error_from_errno("sigaddset");
2601 /* ncurses handles SIGTSTP */
2602 if (sigaddset(&sigset, SIGTSTP) == -1)
2603 return got_error_from_errno("sigaddset");
2605 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2607 return got_error_set_errno(errcode, "pthread_sigmask");
2613 log_thread(void *arg)
2615 const struct got_error *err = NULL;
2617 struct tog_log_thread_args *a = arg;
2621 * Sync startup with main thread such that we begin our
2622 * work once view_input() has released the mutex.
2624 errcode = pthread_mutex_lock(&tog_mutex);
2626 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2630 err = block_signals_used_by_main_thread();
2632 pthread_mutex_unlock(&tog_mutex);
2636 while (!done && !err && !tog_fatal_signal_received()) {
2637 errcode = pthread_mutex_unlock(&tog_mutex);
2639 err = got_error_set_errno(errcode,
2640 "pthread_mutex_unlock");
2643 err = queue_commits(a);
2645 if (err->code != GOT_ERR_ITER_COMPLETED)
2649 } else if (a->commits_needed > 0 && !a->load_all)
2650 a->commits_needed--;
2652 errcode = pthread_mutex_lock(&tog_mutex);
2654 err = got_error_set_errno(errcode,
2655 "pthread_mutex_lock");
2657 } else if (*a->quit)
2659 else if (*a->first_displayed_entry == NULL) {
2660 *a->first_displayed_entry =
2661 TAILQ_FIRST(&a->commits->head);
2662 *a->selected_entry = *a->first_displayed_entry;
2665 errcode = pthread_cond_signal(&a->commit_loaded);
2667 err = got_error_set_errno(errcode,
2668 "pthread_cond_signal");
2669 pthread_mutex_unlock(&tog_mutex);
2674 a->commits_needed = 0;
2676 if (a->commits_needed == 0 && !a->load_all) {
2677 errcode = pthread_cond_wait(&a->need_commits,
2680 err = got_error_set_errno(errcode,
2681 "pthread_cond_wait");
2682 pthread_mutex_unlock(&tog_mutex);
2690 a->log_complete = 1;
2691 errcode = pthread_mutex_unlock(&tog_mutex);
2693 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2696 tog_thread_error = 1;
2697 pthread_cond_signal(&a->commit_loaded);
2702 static const struct got_error *
2703 stop_log_thread(struct tog_log_view_state *s)
2705 const struct got_error *err = NULL, *thread_err = NULL;
2710 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2712 return got_error_set_errno(errcode,
2713 "pthread_cond_signal");
2714 errcode = pthread_mutex_unlock(&tog_mutex);
2716 return got_error_set_errno(errcode,
2717 "pthread_mutex_unlock");
2718 errcode = pthread_join(s->thread, (void **)&thread_err);
2720 return got_error_set_errno(errcode, "pthread_join");
2721 errcode = pthread_mutex_lock(&tog_mutex);
2723 return got_error_set_errno(errcode,
2724 "pthread_mutex_lock");
2725 s->thread = 0; //NULL;
2728 if (s->thread_args.repo) {
2729 err = got_repo_close(s->thread_args.repo);
2730 s->thread_args.repo = NULL;
2733 if (s->thread_args.pack_fds) {
2734 const struct got_error *pack_err =
2735 got_repo_pack_fds_close(s->thread_args.pack_fds);
2738 s->thread_args.pack_fds = NULL;
2741 if (s->thread_args.graph) {
2742 got_commit_graph_close(s->thread_args.graph);
2743 s->thread_args.graph = NULL;
2746 return err ? err : thread_err;
2749 static const struct got_error *
2750 close_log_view(struct tog_view *view)
2752 const struct got_error *err = NULL;
2753 struct tog_log_view_state *s = &view->state.log;
2756 err = stop_log_thread(s);
2758 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2759 if (errcode && err == NULL)
2760 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2762 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2763 if (errcode && err == NULL)
2764 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2766 free_commits(&s->commits);
2767 free(s->in_repo_path);
2768 s->in_repo_path = NULL;
2771 free(s->head_ref_name);
2772 s->head_ref_name = NULL;
2776 static const struct got_error *
2777 search_start_log_view(struct tog_view *view)
2779 struct tog_log_view_state *s = &view->state.log;
2781 s->matched_entry = NULL;
2782 s->search_entry = NULL;
2786 static const struct got_error *
2787 search_next_log_view(struct tog_view *view)
2789 const struct got_error *err = NULL;
2790 struct tog_log_view_state *s = &view->state.log;
2791 struct commit_queue_entry *entry;
2793 /* Display progress update in log view. */
2794 show_log_view(view);
2798 if (s->search_entry) {
2800 errcode = pthread_mutex_unlock(&tog_mutex);
2802 return got_error_set_errno(errcode,
2803 "pthread_mutex_unlock");
2804 ch = wgetch(view->window);
2805 errcode = pthread_mutex_lock(&tog_mutex);
2807 return got_error_set_errno(errcode,
2808 "pthread_mutex_lock");
2809 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2810 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2813 if (view->searching == TOG_SEARCH_FORWARD)
2814 entry = TAILQ_NEXT(s->search_entry, entry);
2816 entry = TAILQ_PREV(s->search_entry,
2817 commit_queue_head, entry);
2818 } else if (s->matched_entry) {
2819 int matched_idx = s->matched_entry->idx;
2820 int selected_idx = s->selected_entry->idx;
2823 * If the user has moved the cursor after we hit a match,
2824 * the position from where we should continue searching
2825 * might have changed.
2827 if (view->searching == TOG_SEARCH_FORWARD) {
2828 if (matched_idx > selected_idx)
2829 entry = TAILQ_NEXT(s->selected_entry, entry);
2831 entry = TAILQ_NEXT(s->matched_entry, entry);
2833 if (matched_idx < selected_idx)
2834 entry = TAILQ_PREV(s->selected_entry,
2835 commit_queue_head, entry);
2837 entry = TAILQ_PREV(s->matched_entry,
2838 commit_queue_head, entry);
2841 entry = s->selected_entry;
2847 if (entry == NULL) {
2848 if (s->thread_args.log_complete ||
2849 view->searching == TOG_SEARCH_BACKWARD) {
2850 view->search_next_done =
2851 (s->matched_entry == NULL ?
2852 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2853 s->search_entry = NULL;
2857 * Poke the log thread for more commits and return,
2858 * allowing the main loop to make progress. Search
2859 * will resume at s->search_entry once we come back.
2861 s->thread_args.commits_needed++;
2862 return trigger_log_thread(view, 0);
2865 err = match_commit(&have_match, entry->id, entry->commit,
2870 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2871 s->matched_entry = entry;
2875 s->search_entry = entry;
2876 if (view->searching == TOG_SEARCH_FORWARD)
2877 entry = TAILQ_NEXT(entry, entry);
2879 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2882 if (s->matched_entry) {
2883 int cur = s->selected_entry->idx;
2884 while (cur < s->matched_entry->idx) {
2885 err = input_log_view(NULL, view, KEY_DOWN);
2890 while (cur > s->matched_entry->idx) {
2891 err = input_log_view(NULL, view, KEY_UP);
2898 s->search_entry = NULL;
2903 static const struct got_error *
2904 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2905 struct got_repository *repo, const char *head_ref_name,
2906 const char *in_repo_path, int log_branches)
2908 const struct got_error *err = NULL;
2909 struct tog_log_view_state *s = &view->state.log;
2910 struct got_repository *thread_repo = NULL;
2911 struct got_commit_graph *thread_graph = NULL;
2914 if (in_repo_path != s->in_repo_path) {
2915 free(s->in_repo_path);
2916 s->in_repo_path = strdup(in_repo_path);
2917 if (s->in_repo_path == NULL)
2918 return got_error_from_errno("strdup");
2921 /* The commit queue only contains commits being displayed. */
2922 TAILQ_INIT(&s->commits.head);
2923 s->commits.ncommits = 0;
2926 if (head_ref_name) {
2927 s->head_ref_name = strdup(head_ref_name);
2928 if (s->head_ref_name == NULL) {
2929 err = got_error_from_errno("strdup");
2933 s->start_id = got_object_id_dup(start_id);
2934 if (s->start_id == NULL) {
2935 err = got_error_from_errno("got_object_id_dup");
2938 s->log_branches = log_branches;
2940 STAILQ_INIT(&s->colors);
2941 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2942 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2943 get_color_value("TOG_COLOR_COMMIT"));
2946 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2947 get_color_value("TOG_COLOR_AUTHOR"));
2949 free_colors(&s->colors);
2952 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2953 get_color_value("TOG_COLOR_DATE"));
2955 free_colors(&s->colors);
2960 view->show = show_log_view;
2961 view->input = input_log_view;
2962 view->close = close_log_view;
2963 view->search_start = search_start_log_view;
2964 view->search_next = search_next_log_view;
2966 if (s->thread_args.pack_fds == NULL) {
2967 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2971 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2972 s->thread_args.pack_fds);
2975 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2979 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2980 s->repo, NULL, NULL);
2984 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2986 err = got_error_set_errno(errcode, "pthread_cond_init");
2989 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2991 err = got_error_set_errno(errcode, "pthread_cond_init");
2995 s->thread_args.commits_needed = view->nlines;
2996 s->thread_args.graph = thread_graph;
2997 s->thread_args.commits = &s->commits;
2998 s->thread_args.in_repo_path = s->in_repo_path;
2999 s->thread_args.start_id = s->start_id;
3000 s->thread_args.repo = thread_repo;
3001 s->thread_args.log_complete = 0;
3002 s->thread_args.quit = &s->quit;
3003 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3004 s->thread_args.selected_entry = &s->selected_entry;
3005 s->thread_args.searching = &view->searching;
3006 s->thread_args.search_next_done = &view->search_next_done;
3007 s->thread_args.regex = &view->regex;
3010 close_log_view(view);
3014 static const struct got_error *
3015 show_log_view(struct tog_view *view)
3017 const struct got_error *err;
3018 struct tog_log_view_state *s = &view->state.log;
3020 if (s->thread == 0) { //NULL) {
3021 int errcode = pthread_create(&s->thread, NULL, log_thread,
3024 return got_error_set_errno(errcode, "pthread_create");
3025 if (s->thread_args.commits_needed > 0) {
3026 err = trigger_log_thread(view, 1);
3032 return draw_commits(view);
3036 log_move_cursor_up(struct tog_view *view, int page, int home)
3038 struct tog_log_view_state *s = &view->state.log;
3040 if (s->selected_entry->idx == 0)
3042 if (s->first_displayed_entry == NULL)
3045 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3047 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3049 if (!page && !home && s->selected > 0)
3052 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3058 static const struct got_error *
3059 log_move_cursor_down(struct tog_view *view, int page)
3061 struct tog_log_view_state *s = &view->state.log;
3062 struct commit_queue_entry *first;
3063 const struct got_error *err = NULL;
3065 first = s->first_displayed_entry;
3066 if (first == NULL) {
3071 if (s->thread_args.log_complete &&
3072 s->selected_entry->idx >= s->commits.ncommits - 1)
3076 int eos = view->nlines - 2;
3078 if (view_is_hsplit_top(view))
3079 --eos; /* border consumes the last line */
3080 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3083 err = log_scroll_down(view, 1);
3084 } else if (s->thread_args.load_all) {
3085 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3086 s->selected += MIN(s->last_displayed_entry->idx -
3087 s->selected_entry->idx, page + 1);
3089 err = log_scroll_down(view, MIN(page,
3090 s->commits.ncommits - s->selected_entry->idx - 1));
3091 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3093 err = log_scroll_down(view, page);
3096 if (first == s->first_displayed_entry && s->selected <
3097 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3098 s->selected = MIN(s->commits.ncommits - 1, page);
3105 * We might necessarily overshoot in horizontal
3106 * splits; if so, select the last displayed commit.
3108 s->selected = MIN(s->selected,
3109 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3113 if (s->thread_args.log_complete &&
3114 s->selected_entry->idx == s->commits.ncommits - 1)
3121 view_get_split(struct tog_view *view, int *y, int *x)
3126 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3127 if (view->child && view->child->resized_y)
3128 *y = view->child->resized_y;
3129 else if (view->resized_y)
3130 *y = view->resized_y;
3132 *y = view_split_begin_y(view->lines);
3133 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3134 if (view->child && view->child->resized_x)
3135 *x = view->child->resized_x;
3136 else if (view->resized_x)
3137 *x = view->resized_x;
3139 *x = view_split_begin_x(view->begin_x);
3143 /* Split view horizontally at y and offset view->state->selected line. */
3144 static const struct got_error *
3145 view_init_hsplit(struct tog_view *view, int y)
3147 const struct got_error *err = NULL;
3151 err = view_resize(view);
3155 err = offset_selection_down(view);
3160 static const struct got_error *
3161 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3163 const struct got_error *err = NULL;
3164 struct tog_log_view_state *s = &view->state.log;
3165 struct tog_view *diff_view = NULL, *tree_view = NULL;
3166 struct tog_view *ref_view = NULL;
3167 struct commit_queue_entry *entry;
3168 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3170 if (s->thread_args.load_all) {
3171 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3172 s->thread_args.load_all = 0;
3173 else if (s->thread_args.log_complete) {
3174 err = log_move_cursor_down(view, s->commits.ncommits);
3175 s->thread_args.load_all = 0;
3180 eos = nscroll = view->nlines - 1;
3181 if (view_is_hsplit_top(view))
3192 view->x = MAX(view->maxx - view->ncols / 2, 0);
3197 if (view->x + view->ncols / 2 < view->maxx)
3198 view->x += 2; /* move two columns right */
3204 view->x -= MIN(view->x, 2); /* move two columns back */
3213 log_move_cursor_up(view, 0, 0);
3217 log_move_cursor_up(view, 0, 1);
3227 log_move_cursor_up(view, nscroll, 0);
3234 err = log_move_cursor_down(view, 0);
3238 /* We don't know yet how many commits, so we're forced to
3239 * traverse them all. */
3241 if (!s->thread_args.log_complete) {
3242 s->thread_args.load_all = 1;
3243 return trigger_log_thread(view, 0);
3247 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3248 for (n = 0; n < eos; n++) {
3251 s->first_displayed_entry = entry;
3252 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3255 s->selected = n - 1;
3267 err = log_move_cursor_down(view, nscroll);
3270 if (s->selected > view->nlines - 2)
3271 s->selected = view->nlines - 2;
3272 if (s->selected > s->commits.ncommits - 1)
3273 s->selected = s->commits.ncommits - 1;
3275 if (s->commits.ncommits < view->nlines - 1 &&
3276 !s->thread_args.log_complete) {
3277 s->thread_args.commits_needed += (view->nlines - 1) -
3278 s->commits.ncommits;
3279 err = trigger_log_thread(view, 1);
3285 if (s->selected_entry == NULL)
3288 /* get dimensions--don't split till initialisation succeeds */
3289 if (view_is_parent_view(view))
3290 view_get_split(view, &begin_y, &begin_x);
3292 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3293 s->selected_entry->commit, s->selected_entry->id,
3298 if (view_is_parent_view(view) &&
3299 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3300 err = view_init_hsplit(view, begin_y);
3306 diff_view->focussed = 1;
3307 diff_view->mode = view->mode;
3308 diff_view->nlines = view->lines - begin_y;
3310 if (view_is_parent_view(view)) {
3311 view_transfer_size(diff_view, view);
3312 err = view_close_child(view);
3315 err = view_set_child(view, diff_view);
3318 view->focus_child = 1;
3320 *new_view = diff_view;
3324 if (s->selected_entry == NULL)
3326 if (view_is_parent_view(view))
3327 view_get_split(view, &begin_y, &begin_x);
3328 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3329 s->selected_entry, s->in_repo_path, s->head_ref_name,
3333 if (view_is_parent_view(view) &&
3334 view->mode == TOG_VIEW_SPLIT_HRZN) {
3335 err = view_init_hsplit(view, begin_y);
3340 tree_view->focussed = 1;
3341 tree_view->mode = view->mode;
3342 tree_view->nlines = view->lines - begin_y;
3343 if (view_is_parent_view(view)) {
3344 view_transfer_size(tree_view, view);
3345 err = view_close_child(view);
3348 err = view_set_child(view, tree_view);
3351 view->focus_child = 1;
3353 *new_view = tree_view;
3359 if (ch == KEY_BACKSPACE &&
3360 got_path_is_root_dir(s->in_repo_path))
3362 err = stop_log_thread(s);
3365 if (ch == KEY_BACKSPACE) {
3367 err = got_path_dirname(&parent_path, s->in_repo_path);
3370 free(s->in_repo_path);
3371 s->in_repo_path = parent_path;
3372 s->thread_args.in_repo_path = s->in_repo_path;
3373 } else if (ch == CTRL('l')) {
3374 struct got_object_id *start_id;
3375 err = got_repo_match_object_id(&start_id, NULL,
3376 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3377 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3381 s->start_id = start_id;
3382 s->thread_args.start_id = s->start_id;
3384 s->log_branches = !s->log_branches;
3386 if (s->thread_args.pack_fds == NULL) {
3387 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3391 err = got_repo_open(&s->thread_args.repo,
3392 got_repo_get_path(s->repo), NULL,
3393 s->thread_args.pack_fds);
3397 err = tog_load_refs(s->repo, 0);
3400 err = got_commit_graph_open(&s->thread_args.graph,
3401 s->in_repo_path, !s->log_branches);
3404 err = got_commit_graph_iter_start(s->thread_args.graph,
3405 s->start_id, s->repo, NULL, NULL);
3408 free_commits(&s->commits);
3409 s->first_displayed_entry = NULL;
3410 s->last_displayed_entry = NULL;
3411 s->selected_entry = NULL;
3413 s->thread_args.log_complete = 0;
3415 s->thread_args.commits_needed = view->lines;
3416 s->matched_entry = NULL;
3417 s->search_entry = NULL;
3421 if (view_is_parent_view(view))
3422 view_get_split(view, &begin_y, &begin_x);
3423 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3424 if (ref_view == NULL)
3425 return got_error_from_errno("view_open");
3426 err = open_ref_view(ref_view, s->repo);
3428 view_close(ref_view);
3431 if (view_is_parent_view(view) &&
3432 view->mode == TOG_VIEW_SPLIT_HRZN) {
3433 err = view_init_hsplit(view, begin_y);
3438 ref_view->focussed = 1;
3439 ref_view->mode = view->mode;
3440 ref_view->nlines = view->lines - begin_y;
3441 if (view_is_parent_view(view)) {
3442 view_transfer_size(ref_view, view);
3443 err = view_close_child(view);
3446 err = view_set_child(view, ref_view);
3449 view->focus_child = 1;
3451 *new_view = ref_view;
3461 static const struct got_error *
3462 apply_unveil(const char *repo_path, const char *worktree_path)
3464 const struct got_error *error;
3467 if (unveil("gmon.out", "rwc") != 0)
3468 return got_error_from_errno2("unveil", "gmon.out");
3470 if (repo_path && unveil(repo_path, "r") != 0)
3471 return got_error_from_errno2("unveil", repo_path);
3473 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3474 return got_error_from_errno2("unveil", worktree_path);
3476 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3477 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3479 error = got_privsep_unveil_exec_helpers();
3483 if (unveil(NULL, NULL) != 0)
3484 return got_error_from_errno("unveil");
3493 * Override default signal handlers before starting ncurses.
3494 * This should prevent ncurses from installing its own
3495 * broken cleanup() signal handler.
3497 signal(SIGWINCH, tog_sigwinch);
3498 signal(SIGPIPE, tog_sigpipe);
3499 signal(SIGCONT, tog_sigcont);
3500 signal(SIGINT, tog_sigint);
3501 signal(SIGTERM, tog_sigterm);
3505 halfdelay(1); /* Do fast refresh while initial view is loading. */
3508 intrflush(stdscr, FALSE);
3509 keypad(stdscr, TRUE);
3511 if (getenv("TOG_COLORS") != NULL) {
3513 use_default_colors();
3517 static const struct got_error *
3518 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3519 struct got_repository *repo, struct got_worktree *worktree)
3521 const struct got_error *err = NULL;
3524 *in_repo_path = strdup("/");
3525 if (*in_repo_path == NULL)
3526 return got_error_from_errno("strdup");
3531 const char *prefix = got_worktree_get_path_prefix(worktree);
3534 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3537 if (asprintf(in_repo_path, "%s%s%s", prefix,
3538 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3540 err = got_error_from_errno("asprintf");
3541 *in_repo_path = NULL;
3545 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3550 static const struct got_error *
3551 cmd_log(int argc, char *argv[])
3553 const struct got_error *error;
3554 struct got_repository *repo = NULL;
3555 struct got_worktree *worktree = NULL;
3556 struct got_object_id *start_id = NULL;
3557 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3558 char *start_commit = NULL, *label = NULL;
3559 struct got_reference *ref = NULL;
3560 const char *head_ref_name = NULL;
3561 int ch, log_branches = 0;
3562 struct tog_view *view;
3563 int *pack_fds = NULL;
3565 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3571 start_commit = optarg;
3574 repo_path = realpath(optarg, NULL);
3575 if (repo_path == NULL)
3576 return got_error_from_errno2("realpath",
3591 error = got_repo_pack_fds_open(&pack_fds);
3595 if (repo_path == NULL) {
3596 cwd = getcwd(NULL, 0);
3598 return got_error_from_errno("getcwd");
3599 error = got_worktree_open(&worktree, cwd);
3600 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3604 strdup(got_worktree_get_repo_path(worktree));
3606 repo_path = strdup(cwd);
3607 if (repo_path == NULL) {
3608 error = got_error_from_errno("strdup");
3613 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3617 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3624 error = apply_unveil(got_repo_get_path(repo),
3625 worktree ? got_worktree_get_root_path(worktree) : NULL);
3629 /* already loaded by tog_log_with_path()? */
3630 if (TAILQ_EMPTY(&tog_refs)) {
3631 error = tog_load_refs(repo, 0);
3636 if (start_commit == NULL) {
3637 error = got_repo_match_object_id(&start_id, &label,
3638 worktree ? got_worktree_get_head_ref_name(worktree) :
3639 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3642 head_ref_name = label;
3644 error = got_ref_open(&ref, repo, start_commit, 0);
3646 head_ref_name = got_ref_get_name(ref);
3647 else if (error->code != GOT_ERR_NOT_REF)
3649 error = got_repo_match_object_id(&start_id, NULL,
3650 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3655 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3657 error = got_error_from_errno("view_open");
3660 error = open_log_view(view, start_id, repo, head_ref_name,
3661 in_repo_path, log_branches);
3665 /* Release work tree lock. */
3666 got_worktree_close(worktree);
3669 error = view_loop(view);
3679 const struct got_error *close_err = got_repo_close(repo);
3684 got_worktree_close(worktree);
3686 const struct got_error *pack_err =
3687 got_repo_pack_fds_close(pack_fds);
3699 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3700 "[-w] object1 object2\n", getprogname());
3705 match_line(const char *line, regex_t *regex, size_t nmatch,
3706 regmatch_t *regmatch)
3708 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3711 static struct tog_color *
3712 match_color(struct tog_colors *colors, const char *line)
3714 struct tog_color *tc = NULL;
3716 STAILQ_FOREACH(tc, colors, entry) {
3717 if (match_line(line, &tc->regex, 0, NULL))
3724 static const struct got_error *
3725 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3726 WINDOW *window, int skipcol, regmatch_t *regmatch)
3728 const struct got_error *err = NULL;
3730 wchar_t *wline = NULL;
3731 int rme, rms, n, width, scrollx;
3732 int width0 = 0, width1 = 0, width2 = 0;
3733 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3737 rms = regmatch->rm_so;
3738 rme = regmatch->rm_eo;
3740 err = expand_tab(&exstr, line);
3744 /* Split the line into 3 segments, according to match offsets. */
3745 seg0 = strndup(exstr, rms);
3747 err = got_error_from_errno("strndup");
3750 seg1 = strndup(exstr + rms, rme - rms);
3752 err = got_error_from_errno("strndup");
3755 seg2 = strdup(exstr + rme);
3757 err = got_error_from_errno("strndup");
3761 /* draw up to matched token if we haven't scrolled past it */
3762 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3766 n = MAX(width0 - skipcol, 0);
3769 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3770 wlimit, col_tab_align, 1);
3773 waddwstr(window, &wline[scrollx]);
3783 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3787 wlen = wcslen(wline);
3789 width = wcwidth(wline[i]);
3791 /* should not happen, tabs are expanded */
3792 err = got_error(GOT_ERR_RANGE);
3795 if (width0 + w + width > skipcol)
3800 /* draw (visible part of) matched token (if scrolled into it) */
3801 if (width1 - w > 0) {
3802 wattron(window, A_STANDOUT);
3803 waddwstr(window, &wline[i]);
3804 wattroff(window, A_STANDOUT);
3805 wlimit -= (width1 - w);
3806 *wtotal += (width1 - w);
3810 if (wlimit > 0) { /* draw rest of line */
3812 if (skipcol > width0 + width1) {
3813 err = format_line(&wline, &width2, &scrollx, seg2,
3814 skipcol - (width0 + width1), wlimit,
3818 waddwstr(window, &wline[scrollx]);
3820 err = format_line(&wline, &width2, NULL, seg2, 0,
3821 wlimit, col_tab_align, 1);
3824 waddwstr(window, wline);
3837 static const struct got_error *
3838 draw_file(struct tog_view *view, const char *header)
3840 struct tog_diff_view_state *s = &view->state.diff;
3841 regmatch_t *regmatch = &view->regmatch;
3842 const struct got_error *err;
3845 size_t linesize = 0;
3847 struct tog_color *tc;
3850 int max_lines = view->nlines;
3851 int nlines = s->nlines;
3854 line_offset = s->line_offsets[s->first_displayed_line - 1];
3855 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3856 return got_error_from_errno("fseek");
3858 werase(view->window);
3861 if (asprintf(&line, "[%d/%d] %s",
3862 s->first_displayed_line - 1 + s->selected_line, nlines,
3864 return got_error_from_errno("asprintf");
3865 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3871 if (view_needs_focus_indication(view))
3872 wstandout(view->window);
3873 waddwstr(view->window, wline);
3876 if (view_needs_focus_indication(view))
3877 wstandend(view->window);
3878 if (width <= view->ncols - 1)
3879 waddch(view->window, '\n');
3889 while (max_lines > 0 && nprinted < max_lines) {
3890 linelen = getline(&line, &linesize, s->f);
3891 if (linelen == -1) {
3897 return got_ferror(s->f, GOT_ERR_IO);
3900 /* Set view->maxx based on full line length. */
3901 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3907 view->maxx = MAX(view->maxx, width);
3911 tc = match_color(&s->colors, line);
3913 wattr_on(view->window,
3914 COLOR_PAIR(tc->colorpair), NULL);
3915 if (s->first_displayed_line + nprinted == s->matched_line &&
3916 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3917 err = add_matched_line(&width, line, view->ncols, 0,
3918 view->window, view->x, regmatch);
3925 err = format_line(&wline, &width, &skip, line,
3926 view->x, view->ncols, 0, view->x ? 1 : 0);
3931 waddwstr(view->window, &wline[skip]);
3936 wattr_off(view->window,
3937 COLOR_PAIR(tc->colorpair), NULL);
3938 if (width <= view->ncols - 1)
3939 waddch(view->window, '\n');
3944 s->last_displayed_line = s->first_displayed_line +
3947 s->last_displayed_line = s->first_displayed_line;
3952 while (nprinted < view->nlines) {
3953 waddch(view->window, '\n');
3957 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3963 wstandout(view->window);
3964 waddwstr(view->window, wline);
3967 wstandend(view->window);
3974 get_datestr(time_t *time, char *datebuf)
3976 struct tm mytm, *tm;
3979 tm = gmtime_r(time, &mytm);
3982 s = asctime_r(tm, datebuf);
3985 p = strchr(s, '\n');
3991 static const struct got_error *
3992 get_changed_paths(struct got_pathlist_head *paths,
3993 struct got_commit_object *commit, struct got_repository *repo)
3995 const struct got_error *err = NULL;
3996 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3997 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3998 struct got_object_qid *qid;
4000 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4002 struct got_commit_object *pcommit;
4003 err = got_object_open_as_commit(&pcommit, repo,
4008 tree_id1 = got_object_id_dup(
4009 got_object_commit_get_tree_id(pcommit));
4010 if (tree_id1 == NULL) {
4011 got_object_commit_close(pcommit);
4012 return got_error_from_errno("got_object_id_dup");
4014 got_object_commit_close(pcommit);
4019 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4024 tree_id2 = got_object_commit_get_tree_id(commit);
4025 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4029 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4030 got_diff_tree_collect_changed_paths, paths, 0);
4033 got_object_tree_close(tree1);
4035 got_object_tree_close(tree2);
4040 static const struct got_error *
4041 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4045 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4047 return got_error_from_errno("reallocarray");
4049 (*line_offsets)[*nlines] = off;
4054 static const struct got_error *
4055 write_commit_info(off_t **line_offsets, size_t *nlines,
4056 struct got_object_id *commit_id, struct got_reflist_head *refs,
4057 struct got_repository *repo, FILE *outfile)
4059 const struct got_error *err = NULL;
4060 char datebuf[26], *datestr;
4061 struct got_commit_object *commit;
4062 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4063 time_t committer_time;
4064 const char *author, *committer;
4065 char *refs_str = NULL;
4066 struct got_pathlist_head changed_paths;
4067 struct got_pathlist_entry *pe;
4071 TAILQ_INIT(&changed_paths);
4074 err = build_refs_str(&refs_str, refs, commit_id, repo);
4079 err = got_object_open_as_commit(&commit, repo, commit_id);
4083 err = got_object_id_str(&id_str, commit_id);
4085 err = got_error_from_errno("got_object_id_str");
4089 err = add_line_offset(line_offsets, nlines, 0);
4093 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4094 refs_str ? refs_str : "", refs_str ? ")" : "");
4096 err = got_error_from_errno("fprintf");
4100 err = add_line_offset(line_offsets, nlines, outoff);
4104 n = fprintf(outfile, "from: %s\n",
4105 got_object_commit_get_author(commit));
4107 err = got_error_from_errno("fprintf");
4111 err = add_line_offset(line_offsets, nlines, outoff);
4115 committer_time = got_object_commit_get_committer_time(commit);
4116 datestr = get_datestr(&committer_time, datebuf);
4118 n = fprintf(outfile, "date: %s UTC\n", datestr);
4120 err = got_error_from_errno("fprintf");
4124 err = add_line_offset(line_offsets, nlines, outoff);
4128 author = got_object_commit_get_author(commit);
4129 committer = got_object_commit_get_committer(commit);
4130 if (strcmp(author, committer) != 0) {
4131 n = fprintf(outfile, "via: %s\n", committer);
4133 err = got_error_from_errno("fprintf");
4137 err = add_line_offset(line_offsets, nlines, outoff);
4141 if (got_object_commit_get_nparents(commit) > 1) {
4142 const struct got_object_id_queue *parent_ids;
4143 struct got_object_qid *qid;
4145 parent_ids = got_object_commit_get_parent_ids(commit);
4146 STAILQ_FOREACH(qid, parent_ids, entry) {
4147 err = got_object_id_str(&id_str, &qid->id);
4150 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4152 err = got_error_from_errno("fprintf");
4156 err = add_line_offset(line_offsets, nlines, outoff);
4164 err = got_object_commit_get_logmsg(&logmsg, commit);
4168 while ((line = strsep(&s, "\n")) != NULL) {
4169 n = fprintf(outfile, "%s\n", line);
4171 err = got_error_from_errno("fprintf");
4175 err = add_line_offset(line_offsets, nlines, outoff);
4180 err = get_changed_paths(&changed_paths, commit, repo);
4183 TAILQ_FOREACH(pe, &changed_paths, entry) {
4184 struct got_diff_changed_path *cp = pe->data;
4185 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4187 err = got_error_from_errno("fprintf");
4191 err = add_line_offset(line_offsets, nlines, outoff);
4194 free((char *)pe->path);
4198 fputc('\n', outfile);
4200 err = add_line_offset(line_offsets, nlines, outoff);
4202 got_pathlist_free(&changed_paths);
4206 got_object_commit_close(commit);
4208 free(*line_offsets);
4209 *line_offsets = NULL;
4215 static const struct got_error *
4216 create_diff(struct tog_diff_view_state *s)
4218 const struct got_error *err = NULL;
4222 free(s->line_offsets);
4223 s->line_offsets = malloc(sizeof(off_t));
4224 if (s->line_offsets == NULL)
4225 return got_error_from_errno("malloc");
4230 err = got_error_from_errno("got_opentemp");
4233 if (s->f && fclose(s->f) == EOF) {
4234 err = got_error_from_errno("fclose");
4240 err = got_object_get_type(&obj_type, s->repo, s->id1);
4242 err = got_object_get_type(&obj_type, s->repo, s->id2);
4247 case GOT_OBJ_TYPE_BLOB:
4248 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4249 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4250 s->label1, s->label2, tog_diff_algo, s->diff_context,
4251 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4253 case GOT_OBJ_TYPE_TREE:
4254 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4255 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4256 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4257 s->force_text_diff, s->repo, s->f);
4259 case GOT_OBJ_TYPE_COMMIT: {
4260 const struct got_object_id_queue *parent_ids;
4261 struct got_object_qid *pid;
4262 struct got_commit_object *commit2;
4263 struct got_reflist_head *refs;
4265 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4268 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4269 /* Show commit info if we're diffing to a parent/root commit. */
4270 if (s->id1 == NULL) {
4271 err = write_commit_info(&s->line_offsets, &s->nlines,
4272 s->id2, refs, s->repo, s->f);
4276 parent_ids = got_object_commit_get_parent_ids(commit2);
4277 STAILQ_FOREACH(pid, parent_ids, entry) {
4278 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4279 err = write_commit_info(
4280 &s->line_offsets, &s->nlines,
4281 s->id2, refs, s->repo, s->f);
4288 got_object_commit_close(commit2);
4290 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4291 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4292 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4293 s->force_text_diff, s->repo, s->f);
4297 err = got_error(GOT_ERR_OBJ_TYPE);
4303 if (s->f && fflush(s->f) != 0 && err == NULL)
4304 err = got_error_from_errno("fflush");
4309 diff_view_indicate_progress(struct tog_view *view)
4311 mvwaddstr(view->window, 0, 0, "diffing...");
4316 static const struct got_error *
4317 search_start_diff_view(struct tog_view *view)
4319 struct tog_diff_view_state *s = &view->state.diff;
4321 s->matched_line = 0;
4325 static const struct got_error *
4326 search_next_diff_view(struct tog_view *view)
4328 struct tog_diff_view_state *s = &view->state.diff;
4329 const struct got_error *err = NULL;
4332 size_t linesize = 0;
4335 if (!view->searching) {
4336 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4340 if (s->matched_line) {
4341 if (view->searching == TOG_SEARCH_FORWARD)
4342 lineno = s->matched_line + 1;
4344 lineno = s->matched_line - 1;
4346 lineno = s->first_displayed_line;
4351 if (lineno <= 0 || lineno > s->nlines) {
4352 if (s->matched_line == 0) {
4353 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4357 if (view->searching == TOG_SEARCH_FORWARD)
4363 offset = s->line_offsets[lineno - 1];
4364 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4366 return got_error_from_errno("fseeko");
4368 linelen = getline(&line, &linesize, s->f);
4369 if (linelen != -1) {
4371 err = expand_tab(&exstr, line);
4374 if (match_line(exstr, &view->regex, 1,
4376 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4377 s->matched_line = lineno;
4383 if (view->searching == TOG_SEARCH_FORWARD)
4390 if (s->matched_line) {
4391 s->first_displayed_line = s->matched_line;
4392 s->selected_line = 1;
4398 static const struct got_error *
4399 close_diff_view(struct tog_view *view)
4401 const struct got_error *err = NULL;
4402 struct tog_diff_view_state *s = &view->state.diff;
4408 if (s->f && fclose(s->f) == EOF)
4409 err = got_error_from_errno("fclose");
4411 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4412 err = got_error_from_errno("fclose");
4414 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4415 err = got_error_from_errno("fclose");
4417 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4418 err = got_error_from_errno("close");
4420 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4421 err = got_error_from_errno("close");
4423 free_colors(&s->colors);
4424 free(s->line_offsets);
4425 s->line_offsets = NULL;
4430 static const struct got_error *
4431 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4432 struct got_object_id *id2, const char *label1, const char *label2,
4433 int diff_context, int ignore_whitespace, int force_text_diff,
4434 struct tog_view *parent_view, struct got_repository *repo)
4436 const struct got_error *err;
4437 struct tog_diff_view_state *s = &view->state.diff;
4439 memset(s, 0, sizeof(*s));
4443 if (id1 != NULL && id2 != NULL) {
4445 err = got_object_get_type(&type1, repo, id1);
4448 err = got_object_get_type(&type2, repo, id2);
4453 return got_error(GOT_ERR_OBJ_TYPE);
4455 s->first_displayed_line = 1;
4456 s->last_displayed_line = view->nlines;
4457 s->selected_line = 1;
4465 s->id1 = got_object_id_dup(id1);
4467 return got_error_from_errno("got_object_id_dup");
4471 s->id2 = got_object_id_dup(id2);
4472 if (s->id2 == NULL) {
4473 err = got_error_from_errno("got_object_id_dup");
4477 s->f1 = got_opentemp();
4478 if (s->f1 == NULL) {
4479 err = got_error_from_errno("got_opentemp");
4483 s->f2 = got_opentemp();
4484 if (s->f2 == NULL) {
4485 err = got_error_from_errno("got_opentemp");
4489 s->fd1 = got_opentempfd();
4491 err = got_error_from_errno("got_opentempfd");
4495 s->fd2 = got_opentempfd();
4497 err = got_error_from_errno("got_opentempfd");
4501 s->first_displayed_line = 1;
4502 s->last_displayed_line = view->nlines;
4503 s->diff_context = diff_context;
4504 s->ignore_whitespace = ignore_whitespace;
4505 s->force_text_diff = force_text_diff;
4506 s->parent_view = parent_view;
4509 STAILQ_INIT(&s->colors);
4510 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4511 err = add_color(&s->colors,
4512 "^-", TOG_COLOR_DIFF_MINUS,
4513 get_color_value("TOG_COLOR_DIFF_MINUS"));
4516 err = add_color(&s->colors, "^\\+",
4517 TOG_COLOR_DIFF_PLUS,
4518 get_color_value("TOG_COLOR_DIFF_PLUS"));
4521 err = add_color(&s->colors,
4522 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4523 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4527 err = add_color(&s->colors,
4528 "^(commit [0-9a-f]|parent [0-9]|"
4529 "(blob|file|tree|commit) [-+] |"
4530 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4531 get_color_value("TOG_COLOR_DIFF_META"));
4535 err = add_color(&s->colors,
4536 "^(from|via): ", TOG_COLOR_AUTHOR,
4537 get_color_value("TOG_COLOR_AUTHOR"));
4541 err = add_color(&s->colors,
4542 "^date: ", TOG_COLOR_DATE,
4543 get_color_value("TOG_COLOR_DATE"));
4548 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4549 view_is_splitscreen(view))
4550 show_log_view(parent_view); /* draw border */
4551 diff_view_indicate_progress(view);
4553 err = create_diff(s);
4555 view->show = show_diff_view;
4556 view->input = input_diff_view;
4557 view->reset = reset_diff_view;
4558 view->close = close_diff_view;
4559 view->search_start = search_start_diff_view;
4560 view->search_next = search_next_diff_view;
4563 close_diff_view(view);
4567 static const struct got_error *
4568 show_diff_view(struct tog_view *view)
4570 const struct got_error *err;
4571 struct tog_diff_view_state *s = &view->state.diff;
4572 char *id_str1 = NULL, *id_str2, *header;
4573 const char *label1, *label2;
4576 err = got_object_id_str(&id_str1, s->id1);
4579 label1 = s->label1 ? : id_str1;
4581 label1 = "/dev/null";
4583 err = got_object_id_str(&id_str2, s->id2);
4586 label2 = s->label2 ? : id_str2;
4588 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4589 err = got_error_from_errno("asprintf");
4597 err = draw_file(view, header);
4602 static const struct got_error *
4603 set_selected_commit(struct tog_diff_view_state *s,
4604 struct commit_queue_entry *entry)
4606 const struct got_error *err;
4607 const struct got_object_id_queue *parent_ids;
4608 struct got_commit_object *selected_commit;
4609 struct got_object_qid *pid;
4612 s->id2 = got_object_id_dup(entry->id);
4614 return got_error_from_errno("got_object_id_dup");
4616 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4619 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4621 pid = STAILQ_FIRST(parent_ids);
4622 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4623 got_object_commit_close(selected_commit);
4627 static const struct got_error *
4628 reset_diff_view(struct tog_view *view)
4630 struct tog_diff_view_state *s = &view->state.diff;
4633 wclear(view->window);
4634 s->first_displayed_line = 1;
4635 s->last_displayed_line = view->nlines;
4636 s->matched_line = 0;
4637 diff_view_indicate_progress(view);
4638 return create_diff(s);
4641 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4643 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4646 static const struct got_error *
4647 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4649 const struct got_error *err = NULL;
4650 struct tog_diff_view_state *s = &view->state.diff;
4651 struct tog_log_view_state *ls;
4652 struct commit_queue_entry *old_selected_entry;
4654 size_t linesize = 0;
4656 int i, nscroll = view->nlines - 1, up = 0;
4663 view->x = MAX(view->maxx - view->ncols / 3, 0);
4668 if (view->x + view->ncols / 3 < view->maxx)
4669 view->x += 2; /* move two columns right */
4675 view->x -= MIN(view->x, 2); /* move two columns back */
4682 s->force_text_diff = !s->force_text_diff;
4684 s->ignore_whitespace = !s->ignore_whitespace;
4685 err = reset_diff_view(view);
4689 s->first_displayed_line = 1;
4698 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4704 if (s->first_displayed_line > 1)
4705 s->first_displayed_line--;
4716 if (s->first_displayed_line == 1) {
4721 while (i++ < nscroll && s->first_displayed_line > 1)
4722 s->first_displayed_line--;
4728 s->first_displayed_line++;
4745 while (!s->eof && i++ < nscroll) {
4746 linelen = getline(&line, &linesize, s->f);
4747 s->first_displayed_line++;
4748 if (linelen == -1) {
4752 err = got_ferror(s->f, GOT_ERR_IO);
4759 if (s->diff_context > 0) {
4761 s->matched_line = 0;
4762 diff_view_indicate_progress(view);
4763 err = create_diff(s);
4764 if (s->first_displayed_line + view->nlines - 1 >
4766 s->first_displayed_line = 1;
4767 s->last_displayed_line = view->nlines;
4773 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4775 s->matched_line = 0;
4776 diff_view_indicate_progress(view);
4777 err = create_diff(s);
4787 if (s->parent_view == NULL) {
4791 s->parent_view->count = view->count;
4793 if (s->parent_view->type == TOG_VIEW_LOG) {
4794 ls = &s->parent_view->state.log;
4795 old_selected_entry = ls->selected_entry;
4797 err = input_log_view(NULL, s->parent_view,
4798 up ? KEY_UP : KEY_DOWN);
4801 view->count = s->parent_view->count;
4803 if (old_selected_entry == ls->selected_entry)
4806 err = set_selected_commit(s, ls->selected_entry);
4809 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4810 struct tog_blame_view_state *bs;
4811 struct got_object_id *id, *prev_id;
4813 bs = &s->parent_view->state.blame;
4814 prev_id = get_annotation_for_line(bs->blame.lines,
4815 bs->blame.nlines, bs->last_diffed_line);
4817 err = input_blame_view(&view, s->parent_view,
4818 up ? KEY_UP : KEY_DOWN);
4821 view->count = s->parent_view->count;
4823 if (prev_id == NULL)
4825 id = get_selected_commit_id(bs->blame.lines,
4826 bs->blame.nlines, bs->first_displayed_line,
4831 if (!got_object_id_cmp(prev_id, id))
4834 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4838 s->first_displayed_line = 1;
4839 s->last_displayed_line = view->nlines;
4840 s->matched_line = 0;
4843 diff_view_indicate_progress(view);
4844 err = create_diff(s);
4854 static const struct got_error *
4855 cmd_diff(int argc, char *argv[])
4857 const struct got_error *error = NULL;
4858 struct got_repository *repo = NULL;
4859 struct got_worktree *worktree = NULL;
4860 struct got_object_id *id1 = NULL, *id2 = NULL;
4861 char *repo_path = NULL, *cwd = NULL;
4862 char *id_str1 = NULL, *id_str2 = NULL;
4863 char *label1 = NULL, *label2 = NULL;
4864 int diff_context = 3, ignore_whitespace = 0;
4865 int ch, force_text_diff = 0;
4867 struct tog_view *view;
4868 int *pack_fds = NULL;
4870 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4873 force_text_diff = 1;
4876 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4879 errx(1, "number of context lines is %s: %s",
4883 repo_path = realpath(optarg, NULL);
4884 if (repo_path == NULL)
4885 return got_error_from_errno2("realpath",
4887 got_path_strip_trailing_slashes(repo_path);
4890 ignore_whitespace = 1;
4902 usage_diff(); /* TODO show local worktree changes */
4903 } else if (argc == 2) {
4909 error = got_repo_pack_fds_open(&pack_fds);
4913 if (repo_path == NULL) {
4914 cwd = getcwd(NULL, 0);
4916 return got_error_from_errno("getcwd");
4917 error = got_worktree_open(&worktree, cwd);
4918 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4922 strdup(got_worktree_get_repo_path(worktree));
4924 repo_path = strdup(cwd);
4925 if (repo_path == NULL) {
4926 error = got_error_from_errno("strdup");
4931 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4937 error = apply_unveil(got_repo_get_path(repo), NULL);
4941 error = tog_load_refs(repo, 0);
4945 error = got_repo_match_object_id(&id1, &label1, id_str1,
4946 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4950 error = got_repo_match_object_id(&id2, &label2, id_str2,
4951 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4955 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4957 error = got_error_from_errno("view_open");
4960 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4961 ignore_whitespace, force_text_diff, NULL, repo);
4964 error = view_loop(view);
4971 const struct got_error *close_err = got_repo_close(repo);
4976 got_worktree_close(worktree);
4978 const struct got_error *pack_err =
4979 got_repo_pack_fds_close(pack_fds);
4992 "usage: %s blame [-c commit] [-r repository-path] path\n",
4997 struct tog_blame_line {
4999 struct got_object_id *id;
5002 static const struct got_error *
5003 draw_blame(struct tog_view *view)
5005 struct tog_blame_view_state *s = &view->state.blame;
5006 struct tog_blame *blame = &s->blame;
5007 regmatch_t *regmatch = &view->regmatch;
5008 const struct got_error *err;
5009 int lineno = 0, nprinted = 0;
5011 size_t linesize = 0;
5015 struct tog_blame_line *blame_line;
5016 struct got_object_id *prev_id = NULL;
5018 struct tog_color *tc;
5020 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5025 werase(view->window);
5027 if (asprintf(&line, "commit %s", id_str) == -1) {
5028 err = got_error_from_errno("asprintf");
5033 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5038 if (view_needs_focus_indication(view))
5039 wstandout(view->window);
5040 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5042 wattr_on(view->window,
5043 COLOR_PAIR(tc->colorpair), NULL);
5044 waddwstr(view->window, wline);
5046 wattr_off(view->window,
5047 COLOR_PAIR(tc->colorpair), NULL);
5048 if (view_needs_focus_indication(view))
5049 wstandend(view->window);
5052 if (width < view->ncols - 1)
5053 waddch(view->window, '\n');
5055 if (asprintf(&line, "[%d/%d] %s%s",
5056 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5057 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5059 return got_error_from_errno("asprintf");
5062 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5067 waddwstr(view->window, wline);
5070 if (width < view->ncols - 1)
5071 waddch(view->window, '\n');
5075 while (nprinted < view->nlines - 2) {
5076 linelen = getline(&line, &linesize, blame->f);
5077 if (linelen == -1) {
5078 if (feof(blame->f)) {
5083 return got_ferror(blame->f, GOT_ERR_IO);
5085 if (++lineno < s->first_displayed_line)
5088 /* Set view->maxx based on full line length. */
5089 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5096 view->maxx = MAX(view->maxx, width);
5098 if (nprinted == s->selected_line - 1)
5099 wstandout(view->window);
5101 if (blame->nlines > 0) {
5102 blame_line = &blame->lines[lineno - 1];
5103 if (blame_line->annotated && prev_id &&
5104 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5105 !(nprinted == s->selected_line - 1)) {
5106 waddstr(view->window, " ");
5107 } else if (blame_line->annotated) {
5109 err = got_object_id_str(&id_str,
5115 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5117 wattr_on(view->window,
5118 COLOR_PAIR(tc->colorpair), NULL);
5119 wprintw(view->window, "%.8s", id_str);
5121 wattr_off(view->window,
5122 COLOR_PAIR(tc->colorpair), NULL);
5124 prev_id = blame_line->id;
5126 waddstr(view->window, "........");
5130 waddstr(view->window, "........");
5134 if (nprinted == s->selected_line - 1)
5135 wstandend(view->window);
5136 waddstr(view->window, " ");
5138 if (view->ncols <= 9) {
5140 } else if (s->first_displayed_line + nprinted ==
5142 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5143 err = add_matched_line(&width, line, view->ncols - 9, 9,
5144 view->window, view->x, regmatch);
5152 err = format_line(&wline, &width, &skip, line,
5153 view->x, view->ncols - 9, 9, 1);
5158 waddwstr(view->window, &wline[skip]);
5164 if (width <= view->ncols - 1)
5165 waddch(view->window, '\n');
5166 if (++nprinted == 1)
5167 s->first_displayed_line = lineno;
5170 s->last_displayed_line = lineno;
5177 static const struct got_error *
5178 blame_cb(void *arg, int nlines, int lineno,
5179 struct got_commit_object *commit, struct got_object_id *id)
5181 const struct got_error *err = NULL;
5182 struct tog_blame_cb_args *a = arg;
5183 struct tog_blame_line *line;
5186 if (nlines != a->nlines ||
5187 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5188 return got_error(GOT_ERR_RANGE);
5190 errcode = pthread_mutex_lock(&tog_mutex);
5192 return got_error_set_errno(errcode, "pthread_mutex_lock");
5194 if (*a->quit) { /* user has quit the blame view */
5195 err = got_error(GOT_ERR_ITER_COMPLETED);
5200 goto done; /* no change in this commit */
5202 line = &a->lines[lineno - 1];
5203 if (line->annotated)
5206 line->id = got_object_id_dup(id);
5207 if (line->id == NULL) {
5208 err = got_error_from_errno("got_object_id_dup");
5211 line->annotated = 1;
5213 errcode = pthread_mutex_unlock(&tog_mutex);
5215 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5220 blame_thread(void *arg)
5222 const struct got_error *err, *close_err;
5223 struct tog_blame_thread_args *ta = arg;
5224 struct tog_blame_cb_args *a = ta->cb_args;
5225 int errcode, fd1 = -1, fd2 = -1;
5226 FILE *f1 = NULL, *f2 = NULL;
5228 fd1 = got_opentempfd();
5230 return (void *)got_error_from_errno("got_opentempfd");
5232 fd2 = got_opentempfd();
5234 err = got_error_from_errno("got_opentempfd");
5238 f1 = got_opentemp();
5240 err = (void *)got_error_from_errno("got_opentemp");
5243 f2 = got_opentemp();
5245 err = (void *)got_error_from_errno("got_opentemp");
5249 err = block_signals_used_by_main_thread();
5253 err = got_blame(ta->path, a->commit_id, ta->repo,
5254 tog_diff_algo, blame_cb, ta->cb_args,
5255 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5256 if (err && err->code == GOT_ERR_CANCELLED)
5259 errcode = pthread_mutex_lock(&tog_mutex);
5261 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5265 close_err = got_repo_close(ta->repo);
5271 errcode = pthread_mutex_unlock(&tog_mutex);
5272 if (errcode && err == NULL)
5273 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5276 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5277 err = got_error_from_errno("close");
5278 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5279 err = got_error_from_errno("close");
5280 if (f1 && fclose(f1) == EOF && err == NULL)
5281 err = got_error_from_errno("fclose");
5282 if (f2 && fclose(f2) == EOF && err == NULL)
5283 err = got_error_from_errno("fclose");
5288 static struct got_object_id *
5289 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5290 int first_displayed_line, int selected_line)
5292 struct tog_blame_line *line;
5297 line = &lines[first_displayed_line - 1 + selected_line - 1];
5298 if (!line->annotated)
5304 static struct got_object_id *
5305 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5308 struct tog_blame_line *line;
5310 if (nlines <= 0 || lineno >= nlines)
5313 line = &lines[lineno - 1];
5314 if (!line->annotated)
5320 static const struct got_error *
5321 stop_blame(struct tog_blame *blame)
5323 const struct got_error *err = NULL;
5326 if (blame->thread) {
5328 errcode = pthread_mutex_unlock(&tog_mutex);
5330 return got_error_set_errno(errcode,
5331 "pthread_mutex_unlock");
5332 errcode = pthread_join(blame->thread, (void **)&err);
5334 return got_error_set_errno(errcode, "pthread_join");
5335 errcode = pthread_mutex_lock(&tog_mutex);
5337 return got_error_set_errno(errcode,
5338 "pthread_mutex_lock");
5339 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5341 blame->thread = 0; //NULL;
5343 if (blame->thread_args.repo) {
5344 const struct got_error *close_err;
5345 close_err = got_repo_close(blame->thread_args.repo);
5348 blame->thread_args.repo = NULL;
5351 if (fclose(blame->f) == EOF && err == NULL)
5352 err = got_error_from_errno("fclose");
5356 for (i = 0; i < blame->nlines; i++)
5357 free(blame->lines[i].id);
5359 blame->lines = NULL;
5361 free(blame->cb_args.commit_id);
5362 blame->cb_args.commit_id = NULL;
5363 if (blame->pack_fds) {
5364 const struct got_error *pack_err =
5365 got_repo_pack_fds_close(blame->pack_fds);
5368 blame->pack_fds = NULL;
5373 static const struct got_error *
5374 cancel_blame_view(void *arg)
5376 const struct got_error *err = NULL;
5380 errcode = pthread_mutex_lock(&tog_mutex);
5382 return got_error_set_errno(errcode,
5383 "pthread_mutex_unlock");
5386 err = got_error(GOT_ERR_CANCELLED);
5388 errcode = pthread_mutex_unlock(&tog_mutex);
5390 return got_error_set_errno(errcode,
5391 "pthread_mutex_lock");
5396 static const struct got_error *
5397 run_blame(struct tog_view *view)
5399 struct tog_blame_view_state *s = &view->state.blame;
5400 struct tog_blame *blame = &s->blame;
5401 const struct got_error *err = NULL;
5402 struct got_commit_object *commit = NULL;
5403 struct got_blob_object *blob = NULL;
5404 struct got_repository *thread_repo = NULL;
5405 struct got_object_id *obj_id = NULL;
5406 int obj_type, fd = -1;
5407 int *pack_fds = NULL;
5409 err = got_object_open_as_commit(&commit, s->repo,
5410 &s->blamed_commit->id);
5414 fd = got_opentempfd();
5416 err = got_error_from_errno("got_opentempfd");
5420 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5424 err = got_object_get_type(&obj_type, s->repo, obj_id);
5428 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5429 err = got_error(GOT_ERR_OBJ_TYPE);
5433 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5436 blame->f = got_opentemp();
5437 if (blame->f == NULL) {
5438 err = got_error_from_errno("got_opentemp");
5441 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5442 &blame->line_offsets, blame->f, blob);
5445 if (blame->nlines == 0) {
5446 s->blame_complete = 1;
5450 /* Don't include \n at EOF in the blame line count. */
5451 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5454 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5455 if (blame->lines == NULL) {
5456 err = got_error_from_errno("calloc");
5460 err = got_repo_pack_fds_open(&pack_fds);
5463 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5468 blame->pack_fds = pack_fds;
5469 blame->cb_args.view = view;
5470 blame->cb_args.lines = blame->lines;
5471 blame->cb_args.nlines = blame->nlines;
5472 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5473 if (blame->cb_args.commit_id == NULL) {
5474 err = got_error_from_errno("got_object_id_dup");
5477 blame->cb_args.quit = &s->done;
5479 blame->thread_args.path = s->path;
5480 blame->thread_args.repo = thread_repo;
5481 blame->thread_args.cb_args = &blame->cb_args;
5482 blame->thread_args.complete = &s->blame_complete;
5483 blame->thread_args.cancel_cb = cancel_blame_view;
5484 blame->thread_args.cancel_arg = &s->done;
5485 s->blame_complete = 0;
5487 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5488 s->first_displayed_line = 1;
5489 s->last_displayed_line = view->nlines;
5490 s->selected_line = 1;
5492 s->matched_line = 0;
5496 got_object_commit_close(commit);
5497 if (fd != -1 && close(fd) == -1 && err == NULL)
5498 err = got_error_from_errno("close");
5500 got_object_blob_close(blob);
5507 static const struct got_error *
5508 open_blame_view(struct tog_view *view, char *path,
5509 struct got_object_id *commit_id, struct got_repository *repo)
5511 const struct got_error *err = NULL;
5512 struct tog_blame_view_state *s = &view->state.blame;
5514 STAILQ_INIT(&s->blamed_commits);
5516 s->path = strdup(path);
5517 if (s->path == NULL)
5518 return got_error_from_errno("strdup");
5520 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5526 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5527 s->first_displayed_line = 1;
5528 s->last_displayed_line = view->nlines;
5529 s->selected_line = 1;
5530 s->blame_complete = 0;
5532 s->commit_id = commit_id;
5533 memset(&s->blame, 0, sizeof(s->blame));
5535 STAILQ_INIT(&s->colors);
5536 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5537 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5538 get_color_value("TOG_COLOR_COMMIT"));
5543 view->show = show_blame_view;
5544 view->input = input_blame_view;
5545 view->reset = reset_blame_view;
5546 view->close = close_blame_view;
5547 view->search_start = search_start_blame_view;
5548 view->search_next = search_next_blame_view;
5550 return run_blame(view);
5553 static const struct got_error *
5554 close_blame_view(struct tog_view *view)
5556 const struct got_error *err = NULL;
5557 struct tog_blame_view_state *s = &view->state.blame;
5559 if (s->blame.thread)
5560 err = stop_blame(&s->blame);
5562 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5563 struct got_object_qid *blamed_commit;
5564 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5565 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5566 got_object_qid_free(blamed_commit);
5570 free_colors(&s->colors);
5574 static const struct got_error *
5575 search_start_blame_view(struct tog_view *view)
5577 struct tog_blame_view_state *s = &view->state.blame;
5579 s->matched_line = 0;
5583 static const struct got_error *
5584 search_next_blame_view(struct tog_view *view)
5586 struct tog_blame_view_state *s = &view->state.blame;
5587 const struct got_error *err = NULL;
5590 size_t linesize = 0;
5593 if (!view->searching) {
5594 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5598 if (s->matched_line) {
5599 if (view->searching == TOG_SEARCH_FORWARD)
5600 lineno = s->matched_line + 1;
5602 lineno = s->matched_line - 1;
5604 lineno = s->first_displayed_line - 1 + s->selected_line;
5609 if (lineno <= 0 || lineno > s->blame.nlines) {
5610 if (s->matched_line == 0) {
5611 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5615 if (view->searching == TOG_SEARCH_FORWARD)
5618 lineno = s->blame.nlines;
5621 offset = s->blame.line_offsets[lineno - 1];
5622 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5624 return got_error_from_errno("fseeko");
5626 linelen = getline(&line, &linesize, s->blame.f);
5627 if (linelen != -1) {
5629 err = expand_tab(&exstr, line);
5632 if (match_line(exstr, &view->regex, 1,
5634 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5635 s->matched_line = lineno;
5641 if (view->searching == TOG_SEARCH_FORWARD)
5648 if (s->matched_line) {
5649 s->first_displayed_line = s->matched_line;
5650 s->selected_line = 1;
5656 static const struct got_error *
5657 show_blame_view(struct tog_view *view)
5659 const struct got_error *err = NULL;
5660 struct tog_blame_view_state *s = &view->state.blame;
5663 if (s->blame.thread == 0 && !s->blame_complete) {
5664 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5665 &s->blame.thread_args);
5667 return got_error_set_errno(errcode, "pthread_create");
5669 halfdelay(1); /* fast refresh while annotating */
5672 if (s->blame_complete)
5673 halfdelay(10); /* disable fast refresh */
5675 err = draw_blame(view);
5681 static const struct got_error *
5682 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5684 const struct got_error *err = NULL, *thread_err = NULL;
5685 struct tog_view *diff_view;
5686 struct tog_blame_view_state *s = &view->state.blame;
5687 int eos, nscroll, begin_y = 0, begin_x = 0;
5689 eos = nscroll = view->nlines - 2;
5690 if (view_is_hsplit_top(view))
5698 view->x = MAX(view->maxx - view->ncols / 3, 0);
5703 if (view->x + view->ncols / 3 < view->maxx)
5704 view->x += 2; /* move two columns right */
5710 view->x -= MIN(view->x, 2); /* move two columns back */
5719 s->selected_line = 1;
5720 s->first_displayed_line = 1;
5725 if (s->blame.nlines < eos) {
5726 s->selected_line = s->blame.nlines;
5727 s->first_displayed_line = 1;
5729 s->selected_line = eos;
5730 s->first_displayed_line = s->blame.nlines - (eos - 1);
5737 if (s->selected_line > 1)
5739 else if (s->selected_line == 1 &&
5740 s->first_displayed_line > 1)
5741 s->first_displayed_line--;
5752 if (s->first_displayed_line == 1) {
5753 if (view->count > 1)
5755 s->selected_line = MAX(1, s->selected_line - nscroll);
5759 if (s->first_displayed_line > nscroll)
5760 s->first_displayed_line -= nscroll;
5762 s->first_displayed_line = 1;
5767 if (s->selected_line < eos && s->first_displayed_line +
5768 s->selected_line <= s->blame.nlines)
5770 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5771 s->first_displayed_line++;
5777 struct got_object_id *id = NULL;
5780 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5781 s->first_displayed_line, s->selected_line);
5785 struct got_commit_object *commit, *pcommit;
5786 struct got_object_qid *pid;
5787 struct got_object_id *blob_id = NULL;
5789 err = got_object_open_as_commit(&commit,
5794 got_object_commit_get_parent_ids(commit));
5796 got_object_commit_close(commit);
5799 /* Check if path history ends here. */
5800 err = got_object_open_as_commit(&pcommit,
5804 err = got_object_id_by_path(&blob_id, s->repo,
5806 got_object_commit_close(pcommit);
5808 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5810 got_object_commit_close(commit);
5813 err = got_object_get_type(&obj_type, s->repo,
5816 /* Can't blame non-blob type objects. */
5817 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5818 got_object_commit_close(commit);
5821 err = got_object_qid_alloc(&s->blamed_commit,
5823 got_object_commit_close(commit);
5825 if (got_object_id_cmp(id,
5826 &s->blamed_commit->id) == 0)
5828 err = got_object_qid_alloc(&s->blamed_commit,
5834 thread_err = stop_blame(&s->blame);
5838 STAILQ_INSERT_HEAD(&s->blamed_commits,
5839 s->blamed_commit, entry);
5840 err = run_blame(view);
5846 struct got_object_qid *first;
5849 first = STAILQ_FIRST(&s->blamed_commits);
5850 if (!got_object_id_cmp(&first->id, s->commit_id))
5853 thread_err = stop_blame(&s->blame);
5857 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5858 got_object_qid_free(s->blamed_commit);
5860 STAILQ_FIRST(&s->blamed_commits);
5861 err = run_blame(view);
5868 struct got_object_id *id = NULL;
5869 struct got_object_qid *pid;
5870 struct got_commit_object *commit = NULL;
5873 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5874 s->first_displayed_line, s->selected_line);
5877 err = got_object_open_as_commit(&commit, s->repo, id);
5880 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5882 /* traversed from diff view, release diff resources */
5883 err = close_diff_view(*new_view);
5886 diff_view = *new_view;
5888 if (view_is_parent_view(view))
5889 view_get_split(view, &begin_y, &begin_x);
5891 diff_view = view_open(0, 0, begin_y, begin_x,
5893 if (diff_view == NULL) {
5894 got_object_commit_close(commit);
5895 err = got_error_from_errno("view_open");
5899 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5900 id, NULL, NULL, 3, 0, 0, view, s->repo);
5901 got_object_commit_close(commit);
5903 view_close(diff_view);
5906 s->last_diffed_line = s->first_displayed_line - 1 +
5909 break; /* still open from active diff view */
5910 if (view_is_parent_view(view) &&
5911 view->mode == TOG_VIEW_SPLIT_HRZN) {
5912 err = view_init_hsplit(view, begin_y);
5918 diff_view->focussed = 1;
5919 diff_view->mode = view->mode;
5920 diff_view->nlines = view->lines - begin_y;
5921 if (view_is_parent_view(view)) {
5922 view_transfer_size(diff_view, view);
5923 err = view_close_child(view);
5926 err = view_set_child(view, diff_view);
5929 view->focus_child = 1;
5931 *new_view = diff_view;
5944 if (s->last_displayed_line >= s->blame.nlines &&
5945 s->selected_line >= MIN(s->blame.nlines,
5946 view->nlines - 2)) {
5950 if (s->last_displayed_line >= s->blame.nlines &&
5951 s->selected_line < view->nlines - 2) {
5953 MIN(nscroll, s->last_displayed_line -
5954 s->first_displayed_line - s->selected_line + 1);
5956 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5957 s->first_displayed_line += nscroll;
5959 s->first_displayed_line =
5960 s->blame.nlines - (view->nlines - 3);
5963 if (s->selected_line > view->nlines - 2) {
5964 s->selected_line = MIN(s->blame.nlines,
5972 return thread_err ? thread_err : err;
5975 static const struct got_error *
5976 reset_blame_view(struct tog_view *view)
5978 const struct got_error *err;
5979 struct tog_blame_view_state *s = &view->state.blame;
5983 err = stop_blame(&s->blame);
5987 return run_blame(view);
5990 static const struct got_error *
5991 cmd_blame(int argc, char *argv[])
5993 const struct got_error *error;
5994 struct got_repository *repo = NULL;
5995 struct got_worktree *worktree = NULL;
5996 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5997 char *link_target = NULL;
5998 struct got_object_id *commit_id = NULL;
5999 struct got_commit_object *commit = NULL;
6000 char *commit_id_str = NULL;
6002 struct tog_view *view;
6003 int *pack_fds = NULL;
6005 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6008 commit_id_str = optarg;
6011 repo_path = realpath(optarg, NULL);
6012 if (repo_path == NULL)
6013 return got_error_from_errno2("realpath",
6028 error = got_repo_pack_fds_open(&pack_fds);
6032 if (repo_path == NULL) {
6033 cwd = getcwd(NULL, 0);
6035 return got_error_from_errno("getcwd");
6036 error = got_worktree_open(&worktree, cwd);
6037 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6041 strdup(got_worktree_get_repo_path(worktree));
6043 repo_path = strdup(cwd);
6044 if (repo_path == NULL) {
6045 error = got_error_from_errno("strdup");
6050 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6054 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6061 error = apply_unveil(got_repo_get_path(repo), NULL);
6065 error = tog_load_refs(repo, 0);
6069 if (commit_id_str == NULL) {
6070 struct got_reference *head_ref;
6071 error = got_ref_open(&head_ref, repo, worktree ?
6072 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6075 error = got_ref_resolve(&commit_id, repo, head_ref);
6076 got_ref_close(head_ref);
6078 error = got_repo_match_object_id(&commit_id, NULL,
6079 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6084 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6086 error = got_error_from_errno("view_open");
6090 error = got_object_open_as_commit(&commit, repo, commit_id);
6094 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6099 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6104 /* Release work tree lock. */
6105 got_worktree_close(worktree);
6108 error = view_loop(view);
6116 got_object_commit_close(commit);
6118 got_worktree_close(worktree);
6120 const struct got_error *close_err = got_repo_close(repo);
6125 const struct got_error *pack_err =
6126 got_repo_pack_fds_close(pack_fds);
6134 static const struct got_error *
6135 draw_tree_entries(struct tog_view *view, const char *parent_path)
6137 struct tog_tree_view_state *s = &view->state.tree;
6138 const struct got_error *err = NULL;
6139 struct got_tree_entry *te;
6141 struct tog_color *tc;
6142 int width, n, i, nentries;
6143 int limit = view->nlines;
6146 if (view_is_hsplit_top(view))
6147 --limit; /* border */
6149 werase(view->window);
6154 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6158 if (view_needs_focus_indication(view))
6159 wstandout(view->window);
6160 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6162 wattr_on(view->window,
6163 COLOR_PAIR(tc->colorpair), NULL);
6164 waddwstr(view->window, wline);
6166 wattr_off(view->window,
6167 COLOR_PAIR(tc->colorpair), NULL);
6168 if (view_needs_focus_indication(view))
6169 wstandend(view->window);
6172 if (width < view->ncols - 1)
6173 waddch(view->window, '\n');
6176 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6180 waddwstr(view->window, wline);
6183 if (width < view->ncols - 1)
6184 waddch(view->window, '\n');
6187 waddch(view->window, '\n');
6191 if (s->first_displayed_entry == NULL) {
6192 te = got_object_tree_get_first_entry(s->tree);
6193 if (s->selected == 0) {
6195 wstandout(view->window);
6196 s->selected_entry = NULL;
6198 waddstr(view->window, " ..\n"); /* parent directory */
6199 if (s->selected == 0 && view->focussed)
6200 wstandend(view->window);
6207 te = s->first_displayed_entry;
6210 nentries = got_object_tree_get_nentries(s->tree);
6211 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6212 char *line = NULL, *id_str = NULL, *link_target = NULL;
6213 const char *modestr = "";
6216 te = got_object_tree_get_entry(s->tree, i);
6217 mode = got_tree_entry_get_mode(te);
6220 err = got_object_id_str(&id_str,
6221 got_tree_entry_get_id(te));
6223 return got_error_from_errno(
6224 "got_object_id_str");
6226 if (got_object_tree_entry_is_submodule(te))
6228 else if (S_ISLNK(mode)) {
6231 err = got_tree_entry_get_symlink_target(&link_target,
6237 for (i = 0; i < strlen(link_target); i++) {
6238 if (!isprint((unsigned char)link_target[i]))
6239 link_target[i] = '?';
6243 else if (S_ISDIR(mode))
6245 else if (mode & S_IXUSR)
6247 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6248 got_tree_entry_get_name(te), modestr,
6249 link_target ? " -> ": "",
6250 link_target ? link_target : "") == -1) {
6253 return got_error_from_errno("asprintf");
6257 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6263 if (n == s->selected) {
6265 wstandout(view->window);
6266 s->selected_entry = te;
6268 tc = match_color(&s->colors, line);
6270 wattr_on(view->window,
6271 COLOR_PAIR(tc->colorpair), NULL);
6272 waddwstr(view->window, wline);
6274 wattr_off(view->window,
6275 COLOR_PAIR(tc->colorpair), NULL);
6276 if (width < view->ncols - 1)
6277 waddch(view->window, '\n');
6278 if (n == s->selected && view->focussed)
6279 wstandend(view->window);
6285 s->last_displayed_entry = te;
6294 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6296 struct got_tree_entry *te;
6297 int isroot = s->tree == s->root;
6300 if (s->first_displayed_entry == NULL)
6303 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6304 while (i++ < maxscroll) {
6307 s->first_displayed_entry = NULL;
6310 s->first_displayed_entry = te;
6311 te = got_tree_entry_get_prev(s->tree, te);
6315 static const struct got_error *
6316 tree_scroll_down(struct tog_view *view, int maxscroll)
6318 struct tog_tree_view_state *s = &view->state.tree;
6319 struct got_tree_entry *next, *last;
6322 if (s->first_displayed_entry)
6323 next = got_tree_entry_get_next(s->tree,
6324 s->first_displayed_entry);
6326 next = got_object_tree_get_first_entry(s->tree);
6328 last = s->last_displayed_entry;
6329 while (next && n++ < maxscroll) {
6331 last = got_tree_entry_get_next(s->tree, last);
6332 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6333 s->first_displayed_entry = next;
6334 next = got_tree_entry_get_next(s->tree, next);
6341 static const struct got_error *
6342 tree_entry_path(char **path, struct tog_parent_trees *parents,
6343 struct got_tree_entry *te)
6345 const struct got_error *err = NULL;
6346 struct tog_parent_tree *pt;
6347 size_t len = 2; /* for leading slash and NUL */
6349 TAILQ_FOREACH(pt, parents, entry)
6350 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6353 len += strlen(got_tree_entry_get_name(te));
6355 *path = calloc(1, len);
6357 return got_error_from_errno("calloc");
6360 pt = TAILQ_LAST(parents, tog_parent_trees);
6362 const char *name = got_tree_entry_get_name(pt->selected_entry);
6363 if (strlcat(*path, name, len) >= len) {
6364 err = got_error(GOT_ERR_NO_SPACE);
6367 if (strlcat(*path, "/", len) >= len) {
6368 err = got_error(GOT_ERR_NO_SPACE);
6371 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6374 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6375 err = got_error(GOT_ERR_NO_SPACE);
6387 static const struct got_error *
6388 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6389 struct got_tree_entry *te, struct tog_parent_trees *parents,
6390 struct got_object_id *commit_id, struct got_repository *repo)
6392 const struct got_error *err = NULL;
6394 struct tog_view *blame_view;
6398 err = tree_entry_path(&path, parents, te);
6402 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6403 if (blame_view == NULL) {
6404 err = got_error_from_errno("view_open");
6408 err = open_blame_view(blame_view, path, commit_id, repo);
6410 if (err->code == GOT_ERR_CANCELLED)
6412 view_close(blame_view);
6414 *new_view = blame_view;
6420 static const struct got_error *
6421 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6422 struct tog_tree_view_state *s)
6424 struct tog_view *log_view;
6425 const struct got_error *err = NULL;
6430 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6431 if (log_view == NULL)
6432 return got_error_from_errno("view_open");
6434 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6438 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6441 view_close(log_view);
6443 *new_view = log_view;
6448 static const struct got_error *
6449 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6450 const char *head_ref_name, struct got_repository *repo)
6452 const struct got_error *err = NULL;
6453 char *commit_id_str = NULL;
6454 struct tog_tree_view_state *s = &view->state.tree;
6455 struct got_commit_object *commit = NULL;
6457 TAILQ_INIT(&s->parents);
6458 STAILQ_INIT(&s->colors);
6460 s->commit_id = got_object_id_dup(commit_id);
6461 if (s->commit_id == NULL)
6462 return got_error_from_errno("got_object_id_dup");
6464 err = got_object_open_as_commit(&commit, repo, commit_id);
6469 * The root is opened here and will be closed when the view is closed.
6470 * Any visited subtrees and their path-wise parents are opened and
6473 err = got_object_open_as_tree(&s->root, repo,
6474 got_object_commit_get_tree_id(commit));
6479 err = got_object_id_str(&commit_id_str, commit_id);
6483 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6484 err = got_error_from_errno("asprintf");
6488 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6489 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6490 if (head_ref_name) {
6491 s->head_ref_name = strdup(head_ref_name);
6492 if (s->head_ref_name == NULL) {
6493 err = got_error_from_errno("strdup");
6499 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6500 err = add_color(&s->colors, "\\$$",
6501 TOG_COLOR_TREE_SUBMODULE,
6502 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6505 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6506 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6509 err = add_color(&s->colors, "/$",
6510 TOG_COLOR_TREE_DIRECTORY,
6511 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6515 err = add_color(&s->colors, "\\*$",
6516 TOG_COLOR_TREE_EXECUTABLE,
6517 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6521 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6522 get_color_value("TOG_COLOR_COMMIT"));
6527 view->show = show_tree_view;
6528 view->input = input_tree_view;
6529 view->close = close_tree_view;
6530 view->search_start = search_start_tree_view;
6531 view->search_next = search_next_tree_view;
6533 free(commit_id_str);
6535 got_object_commit_close(commit);
6537 close_tree_view(view);
6541 static const struct got_error *
6542 close_tree_view(struct tog_view *view)
6544 struct tog_tree_view_state *s = &view->state.tree;
6546 free_colors(&s->colors);
6547 free(s->tree_label);
6548 s->tree_label = NULL;
6550 s->commit_id = NULL;
6551 free(s->head_ref_name);
6552 s->head_ref_name = NULL;
6553 while (!TAILQ_EMPTY(&s->parents)) {
6554 struct tog_parent_tree *parent;
6555 parent = TAILQ_FIRST(&s->parents);
6556 TAILQ_REMOVE(&s->parents, parent, entry);
6557 if (parent->tree != s->root)
6558 got_object_tree_close(parent->tree);
6562 if (s->tree != NULL && s->tree != s->root)
6563 got_object_tree_close(s->tree);
6565 got_object_tree_close(s->root);
6569 static const struct got_error *
6570 search_start_tree_view(struct tog_view *view)
6572 struct tog_tree_view_state *s = &view->state.tree;
6574 s->matched_entry = NULL;
6579 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6581 regmatch_t regmatch;
6583 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6587 static const struct got_error *
6588 search_next_tree_view(struct tog_view *view)
6590 struct tog_tree_view_state *s = &view->state.tree;
6591 struct got_tree_entry *te = NULL;
6593 if (!view->searching) {
6594 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6598 if (s->matched_entry) {
6599 if (view->searching == TOG_SEARCH_FORWARD) {
6600 if (s->selected_entry)
6601 te = got_tree_entry_get_next(s->tree,
6604 te = got_object_tree_get_first_entry(s->tree);
6606 if (s->selected_entry == NULL)
6607 te = got_object_tree_get_last_entry(s->tree);
6609 te = got_tree_entry_get_prev(s->tree,
6613 if (s->selected_entry)
6614 te = s->selected_entry;
6615 else if (view->searching == TOG_SEARCH_FORWARD)
6616 te = got_object_tree_get_first_entry(s->tree);
6618 te = got_object_tree_get_last_entry(s->tree);
6623 if (s->matched_entry == NULL) {
6624 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6627 if (view->searching == TOG_SEARCH_FORWARD)
6628 te = got_object_tree_get_first_entry(s->tree);
6630 te = got_object_tree_get_last_entry(s->tree);
6633 if (match_tree_entry(te, &view->regex)) {
6634 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6635 s->matched_entry = te;
6639 if (view->searching == TOG_SEARCH_FORWARD)
6640 te = got_tree_entry_get_next(s->tree, te);
6642 te = got_tree_entry_get_prev(s->tree, te);
6645 if (s->matched_entry) {
6646 s->first_displayed_entry = s->matched_entry;
6653 static const struct got_error *
6654 show_tree_view(struct tog_view *view)
6656 const struct got_error *err = NULL;
6657 struct tog_tree_view_state *s = &view->state.tree;
6660 err = tree_entry_path(&parent_path, &s->parents, NULL);
6664 err = draw_tree_entries(view, parent_path);
6671 static const struct got_error *
6672 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6674 const struct got_error *err = NULL;
6675 struct tog_tree_view_state *s = &view->state.tree;
6676 struct tog_view *log_view, *ref_view;
6677 struct got_tree_entry *te;
6678 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6682 s->show_ids = !s->show_ids;
6687 if (!s->selected_entry)
6689 if (view_is_parent_view(view))
6690 view_get_split(view, &begin_y, &begin_x);
6691 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6692 if (view_is_parent_view(view) &&
6693 view->mode == TOG_VIEW_SPLIT_HRZN) {
6694 err = view_init_hsplit(view, begin_y);
6699 log_view->focussed = 1;
6700 log_view->mode = view->mode;
6701 log_view->nlines = view->lines - begin_y;
6702 if (view_is_parent_view(view)) {
6703 view_transfer_size(log_view, view);
6704 err = view_close_child(view);
6707 err = view_set_child(view, log_view);
6710 view->focus_child = 1;
6712 *new_view = log_view;
6716 if (view_is_parent_view(view))
6717 view_get_split(view, &begin_y, &begin_x);
6718 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6719 if (ref_view == NULL)
6720 return got_error_from_errno("view_open");
6721 err = open_ref_view(ref_view, s->repo);
6723 view_close(ref_view);
6726 if (view_is_parent_view(view) &&
6727 view->mode == TOG_VIEW_SPLIT_HRZN) {
6728 err = view_init_hsplit(view, begin_y);
6733 ref_view->focussed = 1;
6734 ref_view->mode = view->mode;
6735 ref_view->nlines = view->lines - begin_y;
6736 if (view_is_parent_view(view)) {
6737 view_transfer_size(ref_view, view);
6738 err = view_close_child(view);
6741 err = view_set_child(view, ref_view);
6744 view->focus_child = 1;
6746 *new_view = ref_view;
6752 if (s->tree == s->root)
6753 s->first_displayed_entry =
6754 got_object_tree_get_first_entry(s->tree);
6756 s->first_displayed_entry = NULL;
6760 int eos = view->nlines - 3;
6762 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6766 te = got_object_tree_get_last_entry(s->tree);
6767 for (n = 0; n < eos; n++) {
6769 if(s->tree != s->root) {
6770 s->first_displayed_entry = NULL;
6775 s->first_displayed_entry = te;
6776 te = got_tree_entry_get_prev(s->tree, te);
6779 s->selected = n - 1;
6785 if (s->selected > 0) {
6789 tree_scroll_up(s, 1);
6790 if (s->selected_entry == NULL ||
6791 (s->tree == s->root && s->selected_entry ==
6792 got_object_tree_get_first_entry(s->tree)))
6802 if (s->tree == s->root) {
6803 if (got_object_tree_get_first_entry(s->tree) ==
6804 s->first_displayed_entry)
6805 s->selected -= MIN(s->selected, nscroll);
6807 if (s->first_displayed_entry == NULL)
6808 s->selected -= MIN(s->selected, nscroll);
6810 tree_scroll_up(s, MAX(0, nscroll));
6811 if (s->selected_entry == NULL ||
6812 (s->tree == s->root && s->selected_entry ==
6813 got_object_tree_get_first_entry(s->tree)))
6819 if (s->selected < s->ndisplayed - 1) {
6823 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6825 /* can't scroll any further */
6829 tree_scroll_down(view, 1);
6839 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6841 /* can't scroll any further; move cursor down */
6842 if (s->selected < s->ndisplayed - 1)
6843 s->selected += MIN(nscroll,
6844 s->ndisplayed - s->selected - 1);
6849 tree_scroll_down(view, nscroll);
6854 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6855 struct tog_parent_tree *parent;
6856 /* user selected '..' */
6857 if (s->tree == s->root) {
6861 parent = TAILQ_FIRST(&s->parents);
6862 TAILQ_REMOVE(&s->parents, parent,
6864 got_object_tree_close(s->tree);
6865 s->tree = parent->tree;
6866 s->first_displayed_entry =
6867 parent->first_displayed_entry;
6869 parent->selected_entry;
6870 s->selected = parent->selected;
6871 if (s->selected > view->nlines - 3) {
6872 err = offset_selection_down(view);
6877 } else if (S_ISDIR(got_tree_entry_get_mode(
6878 s->selected_entry))) {
6879 struct got_tree_object *subtree;
6881 err = got_object_open_as_tree(&subtree, s->repo,
6882 got_tree_entry_get_id(s->selected_entry));
6885 err = tree_view_visit_subtree(s, subtree);
6887 got_object_tree_close(subtree);
6890 } else if (S_ISREG(got_tree_entry_get_mode(
6891 s->selected_entry))) {
6892 struct tog_view *blame_view;
6893 int begin_x = 0, begin_y = 0;
6895 if (view_is_parent_view(view))
6896 view_get_split(view, &begin_y, &begin_x);
6898 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6899 s->selected_entry, &s->parents,
6900 s->commit_id, s->repo);
6904 if (view_is_parent_view(view) &&
6905 view->mode == TOG_VIEW_SPLIT_HRZN) {
6906 err = view_init_hsplit(view, begin_y);
6913 blame_view->focussed = 1;
6914 blame_view->mode = view->mode;
6915 blame_view->nlines = view->lines - begin_y;
6916 if (view_is_parent_view(view)) {
6917 view_transfer_size(blame_view, view);
6918 err = view_close_child(view);
6921 err = view_set_child(view, blame_view);
6924 view->focus_child = 1;
6926 *new_view = blame_view;
6930 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6931 s->selected = view->nlines - 4;
6947 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6952 static const struct got_error *
6953 cmd_tree(int argc, char *argv[])
6955 const struct got_error *error;
6956 struct got_repository *repo = NULL;
6957 struct got_worktree *worktree = NULL;
6958 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6959 struct got_object_id *commit_id = NULL;
6960 struct got_commit_object *commit = NULL;
6961 const char *commit_id_arg = NULL;
6963 struct got_reference *ref = NULL;
6964 const char *head_ref_name = NULL;
6966 struct tog_view *view;
6967 int *pack_fds = NULL;
6969 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6972 commit_id_arg = optarg;
6975 repo_path = realpath(optarg, NULL);
6976 if (repo_path == NULL)
6977 return got_error_from_errno2("realpath",
6992 error = got_repo_pack_fds_open(&pack_fds);
6996 if (repo_path == NULL) {
6997 cwd = getcwd(NULL, 0);
6999 return got_error_from_errno("getcwd");
7000 error = got_worktree_open(&worktree, cwd);
7001 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7005 strdup(got_worktree_get_repo_path(worktree));
7007 repo_path = strdup(cwd);
7008 if (repo_path == NULL) {
7009 error = got_error_from_errno("strdup");
7014 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7018 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7025 error = apply_unveil(got_repo_get_path(repo), NULL);
7029 error = tog_load_refs(repo, 0);
7033 if (commit_id_arg == NULL) {
7034 error = got_repo_match_object_id(&commit_id, &label,
7035 worktree ? got_worktree_get_head_ref_name(worktree) :
7036 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7039 head_ref_name = label;
7041 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7043 head_ref_name = got_ref_get_name(ref);
7044 else if (error->code != GOT_ERR_NOT_REF)
7046 error = got_repo_match_object_id(&commit_id, NULL,
7047 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7052 error = got_object_open_as_commit(&commit, repo, commit_id);
7056 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7058 error = got_error_from_errno("view_open");
7061 error = open_tree_view(view, commit_id, head_ref_name, repo);
7064 if (!got_path_is_root_dir(in_repo_path)) {
7065 error = tree_view_walk_path(&view->state.tree, commit,
7072 /* Release work tree lock. */
7073 got_worktree_close(worktree);
7076 error = view_loop(view);
7085 const struct got_error *close_err = got_repo_close(repo);
7090 const struct got_error *pack_err =
7091 got_repo_pack_fds_close(pack_fds);
7099 static const struct got_error *
7100 ref_view_load_refs(struct tog_ref_view_state *s)
7102 struct got_reflist_entry *sre;
7103 struct tog_reflist_entry *re;
7106 TAILQ_FOREACH(sre, &tog_refs, entry) {
7107 if (strncmp(got_ref_get_name(sre->ref),
7108 "refs/got/", 9) == 0 &&
7109 strncmp(got_ref_get_name(sre->ref),
7110 "refs/got/backup/", 16) != 0)
7113 re = malloc(sizeof(*re));
7115 return got_error_from_errno("malloc");
7117 re->ref = got_ref_dup(sre->ref);
7118 if (re->ref == NULL)
7119 return got_error_from_errno("got_ref_dup");
7120 re->idx = s->nrefs++;
7121 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7124 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7129 ref_view_free_refs(struct tog_ref_view_state *s)
7131 struct tog_reflist_entry *re;
7133 while (!TAILQ_EMPTY(&s->refs)) {
7134 re = TAILQ_FIRST(&s->refs);
7135 TAILQ_REMOVE(&s->refs, re, entry);
7136 got_ref_close(re->ref);
7141 static const struct got_error *
7142 open_ref_view(struct tog_view *view, struct got_repository *repo)
7144 const struct got_error *err = NULL;
7145 struct tog_ref_view_state *s = &view->state.ref;
7147 s->selected_entry = 0;
7150 TAILQ_INIT(&s->refs);
7151 STAILQ_INIT(&s->colors);
7153 err = ref_view_load_refs(s);
7157 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7158 err = add_color(&s->colors, "^refs/heads/",
7159 TOG_COLOR_REFS_HEADS,
7160 get_color_value("TOG_COLOR_REFS_HEADS"));
7164 err = add_color(&s->colors, "^refs/tags/",
7165 TOG_COLOR_REFS_TAGS,
7166 get_color_value("TOG_COLOR_REFS_TAGS"));
7170 err = add_color(&s->colors, "^refs/remotes/",
7171 TOG_COLOR_REFS_REMOTES,
7172 get_color_value("TOG_COLOR_REFS_REMOTES"));
7176 err = add_color(&s->colors, "^refs/got/backup/",
7177 TOG_COLOR_REFS_BACKUP,
7178 get_color_value("TOG_COLOR_REFS_BACKUP"));
7183 view->show = show_ref_view;
7184 view->input = input_ref_view;
7185 view->close = close_ref_view;
7186 view->search_start = search_start_ref_view;
7187 view->search_next = search_next_ref_view;
7190 free_colors(&s->colors);
7194 static const struct got_error *
7195 close_ref_view(struct tog_view *view)
7197 struct tog_ref_view_state *s = &view->state.ref;
7199 ref_view_free_refs(s);
7200 free_colors(&s->colors);
7205 static const struct got_error *
7206 resolve_reflist_entry(struct got_object_id **commit_id,
7207 struct tog_reflist_entry *re, struct got_repository *repo)
7209 const struct got_error *err = NULL;
7210 struct got_object_id *obj_id;
7211 struct got_tag_object *tag = NULL;
7216 err = got_ref_resolve(&obj_id, repo, re->ref);
7220 err = got_object_get_type(&obj_type, repo, obj_id);
7225 case GOT_OBJ_TYPE_COMMIT:
7226 *commit_id = obj_id;
7228 case GOT_OBJ_TYPE_TAG:
7229 err = got_object_open_as_tag(&tag, repo, obj_id);
7233 err = got_object_get_type(&obj_type, repo,
7234 got_object_tag_get_object_id(tag));
7237 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7238 err = got_error(GOT_ERR_OBJ_TYPE);
7241 *commit_id = got_object_id_dup(
7242 got_object_tag_get_object_id(tag));
7243 if (*commit_id == NULL) {
7244 err = got_error_from_errno("got_object_id_dup");
7249 err = got_error(GOT_ERR_OBJ_TYPE);
7255 got_object_tag_close(tag);
7263 static const struct got_error *
7264 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7265 struct tog_reflist_entry *re, struct got_repository *repo)
7267 struct tog_view *log_view;
7268 const struct got_error *err = NULL;
7269 struct got_object_id *commit_id = NULL;
7273 err = resolve_reflist_entry(&commit_id, re, repo);
7275 if (err->code != GOT_ERR_OBJ_TYPE)
7281 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7282 if (log_view == NULL) {
7283 err = got_error_from_errno("view_open");
7287 err = open_log_view(log_view, commit_id, repo,
7288 got_ref_get_name(re->ref), "", 0);
7291 view_close(log_view);
7293 *new_view = log_view;
7299 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7301 struct tog_reflist_entry *re;
7304 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7307 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7308 while (i++ < maxscroll) {
7311 s->first_displayed_entry = re;
7312 re = TAILQ_PREV(re, tog_reflist_head, entry);
7316 static const struct got_error *
7317 ref_scroll_down(struct tog_view *view, int maxscroll)
7319 struct tog_ref_view_state *s = &view->state.ref;
7320 struct tog_reflist_entry *next, *last;
7323 if (s->first_displayed_entry)
7324 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7326 next = TAILQ_FIRST(&s->refs);
7328 last = s->last_displayed_entry;
7329 while (next && n++ < maxscroll) {
7331 last = TAILQ_NEXT(last, entry);
7332 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7333 s->first_displayed_entry = next;
7334 next = TAILQ_NEXT(next, entry);
7341 static const struct got_error *
7342 search_start_ref_view(struct tog_view *view)
7344 struct tog_ref_view_state *s = &view->state.ref;
7346 s->matched_entry = NULL;
7351 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7353 regmatch_t regmatch;
7355 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7359 static const struct got_error *
7360 search_next_ref_view(struct tog_view *view)
7362 struct tog_ref_view_state *s = &view->state.ref;
7363 struct tog_reflist_entry *re = NULL;
7365 if (!view->searching) {
7366 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7370 if (s->matched_entry) {
7371 if (view->searching == TOG_SEARCH_FORWARD) {
7372 if (s->selected_entry)
7373 re = TAILQ_NEXT(s->selected_entry, entry);
7375 re = TAILQ_PREV(s->selected_entry,
7376 tog_reflist_head, entry);
7378 if (s->selected_entry == NULL)
7379 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7381 re = TAILQ_PREV(s->selected_entry,
7382 tog_reflist_head, entry);
7385 if (s->selected_entry)
7386 re = s->selected_entry;
7387 else if (view->searching == TOG_SEARCH_FORWARD)
7388 re = TAILQ_FIRST(&s->refs);
7390 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7395 if (s->matched_entry == NULL) {
7396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7399 if (view->searching == TOG_SEARCH_FORWARD)
7400 re = TAILQ_FIRST(&s->refs);
7402 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7405 if (match_reflist_entry(re, &view->regex)) {
7406 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7407 s->matched_entry = re;
7411 if (view->searching == TOG_SEARCH_FORWARD)
7412 re = TAILQ_NEXT(re, entry);
7414 re = TAILQ_PREV(re, tog_reflist_head, entry);
7417 if (s->matched_entry) {
7418 s->first_displayed_entry = s->matched_entry;
7425 static const struct got_error *
7426 show_ref_view(struct tog_view *view)
7428 const struct got_error *err = NULL;
7429 struct tog_ref_view_state *s = &view->state.ref;
7430 struct tog_reflist_entry *re;
7433 struct tog_color *tc;
7435 int limit = view->nlines;
7437 werase(view->window);
7440 if (view_is_hsplit_top(view))
7441 --limit; /* border */
7446 re = s->first_displayed_entry;
7448 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7450 return got_error_from_errno("asprintf");
7452 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7457 if (view_needs_focus_indication(view))
7458 wstandout(view->window);
7459 waddwstr(view->window, wline);
7460 if (view_needs_focus_indication(view))
7461 wstandend(view->window);
7466 if (width < view->ncols - 1)
7467 waddch(view->window, '\n');
7472 while (re && limit > 0) {
7474 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7477 struct got_commit_object *ci;
7478 struct got_tag_object *tag;
7479 struct got_object_id *id;
7483 err = got_ref_resolve(&id, s->repo, re->ref);
7486 err = got_object_open_as_tag(&tag, s->repo, id);
7488 if (err->code != GOT_ERR_OBJ_TYPE) {
7492 err = got_object_open_as_commit(&ci, s->repo,
7498 t = got_object_commit_get_committer_time(ci);
7499 got_object_commit_close(ci);
7501 t = got_object_tag_get_tagger_time(tag);
7502 got_object_tag_close(tag);
7505 if (gmtime_r(&t, &tm) == NULL)
7506 return got_error_from_errno("gmtime_r");
7507 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7508 return got_error(GOT_ERR_NO_SPACE);
7510 if (got_ref_is_symbolic(re->ref)) {
7511 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7512 ymd : "", got_ref_get_name(re->ref),
7513 got_ref_get_symref_target(re->ref)) == -1)
7514 return got_error_from_errno("asprintf");
7515 } else if (s->show_ids) {
7516 struct got_object_id *id;
7518 err = got_ref_resolve(&id, s->repo, re->ref);
7521 err = got_object_id_str(&id_str, id);
7526 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7527 got_ref_get_name(re->ref), id_str) == -1) {
7528 err = got_error_from_errno("asprintf");
7535 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7536 got_ref_get_name(re->ref)) == -1)
7537 return got_error_from_errno("asprintf");
7539 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7545 if (n == s->selected) {
7547 wstandout(view->window);
7548 s->selected_entry = re;
7550 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7552 wattr_on(view->window,
7553 COLOR_PAIR(tc->colorpair), NULL);
7554 waddwstr(view->window, wline);
7556 wattr_off(view->window,
7557 COLOR_PAIR(tc->colorpair), NULL);
7558 if (width < view->ncols - 1)
7559 waddch(view->window, '\n');
7560 if (n == s->selected && view->focussed)
7561 wstandend(view->window);
7567 s->last_displayed_entry = re;
7570 re = TAILQ_NEXT(re, entry);
7577 static const struct got_error *
7578 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7579 struct tog_reflist_entry *re, struct got_repository *repo)
7581 const struct got_error *err = NULL;
7582 struct got_object_id *commit_id = NULL;
7583 struct tog_view *tree_view;
7587 err = resolve_reflist_entry(&commit_id, re, repo);
7589 if (err->code != GOT_ERR_OBJ_TYPE)
7596 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7597 if (tree_view == NULL) {
7598 err = got_error_from_errno("view_open");
7602 err = open_tree_view(tree_view, commit_id,
7603 got_ref_get_name(re->ref), repo);
7607 *new_view = tree_view;
7612 static const struct got_error *
7613 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7615 const struct got_error *err = NULL;
7616 struct tog_ref_view_state *s = &view->state.ref;
7617 struct tog_view *log_view, *tree_view;
7618 struct tog_reflist_entry *re;
7619 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7623 s->show_ids = !s->show_ids;
7627 s->show_date = !s->show_date;
7631 s->sort_by_date = !s->sort_by_date;
7633 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7634 got_ref_cmp_by_commit_timestamp_descending :
7635 tog_ref_cmp_by_name, s->repo);
7638 got_reflist_object_id_map_free(tog_refs_idmap);
7639 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7640 &tog_refs, s->repo);
7643 ref_view_free_refs(s);
7644 err = ref_view_load_refs(s);
7649 if (!s->selected_entry)
7651 if (view_is_parent_view(view))
7652 view_get_split(view, &begin_y, &begin_x);
7654 err = log_ref_entry(&log_view, begin_y, begin_x,
7655 s->selected_entry, s->repo);
7659 if (view_is_parent_view(view) &&
7660 view->mode == TOG_VIEW_SPLIT_HRZN) {
7661 err = view_init_hsplit(view, begin_y);
7667 log_view->focussed = 1;
7668 log_view->mode = view->mode;
7669 log_view->nlines = view->lines - begin_y;
7670 if (view_is_parent_view(view)) {
7671 view_transfer_size(log_view, view);
7672 err = view_close_child(view);
7675 err = view_set_child(view, log_view);
7678 view->focus_child = 1;
7680 *new_view = log_view;
7684 if (!s->selected_entry)
7686 if (view_is_parent_view(view))
7687 view_get_split(view, &begin_y, &begin_x);
7688 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7689 s->selected_entry, s->repo);
7690 if (err || tree_view == NULL)
7692 if (view_is_parent_view(view) &&
7693 view->mode == TOG_VIEW_SPLIT_HRZN) {
7694 err = view_init_hsplit(view, begin_y);
7699 tree_view->focussed = 1;
7700 tree_view->mode = view->mode;
7701 tree_view->nlines = view->lines - begin_y;
7702 if (view_is_parent_view(view)) {
7703 view_transfer_size(tree_view, view);
7704 err = view_close_child(view);
7707 err = view_set_child(view, tree_view);
7710 view->focus_child = 1;
7712 *new_view = tree_view;
7718 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7722 int eos = view->nlines - 1;
7724 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7728 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7729 for (n = 0; n < eos; n++) {
7732 s->first_displayed_entry = re;
7733 re = TAILQ_PREV(re, tog_reflist_head, entry);
7736 s->selected = n - 1;
7742 if (s->selected > 0) {
7746 ref_scroll_up(s, 1);
7747 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7757 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7758 s->selected -= MIN(nscroll, s->selected);
7759 ref_scroll_up(s, MAX(0, nscroll));
7760 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7766 if (s->selected < s->ndisplayed - 1) {
7770 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7771 /* can't scroll any further */
7775 ref_scroll_down(view, 1);
7785 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7786 /* can't scroll any further; move cursor down */
7787 if (s->selected < s->ndisplayed - 1)
7788 s->selected += MIN(nscroll,
7789 s->ndisplayed - s->selected - 1);
7790 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7791 s->selected += s->ndisplayed - s->selected - 1;
7795 ref_scroll_down(view, nscroll);
7800 err = tog_load_refs(s->repo, s->sort_by_date);
7803 ref_view_free_refs(s);
7804 err = ref_view_load_refs(s);
7807 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7808 s->selected = view->nlines - 2;
7822 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7827 static const struct got_error *
7828 cmd_ref(int argc, char *argv[])
7830 const struct got_error *error;
7831 struct got_repository *repo = NULL;
7832 struct got_worktree *worktree = NULL;
7833 char *cwd = NULL, *repo_path = NULL;
7835 struct tog_view *view;
7836 int *pack_fds = NULL;
7838 while ((ch = getopt(argc, argv, "r:")) != -1) {
7841 repo_path = realpath(optarg, NULL);
7842 if (repo_path == NULL)
7843 return got_error_from_errno2("realpath",
7858 error = got_repo_pack_fds_open(&pack_fds);
7862 if (repo_path == NULL) {
7863 cwd = getcwd(NULL, 0);
7865 return got_error_from_errno("getcwd");
7866 error = got_worktree_open(&worktree, cwd);
7867 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7871 strdup(got_worktree_get_repo_path(worktree));
7873 repo_path = strdup(cwd);
7874 if (repo_path == NULL) {
7875 error = got_error_from_errno("strdup");
7880 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7886 error = apply_unveil(got_repo_get_path(repo), NULL);
7890 error = tog_load_refs(repo, 0);
7894 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7896 error = got_error_from_errno("view_open");
7900 error = open_ref_view(view, repo);
7905 /* Release work tree lock. */
7906 got_worktree_close(worktree);
7909 error = view_loop(view);
7914 const struct got_error *close_err = got_repo_close(repo);
7919 const struct got_error *pack_err =
7920 got_repo_pack_fds_close(pack_fds);
7929 * If view was scrolled down to move the selected line into view when opening a
7930 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7933 offset_selection_up(struct tog_view *view)
7935 switch (view->type) {
7936 case TOG_VIEW_BLAME: {
7937 struct tog_blame_view_state *s = &view->state.blame;
7938 if (s->first_displayed_line == 1) {
7939 s->selected_line = MAX(s->selected_line - view->offset,
7943 if (s->first_displayed_line > view->offset)
7944 s->first_displayed_line -= view->offset;
7946 s->first_displayed_line = 1;
7947 s->selected_line += view->offset;
7951 log_scroll_up(&view->state.log, view->offset);
7952 view->state.log.selected += view->offset;
7955 ref_scroll_up(&view->state.ref, view->offset);
7956 view->state.ref.selected += view->offset;
7959 tree_scroll_up(&view->state.tree, view->offset);
7960 view->state.tree.selected += view->offset;
7970 * If the selected line is in the section of screen covered by the bottom split,
7971 * scroll down offset lines to move it into view and index its new position.
7973 static const struct got_error *
7974 offset_selection_down(struct tog_view *view)
7976 const struct got_error *err = NULL;
7977 const struct got_error *(*scrolld)(struct tog_view *, int);
7978 int *selected = NULL;
7981 switch (view->type) {
7982 case TOG_VIEW_BLAME: {
7983 struct tog_blame_view_state *s = &view->state.blame;
7986 if (s->selected_line > view->nlines - header) {
7987 offset = abs(view->nlines - s->selected_line - header);
7988 s->first_displayed_line += offset;
7989 s->selected_line -= offset;
7990 view->offset = offset;
7994 case TOG_VIEW_LOG: {
7995 struct tog_log_view_state *s = &view->state.log;
7996 scrolld = &log_scroll_down;
7997 header = view_is_parent_view(view) ? 3 : 2;
7998 selected = &s->selected;
8001 case TOG_VIEW_REF: {
8002 struct tog_ref_view_state *s = &view->state.ref;
8003 scrolld = &ref_scroll_down;
8005 selected = &s->selected;
8008 case TOG_VIEW_TREE: {
8009 struct tog_tree_view_state *s = &view->state.tree;
8010 scrolld = &tree_scroll_down;
8012 selected = &s->selected;
8022 if (selected && *selected > view->nlines - header) {
8023 offset = abs(view->nlines - *selected - header);
8024 view->offset = offset;
8025 if (scrolld && offset) {
8026 err = scrolld(view, offset);
8027 *selected -= offset;
8035 list_commands(FILE *fp)
8039 fprintf(fp, "commands:");
8040 for (i = 0; i < nitems(tog_commands); i++) {
8041 const struct tog_cmd *cmd = &tog_commands[i];
8042 fprintf(fp, " %s", cmd->name);
8048 usage(int hflag, int status)
8050 FILE *fp = (status == 0) ? stdout : stderr;
8052 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8055 fprintf(fp, "lazy usage: %s path\n", getprogname());
8062 make_argv(int argc, ...)
8070 argv = calloc(argc, sizeof(char *));
8073 for (i = 0; i < argc; i++) {
8074 argv[i] = strdup(va_arg(ap, char *));
8075 if (argv[i] == NULL)
8084 * Try to convert 'tog path' into a 'tog log path' command.
8085 * The user could simply have mistyped the command rather than knowingly
8086 * provided a path. So check whether argv[0] can in fact be resolved
8087 * to a path in the HEAD commit and print a special error if not.
8088 * This hack is for mpi@ <3
8090 static const struct got_error *
8091 tog_log_with_path(int argc, char *argv[])
8093 const struct got_error *error = NULL, *close_err;
8094 const struct tog_cmd *cmd = NULL;
8095 struct got_repository *repo = NULL;
8096 struct got_worktree *worktree = NULL;
8097 struct got_object_id *commit_id = NULL, *id = NULL;
8098 struct got_commit_object *commit = NULL;
8099 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8100 char *commit_id_str = NULL, **cmd_argv = NULL;
8101 int *pack_fds = NULL;
8103 cwd = getcwd(NULL, 0);
8105 return got_error_from_errno("getcwd");
8107 error = got_repo_pack_fds_open(&pack_fds);
8111 error = got_worktree_open(&worktree, cwd);
8112 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8116 repo_path = strdup(got_worktree_get_repo_path(worktree));
8118 repo_path = strdup(cwd);
8119 if (repo_path == NULL) {
8120 error = got_error_from_errno("strdup");
8124 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8128 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8133 error = tog_load_refs(repo, 0);
8136 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8137 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8138 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8143 got_worktree_close(worktree);
8147 error = got_object_open_as_commit(&commit, repo, commit_id);
8151 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8153 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8155 fprintf(stderr, "%s: '%s' is no known command or path\n",
8156 getprogname(), argv[0]);
8161 close_err = got_repo_close(repo);
8166 error = got_object_id_str(&commit_id_str, commit_id);
8170 cmd = &tog_commands[0]; /* log */
8172 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8173 error = cmd->cmd_main(argc, cmd_argv);
8176 close_err = got_repo_close(repo);
8181 got_object_commit_close(commit);
8183 got_worktree_close(worktree);
8185 const struct got_error *pack_err =
8186 got_repo_pack_fds_close(pack_fds);
8191 free(commit_id_str);
8198 for (i = 0; i < argc; i++)
8207 main(int argc, char *argv[])
8209 const struct got_error *error = NULL;
8210 const struct tog_cmd *cmd = NULL;
8211 int ch, hflag = 0, Vflag = 0;
8212 char **cmd_argv = NULL;
8213 static const struct option longopts[] = {
8214 { "version", no_argument, NULL, 'V' },
8217 char *diff_algo_str = NULL;
8219 setlocale(LC_CTYPE, "");
8221 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8241 got_version_print_str();
8246 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8254 /* Build an argument vector which runs a default command. */
8255 cmd = &tog_commands[0];
8257 cmd_argv = make_argv(argc, cmd->name);
8261 /* Did the user specify a command? */
8262 for (i = 0; i < nitems(tog_commands); i++) {
8263 if (strncmp(tog_commands[i].name, argv[0],
8264 strlen(argv[0])) == 0) {
8265 cmd = &tog_commands[i];
8271 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8272 if (diff_algo_str) {
8273 if (strcasecmp(diff_algo_str, "patience") == 0)
8274 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8275 if (strcasecmp(diff_algo_str, "myers") == 0)
8276 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8282 /* No command specified; try log with a path */
8283 error = tog_log_with_path(argc, argv);
8288 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8295 for (i = 0; i < argc; i++)
8300 if (error && error->code != GOT_ERR_CANCELLED)
8301 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);