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 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define CTRL(x) ((x) & 0x1f)
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
119 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
121 #define TOG_EOF_STRING "(END)"
123 struct commit_queue_entry {
124 TAILQ_ENTRY(commit_queue_entry) entry;
125 struct got_object_id *id;
126 struct got_commit_object *commit;
129 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 struct commit_queue {
132 struct commit_queue_head head;
136 STAILQ_ENTRY(tog_color) entry;
140 STAILQ_HEAD(tog_colors, tog_color);
142 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 static struct got_reflist_object_id_map *tog_refs_idmap;
144 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
146 static const struct got_error *
147 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 struct got_reference* re2)
150 const char *name1 = got_ref_get_name(re1);
151 const char *name2 = got_ref_get_name(re2);
152 int isbackup1, isbackup2;
154 /* Sort backup refs towards the bottom of the list. */
155 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 if (!isbackup1 && isbackup2) {
160 } else if (isbackup1 && !isbackup2) {
165 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
169 static const struct got_error *
170 tog_load_refs(struct got_repository *repo, int sort_by_date)
172 const struct got_error *err;
174 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
180 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
187 if (tog_refs_idmap) {
188 got_reflist_object_id_map_free(tog_refs_idmap);
189 tog_refs_idmap = NULL;
191 got_ref_list_free(&tog_refs);
194 static const struct got_error *
195 add_color(struct tog_colors *colors, const char *pattern,
196 int idx, short color)
198 const struct got_error *err = NULL;
199 struct tog_color *tc;
202 if (idx < 1 || idx > COLOR_PAIRS - 1)
205 init_pair(idx, color, -1);
207 tc = calloc(1, sizeof(*tc));
209 return got_error_from_errno("calloc");
210 regerr = regcomp(&tc->regex, pattern,
211 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
213 static char regerr_msg[512];
214 static char err_msg[512];
215 regerror(regerr, &tc->regex, regerr_msg,
217 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
219 err = got_error_msg(GOT_ERR_REGEX, err_msg);
224 STAILQ_INSERT_HEAD(colors, tc, entry);
229 free_colors(struct tog_colors *colors)
231 struct tog_color *tc;
233 while (!STAILQ_EMPTY(colors)) {
234 tc = STAILQ_FIRST(colors);
235 STAILQ_REMOVE_HEAD(colors, entry);
241 static struct tog_color *
242 get_color(struct tog_colors *colors, int colorpair)
244 struct tog_color *tc = NULL;
246 STAILQ_FOREACH(tc, colors, entry) {
247 if (tc->colorpair == colorpair)
255 default_color_value(const char *envvar)
257 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 return COLOR_MAGENTA;
259 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
261 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
263 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
265 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
271 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
273 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
275 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
277 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
279 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
281 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
285 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
292 get_color_value(const char *envvar)
294 const char *val = getenv(envvar);
297 return default_color_value(envvar);
299 if (strcasecmp(val, "black") == 0)
301 if (strcasecmp(val, "red") == 0)
303 if (strcasecmp(val, "green") == 0)
305 if (strcasecmp(val, "yellow") == 0)
307 if (strcasecmp(val, "blue") == 0)
309 if (strcasecmp(val, "magenta") == 0)
310 return COLOR_MAGENTA;
311 if (strcasecmp(val, "cyan") == 0)
313 if (strcasecmp(val, "white") == 0)
315 if (strcasecmp(val, "default") == 0)
318 return default_color_value(envvar);
321 struct tog_diff_view_state {
322 struct got_object_id *id1, *id2;
323 const char *label1, *label2;
327 int first_displayed_line;
328 int last_displayed_line;
331 int ignore_whitespace;
333 struct got_repository *repo;
334 struct got_diff_line *lines;
339 /* passed from log or blame view; may be NULL */
340 struct tog_view *parent_view;
343 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 static volatile sig_atomic_t tog_thread_error;
346 struct tog_log_thread_args {
347 pthread_cond_t need_commits;
348 pthread_cond_t commit_loaded;
351 struct got_commit_graph *graph;
352 struct commit_queue *real_commits;
353 const char *in_repo_path;
354 struct got_object_id *start_id;
355 struct got_repository *repo;
359 struct commit_queue_entry **first_displayed_entry;
360 struct commit_queue_entry **selected_entry;
362 int *search_next_done;
366 regex_t *limit_regex;
367 struct commit_queue *limit_commits;
370 struct tog_log_view_state {
371 struct commit_queue *commits;
372 struct commit_queue_entry *first_displayed_entry;
373 struct commit_queue_entry *last_displayed_entry;
374 struct commit_queue_entry *selected_entry;
375 struct commit_queue real_commits;
380 struct got_repository *repo;
381 struct got_object_id *start_id;
384 struct tog_log_thread_args thread_args;
385 struct commit_queue_entry *matched_entry;
386 struct commit_queue_entry *search_entry;
387 struct tog_colors colors;
391 struct commit_queue limit_commits;
394 #define TOG_COLOR_DIFF_MINUS 1
395 #define TOG_COLOR_DIFF_PLUS 2
396 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
397 #define TOG_COLOR_DIFF_META 4
398 #define TOG_COLOR_TREE_SUBMODULE 5
399 #define TOG_COLOR_TREE_SYMLINK 6
400 #define TOG_COLOR_TREE_DIRECTORY 7
401 #define TOG_COLOR_TREE_EXECUTABLE 8
402 #define TOG_COLOR_COMMIT 9
403 #define TOG_COLOR_AUTHOR 10
404 #define TOG_COLOR_DATE 11
405 #define TOG_COLOR_REFS_HEADS 12
406 #define TOG_COLOR_REFS_TAGS 13
407 #define TOG_COLOR_REFS_REMOTES 14
408 #define TOG_COLOR_REFS_BACKUP 15
410 struct tog_blame_cb_args {
411 struct tog_blame_line *lines; /* one per line */
414 struct tog_view *view;
415 struct got_object_id *commit_id;
419 struct tog_blame_thread_args {
421 struct got_repository *repo;
422 struct tog_blame_cb_args *cb_args;
424 got_cancel_cb cancel_cb;
431 struct tog_blame_line *lines;
435 struct tog_blame_thread_args thread_args;
436 struct tog_blame_cb_args cb_args;
441 struct tog_blame_view_state {
442 int first_displayed_line;
443 int last_displayed_line;
445 int last_diffed_line;
449 struct got_object_id_queue blamed_commits;
450 struct got_object_qid *blamed_commit;
452 struct got_repository *repo;
453 struct got_object_id *commit_id;
454 struct got_object_id *id_to_log;
455 struct tog_blame blame;
457 struct tog_colors colors;
460 struct tog_parent_tree {
461 TAILQ_ENTRY(tog_parent_tree) entry;
462 struct got_tree_object *tree;
463 struct got_tree_entry *first_displayed_entry;
464 struct got_tree_entry *selected_entry;
468 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
470 struct tog_tree_view_state {
472 struct got_object_id *commit_id;/* commit which this tree belongs to */
473 struct got_tree_object *root; /* the commit's root tree entry */
474 struct got_tree_object *tree; /* currently displayed (sub-)tree */
475 struct got_tree_entry *first_displayed_entry;
476 struct got_tree_entry *last_displayed_entry;
477 struct got_tree_entry *selected_entry;
478 int ndisplayed, selected, show_ids;
479 struct tog_parent_trees parents; /* parent trees of current sub-tree */
481 struct got_repository *repo;
482 struct got_tree_entry *matched_entry;
483 struct tog_colors colors;
486 struct tog_reflist_entry {
487 TAILQ_ENTRY(tog_reflist_entry) entry;
488 struct got_reference *ref;
492 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
494 struct tog_ref_view_state {
495 struct tog_reflist_head refs;
496 struct tog_reflist_entry *first_displayed_entry;
497 struct tog_reflist_entry *last_displayed_entry;
498 struct tog_reflist_entry *selected_entry;
499 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
500 struct got_repository *repo;
501 struct tog_reflist_entry *matched_entry;
502 struct tog_colors colors;
506 * We implement two types of views: parent views and child views.
508 * The 'Tab' key switches focus between a parent view and its child view.
509 * Child views are shown side-by-side to their parent view, provided
510 * there is enough screen estate.
512 * When a new view is opened from within a parent view, this new view
513 * becomes a child view of the parent view, replacing any existing child.
515 * When a new view is opened from within a child view, this new view
516 * becomes a parent view which will obscure the views below until the
517 * user quits the new parent view by typing 'q'.
519 * This list of views contains parent views only.
520 * Child views are only pointed to by their parent view.
522 TAILQ_HEAD(tog_view_list_head, tog_view);
525 TAILQ_ENTRY(tog_view) entry;
528 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
529 int resized_y, resized_x; /* begin_y/x based on user resizing */
530 int maxx, x; /* max column and current start column */
531 int lines, cols; /* copies of LINES and COLS */
532 int nscrolled, offset; /* lines scrolled and hsplit line offset */
533 int gline, hiline; /* navigate to and highlight this nG line */
534 int ch, count; /* current keymap and count prefix */
535 int resized; /* set when in a resize event */
536 int focussed; /* Only set on one parent or child view at a time. */
538 struct tog_view *parent;
539 struct tog_view *child;
542 * This flag is initially set on parent views when a new child view
543 * is created. It gets toggled when the 'Tab' key switches focus
544 * between parent and child.
545 * The flag indicates whether focus should be passed on to our child
546 * view if this parent view gets picked for focus after another parent
547 * view was closed. This prevents child views from losing focus in such
552 enum tog_view_mode mode;
553 /* type-specific state */
554 enum tog_view_type type;
556 struct tog_diff_view_state diff;
557 struct tog_log_view_state log;
558 struct tog_blame_view_state blame;
559 struct tog_tree_view_state tree;
560 struct tog_ref_view_state ref;
563 const struct got_error *(*show)(struct tog_view *);
564 const struct got_error *(*input)(struct tog_view **,
565 struct tog_view *, int);
566 const struct got_error *(*reset)(struct tog_view *);
567 const struct got_error *(*resize)(struct tog_view *, int);
568 const struct got_error *(*close)(struct tog_view *);
570 const struct got_error *(*search_start)(struct tog_view *);
571 const struct got_error *(*search_next)(struct tog_view *);
574 #define TOG_SEARCH_FORWARD 1
575 #define TOG_SEARCH_BACKWARD 2
576 int search_next_done;
577 #define TOG_SEARCH_HAVE_MORE 1
578 #define TOG_SEARCH_NO_MORE 2
579 #define TOG_SEARCH_HAVE_NONE 3
584 static const struct got_error *open_diff_view(struct tog_view *,
585 struct got_object_id *, struct got_object_id *,
586 const char *, const char *, int, int, int, struct tog_view *,
587 struct got_repository *);
588 static const struct got_error *show_diff_view(struct tog_view *);
589 static const struct got_error *input_diff_view(struct tog_view **,
590 struct tog_view *, int);
591 static const struct got_error *reset_diff_view(struct tog_view *);
592 static const struct got_error* close_diff_view(struct tog_view *);
593 static const struct got_error *search_start_diff_view(struct tog_view *);
594 static const struct got_error *search_next_diff_view(struct tog_view *);
596 static const struct got_error *open_log_view(struct tog_view *,
597 struct got_object_id *, struct got_repository *,
598 const char *, const char *, int);
599 static const struct got_error * show_log_view(struct tog_view *);
600 static const struct got_error *input_log_view(struct tog_view **,
601 struct tog_view *, int);
602 static const struct got_error *resize_log_view(struct tog_view *, int);
603 static const struct got_error *close_log_view(struct tog_view *);
604 static const struct got_error *search_start_log_view(struct tog_view *);
605 static const struct got_error *search_next_log_view(struct tog_view *);
607 static const struct got_error *open_blame_view(struct tog_view *, char *,
608 struct got_object_id *, struct got_repository *);
609 static const struct got_error *show_blame_view(struct tog_view *);
610 static const struct got_error *input_blame_view(struct tog_view **,
611 struct tog_view *, int);
612 static const struct got_error *reset_blame_view(struct tog_view *);
613 static const struct got_error *close_blame_view(struct tog_view *);
614 static const struct got_error *search_start_blame_view(struct tog_view *);
615 static const struct got_error *search_next_blame_view(struct tog_view *);
617 static const struct got_error *open_tree_view(struct tog_view *,
618 struct got_object_id *, const char *, struct got_repository *);
619 static const struct got_error *show_tree_view(struct tog_view *);
620 static const struct got_error *input_tree_view(struct tog_view **,
621 struct tog_view *, int);
622 static const struct got_error *close_tree_view(struct tog_view *);
623 static const struct got_error *search_start_tree_view(struct tog_view *);
624 static const struct got_error *search_next_tree_view(struct tog_view *);
626 static const struct got_error *open_ref_view(struct tog_view *,
627 struct got_repository *);
628 static const struct got_error *show_ref_view(struct tog_view *);
629 static const struct got_error *input_ref_view(struct tog_view **,
630 struct tog_view *, int);
631 static const struct got_error *close_ref_view(struct tog_view *);
632 static const struct got_error *search_start_ref_view(struct tog_view *);
633 static const struct got_error *search_next_ref_view(struct tog_view *);
635 static volatile sig_atomic_t tog_sigwinch_received;
636 static volatile sig_atomic_t tog_sigpipe_received;
637 static volatile sig_atomic_t tog_sigcont_received;
638 static volatile sig_atomic_t tog_sigint_received;
639 static volatile sig_atomic_t tog_sigterm_received;
642 tog_sigwinch(int signo)
644 tog_sigwinch_received = 1;
648 tog_sigpipe(int signo)
650 tog_sigpipe_received = 1;
654 tog_sigcont(int signo)
656 tog_sigcont_received = 1;
660 tog_sigint(int signo)
662 tog_sigint_received = 1;
666 tog_sigterm(int signo)
668 tog_sigterm_received = 1;
672 tog_fatal_signal_received(void)
674 return (tog_sigpipe_received ||
675 tog_sigint_received || tog_sigint_received);
678 static const struct got_error *
679 view_close(struct tog_view *view)
681 const struct got_error *err = NULL, *child_err = NULL;
684 child_err = view_close(view->child);
688 err = view->close(view);
690 del_panel(view->panel);
692 delwin(view->window);
694 return err ? err : child_err;
697 static struct tog_view *
698 view_open(int nlines, int ncols, int begin_y, int begin_x,
699 enum tog_view_type type)
701 struct tog_view *view = calloc(1, sizeof(*view));
709 view->nlines = nlines ? nlines : LINES - begin_y;
710 view->ncols = ncols ? ncols : COLS - begin_x;
711 view->begin_y = begin_y;
712 view->begin_x = begin_x;
713 view->window = newwin(nlines, ncols, begin_y, begin_x);
714 if (view->window == NULL) {
718 view->panel = new_panel(view->window);
719 if (view->panel == NULL ||
720 set_panel_userptr(view->panel, view) != OK) {
725 keypad(view->window, TRUE);
730 view_split_begin_x(int begin_x)
732 if (begin_x > 0 || COLS < 120)
734 return (COLS - MAX(COLS / 2, 80));
737 /* XXX Stub till we decide what to do. */
739 view_split_begin_y(int lines)
741 return lines * HSPLIT_SCALE;
744 static const struct got_error *view_resize(struct tog_view *);
746 static const struct got_error *
747 view_splitscreen(struct tog_view *view)
749 const struct got_error *err = NULL;
751 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
752 if (view->resized_y && view->resized_y < view->lines)
753 view->begin_y = view->resized_y;
755 view->begin_y = view_split_begin_y(view->nlines);
757 } else if (!view->resized) {
758 if (view->resized_x && view->resized_x < view->cols - 1 &&
760 view->begin_x = view->resized_x;
762 view->begin_x = view_split_begin_x(0);
765 view->nlines = LINES - view->begin_y;
766 view->ncols = COLS - view->begin_x;
769 err = view_resize(view);
773 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
774 view->parent->nlines = view->begin_y;
776 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
777 return got_error_from_errno("mvwin");
782 static const struct got_error *
783 view_fullscreen(struct tog_view *view)
785 const struct got_error *err = NULL;
788 view->begin_y = view->resized ? view->begin_y : 0;
789 view->nlines = view->resized ? view->nlines : LINES;
793 err = view_resize(view);
797 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
798 return got_error_from_errno("mvwin");
804 view_is_parent_view(struct tog_view *view)
806 return view->parent == NULL;
810 view_is_splitscreen(struct tog_view *view)
812 return view->begin_x > 0 || view->begin_y > 0;
816 view_is_fullscreen(struct tog_view *view)
818 return view->nlines == LINES && view->ncols == COLS;
822 view_is_hsplit_top(struct tog_view *view)
824 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
825 view_is_splitscreen(view->child);
829 view_border(struct tog_view *view)
832 const struct tog_view *view_above;
835 return view_border(view->parent);
837 panel = panel_above(view->panel);
841 view_above = panel_userptr(panel);
842 if (view->mode == TOG_VIEW_SPLIT_HRZN)
843 mvwhline(view->window, view_above->begin_y - 1,
844 view->begin_x, got_locale_is_utf8() ?
845 ACS_HLINE : '-', view->ncols);
847 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
848 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
851 static const struct got_error *view_init_hsplit(struct tog_view *, int);
852 static const struct got_error *request_log_commits(struct tog_view *);
853 static const struct got_error *offset_selection_down(struct tog_view *);
854 static void offset_selection_up(struct tog_view *);
855 static void view_get_split(struct tog_view *, int *, int *);
857 static const struct got_error *
858 view_resize(struct tog_view *view)
860 const struct got_error *err = NULL;
861 int dif, nlines, ncols;
863 dif = LINES - view->lines; /* line difference */
865 if (view->lines > LINES)
866 nlines = view->nlines - (view->lines - LINES);
868 nlines = view->nlines + (LINES - view->lines);
869 if (view->cols > COLS)
870 ncols = view->ncols - (view->cols - COLS);
872 ncols = view->ncols + (COLS - view->cols);
875 int hs = view->child->begin_y;
877 if (!view_is_fullscreen(view))
878 view->child->begin_x = view_split_begin_x(view->begin_x);
879 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
880 view->child->begin_x == 0) {
883 view_fullscreen(view->child);
884 if (view->child->focussed)
885 show_panel(view->child->panel);
887 show_panel(view->panel);
889 ncols = view->child->begin_x;
891 view_splitscreen(view->child);
892 show_panel(view->child->panel);
895 * XXX This is ugly and needs to be moved into the above
896 * logic but "works" for now and my attempts at moving it
897 * break either 'tab' or 'F' key maps in horizontal splits.
900 err = view_splitscreen(view->child);
903 if (dif < 0) { /* top split decreased */
904 err = offset_selection_down(view);
911 show_panel(view->child->panel);
912 nlines = view->nlines;
914 } else if (view->parent == NULL)
917 if (view->resize && dif > 0) {
918 err = view->resize(view, dif);
923 if (wresize(view->window, nlines, ncols) == ERR)
924 return got_error_from_errno("wresize");
925 if (replace_panel(view->panel, view->window) == ERR)
926 return got_error_from_errno("replace_panel");
927 wclear(view->window);
929 view->nlines = nlines;
937 static const struct got_error *
938 resize_log_view(struct tog_view *view, int increase)
940 struct tog_log_view_state *s = &view->state.log;
941 const struct got_error *err = NULL;
944 if (s->selected_entry)
945 n = s->selected_entry->idx + view->lines - s->selected;
948 * Request commits to account for the increased
949 * height so we have enough to populate the view.
951 if (s->commits->ncommits < n) {
952 view->nscrolled = n - s->commits->ncommits + increase + 1;
953 err = request_log_commits(view);
960 view_adjust_offset(struct tog_view *view, int n)
965 if (view->parent && view->parent->offset) {
966 if (view->parent->offset + n >= 0)
967 view->parent->offset += n;
969 view->parent->offset = 0;
970 } else if (view->offset) {
971 if (view->offset - n >= 0)
978 static const struct got_error *
979 view_resize_split(struct tog_view *view, int resize)
981 const struct got_error *err = NULL;
982 struct tog_view *v = NULL;
989 if (!v->child || !view_is_splitscreen(v->child))
992 v->resized = v->child->resized = resize; /* lock for resize event */
994 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
995 if (v->child->resized_y)
996 v->child->begin_y = v->child->resized_y;
998 v->child->begin_y -= resize;
1000 v->child->begin_y += resize;
1001 if (v->child->begin_y < 3) {
1003 v->child->begin_y = 3;
1004 } else if (v->child->begin_y > LINES - 1) {
1006 v->child->begin_y = LINES - 1;
1009 v->child->ncols = COLS;
1010 view_adjust_offset(view, resize);
1011 err = view_init_hsplit(v, v->child->begin_y);
1014 v->child->resized_y = v->child->begin_y;
1016 if (v->child->resized_x)
1017 v->child->begin_x = v->child->resized_x;
1019 v->child->begin_x -= resize;
1021 v->child->begin_x += resize;
1022 if (v->child->begin_x < 11) {
1024 v->child->begin_x = 11;
1025 } else if (v->child->begin_x > COLS - 1) {
1027 v->child->begin_x = COLS - 1;
1029 v->child->resized_x = v->child->begin_x;
1032 v->child->mode = v->mode;
1033 v->child->nlines = v->lines - v->child->begin_y;
1034 v->child->ncols = v->cols - v->child->begin_x;
1037 err = view_fullscreen(v);
1040 err = view_splitscreen(v->child);
1044 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1045 err = offset_selection_down(v->child);
1051 err = v->resize(v, 0);
1052 else if (v->child->resize)
1053 err = v->child->resize(v->child, 0);
1055 v->resized = v->child->resized = 0;
1061 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1063 struct tog_view *v = src->child ? src->child : src;
1065 dst->resized_x = v->resized_x;
1066 dst->resized_y = v->resized_y;
1069 static const struct got_error *
1070 view_close_child(struct tog_view *view)
1072 const struct got_error *err = NULL;
1074 if (view->child == NULL)
1077 err = view_close(view->child);
1082 static const struct got_error *
1083 view_set_child(struct tog_view *view, struct tog_view *child)
1085 const struct got_error *err = NULL;
1087 view->child = child;
1088 child->parent = view;
1090 err = view_resize(view);
1094 if (view->child->resized_x || view->child->resized_y)
1095 err = view_resize_split(view, 0);
1100 static const struct got_error *view_dispatch_request(struct tog_view **,
1101 struct tog_view *, enum tog_view_type, int, int);
1103 static const struct got_error *
1104 view_request_new(struct tog_view **requested, struct tog_view *view,
1105 enum tog_view_type request)
1107 struct tog_view *new_view = NULL;
1108 const struct got_error *err;
1113 if (view_is_parent_view(view))
1114 view_get_split(view, &y, &x);
1116 err = view_dispatch_request(&new_view, view, request, y, x);
1120 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1121 err = view_init_hsplit(view, y);
1127 new_view->focussed = 1;
1128 new_view->mode = view->mode;
1129 new_view->nlines = view->lines - y;
1131 if (view_is_parent_view(view)) {
1132 view_transfer_size(new_view, view);
1133 err = view_close_child(view);
1136 err = view_set_child(view, new_view);
1139 view->focus_child = 1;
1141 *requested = new_view;
1147 tog_resizeterm(void)
1150 struct winsize size;
1152 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1153 cols = 80; /* Default */
1157 lines = size.ws_row;
1159 resize_term(lines, cols);
1162 static const struct got_error *
1163 view_search_start(struct tog_view *view)
1165 const struct got_error *err = NULL;
1166 struct tog_view *v = view;
1170 if (view->search_started) {
1171 regfree(&view->regex);
1172 view->searching = 0;
1173 memset(&view->regmatch, 0, sizeof(view->regmatch));
1175 view->search_started = 0;
1177 if (view->nlines < 1)
1180 if (view_is_hsplit_top(view))
1183 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1184 wclrtoeol(v->window);
1186 nodelay(view->window, FALSE); /* block for search term input */
1189 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1190 wrefresh(v->window);
1193 nodelay(view->window, TRUE);
1197 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1198 err = view->search_start(view);
1200 regfree(&view->regex);
1203 view->search_started = 1;
1204 view->searching = TOG_SEARCH_FORWARD;
1205 view->search_next_done = 0;
1206 view->search_next(view);
1212 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1213 static const struct got_error *
1214 switch_split(struct tog_view *view)
1216 const struct got_error *err = NULL;
1217 struct tog_view *v = NULL;
1224 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1225 v->mode = TOG_VIEW_SPLIT_VERT;
1227 v->mode = TOG_VIEW_SPLIT_HRZN;
1231 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1232 v->mode = TOG_VIEW_SPLIT_NONE;
1234 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1235 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1236 v->child->begin_y = v->child->resized_y;
1237 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1238 v->child->begin_x = v->child->resized_x;
1241 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1243 v->child->ncols = COLS;
1244 v->child->nscrolled = LINES - v->child->nlines;
1246 err = view_init_hsplit(v, v->child->begin_y);
1250 v->child->mode = v->mode;
1251 v->child->nlines = v->lines - v->child->begin_y;
1254 err = view_fullscreen(v);
1257 err = view_splitscreen(v->child);
1261 if (v->mode == TOG_VIEW_SPLIT_NONE)
1262 v->mode = TOG_VIEW_SPLIT_VERT;
1263 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1264 err = offset_selection_down(v);
1267 err = offset_selection_down(v->child);
1271 offset_selection_up(v);
1272 offset_selection_up(v->child);
1275 err = v->resize(v, 0);
1276 else if (v->child->resize)
1277 err = v->child->resize(v->child, 0);
1283 * Compute view->count from numeric input. Assign total to view->count and
1284 * return first non-numeric key entered.
1287 get_compound_key(struct tog_view *view, int c)
1289 struct tog_view *v = view;
1292 if (view_is_hsplit_top(view))
1294 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1298 cbreak(); /* block for input */
1299 nodelay(view->window, FALSE);
1300 wmove(v->window, v->nlines - 1, 0);
1301 wclrtoeol(v->window);
1302 waddch(v->window, ':');
1305 x = getcurx(v->window);
1306 if (x != ERR && x < view->ncols) {
1307 waddch(v->window, c);
1308 wrefresh(v->window);
1312 * Don't overflow. Max valid request should be the greatest
1313 * between the longest and total lines; cap at 10 million.
1318 n = n * 10 + (c - '0');
1319 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1321 if (c == 'G' || c == 'g') { /* nG key map */
1322 view->gline = view->hiline = n;
1327 /* Massage excessive or inapplicable values at the input handler. */
1333 static const struct got_error *
1334 view_input(struct tog_view **new, int *done, struct tog_view *view,
1335 struct tog_view_list_head *views)
1337 const struct got_error *err = NULL;
1343 /* Clear "no matches" indicator. */
1344 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1345 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1346 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1350 if (view->searching && !view->search_next_done) {
1351 errcode = pthread_mutex_unlock(&tog_mutex);
1353 return got_error_set_errno(errcode,
1354 "pthread_mutex_unlock");
1356 errcode = pthread_mutex_lock(&tog_mutex);
1358 return got_error_set_errno(errcode,
1359 "pthread_mutex_lock");
1360 view->search_next(view);
1364 /* Allow threads to make progress while we are waiting for input. */
1365 errcode = pthread_mutex_unlock(&tog_mutex);
1367 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1368 /* If we have an unfinished count, let C-g or backspace abort. */
1369 if (view->count && --view->count) {
1371 nodelay(view->window, TRUE);
1372 ch = wgetch(view->window);
1373 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1378 ch = wgetch(view->window);
1379 if (ch >= '1' && ch <= '9')
1380 view->ch = ch = get_compound_key(view, ch);
1382 if (view->hiline && ch != ERR && ch != 0)
1383 view->hiline = 0; /* key pressed, clear line highlight */
1384 nodelay(view->window, TRUE);
1385 errcode = pthread_mutex_lock(&tog_mutex);
1387 return got_error_set_errno(errcode, "pthread_mutex_lock");
1389 if (tog_sigwinch_received || tog_sigcont_received) {
1391 tog_sigwinch_received = 0;
1392 tog_sigcont_received = 0;
1393 TAILQ_FOREACH(v, views, entry) {
1394 err = view_resize(v);
1397 err = v->input(new, v, KEY_RESIZE);
1401 err = view_resize(v->child);
1404 err = v->child->input(new, v->child,
1408 if (v->child->resized_x || v->child->resized_y) {
1409 err = view_resize_split(v, 0);
1422 view->child->focussed = 1;
1423 view->focus_child = 1;
1424 } else if (view->parent) {
1426 view->parent->focussed = 1;
1427 view->parent->focus_child = 0;
1428 if (!view_is_splitscreen(view)) {
1429 if (view->parent->resize) {
1430 err = view->parent->resize(view->parent,
1435 offset_selection_up(view->parent);
1436 err = view_fullscreen(view->parent);
1443 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1444 if (view->parent->resize) {
1445 /* might need more commits to fill fullscreen */
1446 err = view->parent->resize(view->parent, 0);
1450 offset_selection_up(view->parent);
1452 err = view->input(new, view, ch);
1460 if (view_is_parent_view(view)) {
1461 if (view->child == NULL)
1463 if (view_is_splitscreen(view->child)) {
1465 view->child->focussed = 1;
1466 err = view_fullscreen(view->child);
1468 err = view_splitscreen(view->child);
1470 err = view_resize_split(view, 0);
1474 err = view->child->input(new, view->child,
1477 if (view_is_splitscreen(view)) {
1478 view->parent->focussed = 0;
1480 err = view_fullscreen(view);
1482 err = view_splitscreen(view);
1483 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1484 err = view_resize(view->parent);
1486 err = view_resize_split(view, 0);
1490 err = view->input(new, view, KEY_RESIZE);
1495 err = view->resize(view, 0);
1500 err = offset_selection_down(view->parent);
1502 err = offset_selection_down(view);
1506 err = switch_split(view);
1509 err = view_resize_split(view, -1);
1512 err = view_resize_split(view, 1);
1518 if (view->search_start)
1519 view_search_start(view);
1521 err = view->input(new, view, ch);
1525 if (view->search_started && view->search_next) {
1526 view->searching = (ch == 'n' ?
1527 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1528 view->search_next_done = 0;
1529 view->search_next(view);
1531 err = view->input(new, view, ch);
1534 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1535 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1537 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1538 TAILQ_FOREACH(v, views, entry) {
1544 if (v->child && v->child->reset) {
1545 err = v->child->reset(v->child);
1552 err = view->input(new, view, ch);
1560 view_needs_focus_indication(struct tog_view *view)
1562 if (view_is_parent_view(view)) {
1563 if (view->child == NULL || view->child->focussed)
1565 if (!view_is_splitscreen(view->child))
1567 } else if (!view_is_splitscreen(view))
1570 return view->focussed;
1573 static const struct got_error *
1574 view_loop(struct tog_view *view)
1576 const struct got_error *err = NULL;
1577 struct tog_view_list_head views;
1578 struct tog_view *new_view;
1580 int fast_refresh = 10;
1581 int done = 0, errcode;
1583 mode = getenv("TOG_VIEW_SPLIT_MODE");
1584 if (!mode || !(*mode == 'h' || *mode == 'H'))
1585 view->mode = TOG_VIEW_SPLIT_VERT;
1587 view->mode = TOG_VIEW_SPLIT_HRZN;
1589 errcode = pthread_mutex_lock(&tog_mutex);
1591 return got_error_set_errno(errcode, "pthread_mutex_lock");
1594 TAILQ_INSERT_HEAD(&views, view, entry);
1597 err = view->show(view);
1602 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1603 !tog_fatal_signal_received()) {
1604 /* Refresh fast during initialization, then become slower. */
1605 if (fast_refresh && fast_refresh-- == 0)
1606 halfdelay(10); /* switch to once per second */
1608 err = view_input(&new_view, &done, view, &views);
1612 struct tog_view *v, *prev = NULL;
1614 if (view_is_parent_view(view))
1615 prev = TAILQ_PREV(view, tog_view_list_head,
1617 else if (view->parent)
1618 prev = view->parent;
1621 view->parent->child = NULL;
1622 view->parent->focus_child = 0;
1623 /* Restore fullscreen line height. */
1624 view->parent->nlines = view->parent->lines;
1625 err = view_resize(view->parent);
1628 /* Make resized splits persist. */
1629 view_transfer_size(view->parent, view);
1631 TAILQ_REMOVE(&views, view, entry);
1633 err = view_close(view);
1638 TAILQ_FOREACH(v, &views, entry) {
1642 if (view == NULL && new_view == NULL) {
1643 /* No view has focus. Try to pick one. */
1646 else if (!TAILQ_EMPTY(&views)) {
1647 view = TAILQ_LAST(&views,
1648 tog_view_list_head);
1651 if (view->focus_child) {
1652 view->child->focussed = 1;
1660 struct tog_view *v, *t;
1661 /* Only allow one parent view per type. */
1662 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1663 if (v->type != new_view->type)
1665 TAILQ_REMOVE(&views, v, entry);
1666 err = view_close(v);
1671 TAILQ_INSERT_TAIL(&views, new_view, entry);
1675 if (view_is_parent_view(view)) {
1676 if (view->child && view->child->focussed)
1679 if (view->parent && view->parent->focussed)
1680 view = view->parent;
1682 show_panel(view->panel);
1683 if (view->child && view_is_splitscreen(view->child))
1684 show_panel(view->child->panel);
1685 if (view->parent && view_is_splitscreen(view)) {
1686 err = view->parent->show(view->parent);
1690 err = view->show(view);
1694 err = view->child->show(view->child);
1703 while (!TAILQ_EMPTY(&views)) {
1704 const struct got_error *close_err;
1705 view = TAILQ_FIRST(&views);
1706 TAILQ_REMOVE(&views, view, entry);
1707 close_err = view_close(view);
1708 if (close_err && err == NULL)
1712 errcode = pthread_mutex_unlock(&tog_mutex);
1713 if (errcode && err == NULL)
1714 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1724 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1729 /* Create newly allocated wide-character string equivalent to a byte string. */
1730 static const struct got_error *
1731 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1734 const struct got_error *err = NULL;
1737 *wlen = mbstowcs(NULL, s, 0);
1738 if (*wlen == (size_t)-1) {
1740 if (errno != EILSEQ)
1741 return got_error_from_errno("mbstowcs");
1743 /* byte string invalid in current encoding; try to "fix" it */
1744 err = got_mbsavis(&vis, &vislen, s);
1747 *wlen = mbstowcs(NULL, vis, 0);
1748 if (*wlen == (size_t)-1) {
1749 err = got_error_from_errno("mbstowcs"); /* give up */
1754 *ws = calloc(*wlen + 1, sizeof(**ws));
1756 err = got_error_from_errno("calloc");
1760 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1761 err = got_error_from_errno("mbstowcs");
1772 static const struct got_error *
1773 expand_tab(char **ptr, const char *src)
1776 size_t len, n, idx = 0, sz = 0;
1779 n = len = strlen(src);
1780 dst = malloc(n + 1);
1782 return got_error_from_errno("malloc");
1784 while (idx < len && src[idx]) {
1785 const char c = src[idx];
1788 size_t nb = TABSIZE - sz % TABSIZE;
1791 p = realloc(dst, n + nb);
1794 return got_error_from_errno("realloc");
1799 memset(dst + sz, ' ', nb);
1802 dst[sz++] = src[idx];
1812 * Advance at most n columns from wline starting at offset off.
1813 * Return the index to the first character after the span operation.
1814 * Return the combined column width of all spanned wide character in
1818 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1820 int width, i, cols = 0;
1827 for (i = off; wline[i] != L'\0'; ++i) {
1828 if (wline[i] == L'\t')
1829 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1831 width = wcwidth(wline[i]);
1838 if (cols + width > n)
1848 * Format a line for display, ensuring that it won't overflow a width limit.
1849 * With scrolling, the width returned refers to the scrolled version of the
1850 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1852 static const struct got_error *
1853 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1854 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1856 const struct got_error *err = NULL;
1858 wchar_t *wline = NULL;
1867 err = expand_tab(&exstr, line);
1872 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1877 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1879 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1880 wline[wlen - 1] = L'\0';
1883 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1884 wline[wlen - 1] = L'\0';
1888 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1894 *scrollxp = scrollx;
1902 static const struct got_error*
1903 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1904 struct got_object_id *id, struct got_repository *repo)
1906 static const struct got_error *err = NULL;
1907 struct got_reflist_entry *re;
1913 TAILQ_FOREACH(re, refs, entry) {
1914 struct got_tag_object *tag = NULL;
1915 struct got_object_id *ref_id;
1918 name = got_ref_get_name(re->ref);
1919 if (strcmp(name, GOT_REF_HEAD) == 0)
1921 if (strncmp(name, "refs/", 5) == 0)
1923 if (strncmp(name, "got/", 4) == 0 &&
1924 strncmp(name, "got/backup/", 11) != 0)
1926 if (strncmp(name, "heads/", 6) == 0)
1928 if (strncmp(name, "remotes/", 8) == 0) {
1930 s = strstr(name, "/" GOT_REF_HEAD);
1931 if (s != NULL && s[strlen(s)] == '\0')
1934 err = got_ref_resolve(&ref_id, repo, re->ref);
1937 if (strncmp(name, "tags/", 5) == 0) {
1938 err = got_object_open_as_tag(&tag, repo, ref_id);
1940 if (err->code != GOT_ERR_OBJ_TYPE) {
1944 /* Ref points at something other than a tag. */
1949 cmp = got_object_id_cmp(tag ?
1950 got_object_tag_get_object_id(tag) : ref_id, id);
1953 got_object_tag_close(tag);
1957 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1958 s ? ", " : "", name) == -1) {
1959 err = got_error_from_errno("asprintf");
1970 static const struct got_error *
1971 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1976 smallerthan = strchr(author, '<');
1977 if (smallerthan && smallerthan[1] != '\0')
1978 author = smallerthan + 1;
1979 author[strcspn(author, "@>")] = '\0';
1980 return format_line(wauthor, author_width, NULL, author, 0, limit,
1984 static const struct got_error *
1985 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1986 struct got_object_id *id, const size_t date_display_cols,
1987 int author_display_cols)
1989 struct tog_log_view_state *s = &view->state.log;
1990 const struct got_error *err = NULL;
1991 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1992 char *logmsg0 = NULL, *logmsg = NULL;
1993 char *author = NULL;
1994 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1995 int author_width, logmsg_width;
1996 char *newline, *line = NULL;
1997 int col, limit, scrollx;
1998 const int avail = view->ncols;
2000 time_t committer_time;
2001 struct tog_color *tc;
2003 committer_time = got_object_commit_get_committer_time(commit);
2004 if (gmtime_r(&committer_time, &tm) == NULL)
2005 return got_error_from_errno("gmtime_r");
2006 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2007 return got_error(GOT_ERR_NO_SPACE);
2009 if (avail <= date_display_cols)
2010 limit = MIN(sizeof(datebuf) - 1, avail);
2012 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2013 tc = get_color(&s->colors, TOG_COLOR_DATE);
2015 wattr_on(view->window,
2016 COLOR_PAIR(tc->colorpair), NULL);
2017 waddnstr(view->window, datebuf, limit);
2019 wattr_off(view->window,
2020 COLOR_PAIR(tc->colorpair), NULL);
2027 err = got_object_id_str(&id_str, id);
2030 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2032 wattr_on(view->window,
2033 COLOR_PAIR(tc->colorpair), NULL);
2034 wprintw(view->window, "%.8s ", id_str);
2036 wattr_off(view->window,
2037 COLOR_PAIR(tc->colorpair), NULL);
2044 if (s->use_committer)
2045 author = strdup(got_object_commit_get_committer(commit));
2047 author = strdup(got_object_commit_get_author(commit));
2048 if (author == NULL) {
2049 err = got_error_from_errno("strdup");
2052 err = format_author(&wauthor, &author_width, author, avail - col, col);
2055 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2057 wattr_on(view->window,
2058 COLOR_PAIR(tc->colorpair), NULL);
2059 waddwstr(view->window, wauthor);
2060 col += author_width;
2061 while (col < avail && author_width < author_display_cols + 2) {
2062 waddch(view->window, ' ');
2067 wattr_off(view->window,
2068 COLOR_PAIR(tc->colorpair), NULL);
2072 err = got_object_commit_get_logmsg(&logmsg0, commit);
2076 while (*logmsg == '\n')
2078 newline = strchr(logmsg, '\n');
2081 limit = avail - col;
2082 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2083 limit--; /* for the border */
2084 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2088 waddwstr(view->window, &wlogmsg[scrollx]);
2089 col += MAX(logmsg_width, 0);
2090 while (col < avail) {
2091 waddch(view->window, ' ');
2103 static struct commit_queue_entry *
2104 alloc_commit_queue_entry(struct got_commit_object *commit,
2105 struct got_object_id *id)
2107 struct commit_queue_entry *entry;
2108 struct got_object_id *dup;
2110 entry = calloc(1, sizeof(*entry));
2114 dup = got_object_id_dup(id);
2121 entry->commit = commit;
2126 pop_commit(struct commit_queue *commits)
2128 struct commit_queue_entry *entry;
2130 entry = TAILQ_FIRST(&commits->head);
2131 TAILQ_REMOVE(&commits->head, entry, entry);
2132 got_object_commit_close(entry->commit);
2133 commits->ncommits--;
2139 free_commits(struct commit_queue *commits)
2141 while (!TAILQ_EMPTY(&commits->head))
2142 pop_commit(commits);
2145 static const struct got_error *
2146 match_commit(int *have_match, struct got_object_id *id,
2147 struct got_commit_object *commit, regex_t *regex)
2149 const struct got_error *err = NULL;
2150 regmatch_t regmatch;
2151 char *id_str = NULL, *logmsg = NULL;
2155 err = got_object_id_str(&id_str, id);
2159 err = got_object_commit_get_logmsg(&logmsg, commit);
2163 if (regexec(regex, got_object_commit_get_author(commit), 1,
2164 ®match, 0) == 0 ||
2165 regexec(regex, got_object_commit_get_committer(commit), 1,
2166 ®match, 0) == 0 ||
2167 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2168 regexec(regex, logmsg, 1, ®match, 0) == 0)
2176 static const struct got_error *
2177 queue_commits(struct tog_log_thread_args *a)
2179 const struct got_error *err = NULL;
2182 * We keep all commits open throughout the lifetime of the log
2183 * view in order to avoid having to re-fetch commits from disk
2184 * while updating the display.
2187 struct got_object_id id;
2188 struct got_commit_object *commit;
2189 struct commit_queue_entry *entry;
2190 int limit_match = 0;
2193 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2198 err = got_object_open_as_commit(&commit, a->repo, &id);
2201 entry = alloc_commit_queue_entry(commit, &id);
2202 if (entry == NULL) {
2203 err = got_error_from_errno("alloc_commit_queue_entry");
2207 errcode = pthread_mutex_lock(&tog_mutex);
2209 err = got_error_set_errno(errcode,
2210 "pthread_mutex_lock");
2214 entry->idx = a->real_commits->ncommits;
2215 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2216 a->real_commits->ncommits++;
2219 err = match_commit(&limit_match, &id, commit,
2225 struct commit_queue_entry *matched;
2227 matched = alloc_commit_queue_entry(
2228 entry->commit, entry->id);
2229 if (matched == NULL) {
2230 err = got_error_from_errno(
2231 "alloc_commit_queue_entry");
2235 err = got_object_commit_dup(&matched->commit,
2240 matched->idx = a->limit_commits->ncommits;
2241 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2243 a->limit_commits->ncommits++;
2247 * This is how we signal log_thread() that we
2248 * have found a match, and that it should be
2249 * counted as a new entry for the view.
2251 a->limit_match = limit_match;
2254 if (*a->searching == TOG_SEARCH_FORWARD &&
2255 !*a->search_next_done) {
2257 err = match_commit(&have_match, &id, commit, a->regex);
2262 if (limit_match && have_match)
2263 *a->search_next_done =
2264 TOG_SEARCH_HAVE_MORE;
2265 } else if (have_match)
2266 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2269 errcode = pthread_mutex_unlock(&tog_mutex);
2270 if (errcode && err == NULL)
2271 err = got_error_set_errno(errcode,
2272 "pthread_mutex_unlock");
2275 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2281 select_commit(struct tog_log_view_state *s)
2283 struct commit_queue_entry *entry;
2286 entry = s->first_displayed_entry;
2288 if (ncommits == s->selected) {
2289 s->selected_entry = entry;
2292 entry = TAILQ_NEXT(entry, entry);
2297 static const struct got_error *
2298 draw_commits(struct tog_view *view)
2300 const struct got_error *err = NULL;
2301 struct tog_log_view_state *s = &view->state.log;
2302 struct commit_queue_entry *entry = s->selected_entry;
2303 int limit = view->nlines;
2305 int ncommits, author_cols = 4;
2306 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2307 char *refs_str = NULL;
2309 struct tog_color *tc;
2310 static const size_t date_display_cols = 12;
2312 if (view_is_hsplit_top(view))
2313 --limit; /* account for border */
2315 if (s->selected_entry &&
2316 !(view->searching && view->search_next_done == 0)) {
2317 struct got_reflist_head *refs;
2318 err = got_object_id_str(&id_str, s->selected_entry->id);
2321 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2322 s->selected_entry->id);
2324 err = build_refs_str(&refs_str, refs,
2325 s->selected_entry->id, s->repo);
2331 if (s->thread_args.commits_needed == 0)
2332 halfdelay(10); /* disable fast refresh */
2334 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2335 if (asprintf(&ncommits_str, " [%d/%d] %s",
2336 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2337 (view->searching && !view->search_next_done) ?
2338 "searching..." : "loading...") == -1) {
2339 err = got_error_from_errno("asprintf");
2343 const char *search_str = NULL;
2344 const char *limit_str = NULL;
2346 if (view->searching) {
2347 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2348 search_str = "no more matches";
2349 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2350 search_str = "no matches found";
2351 else if (!view->search_next_done)
2352 search_str = "searching...";
2355 if (s->limit_view && s->commits->ncommits == 0)
2356 limit_str = "no matches found";
2358 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2359 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2360 search_str ? search_str : (refs_str ? refs_str : ""),
2361 limit_str ? limit_str : "") == -1) {
2362 err = got_error_from_errno("asprintf");
2367 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2368 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2369 "........................................",
2370 s->in_repo_path, ncommits_str) == -1) {
2371 err = got_error_from_errno("asprintf");
2375 } else if (asprintf(&header, "commit %s%s",
2376 id_str ? id_str : "........................................",
2377 ncommits_str) == -1) {
2378 err = got_error_from_errno("asprintf");
2382 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2386 werase(view->window);
2388 if (view_needs_focus_indication(view))
2389 wstandout(view->window);
2390 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2392 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2393 waddwstr(view->window, wline);
2394 while (width < view->ncols) {
2395 waddch(view->window, ' ');
2399 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2400 if (view_needs_focus_indication(view))
2401 wstandend(view->window);
2406 /* Grow author column size if necessary, and set view->maxx. */
2407 entry = s->first_displayed_entry;
2411 struct got_commit_object *c = entry->commit;
2412 char *author, *eol, *msg, *msg0;
2413 wchar_t *wauthor, *wmsg;
2415 if (ncommits >= limit - 1)
2417 if (s->use_committer)
2418 author = strdup(got_object_commit_get_committer(c));
2420 author = strdup(got_object_commit_get_author(c));
2421 if (author == NULL) {
2422 err = got_error_from_errno("strdup");
2425 err = format_author(&wauthor, &width, author, COLS,
2427 if (author_cols < width)
2428 author_cols = width;
2433 err = got_object_commit_get_logmsg(&msg0, c);
2437 while (*msg == '\n')
2439 if ((eol = strchr(msg, '\n')))
2441 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2442 date_display_cols + author_cols, 0);
2445 view->maxx = MAX(view->maxx, width);
2449 entry = TAILQ_NEXT(entry, entry);
2452 entry = s->first_displayed_entry;
2453 s->last_displayed_entry = s->first_displayed_entry;
2456 if (ncommits >= limit - 1)
2458 if (ncommits == s->selected)
2459 wstandout(view->window);
2460 err = draw_commit(view, entry->commit, entry->id,
2461 date_display_cols, author_cols);
2462 if (ncommits == s->selected)
2463 wstandend(view->window);
2467 s->last_displayed_entry = entry;
2468 entry = TAILQ_NEXT(entry, entry);
2481 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2483 struct commit_queue_entry *entry;
2486 entry = TAILQ_FIRST(&s->commits->head);
2487 if (s->first_displayed_entry == entry)
2490 entry = s->first_displayed_entry;
2491 while (entry && nscrolled < maxscroll) {
2492 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2494 s->first_displayed_entry = entry;
2500 static const struct got_error *
2501 trigger_log_thread(struct tog_view *view, int wait)
2503 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2506 halfdelay(1); /* fast refresh while loading commits */
2508 while (!ta->log_complete && !tog_thread_error &&
2509 (ta->commits_needed > 0 || ta->load_all)) {
2510 /* Wake the log thread. */
2511 errcode = pthread_cond_signal(&ta->need_commits);
2513 return got_error_set_errno(errcode,
2514 "pthread_cond_signal");
2517 * The mutex will be released while the view loop waits
2518 * in wgetch(), at which time the log thread will run.
2523 /* Display progress update in log view. */
2524 show_log_view(view);
2528 /* Wait right here while next commit is being loaded. */
2529 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2531 return got_error_set_errno(errcode,
2532 "pthread_cond_wait");
2534 /* Display progress update in log view. */
2535 show_log_view(view);
2543 static const struct got_error *
2544 request_log_commits(struct tog_view *view)
2546 struct tog_log_view_state *state = &view->state.log;
2547 const struct got_error *err = NULL;
2549 if (state->thread_args.log_complete)
2552 state->thread_args.commits_needed += view->nscrolled;
2553 err = trigger_log_thread(view, 1);
2554 view->nscrolled = 0;
2559 static const struct got_error *
2560 log_scroll_down(struct tog_view *view, int maxscroll)
2562 struct tog_log_view_state *s = &view->state.log;
2563 const struct got_error *err = NULL;
2564 struct commit_queue_entry *pentry;
2565 int nscrolled = 0, ncommits_needed;
2567 if (s->last_displayed_entry == NULL)
2570 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2571 if (s->commits->ncommits < ncommits_needed &&
2572 !s->thread_args.log_complete) {
2574 * Ask the log thread for required amount of commits.
2576 s->thread_args.commits_needed +=
2577 ncommits_needed - s->commits->ncommits;
2578 err = trigger_log_thread(view, 1);
2584 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2585 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2588 s->last_displayed_entry = pentry ?
2589 pentry : s->last_displayed_entry;;
2591 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2594 s->first_displayed_entry = pentry;
2595 } while (++nscrolled < maxscroll);
2597 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2598 view->nscrolled += nscrolled;
2600 view->nscrolled = 0;
2605 static const struct got_error *
2606 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2607 struct got_commit_object *commit, struct got_object_id *commit_id,
2608 struct tog_view *log_view, struct got_repository *repo)
2610 const struct got_error *err;
2611 struct got_object_qid *parent_id;
2612 struct tog_view *diff_view;
2614 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2615 if (diff_view == NULL)
2616 return got_error_from_errno("view_open");
2618 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2619 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2620 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2622 *new_view = diff_view;
2626 static const struct got_error *
2627 tree_view_visit_subtree(struct tog_tree_view_state *s,
2628 struct got_tree_object *subtree)
2630 struct tog_parent_tree *parent;
2632 parent = calloc(1, sizeof(*parent));
2634 return got_error_from_errno("calloc");
2636 parent->tree = s->tree;
2637 parent->first_displayed_entry = s->first_displayed_entry;
2638 parent->selected_entry = s->selected_entry;
2639 parent->selected = s->selected;
2640 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2643 s->first_displayed_entry = NULL;
2647 static const struct got_error *
2648 tree_view_walk_path(struct tog_tree_view_state *s,
2649 struct got_commit_object *commit, const char *path)
2651 const struct got_error *err = NULL;
2652 struct got_tree_object *tree = NULL;
2654 char *slash, *subpath = NULL;
2656 /* Walk the path and open corresponding tree objects. */
2659 struct got_tree_entry *te;
2660 struct got_object_id *tree_id;
2666 /* Ensure the correct subtree entry is selected. */
2667 slash = strchr(p, '/');
2669 te_name = strdup(p);
2671 te_name = strndup(p, slash - p);
2672 if (te_name == NULL) {
2673 err = got_error_from_errno("strndup");
2676 te = got_object_tree_find_entry(s->tree, te_name);
2678 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2683 s->first_displayed_entry = s->selected_entry = te;
2685 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2686 break; /* jump to this file's entry */
2688 slash = strchr(p, '/');
2690 subpath = strndup(path, slash - path);
2692 subpath = strdup(path);
2693 if (subpath == NULL) {
2694 err = got_error_from_errno("strdup");
2698 err = got_object_id_by_path(&tree_id, s->repo, commit,
2703 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2708 err = tree_view_visit_subtree(s, tree);
2710 got_object_tree_close(tree);
2724 static const struct got_error *
2725 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2726 struct commit_queue_entry *entry, const char *path,
2727 const char *head_ref_name, struct got_repository *repo)
2729 const struct got_error *err = NULL;
2730 struct tog_tree_view_state *s;
2731 struct tog_view *tree_view;
2733 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2734 if (tree_view == NULL)
2735 return got_error_from_errno("view_open");
2737 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2740 s = &tree_view->state.tree;
2742 *new_view = tree_view;
2744 if (got_path_is_root_dir(path))
2747 return tree_view_walk_path(s, entry->commit, path);
2750 static const struct got_error *
2751 block_signals_used_by_main_thread(void)
2756 if (sigemptyset(&sigset) == -1)
2757 return got_error_from_errno("sigemptyset");
2759 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2760 if (sigaddset(&sigset, SIGWINCH) == -1)
2761 return got_error_from_errno("sigaddset");
2762 if (sigaddset(&sigset, SIGCONT) == -1)
2763 return got_error_from_errno("sigaddset");
2764 if (sigaddset(&sigset, SIGINT) == -1)
2765 return got_error_from_errno("sigaddset");
2766 if (sigaddset(&sigset, SIGTERM) == -1)
2767 return got_error_from_errno("sigaddset");
2769 /* ncurses handles SIGTSTP */
2770 if (sigaddset(&sigset, SIGTSTP) == -1)
2771 return got_error_from_errno("sigaddset");
2773 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2775 return got_error_set_errno(errcode, "pthread_sigmask");
2781 log_thread(void *arg)
2783 const struct got_error *err = NULL;
2785 struct tog_log_thread_args *a = arg;
2789 * Sync startup with main thread such that we begin our
2790 * work once view_input() has released the mutex.
2792 errcode = pthread_mutex_lock(&tog_mutex);
2794 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2798 err = block_signals_used_by_main_thread();
2800 pthread_mutex_unlock(&tog_mutex);
2804 while (!done && !err && !tog_fatal_signal_received()) {
2805 errcode = pthread_mutex_unlock(&tog_mutex);
2807 err = got_error_set_errno(errcode,
2808 "pthread_mutex_unlock");
2811 err = queue_commits(a);
2813 if (err->code != GOT_ERR_ITER_COMPLETED)
2817 } else if (a->commits_needed > 0 && !a->load_all) {
2820 a->commits_needed--;
2822 a->commits_needed--;
2825 errcode = pthread_mutex_lock(&tog_mutex);
2827 err = got_error_set_errno(errcode,
2828 "pthread_mutex_lock");
2830 } else if (*a->quit)
2832 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2833 *a->first_displayed_entry =
2834 TAILQ_FIRST(&a->limit_commits->head);
2835 *a->selected_entry = *a->first_displayed_entry;
2836 } else if (*a->first_displayed_entry == NULL) {
2837 *a->first_displayed_entry =
2838 TAILQ_FIRST(&a->real_commits->head);
2839 *a->selected_entry = *a->first_displayed_entry;
2842 errcode = pthread_cond_signal(&a->commit_loaded);
2844 err = got_error_set_errno(errcode,
2845 "pthread_cond_signal");
2846 pthread_mutex_unlock(&tog_mutex);
2851 a->commits_needed = 0;
2853 if (a->commits_needed == 0 && !a->load_all) {
2854 errcode = pthread_cond_wait(&a->need_commits,
2857 err = got_error_set_errno(errcode,
2858 "pthread_cond_wait");
2859 pthread_mutex_unlock(&tog_mutex);
2867 a->log_complete = 1;
2868 errcode = pthread_mutex_unlock(&tog_mutex);
2870 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2873 tog_thread_error = 1;
2874 pthread_cond_signal(&a->commit_loaded);
2879 static const struct got_error *
2880 stop_log_thread(struct tog_log_view_state *s)
2882 const struct got_error *err = NULL, *thread_err = NULL;
2887 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2889 return got_error_set_errno(errcode,
2890 "pthread_cond_signal");
2891 errcode = pthread_mutex_unlock(&tog_mutex);
2893 return got_error_set_errno(errcode,
2894 "pthread_mutex_unlock");
2895 errcode = pthread_join(s->thread, (void **)&thread_err);
2897 return got_error_set_errno(errcode, "pthread_join");
2898 errcode = pthread_mutex_lock(&tog_mutex);
2900 return got_error_set_errno(errcode,
2901 "pthread_mutex_lock");
2902 s->thread = 0; //NULL;
2905 if (s->thread_args.repo) {
2906 err = got_repo_close(s->thread_args.repo);
2907 s->thread_args.repo = NULL;
2910 if (s->thread_args.pack_fds) {
2911 const struct got_error *pack_err =
2912 got_repo_pack_fds_close(s->thread_args.pack_fds);
2915 s->thread_args.pack_fds = NULL;
2918 if (s->thread_args.graph) {
2919 got_commit_graph_close(s->thread_args.graph);
2920 s->thread_args.graph = NULL;
2923 return err ? err : thread_err;
2926 static const struct got_error *
2927 close_log_view(struct tog_view *view)
2929 const struct got_error *err = NULL;
2930 struct tog_log_view_state *s = &view->state.log;
2933 err = stop_log_thread(s);
2935 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2936 if (errcode && err == NULL)
2937 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2939 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2940 if (errcode && err == NULL)
2941 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2943 free_commits(&s->limit_commits);
2944 free_commits(&s->real_commits);
2945 free(s->in_repo_path);
2946 s->in_repo_path = NULL;
2949 free(s->head_ref_name);
2950 s->head_ref_name = NULL;
2955 * We use two queues to implement the limit feature: first consists of
2956 * commits matching the current limit_regex; second is the real queue
2957 * of all known commits (real_commits). When the user starts limiting,
2958 * we swap queues such that all movement and displaying functionality
2959 * works with very slight change.
2961 static const struct got_error *
2962 limit_log_view(struct tog_view *view)
2964 struct tog_log_view_state *s = &view->state.log;
2965 struct commit_queue_entry *entry;
2966 struct tog_view *v = view;
2967 const struct got_error *err = NULL;
2971 if (view_is_hsplit_top(view))
2973 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
2976 /* Get the pattern */
2977 wmove(v->window, v->nlines - 1, 0);
2978 wclrtoeol(v->window);
2979 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
2980 nodelay(v->window, FALSE);
2983 ret = wgetnstr(v->window, pattern, sizeof(pattern));
2986 nodelay(v->window, TRUE);
2990 if (*pattern == '\0') {
2992 * Safety measure for the situation where the user
2993 * resets limit without previously limiting anything.
2999 * User could have pressed Ctrl+L, which refreshed the
3000 * commit queues, it means we can't save previously
3001 * (before limit took place) displayed entries,
3002 * because they would point to already free'ed memory,
3003 * so we are forced to always select first entry of
3006 s->commits = &s->real_commits;
3007 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3008 s->selected_entry = s->first_displayed_entry;
3015 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3020 /* Clear the screen while loading limit view */
3021 s->first_displayed_entry = NULL;
3022 s->last_displayed_entry = NULL;
3023 s->selected_entry = NULL;
3024 s->commits = &s->limit_commits;
3026 /* Prepare limit queue for new search */
3027 free_commits(&s->limit_commits);
3028 s->limit_commits.ncommits = 0;
3030 /* First process commits, which are in queue already */
3031 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3034 err = match_commit(&have_match, entry->id,
3035 entry->commit, &s->limit_regex);
3040 struct commit_queue_entry *matched;
3042 matched = alloc_commit_queue_entry(entry->commit,
3044 if (matched == NULL) {
3045 err = got_error_from_errno(
3046 "alloc_commit_queue_entry");
3050 err = got_object_commit_dup(&matched->commit,
3057 matched->idx = s->limit_commits.ncommits;
3058 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3060 s->limit_commits.ncommits++;
3064 /* Second process all the commits, until we fill the screen */
3065 if (s->limit_commits.ncommits < view->nlines - 1 &&
3066 !s->thread_args.log_complete) {
3067 s->thread_args.commits_needed +=
3068 view->nlines - s->limit_commits.ncommits - 1;
3069 err = trigger_log_thread(view, 1);
3074 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3075 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3081 static const struct got_error *
3082 search_start_log_view(struct tog_view *view)
3084 struct tog_log_view_state *s = &view->state.log;
3086 s->matched_entry = NULL;
3087 s->search_entry = NULL;
3091 static const struct got_error *
3092 search_next_log_view(struct tog_view *view)
3094 const struct got_error *err = NULL;
3095 struct tog_log_view_state *s = &view->state.log;
3096 struct commit_queue_entry *entry;
3098 /* Display progress update in log view. */
3099 show_log_view(view);
3103 if (s->search_entry) {
3105 errcode = pthread_mutex_unlock(&tog_mutex);
3107 return got_error_set_errno(errcode,
3108 "pthread_mutex_unlock");
3109 ch = wgetch(view->window);
3110 errcode = pthread_mutex_lock(&tog_mutex);
3112 return got_error_set_errno(errcode,
3113 "pthread_mutex_lock");
3114 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3115 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3118 if (view->searching == TOG_SEARCH_FORWARD)
3119 entry = TAILQ_NEXT(s->search_entry, entry);
3121 entry = TAILQ_PREV(s->search_entry,
3122 commit_queue_head, entry);
3123 } else if (s->matched_entry) {
3125 * If the user has moved the cursor after we hit a match,
3126 * the position from where we should continue searching
3127 * might have changed.
3129 if (view->searching == TOG_SEARCH_FORWARD)
3130 entry = TAILQ_NEXT(s->selected_entry, entry);
3132 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3135 entry = s->selected_entry;
3141 if (entry == NULL) {
3142 if (s->thread_args.log_complete ||
3143 view->searching == TOG_SEARCH_BACKWARD) {
3144 view->search_next_done =
3145 (s->matched_entry == NULL ?
3146 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3147 s->search_entry = NULL;
3151 * Poke the log thread for more commits and return,
3152 * allowing the main loop to make progress. Search
3153 * will resume at s->search_entry once we come back.
3155 s->thread_args.commits_needed++;
3156 return trigger_log_thread(view, 0);
3159 err = match_commit(&have_match, entry->id, entry->commit,
3164 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3165 s->matched_entry = entry;
3169 s->search_entry = entry;
3170 if (view->searching == TOG_SEARCH_FORWARD)
3171 entry = TAILQ_NEXT(entry, entry);
3173 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3176 if (s->matched_entry) {
3177 int cur = s->selected_entry->idx;
3178 while (cur < s->matched_entry->idx) {
3179 err = input_log_view(NULL, view, KEY_DOWN);
3184 while (cur > s->matched_entry->idx) {
3185 err = input_log_view(NULL, view, KEY_UP);
3192 s->search_entry = NULL;
3197 static const struct got_error *
3198 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3199 struct got_repository *repo, const char *head_ref_name,
3200 const char *in_repo_path, int log_branches)
3202 const struct got_error *err = NULL;
3203 struct tog_log_view_state *s = &view->state.log;
3204 struct got_repository *thread_repo = NULL;
3205 struct got_commit_graph *thread_graph = NULL;
3208 if (in_repo_path != s->in_repo_path) {
3209 free(s->in_repo_path);
3210 s->in_repo_path = strdup(in_repo_path);
3211 if (s->in_repo_path == NULL)
3212 return got_error_from_errno("strdup");
3215 /* The commit queue only contains commits being displayed. */
3216 TAILQ_INIT(&s->real_commits.head);
3217 s->real_commits.ncommits = 0;
3218 s->commits = &s->real_commits;
3220 TAILQ_INIT(&s->limit_commits.head);
3222 s->limit_commits.ncommits = 0;
3225 if (head_ref_name) {
3226 s->head_ref_name = strdup(head_ref_name);
3227 if (s->head_ref_name == NULL) {
3228 err = got_error_from_errno("strdup");
3232 s->start_id = got_object_id_dup(start_id);
3233 if (s->start_id == NULL) {
3234 err = got_error_from_errno("got_object_id_dup");
3237 s->log_branches = log_branches;
3239 STAILQ_INIT(&s->colors);
3240 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3241 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3242 get_color_value("TOG_COLOR_COMMIT"));
3245 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3246 get_color_value("TOG_COLOR_AUTHOR"));
3248 free_colors(&s->colors);
3251 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3252 get_color_value("TOG_COLOR_DATE"));
3254 free_colors(&s->colors);
3259 view->show = show_log_view;
3260 view->input = input_log_view;
3261 view->resize = resize_log_view;
3262 view->close = close_log_view;
3263 view->search_start = search_start_log_view;
3264 view->search_next = search_next_log_view;
3266 if (s->thread_args.pack_fds == NULL) {
3267 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3271 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3272 s->thread_args.pack_fds);
3275 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3279 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3280 s->repo, NULL, NULL);
3284 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3286 err = got_error_set_errno(errcode, "pthread_cond_init");
3289 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3291 err = got_error_set_errno(errcode, "pthread_cond_init");
3295 s->thread_args.commits_needed = view->nlines;
3296 s->thread_args.graph = thread_graph;
3297 s->thread_args.real_commits = &s->real_commits;
3298 s->thread_args.limit_commits = &s->limit_commits;
3299 s->thread_args.in_repo_path = s->in_repo_path;
3300 s->thread_args.start_id = s->start_id;
3301 s->thread_args.repo = thread_repo;
3302 s->thread_args.log_complete = 0;
3303 s->thread_args.quit = &s->quit;
3304 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3305 s->thread_args.selected_entry = &s->selected_entry;
3306 s->thread_args.searching = &view->searching;
3307 s->thread_args.search_next_done = &view->search_next_done;
3308 s->thread_args.regex = &view->regex;
3309 s->thread_args.limiting = &s->limit_view;
3310 s->thread_args.limit_regex = &s->limit_regex;
3311 s->thread_args.limit_commits = &s->limit_commits;
3314 close_log_view(view);
3318 static const struct got_error *
3319 show_log_view(struct tog_view *view)
3321 const struct got_error *err;
3322 struct tog_log_view_state *s = &view->state.log;
3324 if (s->thread == 0) { //NULL) {
3325 int errcode = pthread_create(&s->thread, NULL, log_thread,
3328 return got_error_set_errno(errcode, "pthread_create");
3329 if (s->thread_args.commits_needed > 0) {
3330 err = trigger_log_thread(view, 1);
3336 return draw_commits(view);
3340 log_move_cursor_up(struct tog_view *view, int page, int home)
3342 struct tog_log_view_state *s = &view->state.log;
3344 if (s->first_displayed_entry == NULL)
3346 if (s->selected_entry->idx == 0)
3349 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3351 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3353 if (!page && !home && s->selected > 0)
3356 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3362 static const struct got_error *
3363 log_move_cursor_down(struct tog_view *view, int page)
3365 struct tog_log_view_state *s = &view->state.log;
3366 const struct got_error *err = NULL;
3367 int eos = view->nlines - 2;
3369 if (s->first_displayed_entry == NULL)
3372 if (s->thread_args.log_complete &&
3373 s->selected_entry->idx >= s->commits->ncommits - 1)
3376 if (view_is_hsplit_top(view))
3377 --eos; /* border consumes the last line */
3380 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3383 err = log_scroll_down(view, 1);
3384 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3385 struct commit_queue_entry *entry;
3389 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3390 s->last_displayed_entry = entry;
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;
3400 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3401 s->thread_args.log_complete)
3402 s->selected += MIN(page,
3403 s->commits->ncommits - s->selected_entry->idx - 1);
3405 err = log_scroll_down(view, page);
3411 * We might necessarily overshoot in horizontal
3412 * splits; if so, select the last displayed commit.
3414 if (s->first_displayed_entry && s->last_displayed_entry) {
3415 s->selected = MIN(s->selected,
3416 s->last_displayed_entry->idx -
3417 s->first_displayed_entry->idx);
3422 if (s->thread_args.log_complete &&
3423 s->selected_entry->idx == s->commits->ncommits - 1)
3430 view_get_split(struct tog_view *view, int *y, int *x)
3435 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3436 if (view->child && view->child->resized_y)
3437 *y = view->child->resized_y;
3438 else if (view->resized_y)
3439 *y = view->resized_y;
3441 *y = view_split_begin_y(view->lines);
3442 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3443 if (view->child && view->child->resized_x)
3444 *x = view->child->resized_x;
3445 else if (view->resized_x)
3446 *x = view->resized_x;
3448 *x = view_split_begin_x(view->begin_x);
3452 /* Split view horizontally at y and offset view->state->selected line. */
3453 static const struct got_error *
3454 view_init_hsplit(struct tog_view *view, int y)
3456 const struct got_error *err = NULL;
3460 err = view_resize(view);
3464 err = offset_selection_down(view);
3469 static const struct got_error *
3470 log_goto_line(struct tog_view *view, int nlines)
3472 const struct got_error *err = NULL;
3473 struct tog_log_view_state *s = &view->state.log;
3474 int g, idx = s->selected_entry->idx;
3476 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3482 if (g >= s->first_displayed_entry->idx + 1 &&
3483 g <= s->last_displayed_entry->idx + 1 &&
3484 g - s->first_displayed_entry->idx - 1 < nlines) {
3485 s->selected = g - s->first_displayed_entry->idx - 1;
3491 err = log_move_cursor_down(view, g - idx - 1);
3492 if (!err && g > s->selected_entry->idx + 1)
3493 err = log_move_cursor_down(view,
3494 g - s->first_displayed_entry->idx - 1);
3497 } else if (idx + 1 > g)
3498 log_move_cursor_up(view, idx - g + 1, 0);
3500 if (g < nlines && s->first_displayed_entry->idx == 0)
3501 s->selected = g - 1;
3508 static const struct got_error *
3509 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3511 const struct got_error *err = NULL;
3512 struct tog_log_view_state *s = &view->state.log;
3515 if (s->thread_args.load_all) {
3516 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3517 s->thread_args.load_all = 0;
3518 else if (s->thread_args.log_complete) {
3519 err = log_move_cursor_down(view, s->commits->ncommits);
3520 s->thread_args.load_all = 0;
3526 eos = nscroll = view->nlines - 1;
3527 if (view_is_hsplit_top(view))
3531 return log_goto_line(view, eos);
3535 err = limit_log_view(view);
3544 view->x = MAX(view->maxx - view->ncols / 2, 0);
3549 if (view->x + view->ncols / 2 < view->maxx)
3550 view->x += 2; /* move two columns right */
3556 view->x -= MIN(view->x, 2); /* move two columns back */
3565 log_move_cursor_up(view, 0, 0);
3569 log_move_cursor_up(view, 0, 1);
3579 log_move_cursor_up(view, nscroll, 0);
3586 err = log_move_cursor_down(view, 0);
3589 s->use_committer = !s->use_committer;
3593 /* We don't know yet how many commits, so we're forced to
3594 * traverse them all. */
3596 s->thread_args.load_all = 1;
3597 if (!s->thread_args.log_complete)
3598 return trigger_log_thread(view, 0);
3599 err = log_move_cursor_down(view, s->commits->ncommits);
3600 s->thread_args.load_all = 0;
3611 err = log_move_cursor_down(view, nscroll);
3614 if (s->selected > view->nlines - 2)
3615 s->selected = view->nlines - 2;
3616 if (s->selected > s->commits->ncommits - 1)
3617 s->selected = s->commits->ncommits - 1;
3619 if (s->commits->ncommits < view->nlines - 1 &&
3620 !s->thread_args.log_complete) {
3621 s->thread_args.commits_needed += (view->nlines - 1) -
3622 s->commits->ncommits;
3623 err = trigger_log_thread(view, 1);
3629 if (s->selected_entry == NULL)
3631 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3635 if (s->selected_entry == NULL)
3637 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3643 if (ch == KEY_BACKSPACE &&
3644 got_path_is_root_dir(s->in_repo_path))
3646 err = stop_log_thread(s);
3649 if (ch == KEY_BACKSPACE) {
3651 err = got_path_dirname(&parent_path, s->in_repo_path);
3654 free(s->in_repo_path);
3655 s->in_repo_path = parent_path;
3656 s->thread_args.in_repo_path = s->in_repo_path;
3657 } else if (ch == CTRL('l')) {
3658 struct got_object_id *start_id;
3659 err = got_repo_match_object_id(&start_id, NULL,
3660 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3661 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3665 s->start_id = start_id;
3666 s->thread_args.start_id = s->start_id;
3668 s->log_branches = !s->log_branches;
3670 if (s->thread_args.pack_fds == NULL) {
3671 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3675 err = got_repo_open(&s->thread_args.repo,
3676 got_repo_get_path(s->repo), NULL,
3677 s->thread_args.pack_fds);
3681 err = tog_load_refs(s->repo, 0);
3684 err = got_commit_graph_open(&s->thread_args.graph,
3685 s->in_repo_path, !s->log_branches);
3688 err = got_commit_graph_iter_start(s->thread_args.graph,
3689 s->start_id, s->repo, NULL, NULL);
3692 free_commits(&s->real_commits);
3693 free_commits(&s->limit_commits);
3694 s->first_displayed_entry = NULL;
3695 s->last_displayed_entry = NULL;
3696 s->selected_entry = NULL;
3698 s->thread_args.log_complete = 0;
3700 s->thread_args.commits_needed = view->lines;
3701 s->matched_entry = NULL;
3702 s->search_entry = NULL;
3707 err = view_request_new(new_view, view, TOG_VIEW_REF);
3717 static const struct got_error *
3718 apply_unveil(const char *repo_path, const char *worktree_path)
3720 const struct got_error *error;
3723 if (unveil("gmon.out", "rwc") != 0)
3724 return got_error_from_errno2("unveil", "gmon.out");
3726 if (repo_path && unveil(repo_path, "r") != 0)
3727 return got_error_from_errno2("unveil", repo_path);
3729 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3730 return got_error_from_errno2("unveil", worktree_path);
3732 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3733 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3735 error = got_privsep_unveil_exec_helpers();
3739 if (unveil(NULL, NULL) != 0)
3740 return got_error_from_errno("unveil");
3749 * Override default signal handlers before starting ncurses.
3750 * This should prevent ncurses from installing its own
3751 * broken cleanup() signal handler.
3753 signal(SIGWINCH, tog_sigwinch);
3754 signal(SIGPIPE, tog_sigpipe);
3755 signal(SIGCONT, tog_sigcont);
3756 signal(SIGINT, tog_sigint);
3757 signal(SIGTERM, tog_sigterm);
3761 halfdelay(1); /* Do fast refresh while initial view is loading. */
3764 intrflush(stdscr, FALSE);
3765 keypad(stdscr, TRUE);
3767 if (getenv("TOG_COLORS") != NULL) {
3769 use_default_colors();
3773 static const struct got_error *
3774 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3775 struct got_repository *repo, struct got_worktree *worktree)
3777 const struct got_error *err = NULL;
3780 *in_repo_path = strdup("/");
3781 if (*in_repo_path == NULL)
3782 return got_error_from_errno("strdup");
3787 const char *prefix = got_worktree_get_path_prefix(worktree);
3790 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3793 if (asprintf(in_repo_path, "%s%s%s", prefix,
3794 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3796 err = got_error_from_errno("asprintf");
3797 *in_repo_path = NULL;
3801 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3806 static const struct got_error *
3807 cmd_log(int argc, char *argv[])
3809 const struct got_error *error;
3810 struct got_repository *repo = NULL;
3811 struct got_worktree *worktree = NULL;
3812 struct got_object_id *start_id = NULL;
3813 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3814 char *start_commit = NULL, *label = NULL;
3815 struct got_reference *ref = NULL;
3816 const char *head_ref_name = NULL;
3817 int ch, log_branches = 0;
3818 struct tog_view *view;
3819 int *pack_fds = NULL;
3821 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3827 start_commit = optarg;
3830 repo_path = realpath(optarg, NULL);
3831 if (repo_path == NULL)
3832 return got_error_from_errno2("realpath",
3847 error = got_repo_pack_fds_open(&pack_fds);
3851 if (repo_path == NULL) {
3852 cwd = getcwd(NULL, 0);
3854 return got_error_from_errno("getcwd");
3855 error = got_worktree_open(&worktree, cwd);
3856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3860 strdup(got_worktree_get_repo_path(worktree));
3862 repo_path = strdup(cwd);
3863 if (repo_path == NULL) {
3864 error = got_error_from_errno("strdup");
3869 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3873 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3880 error = apply_unveil(got_repo_get_path(repo),
3881 worktree ? got_worktree_get_root_path(worktree) : NULL);
3885 /* already loaded by tog_log_with_path()? */
3886 if (TAILQ_EMPTY(&tog_refs)) {
3887 error = tog_load_refs(repo, 0);
3892 if (start_commit == NULL) {
3893 error = got_repo_match_object_id(&start_id, &label,
3894 worktree ? got_worktree_get_head_ref_name(worktree) :
3895 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3898 head_ref_name = label;
3900 error = got_ref_open(&ref, repo, start_commit, 0);
3902 head_ref_name = got_ref_get_name(ref);
3903 else if (error->code != GOT_ERR_NOT_REF)
3905 error = got_repo_match_object_id(&start_id, NULL,
3906 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3911 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3913 error = got_error_from_errno("view_open");
3916 error = open_log_view(view, start_id, repo, head_ref_name,
3917 in_repo_path, log_branches);
3921 /* Release work tree lock. */
3922 got_worktree_close(worktree);
3925 error = view_loop(view);
3935 const struct got_error *close_err = got_repo_close(repo);
3940 got_worktree_close(worktree);
3942 const struct got_error *pack_err =
3943 got_repo_pack_fds_close(pack_fds);
3955 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3956 "object1 object2\n", getprogname());
3961 match_line(const char *line, regex_t *regex, size_t nmatch,
3962 regmatch_t *regmatch)
3964 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3967 static struct tog_color *
3968 match_color(struct tog_colors *colors, const char *line)
3970 struct tog_color *tc = NULL;
3972 STAILQ_FOREACH(tc, colors, entry) {
3973 if (match_line(line, &tc->regex, 0, NULL))
3980 static const struct got_error *
3981 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3982 WINDOW *window, int skipcol, regmatch_t *regmatch)
3984 const struct got_error *err = NULL;
3986 wchar_t *wline = NULL;
3987 int rme, rms, n, width, scrollx;
3988 int width0 = 0, width1 = 0, width2 = 0;
3989 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3993 rms = regmatch->rm_so;
3994 rme = regmatch->rm_eo;
3996 err = expand_tab(&exstr, line);
4000 /* Split the line into 3 segments, according to match offsets. */
4001 seg0 = strndup(exstr, rms);
4003 err = got_error_from_errno("strndup");
4006 seg1 = strndup(exstr + rms, rme - rms);
4008 err = got_error_from_errno("strndup");
4011 seg2 = strdup(exstr + rme);
4013 err = got_error_from_errno("strndup");
4017 /* draw up to matched token if we haven't scrolled past it */
4018 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4022 n = MAX(width0 - skipcol, 0);
4025 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4026 wlimit, col_tab_align, 1);
4029 waddwstr(window, &wline[scrollx]);
4039 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4043 wlen = wcslen(wline);
4045 width = wcwidth(wline[i]);
4047 /* should not happen, tabs are expanded */
4048 err = got_error(GOT_ERR_RANGE);
4051 if (width0 + w + width > skipcol)
4056 /* draw (visible part of) matched token (if scrolled into it) */
4057 if (width1 - w > 0) {
4058 wattron(window, A_STANDOUT);
4059 waddwstr(window, &wline[i]);
4060 wattroff(window, A_STANDOUT);
4061 wlimit -= (width1 - w);
4062 *wtotal += (width1 - w);
4066 if (wlimit > 0) { /* draw rest of line */
4068 if (skipcol > width0 + width1) {
4069 err = format_line(&wline, &width2, &scrollx, seg2,
4070 skipcol - (width0 + width1), wlimit,
4074 waddwstr(window, &wline[scrollx]);
4076 err = format_line(&wline, &width2, NULL, seg2, 0,
4077 wlimit, col_tab_align, 1);
4080 waddwstr(window, wline);
4094 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4097 int *eof, *first, *selected;
4099 if (view->type == TOG_VIEW_DIFF) {
4100 struct tog_diff_view_state *s = &view->state.diff;
4102 first = &s->first_displayed_line;
4106 } else if (view->type == TOG_VIEW_BLAME) {
4107 struct tog_blame_view_state *s = &view->state.blame;
4109 first = &s->first_displayed_line;
4110 selected = &s->selected_line;
4116 /* Center gline in the middle of the page like vi(1). */
4117 if (*lineno < view->gline - (view->nlines - 3) / 2)
4119 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4128 *selected = view->gline <= (view->nlines - 3) / 2 ?
4129 view->gline : (view->nlines - 3) / 2 + 1;
4135 static const struct got_error *
4136 draw_file(struct tog_view *view, const char *header)
4138 struct tog_diff_view_state *s = &view->state.diff;
4139 regmatch_t *regmatch = &view->regmatch;
4140 const struct got_error *err;
4143 size_t linesize = 0;
4147 int max_lines = view->nlines;
4148 int nlines = s->nlines;
4151 s->lineno = s->first_displayed_line - 1;
4152 line_offset = s->lines[s->first_displayed_line - 1].offset;
4153 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4154 return got_error_from_errno("fseek");
4156 werase(view->window);
4158 if (view->gline > s->nlines - 1)
4159 view->gline = s->nlines - 1;
4162 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4163 1 : view->gline - (view->nlines - 3) / 2 :
4164 s->lineno + s->selected_line;
4166 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4167 return got_error_from_errno("asprintf");
4168 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4174 if (view_needs_focus_indication(view))
4175 wstandout(view->window);
4176 waddwstr(view->window, wline);
4179 while (width++ < view->ncols)
4180 waddch(view->window, ' ');
4181 if (view_needs_focus_indication(view))
4182 wstandend(view->window);
4192 while (max_lines > 0 && nprinted < max_lines) {
4193 enum got_diff_line_type linetype;
4196 linelen = getline(&line, &linesize, s->f);
4197 if (linelen == -1) {
4203 return got_ferror(s->f, GOT_ERR_IO);
4206 if (++s->lineno < s->first_displayed_line)
4208 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4210 if (s->lineno == view->hiline)
4213 /* Set view->maxx based on full line length. */
4214 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4220 view->maxx = MAX(view->maxx, width);
4224 linetype = s->lines[s->lineno].type;
4225 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4226 linetype < GOT_DIFF_LINE_CONTEXT)
4227 attr |= COLOR_PAIR(linetype);
4229 wattron(view->window, attr);
4230 if (s->first_displayed_line + nprinted == s->matched_line &&
4231 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4232 err = add_matched_line(&width, line, view->ncols, 0,
4233 view->window, view->x, regmatch);
4240 err = format_line(&wline, &width, &skip, line,
4241 view->x, view->ncols, 0, view->x ? 1 : 0);
4246 waddwstr(view->window, &wline[skip]);
4250 if (s->lineno == view->hiline) {
4251 /* highlight full gline length */
4252 while (width++ < view->ncols)
4253 waddch(view->window, ' ');
4255 if (width <= view->ncols - 1)
4256 waddch(view->window, '\n');
4259 wattroff(view->window, attr);
4260 if (++nprinted == 1)
4261 s->first_displayed_line = s->lineno;
4265 s->last_displayed_line = s->first_displayed_line +
4268 s->last_displayed_line = s->first_displayed_line;
4273 while (nprinted < view->nlines) {
4274 waddch(view->window, '\n');
4278 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4284 wstandout(view->window);
4285 waddwstr(view->window, wline);
4288 wstandend(view->window);
4295 get_datestr(time_t *time, char *datebuf)
4297 struct tm mytm, *tm;
4300 tm = gmtime_r(time, &mytm);
4303 s = asctime_r(tm, datebuf);
4306 p = strchr(s, '\n');
4312 static const struct got_error *
4313 get_changed_paths(struct got_pathlist_head *paths,
4314 struct got_commit_object *commit, struct got_repository *repo)
4316 const struct got_error *err = NULL;
4317 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4318 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4319 struct got_object_qid *qid;
4321 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4323 struct got_commit_object *pcommit;
4324 err = got_object_open_as_commit(&pcommit, repo,
4329 tree_id1 = got_object_id_dup(
4330 got_object_commit_get_tree_id(pcommit));
4331 if (tree_id1 == NULL) {
4332 got_object_commit_close(pcommit);
4333 return got_error_from_errno("got_object_id_dup");
4335 got_object_commit_close(pcommit);
4340 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4345 tree_id2 = got_object_commit_get_tree_id(commit);
4346 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4350 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4351 got_diff_tree_collect_changed_paths, paths, 0);
4354 got_object_tree_close(tree1);
4356 got_object_tree_close(tree2);
4361 static const struct got_error *
4362 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4363 off_t off, uint8_t type)
4365 struct got_diff_line *p;
4367 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4369 return got_error_from_errno("reallocarray");
4371 (*lines)[*nlines].offset = off;
4372 (*lines)[*nlines].type = type;
4378 static const struct got_error *
4379 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4380 struct got_object_id *commit_id, struct got_reflist_head *refs,
4381 struct got_repository *repo, FILE *outfile)
4383 const struct got_error *err = NULL;
4384 char datebuf[26], *datestr;
4385 struct got_commit_object *commit;
4386 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4387 time_t committer_time;
4388 const char *author, *committer;
4389 char *refs_str = NULL;
4390 struct got_pathlist_head changed_paths;
4391 struct got_pathlist_entry *pe;
4395 TAILQ_INIT(&changed_paths);
4398 err = build_refs_str(&refs_str, refs, commit_id, repo);
4403 err = got_object_open_as_commit(&commit, repo, commit_id);
4407 err = got_object_id_str(&id_str, commit_id);
4409 err = got_error_from_errno("got_object_id_str");
4413 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4417 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4418 refs_str ? refs_str : "", refs_str ? ")" : "");
4420 err = got_error_from_errno("fprintf");
4424 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4428 n = fprintf(outfile, "from: %s\n",
4429 got_object_commit_get_author(commit));
4431 err = got_error_from_errno("fprintf");
4435 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4439 committer_time = got_object_commit_get_committer_time(commit);
4440 datestr = get_datestr(&committer_time, datebuf);
4442 n = fprintf(outfile, "date: %s UTC\n", datestr);
4444 err = got_error_from_errno("fprintf");
4448 err = add_line_metadata(lines, nlines, outoff,
4449 GOT_DIFF_LINE_DATE);
4453 author = got_object_commit_get_author(commit);
4454 committer = got_object_commit_get_committer(commit);
4455 if (strcmp(author, committer) != 0) {
4456 n = fprintf(outfile, "via: %s\n", committer);
4458 err = got_error_from_errno("fprintf");
4462 err = add_line_metadata(lines, nlines, outoff,
4463 GOT_DIFF_LINE_AUTHOR);
4467 if (got_object_commit_get_nparents(commit) > 1) {
4468 const struct got_object_id_queue *parent_ids;
4469 struct got_object_qid *qid;
4471 parent_ids = got_object_commit_get_parent_ids(commit);
4472 STAILQ_FOREACH(qid, parent_ids, entry) {
4473 err = got_object_id_str(&id_str, &qid->id);
4476 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4478 err = got_error_from_errno("fprintf");
4482 err = add_line_metadata(lines, nlines, outoff,
4483 GOT_DIFF_LINE_META);
4491 err = got_object_commit_get_logmsg(&logmsg, commit);
4495 while ((line = strsep(&s, "\n")) != NULL) {
4496 n = fprintf(outfile, "%s\n", line);
4498 err = got_error_from_errno("fprintf");
4502 err = add_line_metadata(lines, nlines, outoff,
4503 GOT_DIFF_LINE_LOGMSG);
4508 err = get_changed_paths(&changed_paths, commit, repo);
4511 TAILQ_FOREACH(pe, &changed_paths, entry) {
4512 struct got_diff_changed_path *cp = pe->data;
4513 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4515 err = got_error_from_errno("fprintf");
4519 err = add_line_metadata(lines, nlines, outoff,
4520 GOT_DIFF_LINE_CHANGES);
4523 free((char *)pe->path);
4527 fputc('\n', outfile);
4529 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4531 got_pathlist_free(&changed_paths);
4535 got_object_commit_close(commit);
4544 static const struct got_error *
4545 create_diff(struct tog_diff_view_state *s)
4547 const struct got_error *err = NULL;
4552 s->lines = malloc(sizeof(*s->lines));
4553 if (s->lines == NULL)
4554 return got_error_from_errno("malloc");
4559 err = got_error_from_errno("got_opentemp");
4562 if (s->f && fclose(s->f) == EOF) {
4563 err = got_error_from_errno("fclose");
4569 err = got_object_get_type(&obj_type, s->repo, s->id1);
4571 err = got_object_get_type(&obj_type, s->repo, s->id2);
4576 case GOT_OBJ_TYPE_BLOB:
4577 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4578 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4579 s->label1, s->label2, tog_diff_algo, s->diff_context,
4580 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4582 case GOT_OBJ_TYPE_TREE:
4583 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4584 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4585 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4586 s->force_text_diff, s->repo, s->f);
4588 case GOT_OBJ_TYPE_COMMIT: {
4589 const struct got_object_id_queue *parent_ids;
4590 struct got_object_qid *pid;
4591 struct got_commit_object *commit2;
4592 struct got_reflist_head *refs;
4594 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4597 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4598 /* Show commit info if we're diffing to a parent/root commit. */
4599 if (s->id1 == NULL) {
4600 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4601 refs, s->repo, s->f);
4605 parent_ids = got_object_commit_get_parent_ids(commit2);
4606 STAILQ_FOREACH(pid, parent_ids, entry) {
4607 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4608 err = write_commit_info(&s->lines,
4609 &s->nlines, s->id2, refs, s->repo,
4617 got_object_commit_close(commit2);
4619 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4620 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4621 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4622 s->force_text_diff, s->repo, s->f);
4626 err = got_error(GOT_ERR_OBJ_TYPE);
4630 if (s->f && fflush(s->f) != 0 && err == NULL)
4631 err = got_error_from_errno("fflush");
4636 diff_view_indicate_progress(struct tog_view *view)
4638 mvwaddstr(view->window, 0, 0, "diffing...");
4643 static const struct got_error *
4644 search_start_diff_view(struct tog_view *view)
4646 struct tog_diff_view_state *s = &view->state.diff;
4648 s->matched_line = 0;
4652 static const struct got_error *
4653 search_next_diff_view(struct tog_view *view)
4655 struct tog_diff_view_state *s = &view->state.diff;
4656 const struct got_error *err = NULL;
4659 size_t linesize = 0;
4662 if (!view->searching) {
4663 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4667 if (s->matched_line) {
4668 if (view->searching == TOG_SEARCH_FORWARD)
4669 lineno = s->matched_line + 1;
4671 lineno = s->matched_line - 1;
4673 lineno = s->first_displayed_line;
4678 if (lineno <= 0 || lineno > s->nlines) {
4679 if (s->matched_line == 0) {
4680 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4684 if (view->searching == TOG_SEARCH_FORWARD)
4690 offset = s->lines[lineno - 1].offset;
4691 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4693 return got_error_from_errno("fseeko");
4695 linelen = getline(&line, &linesize, s->f);
4696 if (linelen != -1) {
4698 err = expand_tab(&exstr, line);
4701 if (match_line(exstr, &view->regex, 1,
4703 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4704 s->matched_line = lineno;
4710 if (view->searching == TOG_SEARCH_FORWARD)
4717 if (s->matched_line) {
4718 s->first_displayed_line = s->matched_line;
4719 s->selected_line = 1;
4725 static const struct got_error *
4726 close_diff_view(struct tog_view *view)
4728 const struct got_error *err = NULL;
4729 struct tog_diff_view_state *s = &view->state.diff;
4735 if (s->f && fclose(s->f) == EOF)
4736 err = got_error_from_errno("fclose");
4738 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4739 err = got_error_from_errno("fclose");
4741 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4742 err = got_error_from_errno("fclose");
4744 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4745 err = got_error_from_errno("close");
4747 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4748 err = got_error_from_errno("close");
4756 static const struct got_error *
4757 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4758 struct got_object_id *id2, const char *label1, const char *label2,
4759 int diff_context, int ignore_whitespace, int force_text_diff,
4760 struct tog_view *parent_view, struct got_repository *repo)
4762 const struct got_error *err;
4763 struct tog_diff_view_state *s = &view->state.diff;
4765 memset(s, 0, sizeof(*s));
4769 if (id1 != NULL && id2 != NULL) {
4771 err = got_object_get_type(&type1, repo, id1);
4774 err = got_object_get_type(&type2, repo, id2);
4779 return got_error(GOT_ERR_OBJ_TYPE);
4781 s->first_displayed_line = 1;
4782 s->last_displayed_line = view->nlines;
4783 s->selected_line = 1;
4791 s->id1 = got_object_id_dup(id1);
4793 return got_error_from_errno("got_object_id_dup");
4797 s->id2 = got_object_id_dup(id2);
4798 if (s->id2 == NULL) {
4799 err = got_error_from_errno("got_object_id_dup");
4803 s->f1 = got_opentemp();
4804 if (s->f1 == NULL) {
4805 err = got_error_from_errno("got_opentemp");
4809 s->f2 = got_opentemp();
4810 if (s->f2 == NULL) {
4811 err = got_error_from_errno("got_opentemp");
4815 s->fd1 = got_opentempfd();
4817 err = got_error_from_errno("got_opentempfd");
4821 s->fd2 = got_opentempfd();
4823 err = got_error_from_errno("got_opentempfd");
4827 s->first_displayed_line = 1;
4828 s->last_displayed_line = view->nlines;
4829 s->diff_context = diff_context;
4830 s->ignore_whitespace = ignore_whitespace;
4831 s->force_text_diff = force_text_diff;
4832 s->parent_view = parent_view;
4835 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4838 rc = init_pair(GOT_DIFF_LINE_MINUS,
4839 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4841 rc = init_pair(GOT_DIFF_LINE_PLUS,
4842 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4844 rc = init_pair(GOT_DIFF_LINE_HUNK,
4845 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4847 rc = init_pair(GOT_DIFF_LINE_META,
4848 get_color_value("TOG_COLOR_DIFF_META"), -1);
4850 rc = init_pair(GOT_DIFF_LINE_CHANGES,
4851 get_color_value("TOG_COLOR_DIFF_META"), -1);
4853 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4854 get_color_value("TOG_COLOR_DIFF_META"), -1);
4856 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4857 get_color_value("TOG_COLOR_DIFF_META"), -1);
4859 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4860 get_color_value("TOG_COLOR_AUTHOR"), -1);
4862 rc = init_pair(GOT_DIFF_LINE_DATE,
4863 get_color_value("TOG_COLOR_DATE"), -1);
4865 err = got_error(GOT_ERR_RANGE);
4870 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4871 view_is_splitscreen(view))
4872 show_log_view(parent_view); /* draw border */
4873 diff_view_indicate_progress(view);
4875 err = create_diff(s);
4877 view->show = show_diff_view;
4878 view->input = input_diff_view;
4879 view->reset = reset_diff_view;
4880 view->close = close_diff_view;
4881 view->search_start = search_start_diff_view;
4882 view->search_next = search_next_diff_view;
4885 close_diff_view(view);
4889 static const struct got_error *
4890 show_diff_view(struct tog_view *view)
4892 const struct got_error *err;
4893 struct tog_diff_view_state *s = &view->state.diff;
4894 char *id_str1 = NULL, *id_str2, *header;
4895 const char *label1, *label2;
4898 err = got_object_id_str(&id_str1, s->id1);
4901 label1 = s->label1 ? s->label1 : id_str1;
4903 label1 = "/dev/null";
4905 err = got_object_id_str(&id_str2, s->id2);
4908 label2 = s->label2 ? s->label2 : id_str2;
4910 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4911 err = got_error_from_errno("asprintf");
4919 err = draw_file(view, header);
4924 static const struct got_error *
4925 set_selected_commit(struct tog_diff_view_state *s,
4926 struct commit_queue_entry *entry)
4928 const struct got_error *err;
4929 const struct got_object_id_queue *parent_ids;
4930 struct got_commit_object *selected_commit;
4931 struct got_object_qid *pid;
4934 s->id2 = got_object_id_dup(entry->id);
4936 return got_error_from_errno("got_object_id_dup");
4938 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4941 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4943 pid = STAILQ_FIRST(parent_ids);
4944 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4945 got_object_commit_close(selected_commit);
4949 static const struct got_error *
4950 reset_diff_view(struct tog_view *view)
4952 struct tog_diff_view_state *s = &view->state.diff;
4955 wclear(view->window);
4956 s->first_displayed_line = 1;
4957 s->last_displayed_line = view->nlines;
4958 s->matched_line = 0;
4959 diff_view_indicate_progress(view);
4960 return create_diff(s);
4964 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4968 i = start = s->first_displayed_line - 1;
4970 while (s->lines[i].type != type) {
4974 return; /* do nothing, requested type not in file */
4977 s->selected_line = 1;
4978 s->first_displayed_line = i;
4982 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4986 i = start = s->first_displayed_line + 1;
4988 while (s->lines[i].type != type) {
4989 if (i == s->nlines - 1)
4992 return; /* do nothing, requested type not in file */
4995 s->selected_line = 1;
4996 s->first_displayed_line = i;
4999 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5001 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5004 static const struct got_error *
5005 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5007 const struct got_error *err = NULL;
5008 struct tog_diff_view_state *s = &view->state.diff;
5009 struct tog_log_view_state *ls;
5010 struct commit_queue_entry *old_selected_entry;
5012 size_t linesize = 0;
5014 int i, nscroll = view->nlines - 1, up = 0;
5016 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5023 view->x = MAX(view->maxx - view->ncols / 3, 0);
5028 if (view->x + view->ncols / 3 < view->maxx)
5029 view->x += 2; /* move two columns right */
5035 view->x -= MIN(view->x, 2); /* move two columns back */
5042 s->force_text_diff = !s->force_text_diff;
5044 s->ignore_whitespace = !s->ignore_whitespace;
5045 err = reset_diff_view(view);
5049 s->first_displayed_line = 1;
5058 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5064 if (s->first_displayed_line > 1)
5065 s->first_displayed_line--;
5076 if (s->first_displayed_line == 1) {
5081 while (i++ < nscroll && s->first_displayed_line > 1)
5082 s->first_displayed_line--;
5088 s->first_displayed_line++;
5105 while (!s->eof && i++ < nscroll) {
5106 linelen = getline(&line, &linesize, s->f);
5107 s->first_displayed_line++;
5108 if (linelen == -1) {
5112 err = got_ferror(s->f, GOT_ERR_IO);
5119 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5122 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5125 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5128 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5131 if (s->diff_context > 0) {
5133 s->matched_line = 0;
5134 diff_view_indicate_progress(view);
5135 err = create_diff(s);
5136 if (s->first_displayed_line + view->nlines - 1 >
5138 s->first_displayed_line = 1;
5139 s->last_displayed_line = view->nlines;
5145 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5147 s->matched_line = 0;
5148 diff_view_indicate_progress(view);
5149 err = create_diff(s);
5161 if (s->parent_view == NULL) {
5165 s->parent_view->count = view->count;
5167 if (s->parent_view->type == TOG_VIEW_LOG) {
5168 ls = &s->parent_view->state.log;
5169 old_selected_entry = ls->selected_entry;
5171 err = input_log_view(NULL, s->parent_view,
5172 up ? KEY_UP : KEY_DOWN);
5175 view->count = s->parent_view->count;
5177 if (old_selected_entry == ls->selected_entry)
5180 err = set_selected_commit(s, ls->selected_entry);
5183 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5184 struct tog_blame_view_state *bs;
5185 struct got_object_id *id, *prev_id;
5187 bs = &s->parent_view->state.blame;
5188 prev_id = get_annotation_for_line(bs->blame.lines,
5189 bs->blame.nlines, bs->last_diffed_line);
5191 err = input_blame_view(&view, s->parent_view,
5192 up ? KEY_UP : KEY_DOWN);
5195 view->count = s->parent_view->count;
5197 if (prev_id == NULL)
5199 id = get_selected_commit_id(bs->blame.lines,
5200 bs->blame.nlines, bs->first_displayed_line,
5205 if (!got_object_id_cmp(prev_id, id))
5208 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5212 s->first_displayed_line = 1;
5213 s->last_displayed_line = view->nlines;
5214 s->matched_line = 0;
5217 diff_view_indicate_progress(view);
5218 err = create_diff(s);
5228 static const struct got_error *
5229 cmd_diff(int argc, char *argv[])
5231 const struct got_error *error = NULL;
5232 struct got_repository *repo = NULL;
5233 struct got_worktree *worktree = NULL;
5234 struct got_object_id *id1 = NULL, *id2 = NULL;
5235 char *repo_path = NULL, *cwd = NULL;
5236 char *id_str1 = NULL, *id_str2 = NULL;
5237 char *label1 = NULL, *label2 = NULL;
5238 int diff_context = 3, ignore_whitespace = 0;
5239 int ch, force_text_diff = 0;
5241 struct tog_view *view;
5242 int *pack_fds = NULL;
5244 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5247 force_text_diff = 1;
5250 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5253 errx(1, "number of context lines is %s: %s",
5257 repo_path = realpath(optarg, NULL);
5258 if (repo_path == NULL)
5259 return got_error_from_errno2("realpath",
5261 got_path_strip_trailing_slashes(repo_path);
5264 ignore_whitespace = 1;
5276 usage_diff(); /* TODO show local worktree changes */
5277 } else if (argc == 2) {
5283 error = got_repo_pack_fds_open(&pack_fds);
5287 if (repo_path == NULL) {
5288 cwd = getcwd(NULL, 0);
5290 return got_error_from_errno("getcwd");
5291 error = got_worktree_open(&worktree, cwd);
5292 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5296 strdup(got_worktree_get_repo_path(worktree));
5298 repo_path = strdup(cwd);
5299 if (repo_path == NULL) {
5300 error = got_error_from_errno("strdup");
5305 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5311 error = apply_unveil(got_repo_get_path(repo), NULL);
5315 error = tog_load_refs(repo, 0);
5319 error = got_repo_match_object_id(&id1, &label1, id_str1,
5320 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5324 error = got_repo_match_object_id(&id2, &label2, id_str2,
5325 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5329 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5331 error = got_error_from_errno("view_open");
5334 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5335 ignore_whitespace, force_text_diff, NULL, repo);
5338 error = view_loop(view);
5345 const struct got_error *close_err = got_repo_close(repo);
5350 got_worktree_close(worktree);
5352 const struct got_error *pack_err =
5353 got_repo_pack_fds_close(pack_fds);
5366 "usage: %s blame [-c commit] [-r repository-path] path\n",
5371 struct tog_blame_line {
5373 struct got_object_id *id;
5376 static const struct got_error *
5377 draw_blame(struct tog_view *view)
5379 struct tog_blame_view_state *s = &view->state.blame;
5380 struct tog_blame *blame = &s->blame;
5381 regmatch_t *regmatch = &view->regmatch;
5382 const struct got_error *err;
5383 int lineno = 0, nprinted = 0;
5385 size_t linesize = 0;
5389 struct tog_blame_line *blame_line;
5390 struct got_object_id *prev_id = NULL;
5392 struct tog_color *tc;
5394 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5399 werase(view->window);
5401 if (asprintf(&line, "commit %s", id_str) == -1) {
5402 err = got_error_from_errno("asprintf");
5407 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5412 if (view_needs_focus_indication(view))
5413 wstandout(view->window);
5414 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5416 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5417 waddwstr(view->window, wline);
5418 while (width++ < view->ncols)
5419 waddch(view->window, ' ');
5421 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5422 if (view_needs_focus_indication(view))
5423 wstandend(view->window);
5427 if (view->gline > blame->nlines)
5428 view->gline = blame->nlines;
5430 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5431 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5432 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5434 return got_error_from_errno("asprintf");
5437 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5442 waddwstr(view->window, wline);
5445 if (width < view->ncols - 1)
5446 waddch(view->window, '\n');
5450 while (nprinted < view->nlines - 2) {
5451 linelen = getline(&line, &linesize, blame->f);
5452 if (linelen == -1) {
5453 if (feof(blame->f)) {
5458 return got_ferror(blame->f, GOT_ERR_IO);
5460 if (++lineno < s->first_displayed_line)
5462 if (view->gline && !gotoline(view, &lineno, &nprinted))
5465 /* Set view->maxx based on full line length. */
5466 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5473 view->maxx = MAX(view->maxx, width);
5475 if (nprinted == s->selected_line - 1)
5476 wstandout(view->window);
5478 if (blame->nlines > 0) {
5479 blame_line = &blame->lines[lineno - 1];
5480 if (blame_line->annotated && prev_id &&
5481 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5482 !(nprinted == s->selected_line - 1)) {
5483 waddstr(view->window, " ");
5484 } else if (blame_line->annotated) {
5486 err = got_object_id_str(&id_str,
5492 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5494 wattr_on(view->window,
5495 COLOR_PAIR(tc->colorpair), NULL);
5496 wprintw(view->window, "%.8s", id_str);
5498 wattr_off(view->window,
5499 COLOR_PAIR(tc->colorpair), NULL);
5501 prev_id = blame_line->id;
5503 waddstr(view->window, "........");
5507 waddstr(view->window, "........");
5511 if (nprinted == s->selected_line - 1)
5512 wstandend(view->window);
5513 waddstr(view->window, " ");
5515 if (view->ncols <= 9) {
5517 } else if (s->first_displayed_line + nprinted ==
5519 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5520 err = add_matched_line(&width, line, view->ncols - 9, 9,
5521 view->window, view->x, regmatch);
5529 err = format_line(&wline, &width, &skip, line,
5530 view->x, view->ncols - 9, 9, 1);
5535 waddwstr(view->window, &wline[skip]);
5541 if (width <= view->ncols - 1)
5542 waddch(view->window, '\n');
5543 if (++nprinted == 1)
5544 s->first_displayed_line = lineno;
5547 s->last_displayed_line = lineno;
5554 static const struct got_error *
5555 blame_cb(void *arg, int nlines, int lineno,
5556 struct got_commit_object *commit, struct got_object_id *id)
5558 const struct got_error *err = NULL;
5559 struct tog_blame_cb_args *a = arg;
5560 struct tog_blame_line *line;
5563 if (nlines != a->nlines ||
5564 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5565 return got_error(GOT_ERR_RANGE);
5567 errcode = pthread_mutex_lock(&tog_mutex);
5569 return got_error_set_errno(errcode, "pthread_mutex_lock");
5571 if (*a->quit) { /* user has quit the blame view */
5572 err = got_error(GOT_ERR_ITER_COMPLETED);
5577 goto done; /* no change in this commit */
5579 line = &a->lines[lineno - 1];
5580 if (line->annotated)
5583 line->id = got_object_id_dup(id);
5584 if (line->id == NULL) {
5585 err = got_error_from_errno("got_object_id_dup");
5588 line->annotated = 1;
5590 errcode = pthread_mutex_unlock(&tog_mutex);
5592 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5597 blame_thread(void *arg)
5599 const struct got_error *err, *close_err;
5600 struct tog_blame_thread_args *ta = arg;
5601 struct tog_blame_cb_args *a = ta->cb_args;
5602 int errcode, fd1 = -1, fd2 = -1;
5603 FILE *f1 = NULL, *f2 = NULL;
5605 fd1 = got_opentempfd();
5607 return (void *)got_error_from_errno("got_opentempfd");
5609 fd2 = got_opentempfd();
5611 err = got_error_from_errno("got_opentempfd");
5615 f1 = got_opentemp();
5617 err = (void *)got_error_from_errno("got_opentemp");
5620 f2 = got_opentemp();
5622 err = (void *)got_error_from_errno("got_opentemp");
5626 err = block_signals_used_by_main_thread();
5630 err = got_blame(ta->path, a->commit_id, ta->repo,
5631 tog_diff_algo, blame_cb, ta->cb_args,
5632 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5633 if (err && err->code == GOT_ERR_CANCELLED)
5636 errcode = pthread_mutex_lock(&tog_mutex);
5638 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5642 close_err = got_repo_close(ta->repo);
5648 errcode = pthread_mutex_unlock(&tog_mutex);
5649 if (errcode && err == NULL)
5650 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5653 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5654 err = got_error_from_errno("close");
5655 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5656 err = got_error_from_errno("close");
5657 if (f1 && fclose(f1) == EOF && err == NULL)
5658 err = got_error_from_errno("fclose");
5659 if (f2 && fclose(f2) == EOF && err == NULL)
5660 err = got_error_from_errno("fclose");
5665 static struct got_object_id *
5666 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5667 int first_displayed_line, int selected_line)
5669 struct tog_blame_line *line;
5674 line = &lines[first_displayed_line - 1 + selected_line - 1];
5675 if (!line->annotated)
5681 static struct got_object_id *
5682 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5685 struct tog_blame_line *line;
5687 if (nlines <= 0 || lineno >= nlines)
5690 line = &lines[lineno - 1];
5691 if (!line->annotated)
5697 static const struct got_error *
5698 stop_blame(struct tog_blame *blame)
5700 const struct got_error *err = NULL;
5703 if (blame->thread) {
5705 errcode = pthread_mutex_unlock(&tog_mutex);
5707 return got_error_set_errno(errcode,
5708 "pthread_mutex_unlock");
5709 errcode = pthread_join(blame->thread, (void **)&err);
5711 return got_error_set_errno(errcode, "pthread_join");
5712 errcode = pthread_mutex_lock(&tog_mutex);
5714 return got_error_set_errno(errcode,
5715 "pthread_mutex_lock");
5716 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5718 blame->thread = 0; //NULL;
5720 if (blame->thread_args.repo) {
5721 const struct got_error *close_err;
5722 close_err = got_repo_close(blame->thread_args.repo);
5725 blame->thread_args.repo = NULL;
5728 if (fclose(blame->f) == EOF && err == NULL)
5729 err = got_error_from_errno("fclose");
5733 for (i = 0; i < blame->nlines; i++)
5734 free(blame->lines[i].id);
5736 blame->lines = NULL;
5738 free(blame->cb_args.commit_id);
5739 blame->cb_args.commit_id = NULL;
5740 if (blame->pack_fds) {
5741 const struct got_error *pack_err =
5742 got_repo_pack_fds_close(blame->pack_fds);
5745 blame->pack_fds = NULL;
5750 static const struct got_error *
5751 cancel_blame_view(void *arg)
5753 const struct got_error *err = NULL;
5757 errcode = pthread_mutex_lock(&tog_mutex);
5759 return got_error_set_errno(errcode,
5760 "pthread_mutex_unlock");
5763 err = got_error(GOT_ERR_CANCELLED);
5765 errcode = pthread_mutex_unlock(&tog_mutex);
5767 return got_error_set_errno(errcode,
5768 "pthread_mutex_lock");
5773 static const struct got_error *
5774 run_blame(struct tog_view *view)
5776 struct tog_blame_view_state *s = &view->state.blame;
5777 struct tog_blame *blame = &s->blame;
5778 const struct got_error *err = NULL;
5779 struct got_commit_object *commit = NULL;
5780 struct got_blob_object *blob = NULL;
5781 struct got_repository *thread_repo = NULL;
5782 struct got_object_id *obj_id = NULL;
5783 int obj_type, fd = -1;
5784 int *pack_fds = NULL;
5786 err = got_object_open_as_commit(&commit, s->repo,
5787 &s->blamed_commit->id);
5791 fd = got_opentempfd();
5793 err = got_error_from_errno("got_opentempfd");
5797 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5801 err = got_object_get_type(&obj_type, s->repo, obj_id);
5805 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5806 err = got_error(GOT_ERR_OBJ_TYPE);
5810 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5813 blame->f = got_opentemp();
5814 if (blame->f == NULL) {
5815 err = got_error_from_errno("got_opentemp");
5818 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5819 &blame->line_offsets, blame->f, blob);
5822 if (blame->nlines == 0) {
5823 s->blame_complete = 1;
5827 /* Don't include \n at EOF in the blame line count. */
5828 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5831 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5832 if (blame->lines == NULL) {
5833 err = got_error_from_errno("calloc");
5837 err = got_repo_pack_fds_open(&pack_fds);
5840 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5845 blame->pack_fds = pack_fds;
5846 blame->cb_args.view = view;
5847 blame->cb_args.lines = blame->lines;
5848 blame->cb_args.nlines = blame->nlines;
5849 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5850 if (blame->cb_args.commit_id == NULL) {
5851 err = got_error_from_errno("got_object_id_dup");
5854 blame->cb_args.quit = &s->done;
5856 blame->thread_args.path = s->path;
5857 blame->thread_args.repo = thread_repo;
5858 blame->thread_args.cb_args = &blame->cb_args;
5859 blame->thread_args.complete = &s->blame_complete;
5860 blame->thread_args.cancel_cb = cancel_blame_view;
5861 blame->thread_args.cancel_arg = &s->done;
5862 s->blame_complete = 0;
5864 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5865 s->first_displayed_line = 1;
5866 s->last_displayed_line = view->nlines;
5867 s->selected_line = 1;
5869 s->matched_line = 0;
5873 got_object_commit_close(commit);
5874 if (fd != -1 && close(fd) == -1 && err == NULL)
5875 err = got_error_from_errno("close");
5877 got_object_blob_close(blob);
5884 static const struct got_error *
5885 open_blame_view(struct tog_view *view, char *path,
5886 struct got_object_id *commit_id, struct got_repository *repo)
5888 const struct got_error *err = NULL;
5889 struct tog_blame_view_state *s = &view->state.blame;
5891 STAILQ_INIT(&s->blamed_commits);
5893 s->path = strdup(path);
5894 if (s->path == NULL)
5895 return got_error_from_errno("strdup");
5897 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5903 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5904 s->first_displayed_line = 1;
5905 s->last_displayed_line = view->nlines;
5906 s->selected_line = 1;
5907 s->blame_complete = 0;
5909 s->commit_id = commit_id;
5910 memset(&s->blame, 0, sizeof(s->blame));
5912 STAILQ_INIT(&s->colors);
5913 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5914 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5915 get_color_value("TOG_COLOR_COMMIT"));
5920 view->show = show_blame_view;
5921 view->input = input_blame_view;
5922 view->reset = reset_blame_view;
5923 view->close = close_blame_view;
5924 view->search_start = search_start_blame_view;
5925 view->search_next = search_next_blame_view;
5927 return run_blame(view);
5930 static const struct got_error *
5931 close_blame_view(struct tog_view *view)
5933 const struct got_error *err = NULL;
5934 struct tog_blame_view_state *s = &view->state.blame;
5936 if (s->blame.thread)
5937 err = stop_blame(&s->blame);
5939 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5940 struct got_object_qid *blamed_commit;
5941 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5942 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5943 got_object_qid_free(blamed_commit);
5947 free_colors(&s->colors);
5951 static const struct got_error *
5952 search_start_blame_view(struct tog_view *view)
5954 struct tog_blame_view_state *s = &view->state.blame;
5956 s->matched_line = 0;
5960 static const struct got_error *
5961 search_next_blame_view(struct tog_view *view)
5963 struct tog_blame_view_state *s = &view->state.blame;
5964 const struct got_error *err = NULL;
5967 size_t linesize = 0;
5970 if (!view->searching) {
5971 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5975 if (s->matched_line) {
5976 if (view->searching == TOG_SEARCH_FORWARD)
5977 lineno = s->matched_line + 1;
5979 lineno = s->matched_line - 1;
5981 lineno = s->first_displayed_line - 1 + s->selected_line;
5986 if (lineno <= 0 || lineno > s->blame.nlines) {
5987 if (s->matched_line == 0) {
5988 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5992 if (view->searching == TOG_SEARCH_FORWARD)
5995 lineno = s->blame.nlines;
5998 offset = s->blame.line_offsets[lineno - 1];
5999 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
6001 return got_error_from_errno("fseeko");
6003 linelen = getline(&line, &linesize, s->blame.f);
6004 if (linelen != -1) {
6006 err = expand_tab(&exstr, line);
6009 if (match_line(exstr, &view->regex, 1,
6011 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6012 s->matched_line = lineno;
6018 if (view->searching == TOG_SEARCH_FORWARD)
6025 if (s->matched_line) {
6026 s->first_displayed_line = s->matched_line;
6027 s->selected_line = 1;
6033 static const struct got_error *
6034 show_blame_view(struct tog_view *view)
6036 const struct got_error *err = NULL;
6037 struct tog_blame_view_state *s = &view->state.blame;
6040 if (s->blame.thread == 0 && !s->blame_complete) {
6041 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6042 &s->blame.thread_args);
6044 return got_error_set_errno(errcode, "pthread_create");
6046 halfdelay(1); /* fast refresh while annotating */
6049 if (s->blame_complete)
6050 halfdelay(10); /* disable fast refresh */
6052 err = draw_blame(view);
6058 static const struct got_error *
6059 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6060 struct got_repository *repo, struct got_object_id *id)
6062 struct tog_view *log_view;
6063 const struct got_error *err = NULL;
6067 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6068 if (log_view == NULL)
6069 return got_error_from_errno("view_open");
6071 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6073 view_close(log_view);
6075 *new_view = log_view;
6080 static const struct got_error *
6081 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6083 const struct got_error *err = NULL, *thread_err = NULL;
6084 struct tog_view *diff_view;
6085 struct tog_blame_view_state *s = &view->state.blame;
6086 int eos, nscroll, begin_y = 0, begin_x = 0;
6088 eos = nscroll = view->nlines - 2;
6089 if (view_is_hsplit_top(view))
6097 view->x = MAX(view->maxx - view->ncols / 3, 0);
6102 if (view->x + view->ncols / 3 < view->maxx)
6103 view->x += 2; /* move two columns right */
6109 view->x -= MIN(view->x, 2); /* move two columns back */
6118 s->selected_line = 1;
6119 s->first_displayed_line = 1;
6124 if (s->blame.nlines < eos) {
6125 s->selected_line = s->blame.nlines;
6126 s->first_displayed_line = 1;
6128 s->selected_line = eos;
6129 s->first_displayed_line = s->blame.nlines - (eos - 1);
6136 if (s->selected_line > 1)
6138 else if (s->selected_line == 1 &&
6139 s->first_displayed_line > 1)
6140 s->first_displayed_line--;
6151 if (s->first_displayed_line == 1) {
6152 if (view->count > 1)
6154 s->selected_line = MAX(1, s->selected_line - nscroll);
6158 if (s->first_displayed_line > nscroll)
6159 s->first_displayed_line -= nscroll;
6161 s->first_displayed_line = 1;
6166 if (s->selected_line < eos && s->first_displayed_line +
6167 s->selected_line <= s->blame.nlines)
6169 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6170 s->first_displayed_line++;
6176 struct got_object_id *id = NULL;
6179 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6180 s->first_displayed_line, s->selected_line);
6184 struct got_commit_object *commit, *pcommit;
6185 struct got_object_qid *pid;
6186 struct got_object_id *blob_id = NULL;
6188 err = got_object_open_as_commit(&commit,
6193 got_object_commit_get_parent_ids(commit));
6195 got_object_commit_close(commit);
6198 /* Check if path history ends here. */
6199 err = got_object_open_as_commit(&pcommit,
6203 err = got_object_id_by_path(&blob_id, s->repo,
6205 got_object_commit_close(pcommit);
6207 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6209 got_object_commit_close(commit);
6212 err = got_object_get_type(&obj_type, s->repo,
6215 /* Can't blame non-blob type objects. */
6216 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6217 got_object_commit_close(commit);
6220 err = got_object_qid_alloc(&s->blamed_commit,
6222 got_object_commit_close(commit);
6224 if (got_object_id_cmp(id,
6225 &s->blamed_commit->id) == 0)
6227 err = got_object_qid_alloc(&s->blamed_commit,
6233 thread_err = stop_blame(&s->blame);
6237 STAILQ_INSERT_HEAD(&s->blamed_commits,
6238 s->blamed_commit, entry);
6239 err = run_blame(view);
6245 struct got_object_qid *first;
6248 first = STAILQ_FIRST(&s->blamed_commits);
6249 if (!got_object_id_cmp(&first->id, s->commit_id))
6252 thread_err = stop_blame(&s->blame);
6256 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6257 got_object_qid_free(s->blamed_commit);
6259 STAILQ_FIRST(&s->blamed_commits);
6260 err = run_blame(view);
6267 s->id_to_log = get_selected_commit_id(s->blame.lines,
6268 s->blame.nlines, s->first_displayed_line, s->selected_line);
6270 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6274 struct got_object_id *id = NULL;
6275 struct got_object_qid *pid;
6276 struct got_commit_object *commit = NULL;
6279 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6280 s->first_displayed_line, s->selected_line);
6283 err = got_object_open_as_commit(&commit, s->repo, id);
6286 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6288 /* traversed from diff view, release diff resources */
6289 err = close_diff_view(*new_view);
6292 diff_view = *new_view;
6294 if (view_is_parent_view(view))
6295 view_get_split(view, &begin_y, &begin_x);
6297 diff_view = view_open(0, 0, begin_y, begin_x,
6299 if (diff_view == NULL) {
6300 got_object_commit_close(commit);
6301 err = got_error_from_errno("view_open");
6305 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6306 id, NULL, NULL, 3, 0, 0, view, s->repo);
6307 got_object_commit_close(commit);
6309 view_close(diff_view);
6312 s->last_diffed_line = s->first_displayed_line - 1 +
6315 break; /* still open from active diff view */
6316 if (view_is_parent_view(view) &&
6317 view->mode == TOG_VIEW_SPLIT_HRZN) {
6318 err = view_init_hsplit(view, begin_y);
6324 diff_view->focussed = 1;
6325 diff_view->mode = view->mode;
6326 diff_view->nlines = view->lines - begin_y;
6327 if (view_is_parent_view(view)) {
6328 view_transfer_size(diff_view, view);
6329 err = view_close_child(view);
6332 err = view_set_child(view, diff_view);
6335 view->focus_child = 1;
6337 *new_view = diff_view;
6350 if (s->last_displayed_line >= s->blame.nlines &&
6351 s->selected_line >= MIN(s->blame.nlines,
6352 view->nlines - 2)) {
6356 if (s->last_displayed_line >= s->blame.nlines &&
6357 s->selected_line < view->nlines - 2) {
6359 MIN(nscroll, s->last_displayed_line -
6360 s->first_displayed_line - s->selected_line + 1);
6362 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6363 s->first_displayed_line += nscroll;
6365 s->first_displayed_line =
6366 s->blame.nlines - (view->nlines - 3);
6369 if (s->selected_line > view->nlines - 2) {
6370 s->selected_line = MIN(s->blame.nlines,
6378 return thread_err ? thread_err : err;
6381 static const struct got_error *
6382 reset_blame_view(struct tog_view *view)
6384 const struct got_error *err;
6385 struct tog_blame_view_state *s = &view->state.blame;
6389 err = stop_blame(&s->blame);
6393 return run_blame(view);
6396 static const struct got_error *
6397 cmd_blame(int argc, char *argv[])
6399 const struct got_error *error;
6400 struct got_repository *repo = NULL;
6401 struct got_worktree *worktree = NULL;
6402 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6403 char *link_target = NULL;
6404 struct got_object_id *commit_id = NULL;
6405 struct got_commit_object *commit = NULL;
6406 char *commit_id_str = NULL;
6408 struct tog_view *view;
6409 int *pack_fds = NULL;
6411 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6414 commit_id_str = optarg;
6417 repo_path = realpath(optarg, NULL);
6418 if (repo_path == NULL)
6419 return got_error_from_errno2("realpath",
6434 error = got_repo_pack_fds_open(&pack_fds);
6438 if (repo_path == NULL) {
6439 cwd = getcwd(NULL, 0);
6441 return got_error_from_errno("getcwd");
6442 error = got_worktree_open(&worktree, cwd);
6443 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6447 strdup(got_worktree_get_repo_path(worktree));
6449 repo_path = strdup(cwd);
6450 if (repo_path == NULL) {
6451 error = got_error_from_errno("strdup");
6456 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6460 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6467 error = apply_unveil(got_repo_get_path(repo), NULL);
6471 error = tog_load_refs(repo, 0);
6475 if (commit_id_str == NULL) {
6476 struct got_reference *head_ref;
6477 error = got_ref_open(&head_ref, repo, worktree ?
6478 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6481 error = got_ref_resolve(&commit_id, repo, head_ref);
6482 got_ref_close(head_ref);
6484 error = got_repo_match_object_id(&commit_id, NULL,
6485 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6490 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6492 error = got_error_from_errno("view_open");
6496 error = got_object_open_as_commit(&commit, repo, commit_id);
6500 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6505 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6510 /* Release work tree lock. */
6511 got_worktree_close(worktree);
6514 error = view_loop(view);
6522 got_object_commit_close(commit);
6524 got_worktree_close(worktree);
6526 const struct got_error *close_err = got_repo_close(repo);
6531 const struct got_error *pack_err =
6532 got_repo_pack_fds_close(pack_fds);
6540 static const struct got_error *
6541 draw_tree_entries(struct tog_view *view, const char *parent_path)
6543 struct tog_tree_view_state *s = &view->state.tree;
6544 const struct got_error *err = NULL;
6545 struct got_tree_entry *te;
6548 struct tog_color *tc;
6549 int width, n, nentries, i = 1;
6550 int limit = view->nlines;
6553 if (view_is_hsplit_top(view))
6554 --limit; /* border */
6556 werase(view->window);
6561 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6565 if (view_needs_focus_indication(view))
6566 wstandout(view->window);
6567 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6569 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6570 waddwstr(view->window, wline);
6573 while (width++ < view->ncols)
6574 waddch(view->window, ' ');
6576 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6577 if (view_needs_focus_indication(view))
6578 wstandend(view->window);
6583 if (s->first_displayed_entry) {
6584 i += got_tree_entry_get_index(s->first_displayed_entry);
6585 if (s->tree != s->root)
6586 ++i; /* account for ".." entry */
6588 nentries = got_object_tree_get_nentries(s->tree);
6589 if (asprintf(&index, "[%d/%d] %s",
6590 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6591 return got_error_from_errno("asprintf");
6592 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6596 waddwstr(view->window, wline);
6599 if (width < view->ncols - 1)
6600 waddch(view->window, '\n');
6603 waddch(view->window, '\n');
6607 if (s->first_displayed_entry == NULL) {
6608 te = got_object_tree_get_first_entry(s->tree);
6609 if (s->selected == 0) {
6611 wstandout(view->window);
6612 s->selected_entry = NULL;
6614 waddstr(view->window, " ..\n"); /* parent directory */
6615 if (s->selected == 0 && view->focussed)
6616 wstandend(view->window);
6623 te = s->first_displayed_entry;
6626 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6627 char *line = NULL, *id_str = NULL, *link_target = NULL;
6628 const char *modestr = "";
6631 te = got_object_tree_get_entry(s->tree, i);
6632 mode = got_tree_entry_get_mode(te);
6635 err = got_object_id_str(&id_str,
6636 got_tree_entry_get_id(te));
6638 return got_error_from_errno(
6639 "got_object_id_str");
6641 if (got_object_tree_entry_is_submodule(te))
6643 else if (S_ISLNK(mode)) {
6646 err = got_tree_entry_get_symlink_target(&link_target,
6652 for (i = 0; i < strlen(link_target); i++) {
6653 if (!isprint((unsigned char)link_target[i]))
6654 link_target[i] = '?';
6658 else if (S_ISDIR(mode))
6660 else if (mode & S_IXUSR)
6662 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6663 got_tree_entry_get_name(te), modestr,
6664 link_target ? " -> ": "",
6665 link_target ? link_target : "") == -1) {
6668 return got_error_from_errno("asprintf");
6672 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6678 if (n == s->selected) {
6680 wstandout(view->window);
6681 s->selected_entry = te;
6683 tc = match_color(&s->colors, line);
6685 wattr_on(view->window,
6686 COLOR_PAIR(tc->colorpair), NULL);
6687 waddwstr(view->window, wline);
6689 wattr_off(view->window,
6690 COLOR_PAIR(tc->colorpair), NULL);
6691 if (width < view->ncols - 1)
6692 waddch(view->window, '\n');
6693 if (n == s->selected && view->focussed)
6694 wstandend(view->window);
6700 s->last_displayed_entry = te;
6709 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6711 struct got_tree_entry *te;
6712 int isroot = s->tree == s->root;
6715 if (s->first_displayed_entry == NULL)
6718 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6719 while (i++ < maxscroll) {
6722 s->first_displayed_entry = NULL;
6725 s->first_displayed_entry = te;
6726 te = got_tree_entry_get_prev(s->tree, te);
6730 static const struct got_error *
6731 tree_scroll_down(struct tog_view *view, int maxscroll)
6733 struct tog_tree_view_state *s = &view->state.tree;
6734 struct got_tree_entry *next, *last;
6737 if (s->first_displayed_entry)
6738 next = got_tree_entry_get_next(s->tree,
6739 s->first_displayed_entry);
6741 next = got_object_tree_get_first_entry(s->tree);
6743 last = s->last_displayed_entry;
6744 while (next && n++ < maxscroll) {
6746 s->last_displayed_entry = last;
6747 last = got_tree_entry_get_next(s->tree, last);
6749 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6750 s->first_displayed_entry = next;
6751 next = got_tree_entry_get_next(s->tree, next);
6758 static const struct got_error *
6759 tree_entry_path(char **path, struct tog_parent_trees *parents,
6760 struct got_tree_entry *te)
6762 const struct got_error *err = NULL;
6763 struct tog_parent_tree *pt;
6764 size_t len = 2; /* for leading slash and NUL */
6766 TAILQ_FOREACH(pt, parents, entry)
6767 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6770 len += strlen(got_tree_entry_get_name(te));
6772 *path = calloc(1, len);
6774 return got_error_from_errno("calloc");
6777 pt = TAILQ_LAST(parents, tog_parent_trees);
6779 const char *name = got_tree_entry_get_name(pt->selected_entry);
6780 if (strlcat(*path, name, len) >= len) {
6781 err = got_error(GOT_ERR_NO_SPACE);
6784 if (strlcat(*path, "/", len) >= len) {
6785 err = got_error(GOT_ERR_NO_SPACE);
6788 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6791 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6792 err = got_error(GOT_ERR_NO_SPACE);
6804 static const struct got_error *
6805 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6806 struct got_tree_entry *te, struct tog_parent_trees *parents,
6807 struct got_object_id *commit_id, struct got_repository *repo)
6809 const struct got_error *err = NULL;
6811 struct tog_view *blame_view;
6815 err = tree_entry_path(&path, parents, te);
6819 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6820 if (blame_view == NULL) {
6821 err = got_error_from_errno("view_open");
6825 err = open_blame_view(blame_view, path, commit_id, repo);
6827 if (err->code == GOT_ERR_CANCELLED)
6829 view_close(blame_view);
6831 *new_view = blame_view;
6837 static const struct got_error *
6838 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6839 struct tog_tree_view_state *s)
6841 struct tog_view *log_view;
6842 const struct got_error *err = NULL;
6847 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6848 if (log_view == NULL)
6849 return got_error_from_errno("view_open");
6851 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6855 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6858 view_close(log_view);
6860 *new_view = log_view;
6865 static const struct got_error *
6866 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6867 const char *head_ref_name, struct got_repository *repo)
6869 const struct got_error *err = NULL;
6870 char *commit_id_str = NULL;
6871 struct tog_tree_view_state *s = &view->state.tree;
6872 struct got_commit_object *commit = NULL;
6874 TAILQ_INIT(&s->parents);
6875 STAILQ_INIT(&s->colors);
6877 s->commit_id = got_object_id_dup(commit_id);
6878 if (s->commit_id == NULL)
6879 return got_error_from_errno("got_object_id_dup");
6881 err = got_object_open_as_commit(&commit, repo, commit_id);
6886 * The root is opened here and will be closed when the view is closed.
6887 * Any visited subtrees and their path-wise parents are opened and
6890 err = got_object_open_as_tree(&s->root, repo,
6891 got_object_commit_get_tree_id(commit));
6896 err = got_object_id_str(&commit_id_str, commit_id);
6900 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6901 err = got_error_from_errno("asprintf");
6905 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6906 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6907 if (head_ref_name) {
6908 s->head_ref_name = strdup(head_ref_name);
6909 if (s->head_ref_name == NULL) {
6910 err = got_error_from_errno("strdup");
6916 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6917 err = add_color(&s->colors, "\\$$",
6918 TOG_COLOR_TREE_SUBMODULE,
6919 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6922 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6923 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6926 err = add_color(&s->colors, "/$",
6927 TOG_COLOR_TREE_DIRECTORY,
6928 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6932 err = add_color(&s->colors, "\\*$",
6933 TOG_COLOR_TREE_EXECUTABLE,
6934 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6938 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6939 get_color_value("TOG_COLOR_COMMIT"));
6944 view->show = show_tree_view;
6945 view->input = input_tree_view;
6946 view->close = close_tree_view;
6947 view->search_start = search_start_tree_view;
6948 view->search_next = search_next_tree_view;
6950 free(commit_id_str);
6952 got_object_commit_close(commit);
6954 close_tree_view(view);
6958 static const struct got_error *
6959 close_tree_view(struct tog_view *view)
6961 struct tog_tree_view_state *s = &view->state.tree;
6963 free_colors(&s->colors);
6964 free(s->tree_label);
6965 s->tree_label = NULL;
6967 s->commit_id = NULL;
6968 free(s->head_ref_name);
6969 s->head_ref_name = NULL;
6970 while (!TAILQ_EMPTY(&s->parents)) {
6971 struct tog_parent_tree *parent;
6972 parent = TAILQ_FIRST(&s->parents);
6973 TAILQ_REMOVE(&s->parents, parent, entry);
6974 if (parent->tree != s->root)
6975 got_object_tree_close(parent->tree);
6979 if (s->tree != NULL && s->tree != s->root)
6980 got_object_tree_close(s->tree);
6982 got_object_tree_close(s->root);
6986 static const struct got_error *
6987 search_start_tree_view(struct tog_view *view)
6989 struct tog_tree_view_state *s = &view->state.tree;
6991 s->matched_entry = NULL;
6996 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6998 regmatch_t regmatch;
7000 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7004 static const struct got_error *
7005 search_next_tree_view(struct tog_view *view)
7007 struct tog_tree_view_state *s = &view->state.tree;
7008 struct got_tree_entry *te = NULL;
7010 if (!view->searching) {
7011 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7015 if (s->matched_entry) {
7016 if (view->searching == TOG_SEARCH_FORWARD) {
7017 if (s->selected_entry)
7018 te = got_tree_entry_get_next(s->tree,
7021 te = got_object_tree_get_first_entry(s->tree);
7023 if (s->selected_entry == NULL)
7024 te = got_object_tree_get_last_entry(s->tree);
7026 te = got_tree_entry_get_prev(s->tree,
7030 if (s->selected_entry)
7031 te = s->selected_entry;
7032 else if (view->searching == TOG_SEARCH_FORWARD)
7033 te = got_object_tree_get_first_entry(s->tree);
7035 te = got_object_tree_get_last_entry(s->tree);
7040 if (s->matched_entry == NULL) {
7041 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7044 if (view->searching == TOG_SEARCH_FORWARD)
7045 te = got_object_tree_get_first_entry(s->tree);
7047 te = got_object_tree_get_last_entry(s->tree);
7050 if (match_tree_entry(te, &view->regex)) {
7051 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7052 s->matched_entry = te;
7056 if (view->searching == TOG_SEARCH_FORWARD)
7057 te = got_tree_entry_get_next(s->tree, te);
7059 te = got_tree_entry_get_prev(s->tree, te);
7062 if (s->matched_entry) {
7063 s->first_displayed_entry = s->matched_entry;
7070 static const struct got_error *
7071 show_tree_view(struct tog_view *view)
7073 const struct got_error *err = NULL;
7074 struct tog_tree_view_state *s = &view->state.tree;
7077 err = tree_entry_path(&parent_path, &s->parents, NULL);
7081 err = draw_tree_entries(view, parent_path);
7088 static const struct got_error *
7089 tree_goto_line(struct tog_view *view, int nlines)
7091 const struct got_error *err = NULL;
7092 struct tog_tree_view_state *s = &view->state.tree;
7093 struct got_tree_entry **fte, **lte, **ste;
7094 int g, last, first = 1, i = 1;
7095 int root = s->tree == s->root;
7096 int off = root ? 1 : 2;
7103 else if (g > got_object_tree_get_nentries(s->tree))
7104 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7106 fte = &s->first_displayed_entry;
7107 lte = &s->last_displayed_entry;
7108 ste = &s->selected_entry;
7111 first = got_tree_entry_get_index(*fte);
7112 first += off; /* account for ".." */
7114 last = got_tree_entry_get_index(*lte);
7117 if (g >= first && g <= last && g - first < nlines) {
7118 s->selected = g - first;
7119 return NULL; /* gline is on the current page */
7123 i = got_tree_entry_get_index(*ste);
7128 err = tree_scroll_down(view, g - i);
7131 if (got_tree_entry_get_index(*lte) >=
7132 got_object_tree_get_nentries(s->tree) - 1 &&
7133 first + s->selected < g &&
7134 s->selected < s->ndisplayed - 1) {
7135 first = got_tree_entry_get_index(*fte);
7137 s->selected = g - first;
7140 tree_scroll_up(s, i - g);
7143 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7144 s->selected = g - 1;
7149 static const struct got_error *
7150 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7152 const struct got_error *err = NULL;
7153 struct tog_tree_view_state *s = &view->state.tree;
7154 struct got_tree_entry *te;
7155 int n, nscroll = view->nlines - 3;
7158 return tree_goto_line(view, nscroll);
7162 s->show_ids = !s->show_ids;
7167 if (!s->selected_entry)
7169 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7173 err = view_request_new(new_view, view, TOG_VIEW_REF);
7179 if (s->tree == s->root)
7180 s->first_displayed_entry =
7181 got_object_tree_get_first_entry(s->tree);
7183 s->first_displayed_entry = NULL;
7187 int eos = view->nlines - 3;
7189 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7193 te = got_object_tree_get_last_entry(s->tree);
7194 for (n = 0; n < eos; n++) {
7196 if (s->tree != s->root) {
7197 s->first_displayed_entry = NULL;
7202 s->first_displayed_entry = te;
7203 te = got_tree_entry_get_prev(s->tree, te);
7206 s->selected = n - 1;
7212 if (s->selected > 0) {
7216 tree_scroll_up(s, 1);
7217 if (s->selected_entry == NULL ||
7218 (s->tree == s->root && s->selected_entry ==
7219 got_object_tree_get_first_entry(s->tree)))
7229 if (s->tree == s->root) {
7230 if (got_object_tree_get_first_entry(s->tree) ==
7231 s->first_displayed_entry)
7232 s->selected -= MIN(s->selected, nscroll);
7234 if (s->first_displayed_entry == NULL)
7235 s->selected -= MIN(s->selected, nscroll);
7237 tree_scroll_up(s, MAX(0, nscroll));
7238 if (s->selected_entry == NULL ||
7239 (s->tree == s->root && s->selected_entry ==
7240 got_object_tree_get_first_entry(s->tree)))
7246 if (s->selected < s->ndisplayed - 1) {
7250 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7252 /* can't scroll any further */
7256 tree_scroll_down(view, 1);
7266 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7268 /* can't scroll any further; move cursor down */
7269 if (s->selected < s->ndisplayed - 1)
7270 s->selected += MIN(nscroll,
7271 s->ndisplayed - s->selected - 1);
7276 tree_scroll_down(view, nscroll);
7281 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7282 struct tog_parent_tree *parent;
7283 /* user selected '..' */
7284 if (s->tree == s->root) {
7288 parent = TAILQ_FIRST(&s->parents);
7289 TAILQ_REMOVE(&s->parents, parent,
7291 got_object_tree_close(s->tree);
7292 s->tree = parent->tree;
7293 s->first_displayed_entry =
7294 parent->first_displayed_entry;
7296 parent->selected_entry;
7297 s->selected = parent->selected;
7298 if (s->selected > view->nlines - 3) {
7299 err = offset_selection_down(view);
7304 } else if (S_ISDIR(got_tree_entry_get_mode(
7305 s->selected_entry))) {
7306 struct got_tree_object *subtree;
7308 err = got_object_open_as_tree(&subtree, s->repo,
7309 got_tree_entry_get_id(s->selected_entry));
7312 err = tree_view_visit_subtree(s, subtree);
7314 got_object_tree_close(subtree);
7317 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7318 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7321 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7322 s->selected = view->nlines - 4;
7338 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7343 static const struct got_error *
7344 cmd_tree(int argc, char *argv[])
7346 const struct got_error *error;
7347 struct got_repository *repo = NULL;
7348 struct got_worktree *worktree = NULL;
7349 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7350 struct got_object_id *commit_id = NULL;
7351 struct got_commit_object *commit = NULL;
7352 const char *commit_id_arg = NULL;
7354 struct got_reference *ref = NULL;
7355 const char *head_ref_name = NULL;
7357 struct tog_view *view;
7358 int *pack_fds = NULL;
7360 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7363 commit_id_arg = optarg;
7366 repo_path = realpath(optarg, NULL);
7367 if (repo_path == NULL)
7368 return got_error_from_errno2("realpath",
7383 error = got_repo_pack_fds_open(&pack_fds);
7387 if (repo_path == NULL) {
7388 cwd = getcwd(NULL, 0);
7390 return got_error_from_errno("getcwd");
7391 error = got_worktree_open(&worktree, cwd);
7392 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7396 strdup(got_worktree_get_repo_path(worktree));
7398 repo_path = strdup(cwd);
7399 if (repo_path == NULL) {
7400 error = got_error_from_errno("strdup");
7405 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7409 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7416 error = apply_unveil(got_repo_get_path(repo), NULL);
7420 error = tog_load_refs(repo, 0);
7424 if (commit_id_arg == NULL) {
7425 error = got_repo_match_object_id(&commit_id, &label,
7426 worktree ? got_worktree_get_head_ref_name(worktree) :
7427 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7430 head_ref_name = label;
7432 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7434 head_ref_name = got_ref_get_name(ref);
7435 else if (error->code != GOT_ERR_NOT_REF)
7437 error = got_repo_match_object_id(&commit_id, NULL,
7438 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7443 error = got_object_open_as_commit(&commit, repo, commit_id);
7447 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7449 error = got_error_from_errno("view_open");
7452 error = open_tree_view(view, commit_id, head_ref_name, repo);
7455 if (!got_path_is_root_dir(in_repo_path)) {
7456 error = tree_view_walk_path(&view->state.tree, commit,
7463 /* Release work tree lock. */
7464 got_worktree_close(worktree);
7467 error = view_loop(view);
7476 const struct got_error *close_err = got_repo_close(repo);
7481 const struct got_error *pack_err =
7482 got_repo_pack_fds_close(pack_fds);
7490 static const struct got_error *
7491 ref_view_load_refs(struct tog_ref_view_state *s)
7493 struct got_reflist_entry *sre;
7494 struct tog_reflist_entry *re;
7497 TAILQ_FOREACH(sre, &tog_refs, entry) {
7498 if (strncmp(got_ref_get_name(sre->ref),
7499 "refs/got/", 9) == 0 &&
7500 strncmp(got_ref_get_name(sre->ref),
7501 "refs/got/backup/", 16) != 0)
7504 re = malloc(sizeof(*re));
7506 return got_error_from_errno("malloc");
7508 re->ref = got_ref_dup(sre->ref);
7509 if (re->ref == NULL)
7510 return got_error_from_errno("got_ref_dup");
7511 re->idx = s->nrefs++;
7512 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7515 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7520 ref_view_free_refs(struct tog_ref_view_state *s)
7522 struct tog_reflist_entry *re;
7524 while (!TAILQ_EMPTY(&s->refs)) {
7525 re = TAILQ_FIRST(&s->refs);
7526 TAILQ_REMOVE(&s->refs, re, entry);
7527 got_ref_close(re->ref);
7532 static const struct got_error *
7533 open_ref_view(struct tog_view *view, struct got_repository *repo)
7535 const struct got_error *err = NULL;
7536 struct tog_ref_view_state *s = &view->state.ref;
7538 s->selected_entry = 0;
7541 TAILQ_INIT(&s->refs);
7542 STAILQ_INIT(&s->colors);
7544 err = ref_view_load_refs(s);
7548 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7549 err = add_color(&s->colors, "^refs/heads/",
7550 TOG_COLOR_REFS_HEADS,
7551 get_color_value("TOG_COLOR_REFS_HEADS"));
7555 err = add_color(&s->colors, "^refs/tags/",
7556 TOG_COLOR_REFS_TAGS,
7557 get_color_value("TOG_COLOR_REFS_TAGS"));
7561 err = add_color(&s->colors, "^refs/remotes/",
7562 TOG_COLOR_REFS_REMOTES,
7563 get_color_value("TOG_COLOR_REFS_REMOTES"));
7567 err = add_color(&s->colors, "^refs/got/backup/",
7568 TOG_COLOR_REFS_BACKUP,
7569 get_color_value("TOG_COLOR_REFS_BACKUP"));
7574 view->show = show_ref_view;
7575 view->input = input_ref_view;
7576 view->close = close_ref_view;
7577 view->search_start = search_start_ref_view;
7578 view->search_next = search_next_ref_view;
7581 free_colors(&s->colors);
7585 static const struct got_error *
7586 close_ref_view(struct tog_view *view)
7588 struct tog_ref_view_state *s = &view->state.ref;
7590 ref_view_free_refs(s);
7591 free_colors(&s->colors);
7596 static const struct got_error *
7597 resolve_reflist_entry(struct got_object_id **commit_id,
7598 struct tog_reflist_entry *re, struct got_repository *repo)
7600 const struct got_error *err = NULL;
7601 struct got_object_id *obj_id;
7602 struct got_tag_object *tag = NULL;
7607 err = got_ref_resolve(&obj_id, repo, re->ref);
7611 err = got_object_get_type(&obj_type, repo, obj_id);
7616 case GOT_OBJ_TYPE_COMMIT:
7617 *commit_id = obj_id;
7619 case GOT_OBJ_TYPE_TAG:
7620 err = got_object_open_as_tag(&tag, repo, obj_id);
7624 err = got_object_get_type(&obj_type, repo,
7625 got_object_tag_get_object_id(tag));
7628 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7629 err = got_error(GOT_ERR_OBJ_TYPE);
7632 *commit_id = got_object_id_dup(
7633 got_object_tag_get_object_id(tag));
7634 if (*commit_id == NULL) {
7635 err = got_error_from_errno("got_object_id_dup");
7640 err = got_error(GOT_ERR_OBJ_TYPE);
7646 got_object_tag_close(tag);
7654 static const struct got_error *
7655 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7656 struct tog_reflist_entry *re, struct got_repository *repo)
7658 struct tog_view *log_view;
7659 const struct got_error *err = NULL;
7660 struct got_object_id *commit_id = NULL;
7664 err = resolve_reflist_entry(&commit_id, re, repo);
7666 if (err->code != GOT_ERR_OBJ_TYPE)
7672 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7673 if (log_view == NULL) {
7674 err = got_error_from_errno("view_open");
7678 err = open_log_view(log_view, commit_id, repo,
7679 got_ref_get_name(re->ref), "", 0);
7682 view_close(log_view);
7684 *new_view = log_view;
7690 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7692 struct tog_reflist_entry *re;
7695 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7698 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7699 while (i++ < maxscroll) {
7702 s->first_displayed_entry = re;
7703 re = TAILQ_PREV(re, tog_reflist_head, entry);
7707 static const struct got_error *
7708 ref_scroll_down(struct tog_view *view, int maxscroll)
7710 struct tog_ref_view_state *s = &view->state.ref;
7711 struct tog_reflist_entry *next, *last;
7714 if (s->first_displayed_entry)
7715 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7717 next = TAILQ_FIRST(&s->refs);
7719 last = s->last_displayed_entry;
7720 while (next && n++ < maxscroll) {
7722 s->last_displayed_entry = last;
7723 last = TAILQ_NEXT(last, entry);
7725 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7726 s->first_displayed_entry = next;
7727 next = TAILQ_NEXT(next, entry);
7734 static const struct got_error *
7735 search_start_ref_view(struct tog_view *view)
7737 struct tog_ref_view_state *s = &view->state.ref;
7739 s->matched_entry = NULL;
7744 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7746 regmatch_t regmatch;
7748 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7752 static const struct got_error *
7753 search_next_ref_view(struct tog_view *view)
7755 struct tog_ref_view_state *s = &view->state.ref;
7756 struct tog_reflist_entry *re = NULL;
7758 if (!view->searching) {
7759 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7763 if (s->matched_entry) {
7764 if (view->searching == TOG_SEARCH_FORWARD) {
7765 if (s->selected_entry)
7766 re = TAILQ_NEXT(s->selected_entry, entry);
7768 re = TAILQ_PREV(s->selected_entry,
7769 tog_reflist_head, entry);
7771 if (s->selected_entry == NULL)
7772 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7774 re = TAILQ_PREV(s->selected_entry,
7775 tog_reflist_head, entry);
7778 if (s->selected_entry)
7779 re = s->selected_entry;
7780 else if (view->searching == TOG_SEARCH_FORWARD)
7781 re = TAILQ_FIRST(&s->refs);
7783 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7788 if (s->matched_entry == NULL) {
7789 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7792 if (view->searching == TOG_SEARCH_FORWARD)
7793 re = TAILQ_FIRST(&s->refs);
7795 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7798 if (match_reflist_entry(re, &view->regex)) {
7799 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7800 s->matched_entry = re;
7804 if (view->searching == TOG_SEARCH_FORWARD)
7805 re = TAILQ_NEXT(re, entry);
7807 re = TAILQ_PREV(re, tog_reflist_head, entry);
7810 if (s->matched_entry) {
7811 s->first_displayed_entry = s->matched_entry;
7818 static const struct got_error *
7819 show_ref_view(struct tog_view *view)
7821 const struct got_error *err = NULL;
7822 struct tog_ref_view_state *s = &view->state.ref;
7823 struct tog_reflist_entry *re;
7826 struct tog_color *tc;
7828 int limit = view->nlines;
7830 werase(view->window);
7833 if (view_is_hsplit_top(view))
7834 --limit; /* border */
7839 re = s->first_displayed_entry;
7841 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7843 return got_error_from_errno("asprintf");
7845 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7850 if (view_needs_focus_indication(view))
7851 wstandout(view->window);
7852 waddwstr(view->window, wline);
7853 while (width++ < view->ncols)
7854 waddch(view->window, ' ');
7855 if (view_needs_focus_indication(view))
7856 wstandend(view->window);
7865 while (re && limit > 0) {
7867 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7870 struct got_commit_object *ci;
7871 struct got_tag_object *tag;
7872 struct got_object_id *id;
7876 err = got_ref_resolve(&id, s->repo, re->ref);
7879 err = got_object_open_as_tag(&tag, s->repo, id);
7881 if (err->code != GOT_ERR_OBJ_TYPE) {
7885 err = got_object_open_as_commit(&ci, s->repo,
7891 t = got_object_commit_get_committer_time(ci);
7892 got_object_commit_close(ci);
7894 t = got_object_tag_get_tagger_time(tag);
7895 got_object_tag_close(tag);
7898 if (gmtime_r(&t, &tm) == NULL)
7899 return got_error_from_errno("gmtime_r");
7900 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7901 return got_error(GOT_ERR_NO_SPACE);
7903 if (got_ref_is_symbolic(re->ref)) {
7904 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7905 ymd : "", got_ref_get_name(re->ref),
7906 got_ref_get_symref_target(re->ref)) == -1)
7907 return got_error_from_errno("asprintf");
7908 } else if (s->show_ids) {
7909 struct got_object_id *id;
7911 err = got_ref_resolve(&id, s->repo, re->ref);
7914 err = got_object_id_str(&id_str, id);
7919 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7920 got_ref_get_name(re->ref), id_str) == -1) {
7921 err = got_error_from_errno("asprintf");
7928 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7929 got_ref_get_name(re->ref)) == -1)
7930 return got_error_from_errno("asprintf");
7932 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7938 if (n == s->selected) {
7940 wstandout(view->window);
7941 s->selected_entry = re;
7943 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7945 wattr_on(view->window,
7946 COLOR_PAIR(tc->colorpair), NULL);
7947 waddwstr(view->window, wline);
7949 wattr_off(view->window,
7950 COLOR_PAIR(tc->colorpair), NULL);
7951 if (width < view->ncols - 1)
7952 waddch(view->window, '\n');
7953 if (n == s->selected && view->focussed)
7954 wstandend(view->window);
7960 s->last_displayed_entry = re;
7963 re = TAILQ_NEXT(re, entry);
7970 static const struct got_error *
7971 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7972 struct tog_reflist_entry *re, struct got_repository *repo)
7974 const struct got_error *err = NULL;
7975 struct got_object_id *commit_id = NULL;
7976 struct tog_view *tree_view;
7980 err = resolve_reflist_entry(&commit_id, re, repo);
7982 if (err->code != GOT_ERR_OBJ_TYPE)
7989 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7990 if (tree_view == NULL) {
7991 err = got_error_from_errno("view_open");
7995 err = open_tree_view(tree_view, commit_id,
7996 got_ref_get_name(re->ref), repo);
8000 *new_view = tree_view;
8006 static const struct got_error *
8007 ref_goto_line(struct tog_view *view, int nlines)
8009 const struct got_error *err = NULL;
8010 struct tog_ref_view_state *s = &view->state.ref;
8011 int g, idx = s->selected_entry->idx;
8018 else if (g > s->nrefs)
8021 if (g >= s->first_displayed_entry->idx + 1 &&
8022 g <= s->last_displayed_entry->idx + 1 &&
8023 g - s->first_displayed_entry->idx - 1 < nlines) {
8024 s->selected = g - s->first_displayed_entry->idx - 1;
8029 err = ref_scroll_down(view, g - idx - 1);
8032 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8033 s->first_displayed_entry->idx + s->selected < g &&
8034 s->selected < s->ndisplayed - 1)
8035 s->selected = g - s->first_displayed_entry->idx - 1;
8036 } else if (idx + 1 > g)
8037 ref_scroll_up(s, idx - g + 1);
8039 if (g < nlines && s->first_displayed_entry->idx == 0)
8040 s->selected = g - 1;
8046 static const struct got_error *
8047 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8049 const struct got_error *err = NULL;
8050 struct tog_ref_view_state *s = &view->state.ref;
8051 struct tog_reflist_entry *re;
8052 int n, nscroll = view->nlines - 1;
8055 return ref_goto_line(view, nscroll);
8059 s->show_ids = !s->show_ids;
8063 s->show_date = !s->show_date;
8067 s->sort_by_date = !s->sort_by_date;
8069 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8070 got_ref_cmp_by_commit_timestamp_descending :
8071 tog_ref_cmp_by_name, s->repo);
8074 got_reflist_object_id_map_free(tog_refs_idmap);
8075 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8076 &tog_refs, s->repo);
8079 ref_view_free_refs(s);
8080 err = ref_view_load_refs(s);
8085 if (!s->selected_entry)
8087 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8091 if (!s->selected_entry)
8093 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8099 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8103 int eos = view->nlines - 1;
8105 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8109 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8110 for (n = 0; n < eos; n++) {
8113 s->first_displayed_entry = re;
8114 re = TAILQ_PREV(re, tog_reflist_head, entry);
8117 s->selected = n - 1;
8123 if (s->selected > 0) {
8127 ref_scroll_up(s, 1);
8128 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8138 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8139 s->selected -= MIN(nscroll, s->selected);
8140 ref_scroll_up(s, MAX(0, nscroll));
8141 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8147 if (s->selected < s->ndisplayed - 1) {
8151 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8152 /* can't scroll any further */
8156 ref_scroll_down(view, 1);
8166 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8167 /* can't scroll any further; move cursor down */
8168 if (s->selected < s->ndisplayed - 1)
8169 s->selected += MIN(nscroll,
8170 s->ndisplayed - s->selected - 1);
8171 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8172 s->selected += s->ndisplayed - s->selected - 1;
8176 ref_scroll_down(view, nscroll);
8181 err = tog_load_refs(s->repo, s->sort_by_date);
8184 ref_view_free_refs(s);
8185 err = ref_view_load_refs(s);
8188 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8189 s->selected = view->nlines - 2;
8203 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8208 static const struct got_error *
8209 cmd_ref(int argc, char *argv[])
8211 const struct got_error *error;
8212 struct got_repository *repo = NULL;
8213 struct got_worktree *worktree = NULL;
8214 char *cwd = NULL, *repo_path = NULL;
8216 struct tog_view *view;
8217 int *pack_fds = NULL;
8219 while ((ch = getopt(argc, argv, "r:")) != -1) {
8222 repo_path = realpath(optarg, NULL);
8223 if (repo_path == NULL)
8224 return got_error_from_errno2("realpath",
8239 error = got_repo_pack_fds_open(&pack_fds);
8243 if (repo_path == NULL) {
8244 cwd = getcwd(NULL, 0);
8246 return got_error_from_errno("getcwd");
8247 error = got_worktree_open(&worktree, cwd);
8248 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8252 strdup(got_worktree_get_repo_path(worktree));
8254 repo_path = strdup(cwd);
8255 if (repo_path == NULL) {
8256 error = got_error_from_errno("strdup");
8261 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8267 error = apply_unveil(got_repo_get_path(repo), NULL);
8271 error = tog_load_refs(repo, 0);
8275 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8277 error = got_error_from_errno("view_open");
8281 error = open_ref_view(view, repo);
8286 /* Release work tree lock. */
8287 got_worktree_close(worktree);
8290 error = view_loop(view);
8295 const struct got_error *close_err = got_repo_close(repo);
8300 const struct got_error *pack_err =
8301 got_repo_pack_fds_close(pack_fds);
8309 static const struct got_error *
8310 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8311 enum tog_view_type request, int y, int x)
8313 const struct got_error *err = NULL;
8319 if (view->type == TOG_VIEW_LOG) {
8320 struct tog_log_view_state *s = &view->state.log;
8322 err = open_diff_view_for_commit(new_view, y, x,
8323 s->selected_entry->commit, s->selected_entry->id,
8326 return got_error_msg(GOT_ERR_NOT_IMPL,
8327 "parent/child view pair not supported");
8329 case TOG_VIEW_BLAME:
8330 if (view->type == TOG_VIEW_TREE) {
8331 struct tog_tree_view_state *s = &view->state.tree;
8333 err = blame_tree_entry(new_view, y, x,
8334 s->selected_entry, &s->parents, s->commit_id,
8337 return got_error_msg(GOT_ERR_NOT_IMPL,
8338 "parent/child view pair not supported");
8341 if (view->type == TOG_VIEW_BLAME)
8342 err = log_annotated_line(new_view, y, x,
8343 view->state.blame.repo, view->state.blame.id_to_log);
8344 else if (view->type == TOG_VIEW_TREE)
8345 err = log_selected_tree_entry(new_view, y, x,
8347 else if (view->type == TOG_VIEW_REF)
8348 err = log_ref_entry(new_view, y, x,
8349 view->state.ref.selected_entry,
8350 view->state.ref.repo);
8352 return got_error_msg(GOT_ERR_NOT_IMPL,
8353 "parent/child view pair not supported");
8356 if (view->type == TOG_VIEW_LOG)
8357 err = browse_commit_tree(new_view, y, x,
8358 view->state.log.selected_entry,
8359 view->state.log.in_repo_path,
8360 view->state.log.head_ref_name,
8361 view->state.log.repo);
8362 else if (view->type == TOG_VIEW_REF)
8363 err = browse_ref_tree(new_view, y, x,
8364 view->state.ref.selected_entry,
8365 view->state.ref.repo);
8367 return got_error_msg(GOT_ERR_NOT_IMPL,
8368 "parent/child view pair not supported");
8371 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8372 if (*new_view == NULL)
8373 return got_error_from_errno("view_open");
8374 if (view->type == TOG_VIEW_LOG)
8375 err = open_ref_view(*new_view, view->state.log.repo);
8376 else if (view->type == TOG_VIEW_TREE)
8377 err = open_ref_view(*new_view, view->state.tree.repo);
8379 err = got_error_msg(GOT_ERR_NOT_IMPL,
8380 "parent/child view pair not supported");
8382 view_close(*new_view);
8385 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8392 * If view was scrolled down to move the selected line into view when opening a
8393 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8396 offset_selection_up(struct tog_view *view)
8398 switch (view->type) {
8399 case TOG_VIEW_BLAME: {
8400 struct tog_blame_view_state *s = &view->state.blame;
8401 if (s->first_displayed_line == 1) {
8402 s->selected_line = MAX(s->selected_line - view->offset,
8406 if (s->first_displayed_line > view->offset)
8407 s->first_displayed_line -= view->offset;
8409 s->first_displayed_line = 1;
8410 s->selected_line += view->offset;
8414 log_scroll_up(&view->state.log, view->offset);
8415 view->state.log.selected += view->offset;
8418 ref_scroll_up(&view->state.ref, view->offset);
8419 view->state.ref.selected += view->offset;
8422 tree_scroll_up(&view->state.tree, view->offset);
8423 view->state.tree.selected += view->offset;
8433 * If the selected line is in the section of screen covered by the bottom split,
8434 * scroll down offset lines to move it into view and index its new position.
8436 static const struct got_error *
8437 offset_selection_down(struct tog_view *view)
8439 const struct got_error *err = NULL;
8440 const struct got_error *(*scrolld)(struct tog_view *, int);
8441 int *selected = NULL;
8444 switch (view->type) {
8445 case TOG_VIEW_BLAME: {
8446 struct tog_blame_view_state *s = &view->state.blame;
8449 if (s->selected_line > view->nlines - header) {
8450 offset = abs(view->nlines - s->selected_line - header);
8451 s->first_displayed_line += offset;
8452 s->selected_line -= offset;
8453 view->offset = offset;
8457 case TOG_VIEW_LOG: {
8458 struct tog_log_view_state *s = &view->state.log;
8459 scrolld = &log_scroll_down;
8460 header = view_is_parent_view(view) ? 3 : 2;
8461 selected = &s->selected;
8464 case TOG_VIEW_REF: {
8465 struct tog_ref_view_state *s = &view->state.ref;
8466 scrolld = &ref_scroll_down;
8468 selected = &s->selected;
8471 case TOG_VIEW_TREE: {
8472 struct tog_tree_view_state *s = &view->state.tree;
8473 scrolld = &tree_scroll_down;
8475 selected = &s->selected;
8485 if (selected && *selected > view->nlines - header) {
8486 offset = abs(view->nlines - *selected - header);
8487 view->offset = offset;
8488 if (scrolld && offset) {
8489 err = scrolld(view, offset);
8490 *selected -= offset;
8498 list_commands(FILE *fp)
8502 fprintf(fp, "commands:");
8503 for (i = 0; i < nitems(tog_commands); i++) {
8504 const struct tog_cmd *cmd = &tog_commands[i];
8505 fprintf(fp, " %s", cmd->name);
8511 usage(int hflag, int status)
8513 FILE *fp = (status == 0) ? stdout : stderr;
8515 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8518 fprintf(fp, "lazy usage: %s path\n", getprogname());
8525 make_argv(int argc, ...)
8533 argv = calloc(argc, sizeof(char *));
8536 for (i = 0; i < argc; i++) {
8537 argv[i] = strdup(va_arg(ap, char *));
8538 if (argv[i] == NULL)
8547 * Try to convert 'tog path' into a 'tog log path' command.
8548 * The user could simply have mistyped the command rather than knowingly
8549 * provided a path. So check whether argv[0] can in fact be resolved
8550 * to a path in the HEAD commit and print a special error if not.
8551 * This hack is for mpi@ <3
8553 static const struct got_error *
8554 tog_log_with_path(int argc, char *argv[])
8556 const struct got_error *error = NULL, *close_err;
8557 const struct tog_cmd *cmd = NULL;
8558 struct got_repository *repo = NULL;
8559 struct got_worktree *worktree = NULL;
8560 struct got_object_id *commit_id = NULL, *id = NULL;
8561 struct got_commit_object *commit = NULL;
8562 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8563 char *commit_id_str = NULL, **cmd_argv = NULL;
8564 int *pack_fds = NULL;
8566 cwd = getcwd(NULL, 0);
8568 return got_error_from_errno("getcwd");
8570 error = got_repo_pack_fds_open(&pack_fds);
8574 error = got_worktree_open(&worktree, cwd);
8575 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8579 repo_path = strdup(got_worktree_get_repo_path(worktree));
8581 repo_path = strdup(cwd);
8582 if (repo_path == NULL) {
8583 error = got_error_from_errno("strdup");
8587 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8591 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8596 error = tog_load_refs(repo, 0);
8599 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8600 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8601 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8606 got_worktree_close(worktree);
8610 error = got_object_open_as_commit(&commit, repo, commit_id);
8614 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8616 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8618 fprintf(stderr, "%s: '%s' is no known command or path\n",
8619 getprogname(), argv[0]);
8624 error = got_object_id_str(&commit_id_str, commit_id);
8628 cmd = &tog_commands[0]; /* log */
8630 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8631 error = cmd->cmd_main(argc, cmd_argv);
8634 close_err = got_repo_close(repo);
8639 got_object_commit_close(commit);
8641 got_worktree_close(worktree);
8643 const struct got_error *pack_err =
8644 got_repo_pack_fds_close(pack_fds);
8649 free(commit_id_str);
8656 for (i = 0; i < argc; i++)
8665 main(int argc, char *argv[])
8667 const struct got_error *error = NULL;
8668 const struct tog_cmd *cmd = NULL;
8669 int ch, hflag = 0, Vflag = 0;
8670 char **cmd_argv = NULL;
8671 static const struct option longopts[] = {
8672 { "version", no_argument, NULL, 'V' },
8675 char *diff_algo_str = NULL;
8677 if (!isatty(STDIN_FILENO))
8678 errx(1, "standard input is not a tty");
8680 setlocale(LC_CTYPE, "");
8682 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8702 got_version_print_str();
8707 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8715 /* Build an argument vector which runs a default command. */
8716 cmd = &tog_commands[0];
8718 cmd_argv = make_argv(argc, cmd->name);
8722 /* Did the user specify a command? */
8723 for (i = 0; i < nitems(tog_commands); i++) {
8724 if (strncmp(tog_commands[i].name, argv[0],
8725 strlen(argv[0])) == 0) {
8726 cmd = &tog_commands[i];
8732 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8733 if (diff_algo_str) {
8734 if (strcasecmp(diff_algo_str, "patience") == 0)
8735 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8736 if (strcasecmp(diff_algo_str, "myers") == 0)
8737 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8743 /* No command specified; try log with a path */
8744 error = tog_log_with_path(argc, argv);
8749 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8756 for (i = 0; i < argc; i++)
8761 if (error && error->code != GOT_ERR_CANCELLED)
8762 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);