2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
127 struct commit_queue_head head;
131 STAILQ_ENTRY(tog_color) entry;
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
155 } else if (isbackup1 && !isbackup2) {
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 STAILQ_INSERT_HEAD(colors, tc, entry);
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
296 if (strcasecmp(val, "red") == 0)
298 if (strcasecmp(val, "green") == 0)
300 if (strcasecmp(val, "yellow") == 0)
302 if (strcasecmp(val, "blue") == 0)
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
308 if (strcasecmp(val, "white") == 0)
310 if (strcasecmp(val, "default") == 0)
313 return default_color_value(envvar);
316 struct tog_diff_view_state {
317 struct got_object_id *id1, *id2;
318 const char *label1, *label2;
322 int first_displayed_line;
323 int last_displayed_line;
326 int ignore_whitespace;
328 struct got_repository *repo;
329 struct got_diff_line *lines;
334 /* passed from log or blame view; may be NULL */
335 struct tog_view *parent_view;
338 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
339 static volatile sig_atomic_t tog_thread_error;
341 struct tog_log_thread_args {
342 pthread_cond_t need_commits;
343 pthread_cond_t commit_loaded;
346 struct got_commit_graph *graph;
347 struct commit_queue *commits;
348 const char *in_repo_path;
349 struct got_object_id *start_id;
350 struct got_repository *repo;
354 struct commit_queue_entry **first_displayed_entry;
355 struct commit_queue_entry **selected_entry;
357 int *search_next_done;
361 struct tog_log_view_state {
362 struct commit_queue commits;
363 struct commit_queue_entry *first_displayed_entry;
364 struct commit_queue_entry *last_displayed_entry;
365 struct commit_queue_entry *selected_entry;
370 struct got_repository *repo;
371 struct got_object_id *start_id;
374 struct tog_log_thread_args thread_args;
375 struct commit_queue_entry *matched_entry;
376 struct commit_queue_entry *search_entry;
377 struct tog_colors colors;
381 #define TOG_COLOR_DIFF_MINUS 1
382 #define TOG_COLOR_DIFF_PLUS 2
383 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
384 #define TOG_COLOR_DIFF_META 4
385 #define TOG_COLOR_TREE_SUBMODULE 5
386 #define TOG_COLOR_TREE_SYMLINK 6
387 #define TOG_COLOR_TREE_DIRECTORY 7
388 #define TOG_COLOR_TREE_EXECUTABLE 8
389 #define TOG_COLOR_COMMIT 9
390 #define TOG_COLOR_AUTHOR 10
391 #define TOG_COLOR_DATE 11
392 #define TOG_COLOR_REFS_HEADS 12
393 #define TOG_COLOR_REFS_TAGS 13
394 #define TOG_COLOR_REFS_REMOTES 14
395 #define TOG_COLOR_REFS_BACKUP 15
397 struct tog_blame_cb_args {
398 struct tog_blame_line *lines; /* one per line */
401 struct tog_view *view;
402 struct got_object_id *commit_id;
406 struct tog_blame_thread_args {
408 struct got_repository *repo;
409 struct tog_blame_cb_args *cb_args;
411 got_cancel_cb cancel_cb;
418 struct tog_blame_line *lines;
422 struct tog_blame_thread_args thread_args;
423 struct tog_blame_cb_args cb_args;
428 struct tog_blame_view_state {
429 int first_displayed_line;
430 int last_displayed_line;
432 int last_diffed_line;
436 struct got_object_id_queue blamed_commits;
437 struct got_object_qid *blamed_commit;
439 struct got_repository *repo;
440 struct got_object_id *commit_id;
441 struct got_object_id *id_to_log;
442 struct tog_blame blame;
444 struct tog_colors colors;
447 struct tog_parent_tree {
448 TAILQ_ENTRY(tog_parent_tree) entry;
449 struct got_tree_object *tree;
450 struct got_tree_entry *first_displayed_entry;
451 struct got_tree_entry *selected_entry;
455 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
457 struct tog_tree_view_state {
459 struct got_object_id *commit_id;/* commit which this tree belongs to */
460 struct got_tree_object *root; /* the commit's root tree entry */
461 struct got_tree_object *tree; /* currently displayed (sub-)tree */
462 struct got_tree_entry *first_displayed_entry;
463 struct got_tree_entry *last_displayed_entry;
464 struct got_tree_entry *selected_entry;
465 int ndisplayed, selected, show_ids;
466 struct tog_parent_trees parents; /* parent trees of current sub-tree */
468 struct got_repository *repo;
469 struct got_tree_entry *matched_entry;
470 struct tog_colors colors;
473 struct tog_reflist_entry {
474 TAILQ_ENTRY(tog_reflist_entry) entry;
475 struct got_reference *ref;
479 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
481 struct tog_ref_view_state {
482 struct tog_reflist_head refs;
483 struct tog_reflist_entry *first_displayed_entry;
484 struct tog_reflist_entry *last_displayed_entry;
485 struct tog_reflist_entry *selected_entry;
486 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
487 struct got_repository *repo;
488 struct tog_reflist_entry *matched_entry;
489 struct tog_colors colors;
493 * We implement two types of views: parent views and child views.
495 * The 'Tab' key switches focus between a parent view and its child view.
496 * Child views are shown side-by-side to their parent view, provided
497 * there is enough screen estate.
499 * When a new view is opened from within a parent view, this new view
500 * becomes a child view of the parent view, replacing any existing child.
502 * When a new view is opened from within a child view, this new view
503 * becomes a parent view which will obscure the views below until the
504 * user quits the new parent view by typing 'q'.
506 * This list of views contains parent views only.
507 * Child views are only pointed to by their parent view.
509 TAILQ_HEAD(tog_view_list_head, tog_view);
512 TAILQ_ENTRY(tog_view) entry;
515 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
516 int resized_y, resized_x; /* begin_y/x based on user resizing */
517 int maxx, x; /* max column and current start column */
518 int lines, cols; /* copies of LINES and COLS */
519 int nscrolled, offset; /* lines scrolled and hsplit line offset */
520 int gline, hiline; /* navigate to and highlight this nG line */
521 int ch, count; /* current keymap and count prefix */
522 int resized; /* set when in a resize event */
523 int focussed; /* Only set on one parent or child view at a time. */
525 struct tog_view *parent;
526 struct tog_view *child;
529 * This flag is initially set on parent views when a new child view
530 * is created. It gets toggled when the 'Tab' key switches focus
531 * between parent and child.
532 * The flag indicates whether focus should be passed on to our child
533 * view if this parent view gets picked for focus after another parent
534 * view was closed. This prevents child views from losing focus in such
539 enum tog_view_mode mode;
540 /* type-specific state */
541 enum tog_view_type type;
543 struct tog_diff_view_state diff;
544 struct tog_log_view_state log;
545 struct tog_blame_view_state blame;
546 struct tog_tree_view_state tree;
547 struct tog_ref_view_state ref;
550 const struct got_error *(*show)(struct tog_view *);
551 const struct got_error *(*input)(struct tog_view **,
552 struct tog_view *, int);
553 const struct got_error *(*reset)(struct tog_view *);
554 const struct got_error *(*resize)(struct tog_view *, int);
555 const struct got_error *(*close)(struct tog_view *);
557 const struct got_error *(*search_start)(struct tog_view *);
558 const struct got_error *(*search_next)(struct tog_view *);
561 #define TOG_SEARCH_FORWARD 1
562 #define TOG_SEARCH_BACKWARD 2
563 int search_next_done;
564 #define TOG_SEARCH_HAVE_MORE 1
565 #define TOG_SEARCH_NO_MORE 2
566 #define TOG_SEARCH_HAVE_NONE 3
571 static const struct got_error *open_diff_view(struct tog_view *,
572 struct got_object_id *, struct got_object_id *,
573 const char *, const char *, int, int, int, struct tog_view *,
574 struct got_repository *);
575 static const struct got_error *show_diff_view(struct tog_view *);
576 static const struct got_error *input_diff_view(struct tog_view **,
577 struct tog_view *, int);
578 static const struct got_error *reset_diff_view(struct tog_view *);
579 static const struct got_error* close_diff_view(struct tog_view *);
580 static const struct got_error *search_start_diff_view(struct tog_view *);
581 static const struct got_error *search_next_diff_view(struct tog_view *);
583 static const struct got_error *open_log_view(struct tog_view *,
584 struct got_object_id *, struct got_repository *,
585 const char *, const char *, int);
586 static const struct got_error * show_log_view(struct tog_view *);
587 static const struct got_error *input_log_view(struct tog_view **,
588 struct tog_view *, int);
589 static const struct got_error *resize_log_view(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->resized && 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->resized) {
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->resized ? view->begin_y : 0;
776 view->nlines = view->resized ? 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 * XXX This is ugly and needs to be moved into the above
883 * logic but "works" for now and my attempts at moving it
884 * break either 'tab' or 'F' key maps in horizontal splits.
887 err = view_splitscreen(view->child);
890 if (dif < 0) { /* top split decreased */
891 err = offset_selection_down(view);
898 show_panel(view->child->panel);
899 nlines = view->nlines;
901 } else if (view->parent == NULL)
904 if (view->resize && dif > 0) {
905 err = view->resize(view, dif);
910 if (wresize(view->window, nlines, ncols) == ERR)
911 return got_error_from_errno("wresize");
912 if (replace_panel(view->panel, view->window) == ERR)
913 return got_error_from_errno("replace_panel");
914 wclear(view->window);
916 view->nlines = nlines;
924 static const struct got_error *
925 resize_log_view(struct tog_view *view, int increase)
927 struct tog_log_view_state *s = &view->state.log;
928 const struct got_error *err = NULL;
931 if (s->selected_entry)
932 n = s->selected_entry->idx + view->lines - s->selected;
935 * Request commits to account for the increased
936 * height so we have enough to populate the view.
938 if (s->commits.ncommits < n) {
939 view->nscrolled = n - s->commits.ncommits + increase + 1;
940 err = request_log_commits(view);
947 view_adjust_offset(struct tog_view *view, int n)
952 if (view->parent && view->parent->offset) {
953 if (view->parent->offset + n >= 0)
954 view->parent->offset += n;
956 view->parent->offset = 0;
957 } else if (view->offset) {
958 if (view->offset - n >= 0)
965 static const struct got_error *
966 view_resize_split(struct tog_view *view, int resize)
968 const struct got_error *err = NULL;
969 struct tog_view *v = NULL;
976 if (!v->child || !view_is_splitscreen(v->child))
979 v->resized = v->child->resized = resize; /* lock for resize event */
981 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
982 if (v->child->resized_y)
983 v->child->begin_y = v->child->resized_y;
985 v->child->begin_y -= resize;
987 v->child->begin_y += resize;
988 if (v->child->begin_y < 3) {
990 v->child->begin_y = 3;
991 } else if (v->child->begin_y > LINES - 1) {
993 v->child->begin_y = LINES - 1;
996 v->child->ncols = COLS;
997 view_adjust_offset(view, resize);
998 err = view_init_hsplit(v, v->child->begin_y);
1001 v->child->resized_y = v->child->begin_y;
1003 if (v->child->resized_x)
1004 v->child->begin_x = v->child->resized_x;
1006 v->child->begin_x -= resize;
1008 v->child->begin_x += resize;
1009 if (v->child->begin_x < 11) {
1011 v->child->begin_x = 11;
1012 } else if (v->child->begin_x > COLS - 1) {
1014 v->child->begin_x = COLS - 1;
1016 v->child->resized_x = v->child->begin_x;
1019 v->child->mode = v->mode;
1020 v->child->nlines = v->lines - v->child->begin_y;
1021 v->child->ncols = v->cols - v->child->begin_x;
1024 err = view_fullscreen(v);
1027 err = view_splitscreen(v->child);
1031 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1032 err = offset_selection_down(v->child);
1038 err = v->resize(v, 0);
1039 else if (v->child->resize)
1040 err = v->child->resize(v->child, 0);
1042 v->resized = v->child->resized = 0;
1048 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1050 struct tog_view *v = src->child ? src->child : src;
1052 dst->resized_x = v->resized_x;
1053 dst->resized_y = v->resized_y;
1056 static const struct got_error *
1057 view_close_child(struct tog_view *view)
1059 const struct got_error *err = NULL;
1061 if (view->child == NULL)
1064 err = view_close(view->child);
1069 static const struct got_error *
1070 view_set_child(struct tog_view *view, struct tog_view *child)
1072 const struct got_error *err = NULL;
1074 view->child = child;
1075 child->parent = view;
1077 err = view_resize(view);
1081 if (view->child->resized_x || view->child->resized_y)
1082 err = view_resize_split(view, 0);
1087 static const struct got_error *view_dispatch_request(struct tog_view **,
1088 struct tog_view *, enum tog_view_type, int, int);
1090 static const struct got_error *
1091 view_request_new(struct tog_view **requested, struct tog_view *view,
1092 enum tog_view_type request)
1094 struct tog_view *new_view = NULL;
1095 const struct got_error *err;
1100 if (view_is_parent_view(view))
1101 view_get_split(view, &y, &x);
1103 err = view_dispatch_request(&new_view, view, request, y, x);
1107 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1108 err = view_init_hsplit(view, y);
1114 new_view->focussed = 1;
1115 new_view->mode = view->mode;
1116 new_view->nlines = view->lines - y;
1118 if (view_is_parent_view(view)) {
1119 view_transfer_size(new_view, view);
1120 err = view_close_child(view);
1123 err = view_set_child(view, new_view);
1126 view->focus_child = 1;
1128 *requested = new_view;
1134 tog_resizeterm(void)
1137 struct winsize size;
1139 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1140 cols = 80; /* Default */
1144 lines = size.ws_row;
1146 resize_term(lines, cols);
1149 static const struct got_error *
1150 view_search_start(struct tog_view *view)
1152 const struct got_error *err = NULL;
1153 struct tog_view *v = view;
1157 if (view->search_started) {
1158 regfree(&view->regex);
1159 view->searching = 0;
1160 memset(&view->regmatch, 0, sizeof(view->regmatch));
1162 view->search_started = 0;
1164 if (view->nlines < 1)
1167 if (view_is_hsplit_top(view))
1170 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1171 wclrtoeol(v->window);
1173 nodelay(view->window, FALSE); /* block for search term input */
1176 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1177 wrefresh(v->window);
1180 nodelay(view->window, TRUE);
1184 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1185 err = view->search_start(view);
1187 regfree(&view->regex);
1190 view->search_started = 1;
1191 view->searching = TOG_SEARCH_FORWARD;
1192 view->search_next_done = 0;
1193 view->search_next(view);
1199 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1200 static const struct got_error *
1201 switch_split(struct tog_view *view)
1203 const struct got_error *err = NULL;
1204 struct tog_view *v = NULL;
1211 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1212 v->mode = TOG_VIEW_SPLIT_VERT;
1214 v->mode = TOG_VIEW_SPLIT_HRZN;
1218 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1219 v->mode = TOG_VIEW_SPLIT_NONE;
1221 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1222 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1223 v->child->begin_y = v->child->resized_y;
1224 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1225 v->child->begin_x = v->child->resized_x;
1228 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1230 v->child->ncols = COLS;
1231 v->child->nscrolled = LINES - v->child->nlines;
1233 err = view_init_hsplit(v, v->child->begin_y);
1237 v->child->mode = v->mode;
1238 v->child->nlines = v->lines - v->child->begin_y;
1241 err = view_fullscreen(v);
1244 err = view_splitscreen(v->child);
1248 if (v->mode == TOG_VIEW_SPLIT_NONE)
1249 v->mode = TOG_VIEW_SPLIT_VERT;
1250 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1251 err = offset_selection_down(v);
1254 err = offset_selection_down(v->child);
1258 offset_selection_up(v);
1259 offset_selection_up(v->child);
1262 err = v->resize(v, 0);
1263 else if (v->child->resize)
1264 err = v->child->resize(v->child, 0);
1270 * Compute view->count from numeric input. Assign total to view->count and
1271 * return first non-numeric key entered.
1274 get_compound_key(struct tog_view *view, int c)
1276 struct tog_view *v = view;
1279 if (view_is_hsplit_top(view))
1281 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1285 cbreak(); /* block for input */
1286 nodelay(view->window, FALSE);
1287 wmove(v->window, v->nlines - 1, 0);
1288 wclrtoeol(v->window);
1289 waddch(v->window, ':');
1292 x = getcurx(v->window);
1293 if (x != ERR && x < view->ncols) {
1294 waddch(v->window, c);
1295 wrefresh(v->window);
1299 * Don't overflow. Max valid request should be the greatest
1300 * between the longest and total lines; cap at 10 million.
1305 n = n * 10 + (c - '0');
1306 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1308 if (c == 'G' || c == 'g') { /* nG key map */
1309 view->gline = view->hiline = n;
1314 /* Massage excessive or inapplicable values at the input handler. */
1320 static const struct got_error *
1321 view_input(struct tog_view **new, int *done, struct tog_view *view,
1322 struct tog_view_list_head *views)
1324 const struct got_error *err = NULL;
1330 /* Clear "no matches" indicator. */
1331 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1332 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1333 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1337 if (view->searching && !view->search_next_done) {
1338 errcode = pthread_mutex_unlock(&tog_mutex);
1340 return got_error_set_errno(errcode,
1341 "pthread_mutex_unlock");
1343 errcode = pthread_mutex_lock(&tog_mutex);
1345 return got_error_set_errno(errcode,
1346 "pthread_mutex_lock");
1347 view->search_next(view);
1351 /* Allow threads to make progress while we are waiting for input. */
1352 errcode = pthread_mutex_unlock(&tog_mutex);
1354 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1355 /* If we have an unfinished count, let C-g or backspace abort. */
1356 if (view->count && --view->count) {
1358 nodelay(view->window, TRUE);
1359 ch = wgetch(view->window);
1360 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1365 ch = wgetch(view->window);
1366 if (ch >= '1' && ch <= '9')
1367 view->ch = ch = get_compound_key(view, ch);
1369 if (view->hiline && ch != ERR && ch != 0)
1370 view->hiline = 0; /* key pressed, clear line highlight */
1371 nodelay(view->window, TRUE);
1372 errcode = pthread_mutex_lock(&tog_mutex);
1374 return got_error_set_errno(errcode, "pthread_mutex_lock");
1376 if (tog_sigwinch_received || tog_sigcont_received) {
1378 tog_sigwinch_received = 0;
1379 tog_sigcont_received = 0;
1380 TAILQ_FOREACH(v, views, entry) {
1381 err = view_resize(v);
1384 err = v->input(new, v, KEY_RESIZE);
1388 err = view_resize(v->child);
1391 err = v->child->input(new, v->child,
1395 if (v->child->resized_x || v->child->resized_y) {
1396 err = view_resize_split(v, 0);
1409 view->child->focussed = 1;
1410 view->focus_child = 1;
1411 } else if (view->parent) {
1413 view->parent->focussed = 1;
1414 view->parent->focus_child = 0;
1415 if (!view_is_splitscreen(view)) {
1416 if (view->parent->resize) {
1417 err = view->parent->resize(view->parent,
1422 offset_selection_up(view->parent);
1423 err = view_fullscreen(view->parent);
1430 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1431 if (view->parent->resize) {
1432 /* might need more commits to fill fullscreen */
1433 err = view->parent->resize(view->parent, 0);
1437 offset_selection_up(view->parent);
1439 err = view->input(new, view, ch);
1447 if (view_is_parent_view(view)) {
1448 if (view->child == NULL)
1450 if (view_is_splitscreen(view->child)) {
1452 view->child->focussed = 1;
1453 err = view_fullscreen(view->child);
1455 err = view_splitscreen(view->child);
1457 err = view_resize_split(view, 0);
1461 err = view->child->input(new, view->child,
1464 if (view_is_splitscreen(view)) {
1465 view->parent->focussed = 0;
1467 err = view_fullscreen(view);
1469 err = view_splitscreen(view);
1470 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1471 err = view_resize(view->parent);
1473 err = view_resize_split(view, 0);
1477 err = view->input(new, view, KEY_RESIZE);
1482 err = view->resize(view, 0);
1487 err = offset_selection_down(view->parent);
1489 err = offset_selection_down(view);
1493 err = switch_split(view);
1496 err = view_resize_split(view, -1);
1499 err = view_resize_split(view, 1);
1505 if (view->search_start)
1506 view_search_start(view);
1508 err = view->input(new, view, ch);
1512 if (view->search_started && view->search_next) {
1513 view->searching = (ch == 'n' ?
1514 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1515 view->search_next_done = 0;
1516 view->search_next(view);
1518 err = view->input(new, view, ch);
1521 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1522 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1524 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1525 TAILQ_FOREACH(v, views, entry) {
1531 if (v->child && v->child->reset) {
1532 err = v->child->reset(v->child);
1539 err = view->input(new, view, ch);
1547 view_needs_focus_indication(struct tog_view *view)
1549 if (view_is_parent_view(view)) {
1550 if (view->child == NULL || view->child->focussed)
1552 if (!view_is_splitscreen(view->child))
1554 } else if (!view_is_splitscreen(view))
1557 return view->focussed;
1560 static const struct got_error *
1561 view_loop(struct tog_view *view)
1563 const struct got_error *err = NULL;
1564 struct tog_view_list_head views;
1565 struct tog_view *new_view;
1567 int fast_refresh = 10;
1568 int done = 0, errcode;
1570 mode = getenv("TOG_VIEW_SPLIT_MODE");
1571 if (!mode || !(*mode == 'h' || *mode == 'H'))
1572 view->mode = TOG_VIEW_SPLIT_VERT;
1574 view->mode = TOG_VIEW_SPLIT_HRZN;
1576 errcode = pthread_mutex_lock(&tog_mutex);
1578 return got_error_set_errno(errcode, "pthread_mutex_lock");
1581 TAILQ_INSERT_HEAD(&views, view, entry);
1584 err = view->show(view);
1589 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1590 !tog_fatal_signal_received()) {
1591 /* Refresh fast during initialization, then become slower. */
1592 if (fast_refresh && fast_refresh-- == 0)
1593 halfdelay(10); /* switch to once per second */
1595 err = view_input(&new_view, &done, view, &views);
1599 struct tog_view *v, *prev = NULL;
1601 if (view_is_parent_view(view))
1602 prev = TAILQ_PREV(view, tog_view_list_head,
1604 else if (view->parent)
1605 prev = view->parent;
1608 view->parent->child = NULL;
1609 view->parent->focus_child = 0;
1610 /* Restore fullscreen line height. */
1611 view->parent->nlines = view->parent->lines;
1612 err = view_resize(view->parent);
1615 /* Make resized splits persist. */
1616 view_transfer_size(view->parent, view);
1618 TAILQ_REMOVE(&views, view, entry);
1620 err = view_close(view);
1625 TAILQ_FOREACH(v, &views, entry) {
1629 if (view == NULL && new_view == NULL) {
1630 /* No view has focus. Try to pick one. */
1633 else if (!TAILQ_EMPTY(&views)) {
1634 view = TAILQ_LAST(&views,
1635 tog_view_list_head);
1638 if (view->focus_child) {
1639 view->child->focussed = 1;
1647 struct tog_view *v, *t;
1648 /* Only allow one parent view per type. */
1649 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1650 if (v->type != new_view->type)
1652 TAILQ_REMOVE(&views, v, entry);
1653 err = view_close(v);
1658 TAILQ_INSERT_TAIL(&views, new_view, entry);
1662 if (view_is_parent_view(view)) {
1663 if (view->child && view->child->focussed)
1666 if (view->parent && view->parent->focussed)
1667 view = view->parent;
1669 show_panel(view->panel);
1670 if (view->child && view_is_splitscreen(view->child))
1671 show_panel(view->child->panel);
1672 if (view->parent && view_is_splitscreen(view)) {
1673 err = view->parent->show(view->parent);
1677 err = view->show(view);
1681 err = view->child->show(view->child);
1690 while (!TAILQ_EMPTY(&views)) {
1691 const struct got_error *close_err;
1692 view = TAILQ_FIRST(&views);
1693 TAILQ_REMOVE(&views, view, entry);
1694 close_err = view_close(view);
1695 if (close_err && err == NULL)
1699 errcode = pthread_mutex_unlock(&tog_mutex);
1700 if (errcode && err == NULL)
1701 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1711 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1716 /* Create newly allocated wide-character string equivalent to a byte string. */
1717 static const struct got_error *
1718 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1721 const struct got_error *err = NULL;
1724 *wlen = mbstowcs(NULL, s, 0);
1725 if (*wlen == (size_t)-1) {
1727 if (errno != EILSEQ)
1728 return got_error_from_errno("mbstowcs");
1730 /* byte string invalid in current encoding; try to "fix" it */
1731 err = got_mbsavis(&vis, &vislen, s);
1734 *wlen = mbstowcs(NULL, vis, 0);
1735 if (*wlen == (size_t)-1) {
1736 err = got_error_from_errno("mbstowcs"); /* give up */
1741 *ws = calloc(*wlen + 1, sizeof(**ws));
1743 err = got_error_from_errno("calloc");
1747 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1748 err = got_error_from_errno("mbstowcs");
1759 static const struct got_error *
1760 expand_tab(char **ptr, const char *src)
1763 size_t len, n, idx = 0, sz = 0;
1766 n = len = strlen(src);
1767 dst = malloc(n + 1);
1769 return got_error_from_errno("malloc");
1771 while (idx < len && src[idx]) {
1772 const char c = src[idx];
1775 size_t nb = TABSIZE - sz % TABSIZE;
1778 p = realloc(dst, n + nb);
1781 return got_error_from_errno("realloc");
1786 memset(dst + sz, ' ', nb);
1789 dst[sz++] = src[idx];
1799 * Advance at most n columns from wline starting at offset off.
1800 * Return the index to the first character after the span operation.
1801 * Return the combined column width of all spanned wide character in
1805 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1807 int width, i, cols = 0;
1814 for (i = off; wline[i] != L'\0'; ++i) {
1815 if (wline[i] == L'\t')
1816 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1818 width = wcwidth(wline[i]);
1825 if (cols + width > n)
1835 * Format a line for display, ensuring that it won't overflow a width limit.
1836 * With scrolling, the width returned refers to the scrolled version of the
1837 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1839 static const struct got_error *
1840 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1841 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1843 const struct got_error *err = NULL;
1845 wchar_t *wline = NULL;
1854 err = expand_tab(&exstr, line);
1859 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1864 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1866 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1867 wline[wlen - 1] = L'\0';
1870 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1871 wline[wlen - 1] = L'\0';
1875 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1881 *scrollxp = scrollx;
1889 static const struct got_error*
1890 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1891 struct got_object_id *id, struct got_repository *repo)
1893 static const struct got_error *err = NULL;
1894 struct got_reflist_entry *re;
1900 TAILQ_FOREACH(re, refs, entry) {
1901 struct got_tag_object *tag = NULL;
1902 struct got_object_id *ref_id;
1905 name = got_ref_get_name(re->ref);
1906 if (strcmp(name, GOT_REF_HEAD) == 0)
1908 if (strncmp(name, "refs/", 5) == 0)
1910 if (strncmp(name, "got/", 4) == 0 &&
1911 strncmp(name, "got/backup/", 11) != 0)
1913 if (strncmp(name, "heads/", 6) == 0)
1915 if (strncmp(name, "remotes/", 8) == 0) {
1917 s = strstr(name, "/" GOT_REF_HEAD);
1918 if (s != NULL && s[strlen(s)] == '\0')
1921 err = got_ref_resolve(&ref_id, repo, re->ref);
1924 if (strncmp(name, "tags/", 5) == 0) {
1925 err = got_object_open_as_tag(&tag, repo, ref_id);
1927 if (err->code != GOT_ERR_OBJ_TYPE) {
1931 /* Ref points at something other than a tag. */
1936 cmp = got_object_id_cmp(tag ?
1937 got_object_tag_get_object_id(tag) : ref_id, id);
1940 got_object_tag_close(tag);
1944 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1945 s ? ", " : "", name) == -1) {
1946 err = got_error_from_errno("asprintf");
1957 static const struct got_error *
1958 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1963 smallerthan = strchr(author, '<');
1964 if (smallerthan && smallerthan[1] != '\0')
1965 author = smallerthan + 1;
1966 author[strcspn(author, "@>")] = '\0';
1967 return format_line(wauthor, author_width, NULL, author, 0, limit,
1971 static const struct got_error *
1972 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1973 struct got_object_id *id, const size_t date_display_cols,
1974 int author_display_cols)
1976 struct tog_log_view_state *s = &view->state.log;
1977 const struct got_error *err = NULL;
1978 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1979 char *logmsg0 = NULL, *logmsg = NULL;
1980 char *author = NULL;
1981 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1982 int author_width, logmsg_width;
1983 char *newline, *line = NULL;
1984 int col, limit, scrollx;
1985 const int avail = view->ncols;
1987 time_t committer_time;
1988 struct tog_color *tc;
1990 committer_time = got_object_commit_get_committer_time(commit);
1991 if (gmtime_r(&committer_time, &tm) == NULL)
1992 return got_error_from_errno("gmtime_r");
1993 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1994 return got_error(GOT_ERR_NO_SPACE);
1996 if (avail <= date_display_cols)
1997 limit = MIN(sizeof(datebuf) - 1, avail);
1999 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2000 tc = get_color(&s->colors, TOG_COLOR_DATE);
2002 wattr_on(view->window,
2003 COLOR_PAIR(tc->colorpair), NULL);
2004 waddnstr(view->window, datebuf, limit);
2006 wattr_off(view->window,
2007 COLOR_PAIR(tc->colorpair), NULL);
2014 err = got_object_id_str(&id_str, id);
2017 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2019 wattr_on(view->window,
2020 COLOR_PAIR(tc->colorpair), NULL);
2021 wprintw(view->window, "%.8s ", id_str);
2023 wattr_off(view->window,
2024 COLOR_PAIR(tc->colorpair), NULL);
2031 if (s->use_committer)
2032 author = strdup(got_object_commit_get_committer(commit));
2034 author = strdup(got_object_commit_get_author(commit));
2035 if (author == NULL) {
2036 err = got_error_from_errno("strdup");
2039 err = format_author(&wauthor, &author_width, author, avail - col, col);
2042 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2044 wattr_on(view->window,
2045 COLOR_PAIR(tc->colorpair), NULL);
2046 waddwstr(view->window, wauthor);
2047 col += author_width;
2048 while (col < avail && author_width < author_display_cols + 2) {
2049 waddch(view->window, ' ');
2054 wattr_off(view->window,
2055 COLOR_PAIR(tc->colorpair), NULL);
2059 err = got_object_commit_get_logmsg(&logmsg0, commit);
2063 while (*logmsg == '\n')
2065 newline = strchr(logmsg, '\n');
2068 limit = avail - col;
2069 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2070 limit--; /* for the border */
2071 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2075 waddwstr(view->window, &wlogmsg[scrollx]);
2076 col += MAX(logmsg_width, 0);
2077 while (col < avail) {
2078 waddch(view->window, ' ');
2090 static struct commit_queue_entry *
2091 alloc_commit_queue_entry(struct got_commit_object *commit,
2092 struct got_object_id *id)
2094 struct commit_queue_entry *entry;
2095 struct got_object_id *dup;
2097 entry = calloc(1, sizeof(*entry));
2101 dup = got_object_id_dup(id);
2108 entry->commit = commit;
2113 pop_commit(struct commit_queue *commits)
2115 struct commit_queue_entry *entry;
2117 entry = TAILQ_FIRST(&commits->head);
2118 TAILQ_REMOVE(&commits->head, entry, entry);
2119 got_object_commit_close(entry->commit);
2120 commits->ncommits--;
2126 free_commits(struct commit_queue *commits)
2128 while (!TAILQ_EMPTY(&commits->head))
2129 pop_commit(commits);
2132 static const struct got_error *
2133 match_commit(int *have_match, struct got_object_id *id,
2134 struct got_commit_object *commit, regex_t *regex)
2136 const struct got_error *err = NULL;
2137 regmatch_t regmatch;
2138 char *id_str = NULL, *logmsg = NULL;
2142 err = got_object_id_str(&id_str, id);
2146 err = got_object_commit_get_logmsg(&logmsg, commit);
2150 if (regexec(regex, got_object_commit_get_author(commit), 1,
2151 ®match, 0) == 0 ||
2152 regexec(regex, got_object_commit_get_committer(commit), 1,
2153 ®match, 0) == 0 ||
2154 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2155 regexec(regex, logmsg, 1, ®match, 0) == 0)
2163 static const struct got_error *
2164 queue_commits(struct tog_log_thread_args *a)
2166 const struct got_error *err = NULL;
2169 * We keep all commits open throughout the lifetime of the log
2170 * view in order to avoid having to re-fetch commits from disk
2171 * while updating the display.
2174 struct got_object_id *id;
2175 struct got_commit_object *commit;
2176 struct commit_queue_entry *entry;
2179 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2181 if (err || id == NULL)
2184 err = got_object_open_as_commit(&commit, a->repo, id);
2187 entry = alloc_commit_queue_entry(commit, id);
2188 if (entry == NULL) {
2189 err = got_error_from_errno("alloc_commit_queue_entry");
2193 errcode = pthread_mutex_lock(&tog_mutex);
2195 err = got_error_set_errno(errcode,
2196 "pthread_mutex_lock");
2200 entry->idx = a->commits->ncommits;
2201 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2202 a->commits->ncommits++;
2204 if (*a->searching == TOG_SEARCH_FORWARD &&
2205 !*a->search_next_done) {
2207 err = match_commit(&have_match, id, commit, a->regex);
2211 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2214 errcode = pthread_mutex_unlock(&tog_mutex);
2215 if (errcode && err == NULL)
2216 err = got_error_set_errno(errcode,
2217 "pthread_mutex_unlock");
2220 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2226 select_commit(struct tog_log_view_state *s)
2228 struct commit_queue_entry *entry;
2231 entry = s->first_displayed_entry;
2233 if (ncommits == s->selected) {
2234 s->selected_entry = entry;
2237 entry = TAILQ_NEXT(entry, entry);
2242 static const struct got_error *
2243 draw_commits(struct tog_view *view)
2245 const struct got_error *err = NULL;
2246 struct tog_log_view_state *s = &view->state.log;
2247 struct commit_queue_entry *entry = s->selected_entry;
2248 int limit = view->nlines;
2250 int ncommits, author_cols = 4;
2251 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2252 char *refs_str = NULL;
2254 struct tog_color *tc;
2255 static const size_t date_display_cols = 12;
2257 if (view_is_hsplit_top(view))
2258 --limit; /* account for border */
2260 if (s->selected_entry &&
2261 !(view->searching && view->search_next_done == 0)) {
2262 struct got_reflist_head *refs;
2263 err = got_object_id_str(&id_str, s->selected_entry->id);
2266 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2267 s->selected_entry->id);
2269 err = build_refs_str(&refs_str, refs,
2270 s->selected_entry->id, s->repo);
2276 if (s->thread_args.commits_needed == 0)
2277 halfdelay(10); /* disable fast refresh */
2279 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2280 if (asprintf(&ncommits_str, " [%d/%d] %s",
2281 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2282 (view->searching && !view->search_next_done) ?
2283 "searching..." : "loading...") == -1) {
2284 err = got_error_from_errno("asprintf");
2288 const char *search_str = NULL;
2290 if (view->searching) {
2291 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2292 search_str = "no more matches";
2293 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2294 search_str = "no matches found";
2295 else if (!view->search_next_done)
2296 search_str = "searching...";
2299 if (asprintf(&ncommits_str, " [%d/%d] %s",
2300 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2301 search_str ? search_str :
2302 (refs_str ? refs_str : "")) == -1) {
2303 err = got_error_from_errno("asprintf");
2308 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2309 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2310 "........................................",
2311 s->in_repo_path, ncommits_str) == -1) {
2312 err = got_error_from_errno("asprintf");
2316 } else if (asprintf(&header, "commit %s%s",
2317 id_str ? id_str : "........................................",
2318 ncommits_str) == -1) {
2319 err = got_error_from_errno("asprintf");
2323 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2327 werase(view->window);
2329 if (view_needs_focus_indication(view))
2330 wstandout(view->window);
2331 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2333 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2334 waddwstr(view->window, wline);
2335 while (width < view->ncols) {
2336 waddch(view->window, ' ');
2340 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2341 if (view_needs_focus_indication(view))
2342 wstandend(view->window);
2347 /* Grow author column size if necessary, and set view->maxx. */
2348 entry = s->first_displayed_entry;
2352 struct got_commit_object *c = entry->commit;
2353 char *author, *eol, *msg, *msg0;
2354 wchar_t *wauthor, *wmsg;
2356 if (ncommits >= limit - 1)
2358 if (s->use_committer)
2359 author = strdup(got_object_commit_get_committer(c));
2361 author = strdup(got_object_commit_get_author(c));
2362 if (author == NULL) {
2363 err = got_error_from_errno("strdup");
2366 err = format_author(&wauthor, &width, author, COLS,
2368 if (author_cols < width)
2369 author_cols = width;
2374 err = got_object_commit_get_logmsg(&msg0, c);
2378 while (*msg == '\n')
2380 if ((eol = strchr(msg, '\n')))
2382 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2383 date_display_cols + author_cols, 0);
2386 view->maxx = MAX(view->maxx, width);
2390 entry = TAILQ_NEXT(entry, entry);
2393 entry = s->first_displayed_entry;
2394 s->last_displayed_entry = s->first_displayed_entry;
2397 if (ncommits >= limit - 1)
2399 if (ncommits == s->selected)
2400 wstandout(view->window);
2401 err = draw_commit(view, entry->commit, entry->id,
2402 date_display_cols, author_cols);
2403 if (ncommits == s->selected)
2404 wstandend(view->window);
2408 s->last_displayed_entry = entry;
2409 entry = TAILQ_NEXT(entry, entry);
2422 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2424 struct commit_queue_entry *entry;
2427 entry = TAILQ_FIRST(&s->commits.head);
2428 if (s->first_displayed_entry == entry)
2431 entry = s->first_displayed_entry;
2432 while (entry && nscrolled < maxscroll) {
2433 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2435 s->first_displayed_entry = entry;
2441 static const struct got_error *
2442 trigger_log_thread(struct tog_view *view, int wait)
2444 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2447 halfdelay(1); /* fast refresh while loading commits */
2449 while (!ta->log_complete && !tog_thread_error &&
2450 (ta->commits_needed > 0 || ta->load_all)) {
2451 /* Wake the log thread. */
2452 errcode = pthread_cond_signal(&ta->need_commits);
2454 return got_error_set_errno(errcode,
2455 "pthread_cond_signal");
2458 * The mutex will be released while the view loop waits
2459 * in wgetch(), at which time the log thread will run.
2464 /* Display progress update in log view. */
2465 show_log_view(view);
2469 /* Wait right here while next commit is being loaded. */
2470 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2472 return got_error_set_errno(errcode,
2473 "pthread_cond_wait");
2475 /* Display progress update in log view. */
2476 show_log_view(view);
2484 static const struct got_error *
2485 request_log_commits(struct tog_view *view)
2487 struct tog_log_view_state *state = &view->state.log;
2488 const struct got_error *err = NULL;
2490 if (state->thread_args.log_complete)
2493 state->thread_args.commits_needed += view->nscrolled;
2494 err = trigger_log_thread(view, 1);
2495 view->nscrolled = 0;
2500 static const struct got_error *
2501 log_scroll_down(struct tog_view *view, int maxscroll)
2503 struct tog_log_view_state *s = &view->state.log;
2504 const struct got_error *err = NULL;
2505 struct commit_queue_entry *pentry;
2506 int nscrolled = 0, ncommits_needed;
2508 if (s->last_displayed_entry == NULL)
2511 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2512 if (s->commits.ncommits < ncommits_needed &&
2513 !s->thread_args.log_complete) {
2515 * Ask the log thread for required amount of commits.
2517 s->thread_args.commits_needed +=
2518 ncommits_needed - s->commits.ncommits;
2519 err = trigger_log_thread(view, 1);
2525 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2526 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2529 s->last_displayed_entry = pentry ?
2530 pentry : s->last_displayed_entry;;
2532 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2535 s->first_displayed_entry = pentry;
2536 } while (++nscrolled < maxscroll);
2538 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2539 view->nscrolled += nscrolled;
2541 view->nscrolled = 0;
2546 static const struct got_error *
2547 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2548 struct got_commit_object *commit, struct got_object_id *commit_id,
2549 struct tog_view *log_view, struct got_repository *repo)
2551 const struct got_error *err;
2552 struct got_object_qid *parent_id;
2553 struct tog_view *diff_view;
2555 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2556 if (diff_view == NULL)
2557 return got_error_from_errno("view_open");
2559 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2560 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2561 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2563 *new_view = diff_view;
2567 static const struct got_error *
2568 tree_view_visit_subtree(struct tog_tree_view_state *s,
2569 struct got_tree_object *subtree)
2571 struct tog_parent_tree *parent;
2573 parent = calloc(1, sizeof(*parent));
2575 return got_error_from_errno("calloc");
2577 parent->tree = s->tree;
2578 parent->first_displayed_entry = s->first_displayed_entry;
2579 parent->selected_entry = s->selected_entry;
2580 parent->selected = s->selected;
2581 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2584 s->first_displayed_entry = NULL;
2588 static const struct got_error *
2589 tree_view_walk_path(struct tog_tree_view_state *s,
2590 struct got_commit_object *commit, const char *path)
2592 const struct got_error *err = NULL;
2593 struct got_tree_object *tree = NULL;
2595 char *slash, *subpath = NULL;
2597 /* Walk the path and open corresponding tree objects. */
2600 struct got_tree_entry *te;
2601 struct got_object_id *tree_id;
2607 /* Ensure the correct subtree entry is selected. */
2608 slash = strchr(p, '/');
2610 te_name = strdup(p);
2612 te_name = strndup(p, slash - p);
2613 if (te_name == NULL) {
2614 err = got_error_from_errno("strndup");
2617 te = got_object_tree_find_entry(s->tree, te_name);
2619 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2624 s->first_displayed_entry = s->selected_entry = te;
2626 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2627 break; /* jump to this file's entry */
2629 slash = strchr(p, '/');
2631 subpath = strndup(path, slash - path);
2633 subpath = strdup(path);
2634 if (subpath == NULL) {
2635 err = got_error_from_errno("strdup");
2639 err = got_object_id_by_path(&tree_id, s->repo, commit,
2644 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2649 err = tree_view_visit_subtree(s, tree);
2651 got_object_tree_close(tree);
2665 static const struct got_error *
2666 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2667 struct commit_queue_entry *entry, const char *path,
2668 const char *head_ref_name, struct got_repository *repo)
2670 const struct got_error *err = NULL;
2671 struct tog_tree_view_state *s;
2672 struct tog_view *tree_view;
2674 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2675 if (tree_view == NULL)
2676 return got_error_from_errno("view_open");
2678 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2681 s = &tree_view->state.tree;
2683 *new_view = tree_view;
2685 if (got_path_is_root_dir(path))
2688 return tree_view_walk_path(s, entry->commit, path);
2691 static const struct got_error *
2692 block_signals_used_by_main_thread(void)
2697 if (sigemptyset(&sigset) == -1)
2698 return got_error_from_errno("sigemptyset");
2700 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2701 if (sigaddset(&sigset, SIGWINCH) == -1)
2702 return got_error_from_errno("sigaddset");
2703 if (sigaddset(&sigset, SIGCONT) == -1)
2704 return got_error_from_errno("sigaddset");
2705 if (sigaddset(&sigset, SIGINT) == -1)
2706 return got_error_from_errno("sigaddset");
2707 if (sigaddset(&sigset, SIGTERM) == -1)
2708 return got_error_from_errno("sigaddset");
2710 /* ncurses handles SIGTSTP */
2711 if (sigaddset(&sigset, SIGTSTP) == -1)
2712 return got_error_from_errno("sigaddset");
2714 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2716 return got_error_set_errno(errcode, "pthread_sigmask");
2722 log_thread(void *arg)
2724 const struct got_error *err = NULL;
2726 struct tog_log_thread_args *a = arg;
2730 * Sync startup with main thread such that we begin our
2731 * work once view_input() has released the mutex.
2733 errcode = pthread_mutex_lock(&tog_mutex);
2735 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2739 err = block_signals_used_by_main_thread();
2741 pthread_mutex_unlock(&tog_mutex);
2745 while (!done && !err && !tog_fatal_signal_received()) {
2746 errcode = pthread_mutex_unlock(&tog_mutex);
2748 err = got_error_set_errno(errcode,
2749 "pthread_mutex_unlock");
2752 err = queue_commits(a);
2754 if (err->code != GOT_ERR_ITER_COMPLETED)
2758 } else if (a->commits_needed > 0 && !a->load_all)
2759 a->commits_needed--;
2761 errcode = pthread_mutex_lock(&tog_mutex);
2763 err = got_error_set_errno(errcode,
2764 "pthread_mutex_lock");
2766 } else if (*a->quit)
2768 else if (*a->first_displayed_entry == NULL) {
2769 *a->first_displayed_entry =
2770 TAILQ_FIRST(&a->commits->head);
2771 *a->selected_entry = *a->first_displayed_entry;
2774 errcode = pthread_cond_signal(&a->commit_loaded);
2776 err = got_error_set_errno(errcode,
2777 "pthread_cond_signal");
2778 pthread_mutex_unlock(&tog_mutex);
2783 a->commits_needed = 0;
2785 if (a->commits_needed == 0 && !a->load_all) {
2786 errcode = pthread_cond_wait(&a->need_commits,
2789 err = got_error_set_errno(errcode,
2790 "pthread_cond_wait");
2791 pthread_mutex_unlock(&tog_mutex);
2799 a->log_complete = 1;
2800 errcode = pthread_mutex_unlock(&tog_mutex);
2802 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2805 tog_thread_error = 1;
2806 pthread_cond_signal(&a->commit_loaded);
2811 static const struct got_error *
2812 stop_log_thread(struct tog_log_view_state *s)
2814 const struct got_error *err = NULL, *thread_err = NULL;
2819 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2821 return got_error_set_errno(errcode,
2822 "pthread_cond_signal");
2823 errcode = pthread_mutex_unlock(&tog_mutex);
2825 return got_error_set_errno(errcode,
2826 "pthread_mutex_unlock");
2827 errcode = pthread_join(s->thread, (void **)&thread_err);
2829 return got_error_set_errno(errcode, "pthread_join");
2830 errcode = pthread_mutex_lock(&tog_mutex);
2832 return got_error_set_errno(errcode,
2833 "pthread_mutex_lock");
2837 if (s->thread_args.repo) {
2838 err = got_repo_close(s->thread_args.repo);
2839 s->thread_args.repo = NULL;
2842 if (s->thread_args.pack_fds) {
2843 const struct got_error *pack_err =
2844 got_repo_pack_fds_close(s->thread_args.pack_fds);
2847 s->thread_args.pack_fds = NULL;
2850 if (s->thread_args.graph) {
2851 got_commit_graph_close(s->thread_args.graph);
2852 s->thread_args.graph = NULL;
2855 return err ? err : thread_err;
2858 static const struct got_error *
2859 close_log_view(struct tog_view *view)
2861 const struct got_error *err = NULL;
2862 struct tog_log_view_state *s = &view->state.log;
2865 err = stop_log_thread(s);
2867 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2868 if (errcode && err == NULL)
2869 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2871 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2872 if (errcode && err == NULL)
2873 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2875 free_commits(&s->commits);
2876 free(s->in_repo_path);
2877 s->in_repo_path = NULL;
2880 free(s->head_ref_name);
2881 s->head_ref_name = NULL;
2885 static const struct got_error *
2886 search_start_log_view(struct tog_view *view)
2888 struct tog_log_view_state *s = &view->state.log;
2890 s->matched_entry = NULL;
2891 s->search_entry = NULL;
2895 static const struct got_error *
2896 search_next_log_view(struct tog_view *view)
2898 const struct got_error *err = NULL;
2899 struct tog_log_view_state *s = &view->state.log;
2900 struct commit_queue_entry *entry;
2902 /* Display progress update in log view. */
2903 show_log_view(view);
2907 if (s->search_entry) {
2909 errcode = pthread_mutex_unlock(&tog_mutex);
2911 return got_error_set_errno(errcode,
2912 "pthread_mutex_unlock");
2913 ch = wgetch(view->window);
2914 errcode = pthread_mutex_lock(&tog_mutex);
2916 return got_error_set_errno(errcode,
2917 "pthread_mutex_lock");
2918 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2919 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2922 if (view->searching == TOG_SEARCH_FORWARD)
2923 entry = TAILQ_NEXT(s->search_entry, entry);
2925 entry = TAILQ_PREV(s->search_entry,
2926 commit_queue_head, entry);
2927 } else if (s->matched_entry) {
2928 int matched_idx = s->matched_entry->idx;
2929 int selected_idx = s->selected_entry->idx;
2932 * If the user has moved the cursor after we hit a match,
2933 * the position from where we should continue searching
2934 * might have changed.
2936 if (view->searching == TOG_SEARCH_FORWARD) {
2937 if (matched_idx > selected_idx)
2938 entry = TAILQ_NEXT(s->selected_entry, entry);
2940 entry = TAILQ_NEXT(s->matched_entry, entry);
2942 if (matched_idx < selected_idx)
2943 entry = TAILQ_PREV(s->selected_entry,
2944 commit_queue_head, entry);
2946 entry = TAILQ_PREV(s->matched_entry,
2947 commit_queue_head, entry);
2950 entry = s->selected_entry;
2956 if (entry == NULL) {
2957 if (s->thread_args.log_complete ||
2958 view->searching == TOG_SEARCH_BACKWARD) {
2959 view->search_next_done =
2960 (s->matched_entry == NULL ?
2961 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2962 s->search_entry = NULL;
2966 * Poke the log thread for more commits and return,
2967 * allowing the main loop to make progress. Search
2968 * will resume at s->search_entry once we come back.
2970 s->thread_args.commits_needed++;
2971 return trigger_log_thread(view, 0);
2974 err = match_commit(&have_match, entry->id, entry->commit,
2979 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2980 s->matched_entry = entry;
2984 s->search_entry = entry;
2985 if (view->searching == TOG_SEARCH_FORWARD)
2986 entry = TAILQ_NEXT(entry, entry);
2988 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2991 if (s->matched_entry) {
2992 int cur = s->selected_entry->idx;
2993 while (cur < s->matched_entry->idx) {
2994 err = input_log_view(NULL, view, KEY_DOWN);
2999 while (cur > s->matched_entry->idx) {
3000 err = input_log_view(NULL, view, KEY_UP);
3007 s->search_entry = NULL;
3012 static const struct got_error *
3013 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3014 struct got_repository *repo, const char *head_ref_name,
3015 const char *in_repo_path, int log_branches)
3017 const struct got_error *err = NULL;
3018 struct tog_log_view_state *s = &view->state.log;
3019 struct got_repository *thread_repo = NULL;
3020 struct got_commit_graph *thread_graph = NULL;
3023 if (in_repo_path != s->in_repo_path) {
3024 free(s->in_repo_path);
3025 s->in_repo_path = strdup(in_repo_path);
3026 if (s->in_repo_path == NULL)
3027 return got_error_from_errno("strdup");
3030 /* The commit queue only contains commits being displayed. */
3031 TAILQ_INIT(&s->commits.head);
3032 s->commits.ncommits = 0;
3035 if (head_ref_name) {
3036 s->head_ref_name = strdup(head_ref_name);
3037 if (s->head_ref_name == NULL) {
3038 err = got_error_from_errno("strdup");
3042 s->start_id = got_object_id_dup(start_id);
3043 if (s->start_id == NULL) {
3044 err = got_error_from_errno("got_object_id_dup");
3047 s->log_branches = log_branches;
3049 STAILQ_INIT(&s->colors);
3050 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3051 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3052 get_color_value("TOG_COLOR_COMMIT"));
3055 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3056 get_color_value("TOG_COLOR_AUTHOR"));
3058 free_colors(&s->colors);
3061 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3062 get_color_value("TOG_COLOR_DATE"));
3064 free_colors(&s->colors);
3069 view->show = show_log_view;
3070 view->input = input_log_view;
3071 view->resize = resize_log_view;
3072 view->close = close_log_view;
3073 view->search_start = search_start_log_view;
3074 view->search_next = search_next_log_view;
3076 if (s->thread_args.pack_fds == NULL) {
3077 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3081 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3082 s->thread_args.pack_fds);
3085 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3089 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3090 s->repo, NULL, NULL);
3094 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3096 err = got_error_set_errno(errcode, "pthread_cond_init");
3099 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3101 err = got_error_set_errno(errcode, "pthread_cond_init");
3105 s->thread_args.commits_needed = view->nlines;
3106 s->thread_args.graph = thread_graph;
3107 s->thread_args.commits = &s->commits;
3108 s->thread_args.in_repo_path = s->in_repo_path;
3109 s->thread_args.start_id = s->start_id;
3110 s->thread_args.repo = thread_repo;
3111 s->thread_args.log_complete = 0;
3112 s->thread_args.quit = &s->quit;
3113 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3114 s->thread_args.selected_entry = &s->selected_entry;
3115 s->thread_args.searching = &view->searching;
3116 s->thread_args.search_next_done = &view->search_next_done;
3117 s->thread_args.regex = &view->regex;
3120 close_log_view(view);
3124 static const struct got_error *
3125 show_log_view(struct tog_view *view)
3127 const struct got_error *err;
3128 struct tog_log_view_state *s = &view->state.log;
3130 if (s->thread == NULL) {
3131 int errcode = pthread_create(&s->thread, NULL, log_thread,
3134 return got_error_set_errno(errcode, "pthread_create");
3135 if (s->thread_args.commits_needed > 0) {
3136 err = trigger_log_thread(view, 1);
3142 return draw_commits(view);
3146 log_move_cursor_up(struct tog_view *view, int page, int home)
3148 struct tog_log_view_state *s = &view->state.log;
3150 if (s->selected_entry->idx == 0)
3152 if (s->first_displayed_entry == NULL)
3155 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3157 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3159 if (!page && !home && s->selected > 0)
3162 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3168 static const struct got_error *
3169 log_move_cursor_down(struct tog_view *view, int page)
3171 struct tog_log_view_state *s = &view->state.log;
3172 const struct got_error *err = NULL;
3173 int eos = view->nlines - 2;
3175 if (s->thread_args.log_complete &&
3176 s->selected_entry->idx >= s->commits.ncommits - 1)
3179 if (view_is_hsplit_top(view))
3180 --eos; /* border consumes the last line */
3183 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3186 err = log_scroll_down(view, 1);
3187 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3188 struct commit_queue_entry *entry;
3192 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3193 s->last_displayed_entry = entry;
3194 for (n = 0; n <= eos; n++) {
3197 s->first_displayed_entry = entry;
3198 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3201 s->selected = n - 1;
3203 if (s->last_displayed_entry->idx == s->commits.ncommits - 1 &&
3204 s->thread_args.log_complete)
3205 s->selected += MIN(page,
3206 s->commits.ncommits - s->selected_entry->idx - 1);
3208 err = log_scroll_down(view, page);
3214 * We might necessarily overshoot in horizontal
3215 * splits; if so, select the last displayed commit.
3217 if (s->first_displayed_entry && s->last_displayed_entry) {
3218 s->selected = MIN(s->selected,
3219 s->last_displayed_entry->idx -
3220 s->first_displayed_entry->idx);
3225 if (s->thread_args.log_complete &&
3226 s->selected_entry->idx == s->commits.ncommits - 1)
3233 view_get_split(struct tog_view *view, int *y, int *x)
3238 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3239 if (view->child && view->child->resized_y)
3240 *y = view->child->resized_y;
3241 else if (view->resized_y)
3242 *y = view->resized_y;
3244 *y = view_split_begin_y(view->lines);
3245 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3246 if (view->child && view->child->resized_x)
3247 *x = view->child->resized_x;
3248 else if (view->resized_x)
3249 *x = view->resized_x;
3251 *x = view_split_begin_x(view->begin_x);
3255 /* Split view horizontally at y and offset view->state->selected line. */
3256 static const struct got_error *
3257 view_init_hsplit(struct tog_view *view, int y)
3259 const struct got_error *err = NULL;
3263 err = view_resize(view);
3267 err = offset_selection_down(view);
3272 static const struct got_error *
3273 log_goto_line(struct tog_view *view, int nlines)
3275 const struct got_error *err = NULL;
3276 struct tog_log_view_state *s = &view->state.log;
3277 int g, idx = s->selected_entry->idx;
3279 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3285 if (g >= s->first_displayed_entry->idx + 1 &&
3286 g <= s->last_displayed_entry->idx + 1 &&
3287 g - s->first_displayed_entry->idx - 1 < nlines) {
3288 s->selected = g - s->first_displayed_entry->idx - 1;
3294 err = log_move_cursor_down(view, g - idx - 1);
3295 if (!err && g > s->selected_entry->idx + 1)
3296 err = log_move_cursor_down(view,
3297 g - s->first_displayed_entry->idx - 1);
3300 } else if (idx + 1 > g)
3301 log_move_cursor_up(view, idx - g + 1, 0);
3303 if (g < nlines && s->first_displayed_entry->idx == 0)
3304 s->selected = g - 1;
3311 static const struct got_error *
3312 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3314 const struct got_error *err = NULL;
3315 struct tog_log_view_state *s = &view->state.log;
3318 if (s->thread_args.load_all) {
3319 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3320 s->thread_args.load_all = 0;
3321 else if (s->thread_args.log_complete) {
3322 err = log_move_cursor_down(view, s->commits.ncommits);
3323 s->thread_args.load_all = 0;
3329 eos = nscroll = view->nlines - 1;
3330 if (view_is_hsplit_top(view))
3334 return log_goto_line(view, eos);
3344 view->x = MAX(view->maxx - view->ncols / 2, 0);
3349 if (view->x + view->ncols / 2 < view->maxx)
3350 view->x += 2; /* move two columns right */
3356 view->x -= MIN(view->x, 2); /* move two columns back */
3365 log_move_cursor_up(view, 0, 0);
3369 log_move_cursor_up(view, 0, 1);
3379 log_move_cursor_up(view, nscroll, 0);
3386 err = log_move_cursor_down(view, 0);
3389 s->use_committer = !s->use_committer;
3393 /* We don't know yet how many commits, so we're forced to
3394 * traverse them all. */
3396 s->thread_args.load_all = 1;
3397 if (!s->thread_args.log_complete)
3398 return trigger_log_thread(view, 0);
3399 err = log_move_cursor_down(view, s->commits.ncommits);
3400 s->thread_args.load_all = 0;
3411 err = log_move_cursor_down(view, nscroll);
3414 if (s->selected > view->nlines - 2)
3415 s->selected = view->nlines - 2;
3416 if (s->selected > s->commits.ncommits - 1)
3417 s->selected = s->commits.ncommits - 1;
3419 if (s->commits.ncommits < view->nlines - 1 &&
3420 !s->thread_args.log_complete) {
3421 s->thread_args.commits_needed += (view->nlines - 1) -
3422 s->commits.ncommits;
3423 err = trigger_log_thread(view, 1);
3429 if (s->selected_entry == NULL)
3431 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3435 if (s->selected_entry == NULL)
3437 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3443 if (ch == KEY_BACKSPACE &&
3444 got_path_is_root_dir(s->in_repo_path))
3446 err = stop_log_thread(s);
3449 if (ch == KEY_BACKSPACE) {
3451 err = got_path_dirname(&parent_path, s->in_repo_path);
3454 free(s->in_repo_path);
3455 s->in_repo_path = parent_path;
3456 s->thread_args.in_repo_path = s->in_repo_path;
3457 } else if (ch == CTRL('l')) {
3458 struct got_object_id *start_id;
3459 err = got_repo_match_object_id(&start_id, NULL,
3460 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3461 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3465 s->start_id = start_id;
3466 s->thread_args.start_id = s->start_id;
3468 s->log_branches = !s->log_branches;
3470 if (s->thread_args.pack_fds == NULL) {
3471 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3475 err = got_repo_open(&s->thread_args.repo,
3476 got_repo_get_path(s->repo), NULL,
3477 s->thread_args.pack_fds);
3481 err = tog_load_refs(s->repo, 0);
3484 err = got_commit_graph_open(&s->thread_args.graph,
3485 s->in_repo_path, !s->log_branches);
3488 err = got_commit_graph_iter_start(s->thread_args.graph,
3489 s->start_id, s->repo, NULL, NULL);
3492 free_commits(&s->commits);
3493 s->first_displayed_entry = NULL;
3494 s->last_displayed_entry = NULL;
3495 s->selected_entry = NULL;
3497 s->thread_args.log_complete = 0;
3499 s->thread_args.commits_needed = view->lines;
3500 s->matched_entry = NULL;
3501 s->search_entry = NULL;
3506 err = view_request_new(new_view, view, TOG_VIEW_REF);
3516 static const struct got_error *
3517 apply_unveil(const char *repo_path, const char *worktree_path)
3519 const struct got_error *error;
3522 if (unveil("gmon.out", "rwc") != 0)
3523 return got_error_from_errno2("unveil", "gmon.out");
3525 if (repo_path && unveil(repo_path, "r") != 0)
3526 return got_error_from_errno2("unveil", repo_path);
3528 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3529 return got_error_from_errno2("unveil", worktree_path);
3531 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3532 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3534 error = got_privsep_unveil_exec_helpers();
3538 if (unveil(NULL, NULL) != 0)
3539 return got_error_from_errno("unveil");
3548 * Override default signal handlers before starting ncurses.
3549 * This should prevent ncurses from installing its own
3550 * broken cleanup() signal handler.
3552 signal(SIGWINCH, tog_sigwinch);
3553 signal(SIGPIPE, tog_sigpipe);
3554 signal(SIGCONT, tog_sigcont);
3555 signal(SIGINT, tog_sigint);
3556 signal(SIGTERM, tog_sigterm);
3560 halfdelay(1); /* Do fast refresh while initial view is loading. */
3563 intrflush(stdscr, FALSE);
3564 keypad(stdscr, TRUE);
3566 if (getenv("TOG_COLORS") != NULL) {
3568 use_default_colors();
3572 static const struct got_error *
3573 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3574 struct got_repository *repo, struct got_worktree *worktree)
3576 const struct got_error *err = NULL;
3579 *in_repo_path = strdup("/");
3580 if (*in_repo_path == NULL)
3581 return got_error_from_errno("strdup");
3586 const char *prefix = got_worktree_get_path_prefix(worktree);
3589 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3592 if (asprintf(in_repo_path, "%s%s%s", prefix,
3593 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3595 err = got_error_from_errno("asprintf");
3596 *in_repo_path = NULL;
3600 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3605 static const struct got_error *
3606 cmd_log(int argc, char *argv[])
3608 const struct got_error *error;
3609 struct got_repository *repo = NULL;
3610 struct got_worktree *worktree = NULL;
3611 struct got_object_id *start_id = NULL;
3612 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3613 char *start_commit = NULL, *label = NULL;
3614 struct got_reference *ref = NULL;
3615 const char *head_ref_name = NULL;
3616 int ch, log_branches = 0;
3617 struct tog_view *view;
3618 int *pack_fds = NULL;
3620 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3626 start_commit = optarg;
3629 repo_path = realpath(optarg, NULL);
3630 if (repo_path == NULL)
3631 return got_error_from_errno2("realpath",
3646 error = got_repo_pack_fds_open(&pack_fds);
3650 if (repo_path == NULL) {
3651 cwd = getcwd(NULL, 0);
3653 return got_error_from_errno("getcwd");
3654 error = got_worktree_open(&worktree, cwd);
3655 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3659 strdup(got_worktree_get_repo_path(worktree));
3661 repo_path = strdup(cwd);
3662 if (repo_path == NULL) {
3663 error = got_error_from_errno("strdup");
3668 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3672 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3679 error = apply_unveil(got_repo_get_path(repo),
3680 worktree ? got_worktree_get_root_path(worktree) : NULL);
3684 /* already loaded by tog_log_with_path()? */
3685 if (TAILQ_EMPTY(&tog_refs)) {
3686 error = tog_load_refs(repo, 0);
3691 if (start_commit == NULL) {
3692 error = got_repo_match_object_id(&start_id, &label,
3693 worktree ? got_worktree_get_head_ref_name(worktree) :
3694 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3697 head_ref_name = label;
3699 error = got_ref_open(&ref, repo, start_commit, 0);
3701 head_ref_name = got_ref_get_name(ref);
3702 else if (error->code != GOT_ERR_NOT_REF)
3704 error = got_repo_match_object_id(&start_id, NULL,
3705 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3710 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3712 error = got_error_from_errno("view_open");
3715 error = open_log_view(view, start_id, repo, head_ref_name,
3716 in_repo_path, log_branches);
3720 /* Release work tree lock. */
3721 got_worktree_close(worktree);
3724 error = view_loop(view);
3734 const struct got_error *close_err = got_repo_close(repo);
3739 got_worktree_close(worktree);
3741 const struct got_error *pack_err =
3742 got_repo_pack_fds_close(pack_fds);
3754 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3755 "object1 object2\n", getprogname());
3760 match_line(const char *line, regex_t *regex, size_t nmatch,
3761 regmatch_t *regmatch)
3763 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3766 static struct tog_color *
3767 match_color(struct tog_colors *colors, const char *line)
3769 struct tog_color *tc = NULL;
3771 STAILQ_FOREACH(tc, colors, entry) {
3772 if (match_line(line, &tc->regex, 0, NULL))
3779 static const struct got_error *
3780 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3781 WINDOW *window, int skipcol, regmatch_t *regmatch)
3783 const struct got_error *err = NULL;
3785 wchar_t *wline = NULL;
3786 int rme, rms, n, width, scrollx;
3787 int width0 = 0, width1 = 0, width2 = 0;
3788 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3792 rms = regmatch->rm_so;
3793 rme = regmatch->rm_eo;
3795 err = expand_tab(&exstr, line);
3799 /* Split the line into 3 segments, according to match offsets. */
3800 seg0 = strndup(exstr, rms);
3802 err = got_error_from_errno("strndup");
3805 seg1 = strndup(exstr + rms, rme - rms);
3807 err = got_error_from_errno("strndup");
3810 seg2 = strdup(exstr + rme);
3812 err = got_error_from_errno("strndup");
3816 /* draw up to matched token if we haven't scrolled past it */
3817 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3821 n = MAX(width0 - skipcol, 0);
3824 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3825 wlimit, col_tab_align, 1);
3828 waddwstr(window, &wline[scrollx]);
3838 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3842 wlen = wcslen(wline);
3844 width = wcwidth(wline[i]);
3846 /* should not happen, tabs are expanded */
3847 err = got_error(GOT_ERR_RANGE);
3850 if (width0 + w + width > skipcol)
3855 /* draw (visible part of) matched token (if scrolled into it) */
3856 if (width1 - w > 0) {
3857 wattron(window, A_STANDOUT);
3858 waddwstr(window, &wline[i]);
3859 wattroff(window, A_STANDOUT);
3860 wlimit -= (width1 - w);
3861 *wtotal += (width1 - w);
3865 if (wlimit > 0) { /* draw rest of line */
3867 if (skipcol > width0 + width1) {
3868 err = format_line(&wline, &width2, &scrollx, seg2,
3869 skipcol - (width0 + width1), wlimit,
3873 waddwstr(window, &wline[scrollx]);
3875 err = format_line(&wline, &width2, NULL, seg2, 0,
3876 wlimit, col_tab_align, 1);
3879 waddwstr(window, wline);
3893 gotoline(struct tog_view *view, int *lineno, int *nprinted)
3896 int *eof, *first, *selected;
3898 if (view->type == TOG_VIEW_DIFF) {
3899 struct tog_diff_view_state *s = &view->state.diff;
3901 first = &s->first_displayed_line;
3905 } else if (view->type == TOG_VIEW_BLAME) {
3906 struct tog_blame_view_state *s = &view->state.blame;
3908 first = &s->first_displayed_line;
3909 selected = &s->selected_line;
3915 /* Center gline in the middle of the page like vi(1). */
3916 if (*lineno < view->gline - (view->nlines - 3) / 2)
3918 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3927 *selected = view->gline <= (view->nlines - 3) / 2 ?
3928 view->gline : (view->nlines - 3) / 2 + 1;
3934 static const struct got_error *
3935 draw_file(struct tog_view *view, const char *header)
3937 struct tog_diff_view_state *s = &view->state.diff;
3938 regmatch_t *regmatch = &view->regmatch;
3939 const struct got_error *err;
3942 size_t linesize = 0;
3946 int max_lines = view->nlines;
3947 int nlines = s->nlines;
3950 s->lineno = s->first_displayed_line - 1;
3951 line_offset = s->lines[s->first_displayed_line - 1].offset;
3952 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3953 return got_error_from_errno("fseek");
3955 werase(view->window);
3957 if (view->gline > s->nlines - 1)
3958 view->gline = s->nlines - 1;
3961 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3962 1 : view->gline - (view->nlines - 3) / 2 :
3963 s->lineno + s->selected_line;
3965 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3966 return got_error_from_errno("asprintf");
3967 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3973 if (view_needs_focus_indication(view))
3974 wstandout(view->window);
3975 waddwstr(view->window, wline);
3978 while (width++ < view->ncols)
3979 waddch(view->window, ' ');
3980 if (view_needs_focus_indication(view))
3981 wstandend(view->window);
3991 while (max_lines > 0 && nprinted < max_lines) {
3992 enum got_diff_line_type linetype;
3995 linelen = getline(&line, &linesize, s->f);
3996 if (linelen == -1) {
4002 return got_ferror(s->f, GOT_ERR_IO);
4005 if (++s->lineno < s->first_displayed_line)
4007 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4009 if (s->lineno == view->hiline)
4012 /* Set view->maxx based on full line length. */
4013 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4019 view->maxx = MAX(view->maxx, width);
4023 linetype = s->lines[s->lineno].type;
4024 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4025 linetype < GOT_DIFF_LINE_CONTEXT)
4026 attr |= COLOR_PAIR(linetype);
4028 wattron(view->window, attr);
4029 if (s->first_displayed_line + nprinted == s->matched_line &&
4030 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4031 err = add_matched_line(&width, line, view->ncols, 0,
4032 view->window, view->x, regmatch);
4039 err = format_line(&wline, &width, &skip, line,
4040 view->x, view->ncols, 0, view->x ? 1 : 0);
4045 waddwstr(view->window, &wline[skip]);
4049 if (s->lineno == view->hiline) {
4050 /* highlight full gline length */
4051 while (width++ < view->ncols)
4052 waddch(view->window, ' ');
4054 if (width <= view->ncols - 1)
4055 waddch(view->window, '\n');
4058 wattroff(view->window, attr);
4059 if (++nprinted == 1)
4060 s->first_displayed_line = s->lineno;
4064 s->last_displayed_line = s->first_displayed_line +
4067 s->last_displayed_line = s->first_displayed_line;
4072 while (nprinted < view->nlines) {
4073 waddch(view->window, '\n');
4077 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4083 wstandout(view->window);
4084 waddwstr(view->window, wline);
4087 wstandend(view->window);
4094 get_datestr(time_t *time, char *datebuf)
4096 struct tm mytm, *tm;
4099 tm = gmtime_r(time, &mytm);
4102 s = asctime_r(tm, datebuf);
4105 p = strchr(s, '\n');
4111 static const struct got_error *
4112 get_changed_paths(struct got_pathlist_head *paths,
4113 struct got_commit_object *commit, struct got_repository *repo)
4115 const struct got_error *err = NULL;
4116 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4117 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4118 struct got_object_qid *qid;
4120 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4122 struct got_commit_object *pcommit;
4123 err = got_object_open_as_commit(&pcommit, repo,
4128 tree_id1 = got_object_id_dup(
4129 got_object_commit_get_tree_id(pcommit));
4130 if (tree_id1 == NULL) {
4131 got_object_commit_close(pcommit);
4132 return got_error_from_errno("got_object_id_dup");
4134 got_object_commit_close(pcommit);
4139 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4144 tree_id2 = got_object_commit_get_tree_id(commit);
4145 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4149 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4150 got_diff_tree_collect_changed_paths, paths, 0);
4153 got_object_tree_close(tree1);
4155 got_object_tree_close(tree2);
4160 static const struct got_error *
4161 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4162 off_t off, uint8_t type)
4164 struct got_diff_line *p;
4166 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4168 return got_error_from_errno("reallocarray");
4170 (*lines)[*nlines].offset = off;
4171 (*lines)[*nlines].type = type;
4177 static const struct got_error *
4178 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4179 struct got_object_id *commit_id, struct got_reflist_head *refs,
4180 struct got_repository *repo, FILE *outfile)
4182 const struct got_error *err = NULL;
4183 char datebuf[26], *datestr;
4184 struct got_commit_object *commit;
4185 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4186 time_t committer_time;
4187 const char *author, *committer;
4188 char *refs_str = NULL;
4189 struct got_pathlist_head changed_paths;
4190 struct got_pathlist_entry *pe;
4194 TAILQ_INIT(&changed_paths);
4197 err = build_refs_str(&refs_str, refs, commit_id, repo);
4202 err = got_object_open_as_commit(&commit, repo, commit_id);
4206 err = got_object_id_str(&id_str, commit_id);
4208 err = got_error_from_errno("got_object_id_str");
4212 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4216 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4217 refs_str ? refs_str : "", refs_str ? ")" : "");
4219 err = got_error_from_errno("fprintf");
4223 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4227 n = fprintf(outfile, "from: %s\n",
4228 got_object_commit_get_author(commit));
4230 err = got_error_from_errno("fprintf");
4234 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4238 committer_time = got_object_commit_get_committer_time(commit);
4239 datestr = get_datestr(&committer_time, datebuf);
4241 n = fprintf(outfile, "date: %s UTC\n", datestr);
4243 err = got_error_from_errno("fprintf");
4247 err = add_line_metadata(lines, nlines, outoff,
4248 GOT_DIFF_LINE_DATE);
4252 author = got_object_commit_get_author(commit);
4253 committer = got_object_commit_get_committer(commit);
4254 if (strcmp(author, committer) != 0) {
4255 n = fprintf(outfile, "via: %s\n", committer);
4257 err = got_error_from_errno("fprintf");
4261 err = add_line_metadata(lines, nlines, outoff,
4262 GOT_DIFF_LINE_AUTHOR);
4266 if (got_object_commit_get_nparents(commit) > 1) {
4267 const struct got_object_id_queue *parent_ids;
4268 struct got_object_qid *qid;
4270 parent_ids = got_object_commit_get_parent_ids(commit);
4271 STAILQ_FOREACH(qid, parent_ids, entry) {
4272 err = got_object_id_str(&id_str, &qid->id);
4275 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4277 err = got_error_from_errno("fprintf");
4281 err = add_line_metadata(lines, nlines, outoff,
4282 GOT_DIFF_LINE_META);
4290 err = got_object_commit_get_logmsg(&logmsg, commit);
4294 while ((line = strsep(&s, "\n")) != NULL) {
4295 n = fprintf(outfile, "%s\n", line);
4297 err = got_error_from_errno("fprintf");
4301 err = add_line_metadata(lines, nlines, outoff,
4302 GOT_DIFF_LINE_LOGMSG);
4307 err = get_changed_paths(&changed_paths, commit, repo);
4310 TAILQ_FOREACH(pe, &changed_paths, entry) {
4311 struct got_diff_changed_path *cp = pe->data;
4312 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4314 err = got_error_from_errno("fprintf");
4318 err = add_line_metadata(lines, nlines, outoff,
4319 GOT_DIFF_LINE_CHANGES);
4322 free((char *)pe->path);
4326 fputc('\n', outfile);
4328 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4330 got_pathlist_free(&changed_paths);
4334 got_object_commit_close(commit);
4343 static const struct got_error *
4344 create_diff(struct tog_diff_view_state *s)
4346 const struct got_error *err = NULL;
4351 s->lines = malloc(sizeof(*s->lines));
4352 if (s->lines == NULL)
4353 return got_error_from_errno("malloc");
4358 err = got_error_from_errno("got_opentemp");
4361 if (s->f && fclose(s->f) == EOF) {
4362 err = got_error_from_errno("fclose");
4368 err = got_object_get_type(&obj_type, s->repo, s->id1);
4370 err = got_object_get_type(&obj_type, s->repo, s->id2);
4375 case GOT_OBJ_TYPE_BLOB:
4376 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4377 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4378 s->label1, s->label2, tog_diff_algo, s->diff_context,
4379 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4381 case GOT_OBJ_TYPE_TREE:
4382 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4383 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4384 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4385 s->force_text_diff, s->repo, s->f);
4387 case GOT_OBJ_TYPE_COMMIT: {
4388 const struct got_object_id_queue *parent_ids;
4389 struct got_object_qid *pid;
4390 struct got_commit_object *commit2;
4391 struct got_reflist_head *refs;
4393 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4396 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4397 /* Show commit info if we're diffing to a parent/root commit. */
4398 if (s->id1 == NULL) {
4399 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4400 refs, s->repo, s->f);
4404 parent_ids = got_object_commit_get_parent_ids(commit2);
4405 STAILQ_FOREACH(pid, parent_ids, entry) {
4406 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4407 err = write_commit_info(&s->lines,
4408 &s->nlines, s->id2, refs, s->repo,
4416 got_object_commit_close(commit2);
4418 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4419 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4420 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4421 s->force_text_diff, s->repo, s->f);
4425 err = got_error(GOT_ERR_OBJ_TYPE);
4429 if (s->f && fflush(s->f) != 0 && err == NULL)
4430 err = got_error_from_errno("fflush");
4435 diff_view_indicate_progress(struct tog_view *view)
4437 mvwaddstr(view->window, 0, 0, "diffing...");
4442 static const struct got_error *
4443 search_start_diff_view(struct tog_view *view)
4445 struct tog_diff_view_state *s = &view->state.diff;
4447 s->matched_line = 0;
4451 static const struct got_error *
4452 search_next_diff_view(struct tog_view *view)
4454 struct tog_diff_view_state *s = &view->state.diff;
4455 const struct got_error *err = NULL;
4458 size_t linesize = 0;
4461 if (!view->searching) {
4462 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4466 if (s->matched_line) {
4467 if (view->searching == TOG_SEARCH_FORWARD)
4468 lineno = s->matched_line + 1;
4470 lineno = s->matched_line - 1;
4472 lineno = s->first_displayed_line;
4477 if (lineno <= 0 || lineno > s->nlines) {
4478 if (s->matched_line == 0) {
4479 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4483 if (view->searching == TOG_SEARCH_FORWARD)
4489 offset = s->lines[lineno - 1].offset;
4490 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4492 return got_error_from_errno("fseeko");
4494 linelen = getline(&line, &linesize, s->f);
4495 if (linelen != -1) {
4497 err = expand_tab(&exstr, line);
4500 if (match_line(exstr, &view->regex, 1,
4502 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4503 s->matched_line = lineno;
4509 if (view->searching == TOG_SEARCH_FORWARD)
4516 if (s->matched_line) {
4517 s->first_displayed_line = s->matched_line;
4518 s->selected_line = 1;
4524 static const struct got_error *
4525 close_diff_view(struct tog_view *view)
4527 const struct got_error *err = NULL;
4528 struct tog_diff_view_state *s = &view->state.diff;
4534 if (s->f && fclose(s->f) == EOF)
4535 err = got_error_from_errno("fclose");
4537 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4538 err = got_error_from_errno("fclose");
4540 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4541 err = got_error_from_errno("fclose");
4543 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4544 err = got_error_from_errno("close");
4546 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4547 err = got_error_from_errno("close");
4555 static const struct got_error *
4556 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4557 struct got_object_id *id2, const char *label1, const char *label2,
4558 int diff_context, int ignore_whitespace, int force_text_diff,
4559 struct tog_view *parent_view, struct got_repository *repo)
4561 const struct got_error *err;
4562 struct tog_diff_view_state *s = &view->state.diff;
4564 memset(s, 0, sizeof(*s));
4568 if (id1 != NULL && id2 != NULL) {
4570 err = got_object_get_type(&type1, repo, id1);
4573 err = got_object_get_type(&type2, repo, id2);
4578 return got_error(GOT_ERR_OBJ_TYPE);
4580 s->first_displayed_line = 1;
4581 s->last_displayed_line = view->nlines;
4582 s->selected_line = 1;
4590 s->id1 = got_object_id_dup(id1);
4592 return got_error_from_errno("got_object_id_dup");
4596 s->id2 = got_object_id_dup(id2);
4597 if (s->id2 == NULL) {
4598 err = got_error_from_errno("got_object_id_dup");
4602 s->f1 = got_opentemp();
4603 if (s->f1 == NULL) {
4604 err = got_error_from_errno("got_opentemp");
4608 s->f2 = got_opentemp();
4609 if (s->f2 == NULL) {
4610 err = got_error_from_errno("got_opentemp");
4614 s->fd1 = got_opentempfd();
4616 err = got_error_from_errno("got_opentempfd");
4620 s->fd2 = got_opentempfd();
4622 err = got_error_from_errno("got_opentempfd");
4626 s->first_displayed_line = 1;
4627 s->last_displayed_line = view->nlines;
4628 s->diff_context = diff_context;
4629 s->ignore_whitespace = ignore_whitespace;
4630 s->force_text_diff = force_text_diff;
4631 s->parent_view = parent_view;
4634 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4637 rc = init_pair(GOT_DIFF_LINE_MINUS,
4638 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4640 rc = init_pair(GOT_DIFF_LINE_PLUS,
4641 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4643 rc = init_pair(GOT_DIFF_LINE_HUNK,
4644 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4646 rc = init_pair(GOT_DIFF_LINE_META,
4647 get_color_value("TOG_COLOR_DIFF_META"), -1);
4649 rc = init_pair(GOT_DIFF_LINE_CHANGES,
4650 get_color_value("TOG_COLOR_DIFF_META"), -1);
4652 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4653 get_color_value("TOG_COLOR_DIFF_META"), -1);
4655 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4656 get_color_value("TOG_COLOR_DIFF_META"), -1);
4658 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4659 get_color_value("TOG_COLOR_AUTHOR"), -1);
4661 rc = init_pair(GOT_DIFF_LINE_DATE,
4662 get_color_value("TOG_COLOR_DATE"), -1);
4664 err = got_error(GOT_ERR_RANGE);
4669 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4670 view_is_splitscreen(view))
4671 show_log_view(parent_view); /* draw border */
4672 diff_view_indicate_progress(view);
4674 err = create_diff(s);
4676 view->show = show_diff_view;
4677 view->input = input_diff_view;
4678 view->reset = reset_diff_view;
4679 view->close = close_diff_view;
4680 view->search_start = search_start_diff_view;
4681 view->search_next = search_next_diff_view;
4684 close_diff_view(view);
4688 static const struct got_error *
4689 show_diff_view(struct tog_view *view)
4691 const struct got_error *err;
4692 struct tog_diff_view_state *s = &view->state.diff;
4693 char *id_str1 = NULL, *id_str2, *header;
4694 const char *label1, *label2;
4697 err = got_object_id_str(&id_str1, s->id1);
4700 label1 = s->label1 ? s->label1 : id_str1;
4702 label1 = "/dev/null";
4704 err = got_object_id_str(&id_str2, s->id2);
4707 label2 = s->label2 ? s->label2 : id_str2;
4709 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4710 err = got_error_from_errno("asprintf");
4718 err = draw_file(view, header);
4723 static const struct got_error *
4724 set_selected_commit(struct tog_diff_view_state *s,
4725 struct commit_queue_entry *entry)
4727 const struct got_error *err;
4728 const struct got_object_id_queue *parent_ids;
4729 struct got_commit_object *selected_commit;
4730 struct got_object_qid *pid;
4733 s->id2 = got_object_id_dup(entry->id);
4735 return got_error_from_errno("got_object_id_dup");
4737 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4740 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4742 pid = STAILQ_FIRST(parent_ids);
4743 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4744 got_object_commit_close(selected_commit);
4748 static const struct got_error *
4749 reset_diff_view(struct tog_view *view)
4751 struct tog_diff_view_state *s = &view->state.diff;
4754 wclear(view->window);
4755 s->first_displayed_line = 1;
4756 s->last_displayed_line = view->nlines;
4757 s->matched_line = 0;
4758 diff_view_indicate_progress(view);
4759 return create_diff(s);
4763 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4767 i = start = s->first_displayed_line - 1;
4769 while (s->lines[i].type != type) {
4773 return; /* do nothing, requested type not in file */
4776 s->selected_line = 1;
4777 s->first_displayed_line = i;
4781 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4785 i = start = s->first_displayed_line + 1;
4787 while (s->lines[i].type != type) {
4788 if (i == s->nlines - 1)
4791 return; /* do nothing, requested type not in file */
4794 s->selected_line = 1;
4795 s->first_displayed_line = i;
4798 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4800 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4803 static const struct got_error *
4804 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4806 const struct got_error *err = NULL;
4807 struct tog_diff_view_state *s = &view->state.diff;
4808 struct tog_log_view_state *ls;
4809 struct commit_queue_entry *old_selected_entry;
4811 size_t linesize = 0;
4813 int i, nscroll = view->nlines - 1, up = 0;
4815 s->lineno = s->first_displayed_line - 1 + s->selected_line;
4822 view->x = MAX(view->maxx - view->ncols / 3, 0);
4827 if (view->x + view->ncols / 3 < view->maxx)
4828 view->x += 2; /* move two columns right */
4834 view->x -= MIN(view->x, 2); /* move two columns back */
4841 s->force_text_diff = !s->force_text_diff;
4843 s->ignore_whitespace = !s->ignore_whitespace;
4844 err = reset_diff_view(view);
4848 s->first_displayed_line = 1;
4857 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4863 if (s->first_displayed_line > 1)
4864 s->first_displayed_line--;
4875 if (s->first_displayed_line == 1) {
4880 while (i++ < nscroll && s->first_displayed_line > 1)
4881 s->first_displayed_line--;
4887 s->first_displayed_line++;
4904 while (!s->eof && i++ < nscroll) {
4905 linelen = getline(&line, &linesize, s->f);
4906 s->first_displayed_line++;
4907 if (linelen == -1) {
4911 err = got_ferror(s->f, GOT_ERR_IO);
4918 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
4921 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
4924 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
4927 diff_next_index(s, GOT_DIFF_LINE_HUNK);
4930 if (s->diff_context > 0) {
4932 s->matched_line = 0;
4933 diff_view_indicate_progress(view);
4934 err = create_diff(s);
4935 if (s->first_displayed_line + view->nlines - 1 >
4937 s->first_displayed_line = 1;
4938 s->last_displayed_line = view->nlines;
4944 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4946 s->matched_line = 0;
4947 diff_view_indicate_progress(view);
4948 err = create_diff(s);
4960 if (s->parent_view == NULL) {
4964 s->parent_view->count = view->count;
4966 if (s->parent_view->type == TOG_VIEW_LOG) {
4967 ls = &s->parent_view->state.log;
4968 old_selected_entry = ls->selected_entry;
4970 err = input_log_view(NULL, s->parent_view,
4971 up ? KEY_UP : KEY_DOWN);
4974 view->count = s->parent_view->count;
4976 if (old_selected_entry == ls->selected_entry)
4979 err = set_selected_commit(s, ls->selected_entry);
4982 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4983 struct tog_blame_view_state *bs;
4984 struct got_object_id *id, *prev_id;
4986 bs = &s->parent_view->state.blame;
4987 prev_id = get_annotation_for_line(bs->blame.lines,
4988 bs->blame.nlines, bs->last_diffed_line);
4990 err = input_blame_view(&view, s->parent_view,
4991 up ? KEY_UP : KEY_DOWN);
4994 view->count = s->parent_view->count;
4996 if (prev_id == NULL)
4998 id = get_selected_commit_id(bs->blame.lines,
4999 bs->blame.nlines, bs->first_displayed_line,
5004 if (!got_object_id_cmp(prev_id, id))
5007 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5011 s->first_displayed_line = 1;
5012 s->last_displayed_line = view->nlines;
5013 s->matched_line = 0;
5016 diff_view_indicate_progress(view);
5017 err = create_diff(s);
5027 static const struct got_error *
5028 cmd_diff(int argc, char *argv[])
5030 const struct got_error *error = NULL;
5031 struct got_repository *repo = NULL;
5032 struct got_worktree *worktree = NULL;
5033 struct got_object_id *id1 = NULL, *id2 = NULL;
5034 char *repo_path = NULL, *cwd = NULL;
5035 char *id_str1 = NULL, *id_str2 = NULL;
5036 char *label1 = NULL, *label2 = NULL;
5037 int diff_context = 3, ignore_whitespace = 0;
5038 int ch, force_text_diff = 0;
5040 struct tog_view *view;
5041 int *pack_fds = NULL;
5043 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5046 force_text_diff = 1;
5049 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5052 errx(1, "number of context lines is %s: %s",
5056 repo_path = realpath(optarg, NULL);
5057 if (repo_path == NULL)
5058 return got_error_from_errno2("realpath",
5060 got_path_strip_trailing_slashes(repo_path);
5063 ignore_whitespace = 1;
5075 usage_diff(); /* TODO show local worktree changes */
5076 } else if (argc == 2) {
5082 error = got_repo_pack_fds_open(&pack_fds);
5086 if (repo_path == NULL) {
5087 cwd = getcwd(NULL, 0);
5089 return got_error_from_errno("getcwd");
5090 error = got_worktree_open(&worktree, cwd);
5091 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5095 strdup(got_worktree_get_repo_path(worktree));
5097 repo_path = strdup(cwd);
5098 if (repo_path == NULL) {
5099 error = got_error_from_errno("strdup");
5104 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5110 error = apply_unveil(got_repo_get_path(repo), NULL);
5114 error = tog_load_refs(repo, 0);
5118 error = got_repo_match_object_id(&id1, &label1, id_str1,
5119 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5123 error = got_repo_match_object_id(&id2, &label2, id_str2,
5124 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5128 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5130 error = got_error_from_errno("view_open");
5133 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5134 ignore_whitespace, force_text_diff, NULL, repo);
5137 error = view_loop(view);
5144 const struct got_error *close_err = got_repo_close(repo);
5149 got_worktree_close(worktree);
5151 const struct got_error *pack_err =
5152 got_repo_pack_fds_close(pack_fds);
5165 "usage: %s blame [-c commit] [-r repository-path] path\n",
5170 struct tog_blame_line {
5172 struct got_object_id *id;
5175 static const struct got_error *
5176 draw_blame(struct tog_view *view)
5178 struct tog_blame_view_state *s = &view->state.blame;
5179 struct tog_blame *blame = &s->blame;
5180 regmatch_t *regmatch = &view->regmatch;
5181 const struct got_error *err;
5182 int lineno = 0, nprinted = 0;
5184 size_t linesize = 0;
5188 struct tog_blame_line *blame_line;
5189 struct got_object_id *prev_id = NULL;
5191 struct tog_color *tc;
5193 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5198 werase(view->window);
5200 if (asprintf(&line, "commit %s", id_str) == -1) {
5201 err = got_error_from_errno("asprintf");
5206 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5211 if (view_needs_focus_indication(view))
5212 wstandout(view->window);
5213 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5215 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5216 waddwstr(view->window, wline);
5217 while (width++ < view->ncols)
5218 waddch(view->window, ' ');
5220 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5221 if (view_needs_focus_indication(view))
5222 wstandend(view->window);
5226 if (view->gline > blame->nlines)
5227 view->gline = blame->nlines;
5229 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5230 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5231 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5233 return got_error_from_errno("asprintf");
5236 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5241 waddwstr(view->window, wline);
5244 if (width < view->ncols - 1)
5245 waddch(view->window, '\n');
5249 while (nprinted < view->nlines - 2) {
5250 linelen = getline(&line, &linesize, blame->f);
5251 if (linelen == -1) {
5252 if (feof(blame->f)) {
5257 return got_ferror(blame->f, GOT_ERR_IO);
5259 if (++lineno < s->first_displayed_line)
5261 if (view->gline && !gotoline(view, &lineno, &nprinted))
5264 /* Set view->maxx based on full line length. */
5265 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5272 view->maxx = MAX(view->maxx, width);
5274 if (nprinted == s->selected_line - 1)
5275 wstandout(view->window);
5277 if (blame->nlines > 0) {
5278 blame_line = &blame->lines[lineno - 1];
5279 if (blame_line->annotated && prev_id &&
5280 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5281 !(nprinted == s->selected_line - 1)) {
5282 waddstr(view->window, " ");
5283 } else if (blame_line->annotated) {
5285 err = got_object_id_str(&id_str,
5291 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5293 wattr_on(view->window,
5294 COLOR_PAIR(tc->colorpair), NULL);
5295 wprintw(view->window, "%.8s", id_str);
5297 wattr_off(view->window,
5298 COLOR_PAIR(tc->colorpair), NULL);
5300 prev_id = blame_line->id;
5302 waddstr(view->window, "........");
5306 waddstr(view->window, "........");
5310 if (nprinted == s->selected_line - 1)
5311 wstandend(view->window);
5312 waddstr(view->window, " ");
5314 if (view->ncols <= 9) {
5316 } else if (s->first_displayed_line + nprinted ==
5318 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5319 err = add_matched_line(&width, line, view->ncols - 9, 9,
5320 view->window, view->x, regmatch);
5328 err = format_line(&wline, &width, &skip, line,
5329 view->x, view->ncols - 9, 9, 1);
5334 waddwstr(view->window, &wline[skip]);
5340 if (width <= view->ncols - 1)
5341 waddch(view->window, '\n');
5342 if (++nprinted == 1)
5343 s->first_displayed_line = lineno;
5346 s->last_displayed_line = lineno;
5353 static const struct got_error *
5354 blame_cb(void *arg, int nlines, int lineno,
5355 struct got_commit_object *commit, struct got_object_id *id)
5357 const struct got_error *err = NULL;
5358 struct tog_blame_cb_args *a = arg;
5359 struct tog_blame_line *line;
5362 if (nlines != a->nlines ||
5363 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5364 return got_error(GOT_ERR_RANGE);
5366 errcode = pthread_mutex_lock(&tog_mutex);
5368 return got_error_set_errno(errcode, "pthread_mutex_lock");
5370 if (*a->quit) { /* user has quit the blame view */
5371 err = got_error(GOT_ERR_ITER_COMPLETED);
5376 goto done; /* no change in this commit */
5378 line = &a->lines[lineno - 1];
5379 if (line->annotated)
5382 line->id = got_object_id_dup(id);
5383 if (line->id == NULL) {
5384 err = got_error_from_errno("got_object_id_dup");
5387 line->annotated = 1;
5389 errcode = pthread_mutex_unlock(&tog_mutex);
5391 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5396 blame_thread(void *arg)
5398 const struct got_error *err, *close_err;
5399 struct tog_blame_thread_args *ta = arg;
5400 struct tog_blame_cb_args *a = ta->cb_args;
5401 int errcode, fd1 = -1, fd2 = -1;
5402 FILE *f1 = NULL, *f2 = NULL;
5404 fd1 = got_opentempfd();
5406 return (void *)got_error_from_errno("got_opentempfd");
5408 fd2 = got_opentempfd();
5410 err = got_error_from_errno("got_opentempfd");
5414 f1 = got_opentemp();
5416 err = (void *)got_error_from_errno("got_opentemp");
5419 f2 = got_opentemp();
5421 err = (void *)got_error_from_errno("got_opentemp");
5425 err = block_signals_used_by_main_thread();
5429 err = got_blame(ta->path, a->commit_id, ta->repo,
5430 tog_diff_algo, blame_cb, ta->cb_args,
5431 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5432 if (err && err->code == GOT_ERR_CANCELLED)
5435 errcode = pthread_mutex_lock(&tog_mutex);
5437 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5441 close_err = got_repo_close(ta->repo);
5447 errcode = pthread_mutex_unlock(&tog_mutex);
5448 if (errcode && err == NULL)
5449 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5452 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5453 err = got_error_from_errno("close");
5454 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5455 err = got_error_from_errno("close");
5456 if (f1 && fclose(f1) == EOF && err == NULL)
5457 err = got_error_from_errno("fclose");
5458 if (f2 && fclose(f2) == EOF && err == NULL)
5459 err = got_error_from_errno("fclose");
5464 static struct got_object_id *
5465 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5466 int first_displayed_line, int selected_line)
5468 struct tog_blame_line *line;
5473 line = &lines[first_displayed_line - 1 + selected_line - 1];
5474 if (!line->annotated)
5480 static struct got_object_id *
5481 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5484 struct tog_blame_line *line;
5486 if (nlines <= 0 || lineno >= nlines)
5489 line = &lines[lineno - 1];
5490 if (!line->annotated)
5496 static const struct got_error *
5497 stop_blame(struct tog_blame *blame)
5499 const struct got_error *err = NULL;
5502 if (blame->thread) {
5504 errcode = pthread_mutex_unlock(&tog_mutex);
5506 return got_error_set_errno(errcode,
5507 "pthread_mutex_unlock");
5508 errcode = pthread_join(blame->thread, (void **)&err);
5510 return got_error_set_errno(errcode, "pthread_join");
5511 errcode = pthread_mutex_lock(&tog_mutex);
5513 return got_error_set_errno(errcode,
5514 "pthread_mutex_lock");
5515 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5517 blame->thread = NULL;
5519 if (blame->thread_args.repo) {
5520 const struct got_error *close_err;
5521 close_err = got_repo_close(blame->thread_args.repo);
5524 blame->thread_args.repo = NULL;
5527 if (fclose(blame->f) == EOF && err == NULL)
5528 err = got_error_from_errno("fclose");
5532 for (i = 0; i < blame->nlines; i++)
5533 free(blame->lines[i].id);
5535 blame->lines = NULL;
5537 free(blame->cb_args.commit_id);
5538 blame->cb_args.commit_id = NULL;
5539 if (blame->pack_fds) {
5540 const struct got_error *pack_err =
5541 got_repo_pack_fds_close(blame->pack_fds);
5544 blame->pack_fds = NULL;
5549 static const struct got_error *
5550 cancel_blame_view(void *arg)
5552 const struct got_error *err = NULL;
5556 errcode = pthread_mutex_lock(&tog_mutex);
5558 return got_error_set_errno(errcode,
5559 "pthread_mutex_unlock");
5562 err = got_error(GOT_ERR_CANCELLED);
5564 errcode = pthread_mutex_unlock(&tog_mutex);
5566 return got_error_set_errno(errcode,
5567 "pthread_mutex_lock");
5572 static const struct got_error *
5573 run_blame(struct tog_view *view)
5575 struct tog_blame_view_state *s = &view->state.blame;
5576 struct tog_blame *blame = &s->blame;
5577 const struct got_error *err = NULL;
5578 struct got_commit_object *commit = NULL;
5579 struct got_blob_object *blob = NULL;
5580 struct got_repository *thread_repo = NULL;
5581 struct got_object_id *obj_id = NULL;
5582 int obj_type, fd = -1;
5583 int *pack_fds = NULL;
5585 err = got_object_open_as_commit(&commit, s->repo,
5586 &s->blamed_commit->id);
5590 fd = got_opentempfd();
5592 err = got_error_from_errno("got_opentempfd");
5596 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5600 err = got_object_get_type(&obj_type, s->repo, obj_id);
5604 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5605 err = got_error(GOT_ERR_OBJ_TYPE);
5609 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5612 blame->f = got_opentemp();
5613 if (blame->f == NULL) {
5614 err = got_error_from_errno("got_opentemp");
5617 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5618 &blame->line_offsets, blame->f, blob);
5621 if (blame->nlines == 0) {
5622 s->blame_complete = 1;
5626 /* Don't include \n at EOF in the blame line count. */
5627 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5630 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5631 if (blame->lines == NULL) {
5632 err = got_error_from_errno("calloc");
5636 err = got_repo_pack_fds_open(&pack_fds);
5639 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5644 blame->pack_fds = pack_fds;
5645 blame->cb_args.view = view;
5646 blame->cb_args.lines = blame->lines;
5647 blame->cb_args.nlines = blame->nlines;
5648 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5649 if (blame->cb_args.commit_id == NULL) {
5650 err = got_error_from_errno("got_object_id_dup");
5653 blame->cb_args.quit = &s->done;
5655 blame->thread_args.path = s->path;
5656 blame->thread_args.repo = thread_repo;
5657 blame->thread_args.cb_args = &blame->cb_args;
5658 blame->thread_args.complete = &s->blame_complete;
5659 blame->thread_args.cancel_cb = cancel_blame_view;
5660 blame->thread_args.cancel_arg = &s->done;
5661 s->blame_complete = 0;
5663 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5664 s->first_displayed_line = 1;
5665 s->last_displayed_line = view->nlines;
5666 s->selected_line = 1;
5668 s->matched_line = 0;
5672 got_object_commit_close(commit);
5673 if (fd != -1 && close(fd) == -1 && err == NULL)
5674 err = got_error_from_errno("close");
5676 got_object_blob_close(blob);
5683 static const struct got_error *
5684 open_blame_view(struct tog_view *view, char *path,
5685 struct got_object_id *commit_id, struct got_repository *repo)
5687 const struct got_error *err = NULL;
5688 struct tog_blame_view_state *s = &view->state.blame;
5690 STAILQ_INIT(&s->blamed_commits);
5692 s->path = strdup(path);
5693 if (s->path == NULL)
5694 return got_error_from_errno("strdup");
5696 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5702 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5703 s->first_displayed_line = 1;
5704 s->last_displayed_line = view->nlines;
5705 s->selected_line = 1;
5706 s->blame_complete = 0;
5708 s->commit_id = commit_id;
5709 memset(&s->blame, 0, sizeof(s->blame));
5711 STAILQ_INIT(&s->colors);
5712 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5713 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5714 get_color_value("TOG_COLOR_COMMIT"));
5719 view->show = show_blame_view;
5720 view->input = input_blame_view;
5721 view->reset = reset_blame_view;
5722 view->close = close_blame_view;
5723 view->search_start = search_start_blame_view;
5724 view->search_next = search_next_blame_view;
5726 return run_blame(view);
5729 static const struct got_error *
5730 close_blame_view(struct tog_view *view)
5732 const struct got_error *err = NULL;
5733 struct tog_blame_view_state *s = &view->state.blame;
5735 if (s->blame.thread)
5736 err = stop_blame(&s->blame);
5738 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5739 struct got_object_qid *blamed_commit;
5740 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5741 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5742 got_object_qid_free(blamed_commit);
5746 free_colors(&s->colors);
5750 static const struct got_error *
5751 search_start_blame_view(struct tog_view *view)
5753 struct tog_blame_view_state *s = &view->state.blame;
5755 s->matched_line = 0;
5759 static const struct got_error *
5760 search_next_blame_view(struct tog_view *view)
5762 struct tog_blame_view_state *s = &view->state.blame;
5763 const struct got_error *err = NULL;
5766 size_t linesize = 0;
5769 if (!view->searching) {
5770 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5774 if (s->matched_line) {
5775 if (view->searching == TOG_SEARCH_FORWARD)
5776 lineno = s->matched_line + 1;
5778 lineno = s->matched_line - 1;
5780 lineno = s->first_displayed_line - 1 + s->selected_line;
5785 if (lineno <= 0 || lineno > s->blame.nlines) {
5786 if (s->matched_line == 0) {
5787 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5791 if (view->searching == TOG_SEARCH_FORWARD)
5794 lineno = s->blame.nlines;
5797 offset = s->blame.line_offsets[lineno - 1];
5798 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5800 return got_error_from_errno("fseeko");
5802 linelen = getline(&line, &linesize, s->blame.f);
5803 if (linelen != -1) {
5805 err = expand_tab(&exstr, line);
5808 if (match_line(exstr, &view->regex, 1,
5810 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5811 s->matched_line = lineno;
5817 if (view->searching == TOG_SEARCH_FORWARD)
5824 if (s->matched_line) {
5825 s->first_displayed_line = s->matched_line;
5826 s->selected_line = 1;
5832 static const struct got_error *
5833 show_blame_view(struct tog_view *view)
5835 const struct got_error *err = NULL;
5836 struct tog_blame_view_state *s = &view->state.blame;
5839 if (s->blame.thread == NULL && !s->blame_complete) {
5840 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5841 &s->blame.thread_args);
5843 return got_error_set_errno(errcode, "pthread_create");
5845 halfdelay(1); /* fast refresh while annotating */
5848 if (s->blame_complete)
5849 halfdelay(10); /* disable fast refresh */
5851 err = draw_blame(view);
5857 static const struct got_error *
5858 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5859 struct got_repository *repo, struct got_object_id *id)
5861 struct tog_view *log_view;
5862 const struct got_error *err = NULL;
5866 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5867 if (log_view == NULL)
5868 return got_error_from_errno("view_open");
5870 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5872 view_close(log_view);
5874 *new_view = log_view;
5879 static const struct got_error *
5880 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5882 const struct got_error *err = NULL, *thread_err = NULL;
5883 struct tog_view *diff_view;
5884 struct tog_blame_view_state *s = &view->state.blame;
5885 int eos, nscroll, begin_y = 0, begin_x = 0;
5887 eos = nscroll = view->nlines - 2;
5888 if (view_is_hsplit_top(view))
5896 view->x = MAX(view->maxx - view->ncols / 3, 0);
5901 if (view->x + view->ncols / 3 < view->maxx)
5902 view->x += 2; /* move two columns right */
5908 view->x -= MIN(view->x, 2); /* move two columns back */
5917 s->selected_line = 1;
5918 s->first_displayed_line = 1;
5923 if (s->blame.nlines < eos) {
5924 s->selected_line = s->blame.nlines;
5925 s->first_displayed_line = 1;
5927 s->selected_line = eos;
5928 s->first_displayed_line = s->blame.nlines - (eos - 1);
5935 if (s->selected_line > 1)
5937 else if (s->selected_line == 1 &&
5938 s->first_displayed_line > 1)
5939 s->first_displayed_line--;
5950 if (s->first_displayed_line == 1) {
5951 if (view->count > 1)
5953 s->selected_line = MAX(1, s->selected_line - nscroll);
5957 if (s->first_displayed_line > nscroll)
5958 s->first_displayed_line -= nscroll;
5960 s->first_displayed_line = 1;
5965 if (s->selected_line < eos && s->first_displayed_line +
5966 s->selected_line <= s->blame.nlines)
5968 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5969 s->first_displayed_line++;
5975 struct got_object_id *id = NULL;
5978 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5979 s->first_displayed_line, s->selected_line);
5983 struct got_commit_object *commit, *pcommit;
5984 struct got_object_qid *pid;
5985 struct got_object_id *blob_id = NULL;
5987 err = got_object_open_as_commit(&commit,
5992 got_object_commit_get_parent_ids(commit));
5994 got_object_commit_close(commit);
5997 /* Check if path history ends here. */
5998 err = got_object_open_as_commit(&pcommit,
6002 err = got_object_id_by_path(&blob_id, s->repo,
6004 got_object_commit_close(pcommit);
6006 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6008 got_object_commit_close(commit);
6011 err = got_object_get_type(&obj_type, s->repo,
6014 /* Can't blame non-blob type objects. */
6015 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6016 got_object_commit_close(commit);
6019 err = got_object_qid_alloc(&s->blamed_commit,
6021 got_object_commit_close(commit);
6023 if (got_object_id_cmp(id,
6024 &s->blamed_commit->id) == 0)
6026 err = got_object_qid_alloc(&s->blamed_commit,
6032 thread_err = stop_blame(&s->blame);
6036 STAILQ_INSERT_HEAD(&s->blamed_commits,
6037 s->blamed_commit, entry);
6038 err = run_blame(view);
6044 struct got_object_qid *first;
6047 first = STAILQ_FIRST(&s->blamed_commits);
6048 if (!got_object_id_cmp(&first->id, s->commit_id))
6051 thread_err = stop_blame(&s->blame);
6055 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6056 got_object_qid_free(s->blamed_commit);
6058 STAILQ_FIRST(&s->blamed_commits);
6059 err = run_blame(view);
6066 s->id_to_log = get_selected_commit_id(s->blame.lines,
6067 s->blame.nlines, s->first_displayed_line, s->selected_line);
6069 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6073 struct got_object_id *id = NULL;
6074 struct got_object_qid *pid;
6075 struct got_commit_object *commit = NULL;
6078 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6079 s->first_displayed_line, s->selected_line);
6082 err = got_object_open_as_commit(&commit, s->repo, id);
6085 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6087 /* traversed from diff view, release diff resources */
6088 err = close_diff_view(*new_view);
6091 diff_view = *new_view;
6093 if (view_is_parent_view(view))
6094 view_get_split(view, &begin_y, &begin_x);
6096 diff_view = view_open(0, 0, begin_y, begin_x,
6098 if (diff_view == NULL) {
6099 got_object_commit_close(commit);
6100 err = got_error_from_errno("view_open");
6104 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6105 id, NULL, NULL, 3, 0, 0, view, s->repo);
6106 got_object_commit_close(commit);
6108 view_close(diff_view);
6111 s->last_diffed_line = s->first_displayed_line - 1 +
6114 break; /* still open from active diff view */
6115 if (view_is_parent_view(view) &&
6116 view->mode == TOG_VIEW_SPLIT_HRZN) {
6117 err = view_init_hsplit(view, begin_y);
6123 diff_view->focussed = 1;
6124 diff_view->mode = view->mode;
6125 diff_view->nlines = view->lines - begin_y;
6126 if (view_is_parent_view(view)) {
6127 view_transfer_size(diff_view, view);
6128 err = view_close_child(view);
6131 err = view_set_child(view, diff_view);
6134 view->focus_child = 1;
6136 *new_view = diff_view;
6149 if (s->last_displayed_line >= s->blame.nlines &&
6150 s->selected_line >= MIN(s->blame.nlines,
6151 view->nlines - 2)) {
6155 if (s->last_displayed_line >= s->blame.nlines &&
6156 s->selected_line < view->nlines - 2) {
6158 MIN(nscroll, s->last_displayed_line -
6159 s->first_displayed_line - s->selected_line + 1);
6161 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6162 s->first_displayed_line += nscroll;
6164 s->first_displayed_line =
6165 s->blame.nlines - (view->nlines - 3);
6168 if (s->selected_line > view->nlines - 2) {
6169 s->selected_line = MIN(s->blame.nlines,
6177 return thread_err ? thread_err : err;
6180 static const struct got_error *
6181 reset_blame_view(struct tog_view *view)
6183 const struct got_error *err;
6184 struct tog_blame_view_state *s = &view->state.blame;
6188 err = stop_blame(&s->blame);
6192 return run_blame(view);
6195 static const struct got_error *
6196 cmd_blame(int argc, char *argv[])
6198 const struct got_error *error;
6199 struct got_repository *repo = NULL;
6200 struct got_worktree *worktree = NULL;
6201 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6202 char *link_target = NULL;
6203 struct got_object_id *commit_id = NULL;
6204 struct got_commit_object *commit = NULL;
6205 char *commit_id_str = NULL;
6207 struct tog_view *view;
6208 int *pack_fds = NULL;
6210 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6213 commit_id_str = optarg;
6216 repo_path = realpath(optarg, NULL);
6217 if (repo_path == NULL)
6218 return got_error_from_errno2("realpath",
6233 error = got_repo_pack_fds_open(&pack_fds);
6237 if (repo_path == NULL) {
6238 cwd = getcwd(NULL, 0);
6240 return got_error_from_errno("getcwd");
6241 error = got_worktree_open(&worktree, cwd);
6242 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6246 strdup(got_worktree_get_repo_path(worktree));
6248 repo_path = strdup(cwd);
6249 if (repo_path == NULL) {
6250 error = got_error_from_errno("strdup");
6255 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6259 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6266 error = apply_unveil(got_repo_get_path(repo), NULL);
6270 error = tog_load_refs(repo, 0);
6274 if (commit_id_str == NULL) {
6275 struct got_reference *head_ref;
6276 error = got_ref_open(&head_ref, repo, worktree ?
6277 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6280 error = got_ref_resolve(&commit_id, repo, head_ref);
6281 got_ref_close(head_ref);
6283 error = got_repo_match_object_id(&commit_id, NULL,
6284 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6289 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6291 error = got_error_from_errno("view_open");
6295 error = got_object_open_as_commit(&commit, repo, commit_id);
6299 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6304 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6309 /* Release work tree lock. */
6310 got_worktree_close(worktree);
6313 error = view_loop(view);
6321 got_object_commit_close(commit);
6323 got_worktree_close(worktree);
6325 const struct got_error *close_err = got_repo_close(repo);
6330 const struct got_error *pack_err =
6331 got_repo_pack_fds_close(pack_fds);
6339 static const struct got_error *
6340 draw_tree_entries(struct tog_view *view, const char *parent_path)
6342 struct tog_tree_view_state *s = &view->state.tree;
6343 const struct got_error *err = NULL;
6344 struct got_tree_entry *te;
6347 struct tog_color *tc;
6348 int width, n, nentries, i = 1;
6349 int limit = view->nlines;
6352 if (view_is_hsplit_top(view))
6353 --limit; /* border */
6355 werase(view->window);
6360 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6364 if (view_needs_focus_indication(view))
6365 wstandout(view->window);
6366 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6368 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6369 waddwstr(view->window, wline);
6372 while (width++ < view->ncols)
6373 waddch(view->window, ' ');
6375 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6376 if (view_needs_focus_indication(view))
6377 wstandend(view->window);
6382 if (s->first_displayed_entry) {
6383 i += got_tree_entry_get_index(s->first_displayed_entry);
6384 if (s->tree != s->root)
6385 ++i; /* account for ".." entry */
6387 nentries = got_object_tree_get_nentries(s->tree);
6388 if (asprintf(&index, "[%d/%d] %s",
6389 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6390 return got_error_from_errno("asprintf");
6391 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6395 waddwstr(view->window, wline);
6398 if (width < view->ncols - 1)
6399 waddch(view->window, '\n');
6402 waddch(view->window, '\n');
6406 if (s->first_displayed_entry == NULL) {
6407 te = got_object_tree_get_first_entry(s->tree);
6408 if (s->selected == 0) {
6410 wstandout(view->window);
6411 s->selected_entry = NULL;
6413 waddstr(view->window, " ..\n"); /* parent directory */
6414 if (s->selected == 0 && view->focussed)
6415 wstandend(view->window);
6422 te = s->first_displayed_entry;
6425 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6426 char *line = NULL, *id_str = NULL, *link_target = NULL;
6427 const char *modestr = "";
6430 te = got_object_tree_get_entry(s->tree, i);
6431 mode = got_tree_entry_get_mode(te);
6434 err = got_object_id_str(&id_str,
6435 got_tree_entry_get_id(te));
6437 return got_error_from_errno(
6438 "got_object_id_str");
6440 if (got_object_tree_entry_is_submodule(te))
6442 else if (S_ISLNK(mode)) {
6445 err = got_tree_entry_get_symlink_target(&link_target,
6451 for (i = 0; i < strlen(link_target); i++) {
6452 if (!isprint((unsigned char)link_target[i]))
6453 link_target[i] = '?';
6457 else if (S_ISDIR(mode))
6459 else if (mode & S_IXUSR)
6461 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6462 got_tree_entry_get_name(te), modestr,
6463 link_target ? " -> ": "",
6464 link_target ? link_target : "") == -1) {
6467 return got_error_from_errno("asprintf");
6471 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6477 if (n == s->selected) {
6479 wstandout(view->window);
6480 s->selected_entry = te;
6482 tc = match_color(&s->colors, line);
6484 wattr_on(view->window,
6485 COLOR_PAIR(tc->colorpair), NULL);
6486 waddwstr(view->window, wline);
6488 wattr_off(view->window,
6489 COLOR_PAIR(tc->colorpair), NULL);
6490 if (width < view->ncols - 1)
6491 waddch(view->window, '\n');
6492 if (n == s->selected && view->focussed)
6493 wstandend(view->window);
6499 s->last_displayed_entry = te;
6508 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6510 struct got_tree_entry *te;
6511 int isroot = s->tree == s->root;
6514 if (s->first_displayed_entry == NULL)
6517 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6518 while (i++ < maxscroll) {
6521 s->first_displayed_entry = NULL;
6524 s->first_displayed_entry = te;
6525 te = got_tree_entry_get_prev(s->tree, te);
6529 static const struct got_error *
6530 tree_scroll_down(struct tog_view *view, int maxscroll)
6532 struct tog_tree_view_state *s = &view->state.tree;
6533 struct got_tree_entry *next, *last;
6536 if (s->first_displayed_entry)
6537 next = got_tree_entry_get_next(s->tree,
6538 s->first_displayed_entry);
6540 next = got_object_tree_get_first_entry(s->tree);
6542 last = s->last_displayed_entry;
6543 while (next && n++ < maxscroll) {
6545 s->last_displayed_entry = last;
6546 last = got_tree_entry_get_next(s->tree, last);
6548 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6549 s->first_displayed_entry = next;
6550 next = got_tree_entry_get_next(s->tree, next);
6557 static const struct got_error *
6558 tree_entry_path(char **path, struct tog_parent_trees *parents,
6559 struct got_tree_entry *te)
6561 const struct got_error *err = NULL;
6562 struct tog_parent_tree *pt;
6563 size_t len = 2; /* for leading slash and NUL */
6565 TAILQ_FOREACH(pt, parents, entry)
6566 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6569 len += strlen(got_tree_entry_get_name(te));
6571 *path = calloc(1, len);
6573 return got_error_from_errno("calloc");
6576 pt = TAILQ_LAST(parents, tog_parent_trees);
6578 const char *name = got_tree_entry_get_name(pt->selected_entry);
6579 if (strlcat(*path, name, len) >= len) {
6580 err = got_error(GOT_ERR_NO_SPACE);
6583 if (strlcat(*path, "/", len) >= len) {
6584 err = got_error(GOT_ERR_NO_SPACE);
6587 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6590 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6591 err = got_error(GOT_ERR_NO_SPACE);
6603 static const struct got_error *
6604 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6605 struct got_tree_entry *te, struct tog_parent_trees *parents,
6606 struct got_object_id *commit_id, struct got_repository *repo)
6608 const struct got_error *err = NULL;
6610 struct tog_view *blame_view;
6614 err = tree_entry_path(&path, parents, te);
6618 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6619 if (blame_view == NULL) {
6620 err = got_error_from_errno("view_open");
6624 err = open_blame_view(blame_view, path, commit_id, repo);
6626 if (err->code == GOT_ERR_CANCELLED)
6628 view_close(blame_view);
6630 *new_view = blame_view;
6636 static const struct got_error *
6637 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6638 struct tog_tree_view_state *s)
6640 struct tog_view *log_view;
6641 const struct got_error *err = NULL;
6646 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6647 if (log_view == NULL)
6648 return got_error_from_errno("view_open");
6650 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6654 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6657 view_close(log_view);
6659 *new_view = log_view;
6664 static const struct got_error *
6665 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6666 const char *head_ref_name, struct got_repository *repo)
6668 const struct got_error *err = NULL;
6669 char *commit_id_str = NULL;
6670 struct tog_tree_view_state *s = &view->state.tree;
6671 struct got_commit_object *commit = NULL;
6673 TAILQ_INIT(&s->parents);
6674 STAILQ_INIT(&s->colors);
6676 s->commit_id = got_object_id_dup(commit_id);
6677 if (s->commit_id == NULL)
6678 return got_error_from_errno("got_object_id_dup");
6680 err = got_object_open_as_commit(&commit, repo, commit_id);
6685 * The root is opened here and will be closed when the view is closed.
6686 * Any visited subtrees and their path-wise parents are opened and
6689 err = got_object_open_as_tree(&s->root, repo,
6690 got_object_commit_get_tree_id(commit));
6695 err = got_object_id_str(&commit_id_str, commit_id);
6699 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6700 err = got_error_from_errno("asprintf");
6704 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6705 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6706 if (head_ref_name) {
6707 s->head_ref_name = strdup(head_ref_name);
6708 if (s->head_ref_name == NULL) {
6709 err = got_error_from_errno("strdup");
6715 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6716 err = add_color(&s->colors, "\\$$",
6717 TOG_COLOR_TREE_SUBMODULE,
6718 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6721 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6722 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6725 err = add_color(&s->colors, "/$",
6726 TOG_COLOR_TREE_DIRECTORY,
6727 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6731 err = add_color(&s->colors, "\\*$",
6732 TOG_COLOR_TREE_EXECUTABLE,
6733 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6737 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6738 get_color_value("TOG_COLOR_COMMIT"));
6743 view->show = show_tree_view;
6744 view->input = input_tree_view;
6745 view->close = close_tree_view;
6746 view->search_start = search_start_tree_view;
6747 view->search_next = search_next_tree_view;
6749 free(commit_id_str);
6751 got_object_commit_close(commit);
6753 close_tree_view(view);
6757 static const struct got_error *
6758 close_tree_view(struct tog_view *view)
6760 struct tog_tree_view_state *s = &view->state.tree;
6762 free_colors(&s->colors);
6763 free(s->tree_label);
6764 s->tree_label = NULL;
6766 s->commit_id = NULL;
6767 free(s->head_ref_name);
6768 s->head_ref_name = NULL;
6769 while (!TAILQ_EMPTY(&s->parents)) {
6770 struct tog_parent_tree *parent;
6771 parent = TAILQ_FIRST(&s->parents);
6772 TAILQ_REMOVE(&s->parents, parent, entry);
6773 if (parent->tree != s->root)
6774 got_object_tree_close(parent->tree);
6778 if (s->tree != NULL && s->tree != s->root)
6779 got_object_tree_close(s->tree);
6781 got_object_tree_close(s->root);
6785 static const struct got_error *
6786 search_start_tree_view(struct tog_view *view)
6788 struct tog_tree_view_state *s = &view->state.tree;
6790 s->matched_entry = NULL;
6795 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6797 regmatch_t regmatch;
6799 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6803 static const struct got_error *
6804 search_next_tree_view(struct tog_view *view)
6806 struct tog_tree_view_state *s = &view->state.tree;
6807 struct got_tree_entry *te = NULL;
6809 if (!view->searching) {
6810 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6814 if (s->matched_entry) {
6815 if (view->searching == TOG_SEARCH_FORWARD) {
6816 if (s->selected_entry)
6817 te = got_tree_entry_get_next(s->tree,
6820 te = got_object_tree_get_first_entry(s->tree);
6822 if (s->selected_entry == NULL)
6823 te = got_object_tree_get_last_entry(s->tree);
6825 te = got_tree_entry_get_prev(s->tree,
6829 if (s->selected_entry)
6830 te = s->selected_entry;
6831 else if (view->searching == TOG_SEARCH_FORWARD)
6832 te = got_object_tree_get_first_entry(s->tree);
6834 te = got_object_tree_get_last_entry(s->tree);
6839 if (s->matched_entry == NULL) {
6840 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6843 if (view->searching == TOG_SEARCH_FORWARD)
6844 te = got_object_tree_get_first_entry(s->tree);
6846 te = got_object_tree_get_last_entry(s->tree);
6849 if (match_tree_entry(te, &view->regex)) {
6850 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6851 s->matched_entry = te;
6855 if (view->searching == TOG_SEARCH_FORWARD)
6856 te = got_tree_entry_get_next(s->tree, te);
6858 te = got_tree_entry_get_prev(s->tree, te);
6861 if (s->matched_entry) {
6862 s->first_displayed_entry = s->matched_entry;
6869 static const struct got_error *
6870 show_tree_view(struct tog_view *view)
6872 const struct got_error *err = NULL;
6873 struct tog_tree_view_state *s = &view->state.tree;
6876 err = tree_entry_path(&parent_path, &s->parents, NULL);
6880 err = draw_tree_entries(view, parent_path);
6887 static const struct got_error *
6888 tree_goto_line(struct tog_view *view, int nlines)
6890 const struct got_error *err = NULL;
6891 struct tog_tree_view_state *s = &view->state.tree;
6892 struct got_tree_entry **fte, **lte, **ste;
6893 int g, last, first = 1, i = 1;
6894 int root = s->tree == s->root;
6895 int off = root ? 1 : 2;
6902 else if (g > got_object_tree_get_nentries(s->tree))
6903 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6905 fte = &s->first_displayed_entry;
6906 lte = &s->last_displayed_entry;
6907 ste = &s->selected_entry;
6910 first = got_tree_entry_get_index(*fte);
6911 first += off; /* account for ".." */
6913 last = got_tree_entry_get_index(*lte);
6916 if (g >= first && g <= last && g - first < nlines) {
6917 s->selected = g - first;
6918 return NULL; /* gline is on the current page */
6922 i = got_tree_entry_get_index(*ste);
6927 err = tree_scroll_down(view, g - i);
6930 if (got_tree_entry_get_index(*lte) >=
6931 got_object_tree_get_nentries(s->tree) - 1 &&
6932 first + s->selected < g &&
6933 s->selected < s->ndisplayed - 1) {
6934 first = got_tree_entry_get_index(*fte);
6936 s->selected = g - first;
6939 tree_scroll_up(s, i - g);
6942 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6943 s->selected = g - 1;
6948 static const struct got_error *
6949 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6951 const struct got_error *err = NULL;
6952 struct tog_tree_view_state *s = &view->state.tree;
6953 struct got_tree_entry *te;
6954 int n, nscroll = view->nlines - 3;
6957 return tree_goto_line(view, nscroll);
6961 s->show_ids = !s->show_ids;
6966 if (!s->selected_entry)
6968 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6972 err = view_request_new(new_view, view, TOG_VIEW_REF);
6978 if (s->tree == s->root)
6979 s->first_displayed_entry =
6980 got_object_tree_get_first_entry(s->tree);
6982 s->first_displayed_entry = NULL;
6986 int eos = view->nlines - 3;
6988 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6992 te = got_object_tree_get_last_entry(s->tree);
6993 for (n = 0; n < eos; n++) {
6995 if (s->tree != s->root) {
6996 s->first_displayed_entry = NULL;
7001 s->first_displayed_entry = te;
7002 te = got_tree_entry_get_prev(s->tree, te);
7005 s->selected = n - 1;
7011 if (s->selected > 0) {
7015 tree_scroll_up(s, 1);
7016 if (s->selected_entry == NULL ||
7017 (s->tree == s->root && s->selected_entry ==
7018 got_object_tree_get_first_entry(s->tree)))
7028 if (s->tree == s->root) {
7029 if (got_object_tree_get_first_entry(s->tree) ==
7030 s->first_displayed_entry)
7031 s->selected -= MIN(s->selected, nscroll);
7033 if (s->first_displayed_entry == NULL)
7034 s->selected -= MIN(s->selected, nscroll);
7036 tree_scroll_up(s, MAX(0, nscroll));
7037 if (s->selected_entry == NULL ||
7038 (s->tree == s->root && s->selected_entry ==
7039 got_object_tree_get_first_entry(s->tree)))
7045 if (s->selected < s->ndisplayed - 1) {
7049 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7051 /* can't scroll any further */
7055 tree_scroll_down(view, 1);
7065 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7067 /* can't scroll any further; move cursor down */
7068 if (s->selected < s->ndisplayed - 1)
7069 s->selected += MIN(nscroll,
7070 s->ndisplayed - s->selected - 1);
7075 tree_scroll_down(view, nscroll);
7080 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7081 struct tog_parent_tree *parent;
7082 /* user selected '..' */
7083 if (s->tree == s->root) {
7087 parent = TAILQ_FIRST(&s->parents);
7088 TAILQ_REMOVE(&s->parents, parent,
7090 got_object_tree_close(s->tree);
7091 s->tree = parent->tree;
7092 s->first_displayed_entry =
7093 parent->first_displayed_entry;
7095 parent->selected_entry;
7096 s->selected = parent->selected;
7097 if (s->selected > view->nlines - 3) {
7098 err = offset_selection_down(view);
7103 } else if (S_ISDIR(got_tree_entry_get_mode(
7104 s->selected_entry))) {
7105 struct got_tree_object *subtree;
7107 err = got_object_open_as_tree(&subtree, s->repo,
7108 got_tree_entry_get_id(s->selected_entry));
7111 err = tree_view_visit_subtree(s, subtree);
7113 got_object_tree_close(subtree);
7116 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7117 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7120 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7121 s->selected = view->nlines - 4;
7137 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7142 static const struct got_error *
7143 cmd_tree(int argc, char *argv[])
7145 const struct got_error *error;
7146 struct got_repository *repo = NULL;
7147 struct got_worktree *worktree = NULL;
7148 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7149 struct got_object_id *commit_id = NULL;
7150 struct got_commit_object *commit = NULL;
7151 const char *commit_id_arg = NULL;
7153 struct got_reference *ref = NULL;
7154 const char *head_ref_name = NULL;
7156 struct tog_view *view;
7157 int *pack_fds = NULL;
7159 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7162 commit_id_arg = optarg;
7165 repo_path = realpath(optarg, NULL);
7166 if (repo_path == NULL)
7167 return got_error_from_errno2("realpath",
7182 error = got_repo_pack_fds_open(&pack_fds);
7186 if (repo_path == NULL) {
7187 cwd = getcwd(NULL, 0);
7189 return got_error_from_errno("getcwd");
7190 error = got_worktree_open(&worktree, cwd);
7191 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7195 strdup(got_worktree_get_repo_path(worktree));
7197 repo_path = strdup(cwd);
7198 if (repo_path == NULL) {
7199 error = got_error_from_errno("strdup");
7204 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7208 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7215 error = apply_unveil(got_repo_get_path(repo), NULL);
7219 error = tog_load_refs(repo, 0);
7223 if (commit_id_arg == NULL) {
7224 error = got_repo_match_object_id(&commit_id, &label,
7225 worktree ? got_worktree_get_head_ref_name(worktree) :
7226 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7229 head_ref_name = label;
7231 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7233 head_ref_name = got_ref_get_name(ref);
7234 else if (error->code != GOT_ERR_NOT_REF)
7236 error = got_repo_match_object_id(&commit_id, NULL,
7237 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7242 error = got_object_open_as_commit(&commit, repo, commit_id);
7246 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7248 error = got_error_from_errno("view_open");
7251 error = open_tree_view(view, commit_id, head_ref_name, repo);
7254 if (!got_path_is_root_dir(in_repo_path)) {
7255 error = tree_view_walk_path(&view->state.tree, commit,
7262 /* Release work tree lock. */
7263 got_worktree_close(worktree);
7266 error = view_loop(view);
7275 const struct got_error *close_err = got_repo_close(repo);
7280 const struct got_error *pack_err =
7281 got_repo_pack_fds_close(pack_fds);
7289 static const struct got_error *
7290 ref_view_load_refs(struct tog_ref_view_state *s)
7292 struct got_reflist_entry *sre;
7293 struct tog_reflist_entry *re;
7296 TAILQ_FOREACH(sre, &tog_refs, entry) {
7297 if (strncmp(got_ref_get_name(sre->ref),
7298 "refs/got/", 9) == 0 &&
7299 strncmp(got_ref_get_name(sre->ref),
7300 "refs/got/backup/", 16) != 0)
7303 re = malloc(sizeof(*re));
7305 return got_error_from_errno("malloc");
7307 re->ref = got_ref_dup(sre->ref);
7308 if (re->ref == NULL)
7309 return got_error_from_errno("got_ref_dup");
7310 re->idx = s->nrefs++;
7311 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7314 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7319 ref_view_free_refs(struct tog_ref_view_state *s)
7321 struct tog_reflist_entry *re;
7323 while (!TAILQ_EMPTY(&s->refs)) {
7324 re = TAILQ_FIRST(&s->refs);
7325 TAILQ_REMOVE(&s->refs, re, entry);
7326 got_ref_close(re->ref);
7331 static const struct got_error *
7332 open_ref_view(struct tog_view *view, struct got_repository *repo)
7334 const struct got_error *err = NULL;
7335 struct tog_ref_view_state *s = &view->state.ref;
7337 s->selected_entry = 0;
7340 TAILQ_INIT(&s->refs);
7341 STAILQ_INIT(&s->colors);
7343 err = ref_view_load_refs(s);
7347 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7348 err = add_color(&s->colors, "^refs/heads/",
7349 TOG_COLOR_REFS_HEADS,
7350 get_color_value("TOG_COLOR_REFS_HEADS"));
7354 err = add_color(&s->colors, "^refs/tags/",
7355 TOG_COLOR_REFS_TAGS,
7356 get_color_value("TOG_COLOR_REFS_TAGS"));
7360 err = add_color(&s->colors, "^refs/remotes/",
7361 TOG_COLOR_REFS_REMOTES,
7362 get_color_value("TOG_COLOR_REFS_REMOTES"));
7366 err = add_color(&s->colors, "^refs/got/backup/",
7367 TOG_COLOR_REFS_BACKUP,
7368 get_color_value("TOG_COLOR_REFS_BACKUP"));
7373 view->show = show_ref_view;
7374 view->input = input_ref_view;
7375 view->close = close_ref_view;
7376 view->search_start = search_start_ref_view;
7377 view->search_next = search_next_ref_view;
7380 free_colors(&s->colors);
7384 static const struct got_error *
7385 close_ref_view(struct tog_view *view)
7387 struct tog_ref_view_state *s = &view->state.ref;
7389 ref_view_free_refs(s);
7390 free_colors(&s->colors);
7395 static const struct got_error *
7396 resolve_reflist_entry(struct got_object_id **commit_id,
7397 struct tog_reflist_entry *re, struct got_repository *repo)
7399 const struct got_error *err = NULL;
7400 struct got_object_id *obj_id;
7401 struct got_tag_object *tag = NULL;
7406 err = got_ref_resolve(&obj_id, repo, re->ref);
7410 err = got_object_get_type(&obj_type, repo, obj_id);
7415 case GOT_OBJ_TYPE_COMMIT:
7416 *commit_id = obj_id;
7418 case GOT_OBJ_TYPE_TAG:
7419 err = got_object_open_as_tag(&tag, repo, obj_id);
7423 err = got_object_get_type(&obj_type, repo,
7424 got_object_tag_get_object_id(tag));
7427 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7428 err = got_error(GOT_ERR_OBJ_TYPE);
7431 *commit_id = got_object_id_dup(
7432 got_object_tag_get_object_id(tag));
7433 if (*commit_id == NULL) {
7434 err = got_error_from_errno("got_object_id_dup");
7439 err = got_error(GOT_ERR_OBJ_TYPE);
7445 got_object_tag_close(tag);
7453 static const struct got_error *
7454 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7455 struct tog_reflist_entry *re, struct got_repository *repo)
7457 struct tog_view *log_view;
7458 const struct got_error *err = NULL;
7459 struct got_object_id *commit_id = NULL;
7463 err = resolve_reflist_entry(&commit_id, re, repo);
7465 if (err->code != GOT_ERR_OBJ_TYPE)
7471 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7472 if (log_view == NULL) {
7473 err = got_error_from_errno("view_open");
7477 err = open_log_view(log_view, commit_id, repo,
7478 got_ref_get_name(re->ref), "", 0);
7481 view_close(log_view);
7483 *new_view = log_view;
7489 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7491 struct tog_reflist_entry *re;
7494 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7497 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7498 while (i++ < maxscroll) {
7501 s->first_displayed_entry = re;
7502 re = TAILQ_PREV(re, tog_reflist_head, entry);
7506 static const struct got_error *
7507 ref_scroll_down(struct tog_view *view, int maxscroll)
7509 struct tog_ref_view_state *s = &view->state.ref;
7510 struct tog_reflist_entry *next, *last;
7513 if (s->first_displayed_entry)
7514 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7516 next = TAILQ_FIRST(&s->refs);
7518 last = s->last_displayed_entry;
7519 while (next && n++ < maxscroll) {
7521 s->last_displayed_entry = last;
7522 last = TAILQ_NEXT(last, entry);
7524 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7525 s->first_displayed_entry = next;
7526 next = TAILQ_NEXT(next, entry);
7533 static const struct got_error *
7534 search_start_ref_view(struct tog_view *view)
7536 struct tog_ref_view_state *s = &view->state.ref;
7538 s->matched_entry = NULL;
7543 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7545 regmatch_t regmatch;
7547 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7551 static const struct got_error *
7552 search_next_ref_view(struct tog_view *view)
7554 struct tog_ref_view_state *s = &view->state.ref;
7555 struct tog_reflist_entry *re = NULL;
7557 if (!view->searching) {
7558 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7562 if (s->matched_entry) {
7563 if (view->searching == TOG_SEARCH_FORWARD) {
7564 if (s->selected_entry)
7565 re = TAILQ_NEXT(s->selected_entry, entry);
7567 re = TAILQ_PREV(s->selected_entry,
7568 tog_reflist_head, entry);
7570 if (s->selected_entry == NULL)
7571 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7573 re = TAILQ_PREV(s->selected_entry,
7574 tog_reflist_head, entry);
7577 if (s->selected_entry)
7578 re = s->selected_entry;
7579 else if (view->searching == TOG_SEARCH_FORWARD)
7580 re = TAILQ_FIRST(&s->refs);
7582 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7587 if (s->matched_entry == NULL) {
7588 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7591 if (view->searching == TOG_SEARCH_FORWARD)
7592 re = TAILQ_FIRST(&s->refs);
7594 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7597 if (match_reflist_entry(re, &view->regex)) {
7598 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7599 s->matched_entry = re;
7603 if (view->searching == TOG_SEARCH_FORWARD)
7604 re = TAILQ_NEXT(re, entry);
7606 re = TAILQ_PREV(re, tog_reflist_head, entry);
7609 if (s->matched_entry) {
7610 s->first_displayed_entry = s->matched_entry;
7617 static const struct got_error *
7618 show_ref_view(struct tog_view *view)
7620 const struct got_error *err = NULL;
7621 struct tog_ref_view_state *s = &view->state.ref;
7622 struct tog_reflist_entry *re;
7625 struct tog_color *tc;
7627 int limit = view->nlines;
7629 werase(view->window);
7632 if (view_is_hsplit_top(view))
7633 --limit; /* border */
7638 re = s->first_displayed_entry;
7640 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7642 return got_error_from_errno("asprintf");
7644 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7649 if (view_needs_focus_indication(view))
7650 wstandout(view->window);
7651 waddwstr(view->window, wline);
7652 while (width++ < view->ncols)
7653 waddch(view->window, ' ');
7654 if (view_needs_focus_indication(view))
7655 wstandend(view->window);
7664 while (re && limit > 0) {
7666 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7669 struct got_commit_object *ci;
7670 struct got_tag_object *tag;
7671 struct got_object_id *id;
7675 err = got_ref_resolve(&id, s->repo, re->ref);
7678 err = got_object_open_as_tag(&tag, s->repo, id);
7680 if (err->code != GOT_ERR_OBJ_TYPE) {
7684 err = got_object_open_as_commit(&ci, s->repo,
7690 t = got_object_commit_get_committer_time(ci);
7691 got_object_commit_close(ci);
7693 t = got_object_tag_get_tagger_time(tag);
7694 got_object_tag_close(tag);
7697 if (gmtime_r(&t, &tm) == NULL)
7698 return got_error_from_errno("gmtime_r");
7699 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7700 return got_error(GOT_ERR_NO_SPACE);
7702 if (got_ref_is_symbolic(re->ref)) {
7703 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7704 ymd : "", got_ref_get_name(re->ref),
7705 got_ref_get_symref_target(re->ref)) == -1)
7706 return got_error_from_errno("asprintf");
7707 } else if (s->show_ids) {
7708 struct got_object_id *id;
7710 err = got_ref_resolve(&id, s->repo, re->ref);
7713 err = got_object_id_str(&id_str, id);
7718 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7719 got_ref_get_name(re->ref), id_str) == -1) {
7720 err = got_error_from_errno("asprintf");
7727 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7728 got_ref_get_name(re->ref)) == -1)
7729 return got_error_from_errno("asprintf");
7731 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7737 if (n == s->selected) {
7739 wstandout(view->window);
7740 s->selected_entry = re;
7742 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7744 wattr_on(view->window,
7745 COLOR_PAIR(tc->colorpair), NULL);
7746 waddwstr(view->window, wline);
7748 wattr_off(view->window,
7749 COLOR_PAIR(tc->colorpair), NULL);
7750 if (width < view->ncols - 1)
7751 waddch(view->window, '\n');
7752 if (n == s->selected && view->focussed)
7753 wstandend(view->window);
7759 s->last_displayed_entry = re;
7762 re = TAILQ_NEXT(re, entry);
7769 static const struct got_error *
7770 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7771 struct tog_reflist_entry *re, struct got_repository *repo)
7773 const struct got_error *err = NULL;
7774 struct got_object_id *commit_id = NULL;
7775 struct tog_view *tree_view;
7779 err = resolve_reflist_entry(&commit_id, re, repo);
7781 if (err->code != GOT_ERR_OBJ_TYPE)
7788 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7789 if (tree_view == NULL) {
7790 err = got_error_from_errno("view_open");
7794 err = open_tree_view(tree_view, commit_id,
7795 got_ref_get_name(re->ref), repo);
7799 *new_view = tree_view;
7805 static const struct got_error *
7806 ref_goto_line(struct tog_view *view, int nlines)
7808 const struct got_error *err = NULL;
7809 struct tog_ref_view_state *s = &view->state.ref;
7810 int g, idx = s->selected_entry->idx;
7817 else if (g > s->nrefs)
7820 if (g >= s->first_displayed_entry->idx + 1 &&
7821 g <= s->last_displayed_entry->idx + 1 &&
7822 g - s->first_displayed_entry->idx - 1 < nlines) {
7823 s->selected = g - s->first_displayed_entry->idx - 1;
7828 err = ref_scroll_down(view, g - idx - 1);
7831 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7832 s->first_displayed_entry->idx + s->selected < g &&
7833 s->selected < s->ndisplayed - 1)
7834 s->selected = g - s->first_displayed_entry->idx - 1;
7835 } else if (idx + 1 > g)
7836 ref_scroll_up(s, idx - g + 1);
7838 if (g < nlines && s->first_displayed_entry->idx == 0)
7839 s->selected = g - 1;
7845 static const struct got_error *
7846 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7848 const struct got_error *err = NULL;
7849 struct tog_ref_view_state *s = &view->state.ref;
7850 struct tog_reflist_entry *re;
7851 int n, nscroll = view->nlines - 1;
7854 return ref_goto_line(view, nscroll);
7858 s->show_ids = !s->show_ids;
7862 s->show_date = !s->show_date;
7866 s->sort_by_date = !s->sort_by_date;
7868 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7869 got_ref_cmp_by_commit_timestamp_descending :
7870 tog_ref_cmp_by_name, s->repo);
7873 got_reflist_object_id_map_free(tog_refs_idmap);
7874 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7875 &tog_refs, s->repo);
7878 ref_view_free_refs(s);
7879 err = ref_view_load_refs(s);
7884 if (!s->selected_entry)
7886 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7890 if (!s->selected_entry)
7892 err = view_request_new(new_view, view, TOG_VIEW_TREE);
7898 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7902 int eos = view->nlines - 1;
7904 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7908 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7909 for (n = 0; n < eos; n++) {
7912 s->first_displayed_entry = re;
7913 re = TAILQ_PREV(re, tog_reflist_head, entry);
7916 s->selected = n - 1;
7922 if (s->selected > 0) {
7926 ref_scroll_up(s, 1);
7927 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7937 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7938 s->selected -= MIN(nscroll, s->selected);
7939 ref_scroll_up(s, MAX(0, nscroll));
7940 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7946 if (s->selected < s->ndisplayed - 1) {
7950 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7951 /* can't scroll any further */
7955 ref_scroll_down(view, 1);
7965 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7966 /* can't scroll any further; move cursor down */
7967 if (s->selected < s->ndisplayed - 1)
7968 s->selected += MIN(nscroll,
7969 s->ndisplayed - s->selected - 1);
7970 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7971 s->selected += s->ndisplayed - s->selected - 1;
7975 ref_scroll_down(view, nscroll);
7980 err = tog_load_refs(s->repo, s->sort_by_date);
7983 ref_view_free_refs(s);
7984 err = ref_view_load_refs(s);
7987 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7988 s->selected = view->nlines - 2;
8002 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8007 static const struct got_error *
8008 cmd_ref(int argc, char *argv[])
8010 const struct got_error *error;
8011 struct got_repository *repo = NULL;
8012 struct got_worktree *worktree = NULL;
8013 char *cwd = NULL, *repo_path = NULL;
8015 struct tog_view *view;
8016 int *pack_fds = NULL;
8018 while ((ch = getopt(argc, argv, "r:")) != -1) {
8021 repo_path = realpath(optarg, NULL);
8022 if (repo_path == NULL)
8023 return got_error_from_errno2("realpath",
8038 error = got_repo_pack_fds_open(&pack_fds);
8042 if (repo_path == NULL) {
8043 cwd = getcwd(NULL, 0);
8045 return got_error_from_errno("getcwd");
8046 error = got_worktree_open(&worktree, cwd);
8047 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8051 strdup(got_worktree_get_repo_path(worktree));
8053 repo_path = strdup(cwd);
8054 if (repo_path == NULL) {
8055 error = got_error_from_errno("strdup");
8060 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8066 error = apply_unveil(got_repo_get_path(repo), NULL);
8070 error = tog_load_refs(repo, 0);
8074 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8076 error = got_error_from_errno("view_open");
8080 error = open_ref_view(view, repo);
8085 /* Release work tree lock. */
8086 got_worktree_close(worktree);
8089 error = view_loop(view);
8094 const struct got_error *close_err = got_repo_close(repo);
8099 const struct got_error *pack_err =
8100 got_repo_pack_fds_close(pack_fds);
8108 static const struct got_error *
8109 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8110 enum tog_view_type request, int y, int x)
8112 const struct got_error *err = NULL;
8118 if (view->type == TOG_VIEW_LOG) {
8119 struct tog_log_view_state *s = &view->state.log;
8121 err = open_diff_view_for_commit(new_view, y, x,
8122 s->selected_entry->commit, s->selected_entry->id,
8125 return got_error_msg(GOT_ERR_NOT_IMPL,
8126 "parent/child view pair not supported");
8128 case TOG_VIEW_BLAME:
8129 if (view->type == TOG_VIEW_TREE) {
8130 struct tog_tree_view_state *s = &view->state.tree;
8132 err = blame_tree_entry(new_view, y, x,
8133 s->selected_entry, &s->parents, s->commit_id,
8136 return got_error_msg(GOT_ERR_NOT_IMPL,
8137 "parent/child view pair not supported");
8140 if (view->type == TOG_VIEW_BLAME)
8141 err = log_annotated_line(new_view, y, x,
8142 view->state.blame.repo, view->state.blame.id_to_log);
8143 else if (view->type == TOG_VIEW_TREE)
8144 err = log_selected_tree_entry(new_view, y, x,
8146 else if (view->type == TOG_VIEW_REF)
8147 err = log_ref_entry(new_view, y, x,
8148 view->state.ref.selected_entry,
8149 view->state.ref.repo);
8151 return got_error_msg(GOT_ERR_NOT_IMPL,
8152 "parent/child view pair not supported");
8155 if (view->type == TOG_VIEW_LOG)
8156 err = browse_commit_tree(new_view, y, x,
8157 view->state.log.selected_entry,
8158 view->state.log.in_repo_path,
8159 view->state.log.head_ref_name,
8160 view->state.log.repo);
8161 else if (view->type == TOG_VIEW_REF)
8162 err = browse_ref_tree(new_view, y, x,
8163 view->state.ref.selected_entry,
8164 view->state.ref.repo);
8166 return got_error_msg(GOT_ERR_NOT_IMPL,
8167 "parent/child view pair not supported");
8170 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8171 if (*new_view == NULL)
8172 return got_error_from_errno("view_open");
8173 if (view->type == TOG_VIEW_LOG)
8174 err = open_ref_view(*new_view, view->state.log.repo);
8175 else if (view->type == TOG_VIEW_TREE)
8176 err = open_ref_view(*new_view, view->state.tree.repo);
8178 err = got_error_msg(GOT_ERR_NOT_IMPL,
8179 "parent/child view pair not supported");
8181 view_close(*new_view);
8184 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8191 * If view was scrolled down to move the selected line into view when opening a
8192 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8195 offset_selection_up(struct tog_view *view)
8197 switch (view->type) {
8198 case TOG_VIEW_BLAME: {
8199 struct tog_blame_view_state *s = &view->state.blame;
8200 if (s->first_displayed_line == 1) {
8201 s->selected_line = MAX(s->selected_line - view->offset,
8205 if (s->first_displayed_line > view->offset)
8206 s->first_displayed_line -= view->offset;
8208 s->first_displayed_line = 1;
8209 s->selected_line += view->offset;
8213 log_scroll_up(&view->state.log, view->offset);
8214 view->state.log.selected += view->offset;
8217 ref_scroll_up(&view->state.ref, view->offset);
8218 view->state.ref.selected += view->offset;
8221 tree_scroll_up(&view->state.tree, view->offset);
8222 view->state.tree.selected += view->offset;
8232 * If the selected line is in the section of screen covered by the bottom split,
8233 * scroll down offset lines to move it into view and index its new position.
8235 static const struct got_error *
8236 offset_selection_down(struct tog_view *view)
8238 const struct got_error *err = NULL;
8239 const struct got_error *(*scrolld)(struct tog_view *, int);
8240 int *selected = NULL;
8243 switch (view->type) {
8244 case TOG_VIEW_BLAME: {
8245 struct tog_blame_view_state *s = &view->state.blame;
8248 if (s->selected_line > view->nlines - header) {
8249 offset = abs(view->nlines - s->selected_line - header);
8250 s->first_displayed_line += offset;
8251 s->selected_line -= offset;
8252 view->offset = offset;
8256 case TOG_VIEW_LOG: {
8257 struct tog_log_view_state *s = &view->state.log;
8258 scrolld = &log_scroll_down;
8259 header = view_is_parent_view(view) ? 3 : 2;
8260 selected = &s->selected;
8263 case TOG_VIEW_REF: {
8264 struct tog_ref_view_state *s = &view->state.ref;
8265 scrolld = &ref_scroll_down;
8267 selected = &s->selected;
8270 case TOG_VIEW_TREE: {
8271 struct tog_tree_view_state *s = &view->state.tree;
8272 scrolld = &tree_scroll_down;
8274 selected = &s->selected;
8284 if (selected && *selected > view->nlines - header) {
8285 offset = abs(view->nlines - *selected - header);
8286 view->offset = offset;
8287 if (scrolld && offset) {
8288 err = scrolld(view, offset);
8289 *selected -= offset;
8297 list_commands(FILE *fp)
8301 fprintf(fp, "commands:");
8302 for (i = 0; i < nitems(tog_commands); i++) {
8303 const struct tog_cmd *cmd = &tog_commands[i];
8304 fprintf(fp, " %s", cmd->name);
8310 usage(int hflag, int status)
8312 FILE *fp = (status == 0) ? stdout : stderr;
8314 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8317 fprintf(fp, "lazy usage: %s path\n", getprogname());
8324 make_argv(int argc, ...)
8332 argv = calloc(argc, sizeof(char *));
8335 for (i = 0; i < argc; i++) {
8336 argv[i] = strdup(va_arg(ap, char *));
8337 if (argv[i] == NULL)
8346 * Try to convert 'tog path' into a 'tog log path' command.
8347 * The user could simply have mistyped the command rather than knowingly
8348 * provided a path. So check whether argv[0] can in fact be resolved
8349 * to a path in the HEAD commit and print a special error if not.
8350 * This hack is for mpi@ <3
8352 static const struct got_error *
8353 tog_log_with_path(int argc, char *argv[])
8355 const struct got_error *error = NULL, *close_err;
8356 const struct tog_cmd *cmd = NULL;
8357 struct got_repository *repo = NULL;
8358 struct got_worktree *worktree = NULL;
8359 struct got_object_id *commit_id = NULL, *id = NULL;
8360 struct got_commit_object *commit = NULL;
8361 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8362 char *commit_id_str = NULL, **cmd_argv = NULL;
8363 int *pack_fds = NULL;
8365 cwd = getcwd(NULL, 0);
8367 return got_error_from_errno("getcwd");
8369 error = got_repo_pack_fds_open(&pack_fds);
8373 error = got_worktree_open(&worktree, cwd);
8374 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8378 repo_path = strdup(got_worktree_get_repo_path(worktree));
8380 repo_path = strdup(cwd);
8381 if (repo_path == NULL) {
8382 error = got_error_from_errno("strdup");
8386 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8390 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8395 error = tog_load_refs(repo, 0);
8398 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8399 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8400 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8405 got_worktree_close(worktree);
8409 error = got_object_open_as_commit(&commit, repo, commit_id);
8413 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8415 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8417 fprintf(stderr, "%s: '%s' is no known command or path\n",
8418 getprogname(), argv[0]);
8423 error = got_object_id_str(&commit_id_str, commit_id);
8427 cmd = &tog_commands[0]; /* log */
8429 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8430 error = cmd->cmd_main(argc, cmd_argv);
8433 close_err = got_repo_close(repo);
8438 got_object_commit_close(commit);
8440 got_worktree_close(worktree);
8442 const struct got_error *pack_err =
8443 got_repo_pack_fds_close(pack_fds);
8448 free(commit_id_str);
8455 for (i = 0; i < argc; i++)
8464 main(int argc, char *argv[])
8466 const struct got_error *error = NULL;
8467 const struct tog_cmd *cmd = NULL;
8468 int ch, hflag = 0, Vflag = 0;
8469 char **cmd_argv = NULL;
8470 static const struct option longopts[] = {
8471 { "version", no_argument, NULL, 'V' },
8474 char *diff_algo_str = NULL;
8476 if (!isatty(STDIN_FILENO))
8477 errx(1, "standard input is not a tty");
8479 setlocale(LC_CTYPE, "");
8481 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8501 got_version_print_str();
8506 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8514 /* Build an argument vector which runs a default command. */
8515 cmd = &tog_commands[0];
8517 cmd_argv = make_argv(argc, cmd->name);
8521 /* Did the user specify a command? */
8522 for (i = 0; i < nitems(tog_commands); i++) {
8523 if (strncmp(tog_commands[i].name, argv[0],
8524 strlen(argv[0])) == 0) {
8525 cmd = &tog_commands[i];
8531 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8532 if (diff_algo_str) {
8533 if (strcasecmp(diff_algo_str, "patience") == 0)
8534 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8535 if (strcasecmp(diff_algo_str, "myers") == 0)
8536 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8542 /* No command specified; try log with a path */
8543 error = tog_log_with_path(argc, argv);
8548 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8555 for (i = 0; i < argc; i++)
8560 if (error && error->code != GOT_ERR_CANCELLED)
8561 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);