2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
127 struct commit_queue_head head;
131 STAILQ_ENTRY(tog_color) entry;
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
155 } else if (isbackup1 && !isbackup2) {
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 STAILQ_INSERT_HEAD(colors, tc, entry);
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
296 if (strcasecmp(val, "red") == 0)
298 if (strcasecmp(val, "green") == 0)
300 if (strcasecmp(val, "yellow") == 0)
302 if (strcasecmp(val, "blue") == 0)
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
308 if (strcasecmp(val, "white") == 0)
310 if (strcasecmp(val, "default") == 0)
313 return default_color_value(envvar);
317 struct tog_diff_view_state {
318 struct got_object_id *id1, *id2;
319 const char *label1, *label2;
322 int first_displayed_line;
323 int last_displayed_line;
326 int ignore_whitespace;
328 struct got_repository *repo;
329 struct tog_colors colors;
335 /* passed from log or blame view; may be NULL */
336 struct tog_view *parent_view;
339 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 static volatile sig_atomic_t tog_thread_error;
342 struct tog_log_thread_args {
343 pthread_cond_t need_commits;
344 pthread_cond_t commit_loaded;
347 struct got_commit_graph *graph;
348 struct commit_queue *commits;
349 const char *in_repo_path;
350 struct got_object_id *start_id;
351 struct got_repository *repo;
355 struct commit_queue_entry **first_displayed_entry;
356 struct commit_queue_entry **selected_entry;
358 int *search_next_done;
362 struct tog_log_view_state {
363 struct commit_queue commits;
364 struct commit_queue_entry *first_displayed_entry;
365 struct commit_queue_entry *last_displayed_entry;
366 struct commit_queue_entry *selected_entry;
371 struct got_repository *repo;
372 struct got_object_id *start_id;
375 struct tog_log_thread_args thread_args;
376 struct commit_queue_entry *matched_entry;
377 struct commit_queue_entry *search_entry;
378 struct tog_colors colors;
382 #define TOG_COLOR_DIFF_MINUS 1
383 #define TOG_COLOR_DIFF_PLUS 2
384 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
385 #define TOG_COLOR_DIFF_META 4
386 #define TOG_COLOR_TREE_SUBMODULE 5
387 #define TOG_COLOR_TREE_SYMLINK 6
388 #define TOG_COLOR_TREE_DIRECTORY 7
389 #define TOG_COLOR_TREE_EXECUTABLE 8
390 #define TOG_COLOR_COMMIT 9
391 #define TOG_COLOR_AUTHOR 10
392 #define TOG_COLOR_DATE 11
393 #define TOG_COLOR_REFS_HEADS 12
394 #define TOG_COLOR_REFS_TAGS 13
395 #define TOG_COLOR_REFS_REMOTES 14
396 #define TOG_COLOR_REFS_BACKUP 15
398 struct tog_blame_cb_args {
399 struct tog_blame_line *lines; /* one per line */
402 struct tog_view *view;
403 struct got_object_id *commit_id;
407 struct tog_blame_thread_args {
409 struct got_repository *repo;
410 struct tog_blame_cb_args *cb_args;
412 got_cancel_cb cancel_cb;
419 struct tog_blame_line *lines;
423 struct tog_blame_thread_args thread_args;
424 struct tog_blame_cb_args cb_args;
429 struct tog_blame_view_state {
430 int first_displayed_line;
431 int last_displayed_line;
433 int last_diffed_line;
437 struct got_object_id_queue blamed_commits;
438 struct got_object_qid *blamed_commit;
440 struct got_repository *repo;
441 struct got_object_id *commit_id;
442 struct got_object_id *id_to_log;
443 struct tog_blame blame;
445 struct tog_colors colors;
448 struct tog_parent_tree {
449 TAILQ_ENTRY(tog_parent_tree) entry;
450 struct got_tree_object *tree;
451 struct got_tree_entry *first_displayed_entry;
452 struct got_tree_entry *selected_entry;
456 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
458 struct tog_tree_view_state {
460 struct got_object_id *commit_id;/* commit which this tree belongs to */
461 struct got_tree_object *root; /* the commit's root tree entry */
462 struct got_tree_object *tree; /* currently displayed (sub-)tree */
463 struct got_tree_entry *first_displayed_entry;
464 struct got_tree_entry *last_displayed_entry;
465 struct got_tree_entry *selected_entry;
466 int ndisplayed, selected, show_ids;
467 struct tog_parent_trees parents; /* parent trees of current sub-tree */
469 struct got_repository *repo;
470 struct got_tree_entry *matched_entry;
471 struct tog_colors colors;
474 struct tog_reflist_entry {
475 TAILQ_ENTRY(tog_reflist_entry) entry;
476 struct got_reference *ref;
480 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
482 struct tog_ref_view_state {
483 struct tog_reflist_head refs;
484 struct tog_reflist_entry *first_displayed_entry;
485 struct tog_reflist_entry *last_displayed_entry;
486 struct tog_reflist_entry *selected_entry;
487 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
488 struct got_repository *repo;
489 struct tog_reflist_entry *matched_entry;
490 struct tog_colors colors;
494 * We implement two types of views: parent views and child views.
496 * The 'Tab' key switches focus between a parent view and its child view.
497 * Child views are shown side-by-side to their parent view, provided
498 * there is enough screen estate.
500 * When a new view is opened from within a parent view, this new view
501 * becomes a child view of the parent view, replacing any existing child.
503 * When a new view is opened from within a child view, this new view
504 * becomes a parent view which will obscure the views below until the
505 * user quits the new parent view by typing 'q'.
507 * This list of views contains parent views only.
508 * Child views are only pointed to by their parent view.
510 TAILQ_HEAD(tog_view_list_head, tog_view);
513 TAILQ_ENTRY(tog_view) entry;
516 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
517 int resized_y, resized_x; /* begin_y/x based on user resizing */
518 int maxx, x; /* max column and current start column */
519 int lines, cols; /* copies of LINES and COLS */
520 int nscrolled, offset; /* lines scrolled and hsplit line offset */
521 int gline, hiline; /* navigate to and highlight this nG line */
522 int ch, count; /* current keymap and count prefix */
523 int resized; /* set when in a resize event */
524 int focussed; /* Only set on one parent or child view at a time. */
526 struct tog_view *parent;
527 struct tog_view *child;
530 * This flag is initially set on parent views when a new child view
531 * is created. It gets toggled when the 'Tab' key switches focus
532 * between parent and child.
533 * The flag indicates whether focus should be passed on to our child
534 * view if this parent view gets picked for focus after another parent
535 * view was closed. This prevents child views from losing focus in such
540 enum tog_view_mode mode;
541 /* type-specific state */
542 enum tog_view_type type;
544 struct tog_diff_view_state diff;
545 struct tog_log_view_state log;
546 struct tog_blame_view_state blame;
547 struct tog_tree_view_state tree;
548 struct tog_ref_view_state ref;
551 const struct got_error *(*show)(struct tog_view *);
552 const struct got_error *(*input)(struct tog_view **,
553 struct tog_view *, int);
554 const struct got_error *(*reset)(struct tog_view *);
555 const struct got_error *(*resize)(struct tog_view *, int);
556 const struct got_error *(*close)(struct tog_view *);
558 const struct got_error *(*search_start)(struct tog_view *);
559 const struct got_error *(*search_next)(struct tog_view *);
562 #define TOG_SEARCH_FORWARD 1
563 #define TOG_SEARCH_BACKWARD 2
564 int search_next_done;
565 #define TOG_SEARCH_HAVE_MORE 1
566 #define TOG_SEARCH_NO_MORE 2
567 #define TOG_SEARCH_HAVE_NONE 3
572 static const struct got_error *open_diff_view(struct tog_view *,
573 struct got_object_id *, struct got_object_id *,
574 const char *, const char *, int, int, int, struct tog_view *,
575 struct got_repository *);
576 static const struct got_error *show_diff_view(struct tog_view *);
577 static const struct got_error *input_diff_view(struct tog_view **,
578 struct tog_view *, int);
579 static const struct got_error *reset_diff_view(struct tog_view *);
580 static const struct got_error* close_diff_view(struct tog_view *);
581 static const struct got_error *search_start_diff_view(struct tog_view *);
582 static const struct got_error *search_next_diff_view(struct tog_view *);
584 static const struct got_error *open_log_view(struct tog_view *,
585 struct got_object_id *, struct got_repository *,
586 const char *, const char *, int);
587 static const struct got_error * show_log_view(struct tog_view *);
588 static const struct got_error *input_log_view(struct tog_view **,
589 struct tog_view *, int);
590 static const struct got_error *resize_log_view(struct tog_view *, int);
591 static const struct got_error *close_log_view(struct tog_view *);
592 static const struct got_error *search_start_log_view(struct tog_view *);
593 static const struct got_error *search_next_log_view(struct tog_view *);
595 static const struct got_error *open_blame_view(struct tog_view *, char *,
596 struct got_object_id *, struct got_repository *);
597 static const struct got_error *show_blame_view(struct tog_view *);
598 static const struct got_error *input_blame_view(struct tog_view **,
599 struct tog_view *, int);
600 static const struct got_error *reset_blame_view(struct tog_view *);
601 static const struct got_error *close_blame_view(struct tog_view *);
602 static const struct got_error *search_start_blame_view(struct tog_view *);
603 static const struct got_error *search_next_blame_view(struct tog_view *);
605 static const struct got_error *open_tree_view(struct tog_view *,
606 struct got_object_id *, const char *, struct got_repository *);
607 static const struct got_error *show_tree_view(struct tog_view *);
608 static const struct got_error *input_tree_view(struct tog_view **,
609 struct tog_view *, int);
610 static const struct got_error *close_tree_view(struct tog_view *);
611 static const struct got_error *search_start_tree_view(struct tog_view *);
612 static const struct got_error *search_next_tree_view(struct tog_view *);
614 static const struct got_error *open_ref_view(struct tog_view *,
615 struct got_repository *);
616 static const struct got_error *show_ref_view(struct tog_view *);
617 static const struct got_error *input_ref_view(struct tog_view **,
618 struct tog_view *, int);
619 static const struct got_error *close_ref_view(struct tog_view *);
620 static const struct got_error *search_start_ref_view(struct tog_view *);
621 static const struct got_error *search_next_ref_view(struct tog_view *);
623 static volatile sig_atomic_t tog_sigwinch_received;
624 static volatile sig_atomic_t tog_sigpipe_received;
625 static volatile sig_atomic_t tog_sigcont_received;
626 static volatile sig_atomic_t tog_sigint_received;
627 static volatile sig_atomic_t tog_sigterm_received;
630 tog_sigwinch(int signo)
632 tog_sigwinch_received = 1;
636 tog_sigpipe(int signo)
638 tog_sigpipe_received = 1;
642 tog_sigcont(int signo)
644 tog_sigcont_received = 1;
648 tog_sigint(int signo)
650 tog_sigint_received = 1;
654 tog_sigterm(int signo)
656 tog_sigterm_received = 1;
660 tog_fatal_signal_received(void)
662 return (tog_sigpipe_received ||
663 tog_sigint_received || tog_sigint_received);
666 static const struct got_error *
667 view_close(struct tog_view *view)
669 const struct got_error *err = NULL, *child_err = NULL;
672 child_err = view_close(view->child);
676 err = view->close(view);
678 del_panel(view->panel);
680 delwin(view->window);
682 return err ? err : child_err;
685 static struct tog_view *
686 view_open(int nlines, int ncols, int begin_y, int begin_x,
687 enum tog_view_type type)
689 struct tog_view *view = calloc(1, sizeof(*view));
697 view->nlines = nlines ? nlines : LINES - begin_y;
698 view->ncols = ncols ? ncols : COLS - begin_x;
699 view->begin_y = begin_y;
700 view->begin_x = begin_x;
701 view->window = newwin(nlines, ncols, begin_y, begin_x);
702 if (view->window == NULL) {
706 view->panel = new_panel(view->window);
707 if (view->panel == NULL ||
708 set_panel_userptr(view->panel, view) != OK) {
713 keypad(view->window, TRUE);
718 view_split_begin_x(int begin_x)
720 if (begin_x > 0 || COLS < 120)
722 return (COLS - MAX(COLS / 2, 80));
725 /* XXX Stub till we decide what to do. */
727 view_split_begin_y(int lines)
729 return lines * HSPLIT_SCALE;
732 static const struct got_error *view_resize(struct tog_view *);
734 static const struct got_error *
735 view_splitscreen(struct tog_view *view)
737 const struct got_error *err = NULL;
739 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
740 if (view->resized_y && view->resized_y < view->lines)
741 view->begin_y = view->resized_y;
743 view->begin_y = view_split_begin_y(view->nlines);
745 } else if (!view->resized) {
746 if (view->resized_x && view->resized_x < view->cols - 1 &&
748 view->begin_x = view->resized_x;
750 view->begin_x = view_split_begin_x(0);
753 view->nlines = LINES - view->begin_y;
754 view->ncols = COLS - view->begin_x;
757 err = view_resize(view);
761 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
762 view->parent->nlines = view->begin_y;
764 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
765 return got_error_from_errno("mvwin");
770 static const struct got_error *
771 view_fullscreen(struct tog_view *view)
773 const struct got_error *err = NULL;
776 view->begin_y = view->resized ? view->begin_y : 0;
777 view->nlines = view->resized ? view->nlines : LINES;
781 err = view_resize(view);
785 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
786 return got_error_from_errno("mvwin");
792 view_is_parent_view(struct tog_view *view)
794 return view->parent == NULL;
798 view_is_splitscreen(struct tog_view *view)
800 return view->begin_x > 0 || view->begin_y > 0;
804 view_is_fullscreen(struct tog_view *view)
806 return view->nlines == LINES && view->ncols == COLS;
810 view_is_hsplit_top(struct tog_view *view)
812 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
813 view_is_splitscreen(view->child);
817 view_border(struct tog_view *view)
820 const struct tog_view *view_above;
823 return view_border(view->parent);
825 panel = panel_above(view->panel);
829 view_above = panel_userptr(panel);
830 if (view->mode == TOG_VIEW_SPLIT_HRZN)
831 mvwhline(view->window, view_above->begin_y - 1,
832 view->begin_x, got_locale_is_utf8() ?
833 ACS_HLINE : '-', view->ncols);
835 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
836 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
839 static const struct got_error *view_init_hsplit(struct tog_view *, int);
840 static const struct got_error *request_log_commits(struct tog_view *);
841 static const struct got_error *offset_selection_down(struct tog_view *);
842 static void offset_selection_up(struct tog_view *);
843 static void view_get_split(struct tog_view *, int *, int *);
845 static const struct got_error *
846 view_resize(struct tog_view *view)
848 const struct got_error *err = NULL;
849 int dif, nlines, ncols;
851 dif = LINES - view->lines; /* line difference */
853 if (view->lines > LINES)
854 nlines = view->nlines - (view->lines - LINES);
856 nlines = view->nlines + (LINES - view->lines);
857 if (view->cols > COLS)
858 ncols = view->ncols - (view->cols - COLS);
860 ncols = view->ncols + (COLS - view->cols);
863 int hs = view->child->begin_y;
865 if (!view_is_fullscreen(view))
866 view->child->begin_x = view_split_begin_x(view->begin_x);
867 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
868 view->child->begin_x == 0) {
871 view_fullscreen(view->child);
872 if (view->child->focussed)
873 show_panel(view->child->panel);
875 show_panel(view->panel);
877 ncols = view->child->begin_x;
879 view_splitscreen(view->child);
880 show_panel(view->child->panel);
883 * XXX This is ugly and needs to be moved into the above
884 * logic but "works" for now and my attempts at moving it
885 * break either 'tab' or 'F' key maps in horizontal splits.
888 err = view_splitscreen(view->child);
891 if (dif < 0) { /* top split decreased */
892 err = offset_selection_down(view);
899 show_panel(view->child->panel);
900 nlines = view->nlines;
902 } else if (view->parent == NULL)
905 if (view->resize && dif > 0) {
906 err = view->resize(view, dif);
911 if (wresize(view->window, nlines, ncols) == ERR)
912 return got_error_from_errno("wresize");
913 if (replace_panel(view->panel, view->window) == ERR)
914 return got_error_from_errno("replace_panel");
915 wclear(view->window);
917 view->nlines = nlines;
925 static const struct got_error *
926 resize_log_view(struct tog_view *view, int increase)
928 struct tog_log_view_state *s = &view->state.log;
929 const struct got_error *err = NULL;
932 if (s->selected_entry)
933 n = s->selected_entry->idx + view->lines - s->selected;
936 * Request commits to account for the increased
937 * height so we have enough to populate the view.
939 if (s->commits.ncommits < n) {
940 view->nscrolled = n - s->commits.ncommits + increase + 1;
941 err = request_log_commits(view);
948 view_adjust_offset(struct tog_view *view, int n)
953 if (view->parent && view->parent->offset) {
954 if (view->parent->offset + n >= 0)
955 view->parent->offset += n;
957 view->parent->offset = 0;
958 } else if (view->offset) {
959 if (view->offset - n >= 0)
966 static const struct got_error *
967 view_resize_split(struct tog_view *view, int resize)
969 const struct got_error *err = NULL;
970 struct tog_view *v = NULL;
977 if (!v->child || !view_is_splitscreen(v->child))
980 v->resized = v->child->resized = resize; /* lock for resize event */
982 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
983 if (v->child->resized_y)
984 v->child->begin_y = v->child->resized_y;
986 v->child->begin_y -= resize;
988 v->child->begin_y += resize;
989 if (v->child->begin_y < 3) {
991 v->child->begin_y = 3;
992 } else if (v->child->begin_y > LINES - 1) {
994 v->child->begin_y = LINES - 1;
997 v->child->ncols = COLS;
998 view_adjust_offset(view, resize);
999 err = view_init_hsplit(v, v->child->begin_y);
1002 v->child->resized_y = v->child->begin_y;
1004 if (v->child->resized_x)
1005 v->child->begin_x = v->child->resized_x;
1007 v->child->begin_x -= resize;
1009 v->child->begin_x += resize;
1010 if (v->child->begin_x < 11) {
1012 v->child->begin_x = 11;
1013 } else if (v->child->begin_x > COLS - 1) {
1015 v->child->begin_x = COLS - 1;
1017 v->child->resized_x = v->child->begin_x;
1020 v->child->mode = v->mode;
1021 v->child->nlines = v->lines - v->child->begin_y;
1022 v->child->ncols = v->cols - v->child->begin_x;
1025 err = view_fullscreen(v);
1028 err = view_splitscreen(v->child);
1032 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1033 err = offset_selection_down(v->child);
1039 err = v->resize(v, 0);
1040 else if (v->child->resize)
1041 err = v->child->resize(v->child, 0);
1043 v->resized = v->child->resized = 0;
1049 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1051 struct tog_view *v = src->child ? src->child : src;
1053 dst->resized_x = v->resized_x;
1054 dst->resized_y = v->resized_y;
1057 static const struct got_error *
1058 view_close_child(struct tog_view *view)
1060 const struct got_error *err = NULL;
1062 if (view->child == NULL)
1065 err = view_close(view->child);
1070 static const struct got_error *
1071 view_set_child(struct tog_view *view, struct tog_view *child)
1073 const struct got_error *err = NULL;
1075 view->child = child;
1076 child->parent = view;
1078 err = view_resize(view);
1082 if (view->child->resized_x || view->child->resized_y)
1083 err = view_resize_split(view, 0);
1088 static const struct got_error *view_dispatch_request(struct tog_view **,
1089 struct tog_view *, enum tog_view_type, int, int);
1091 static const struct got_error *
1092 view_request_new(struct tog_view **requested, struct tog_view *view,
1093 enum tog_view_type request)
1095 struct tog_view *new_view = NULL;
1096 const struct got_error *err;
1101 if (view_is_parent_view(view))
1102 view_get_split(view, &y, &x);
1104 err = view_dispatch_request(&new_view, view, request, y, x);
1108 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1109 err = view_init_hsplit(view, y);
1115 new_view->focussed = 1;
1116 new_view->mode = view->mode;
1117 new_view->nlines = view->lines - y;
1119 if (view_is_parent_view(view)) {
1120 view_transfer_size(new_view, view);
1121 err = view_close_child(view);
1124 err = view_set_child(view, new_view);
1127 view->focus_child = 1;
1129 *requested = new_view;
1135 tog_resizeterm(void)
1138 struct winsize size;
1140 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1141 cols = 80; /* Default */
1145 lines = size.ws_row;
1147 resize_term(lines, cols);
1150 static const struct got_error *
1151 view_search_start(struct tog_view *view)
1153 const struct got_error *err = NULL;
1154 struct tog_view *v = view;
1158 if (view->search_started) {
1159 regfree(&view->regex);
1160 view->searching = 0;
1161 memset(&view->regmatch, 0, sizeof(view->regmatch));
1163 view->search_started = 0;
1165 if (view->nlines < 1)
1168 if (view_is_hsplit_top(view))
1171 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1172 wclrtoeol(v->window);
1174 nodelay(view->window, FALSE); /* block for search term input */
1177 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1178 wrefresh(v->window);
1181 nodelay(view->window, TRUE);
1185 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1186 err = view->search_start(view);
1188 regfree(&view->regex);
1191 view->search_started = 1;
1192 view->searching = TOG_SEARCH_FORWARD;
1193 view->search_next_done = 0;
1194 view->search_next(view);
1200 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1201 static const struct got_error *
1202 switch_split(struct tog_view *view)
1204 const struct got_error *err = NULL;
1205 struct tog_view *v = NULL;
1212 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1213 v->mode = TOG_VIEW_SPLIT_VERT;
1215 v->mode = TOG_VIEW_SPLIT_HRZN;
1219 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1220 v->mode = TOG_VIEW_SPLIT_NONE;
1222 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1223 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1224 v->child->begin_y = v->child->resized_y;
1225 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1226 v->child->begin_x = v->child->resized_x;
1229 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1231 v->child->ncols = COLS;
1232 v->child->nscrolled = LINES - v->child->nlines;
1234 err = view_init_hsplit(v, v->child->begin_y);
1238 v->child->mode = v->mode;
1239 v->child->nlines = v->lines - v->child->begin_y;
1242 err = view_fullscreen(v);
1245 err = view_splitscreen(v->child);
1249 if (v->mode == TOG_VIEW_SPLIT_NONE)
1250 v->mode = TOG_VIEW_SPLIT_VERT;
1251 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1252 err = offset_selection_down(v);
1255 err = offset_selection_down(v->child);
1259 offset_selection_up(v);
1260 offset_selection_up(v->child);
1263 err = v->resize(v, 0);
1264 else if (v->child->resize)
1265 err = v->child->resize(v->child, 0);
1271 * Compute view->count from numeric input. Assign total to view->count and
1272 * return first non-numeric key entered.
1275 get_compound_key(struct tog_view *view, int c)
1277 struct tog_view *v = view;
1280 if (view_is_hsplit_top(view))
1282 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1286 cbreak(); /* block for input */
1287 nodelay(view->window, FALSE);
1288 wmove(v->window, v->nlines - 1, 0);
1289 wclrtoeol(v->window);
1290 waddch(v->window, ':');
1293 x = getcurx(v->window);
1294 if (x != ERR && x < view->ncols) {
1295 waddch(v->window, c);
1296 wrefresh(v->window);
1300 * Don't overflow. Max valid request should be the greatest
1301 * between the longest and total lines; cap at 10 million.
1306 n = n * 10 + (c - '0');
1307 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1309 if (c == 'G' || c == 'g') { /* nG key map */
1310 view->gline = view->hiline = n;
1315 /* Massage excessive or inapplicable values at the input handler. */
1321 static const struct got_error *
1322 view_input(struct tog_view **new, int *done, struct tog_view *view,
1323 struct tog_view_list_head *views)
1325 const struct got_error *err = NULL;
1331 /* Clear "no matches" indicator. */
1332 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1333 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1334 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1338 if (view->searching && !view->search_next_done) {
1339 errcode = pthread_mutex_unlock(&tog_mutex);
1341 return got_error_set_errno(errcode,
1342 "pthread_mutex_unlock");
1344 errcode = pthread_mutex_lock(&tog_mutex);
1346 return got_error_set_errno(errcode,
1347 "pthread_mutex_lock");
1348 view->search_next(view);
1352 /* Allow threads to make progress while we are waiting for input. */
1353 errcode = pthread_mutex_unlock(&tog_mutex);
1355 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1356 /* If we have an unfinished count, let C-g or backspace abort. */
1357 if (view->count && --view->count) {
1359 nodelay(view->window, TRUE);
1360 ch = wgetch(view->window);
1361 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1366 ch = wgetch(view->window);
1367 if (ch >= '1' && ch <= '9')
1368 view->ch = ch = get_compound_key(view, ch);
1370 if (view->hiline && ch != ERR && ch != 0)
1371 view->hiline = 0; /* key pressed, clear line highlight */
1372 nodelay(view->window, TRUE);
1373 errcode = pthread_mutex_lock(&tog_mutex);
1375 return got_error_set_errno(errcode, "pthread_mutex_lock");
1377 if (tog_sigwinch_received || tog_sigcont_received) {
1379 tog_sigwinch_received = 0;
1380 tog_sigcont_received = 0;
1381 TAILQ_FOREACH(v, views, entry) {
1382 err = view_resize(v);
1385 err = v->input(new, v, KEY_RESIZE);
1389 err = view_resize(v->child);
1392 err = v->child->input(new, v->child,
1396 if (v->child->resized_x || v->child->resized_y) {
1397 err = view_resize_split(v, 0);
1410 view->child->focussed = 1;
1411 view->focus_child = 1;
1412 } else if (view->parent) {
1414 view->parent->focussed = 1;
1415 view->parent->focus_child = 0;
1416 if (!view_is_splitscreen(view)) {
1417 if (view->parent->resize) {
1418 err = view->parent->resize(view->parent,
1423 offset_selection_up(view->parent);
1424 err = view_fullscreen(view->parent);
1431 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1432 if (view->parent->resize) {
1433 /* might need more commits to fill fullscreen */
1434 err = view->parent->resize(view->parent, 0);
1438 offset_selection_up(view->parent);
1440 err = view->input(new, view, ch);
1448 if (view_is_parent_view(view)) {
1449 if (view->child == NULL)
1451 if (view_is_splitscreen(view->child)) {
1453 view->child->focussed = 1;
1454 err = view_fullscreen(view->child);
1456 err = view_splitscreen(view->child);
1458 err = view_resize_split(view, 0);
1462 err = view->child->input(new, view->child,
1465 if (view_is_splitscreen(view)) {
1466 view->parent->focussed = 0;
1468 err = view_fullscreen(view);
1470 err = view_splitscreen(view);
1471 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1472 err = view_resize(view->parent);
1474 err = view_resize_split(view, 0);
1478 err = view->input(new, view, KEY_RESIZE);
1483 err = view->resize(view, 0);
1488 err = offset_selection_down(view->parent);
1490 err = offset_selection_down(view);
1494 err = switch_split(view);
1497 err = view_resize_split(view, -1);
1500 err = view_resize_split(view, 1);
1506 if (view->search_start)
1507 view_search_start(view);
1509 err = view->input(new, view, ch);
1513 if (view->search_started && view->search_next) {
1514 view->searching = (ch == 'n' ?
1515 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1516 view->search_next_done = 0;
1517 view->search_next(view);
1519 err = view->input(new, view, ch);
1522 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1523 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1525 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1526 TAILQ_FOREACH(v, views, entry) {
1532 if (v->child && v->child->reset) {
1533 err = v->child->reset(v->child);
1540 err = view->input(new, view, ch);
1548 view_needs_focus_indication(struct tog_view *view)
1550 if (view_is_parent_view(view)) {
1551 if (view->child == NULL || view->child->focussed)
1553 if (!view_is_splitscreen(view->child))
1555 } else if (!view_is_splitscreen(view))
1558 return view->focussed;
1561 static const struct got_error *
1562 view_loop(struct tog_view *view)
1564 const struct got_error *err = NULL;
1565 struct tog_view_list_head views;
1566 struct tog_view *new_view;
1568 int fast_refresh = 10;
1569 int done = 0, errcode;
1571 mode = getenv("TOG_VIEW_SPLIT_MODE");
1572 if (!mode || !(*mode == 'h' || *mode == 'H'))
1573 view->mode = TOG_VIEW_SPLIT_VERT;
1575 view->mode = TOG_VIEW_SPLIT_HRZN;
1577 errcode = pthread_mutex_lock(&tog_mutex);
1579 return got_error_set_errno(errcode, "pthread_mutex_lock");
1582 TAILQ_INSERT_HEAD(&views, view, entry);
1585 err = view->show(view);
1590 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1591 !tog_fatal_signal_received()) {
1592 /* Refresh fast during initialization, then become slower. */
1593 if (fast_refresh && fast_refresh-- == 0)
1594 halfdelay(10); /* switch to once per second */
1596 err = view_input(&new_view, &done, view, &views);
1600 struct tog_view *v, *prev = NULL;
1602 if (view_is_parent_view(view))
1603 prev = TAILQ_PREV(view, tog_view_list_head,
1605 else if (view->parent)
1606 prev = view->parent;
1609 view->parent->child = NULL;
1610 view->parent->focus_child = 0;
1611 /* Restore fullscreen line height. */
1612 view->parent->nlines = view->parent->lines;
1613 err = view_resize(view->parent);
1616 /* Make resized splits persist. */
1617 view_transfer_size(view->parent, view);
1619 TAILQ_REMOVE(&views, view, entry);
1621 err = view_close(view);
1626 TAILQ_FOREACH(v, &views, entry) {
1630 if (view == NULL && new_view == NULL) {
1631 /* No view has focus. Try to pick one. */
1634 else if (!TAILQ_EMPTY(&views)) {
1635 view = TAILQ_LAST(&views,
1636 tog_view_list_head);
1639 if (view->focus_child) {
1640 view->child->focussed = 1;
1648 struct tog_view *v, *t;
1649 /* Only allow one parent view per type. */
1650 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1651 if (v->type != new_view->type)
1653 TAILQ_REMOVE(&views, v, entry);
1654 err = view_close(v);
1659 TAILQ_INSERT_TAIL(&views, new_view, entry);
1663 if (view_is_parent_view(view)) {
1664 if (view->child && view->child->focussed)
1667 if (view->parent && view->parent->focussed)
1668 view = view->parent;
1670 show_panel(view->panel);
1671 if (view->child && view_is_splitscreen(view->child))
1672 show_panel(view->child->panel);
1673 if (view->parent && view_is_splitscreen(view)) {
1674 err = view->parent->show(view->parent);
1678 err = view->show(view);
1682 err = view->child->show(view->child);
1691 while (!TAILQ_EMPTY(&views)) {
1692 const struct got_error *close_err;
1693 view = TAILQ_FIRST(&views);
1694 TAILQ_REMOVE(&views, view, entry);
1695 close_err = view_close(view);
1696 if (close_err && err == NULL)
1700 errcode = pthread_mutex_unlock(&tog_mutex);
1701 if (errcode && err == NULL)
1702 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1712 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1717 /* Create newly allocated wide-character string equivalent to a byte string. */
1718 static const struct got_error *
1719 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1722 const struct got_error *err = NULL;
1725 *wlen = mbstowcs(NULL, s, 0);
1726 if (*wlen == (size_t)-1) {
1728 if (errno != EILSEQ)
1729 return got_error_from_errno("mbstowcs");
1731 /* byte string invalid in current encoding; try to "fix" it */
1732 err = got_mbsavis(&vis, &vislen, s);
1735 *wlen = mbstowcs(NULL, vis, 0);
1736 if (*wlen == (size_t)-1) {
1737 err = got_error_from_errno("mbstowcs"); /* give up */
1742 *ws = calloc(*wlen + 1, sizeof(**ws));
1744 err = got_error_from_errno("calloc");
1748 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1749 err = got_error_from_errno("mbstowcs");
1760 static const struct got_error *
1761 expand_tab(char **ptr, const char *src)
1764 size_t len, n, idx = 0, sz = 0;
1767 n = len = strlen(src);
1768 dst = malloc(n + 1);
1770 return got_error_from_errno("malloc");
1772 while (idx < len && src[idx]) {
1773 const char c = src[idx];
1776 size_t nb = TABSIZE - sz % TABSIZE;
1779 p = realloc(dst, n + nb);
1782 return got_error_from_errno("realloc");
1787 memset(dst + sz, ' ', nb);
1790 dst[sz++] = src[idx];
1800 * Advance at most n columns from wline starting at offset off.
1801 * Return the index to the first character after the span operation.
1802 * Return the combined column width of all spanned wide character in
1806 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1808 int width, i, cols = 0;
1815 for (i = off; wline[i] != L'\0'; ++i) {
1816 if (wline[i] == L'\t')
1817 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1819 width = wcwidth(wline[i]);
1826 if (cols + width > n)
1836 * Format a line for display, ensuring that it won't overflow a width limit.
1837 * With scrolling, the width returned refers to the scrolled version of the
1838 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1840 static const struct got_error *
1841 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1842 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1844 const struct got_error *err = NULL;
1846 wchar_t *wline = NULL;
1855 err = expand_tab(&exstr, line);
1860 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1865 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1867 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1868 wline[wlen - 1] = L'\0';
1871 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1872 wline[wlen - 1] = L'\0';
1876 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1882 *scrollxp = scrollx;
1890 static const struct got_error*
1891 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1892 struct got_object_id *id, struct got_repository *repo)
1894 static const struct got_error *err = NULL;
1895 struct got_reflist_entry *re;
1901 TAILQ_FOREACH(re, refs, entry) {
1902 struct got_tag_object *tag = NULL;
1903 struct got_object_id *ref_id;
1906 name = got_ref_get_name(re->ref);
1907 if (strcmp(name, GOT_REF_HEAD) == 0)
1909 if (strncmp(name, "refs/", 5) == 0)
1911 if (strncmp(name, "got/", 4) == 0 &&
1912 strncmp(name, "got/backup/", 11) != 0)
1914 if (strncmp(name, "heads/", 6) == 0)
1916 if (strncmp(name, "remotes/", 8) == 0) {
1918 s = strstr(name, "/" GOT_REF_HEAD);
1919 if (s != NULL && s[strlen(s)] == '\0')
1922 err = got_ref_resolve(&ref_id, repo, re->ref);
1925 if (strncmp(name, "tags/", 5) == 0) {
1926 err = got_object_open_as_tag(&tag, repo, ref_id);
1928 if (err->code != GOT_ERR_OBJ_TYPE) {
1932 /* Ref points at something other than a tag. */
1937 cmp = got_object_id_cmp(tag ?
1938 got_object_tag_get_object_id(tag) : ref_id, id);
1941 got_object_tag_close(tag);
1945 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1946 s ? ", " : "", name) == -1) {
1947 err = got_error_from_errno("asprintf");
1958 static const struct got_error *
1959 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1964 smallerthan = strchr(author, '<');
1965 if (smallerthan && smallerthan[1] != '\0')
1966 author = smallerthan + 1;
1967 author[strcspn(author, "@>")] = '\0';
1968 return format_line(wauthor, author_width, NULL, author, 0, limit,
1972 static const struct got_error *
1973 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1974 struct got_object_id *id, const size_t date_display_cols,
1975 int author_display_cols)
1977 struct tog_log_view_state *s = &view->state.log;
1978 const struct got_error *err = NULL;
1979 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1980 char *logmsg0 = NULL, *logmsg = NULL;
1981 char *author = NULL;
1982 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1983 int author_width, logmsg_width;
1984 char *newline, *line = NULL;
1985 int col, limit, scrollx;
1986 const int avail = view->ncols;
1988 time_t committer_time;
1989 struct tog_color *tc;
1991 committer_time = got_object_commit_get_committer_time(commit);
1992 if (gmtime_r(&committer_time, &tm) == NULL)
1993 return got_error_from_errno("gmtime_r");
1994 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1995 return got_error(GOT_ERR_NO_SPACE);
1997 if (avail <= date_display_cols)
1998 limit = MIN(sizeof(datebuf) - 1, avail);
2000 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2001 tc = get_color(&s->colors, TOG_COLOR_DATE);
2003 wattr_on(view->window,
2004 COLOR_PAIR(tc->colorpair), NULL);
2005 waddnstr(view->window, datebuf, limit);
2007 wattr_off(view->window,
2008 COLOR_PAIR(tc->colorpair), NULL);
2015 err = got_object_id_str(&id_str, id);
2018 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2020 wattr_on(view->window,
2021 COLOR_PAIR(tc->colorpair), NULL);
2022 wprintw(view->window, "%.8s ", id_str);
2024 wattr_off(view->window,
2025 COLOR_PAIR(tc->colorpair), NULL);
2032 if (s->use_committer)
2033 author = strdup(got_object_commit_get_committer(commit));
2035 author = strdup(got_object_commit_get_author(commit));
2036 if (author == NULL) {
2037 err = got_error_from_errno("strdup");
2040 err = format_author(&wauthor, &author_width, author, avail - col, col);
2043 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2045 wattr_on(view->window,
2046 COLOR_PAIR(tc->colorpair), NULL);
2047 waddwstr(view->window, wauthor);
2049 wattr_off(view->window,
2050 COLOR_PAIR(tc->colorpair), NULL);
2051 col += author_width;
2052 while (col < avail && author_width < author_display_cols + 2) {
2053 waddch(view->window, ' ');
2060 err = got_object_commit_get_logmsg(&logmsg0, commit);
2064 while (*logmsg == '\n')
2066 newline = strchr(logmsg, '\n');
2069 limit = avail - col;
2070 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2071 limit--; /* for the border */
2072 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2076 waddwstr(view->window, &wlogmsg[scrollx]);
2077 col += MAX(logmsg_width, 0);
2078 while (col < avail) {
2079 waddch(view->window, ' ');
2091 static struct commit_queue_entry *
2092 alloc_commit_queue_entry(struct got_commit_object *commit,
2093 struct got_object_id *id)
2095 struct commit_queue_entry *entry;
2097 entry = calloc(1, sizeof(*entry));
2102 entry->commit = commit;
2107 pop_commit(struct commit_queue *commits)
2109 struct commit_queue_entry *entry;
2111 entry = TAILQ_FIRST(&commits->head);
2112 TAILQ_REMOVE(&commits->head, entry, entry);
2113 got_object_commit_close(entry->commit);
2114 commits->ncommits--;
2115 /* Don't free entry->id! It is owned by the commit graph. */
2120 free_commits(struct commit_queue *commits)
2122 while (!TAILQ_EMPTY(&commits->head))
2123 pop_commit(commits);
2126 static const struct got_error *
2127 match_commit(int *have_match, struct got_object_id *id,
2128 struct got_commit_object *commit, regex_t *regex)
2130 const struct got_error *err = NULL;
2131 regmatch_t regmatch;
2132 char *id_str = NULL, *logmsg = NULL;
2136 err = got_object_id_str(&id_str, id);
2140 err = got_object_commit_get_logmsg(&logmsg, commit);
2144 if (regexec(regex, got_object_commit_get_author(commit), 1,
2145 ®match, 0) == 0 ||
2146 regexec(regex, got_object_commit_get_committer(commit), 1,
2147 ®match, 0) == 0 ||
2148 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2149 regexec(regex, logmsg, 1, ®match, 0) == 0)
2157 static const struct got_error *
2158 queue_commits(struct tog_log_thread_args *a)
2160 const struct got_error *err = NULL;
2163 * We keep all commits open throughout the lifetime of the log
2164 * view in order to avoid having to re-fetch commits from disk
2165 * while updating the display.
2168 struct got_object_id *id;
2169 struct got_commit_object *commit;
2170 struct commit_queue_entry *entry;
2173 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2175 if (err || id == NULL)
2178 err = got_object_open_as_commit(&commit, a->repo, id);
2181 entry = alloc_commit_queue_entry(commit, id);
2182 if (entry == NULL) {
2183 err = got_error_from_errno("alloc_commit_queue_entry");
2187 errcode = pthread_mutex_lock(&tog_mutex);
2189 err = got_error_set_errno(errcode,
2190 "pthread_mutex_lock");
2194 entry->idx = a->commits->ncommits;
2195 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2196 a->commits->ncommits++;
2198 if (*a->searching == TOG_SEARCH_FORWARD &&
2199 !*a->search_next_done) {
2201 err = match_commit(&have_match, id, commit, a->regex);
2205 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2208 errcode = pthread_mutex_unlock(&tog_mutex);
2209 if (errcode && err == NULL)
2210 err = got_error_set_errno(errcode,
2211 "pthread_mutex_unlock");
2214 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2220 select_commit(struct tog_log_view_state *s)
2222 struct commit_queue_entry *entry;
2225 entry = s->first_displayed_entry;
2227 if (ncommits == s->selected) {
2228 s->selected_entry = entry;
2231 entry = TAILQ_NEXT(entry, entry);
2236 static const struct got_error *
2237 draw_commits(struct tog_view *view)
2239 const struct got_error *err = NULL;
2240 struct tog_log_view_state *s = &view->state.log;
2241 struct commit_queue_entry *entry = s->selected_entry;
2242 const int limit = view->nlines;
2244 int ncommits, author_cols = 4;
2245 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2246 char *refs_str = NULL;
2248 struct tog_color *tc;
2249 static const size_t date_display_cols = 12;
2251 if (s->selected_entry &&
2252 !(view->searching && view->search_next_done == 0)) {
2253 struct got_reflist_head *refs;
2254 err = got_object_id_str(&id_str, s->selected_entry->id);
2257 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2258 s->selected_entry->id);
2260 err = build_refs_str(&refs_str, refs,
2261 s->selected_entry->id, s->repo);
2267 if (s->thread_args.commits_needed == 0)
2268 halfdelay(10); /* disable fast refresh */
2270 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2271 if (asprintf(&ncommits_str, " [%d/%d] %s",
2272 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2273 (view->searching && !view->search_next_done) ?
2274 "searching..." : "loading...") == -1) {
2275 err = got_error_from_errno("asprintf");
2279 const char *search_str = NULL;
2281 if (view->searching) {
2282 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2283 search_str = "no more matches";
2284 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2285 search_str = "no matches found";
2286 else if (!view->search_next_done)
2287 search_str = "searching...";
2290 if (asprintf(&ncommits_str, " [%d/%d] %s",
2291 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2292 search_str ? search_str :
2293 (refs_str ? refs_str : "")) == -1) {
2294 err = got_error_from_errno("asprintf");
2299 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2300 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2301 "........................................",
2302 s->in_repo_path, ncommits_str) == -1) {
2303 err = got_error_from_errno("asprintf");
2307 } else if (asprintf(&header, "commit %s%s",
2308 id_str ? id_str : "........................................",
2309 ncommits_str) == -1) {
2310 err = got_error_from_errno("asprintf");
2314 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2318 werase(view->window);
2320 if (view_needs_focus_indication(view))
2321 wstandout(view->window);
2322 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2324 wattr_on(view->window,
2325 COLOR_PAIR(tc->colorpair), NULL);
2326 waddwstr(view->window, wline);
2328 wattr_off(view->window,
2329 COLOR_PAIR(tc->colorpair), NULL);
2330 while (width < view->ncols) {
2331 waddch(view->window, ' ');
2334 if (view_needs_focus_indication(view))
2335 wstandend(view->window);
2340 /* Grow author column size if necessary, and set view->maxx. */
2341 entry = s->first_displayed_entry;
2345 struct got_commit_object *c = entry->commit;
2346 char *author, *eol, *msg, *msg0;
2347 wchar_t *wauthor, *wmsg;
2349 if (ncommits >= limit - 1)
2351 if (s->use_committer)
2352 author = strdup(got_object_commit_get_committer(c));
2354 author = strdup(got_object_commit_get_author(c));
2355 if (author == NULL) {
2356 err = got_error_from_errno("strdup");
2359 err = format_author(&wauthor, &width, author, COLS,
2361 if (author_cols < width)
2362 author_cols = width;
2367 err = got_object_commit_get_logmsg(&msg0, c);
2371 while (*msg == '\n')
2373 if ((eol = strchr(msg, '\n')))
2375 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2376 date_display_cols + author_cols, 0);
2379 view->maxx = MAX(view->maxx, width);
2383 entry = TAILQ_NEXT(entry, entry);
2386 entry = s->first_displayed_entry;
2387 s->last_displayed_entry = s->first_displayed_entry;
2390 if (ncommits >= limit - 1)
2392 if (ncommits == s->selected)
2393 wstandout(view->window);
2394 err = draw_commit(view, entry->commit, entry->id,
2395 date_display_cols, author_cols);
2396 if (ncommits == s->selected)
2397 wstandend(view->window);
2401 s->last_displayed_entry = entry;
2402 entry = TAILQ_NEXT(entry, entry);
2415 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2417 struct commit_queue_entry *entry;
2420 entry = TAILQ_FIRST(&s->commits.head);
2421 if (s->first_displayed_entry == entry)
2424 entry = s->first_displayed_entry;
2425 while (entry && nscrolled < maxscroll) {
2426 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2428 s->first_displayed_entry = entry;
2434 static const struct got_error *
2435 trigger_log_thread(struct tog_view *view, int wait)
2437 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2440 halfdelay(1); /* fast refresh while loading commits */
2442 while (!ta->log_complete && !tog_thread_error &&
2443 (ta->commits_needed > 0 || ta->load_all)) {
2444 /* Wake the log thread. */
2445 errcode = pthread_cond_signal(&ta->need_commits);
2447 return got_error_set_errno(errcode,
2448 "pthread_cond_signal");
2451 * The mutex will be released while the view loop waits
2452 * in wgetch(), at which time the log thread will run.
2457 /* Display progress update in log view. */
2458 show_log_view(view);
2462 /* Wait right here while next commit is being loaded. */
2463 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2465 return got_error_set_errno(errcode,
2466 "pthread_cond_wait");
2468 /* Display progress update in log view. */
2469 show_log_view(view);
2477 static const struct got_error *
2478 request_log_commits(struct tog_view *view)
2480 struct tog_log_view_state *state = &view->state.log;
2481 const struct got_error *err = NULL;
2483 if (state->thread_args.log_complete)
2486 state->thread_args.commits_needed += view->nscrolled;
2487 err = trigger_log_thread(view, 1);
2488 view->nscrolled = 0;
2493 static const struct got_error *
2494 log_scroll_down(struct tog_view *view, int maxscroll)
2496 struct tog_log_view_state *s = &view->state.log;
2497 const struct got_error *err = NULL;
2498 struct commit_queue_entry *pentry;
2499 int nscrolled = 0, ncommits_needed;
2501 if (s->last_displayed_entry == NULL)
2504 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2505 if (s->commits.ncommits < ncommits_needed &&
2506 !s->thread_args.log_complete) {
2508 * Ask the log thread for required amount of commits.
2510 s->thread_args.commits_needed +=
2511 ncommits_needed - s->commits.ncommits;
2512 err = trigger_log_thread(view, 1);
2518 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2519 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2522 s->last_displayed_entry = pentry ?
2523 pentry : s->last_displayed_entry;;
2525 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2528 s->first_displayed_entry = pentry;
2529 } while (++nscrolled < maxscroll);
2531 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2532 view->nscrolled += nscrolled;
2534 view->nscrolled = 0;
2539 static const struct got_error *
2540 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2541 struct got_commit_object *commit, struct got_object_id *commit_id,
2542 struct tog_view *log_view, struct got_repository *repo)
2544 const struct got_error *err;
2545 struct got_object_qid *parent_id;
2546 struct tog_view *diff_view;
2548 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2549 if (diff_view == NULL)
2550 return got_error_from_errno("view_open");
2552 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2553 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2554 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2556 *new_view = diff_view;
2560 static const struct got_error *
2561 tree_view_visit_subtree(struct tog_tree_view_state *s,
2562 struct got_tree_object *subtree)
2564 struct tog_parent_tree *parent;
2566 parent = calloc(1, sizeof(*parent));
2568 return got_error_from_errno("calloc");
2570 parent->tree = s->tree;
2571 parent->first_displayed_entry = s->first_displayed_entry;
2572 parent->selected_entry = s->selected_entry;
2573 parent->selected = s->selected;
2574 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2577 s->first_displayed_entry = NULL;
2581 static const struct got_error *
2582 tree_view_walk_path(struct tog_tree_view_state *s,
2583 struct got_commit_object *commit, const char *path)
2585 const struct got_error *err = NULL;
2586 struct got_tree_object *tree = NULL;
2588 char *slash, *subpath = NULL;
2590 /* Walk the path and open corresponding tree objects. */
2593 struct got_tree_entry *te;
2594 struct got_object_id *tree_id;
2600 /* Ensure the correct subtree entry is selected. */
2601 slash = strchr(p, '/');
2603 te_name = strdup(p);
2605 te_name = strndup(p, slash - p);
2606 if (te_name == NULL) {
2607 err = got_error_from_errno("strndup");
2610 te = got_object_tree_find_entry(s->tree, te_name);
2612 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2617 s->first_displayed_entry = s->selected_entry = te;
2619 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2620 break; /* jump to this file's entry */
2622 slash = strchr(p, '/');
2624 subpath = strndup(path, slash - path);
2626 subpath = strdup(path);
2627 if (subpath == NULL) {
2628 err = got_error_from_errno("strdup");
2632 err = got_object_id_by_path(&tree_id, s->repo, commit,
2637 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2642 err = tree_view_visit_subtree(s, tree);
2644 got_object_tree_close(tree);
2658 static const struct got_error *
2659 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2660 struct commit_queue_entry *entry, const char *path,
2661 const char *head_ref_name, struct got_repository *repo)
2663 const struct got_error *err = NULL;
2664 struct tog_tree_view_state *s;
2665 struct tog_view *tree_view;
2667 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2668 if (tree_view == NULL)
2669 return got_error_from_errno("view_open");
2671 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2674 s = &tree_view->state.tree;
2676 *new_view = tree_view;
2678 if (got_path_is_root_dir(path))
2681 return tree_view_walk_path(s, entry->commit, path);
2684 static const struct got_error *
2685 block_signals_used_by_main_thread(void)
2690 if (sigemptyset(&sigset) == -1)
2691 return got_error_from_errno("sigemptyset");
2693 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2694 if (sigaddset(&sigset, SIGWINCH) == -1)
2695 return got_error_from_errno("sigaddset");
2696 if (sigaddset(&sigset, SIGCONT) == -1)
2697 return got_error_from_errno("sigaddset");
2698 if (sigaddset(&sigset, SIGINT) == -1)
2699 return got_error_from_errno("sigaddset");
2700 if (sigaddset(&sigset, SIGTERM) == -1)
2701 return got_error_from_errno("sigaddset");
2703 /* ncurses handles SIGTSTP */
2704 if (sigaddset(&sigset, SIGTSTP) == -1)
2705 return got_error_from_errno("sigaddset");
2707 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2709 return got_error_set_errno(errcode, "pthread_sigmask");
2715 log_thread(void *arg)
2717 const struct got_error *err = NULL;
2719 struct tog_log_thread_args *a = arg;
2723 * Sync startup with main thread such that we begin our
2724 * work once view_input() has released the mutex.
2726 errcode = pthread_mutex_lock(&tog_mutex);
2728 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2732 err = block_signals_used_by_main_thread();
2734 pthread_mutex_unlock(&tog_mutex);
2738 while (!done && !err && !tog_fatal_signal_received()) {
2739 errcode = pthread_mutex_unlock(&tog_mutex);
2741 err = got_error_set_errno(errcode,
2742 "pthread_mutex_unlock");
2745 err = queue_commits(a);
2747 if (err->code != GOT_ERR_ITER_COMPLETED)
2751 } else if (a->commits_needed > 0 && !a->load_all)
2752 a->commits_needed--;
2754 errcode = pthread_mutex_lock(&tog_mutex);
2756 err = got_error_set_errno(errcode,
2757 "pthread_mutex_lock");
2759 } else if (*a->quit)
2761 else if (*a->first_displayed_entry == NULL) {
2762 *a->first_displayed_entry =
2763 TAILQ_FIRST(&a->commits->head);
2764 *a->selected_entry = *a->first_displayed_entry;
2767 errcode = pthread_cond_signal(&a->commit_loaded);
2769 err = got_error_set_errno(errcode,
2770 "pthread_cond_signal");
2771 pthread_mutex_unlock(&tog_mutex);
2776 a->commits_needed = 0;
2778 if (a->commits_needed == 0 && !a->load_all) {
2779 errcode = pthread_cond_wait(&a->need_commits,
2782 err = got_error_set_errno(errcode,
2783 "pthread_cond_wait");
2784 pthread_mutex_unlock(&tog_mutex);
2792 a->log_complete = 1;
2793 errcode = pthread_mutex_unlock(&tog_mutex);
2795 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2798 tog_thread_error = 1;
2799 pthread_cond_signal(&a->commit_loaded);
2804 static const struct got_error *
2805 stop_log_thread(struct tog_log_view_state *s)
2807 const struct got_error *err = NULL, *thread_err = NULL;
2812 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2814 return got_error_set_errno(errcode,
2815 "pthread_cond_signal");
2816 errcode = pthread_mutex_unlock(&tog_mutex);
2818 return got_error_set_errno(errcode,
2819 "pthread_mutex_unlock");
2820 errcode = pthread_join(s->thread, (void **)&thread_err);
2822 return got_error_set_errno(errcode, "pthread_join");
2823 errcode = pthread_mutex_lock(&tog_mutex);
2825 return got_error_set_errno(errcode,
2826 "pthread_mutex_lock");
2830 if (s->thread_args.repo) {
2831 err = got_repo_close(s->thread_args.repo);
2832 s->thread_args.repo = NULL;
2835 if (s->thread_args.pack_fds) {
2836 const struct got_error *pack_err =
2837 got_repo_pack_fds_close(s->thread_args.pack_fds);
2840 s->thread_args.pack_fds = NULL;
2843 if (s->thread_args.graph) {
2844 got_commit_graph_close(s->thread_args.graph);
2845 s->thread_args.graph = NULL;
2848 return err ? err : thread_err;
2851 static const struct got_error *
2852 close_log_view(struct tog_view *view)
2854 const struct got_error *err = NULL;
2855 struct tog_log_view_state *s = &view->state.log;
2858 err = stop_log_thread(s);
2860 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2861 if (errcode && err == NULL)
2862 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2864 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2865 if (errcode && err == NULL)
2866 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2868 free_commits(&s->commits);
2869 free(s->in_repo_path);
2870 s->in_repo_path = NULL;
2873 free(s->head_ref_name);
2874 s->head_ref_name = NULL;
2878 static const struct got_error *
2879 search_start_log_view(struct tog_view *view)
2881 struct tog_log_view_state *s = &view->state.log;
2883 s->matched_entry = NULL;
2884 s->search_entry = NULL;
2888 static const struct got_error *
2889 search_next_log_view(struct tog_view *view)
2891 const struct got_error *err = NULL;
2892 struct tog_log_view_state *s = &view->state.log;
2893 struct commit_queue_entry *entry;
2895 /* Display progress update in log view. */
2896 show_log_view(view);
2900 if (s->search_entry) {
2902 errcode = pthread_mutex_unlock(&tog_mutex);
2904 return got_error_set_errno(errcode,
2905 "pthread_mutex_unlock");
2906 ch = wgetch(view->window);
2907 errcode = pthread_mutex_lock(&tog_mutex);
2909 return got_error_set_errno(errcode,
2910 "pthread_mutex_lock");
2911 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2912 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2915 if (view->searching == TOG_SEARCH_FORWARD)
2916 entry = TAILQ_NEXT(s->search_entry, entry);
2918 entry = TAILQ_PREV(s->search_entry,
2919 commit_queue_head, entry);
2920 } else if (s->matched_entry) {
2921 int matched_idx = s->matched_entry->idx;
2922 int selected_idx = s->selected_entry->idx;
2925 * If the user has moved the cursor after we hit a match,
2926 * the position from where we should continue searching
2927 * might have changed.
2929 if (view->searching == TOG_SEARCH_FORWARD) {
2930 if (matched_idx > selected_idx)
2931 entry = TAILQ_NEXT(s->selected_entry, entry);
2933 entry = TAILQ_NEXT(s->matched_entry, entry);
2935 if (matched_idx < selected_idx)
2936 entry = TAILQ_PREV(s->selected_entry,
2937 commit_queue_head, entry);
2939 entry = TAILQ_PREV(s->matched_entry,
2940 commit_queue_head, entry);
2943 entry = s->selected_entry;
2949 if (entry == NULL) {
2950 if (s->thread_args.log_complete ||
2951 view->searching == TOG_SEARCH_BACKWARD) {
2952 view->search_next_done =
2953 (s->matched_entry == NULL ?
2954 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2955 s->search_entry = NULL;
2959 * Poke the log thread for more commits and return,
2960 * allowing the main loop to make progress. Search
2961 * will resume at s->search_entry once we come back.
2963 s->thread_args.commits_needed++;
2964 return trigger_log_thread(view, 0);
2967 err = match_commit(&have_match, entry->id, entry->commit,
2972 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2973 s->matched_entry = entry;
2977 s->search_entry = entry;
2978 if (view->searching == TOG_SEARCH_FORWARD)
2979 entry = TAILQ_NEXT(entry, entry);
2981 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2984 if (s->matched_entry) {
2985 int cur = s->selected_entry->idx;
2986 while (cur < s->matched_entry->idx) {
2987 err = input_log_view(NULL, view, KEY_DOWN);
2992 while (cur > s->matched_entry->idx) {
2993 err = input_log_view(NULL, view, KEY_UP);
3000 s->search_entry = NULL;
3005 static const struct got_error *
3006 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3007 struct got_repository *repo, const char *head_ref_name,
3008 const char *in_repo_path, int log_branches)
3010 const struct got_error *err = NULL;
3011 struct tog_log_view_state *s = &view->state.log;
3012 struct got_repository *thread_repo = NULL;
3013 struct got_commit_graph *thread_graph = NULL;
3016 if (in_repo_path != s->in_repo_path) {
3017 free(s->in_repo_path);
3018 s->in_repo_path = strdup(in_repo_path);
3019 if (s->in_repo_path == NULL)
3020 return got_error_from_errno("strdup");
3023 /* The commit queue only contains commits being displayed. */
3024 TAILQ_INIT(&s->commits.head);
3025 s->commits.ncommits = 0;
3028 if (head_ref_name) {
3029 s->head_ref_name = strdup(head_ref_name);
3030 if (s->head_ref_name == NULL) {
3031 err = got_error_from_errno("strdup");
3035 s->start_id = got_object_id_dup(start_id);
3036 if (s->start_id == NULL) {
3037 err = got_error_from_errno("got_object_id_dup");
3040 s->log_branches = log_branches;
3042 STAILQ_INIT(&s->colors);
3043 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3044 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3045 get_color_value("TOG_COLOR_COMMIT"));
3048 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3049 get_color_value("TOG_COLOR_AUTHOR"));
3051 free_colors(&s->colors);
3054 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3055 get_color_value("TOG_COLOR_DATE"));
3057 free_colors(&s->colors);
3062 view->show = show_log_view;
3063 view->input = input_log_view;
3064 view->resize = resize_log_view;
3065 view->close = close_log_view;
3066 view->search_start = search_start_log_view;
3067 view->search_next = search_next_log_view;
3069 if (s->thread_args.pack_fds == NULL) {
3070 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3074 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3075 s->thread_args.pack_fds);
3078 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3082 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3083 s->repo, NULL, NULL);
3087 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3089 err = got_error_set_errno(errcode, "pthread_cond_init");
3092 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3094 err = got_error_set_errno(errcode, "pthread_cond_init");
3098 s->thread_args.commits_needed = view->nlines;
3099 s->thread_args.graph = thread_graph;
3100 s->thread_args.commits = &s->commits;
3101 s->thread_args.in_repo_path = s->in_repo_path;
3102 s->thread_args.start_id = s->start_id;
3103 s->thread_args.repo = thread_repo;
3104 s->thread_args.log_complete = 0;
3105 s->thread_args.quit = &s->quit;
3106 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3107 s->thread_args.selected_entry = &s->selected_entry;
3108 s->thread_args.searching = &view->searching;
3109 s->thread_args.search_next_done = &view->search_next_done;
3110 s->thread_args.regex = &view->regex;
3113 close_log_view(view);
3117 static const struct got_error *
3118 show_log_view(struct tog_view *view)
3120 const struct got_error *err;
3121 struct tog_log_view_state *s = &view->state.log;
3123 if (s->thread == NULL) {
3124 int errcode = pthread_create(&s->thread, NULL, log_thread,
3127 return got_error_set_errno(errcode, "pthread_create");
3128 if (s->thread_args.commits_needed > 0) {
3129 err = trigger_log_thread(view, 1);
3135 return draw_commits(view);
3139 log_move_cursor_up(struct tog_view *view, int page, int home)
3141 struct tog_log_view_state *s = &view->state.log;
3143 if (s->selected_entry->idx == 0)
3145 if (s->first_displayed_entry == NULL)
3148 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3150 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3152 if (!page && !home && s->selected > 0)
3155 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3161 static const struct got_error *
3162 log_move_cursor_down(struct tog_view *view, int page)
3164 struct tog_log_view_state *s = &view->state.log;
3165 struct commit_queue_entry *first;
3166 const struct got_error *err = NULL;
3168 first = s->first_displayed_entry;
3169 if (first == NULL) {
3174 if (s->thread_args.log_complete &&
3175 s->selected_entry->idx >= s->commits.ncommits - 1)
3179 int eos = view->nlines - 2;
3181 if (view_is_hsplit_top(view))
3182 --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) {
3188 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3189 s->selected += MIN(s->last_displayed_entry->idx -
3190 s->selected_entry->idx, page + 1);
3192 err = log_scroll_down(view, MIN(page,
3193 s->commits.ncommits - s->selected_entry->idx - 1));
3194 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3196 err = log_scroll_down(view, page);
3199 if (first == s->first_displayed_entry && s->selected <
3200 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3201 s->selected = MIN(s->commits.ncommits - 1, page);
3208 * We might necessarily overshoot in horizontal
3209 * splits; if so, select the last displayed commit.
3211 s->selected = MIN(s->selected,
3212 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3216 if (s->thread_args.log_complete &&
3217 s->selected_entry->idx == s->commits.ncommits - 1)
3224 view_get_split(struct tog_view *view, int *y, int *x)
3229 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3230 if (view->child && view->child->resized_y)
3231 *y = view->child->resized_y;
3232 else if (view->resized_y)
3233 *y = view->resized_y;
3235 *y = view_split_begin_y(view->lines);
3236 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3237 if (view->child && view->child->resized_x)
3238 *x = view->child->resized_x;
3239 else if (view->resized_x)
3240 *x = view->resized_x;
3242 *x = view_split_begin_x(view->begin_x);
3246 /* Split view horizontally at y and offset view->state->selected line. */
3247 static const struct got_error *
3248 view_init_hsplit(struct tog_view *view, int y)
3250 const struct got_error *err = NULL;
3254 err = view_resize(view);
3258 err = offset_selection_down(view);
3263 static const struct got_error *
3264 log_goto_line(struct tog_view *view, int nlines)
3266 const struct got_error *err = NULL;
3267 struct tog_log_view_state *s = &view->state.log;
3268 int g, idx = s->selected_entry->idx;
3273 if (g >= s->first_displayed_entry->idx + 1 &&
3274 g <= s->last_displayed_entry->idx + 1 &&
3275 g - s->first_displayed_entry->idx - 1 < nlines) {
3276 s->selected = g - s->first_displayed_entry->idx - 1;
3282 err = log_move_cursor_down(view, g - idx - 1);
3283 if (!err && g > s->selected_entry->idx + 1)
3284 err = log_move_cursor_down(view,
3285 g - s->first_displayed_entry->idx - 1);
3288 } else if (idx + 1 > g)
3289 log_move_cursor_up(view, idx - g + 1, 0);
3291 if (g < nlines && s->first_displayed_entry->idx == 0)
3292 s->selected = g - 1;
3299 static const struct got_error *
3300 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3302 const struct got_error *err = NULL;
3303 struct tog_log_view_state *s = &view->state.log;
3304 struct commit_queue_entry *entry;
3305 int eos, n, nscroll;
3307 if (s->thread_args.load_all) {
3308 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3309 s->thread_args.load_all = 0;
3310 else if (s->thread_args.log_complete) {
3311 err = log_move_cursor_down(view, s->commits.ncommits);
3312 s->thread_args.load_all = 0;
3317 eos = nscroll = view->nlines - 1;
3318 if (view_is_hsplit_top(view))
3322 return log_goto_line(view, eos);
3332 view->x = MAX(view->maxx - view->ncols / 2, 0);
3337 if (view->x + view->ncols / 2 < view->maxx)
3338 view->x += 2; /* move two columns right */
3344 view->x -= MIN(view->x, 2); /* move two columns back */
3353 log_move_cursor_up(view, 0, 0);
3357 log_move_cursor_up(view, 0, 1);
3367 log_move_cursor_up(view, nscroll, 0);
3374 err = log_move_cursor_down(view, 0);
3377 s->use_committer = !s->use_committer;
3381 /* We don't know yet how many commits, so we're forced to
3382 * traverse them all. */
3384 if (!s->thread_args.log_complete) {
3385 s->thread_args.load_all = 1;
3386 return trigger_log_thread(view, 0);
3390 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3391 for (n = 0; n < eos; n++) {
3394 s->first_displayed_entry = entry;
3395 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3398 s->selected = n - 1;
3410 err = log_move_cursor_down(view, nscroll);
3413 if (s->selected > view->nlines - 2)
3414 s->selected = view->nlines - 2;
3415 if (s->selected > s->commits.ncommits - 1)
3416 s->selected = s->commits.ncommits - 1;
3418 if (s->commits.ncommits < view->nlines - 1 &&
3419 !s->thread_args.log_complete) {
3420 s->thread_args.commits_needed += (view->nlines - 1) -
3421 s->commits.ncommits;
3422 err = trigger_log_thread(view, 1);
3428 if (s->selected_entry == NULL)
3430 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3434 if (s->selected_entry == NULL)
3436 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3442 if (ch == KEY_BACKSPACE &&
3443 got_path_is_root_dir(s->in_repo_path))
3445 err = stop_log_thread(s);
3448 if (ch == KEY_BACKSPACE) {
3450 err = got_path_dirname(&parent_path, s->in_repo_path);
3453 free(s->in_repo_path);
3454 s->in_repo_path = parent_path;
3455 s->thread_args.in_repo_path = s->in_repo_path;
3456 } else if (ch == CTRL('l')) {
3457 struct got_object_id *start_id;
3458 err = got_repo_match_object_id(&start_id, NULL,
3459 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3460 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3464 s->start_id = start_id;
3465 s->thread_args.start_id = s->start_id;
3467 s->log_branches = !s->log_branches;
3469 if (s->thread_args.pack_fds == NULL) {
3470 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3474 err = got_repo_open(&s->thread_args.repo,
3475 got_repo_get_path(s->repo), NULL,
3476 s->thread_args.pack_fds);
3480 err = tog_load_refs(s->repo, 0);
3483 err = got_commit_graph_open(&s->thread_args.graph,
3484 s->in_repo_path, !s->log_branches);
3487 err = got_commit_graph_iter_start(s->thread_args.graph,
3488 s->start_id, s->repo, NULL, NULL);
3491 free_commits(&s->commits);
3492 s->first_displayed_entry = NULL;
3493 s->last_displayed_entry = NULL;
3494 s->selected_entry = NULL;
3496 s->thread_args.log_complete = 0;
3498 s->thread_args.commits_needed = view->lines;
3499 s->matched_entry = NULL;
3500 s->search_entry = NULL;
3505 err = view_request_new(new_view, view, TOG_VIEW_REF);
3515 static const struct got_error *
3516 apply_unveil(const char *repo_path, const char *worktree_path)
3518 const struct got_error *error;
3521 if (unveil("gmon.out", "rwc") != 0)
3522 return got_error_from_errno2("unveil", "gmon.out");
3524 if (repo_path && unveil(repo_path, "r") != 0)
3525 return got_error_from_errno2("unveil", repo_path);
3527 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3528 return got_error_from_errno2("unveil", worktree_path);
3530 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3531 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3533 error = got_privsep_unveil_exec_helpers();
3537 if (unveil(NULL, NULL) != 0)
3538 return got_error_from_errno("unveil");
3547 * Override default signal handlers before starting ncurses.
3548 * This should prevent ncurses from installing its own
3549 * broken cleanup() signal handler.
3551 signal(SIGWINCH, tog_sigwinch);
3552 signal(SIGPIPE, tog_sigpipe);
3553 signal(SIGCONT, tog_sigcont);
3554 signal(SIGINT, tog_sigint);
3555 signal(SIGTERM, tog_sigterm);
3559 halfdelay(1); /* Do fast refresh while initial view is loading. */
3562 intrflush(stdscr, FALSE);
3563 keypad(stdscr, TRUE);
3565 if (getenv("TOG_COLORS") != NULL) {
3567 use_default_colors();
3571 static const struct got_error *
3572 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3573 struct got_repository *repo, struct got_worktree *worktree)
3575 const struct got_error *err = NULL;
3578 *in_repo_path = strdup("/");
3579 if (*in_repo_path == NULL)
3580 return got_error_from_errno("strdup");
3585 const char *prefix = got_worktree_get_path_prefix(worktree);
3588 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3591 if (asprintf(in_repo_path, "%s%s%s", prefix,
3592 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3594 err = got_error_from_errno("asprintf");
3595 *in_repo_path = NULL;
3599 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3604 static const struct got_error *
3605 cmd_log(int argc, char *argv[])
3607 const struct got_error *error;
3608 struct got_repository *repo = NULL;
3609 struct got_worktree *worktree = NULL;
3610 struct got_object_id *start_id = NULL;
3611 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3612 char *start_commit = NULL, *label = NULL;
3613 struct got_reference *ref = NULL;
3614 const char *head_ref_name = NULL;
3615 int ch, log_branches = 0;
3616 struct tog_view *view;
3617 int *pack_fds = NULL;
3619 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3625 start_commit = optarg;
3628 repo_path = realpath(optarg, NULL);
3629 if (repo_path == NULL)
3630 return got_error_from_errno2("realpath",
3645 error = got_repo_pack_fds_open(&pack_fds);
3649 if (repo_path == NULL) {
3650 cwd = getcwd(NULL, 0);
3652 return got_error_from_errno("getcwd");
3653 error = got_worktree_open(&worktree, cwd);
3654 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3658 strdup(got_worktree_get_repo_path(worktree));
3660 repo_path = strdup(cwd);
3661 if (repo_path == NULL) {
3662 error = got_error_from_errno("strdup");
3667 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3671 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3678 error = apply_unveil(got_repo_get_path(repo),
3679 worktree ? got_worktree_get_root_path(worktree) : NULL);
3683 /* already loaded by tog_log_with_path()? */
3684 if (TAILQ_EMPTY(&tog_refs)) {
3685 error = tog_load_refs(repo, 0);
3690 if (start_commit == NULL) {
3691 error = got_repo_match_object_id(&start_id, &label,
3692 worktree ? got_worktree_get_head_ref_name(worktree) :
3693 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3696 head_ref_name = label;
3698 error = got_ref_open(&ref, repo, start_commit, 0);
3700 head_ref_name = got_ref_get_name(ref);
3701 else if (error->code != GOT_ERR_NOT_REF)
3703 error = got_repo_match_object_id(&start_id, NULL,
3704 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3709 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3711 error = got_error_from_errno("view_open");
3714 error = open_log_view(view, start_id, repo, head_ref_name,
3715 in_repo_path, log_branches);
3719 /* Release work tree lock. */
3720 got_worktree_close(worktree);
3723 error = view_loop(view);
3733 const struct got_error *close_err = got_repo_close(repo);
3738 got_worktree_close(worktree);
3740 const struct got_error *pack_err =
3741 got_repo_pack_fds_close(pack_fds);
3753 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3754 "[-w] object1 object2\n", getprogname());
3759 match_line(const char *line, regex_t *regex, size_t nmatch,
3760 regmatch_t *regmatch)
3762 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3765 static struct tog_color *
3766 match_color(struct tog_colors *colors, const char *line)
3768 struct tog_color *tc = NULL;
3770 STAILQ_FOREACH(tc, colors, entry) {
3771 if (match_line(line, &tc->regex, 0, NULL))
3778 static const struct got_error *
3779 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3780 WINDOW *window, int skipcol, regmatch_t *regmatch)
3782 const struct got_error *err = NULL;
3784 wchar_t *wline = NULL;
3785 int rme, rms, n, width, scrollx;
3786 int width0 = 0, width1 = 0, width2 = 0;
3787 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3791 rms = regmatch->rm_so;
3792 rme = regmatch->rm_eo;
3794 err = expand_tab(&exstr, line);
3798 /* Split the line into 3 segments, according to match offsets. */
3799 seg0 = strndup(exstr, rms);
3801 err = got_error_from_errno("strndup");
3804 seg1 = strndup(exstr + rms, rme - rms);
3806 err = got_error_from_errno("strndup");
3809 seg2 = strdup(exstr + rme);
3811 err = got_error_from_errno("strndup");
3815 /* draw up to matched token if we haven't scrolled past it */
3816 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3820 n = MAX(width0 - skipcol, 0);
3823 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3824 wlimit, col_tab_align, 1);
3827 waddwstr(window, &wline[scrollx]);
3837 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3841 wlen = wcslen(wline);
3843 width = wcwidth(wline[i]);
3845 /* should not happen, tabs are expanded */
3846 err = got_error(GOT_ERR_RANGE);
3849 if (width0 + w + width > skipcol)
3854 /* draw (visible part of) matched token (if scrolled into it) */
3855 if (width1 - w > 0) {
3856 wattron(window, A_STANDOUT);
3857 waddwstr(window, &wline[i]);
3858 wattroff(window, A_STANDOUT);
3859 wlimit -= (width1 - w);
3860 *wtotal += (width1 - w);
3864 if (wlimit > 0) { /* draw rest of line */
3866 if (skipcol > width0 + width1) {
3867 err = format_line(&wline, &width2, &scrollx, seg2,
3868 skipcol - (width0 + width1), wlimit,
3872 waddwstr(window, &wline[scrollx]);
3874 err = format_line(&wline, &width2, NULL, seg2, 0,
3875 wlimit, col_tab_align, 1);
3878 waddwstr(window, wline);
3892 gotoline(struct tog_view *view, int *lineno, int *nprinted)
3895 int *eof, *first, *selected;
3897 if (view->type == TOG_VIEW_DIFF) {
3898 struct tog_diff_view_state *s = &view->state.diff;
3900 first = &s->first_displayed_line;
3904 } else if (view->type == TOG_VIEW_BLAME) {
3905 struct tog_blame_view_state *s = &view->state.blame;
3907 first = &s->first_displayed_line;
3908 selected = &s->selected_line;
3914 /* Center gline in the middle of the page like vi(1). */
3915 if (*lineno < view->gline - (view->nlines - 3) / 2)
3917 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3926 *selected = view->gline <= (view->nlines - 3) / 2 ?
3927 view->gline : (view->nlines - 3) / 2 + 1;
3933 static const struct got_error *
3934 draw_file(struct tog_view *view, const char *header)
3936 struct tog_diff_view_state *s = &view->state.diff;
3937 regmatch_t *regmatch = &view->regmatch;
3938 const struct got_error *err;
3939 int lineno, nprinted = 0;
3941 size_t linesize = 0;
3943 struct tog_color *tc;
3946 int max_lines = view->nlines;
3947 int nlines = s->nlines;
3951 lineno = s->first_displayed_line - 1;
3952 line_offset = s->line_offsets[s->first_displayed_line - 1];
3953 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3954 return got_error_from_errno("fseek");
3956 werase(view->window);
3958 if (view->gline > s->nlines - 1)
3959 view->gline = s->nlines - 1;
3962 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3963 1 : view->gline - (view->nlines - 3) / 2 :
3964 lineno + s->selected_line;
3966 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3967 return got_error_from_errno("asprintf");
3968 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3974 if (view_needs_focus_indication(view))
3975 wstandout(view->window);
3976 waddwstr(view->window, wline);
3979 if (view_needs_focus_indication(view))
3980 wstandend(view->window);
3981 if (width <= view->ncols - 1)
3982 waddch(view->window, '\n');
3992 while (max_lines > 0 && nprinted < max_lines) {
3993 linelen = getline(&line, &linesize, s->f);
3994 if (linelen == -1) {
4000 return got_ferror(s->f, GOT_ERR_IO);
4004 if (++lineno < s->first_displayed_line)
4006 if (view->gline && !gotoline(view, &lineno, &nprinted))
4008 if (lineno == view->hiline)
4011 /* Set view->maxx based on full line length. */
4012 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4018 view->maxx = MAX(view->maxx, width);
4022 tc = match_color(&s->colors, line);
4024 attr |= COLOR_PAIR(tc->colorpair);
4026 wattron(view->window, attr);
4027 if (s->first_displayed_line + nprinted == s->matched_line &&
4028 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4029 err = add_matched_line(&width, line, view->ncols, 0,
4030 view->window, view->x, regmatch);
4037 err = format_line(&wline, &width, &skip, line,
4038 view->x, view->ncols, 0, view->x ? 1 : 0);
4043 waddwstr(view->window, &wline[skip]);
4047 if (lineno == view->hiline) {
4048 /* highlight full gline length */
4049 while (width++ < view->ncols)
4050 waddch(view->window, ' ');
4052 if (width <= view->ncols - 1)
4053 waddch(view->window, '\n');
4056 wattroff(view->window, attr);
4057 if (++nprinted == 1)
4058 s->first_displayed_line = lineno;
4062 s->last_displayed_line = s->first_displayed_line +
4065 s->last_displayed_line = s->first_displayed_line;
4070 while (nprinted < view->nlines) {
4071 waddch(view->window, '\n');
4075 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4081 wstandout(view->window);
4082 waddwstr(view->window, wline);
4085 wstandend(view->window);
4092 get_datestr(time_t *time, char *datebuf)
4094 struct tm mytm, *tm;
4097 tm = gmtime_r(time, &mytm);
4100 s = asctime_r(tm, datebuf);
4103 p = strchr(s, '\n');
4109 static const struct got_error *
4110 get_changed_paths(struct got_pathlist_head *paths,
4111 struct got_commit_object *commit, struct got_repository *repo)
4113 const struct got_error *err = NULL;
4114 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4115 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4116 struct got_object_qid *qid;
4118 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4120 struct got_commit_object *pcommit;
4121 err = got_object_open_as_commit(&pcommit, repo,
4126 tree_id1 = got_object_id_dup(
4127 got_object_commit_get_tree_id(pcommit));
4128 if (tree_id1 == NULL) {
4129 got_object_commit_close(pcommit);
4130 return got_error_from_errno("got_object_id_dup");
4132 got_object_commit_close(pcommit);
4137 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4142 tree_id2 = got_object_commit_get_tree_id(commit);
4143 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4147 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4148 got_diff_tree_collect_changed_paths, paths, 0);
4151 got_object_tree_close(tree1);
4153 got_object_tree_close(tree2);
4158 static const struct got_error *
4159 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4163 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4165 return got_error_from_errno("reallocarray");
4167 (*line_offsets)[*nlines] = off;
4172 static const struct got_error *
4173 write_commit_info(off_t **line_offsets, size_t *nlines,
4174 struct got_object_id *commit_id, struct got_reflist_head *refs,
4175 struct got_repository *repo, FILE *outfile)
4177 const struct got_error *err = NULL;
4178 char datebuf[26], *datestr;
4179 struct got_commit_object *commit;
4180 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4181 time_t committer_time;
4182 const char *author, *committer;
4183 char *refs_str = NULL;
4184 struct got_pathlist_head changed_paths;
4185 struct got_pathlist_entry *pe;
4189 TAILQ_INIT(&changed_paths);
4192 err = build_refs_str(&refs_str, refs, commit_id, repo);
4197 err = got_object_open_as_commit(&commit, repo, commit_id);
4201 err = got_object_id_str(&id_str, commit_id);
4203 err = got_error_from_errno("got_object_id_str");
4207 err = add_line_offset(line_offsets, nlines, 0);
4211 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4212 refs_str ? refs_str : "", refs_str ? ")" : "");
4214 err = got_error_from_errno("fprintf");
4218 err = add_line_offset(line_offsets, nlines, outoff);
4222 n = fprintf(outfile, "from: %s\n",
4223 got_object_commit_get_author(commit));
4225 err = got_error_from_errno("fprintf");
4229 err = add_line_offset(line_offsets, nlines, outoff);
4233 committer_time = got_object_commit_get_committer_time(commit);
4234 datestr = get_datestr(&committer_time, datebuf);
4236 n = fprintf(outfile, "date: %s UTC\n", datestr);
4238 err = got_error_from_errno("fprintf");
4242 err = add_line_offset(line_offsets, nlines, outoff);
4246 author = got_object_commit_get_author(commit);
4247 committer = got_object_commit_get_committer(commit);
4248 if (strcmp(author, committer) != 0) {
4249 n = fprintf(outfile, "via: %s\n", committer);
4251 err = got_error_from_errno("fprintf");
4255 err = add_line_offset(line_offsets, nlines, outoff);
4259 if (got_object_commit_get_nparents(commit) > 1) {
4260 const struct got_object_id_queue *parent_ids;
4261 struct got_object_qid *qid;
4263 parent_ids = got_object_commit_get_parent_ids(commit);
4264 STAILQ_FOREACH(qid, parent_ids, entry) {
4265 err = got_object_id_str(&id_str, &qid->id);
4268 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4270 err = got_error_from_errno("fprintf");
4274 err = add_line_offset(line_offsets, nlines, outoff);
4282 err = got_object_commit_get_logmsg(&logmsg, commit);
4286 while ((line = strsep(&s, "\n")) != NULL) {
4287 n = fprintf(outfile, "%s\n", line);
4289 err = got_error_from_errno("fprintf");
4293 err = add_line_offset(line_offsets, nlines, outoff);
4298 err = get_changed_paths(&changed_paths, commit, repo);
4301 TAILQ_FOREACH(pe, &changed_paths, entry) {
4302 struct got_diff_changed_path *cp = pe->data;
4303 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4305 err = got_error_from_errno("fprintf");
4309 err = add_line_offset(line_offsets, nlines, outoff);
4312 free((char *)pe->path);
4316 fputc('\n', outfile);
4318 err = add_line_offset(line_offsets, nlines, outoff);
4320 got_pathlist_free(&changed_paths);
4324 got_object_commit_close(commit);
4326 free(*line_offsets);
4327 *line_offsets = NULL;
4333 static const struct got_error *
4334 create_diff(struct tog_diff_view_state *s)
4336 const struct got_error *err = NULL;
4340 free(s->line_offsets);
4341 s->line_offsets = malloc(sizeof(off_t));
4342 if (s->line_offsets == NULL)
4343 return got_error_from_errno("malloc");
4348 err = got_error_from_errno("got_opentemp");
4351 if (s->f && fclose(s->f) == EOF) {
4352 err = got_error_from_errno("fclose");
4358 err = got_object_get_type(&obj_type, s->repo, s->id1);
4360 err = got_object_get_type(&obj_type, s->repo, s->id2);
4365 case GOT_OBJ_TYPE_BLOB:
4366 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4367 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4368 s->label1, s->label2, tog_diff_algo, s->diff_context,
4369 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4371 case GOT_OBJ_TYPE_TREE:
4372 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4373 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4374 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4375 s->force_text_diff, s->repo, s->f);
4377 case GOT_OBJ_TYPE_COMMIT: {
4378 const struct got_object_id_queue *parent_ids;
4379 struct got_object_qid *pid;
4380 struct got_commit_object *commit2;
4381 struct got_reflist_head *refs;
4383 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4386 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4387 /* Show commit info if we're diffing to a parent/root commit. */
4388 if (s->id1 == NULL) {
4389 err = write_commit_info(&s->line_offsets, &s->nlines,
4390 s->id2, refs, s->repo, s->f);
4394 parent_ids = got_object_commit_get_parent_ids(commit2);
4395 STAILQ_FOREACH(pid, parent_ids, entry) {
4396 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4397 err = write_commit_info(
4398 &s->line_offsets, &s->nlines,
4399 s->id2, refs, s->repo, s->f);
4406 got_object_commit_close(commit2);
4408 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4409 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4410 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4411 s->force_text_diff, s->repo, s->f);
4415 err = got_error(GOT_ERR_OBJ_TYPE);
4421 if (s->f && fflush(s->f) != 0 && err == NULL)
4422 err = got_error_from_errno("fflush");
4427 diff_view_indicate_progress(struct tog_view *view)
4429 mvwaddstr(view->window, 0, 0, "diffing...");
4434 static const struct got_error *
4435 search_start_diff_view(struct tog_view *view)
4437 struct tog_diff_view_state *s = &view->state.diff;
4439 s->matched_line = 0;
4443 static const struct got_error *
4444 search_next_diff_view(struct tog_view *view)
4446 struct tog_diff_view_state *s = &view->state.diff;
4447 const struct got_error *err = NULL;
4450 size_t linesize = 0;
4453 if (!view->searching) {
4454 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4458 if (s->matched_line) {
4459 if (view->searching == TOG_SEARCH_FORWARD)
4460 lineno = s->matched_line + 1;
4462 lineno = s->matched_line - 1;
4464 lineno = s->first_displayed_line;
4469 if (lineno <= 0 || lineno > s->nlines) {
4470 if (s->matched_line == 0) {
4471 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4475 if (view->searching == TOG_SEARCH_FORWARD)
4481 offset = s->line_offsets[lineno - 1];
4482 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4484 return got_error_from_errno("fseeko");
4486 linelen = getline(&line, &linesize, s->f);
4487 if (linelen != -1) {
4489 err = expand_tab(&exstr, line);
4492 if (match_line(exstr, &view->regex, 1,
4494 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4495 s->matched_line = lineno;
4501 if (view->searching == TOG_SEARCH_FORWARD)
4508 if (s->matched_line) {
4509 s->first_displayed_line = s->matched_line;
4510 s->selected_line = 1;
4516 static const struct got_error *
4517 close_diff_view(struct tog_view *view)
4519 const struct got_error *err = NULL;
4520 struct tog_diff_view_state *s = &view->state.diff;
4526 if (s->f && fclose(s->f) == EOF)
4527 err = got_error_from_errno("fclose");
4529 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4530 err = got_error_from_errno("fclose");
4532 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4533 err = got_error_from_errno("fclose");
4535 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4536 err = got_error_from_errno("close");
4538 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4539 err = got_error_from_errno("close");
4541 free_colors(&s->colors);
4542 free(s->line_offsets);
4543 s->line_offsets = NULL;
4548 static const struct got_error *
4549 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4550 struct got_object_id *id2, const char *label1, const char *label2,
4551 int diff_context, int ignore_whitespace, int force_text_diff,
4552 struct tog_view *parent_view, struct got_repository *repo)
4554 const struct got_error *err;
4555 struct tog_diff_view_state *s = &view->state.diff;
4557 memset(s, 0, sizeof(*s));
4561 if (id1 != NULL && id2 != NULL) {
4563 err = got_object_get_type(&type1, repo, id1);
4566 err = got_object_get_type(&type2, repo, id2);
4571 return got_error(GOT_ERR_OBJ_TYPE);
4573 s->first_displayed_line = 1;
4574 s->last_displayed_line = view->nlines;
4575 s->selected_line = 1;
4583 s->id1 = got_object_id_dup(id1);
4585 return got_error_from_errno("got_object_id_dup");
4589 s->id2 = got_object_id_dup(id2);
4590 if (s->id2 == NULL) {
4591 err = got_error_from_errno("got_object_id_dup");
4595 s->f1 = got_opentemp();
4596 if (s->f1 == NULL) {
4597 err = got_error_from_errno("got_opentemp");
4601 s->f2 = got_opentemp();
4602 if (s->f2 == NULL) {
4603 err = got_error_from_errno("got_opentemp");
4607 s->fd1 = got_opentempfd();
4609 err = got_error_from_errno("got_opentempfd");
4613 s->fd2 = got_opentempfd();
4615 err = got_error_from_errno("got_opentempfd");
4619 s->first_displayed_line = 1;
4620 s->last_displayed_line = view->nlines;
4621 s->diff_context = diff_context;
4622 s->ignore_whitespace = ignore_whitespace;
4623 s->force_text_diff = force_text_diff;
4624 s->parent_view = parent_view;
4627 STAILQ_INIT(&s->colors);
4628 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4629 err = add_color(&s->colors,
4630 "^-", TOG_COLOR_DIFF_MINUS,
4631 get_color_value("TOG_COLOR_DIFF_MINUS"));
4634 err = add_color(&s->colors, "^\\+",
4635 TOG_COLOR_DIFF_PLUS,
4636 get_color_value("TOG_COLOR_DIFF_PLUS"));
4639 err = add_color(&s->colors,
4640 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4641 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4645 err = add_color(&s->colors,
4646 "^(commit [0-9a-f]|parent [0-9]|"
4647 "(blob|file|tree|commit) [-+] |"
4648 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4649 get_color_value("TOG_COLOR_DIFF_META"));
4653 err = add_color(&s->colors,
4654 "^(from|via): ", TOG_COLOR_AUTHOR,
4655 get_color_value("TOG_COLOR_AUTHOR"));
4659 err = add_color(&s->colors,
4660 "^date: ", TOG_COLOR_DATE,
4661 get_color_value("TOG_COLOR_DATE"));
4666 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4667 view_is_splitscreen(view))
4668 show_log_view(parent_view); /* draw border */
4669 diff_view_indicate_progress(view);
4671 err = create_diff(s);
4673 view->show = show_diff_view;
4674 view->input = input_diff_view;
4675 view->reset = reset_diff_view;
4676 view->close = close_diff_view;
4677 view->search_start = search_start_diff_view;
4678 view->search_next = search_next_diff_view;
4681 close_diff_view(view);
4685 static const struct got_error *
4686 show_diff_view(struct tog_view *view)
4688 const struct got_error *err;
4689 struct tog_diff_view_state *s = &view->state.diff;
4690 char *id_str1 = NULL, *id_str2, *header;
4691 const char *label1, *label2;
4694 err = got_object_id_str(&id_str1, s->id1);
4697 label1 = s->label1 ? : id_str1;
4699 label1 = "/dev/null";
4701 err = got_object_id_str(&id_str2, s->id2);
4704 label2 = s->label2 ? : id_str2;
4706 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4707 err = got_error_from_errno("asprintf");
4715 err = draw_file(view, header);
4720 static const struct got_error *
4721 set_selected_commit(struct tog_diff_view_state *s,
4722 struct commit_queue_entry *entry)
4724 const struct got_error *err;
4725 const struct got_object_id_queue *parent_ids;
4726 struct got_commit_object *selected_commit;
4727 struct got_object_qid *pid;
4730 s->id2 = got_object_id_dup(entry->id);
4732 return got_error_from_errno("got_object_id_dup");
4734 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4737 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4739 pid = STAILQ_FIRST(parent_ids);
4740 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4741 got_object_commit_close(selected_commit);
4745 static const struct got_error *
4746 reset_diff_view(struct tog_view *view)
4748 struct tog_diff_view_state *s = &view->state.diff;
4751 wclear(view->window);
4752 s->first_displayed_line = 1;
4753 s->last_displayed_line = view->nlines;
4754 s->matched_line = 0;
4755 diff_view_indicate_progress(view);
4756 return create_diff(s);
4759 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4761 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4764 static const struct got_error *
4765 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4767 const struct got_error *err = NULL;
4768 struct tog_diff_view_state *s = &view->state.diff;
4769 struct tog_log_view_state *ls;
4770 struct commit_queue_entry *old_selected_entry;
4772 size_t linesize = 0;
4774 int i, nscroll = view->nlines - 1, up = 0;
4781 view->x = MAX(view->maxx - view->ncols / 3, 0);
4786 if (view->x + view->ncols / 3 < view->maxx)
4787 view->x += 2; /* move two columns right */
4793 view->x -= MIN(view->x, 2); /* move two columns back */
4800 s->force_text_diff = !s->force_text_diff;
4802 s->ignore_whitespace = !s->ignore_whitespace;
4803 err = reset_diff_view(view);
4807 s->first_displayed_line = 1;
4816 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4822 if (s->first_displayed_line > 1)
4823 s->first_displayed_line--;
4834 if (s->first_displayed_line == 1) {
4839 while (i++ < nscroll && s->first_displayed_line > 1)
4840 s->first_displayed_line--;
4846 s->first_displayed_line++;
4863 while (!s->eof && i++ < nscroll) {
4864 linelen = getline(&line, &linesize, s->f);
4865 s->first_displayed_line++;
4866 if (linelen == -1) {
4870 err = got_ferror(s->f, GOT_ERR_IO);
4877 if (s->diff_context > 0) {
4879 s->matched_line = 0;
4880 diff_view_indicate_progress(view);
4881 err = create_diff(s);
4882 if (s->first_displayed_line + view->nlines - 1 >
4884 s->first_displayed_line = 1;
4885 s->last_displayed_line = view->nlines;
4891 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4893 s->matched_line = 0;
4894 diff_view_indicate_progress(view);
4895 err = create_diff(s);
4907 if (s->parent_view == NULL) {
4911 s->parent_view->count = view->count;
4913 if (s->parent_view->type == TOG_VIEW_LOG) {
4914 ls = &s->parent_view->state.log;
4915 old_selected_entry = ls->selected_entry;
4917 err = input_log_view(NULL, s->parent_view,
4918 up ? KEY_UP : KEY_DOWN);
4921 view->count = s->parent_view->count;
4923 if (old_selected_entry == ls->selected_entry)
4926 err = set_selected_commit(s, ls->selected_entry);
4929 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4930 struct tog_blame_view_state *bs;
4931 struct got_object_id *id, *prev_id;
4933 bs = &s->parent_view->state.blame;
4934 prev_id = get_annotation_for_line(bs->blame.lines,
4935 bs->blame.nlines, bs->last_diffed_line);
4937 err = input_blame_view(&view, s->parent_view,
4938 up ? KEY_UP : KEY_DOWN);
4941 view->count = s->parent_view->count;
4943 if (prev_id == NULL)
4945 id = get_selected_commit_id(bs->blame.lines,
4946 bs->blame.nlines, bs->first_displayed_line,
4951 if (!got_object_id_cmp(prev_id, id))
4954 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4958 s->first_displayed_line = 1;
4959 s->last_displayed_line = view->nlines;
4960 s->matched_line = 0;
4963 diff_view_indicate_progress(view);
4964 err = create_diff(s);
4974 static const struct got_error *
4975 cmd_diff(int argc, char *argv[])
4977 const struct got_error *error = NULL;
4978 struct got_repository *repo = NULL;
4979 struct got_worktree *worktree = NULL;
4980 struct got_object_id *id1 = NULL, *id2 = NULL;
4981 char *repo_path = NULL, *cwd = NULL;
4982 char *id_str1 = NULL, *id_str2 = NULL;
4983 char *label1 = NULL, *label2 = NULL;
4984 int diff_context = 3, ignore_whitespace = 0;
4985 int ch, force_text_diff = 0;
4987 struct tog_view *view;
4988 int *pack_fds = NULL;
4990 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4993 force_text_diff = 1;
4996 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4999 errx(1, "number of context lines is %s: %s",
5003 repo_path = realpath(optarg, NULL);
5004 if (repo_path == NULL)
5005 return got_error_from_errno2("realpath",
5007 got_path_strip_trailing_slashes(repo_path);
5010 ignore_whitespace = 1;
5022 usage_diff(); /* TODO show local worktree changes */
5023 } else if (argc == 2) {
5029 error = got_repo_pack_fds_open(&pack_fds);
5033 if (repo_path == NULL) {
5034 cwd = getcwd(NULL, 0);
5036 return got_error_from_errno("getcwd");
5037 error = got_worktree_open(&worktree, cwd);
5038 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5042 strdup(got_worktree_get_repo_path(worktree));
5044 repo_path = strdup(cwd);
5045 if (repo_path == NULL) {
5046 error = got_error_from_errno("strdup");
5051 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5057 error = apply_unveil(got_repo_get_path(repo), NULL);
5061 error = tog_load_refs(repo, 0);
5065 error = got_repo_match_object_id(&id1, &label1, id_str1,
5066 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5070 error = got_repo_match_object_id(&id2, &label2, id_str2,
5071 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5075 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5077 error = got_error_from_errno("view_open");
5080 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5081 ignore_whitespace, force_text_diff, NULL, repo);
5084 error = view_loop(view);
5091 const struct got_error *close_err = got_repo_close(repo);
5096 got_worktree_close(worktree);
5098 const struct got_error *pack_err =
5099 got_repo_pack_fds_close(pack_fds);
5112 "usage: %s blame [-c commit] [-r repository-path] path\n",
5117 struct tog_blame_line {
5119 struct got_object_id *id;
5122 static const struct got_error *
5123 draw_blame(struct tog_view *view)
5125 struct tog_blame_view_state *s = &view->state.blame;
5126 struct tog_blame *blame = &s->blame;
5127 regmatch_t *regmatch = &view->regmatch;
5128 const struct got_error *err;
5129 int lineno = 0, nprinted = 0;
5131 size_t linesize = 0;
5135 struct tog_blame_line *blame_line;
5136 struct got_object_id *prev_id = NULL;
5138 struct tog_color *tc;
5140 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5145 werase(view->window);
5147 if (asprintf(&line, "commit %s", id_str) == -1) {
5148 err = got_error_from_errno("asprintf");
5153 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5158 if (view_needs_focus_indication(view))
5159 wstandout(view->window);
5160 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5162 wattr_on(view->window,
5163 COLOR_PAIR(tc->colorpair), NULL);
5164 waddwstr(view->window, wline);
5166 wattr_off(view->window,
5167 COLOR_PAIR(tc->colorpair), NULL);
5168 if (view_needs_focus_indication(view))
5169 wstandend(view->window);
5172 if (width < view->ncols - 1)
5173 waddch(view->window, '\n');
5175 if (view->gline > blame->nlines)
5176 view->gline = blame->nlines;
5178 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5179 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5180 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5182 return got_error_from_errno("asprintf");
5185 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5190 waddwstr(view->window, wline);
5193 if (width < view->ncols - 1)
5194 waddch(view->window, '\n');
5198 while (nprinted < view->nlines - 2) {
5199 linelen = getline(&line, &linesize, blame->f);
5200 if (linelen == -1) {
5201 if (feof(blame->f)) {
5206 return got_ferror(blame->f, GOT_ERR_IO);
5208 if (++lineno < s->first_displayed_line)
5210 if (view->gline && !gotoline(view, &lineno, &nprinted))
5213 /* Set view->maxx based on full line length. */
5214 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5221 view->maxx = MAX(view->maxx, width);
5223 if (nprinted == s->selected_line - 1)
5224 wstandout(view->window);
5226 if (blame->nlines > 0) {
5227 blame_line = &blame->lines[lineno - 1];
5228 if (blame_line->annotated && prev_id &&
5229 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5230 !(nprinted == s->selected_line - 1)) {
5231 waddstr(view->window, " ");
5232 } else if (blame_line->annotated) {
5234 err = got_object_id_str(&id_str,
5240 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5242 wattr_on(view->window,
5243 COLOR_PAIR(tc->colorpair), NULL);
5244 wprintw(view->window, "%.8s", id_str);
5246 wattr_off(view->window,
5247 COLOR_PAIR(tc->colorpair), NULL);
5249 prev_id = blame_line->id;
5251 waddstr(view->window, "........");
5255 waddstr(view->window, "........");
5259 if (nprinted == s->selected_line - 1)
5260 wstandend(view->window);
5261 waddstr(view->window, " ");
5263 if (view->ncols <= 9) {
5265 } else if (s->first_displayed_line + nprinted ==
5267 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5268 err = add_matched_line(&width, line, view->ncols - 9, 9,
5269 view->window, view->x, regmatch);
5277 err = format_line(&wline, &width, &skip, line,
5278 view->x, view->ncols - 9, 9, 1);
5283 waddwstr(view->window, &wline[skip]);
5289 if (width <= view->ncols - 1)
5290 waddch(view->window, '\n');
5291 if (++nprinted == 1)
5292 s->first_displayed_line = lineno;
5295 s->last_displayed_line = lineno;
5302 static const struct got_error *
5303 blame_cb(void *arg, int nlines, int lineno,
5304 struct got_commit_object *commit, struct got_object_id *id)
5306 const struct got_error *err = NULL;
5307 struct tog_blame_cb_args *a = arg;
5308 struct tog_blame_line *line;
5311 if (nlines != a->nlines ||
5312 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5313 return got_error(GOT_ERR_RANGE);
5315 errcode = pthread_mutex_lock(&tog_mutex);
5317 return got_error_set_errno(errcode, "pthread_mutex_lock");
5319 if (*a->quit) { /* user has quit the blame view */
5320 err = got_error(GOT_ERR_ITER_COMPLETED);
5325 goto done; /* no change in this commit */
5327 line = &a->lines[lineno - 1];
5328 if (line->annotated)
5331 line->id = got_object_id_dup(id);
5332 if (line->id == NULL) {
5333 err = got_error_from_errno("got_object_id_dup");
5336 line->annotated = 1;
5338 errcode = pthread_mutex_unlock(&tog_mutex);
5340 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5345 blame_thread(void *arg)
5347 const struct got_error *err, *close_err;
5348 struct tog_blame_thread_args *ta = arg;
5349 struct tog_blame_cb_args *a = ta->cb_args;
5350 int errcode, fd1 = -1, fd2 = -1;
5351 FILE *f1 = NULL, *f2 = NULL;
5353 fd1 = got_opentempfd();
5355 return (void *)got_error_from_errno("got_opentempfd");
5357 fd2 = got_opentempfd();
5359 err = got_error_from_errno("got_opentempfd");
5363 f1 = got_opentemp();
5365 err = (void *)got_error_from_errno("got_opentemp");
5368 f2 = got_opentemp();
5370 err = (void *)got_error_from_errno("got_opentemp");
5374 err = block_signals_used_by_main_thread();
5378 err = got_blame(ta->path, a->commit_id, ta->repo,
5379 tog_diff_algo, blame_cb, ta->cb_args,
5380 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5381 if (err && err->code == GOT_ERR_CANCELLED)
5384 errcode = pthread_mutex_lock(&tog_mutex);
5386 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5390 close_err = got_repo_close(ta->repo);
5396 errcode = pthread_mutex_unlock(&tog_mutex);
5397 if (errcode && err == NULL)
5398 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5401 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5402 err = got_error_from_errno("close");
5403 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5404 err = got_error_from_errno("close");
5405 if (f1 && fclose(f1) == EOF && err == NULL)
5406 err = got_error_from_errno("fclose");
5407 if (f2 && fclose(f2) == EOF && err == NULL)
5408 err = got_error_from_errno("fclose");
5413 static struct got_object_id *
5414 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5415 int first_displayed_line, int selected_line)
5417 struct tog_blame_line *line;
5422 line = &lines[first_displayed_line - 1 + selected_line - 1];
5423 if (!line->annotated)
5429 static struct got_object_id *
5430 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5433 struct tog_blame_line *line;
5435 if (nlines <= 0 || lineno >= nlines)
5438 line = &lines[lineno - 1];
5439 if (!line->annotated)
5445 static const struct got_error *
5446 stop_blame(struct tog_blame *blame)
5448 const struct got_error *err = NULL;
5451 if (blame->thread) {
5453 errcode = pthread_mutex_unlock(&tog_mutex);
5455 return got_error_set_errno(errcode,
5456 "pthread_mutex_unlock");
5457 errcode = pthread_join(blame->thread, (void **)&err);
5459 return got_error_set_errno(errcode, "pthread_join");
5460 errcode = pthread_mutex_lock(&tog_mutex);
5462 return got_error_set_errno(errcode,
5463 "pthread_mutex_lock");
5464 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5466 blame->thread = NULL;
5468 if (blame->thread_args.repo) {
5469 const struct got_error *close_err;
5470 close_err = got_repo_close(blame->thread_args.repo);
5473 blame->thread_args.repo = NULL;
5476 if (fclose(blame->f) == EOF && err == NULL)
5477 err = got_error_from_errno("fclose");
5481 for (i = 0; i < blame->nlines; i++)
5482 free(blame->lines[i].id);
5484 blame->lines = NULL;
5486 free(blame->cb_args.commit_id);
5487 blame->cb_args.commit_id = NULL;
5488 if (blame->pack_fds) {
5489 const struct got_error *pack_err =
5490 got_repo_pack_fds_close(blame->pack_fds);
5493 blame->pack_fds = NULL;
5498 static const struct got_error *
5499 cancel_blame_view(void *arg)
5501 const struct got_error *err = NULL;
5505 errcode = pthread_mutex_lock(&tog_mutex);
5507 return got_error_set_errno(errcode,
5508 "pthread_mutex_unlock");
5511 err = got_error(GOT_ERR_CANCELLED);
5513 errcode = pthread_mutex_unlock(&tog_mutex);
5515 return got_error_set_errno(errcode,
5516 "pthread_mutex_lock");
5521 static const struct got_error *
5522 run_blame(struct tog_view *view)
5524 struct tog_blame_view_state *s = &view->state.blame;
5525 struct tog_blame *blame = &s->blame;
5526 const struct got_error *err = NULL;
5527 struct got_commit_object *commit = NULL;
5528 struct got_blob_object *blob = NULL;
5529 struct got_repository *thread_repo = NULL;
5530 struct got_object_id *obj_id = NULL;
5531 int obj_type, fd = -1;
5532 int *pack_fds = NULL;
5534 err = got_object_open_as_commit(&commit, s->repo,
5535 &s->blamed_commit->id);
5539 fd = got_opentempfd();
5541 err = got_error_from_errno("got_opentempfd");
5545 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5549 err = got_object_get_type(&obj_type, s->repo, obj_id);
5553 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5554 err = got_error(GOT_ERR_OBJ_TYPE);
5558 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5561 blame->f = got_opentemp();
5562 if (blame->f == NULL) {
5563 err = got_error_from_errno("got_opentemp");
5566 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5567 &blame->line_offsets, blame->f, blob);
5570 if (blame->nlines == 0) {
5571 s->blame_complete = 1;
5575 /* Don't include \n at EOF in the blame line count. */
5576 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5579 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5580 if (blame->lines == NULL) {
5581 err = got_error_from_errno("calloc");
5585 err = got_repo_pack_fds_open(&pack_fds);
5588 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5593 blame->pack_fds = pack_fds;
5594 blame->cb_args.view = view;
5595 blame->cb_args.lines = blame->lines;
5596 blame->cb_args.nlines = blame->nlines;
5597 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5598 if (blame->cb_args.commit_id == NULL) {
5599 err = got_error_from_errno("got_object_id_dup");
5602 blame->cb_args.quit = &s->done;
5604 blame->thread_args.path = s->path;
5605 blame->thread_args.repo = thread_repo;
5606 blame->thread_args.cb_args = &blame->cb_args;
5607 blame->thread_args.complete = &s->blame_complete;
5608 blame->thread_args.cancel_cb = cancel_blame_view;
5609 blame->thread_args.cancel_arg = &s->done;
5610 s->blame_complete = 0;
5612 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5613 s->first_displayed_line = 1;
5614 s->last_displayed_line = view->nlines;
5615 s->selected_line = 1;
5617 s->matched_line = 0;
5621 got_object_commit_close(commit);
5622 if (fd != -1 && close(fd) == -1 && err == NULL)
5623 err = got_error_from_errno("close");
5625 got_object_blob_close(blob);
5632 static const struct got_error *
5633 open_blame_view(struct tog_view *view, char *path,
5634 struct got_object_id *commit_id, struct got_repository *repo)
5636 const struct got_error *err = NULL;
5637 struct tog_blame_view_state *s = &view->state.blame;
5639 STAILQ_INIT(&s->blamed_commits);
5641 s->path = strdup(path);
5642 if (s->path == NULL)
5643 return got_error_from_errno("strdup");
5645 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5651 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5652 s->first_displayed_line = 1;
5653 s->last_displayed_line = view->nlines;
5654 s->selected_line = 1;
5655 s->blame_complete = 0;
5657 s->commit_id = commit_id;
5658 memset(&s->blame, 0, sizeof(s->blame));
5660 STAILQ_INIT(&s->colors);
5661 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5662 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5663 get_color_value("TOG_COLOR_COMMIT"));
5668 view->show = show_blame_view;
5669 view->input = input_blame_view;
5670 view->reset = reset_blame_view;
5671 view->close = close_blame_view;
5672 view->search_start = search_start_blame_view;
5673 view->search_next = search_next_blame_view;
5675 return run_blame(view);
5678 static const struct got_error *
5679 close_blame_view(struct tog_view *view)
5681 const struct got_error *err = NULL;
5682 struct tog_blame_view_state *s = &view->state.blame;
5684 if (s->blame.thread)
5685 err = stop_blame(&s->blame);
5687 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5688 struct got_object_qid *blamed_commit;
5689 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5690 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5691 got_object_qid_free(blamed_commit);
5695 free_colors(&s->colors);
5699 static const struct got_error *
5700 search_start_blame_view(struct tog_view *view)
5702 struct tog_blame_view_state *s = &view->state.blame;
5704 s->matched_line = 0;
5708 static const struct got_error *
5709 search_next_blame_view(struct tog_view *view)
5711 struct tog_blame_view_state *s = &view->state.blame;
5712 const struct got_error *err = NULL;
5715 size_t linesize = 0;
5718 if (!view->searching) {
5719 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5723 if (s->matched_line) {
5724 if (view->searching == TOG_SEARCH_FORWARD)
5725 lineno = s->matched_line + 1;
5727 lineno = s->matched_line - 1;
5729 lineno = s->first_displayed_line - 1 + s->selected_line;
5734 if (lineno <= 0 || lineno > s->blame.nlines) {
5735 if (s->matched_line == 0) {
5736 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5740 if (view->searching == TOG_SEARCH_FORWARD)
5743 lineno = s->blame.nlines;
5746 offset = s->blame.line_offsets[lineno - 1];
5747 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5749 return got_error_from_errno("fseeko");
5751 linelen = getline(&line, &linesize, s->blame.f);
5752 if (linelen != -1) {
5754 err = expand_tab(&exstr, line);
5757 if (match_line(exstr, &view->regex, 1,
5759 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5760 s->matched_line = lineno;
5766 if (view->searching == TOG_SEARCH_FORWARD)
5773 if (s->matched_line) {
5774 s->first_displayed_line = s->matched_line;
5775 s->selected_line = 1;
5781 static const struct got_error *
5782 show_blame_view(struct tog_view *view)
5784 const struct got_error *err = NULL;
5785 struct tog_blame_view_state *s = &view->state.blame;
5788 if (s->blame.thread == NULL && !s->blame_complete) {
5789 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5790 &s->blame.thread_args);
5792 return got_error_set_errno(errcode, "pthread_create");
5794 halfdelay(1); /* fast refresh while annotating */
5797 if (s->blame_complete)
5798 halfdelay(10); /* disable fast refresh */
5800 err = draw_blame(view);
5806 static const struct got_error *
5807 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5808 struct got_repository *repo, struct got_object_id *id)
5810 struct tog_view *log_view;
5811 const struct got_error *err = NULL;
5815 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5816 if (log_view == NULL)
5817 return got_error_from_errno("view_open");
5819 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5821 view_close(log_view);
5823 *new_view = log_view;
5828 static const struct got_error *
5829 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5831 const struct got_error *err = NULL, *thread_err = NULL;
5832 struct tog_view *diff_view;
5833 struct tog_blame_view_state *s = &view->state.blame;
5834 int eos, nscroll, begin_y = 0, begin_x = 0;
5836 eos = nscroll = view->nlines - 2;
5837 if (view_is_hsplit_top(view))
5845 view->x = MAX(view->maxx - view->ncols / 3, 0);
5850 if (view->x + view->ncols / 3 < view->maxx)
5851 view->x += 2; /* move two columns right */
5857 view->x -= MIN(view->x, 2); /* move two columns back */
5866 s->selected_line = 1;
5867 s->first_displayed_line = 1;
5872 if (s->blame.nlines < eos) {
5873 s->selected_line = s->blame.nlines;
5874 s->first_displayed_line = 1;
5876 s->selected_line = eos;
5877 s->first_displayed_line = s->blame.nlines - (eos - 1);
5884 if (s->selected_line > 1)
5886 else if (s->selected_line == 1 &&
5887 s->first_displayed_line > 1)
5888 s->first_displayed_line--;
5899 if (s->first_displayed_line == 1) {
5900 if (view->count > 1)
5902 s->selected_line = MAX(1, s->selected_line - nscroll);
5906 if (s->first_displayed_line > nscroll)
5907 s->first_displayed_line -= nscroll;
5909 s->first_displayed_line = 1;
5914 if (s->selected_line < eos && s->first_displayed_line +
5915 s->selected_line <= s->blame.nlines)
5917 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5918 s->first_displayed_line++;
5924 struct got_object_id *id = NULL;
5927 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5928 s->first_displayed_line, s->selected_line);
5932 struct got_commit_object *commit, *pcommit;
5933 struct got_object_qid *pid;
5934 struct got_object_id *blob_id = NULL;
5936 err = got_object_open_as_commit(&commit,
5941 got_object_commit_get_parent_ids(commit));
5943 got_object_commit_close(commit);
5946 /* Check if path history ends here. */
5947 err = got_object_open_as_commit(&pcommit,
5951 err = got_object_id_by_path(&blob_id, s->repo,
5953 got_object_commit_close(pcommit);
5955 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5957 got_object_commit_close(commit);
5960 err = got_object_get_type(&obj_type, s->repo,
5963 /* Can't blame non-blob type objects. */
5964 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5965 got_object_commit_close(commit);
5968 err = got_object_qid_alloc(&s->blamed_commit,
5970 got_object_commit_close(commit);
5972 if (got_object_id_cmp(id,
5973 &s->blamed_commit->id) == 0)
5975 err = got_object_qid_alloc(&s->blamed_commit,
5981 thread_err = stop_blame(&s->blame);
5985 STAILQ_INSERT_HEAD(&s->blamed_commits,
5986 s->blamed_commit, entry);
5987 err = run_blame(view);
5993 struct got_object_qid *first;
5996 first = STAILQ_FIRST(&s->blamed_commits);
5997 if (!got_object_id_cmp(&first->id, s->commit_id))
6000 thread_err = stop_blame(&s->blame);
6004 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6005 got_object_qid_free(s->blamed_commit);
6007 STAILQ_FIRST(&s->blamed_commits);
6008 err = run_blame(view);
6015 s->id_to_log = get_selected_commit_id(s->blame.lines,
6016 s->blame.nlines, s->first_displayed_line, s->selected_line);
6018 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6022 struct got_object_id *id = NULL;
6023 struct got_object_qid *pid;
6024 struct got_commit_object *commit = NULL;
6027 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6028 s->first_displayed_line, s->selected_line);
6031 err = got_object_open_as_commit(&commit, s->repo, id);
6034 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6036 /* traversed from diff view, release diff resources */
6037 err = close_diff_view(*new_view);
6040 diff_view = *new_view;
6042 if (view_is_parent_view(view))
6043 view_get_split(view, &begin_y, &begin_x);
6045 diff_view = view_open(0, 0, begin_y, begin_x,
6047 if (diff_view == NULL) {
6048 got_object_commit_close(commit);
6049 err = got_error_from_errno("view_open");
6053 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6054 id, NULL, NULL, 3, 0, 0, view, s->repo);
6055 got_object_commit_close(commit);
6057 view_close(diff_view);
6060 s->last_diffed_line = s->first_displayed_line - 1 +
6063 break; /* still open from active diff view */
6064 if (view_is_parent_view(view) &&
6065 view->mode == TOG_VIEW_SPLIT_HRZN) {
6066 err = view_init_hsplit(view, begin_y);
6072 diff_view->focussed = 1;
6073 diff_view->mode = view->mode;
6074 diff_view->nlines = view->lines - begin_y;
6075 if (view_is_parent_view(view)) {
6076 view_transfer_size(diff_view, view);
6077 err = view_close_child(view);
6080 err = view_set_child(view, diff_view);
6083 view->focus_child = 1;
6085 *new_view = diff_view;
6098 if (s->last_displayed_line >= s->blame.nlines &&
6099 s->selected_line >= MIN(s->blame.nlines,
6100 view->nlines - 2)) {
6104 if (s->last_displayed_line >= s->blame.nlines &&
6105 s->selected_line < view->nlines - 2) {
6107 MIN(nscroll, s->last_displayed_line -
6108 s->first_displayed_line - s->selected_line + 1);
6110 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6111 s->first_displayed_line += nscroll;
6113 s->first_displayed_line =
6114 s->blame.nlines - (view->nlines - 3);
6117 if (s->selected_line > view->nlines - 2) {
6118 s->selected_line = MIN(s->blame.nlines,
6126 return thread_err ? thread_err : err;
6129 static const struct got_error *
6130 reset_blame_view(struct tog_view *view)
6132 const struct got_error *err;
6133 struct tog_blame_view_state *s = &view->state.blame;
6137 err = stop_blame(&s->blame);
6141 return run_blame(view);
6144 static const struct got_error *
6145 cmd_blame(int argc, char *argv[])
6147 const struct got_error *error;
6148 struct got_repository *repo = NULL;
6149 struct got_worktree *worktree = NULL;
6150 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6151 char *link_target = NULL;
6152 struct got_object_id *commit_id = NULL;
6153 struct got_commit_object *commit = NULL;
6154 char *commit_id_str = NULL;
6156 struct tog_view *view;
6157 int *pack_fds = NULL;
6159 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6162 commit_id_str = optarg;
6165 repo_path = realpath(optarg, NULL);
6166 if (repo_path == NULL)
6167 return got_error_from_errno2("realpath",
6182 error = got_repo_pack_fds_open(&pack_fds);
6186 if (repo_path == NULL) {
6187 cwd = getcwd(NULL, 0);
6189 return got_error_from_errno("getcwd");
6190 error = got_worktree_open(&worktree, cwd);
6191 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6195 strdup(got_worktree_get_repo_path(worktree));
6197 repo_path = strdup(cwd);
6198 if (repo_path == NULL) {
6199 error = got_error_from_errno("strdup");
6204 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6208 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6215 error = apply_unveil(got_repo_get_path(repo), NULL);
6219 error = tog_load_refs(repo, 0);
6223 if (commit_id_str == NULL) {
6224 struct got_reference *head_ref;
6225 error = got_ref_open(&head_ref, repo, worktree ?
6226 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6229 error = got_ref_resolve(&commit_id, repo, head_ref);
6230 got_ref_close(head_ref);
6232 error = got_repo_match_object_id(&commit_id, NULL,
6233 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6238 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6240 error = got_error_from_errno("view_open");
6244 error = got_object_open_as_commit(&commit, repo, commit_id);
6248 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6253 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6258 /* Release work tree lock. */
6259 got_worktree_close(worktree);
6262 error = view_loop(view);
6270 got_object_commit_close(commit);
6272 got_worktree_close(worktree);
6274 const struct got_error *close_err = got_repo_close(repo);
6279 const struct got_error *pack_err =
6280 got_repo_pack_fds_close(pack_fds);
6288 static const struct got_error *
6289 draw_tree_entries(struct tog_view *view, const char *parent_path)
6291 struct tog_tree_view_state *s = &view->state.tree;
6292 const struct got_error *err = NULL;
6293 struct got_tree_entry *te;
6295 struct tog_color *tc;
6296 int width, n, nentries, i = 1;
6297 int limit = view->nlines;
6300 if (view_is_hsplit_top(view))
6301 --limit; /* border */
6303 werase(view->window);
6308 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6312 if (view_needs_focus_indication(view))
6313 wstandout(view->window);
6314 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6316 wattr_on(view->window,
6317 COLOR_PAIR(tc->colorpair), NULL);
6318 waddwstr(view->window, wline);
6320 wattr_off(view->window,
6321 COLOR_PAIR(tc->colorpair), NULL);
6322 if (view_needs_focus_indication(view))
6323 wstandend(view->window);
6327 if (s->selected_entry) {
6328 i = got_tree_entry_get_index(s->selected_entry);
6329 i += s->tree == s->root ? 1 : 2; /* account for ".." entry */
6331 nentries = got_object_tree_get_nentries(s->tree);
6332 wprintw(view->window, " [%d/%d]", i,
6333 nentries + (s->tree == s->root ? 0 : 1)); /* ".." in !root tree */
6335 if (width < view->ncols - 1)
6336 waddch(view->window, '\n');
6339 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6343 waddwstr(view->window, wline);
6346 if (width < view->ncols - 1)
6347 waddch(view->window, '\n');
6350 waddch(view->window, '\n');
6354 if (s->first_displayed_entry == NULL) {
6355 te = got_object_tree_get_first_entry(s->tree);
6356 if (s->selected == 0) {
6358 wstandout(view->window);
6359 s->selected_entry = NULL;
6361 waddstr(view->window, " ..\n"); /* parent directory */
6362 if (s->selected == 0 && view->focussed)
6363 wstandend(view->window);
6370 te = s->first_displayed_entry;
6373 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6374 char *line = NULL, *id_str = NULL, *link_target = NULL;
6375 const char *modestr = "";
6378 te = got_object_tree_get_entry(s->tree, i);
6379 mode = got_tree_entry_get_mode(te);
6382 err = got_object_id_str(&id_str,
6383 got_tree_entry_get_id(te));
6385 return got_error_from_errno(
6386 "got_object_id_str");
6388 if (got_object_tree_entry_is_submodule(te))
6390 else if (S_ISLNK(mode)) {
6393 err = got_tree_entry_get_symlink_target(&link_target,
6399 for (i = 0; i < strlen(link_target); i++) {
6400 if (!isprint((unsigned char)link_target[i]))
6401 link_target[i] = '?';
6405 else if (S_ISDIR(mode))
6407 else if (mode & S_IXUSR)
6409 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6410 got_tree_entry_get_name(te), modestr,
6411 link_target ? " -> ": "",
6412 link_target ? link_target : "") == -1) {
6415 return got_error_from_errno("asprintf");
6419 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6425 if (n == s->selected) {
6427 wstandout(view->window);
6428 s->selected_entry = te;
6430 tc = match_color(&s->colors, line);
6432 wattr_on(view->window,
6433 COLOR_PAIR(tc->colorpair), NULL);
6434 waddwstr(view->window, wline);
6436 wattr_off(view->window,
6437 COLOR_PAIR(tc->colorpair), NULL);
6438 if (width < view->ncols - 1)
6439 waddch(view->window, '\n');
6440 if (n == s->selected && view->focussed)
6441 wstandend(view->window);
6447 s->last_displayed_entry = te;
6456 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6458 struct got_tree_entry *te;
6459 int isroot = s->tree == s->root;
6462 if (s->first_displayed_entry == NULL)
6465 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6466 while (i++ < maxscroll) {
6469 s->first_displayed_entry = NULL;
6472 s->first_displayed_entry = te;
6473 te = got_tree_entry_get_prev(s->tree, te);
6477 static const struct got_error *
6478 tree_scroll_down(struct tog_view *view, int maxscroll)
6480 struct tog_tree_view_state *s = &view->state.tree;
6481 struct got_tree_entry *next, *last;
6484 if (s->first_displayed_entry)
6485 next = got_tree_entry_get_next(s->tree,
6486 s->first_displayed_entry);
6488 next = got_object_tree_get_first_entry(s->tree);
6490 last = s->last_displayed_entry;
6491 while (next && n++ < maxscroll) {
6493 s->last_displayed_entry = last;
6494 last = got_tree_entry_get_next(s->tree, last);
6496 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6497 s->first_displayed_entry = next;
6498 next = got_tree_entry_get_next(s->tree, next);
6505 static const struct got_error *
6506 tree_entry_path(char **path, struct tog_parent_trees *parents,
6507 struct got_tree_entry *te)
6509 const struct got_error *err = NULL;
6510 struct tog_parent_tree *pt;
6511 size_t len = 2; /* for leading slash and NUL */
6513 TAILQ_FOREACH(pt, parents, entry)
6514 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6517 len += strlen(got_tree_entry_get_name(te));
6519 *path = calloc(1, len);
6521 return got_error_from_errno("calloc");
6524 pt = TAILQ_LAST(parents, tog_parent_trees);
6526 const char *name = got_tree_entry_get_name(pt->selected_entry);
6527 if (strlcat(*path, name, len) >= len) {
6528 err = got_error(GOT_ERR_NO_SPACE);
6531 if (strlcat(*path, "/", len) >= len) {
6532 err = got_error(GOT_ERR_NO_SPACE);
6535 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6538 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6539 err = got_error(GOT_ERR_NO_SPACE);
6551 static const struct got_error *
6552 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6553 struct got_tree_entry *te, struct tog_parent_trees *parents,
6554 struct got_object_id *commit_id, struct got_repository *repo)
6556 const struct got_error *err = NULL;
6558 struct tog_view *blame_view;
6562 err = tree_entry_path(&path, parents, te);
6566 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6567 if (blame_view == NULL) {
6568 err = got_error_from_errno("view_open");
6572 err = open_blame_view(blame_view, path, commit_id, repo);
6574 if (err->code == GOT_ERR_CANCELLED)
6576 view_close(blame_view);
6578 *new_view = blame_view;
6584 static const struct got_error *
6585 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6586 struct tog_tree_view_state *s)
6588 struct tog_view *log_view;
6589 const struct got_error *err = NULL;
6594 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6595 if (log_view == NULL)
6596 return got_error_from_errno("view_open");
6598 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6602 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6605 view_close(log_view);
6607 *new_view = log_view;
6612 static const struct got_error *
6613 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6614 const char *head_ref_name, struct got_repository *repo)
6616 const struct got_error *err = NULL;
6617 char *commit_id_str = NULL;
6618 struct tog_tree_view_state *s = &view->state.tree;
6619 struct got_commit_object *commit = NULL;
6621 TAILQ_INIT(&s->parents);
6622 STAILQ_INIT(&s->colors);
6624 s->commit_id = got_object_id_dup(commit_id);
6625 if (s->commit_id == NULL)
6626 return got_error_from_errno("got_object_id_dup");
6628 err = got_object_open_as_commit(&commit, repo, commit_id);
6633 * The root is opened here and will be closed when the view is closed.
6634 * Any visited subtrees and their path-wise parents are opened and
6637 err = got_object_open_as_tree(&s->root, repo,
6638 got_object_commit_get_tree_id(commit));
6643 err = got_object_id_str(&commit_id_str, commit_id);
6647 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6648 err = got_error_from_errno("asprintf");
6652 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6653 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6654 if (head_ref_name) {
6655 s->head_ref_name = strdup(head_ref_name);
6656 if (s->head_ref_name == NULL) {
6657 err = got_error_from_errno("strdup");
6663 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6664 err = add_color(&s->colors, "\\$$",
6665 TOG_COLOR_TREE_SUBMODULE,
6666 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6669 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6670 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6673 err = add_color(&s->colors, "/$",
6674 TOG_COLOR_TREE_DIRECTORY,
6675 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6679 err = add_color(&s->colors, "\\*$",
6680 TOG_COLOR_TREE_EXECUTABLE,
6681 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6685 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6686 get_color_value("TOG_COLOR_COMMIT"));
6691 view->show = show_tree_view;
6692 view->input = input_tree_view;
6693 view->close = close_tree_view;
6694 view->search_start = search_start_tree_view;
6695 view->search_next = search_next_tree_view;
6697 free(commit_id_str);
6699 got_object_commit_close(commit);
6701 close_tree_view(view);
6705 static const struct got_error *
6706 close_tree_view(struct tog_view *view)
6708 struct tog_tree_view_state *s = &view->state.tree;
6710 free_colors(&s->colors);
6711 free(s->tree_label);
6712 s->tree_label = NULL;
6714 s->commit_id = NULL;
6715 free(s->head_ref_name);
6716 s->head_ref_name = NULL;
6717 while (!TAILQ_EMPTY(&s->parents)) {
6718 struct tog_parent_tree *parent;
6719 parent = TAILQ_FIRST(&s->parents);
6720 TAILQ_REMOVE(&s->parents, parent, entry);
6721 if (parent->tree != s->root)
6722 got_object_tree_close(parent->tree);
6726 if (s->tree != NULL && s->tree != s->root)
6727 got_object_tree_close(s->tree);
6729 got_object_tree_close(s->root);
6733 static const struct got_error *
6734 search_start_tree_view(struct tog_view *view)
6736 struct tog_tree_view_state *s = &view->state.tree;
6738 s->matched_entry = NULL;
6743 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6745 regmatch_t regmatch;
6747 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6751 static const struct got_error *
6752 search_next_tree_view(struct tog_view *view)
6754 struct tog_tree_view_state *s = &view->state.tree;
6755 struct got_tree_entry *te = NULL;
6757 if (!view->searching) {
6758 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6762 if (s->matched_entry) {
6763 if (view->searching == TOG_SEARCH_FORWARD) {
6764 if (s->selected_entry)
6765 te = got_tree_entry_get_next(s->tree,
6768 te = got_object_tree_get_first_entry(s->tree);
6770 if (s->selected_entry == NULL)
6771 te = got_object_tree_get_last_entry(s->tree);
6773 te = got_tree_entry_get_prev(s->tree,
6777 if (s->selected_entry)
6778 te = s->selected_entry;
6779 else if (view->searching == TOG_SEARCH_FORWARD)
6780 te = got_object_tree_get_first_entry(s->tree);
6782 te = got_object_tree_get_last_entry(s->tree);
6787 if (s->matched_entry == NULL) {
6788 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6791 if (view->searching == TOG_SEARCH_FORWARD)
6792 te = got_object_tree_get_first_entry(s->tree);
6794 te = got_object_tree_get_last_entry(s->tree);
6797 if (match_tree_entry(te, &view->regex)) {
6798 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6799 s->matched_entry = te;
6803 if (view->searching == TOG_SEARCH_FORWARD)
6804 te = got_tree_entry_get_next(s->tree, te);
6806 te = got_tree_entry_get_prev(s->tree, te);
6809 if (s->matched_entry) {
6810 s->first_displayed_entry = s->matched_entry;
6817 static const struct got_error *
6818 show_tree_view(struct tog_view *view)
6820 const struct got_error *err = NULL;
6821 struct tog_tree_view_state *s = &view->state.tree;
6824 err = tree_entry_path(&parent_path, &s->parents, NULL);
6828 err = draw_tree_entries(view, parent_path);
6835 static const struct got_error *
6836 tree_goto_line(struct tog_view *view, int nlines)
6838 const struct got_error *err = NULL;
6839 struct tog_tree_view_state *s = &view->state.tree;
6840 struct got_tree_entry **fte, **lte, **ste;
6841 int g, last, first = 1, i = 1;
6842 int root = s->tree == s->root;
6843 int off = root ? 1 : 2;
6850 else if (g > got_object_tree_get_nentries(s->tree))
6851 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6853 fte = &s->first_displayed_entry;
6854 lte = &s->last_displayed_entry;
6855 ste = &s->selected_entry;
6858 first = got_tree_entry_get_index(*fte);
6859 first += off; /* account for ".." */
6861 last = got_tree_entry_get_index(*lte);
6864 if (g >= first && g <= last && g - first < nlines) {
6865 s->selected = g - first;
6866 return NULL; /* gline is on the current page */
6870 i = got_tree_entry_get_index(*ste);
6875 err = tree_scroll_down(view, g - i);
6878 if (got_tree_entry_get_index(*lte) >=
6879 got_object_tree_get_nentries(s->tree) - 1 &&
6880 first + s->selected < g &&
6881 s->selected < s->ndisplayed - 1) {
6882 first = got_tree_entry_get_index(*fte);
6884 s->selected = g - first;
6887 tree_scroll_up(s, i - g);
6890 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6891 s->selected = g - 1;
6896 static const struct got_error *
6897 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6899 const struct got_error *err = NULL;
6900 struct tog_tree_view_state *s = &view->state.tree;
6901 struct got_tree_entry *te;
6902 int n, nscroll = view->nlines - 3;
6905 return tree_goto_line(view, nscroll);
6909 s->show_ids = !s->show_ids;
6914 if (!s->selected_entry)
6916 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6920 err = view_request_new(new_view, view, TOG_VIEW_REF);
6926 if (s->tree == s->root)
6927 s->first_displayed_entry =
6928 got_object_tree_get_first_entry(s->tree);
6930 s->first_displayed_entry = NULL;
6934 int eos = view->nlines - 3;
6936 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6940 te = got_object_tree_get_last_entry(s->tree);
6941 for (n = 0; n < eos; n++) {
6943 if (s->tree != s->root) {
6944 s->first_displayed_entry = NULL;
6949 s->first_displayed_entry = te;
6950 te = got_tree_entry_get_prev(s->tree, te);
6953 s->selected = n - 1;
6959 if (s->selected > 0) {
6963 tree_scroll_up(s, 1);
6964 if (s->selected_entry == NULL ||
6965 (s->tree == s->root && s->selected_entry ==
6966 got_object_tree_get_first_entry(s->tree)))
6976 if (s->tree == s->root) {
6977 if (got_object_tree_get_first_entry(s->tree) ==
6978 s->first_displayed_entry)
6979 s->selected -= MIN(s->selected, nscroll);
6981 if (s->first_displayed_entry == NULL)
6982 s->selected -= MIN(s->selected, nscroll);
6984 tree_scroll_up(s, MAX(0, nscroll));
6985 if (s->selected_entry == NULL ||
6986 (s->tree == s->root && s->selected_entry ==
6987 got_object_tree_get_first_entry(s->tree)))
6993 if (s->selected < s->ndisplayed - 1) {
6997 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6999 /* can't scroll any further */
7003 tree_scroll_down(view, 1);
7013 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7015 /* can't scroll any further; move cursor down */
7016 if (s->selected < s->ndisplayed - 1)
7017 s->selected += MIN(nscroll,
7018 s->ndisplayed - s->selected - 1);
7023 tree_scroll_down(view, nscroll);
7028 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7029 struct tog_parent_tree *parent;
7030 /* user selected '..' */
7031 if (s->tree == s->root) {
7035 parent = TAILQ_FIRST(&s->parents);
7036 TAILQ_REMOVE(&s->parents, parent,
7038 got_object_tree_close(s->tree);
7039 s->tree = parent->tree;
7040 s->first_displayed_entry =
7041 parent->first_displayed_entry;
7043 parent->selected_entry;
7044 s->selected = parent->selected;
7045 if (s->selected > view->nlines - 3) {
7046 err = offset_selection_down(view);
7051 } else if (S_ISDIR(got_tree_entry_get_mode(
7052 s->selected_entry))) {
7053 struct got_tree_object *subtree;
7055 err = got_object_open_as_tree(&subtree, s->repo,
7056 got_tree_entry_get_id(s->selected_entry));
7059 err = tree_view_visit_subtree(s, subtree);
7061 got_object_tree_close(subtree);
7064 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7065 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7068 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7069 s->selected = view->nlines - 4;
7085 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7090 static const struct got_error *
7091 cmd_tree(int argc, char *argv[])
7093 const struct got_error *error;
7094 struct got_repository *repo = NULL;
7095 struct got_worktree *worktree = NULL;
7096 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7097 struct got_object_id *commit_id = NULL;
7098 struct got_commit_object *commit = NULL;
7099 const char *commit_id_arg = NULL;
7101 struct got_reference *ref = NULL;
7102 const char *head_ref_name = NULL;
7104 struct tog_view *view;
7105 int *pack_fds = NULL;
7107 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7110 commit_id_arg = optarg;
7113 repo_path = realpath(optarg, NULL);
7114 if (repo_path == NULL)
7115 return got_error_from_errno2("realpath",
7130 error = got_repo_pack_fds_open(&pack_fds);
7134 if (repo_path == NULL) {
7135 cwd = getcwd(NULL, 0);
7137 return got_error_from_errno("getcwd");
7138 error = got_worktree_open(&worktree, cwd);
7139 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7143 strdup(got_worktree_get_repo_path(worktree));
7145 repo_path = strdup(cwd);
7146 if (repo_path == NULL) {
7147 error = got_error_from_errno("strdup");
7152 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7156 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7163 error = apply_unveil(got_repo_get_path(repo), NULL);
7167 error = tog_load_refs(repo, 0);
7171 if (commit_id_arg == NULL) {
7172 error = got_repo_match_object_id(&commit_id, &label,
7173 worktree ? got_worktree_get_head_ref_name(worktree) :
7174 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7177 head_ref_name = label;
7179 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7181 head_ref_name = got_ref_get_name(ref);
7182 else if (error->code != GOT_ERR_NOT_REF)
7184 error = got_repo_match_object_id(&commit_id, NULL,
7185 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7190 error = got_object_open_as_commit(&commit, repo, commit_id);
7194 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7196 error = got_error_from_errno("view_open");
7199 error = open_tree_view(view, commit_id, head_ref_name, repo);
7202 if (!got_path_is_root_dir(in_repo_path)) {
7203 error = tree_view_walk_path(&view->state.tree, commit,
7210 /* Release work tree lock. */
7211 got_worktree_close(worktree);
7214 error = view_loop(view);
7223 const struct got_error *close_err = got_repo_close(repo);
7228 const struct got_error *pack_err =
7229 got_repo_pack_fds_close(pack_fds);
7237 static const struct got_error *
7238 ref_view_load_refs(struct tog_ref_view_state *s)
7240 struct got_reflist_entry *sre;
7241 struct tog_reflist_entry *re;
7244 TAILQ_FOREACH(sre, &tog_refs, entry) {
7245 if (strncmp(got_ref_get_name(sre->ref),
7246 "refs/got/", 9) == 0 &&
7247 strncmp(got_ref_get_name(sre->ref),
7248 "refs/got/backup/", 16) != 0)
7251 re = malloc(sizeof(*re));
7253 return got_error_from_errno("malloc");
7255 re->ref = got_ref_dup(sre->ref);
7256 if (re->ref == NULL)
7257 return got_error_from_errno("got_ref_dup");
7258 re->idx = s->nrefs++;
7259 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7262 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7267 ref_view_free_refs(struct tog_ref_view_state *s)
7269 struct tog_reflist_entry *re;
7271 while (!TAILQ_EMPTY(&s->refs)) {
7272 re = TAILQ_FIRST(&s->refs);
7273 TAILQ_REMOVE(&s->refs, re, entry);
7274 got_ref_close(re->ref);
7279 static const struct got_error *
7280 open_ref_view(struct tog_view *view, struct got_repository *repo)
7282 const struct got_error *err = NULL;
7283 struct tog_ref_view_state *s = &view->state.ref;
7285 s->selected_entry = 0;
7288 TAILQ_INIT(&s->refs);
7289 STAILQ_INIT(&s->colors);
7291 err = ref_view_load_refs(s);
7295 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7296 err = add_color(&s->colors, "^refs/heads/",
7297 TOG_COLOR_REFS_HEADS,
7298 get_color_value("TOG_COLOR_REFS_HEADS"));
7302 err = add_color(&s->colors, "^refs/tags/",
7303 TOG_COLOR_REFS_TAGS,
7304 get_color_value("TOG_COLOR_REFS_TAGS"));
7308 err = add_color(&s->colors, "^refs/remotes/",
7309 TOG_COLOR_REFS_REMOTES,
7310 get_color_value("TOG_COLOR_REFS_REMOTES"));
7314 err = add_color(&s->colors, "^refs/got/backup/",
7315 TOG_COLOR_REFS_BACKUP,
7316 get_color_value("TOG_COLOR_REFS_BACKUP"));
7321 view->show = show_ref_view;
7322 view->input = input_ref_view;
7323 view->close = close_ref_view;
7324 view->search_start = search_start_ref_view;
7325 view->search_next = search_next_ref_view;
7328 free_colors(&s->colors);
7332 static const struct got_error *
7333 close_ref_view(struct tog_view *view)
7335 struct tog_ref_view_state *s = &view->state.ref;
7337 ref_view_free_refs(s);
7338 free_colors(&s->colors);
7343 static const struct got_error *
7344 resolve_reflist_entry(struct got_object_id **commit_id,
7345 struct tog_reflist_entry *re, struct got_repository *repo)
7347 const struct got_error *err = NULL;
7348 struct got_object_id *obj_id;
7349 struct got_tag_object *tag = NULL;
7354 err = got_ref_resolve(&obj_id, repo, re->ref);
7358 err = got_object_get_type(&obj_type, repo, obj_id);
7363 case GOT_OBJ_TYPE_COMMIT:
7364 *commit_id = obj_id;
7366 case GOT_OBJ_TYPE_TAG:
7367 err = got_object_open_as_tag(&tag, repo, obj_id);
7371 err = got_object_get_type(&obj_type, repo,
7372 got_object_tag_get_object_id(tag));
7375 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7376 err = got_error(GOT_ERR_OBJ_TYPE);
7379 *commit_id = got_object_id_dup(
7380 got_object_tag_get_object_id(tag));
7381 if (*commit_id == NULL) {
7382 err = got_error_from_errno("got_object_id_dup");
7387 err = got_error(GOT_ERR_OBJ_TYPE);
7393 got_object_tag_close(tag);
7401 static const struct got_error *
7402 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7403 struct tog_reflist_entry *re, struct got_repository *repo)
7405 struct tog_view *log_view;
7406 const struct got_error *err = NULL;
7407 struct got_object_id *commit_id = NULL;
7411 err = resolve_reflist_entry(&commit_id, re, repo);
7413 if (err->code != GOT_ERR_OBJ_TYPE)
7419 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7420 if (log_view == NULL) {
7421 err = got_error_from_errno("view_open");
7425 err = open_log_view(log_view, commit_id, repo,
7426 got_ref_get_name(re->ref), "", 0);
7429 view_close(log_view);
7431 *new_view = log_view;
7437 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7439 struct tog_reflist_entry *re;
7442 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7445 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7446 while (i++ < maxscroll) {
7449 s->first_displayed_entry = re;
7450 re = TAILQ_PREV(re, tog_reflist_head, entry);
7454 static const struct got_error *
7455 ref_scroll_down(struct tog_view *view, int maxscroll)
7457 struct tog_ref_view_state *s = &view->state.ref;
7458 struct tog_reflist_entry *next, *last;
7461 if (s->first_displayed_entry)
7462 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7464 next = TAILQ_FIRST(&s->refs);
7466 last = s->last_displayed_entry;
7467 while (next && n++ < maxscroll) {
7469 s->last_displayed_entry = last;
7470 last = TAILQ_NEXT(last, entry);
7472 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7473 s->first_displayed_entry = next;
7474 next = TAILQ_NEXT(next, entry);
7481 static const struct got_error *
7482 search_start_ref_view(struct tog_view *view)
7484 struct tog_ref_view_state *s = &view->state.ref;
7486 s->matched_entry = NULL;
7491 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7493 regmatch_t regmatch;
7495 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7499 static const struct got_error *
7500 search_next_ref_view(struct tog_view *view)
7502 struct tog_ref_view_state *s = &view->state.ref;
7503 struct tog_reflist_entry *re = NULL;
7505 if (!view->searching) {
7506 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7510 if (s->matched_entry) {
7511 if (view->searching == TOG_SEARCH_FORWARD) {
7512 if (s->selected_entry)
7513 re = TAILQ_NEXT(s->selected_entry, entry);
7515 re = TAILQ_PREV(s->selected_entry,
7516 tog_reflist_head, entry);
7518 if (s->selected_entry == NULL)
7519 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7521 re = TAILQ_PREV(s->selected_entry,
7522 tog_reflist_head, entry);
7525 if (s->selected_entry)
7526 re = s->selected_entry;
7527 else if (view->searching == TOG_SEARCH_FORWARD)
7528 re = TAILQ_FIRST(&s->refs);
7530 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7535 if (s->matched_entry == NULL) {
7536 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7539 if (view->searching == TOG_SEARCH_FORWARD)
7540 re = TAILQ_FIRST(&s->refs);
7542 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7545 if (match_reflist_entry(re, &view->regex)) {
7546 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7547 s->matched_entry = re;
7551 if (view->searching == TOG_SEARCH_FORWARD)
7552 re = TAILQ_NEXT(re, entry);
7554 re = TAILQ_PREV(re, tog_reflist_head, entry);
7557 if (s->matched_entry) {
7558 s->first_displayed_entry = s->matched_entry;
7565 static const struct got_error *
7566 show_ref_view(struct tog_view *view)
7568 const struct got_error *err = NULL;
7569 struct tog_ref_view_state *s = &view->state.ref;
7570 struct tog_reflist_entry *re;
7573 struct tog_color *tc;
7575 int limit = view->nlines;
7577 werase(view->window);
7580 if (view_is_hsplit_top(view))
7581 --limit; /* border */
7586 re = s->first_displayed_entry;
7588 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7590 return got_error_from_errno("asprintf");
7592 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7597 if (view_needs_focus_indication(view))
7598 wstandout(view->window);
7599 waddwstr(view->window, wline);
7600 if (view_needs_focus_indication(view))
7601 wstandend(view->window);
7606 if (width < view->ncols - 1)
7607 waddch(view->window, '\n');
7612 while (re && limit > 0) {
7614 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7617 struct got_commit_object *ci;
7618 struct got_tag_object *tag;
7619 struct got_object_id *id;
7623 err = got_ref_resolve(&id, s->repo, re->ref);
7626 err = got_object_open_as_tag(&tag, s->repo, id);
7628 if (err->code != GOT_ERR_OBJ_TYPE) {
7632 err = got_object_open_as_commit(&ci, s->repo,
7638 t = got_object_commit_get_committer_time(ci);
7639 got_object_commit_close(ci);
7641 t = got_object_tag_get_tagger_time(tag);
7642 got_object_tag_close(tag);
7645 if (gmtime_r(&t, &tm) == NULL)
7646 return got_error_from_errno("gmtime_r");
7647 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7648 return got_error(GOT_ERR_NO_SPACE);
7650 if (got_ref_is_symbolic(re->ref)) {
7651 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7652 ymd : "", got_ref_get_name(re->ref),
7653 got_ref_get_symref_target(re->ref)) == -1)
7654 return got_error_from_errno("asprintf");
7655 } else if (s->show_ids) {
7656 struct got_object_id *id;
7658 err = got_ref_resolve(&id, s->repo, re->ref);
7661 err = got_object_id_str(&id_str, id);
7666 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7667 got_ref_get_name(re->ref), id_str) == -1) {
7668 err = got_error_from_errno("asprintf");
7675 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7676 got_ref_get_name(re->ref)) == -1)
7677 return got_error_from_errno("asprintf");
7679 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7685 if (n == s->selected) {
7687 wstandout(view->window);
7688 s->selected_entry = re;
7690 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7692 wattr_on(view->window,
7693 COLOR_PAIR(tc->colorpair), NULL);
7694 waddwstr(view->window, wline);
7696 wattr_off(view->window,
7697 COLOR_PAIR(tc->colorpair), NULL);
7698 if (width < view->ncols - 1)
7699 waddch(view->window, '\n');
7700 if (n == s->selected && view->focussed)
7701 wstandend(view->window);
7707 s->last_displayed_entry = re;
7710 re = TAILQ_NEXT(re, entry);
7717 static const struct got_error *
7718 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7719 struct tog_reflist_entry *re, struct got_repository *repo)
7721 const struct got_error *err = NULL;
7722 struct got_object_id *commit_id = NULL;
7723 struct tog_view *tree_view;
7727 err = resolve_reflist_entry(&commit_id, re, repo);
7729 if (err->code != GOT_ERR_OBJ_TYPE)
7736 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7737 if (tree_view == NULL) {
7738 err = got_error_from_errno("view_open");
7742 err = open_tree_view(tree_view, commit_id,
7743 got_ref_get_name(re->ref), repo);
7747 *new_view = tree_view;
7753 static const struct got_error *
7754 ref_goto_line(struct tog_view *view, int nlines)
7756 const struct got_error *err = NULL;
7757 struct tog_ref_view_state *s = &view->state.ref;
7758 int g, idx = s->selected_entry->idx;
7765 else if (g > s->nrefs)
7768 if (g >= s->first_displayed_entry->idx + 1 &&
7769 g <= s->last_displayed_entry->idx + 1 &&
7770 g - s->first_displayed_entry->idx - 1 < nlines) {
7771 s->selected = g - s->first_displayed_entry->idx - 1;
7776 err = ref_scroll_down(view, g - idx - 1);
7779 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7780 s->first_displayed_entry->idx + s->selected < g &&
7781 s->selected < s->ndisplayed - 1)
7782 s->selected = g - s->first_displayed_entry->idx - 1;
7783 } else if (idx + 1 > g)
7784 ref_scroll_up(s, idx - g + 1);
7786 if (g < nlines && s->first_displayed_entry->idx == 0)
7787 s->selected = g - 1;
7793 static const struct got_error *
7794 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7796 const struct got_error *err = NULL;
7797 struct tog_ref_view_state *s = &view->state.ref;
7798 struct tog_reflist_entry *re;
7799 int n, nscroll = view->nlines - 1;
7802 return ref_goto_line(view, nscroll);
7806 s->show_ids = !s->show_ids;
7810 s->show_date = !s->show_date;
7814 s->sort_by_date = !s->sort_by_date;
7816 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7817 got_ref_cmp_by_commit_timestamp_descending :
7818 tog_ref_cmp_by_name, s->repo);
7821 got_reflist_object_id_map_free(tog_refs_idmap);
7822 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7823 &tog_refs, s->repo);
7826 ref_view_free_refs(s);
7827 err = ref_view_load_refs(s);
7832 if (!s->selected_entry)
7834 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7838 if (!s->selected_entry)
7840 err = view_request_new(new_view, view, TOG_VIEW_TREE);
7846 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7850 int eos = view->nlines - 1;
7852 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7856 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7857 for (n = 0; n < eos; n++) {
7860 s->first_displayed_entry = re;
7861 re = TAILQ_PREV(re, tog_reflist_head, entry);
7864 s->selected = n - 1;
7870 if (s->selected > 0) {
7874 ref_scroll_up(s, 1);
7875 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7885 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7886 s->selected -= MIN(nscroll, s->selected);
7887 ref_scroll_up(s, MAX(0, nscroll));
7888 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7894 if (s->selected < s->ndisplayed - 1) {
7898 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7899 /* can't scroll any further */
7903 ref_scroll_down(view, 1);
7913 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7914 /* can't scroll any further; move cursor down */
7915 if (s->selected < s->ndisplayed - 1)
7916 s->selected += MIN(nscroll,
7917 s->ndisplayed - s->selected - 1);
7918 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7919 s->selected += s->ndisplayed - s->selected - 1;
7923 ref_scroll_down(view, nscroll);
7928 err = tog_load_refs(s->repo, s->sort_by_date);
7931 ref_view_free_refs(s);
7932 err = ref_view_load_refs(s);
7935 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7936 s->selected = view->nlines - 2;
7950 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7955 static const struct got_error *
7956 cmd_ref(int argc, char *argv[])
7958 const struct got_error *error;
7959 struct got_repository *repo = NULL;
7960 struct got_worktree *worktree = NULL;
7961 char *cwd = NULL, *repo_path = NULL;
7963 struct tog_view *view;
7964 int *pack_fds = NULL;
7966 while ((ch = getopt(argc, argv, "r:")) != -1) {
7969 repo_path = realpath(optarg, NULL);
7970 if (repo_path == NULL)
7971 return got_error_from_errno2("realpath",
7986 error = got_repo_pack_fds_open(&pack_fds);
7990 if (repo_path == NULL) {
7991 cwd = getcwd(NULL, 0);
7993 return got_error_from_errno("getcwd");
7994 error = got_worktree_open(&worktree, cwd);
7995 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7999 strdup(got_worktree_get_repo_path(worktree));
8001 repo_path = strdup(cwd);
8002 if (repo_path == NULL) {
8003 error = got_error_from_errno("strdup");
8008 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8014 error = apply_unveil(got_repo_get_path(repo), NULL);
8018 error = tog_load_refs(repo, 0);
8022 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8024 error = got_error_from_errno("view_open");
8028 error = open_ref_view(view, repo);
8033 /* Release work tree lock. */
8034 got_worktree_close(worktree);
8037 error = view_loop(view);
8042 const struct got_error *close_err = got_repo_close(repo);
8047 const struct got_error *pack_err =
8048 got_repo_pack_fds_close(pack_fds);
8056 static const struct got_error *
8057 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8058 enum tog_view_type request, int y, int x)
8060 const struct got_error *err = NULL;
8066 if (view->type == TOG_VIEW_LOG) {
8067 struct tog_log_view_state *s = &view->state.log;
8069 err = open_diff_view_for_commit(new_view, y, x,
8070 s->selected_entry->commit, s->selected_entry->id,
8073 return got_error_msg(GOT_ERR_NOT_IMPL,
8074 "parent/child view pair not supported");
8076 case TOG_VIEW_BLAME:
8077 if (view->type == TOG_VIEW_TREE) {
8078 struct tog_tree_view_state *s = &view->state.tree;
8080 err = blame_tree_entry(new_view, y, x,
8081 s->selected_entry, &s->parents, s->commit_id,
8084 return got_error_msg(GOT_ERR_NOT_IMPL,
8085 "parent/child view pair not supported");
8088 if (view->type == TOG_VIEW_BLAME)
8089 err = log_annotated_line(new_view, y, x,
8090 view->state.blame.repo, view->state.blame.id_to_log);
8091 else if (view->type == TOG_VIEW_TREE)
8092 err = log_selected_tree_entry(new_view, y, x,
8094 else if (view->type == TOG_VIEW_REF)
8095 err = log_ref_entry(new_view, y, x,
8096 view->state.ref.selected_entry,
8097 view->state.ref.repo);
8099 return got_error_msg(GOT_ERR_NOT_IMPL,
8100 "parent/child view pair not supported");
8103 if (view->type == TOG_VIEW_LOG)
8104 err = browse_commit_tree(new_view, y, x,
8105 view->state.log.selected_entry,
8106 view->state.log.in_repo_path,
8107 view->state.log.head_ref_name,
8108 view->state.log.repo);
8109 else if (view->type == TOG_VIEW_REF)
8110 err = browse_ref_tree(new_view, y, x,
8111 view->state.ref.selected_entry,
8112 view->state.ref.repo);
8114 return got_error_msg(GOT_ERR_NOT_IMPL,
8115 "parent/child view pair not supported");
8118 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8119 if (*new_view == NULL)
8120 return got_error_from_errno("view_open");
8121 if (view->type == TOG_VIEW_LOG)
8122 err = open_ref_view(*new_view, view->state.log.repo);
8123 else if (view->type == TOG_VIEW_TREE)
8124 err = open_ref_view(*new_view, view->state.tree.repo);
8126 err = got_error_msg(GOT_ERR_NOT_IMPL,
8127 "parent/child view pair not supported");
8129 view_close(*new_view);
8132 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8139 * If view was scrolled down to move the selected line into view when opening a
8140 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8143 offset_selection_up(struct tog_view *view)
8145 switch (view->type) {
8146 case TOG_VIEW_BLAME: {
8147 struct tog_blame_view_state *s = &view->state.blame;
8148 if (s->first_displayed_line == 1) {
8149 s->selected_line = MAX(s->selected_line - view->offset,
8153 if (s->first_displayed_line > view->offset)
8154 s->first_displayed_line -= view->offset;
8156 s->first_displayed_line = 1;
8157 s->selected_line += view->offset;
8161 log_scroll_up(&view->state.log, view->offset);
8162 view->state.log.selected += view->offset;
8165 ref_scroll_up(&view->state.ref, view->offset);
8166 view->state.ref.selected += view->offset;
8169 tree_scroll_up(&view->state.tree, view->offset);
8170 view->state.tree.selected += view->offset;
8180 * If the selected line is in the section of screen covered by the bottom split,
8181 * scroll down offset lines to move it into view and index its new position.
8183 static const struct got_error *
8184 offset_selection_down(struct tog_view *view)
8186 const struct got_error *err = NULL;
8187 const struct got_error *(*scrolld)(struct tog_view *, int);
8188 int *selected = NULL;
8191 switch (view->type) {
8192 case TOG_VIEW_BLAME: {
8193 struct tog_blame_view_state *s = &view->state.blame;
8196 if (s->selected_line > view->nlines - header) {
8197 offset = abs(view->nlines - s->selected_line - header);
8198 s->first_displayed_line += offset;
8199 s->selected_line -= offset;
8200 view->offset = offset;
8204 case TOG_VIEW_LOG: {
8205 struct tog_log_view_state *s = &view->state.log;
8206 scrolld = &log_scroll_down;
8207 header = view_is_parent_view(view) ? 3 : 2;
8208 selected = &s->selected;
8211 case TOG_VIEW_REF: {
8212 struct tog_ref_view_state *s = &view->state.ref;
8213 scrolld = &ref_scroll_down;
8215 selected = &s->selected;
8218 case TOG_VIEW_TREE: {
8219 struct tog_tree_view_state *s = &view->state.tree;
8220 scrolld = &tree_scroll_down;
8222 selected = &s->selected;
8232 if (selected && *selected > view->nlines - header) {
8233 offset = abs(view->nlines - *selected - header);
8234 view->offset = offset;
8235 if (scrolld && offset) {
8236 err = scrolld(view, offset);
8237 *selected -= offset;
8245 list_commands(FILE *fp)
8249 fprintf(fp, "commands:");
8250 for (i = 0; i < nitems(tog_commands); i++) {
8251 const struct tog_cmd *cmd = &tog_commands[i];
8252 fprintf(fp, " %s", cmd->name);
8258 usage(int hflag, int status)
8260 FILE *fp = (status == 0) ? stdout : stderr;
8262 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8265 fprintf(fp, "lazy usage: %s path\n", getprogname());
8272 make_argv(int argc, ...)
8280 argv = calloc(argc, sizeof(char *));
8283 for (i = 0; i < argc; i++) {
8284 argv[i] = strdup(va_arg(ap, char *));
8285 if (argv[i] == NULL)
8294 * Try to convert 'tog path' into a 'tog log path' command.
8295 * The user could simply have mistyped the command rather than knowingly
8296 * provided a path. So check whether argv[0] can in fact be resolved
8297 * to a path in the HEAD commit and print a special error if not.
8298 * This hack is for mpi@ <3
8300 static const struct got_error *
8301 tog_log_with_path(int argc, char *argv[])
8303 const struct got_error *error = NULL, *close_err;
8304 const struct tog_cmd *cmd = NULL;
8305 struct got_repository *repo = NULL;
8306 struct got_worktree *worktree = NULL;
8307 struct got_object_id *commit_id = NULL, *id = NULL;
8308 struct got_commit_object *commit = NULL;
8309 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8310 char *commit_id_str = NULL, **cmd_argv = NULL;
8311 int *pack_fds = NULL;
8313 cwd = getcwd(NULL, 0);
8315 return got_error_from_errno("getcwd");
8317 error = got_repo_pack_fds_open(&pack_fds);
8321 error = got_worktree_open(&worktree, cwd);
8322 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8326 repo_path = strdup(got_worktree_get_repo_path(worktree));
8328 repo_path = strdup(cwd);
8329 if (repo_path == NULL) {
8330 error = got_error_from_errno("strdup");
8334 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8338 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8343 error = tog_load_refs(repo, 0);
8346 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8347 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8348 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8353 got_worktree_close(worktree);
8357 error = got_object_open_as_commit(&commit, repo, commit_id);
8361 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8363 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8365 fprintf(stderr, "%s: '%s' is no known command or path\n",
8366 getprogname(), argv[0]);
8371 error = got_object_id_str(&commit_id_str, commit_id);
8375 cmd = &tog_commands[0]; /* log */
8377 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8378 error = cmd->cmd_main(argc, cmd_argv);
8381 close_err = got_repo_close(repo);
8386 got_object_commit_close(commit);
8388 got_worktree_close(worktree);
8390 const struct got_error *pack_err =
8391 got_repo_pack_fds_close(pack_fds);
8396 free(commit_id_str);
8403 for (i = 0; i < argc; i++)
8412 main(int argc, char *argv[])
8414 const struct got_error *error = NULL;
8415 const struct tog_cmd *cmd = NULL;
8416 int ch, hflag = 0, Vflag = 0;
8417 char **cmd_argv = NULL;
8418 static const struct option longopts[] = {
8419 { "version", no_argument, NULL, 'V' },
8422 char *diff_algo_str = NULL;
8424 setlocale(LC_CTYPE, "");
8426 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8446 got_version_print_str();
8451 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8459 /* Build an argument vector which runs a default command. */
8460 cmd = &tog_commands[0];
8462 cmd_argv = make_argv(argc, cmd->name);
8466 /* Did the user specify a command? */
8467 for (i = 0; i < nitems(tog_commands); i++) {
8468 if (strncmp(tog_commands[i].name, argv[0],
8469 strlen(argv[0])) == 0) {
8470 cmd = &tog_commands[i];
8476 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8477 if (diff_algo_str) {
8478 if (strcasecmp(diff_algo_str, "patience") == 0)
8479 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8480 if (strcasecmp(diff_algo_str, "myers") == 0)
8481 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8487 /* No command specified; try log with a path */
8488 error = tog_log_with_path(argc, argv);
8493 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8500 for (i = 0; i < argc; i++)
8505 if (error && error->code != GOT_ERR_CANCELLED)
8506 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);