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 *commits;
353 const char *in_repo_path;
354 struct got_object_id *start_id;
355 struct got_repository *repo;
359 struct commit_queue_entry **first_displayed_entry;
360 struct commit_queue_entry **selected_entry;
362 int *search_next_done;
366 struct tog_log_view_state {
367 struct commit_queue commits;
368 struct commit_queue_entry *first_displayed_entry;
369 struct commit_queue_entry *last_displayed_entry;
370 struct commit_queue_entry *selected_entry;
375 struct got_repository *repo;
376 struct got_object_id *start_id;
379 struct tog_log_thread_args thread_args;
380 struct commit_queue_entry *matched_entry;
381 struct commit_queue_entry *search_entry;
382 struct tog_colors colors;
386 #define TOG_COLOR_DIFF_MINUS 1
387 #define TOG_COLOR_DIFF_PLUS 2
388 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
389 #define TOG_COLOR_DIFF_META 4
390 #define TOG_COLOR_TREE_SUBMODULE 5
391 #define TOG_COLOR_TREE_SYMLINK 6
392 #define TOG_COLOR_TREE_DIRECTORY 7
393 #define TOG_COLOR_TREE_EXECUTABLE 8
394 #define TOG_COLOR_COMMIT 9
395 #define TOG_COLOR_AUTHOR 10
396 #define TOG_COLOR_DATE 11
397 #define TOG_COLOR_REFS_HEADS 12
398 #define TOG_COLOR_REFS_TAGS 13
399 #define TOG_COLOR_REFS_REMOTES 14
400 #define TOG_COLOR_REFS_BACKUP 15
402 struct tog_blame_cb_args {
403 struct tog_blame_line *lines; /* one per line */
406 struct tog_view *view;
407 struct got_object_id *commit_id;
411 struct tog_blame_thread_args {
413 struct got_repository *repo;
414 struct tog_blame_cb_args *cb_args;
416 got_cancel_cb cancel_cb;
423 struct tog_blame_line *lines;
427 struct tog_blame_thread_args thread_args;
428 struct tog_blame_cb_args cb_args;
433 struct tog_blame_view_state {
434 int first_displayed_line;
435 int last_displayed_line;
437 int last_diffed_line;
441 struct got_object_id_queue blamed_commits;
442 struct got_object_qid *blamed_commit;
444 struct got_repository *repo;
445 struct got_object_id *commit_id;
446 struct got_object_id *id_to_log;
447 struct tog_blame blame;
449 struct tog_colors colors;
452 struct tog_parent_tree {
453 TAILQ_ENTRY(tog_parent_tree) entry;
454 struct got_tree_object *tree;
455 struct got_tree_entry *first_displayed_entry;
456 struct got_tree_entry *selected_entry;
460 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
462 struct tog_tree_view_state {
464 struct got_object_id *commit_id;/* commit which this tree belongs to */
465 struct got_tree_object *root; /* the commit's root tree entry */
466 struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 struct got_tree_entry *first_displayed_entry;
468 struct got_tree_entry *last_displayed_entry;
469 struct got_tree_entry *selected_entry;
470 int ndisplayed, selected, show_ids;
471 struct tog_parent_trees parents; /* parent trees of current sub-tree */
473 struct got_repository *repo;
474 struct got_tree_entry *matched_entry;
475 struct tog_colors colors;
478 struct tog_reflist_entry {
479 TAILQ_ENTRY(tog_reflist_entry) entry;
480 struct got_reference *ref;
484 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
486 struct tog_ref_view_state {
487 struct tog_reflist_head refs;
488 struct tog_reflist_entry *first_displayed_entry;
489 struct tog_reflist_entry *last_displayed_entry;
490 struct tog_reflist_entry *selected_entry;
491 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 struct got_repository *repo;
493 struct tog_reflist_entry *matched_entry;
494 struct tog_colors colors;
498 * We implement two types of views: parent views and child views.
500 * The 'Tab' key switches focus between a parent view and its child view.
501 * Child views are shown side-by-side to their parent view, provided
502 * there is enough screen estate.
504 * When a new view is opened from within a parent view, this new view
505 * becomes a child view of the parent view, replacing any existing child.
507 * When a new view is opened from within a child view, this new view
508 * becomes a parent view which will obscure the views below until the
509 * user quits the new parent view by typing 'q'.
511 * This list of views contains parent views only.
512 * Child views are only pointed to by their parent view.
514 TAILQ_HEAD(tog_view_list_head, tog_view);
517 TAILQ_ENTRY(tog_view) entry;
520 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 int resized_y, resized_x; /* begin_y/x based on user resizing */
522 int maxx, x; /* max column and current start column */
523 int lines, cols; /* copies of LINES and COLS */
524 int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 int gline, hiline; /* navigate to and highlight this nG line */
526 int ch, count; /* current keymap and count prefix */
527 int resized; /* set when in a resize event */
528 int focussed; /* Only set on one parent or child view at a time. */
530 struct tog_view *parent;
531 struct tog_view *child;
534 * This flag is initially set on parent views when a new child view
535 * is created. It gets toggled when the 'Tab' key switches focus
536 * between parent and child.
537 * The flag indicates whether focus should be passed on to our child
538 * view if this parent view gets picked for focus after another parent
539 * view was closed. This prevents child views from losing focus in such
544 enum tog_view_mode mode;
545 /* type-specific state */
546 enum tog_view_type type;
548 struct tog_diff_view_state diff;
549 struct tog_log_view_state log;
550 struct tog_blame_view_state blame;
551 struct tog_tree_view_state tree;
552 struct tog_ref_view_state ref;
555 const struct got_error *(*show)(struct tog_view *);
556 const struct got_error *(*input)(struct tog_view **,
557 struct tog_view *, int);
558 const struct got_error *(*reset)(struct tog_view *);
559 const struct got_error *(*resize)(struct tog_view *, int);
560 const struct got_error *(*close)(struct tog_view *);
562 const struct got_error *(*search_start)(struct tog_view *);
563 const struct got_error *(*search_next)(struct tog_view *);
566 #define TOG_SEARCH_FORWARD 1
567 #define TOG_SEARCH_BACKWARD 2
568 int search_next_done;
569 #define TOG_SEARCH_HAVE_MORE 1
570 #define TOG_SEARCH_NO_MORE 2
571 #define TOG_SEARCH_HAVE_NONE 3
576 static const struct got_error *open_diff_view(struct tog_view *,
577 struct got_object_id *, struct got_object_id *,
578 const char *, const char *, int, int, int, struct tog_view *,
579 struct got_repository *);
580 static const struct got_error *show_diff_view(struct tog_view *);
581 static const struct got_error *input_diff_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *reset_diff_view(struct tog_view *);
584 static const struct got_error* close_diff_view(struct tog_view *);
585 static const struct got_error *search_start_diff_view(struct tog_view *);
586 static const struct got_error *search_next_diff_view(struct tog_view *);
588 static const struct got_error *open_log_view(struct tog_view *,
589 struct got_object_id *, struct got_repository *,
590 const char *, const char *, int);
591 static const struct got_error * show_log_view(struct tog_view *);
592 static const struct got_error *input_log_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *resize_log_view(struct tog_view *, int);
595 static const struct got_error *close_log_view(struct tog_view *);
596 static const struct got_error *search_start_log_view(struct tog_view *);
597 static const struct got_error *search_next_log_view(struct tog_view *);
599 static const struct got_error *open_blame_view(struct tog_view *, char *,
600 struct got_object_id *, struct got_repository *);
601 static const struct got_error *show_blame_view(struct tog_view *);
602 static const struct got_error *input_blame_view(struct tog_view **,
603 struct tog_view *, int);
604 static const struct got_error *reset_blame_view(struct tog_view *);
605 static const struct got_error *close_blame_view(struct tog_view *);
606 static const struct got_error *search_start_blame_view(struct tog_view *);
607 static const struct got_error *search_next_blame_view(struct tog_view *);
609 static const struct got_error *open_tree_view(struct tog_view *,
610 struct got_object_id *, const char *, struct got_repository *);
611 static const struct got_error *show_tree_view(struct tog_view *);
612 static const struct got_error *input_tree_view(struct tog_view **,
613 struct tog_view *, int);
614 static const struct got_error *close_tree_view(struct tog_view *);
615 static const struct got_error *search_start_tree_view(struct tog_view *);
616 static const struct got_error *search_next_tree_view(struct tog_view *);
618 static const struct got_error *open_ref_view(struct tog_view *,
619 struct got_repository *);
620 static const struct got_error *show_ref_view(struct tog_view *);
621 static const struct got_error *input_ref_view(struct tog_view **,
622 struct tog_view *, int);
623 static const struct got_error *close_ref_view(struct tog_view *);
624 static const struct got_error *search_start_ref_view(struct tog_view *);
625 static const struct got_error *search_next_ref_view(struct tog_view *);
627 static volatile sig_atomic_t tog_sigwinch_received;
628 static volatile sig_atomic_t tog_sigpipe_received;
629 static volatile sig_atomic_t tog_sigcont_received;
630 static volatile sig_atomic_t tog_sigint_received;
631 static volatile sig_atomic_t tog_sigterm_received;
634 tog_sigwinch(int signo)
636 tog_sigwinch_received = 1;
640 tog_sigpipe(int signo)
642 tog_sigpipe_received = 1;
646 tog_sigcont(int signo)
648 tog_sigcont_received = 1;
652 tog_sigint(int signo)
654 tog_sigint_received = 1;
658 tog_sigterm(int signo)
660 tog_sigterm_received = 1;
664 tog_fatal_signal_received(void)
666 return (tog_sigpipe_received ||
667 tog_sigint_received || tog_sigint_received);
670 static const struct got_error *
671 view_close(struct tog_view *view)
673 const struct got_error *err = NULL, *child_err = NULL;
676 child_err = view_close(view->child);
680 err = view->close(view);
682 del_panel(view->panel);
684 delwin(view->window);
686 return err ? err : child_err;
689 static struct tog_view *
690 view_open(int nlines, int ncols, int begin_y, int begin_x,
691 enum tog_view_type type)
693 struct tog_view *view = calloc(1, sizeof(*view));
701 view->nlines = nlines ? nlines : LINES - begin_y;
702 view->ncols = ncols ? ncols : COLS - begin_x;
703 view->begin_y = begin_y;
704 view->begin_x = begin_x;
705 view->window = newwin(nlines, ncols, begin_y, begin_x);
706 if (view->window == NULL) {
710 view->panel = new_panel(view->window);
711 if (view->panel == NULL ||
712 set_panel_userptr(view->panel, view) != OK) {
717 keypad(view->window, TRUE);
722 view_split_begin_x(int begin_x)
724 if (begin_x > 0 || COLS < 120)
726 return (COLS - MAX(COLS / 2, 80));
729 /* XXX Stub till we decide what to do. */
731 view_split_begin_y(int lines)
733 return lines * HSPLIT_SCALE;
736 static const struct got_error *view_resize(struct tog_view *);
738 static const struct got_error *
739 view_splitscreen(struct tog_view *view)
741 const struct got_error *err = NULL;
743 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
744 if (view->resized_y && view->resized_y < view->lines)
745 view->begin_y = view->resized_y;
747 view->begin_y = view_split_begin_y(view->nlines);
749 } else if (!view->resized) {
750 if (view->resized_x && view->resized_x < view->cols - 1 &&
752 view->begin_x = view->resized_x;
754 view->begin_x = view_split_begin_x(0);
757 view->nlines = LINES - view->begin_y;
758 view->ncols = COLS - view->begin_x;
761 err = view_resize(view);
765 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
766 view->parent->nlines = view->begin_y;
768 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
769 return got_error_from_errno("mvwin");
774 static const struct got_error *
775 view_fullscreen(struct tog_view *view)
777 const struct got_error *err = NULL;
780 view->begin_y = view->resized ? view->begin_y : 0;
781 view->nlines = view->resized ? view->nlines : LINES;
785 err = view_resize(view);
789 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
790 return got_error_from_errno("mvwin");
796 view_is_parent_view(struct tog_view *view)
798 return view->parent == NULL;
802 view_is_splitscreen(struct tog_view *view)
804 return view->begin_x > 0 || view->begin_y > 0;
808 view_is_fullscreen(struct tog_view *view)
810 return view->nlines == LINES && view->ncols == COLS;
814 view_is_hsplit_top(struct tog_view *view)
816 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
817 view_is_splitscreen(view->child);
821 view_border(struct tog_view *view)
824 const struct tog_view *view_above;
827 return view_border(view->parent);
829 panel = panel_above(view->panel);
833 view_above = panel_userptr(panel);
834 if (view->mode == TOG_VIEW_SPLIT_HRZN)
835 mvwhline(view->window, view_above->begin_y - 1,
836 view->begin_x, got_locale_is_utf8() ?
837 ACS_HLINE : '-', view->ncols);
839 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
840 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
843 static const struct got_error *view_init_hsplit(struct tog_view *, int);
844 static const struct got_error *request_log_commits(struct tog_view *);
845 static const struct got_error *offset_selection_down(struct tog_view *);
846 static void offset_selection_up(struct tog_view *);
847 static void view_get_split(struct tog_view *, int *, int *);
849 static const struct got_error *
850 view_resize(struct tog_view *view)
852 const struct got_error *err = NULL;
853 int dif, nlines, ncols;
855 dif = LINES - view->lines; /* line difference */
857 if (view->lines > LINES)
858 nlines = view->nlines - (view->lines - LINES);
860 nlines = view->nlines + (LINES - view->lines);
861 if (view->cols > COLS)
862 ncols = view->ncols - (view->cols - COLS);
864 ncols = view->ncols + (COLS - view->cols);
867 int hs = view->child->begin_y;
869 if (!view_is_fullscreen(view))
870 view->child->begin_x = view_split_begin_x(view->begin_x);
871 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
872 view->child->begin_x == 0) {
875 view_fullscreen(view->child);
876 if (view->child->focussed)
877 show_panel(view->child->panel);
879 show_panel(view->panel);
881 ncols = view->child->begin_x;
883 view_splitscreen(view->child);
884 show_panel(view->child->panel);
887 * XXX This is ugly and needs to be moved into the above
888 * logic but "works" for now and my attempts at moving it
889 * break either 'tab' or 'F' key maps in horizontal splits.
892 err = view_splitscreen(view->child);
895 if (dif < 0) { /* top split decreased */
896 err = offset_selection_down(view);
903 show_panel(view->child->panel);
904 nlines = view->nlines;
906 } else if (view->parent == NULL)
909 if (view->resize && dif > 0) {
910 err = view->resize(view, dif);
915 if (wresize(view->window, nlines, ncols) == ERR)
916 return got_error_from_errno("wresize");
917 if (replace_panel(view->panel, view->window) == ERR)
918 return got_error_from_errno("replace_panel");
919 wclear(view->window);
921 view->nlines = nlines;
929 static const struct got_error *
930 resize_log_view(struct tog_view *view, int increase)
932 struct tog_log_view_state *s = &view->state.log;
933 const struct got_error *err = NULL;
936 if (s->selected_entry)
937 n = s->selected_entry->idx + view->lines - s->selected;
940 * Request commits to account for the increased
941 * height so we have enough to populate the view.
943 if (s->commits.ncommits < n) {
944 view->nscrolled = n - s->commits.ncommits + increase + 1;
945 err = request_log_commits(view);
952 view_adjust_offset(struct tog_view *view, int n)
957 if (view->parent && view->parent->offset) {
958 if (view->parent->offset + n >= 0)
959 view->parent->offset += n;
961 view->parent->offset = 0;
962 } else if (view->offset) {
963 if (view->offset - n >= 0)
970 static const struct got_error *
971 view_resize_split(struct tog_view *view, int resize)
973 const struct got_error *err = NULL;
974 struct tog_view *v = NULL;
981 if (!v->child || !view_is_splitscreen(v->child))
984 v->resized = v->child->resized = resize; /* lock for resize event */
986 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
987 if (v->child->resized_y)
988 v->child->begin_y = v->child->resized_y;
990 v->child->begin_y -= resize;
992 v->child->begin_y += resize;
993 if (v->child->begin_y < 3) {
995 v->child->begin_y = 3;
996 } else if (v->child->begin_y > LINES - 1) {
998 v->child->begin_y = LINES - 1;
1001 v->child->ncols = COLS;
1002 view_adjust_offset(view, resize);
1003 err = view_init_hsplit(v, v->child->begin_y);
1006 v->child->resized_y = v->child->begin_y;
1008 if (v->child->resized_x)
1009 v->child->begin_x = v->child->resized_x;
1011 v->child->begin_x -= resize;
1013 v->child->begin_x += resize;
1014 if (v->child->begin_x < 11) {
1016 v->child->begin_x = 11;
1017 } else if (v->child->begin_x > COLS - 1) {
1019 v->child->begin_x = COLS - 1;
1021 v->child->resized_x = v->child->begin_x;
1024 v->child->mode = v->mode;
1025 v->child->nlines = v->lines - v->child->begin_y;
1026 v->child->ncols = v->cols - v->child->begin_x;
1029 err = view_fullscreen(v);
1032 err = view_splitscreen(v->child);
1036 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1037 err = offset_selection_down(v->child);
1043 err = v->resize(v, 0);
1044 else if (v->child->resize)
1045 err = v->child->resize(v->child, 0);
1047 v->resized = v->child->resized = 0;
1053 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1055 struct tog_view *v = src->child ? src->child : src;
1057 dst->resized_x = v->resized_x;
1058 dst->resized_y = v->resized_y;
1061 static const struct got_error *
1062 view_close_child(struct tog_view *view)
1064 const struct got_error *err = NULL;
1066 if (view->child == NULL)
1069 err = view_close(view->child);
1074 static const struct got_error *
1075 view_set_child(struct tog_view *view, struct tog_view *child)
1077 const struct got_error *err = NULL;
1079 view->child = child;
1080 child->parent = view;
1082 err = view_resize(view);
1086 if (view->child->resized_x || view->child->resized_y)
1087 err = view_resize_split(view, 0);
1092 static const struct got_error *view_dispatch_request(struct tog_view **,
1093 struct tog_view *, enum tog_view_type, int, int);
1095 static const struct got_error *
1096 view_request_new(struct tog_view **requested, struct tog_view *view,
1097 enum tog_view_type request)
1099 struct tog_view *new_view = NULL;
1100 const struct got_error *err;
1105 if (view_is_parent_view(view))
1106 view_get_split(view, &y, &x);
1108 err = view_dispatch_request(&new_view, view, request, y, x);
1112 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1113 err = view_init_hsplit(view, y);
1119 new_view->focussed = 1;
1120 new_view->mode = view->mode;
1121 new_view->nlines = view->lines - y;
1123 if (view_is_parent_view(view)) {
1124 view_transfer_size(new_view, view);
1125 err = view_close_child(view);
1128 err = view_set_child(view, new_view);
1131 view->focus_child = 1;
1133 *requested = new_view;
1139 tog_resizeterm(void)
1142 struct winsize size;
1144 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1145 cols = 80; /* Default */
1149 lines = size.ws_row;
1151 resize_term(lines, cols);
1154 static const struct got_error *
1155 view_search_start(struct tog_view *view)
1157 const struct got_error *err = NULL;
1158 struct tog_view *v = view;
1162 if (view->search_started) {
1163 regfree(&view->regex);
1164 view->searching = 0;
1165 memset(&view->regmatch, 0, sizeof(view->regmatch));
1167 view->search_started = 0;
1169 if (view->nlines < 1)
1172 if (view_is_hsplit_top(view))
1175 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1176 wclrtoeol(v->window);
1178 nodelay(view->window, FALSE); /* block for search term input */
1181 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1182 wrefresh(v->window);
1185 nodelay(view->window, TRUE);
1189 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1190 err = view->search_start(view);
1192 regfree(&view->regex);
1195 view->search_started = 1;
1196 view->searching = TOG_SEARCH_FORWARD;
1197 view->search_next_done = 0;
1198 view->search_next(view);
1204 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1205 static const struct got_error *
1206 switch_split(struct tog_view *view)
1208 const struct got_error *err = NULL;
1209 struct tog_view *v = NULL;
1216 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1217 v->mode = TOG_VIEW_SPLIT_VERT;
1219 v->mode = TOG_VIEW_SPLIT_HRZN;
1223 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1224 v->mode = TOG_VIEW_SPLIT_NONE;
1226 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1227 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1228 v->child->begin_y = v->child->resized_y;
1229 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1230 v->child->begin_x = v->child->resized_x;
1233 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1235 v->child->ncols = COLS;
1236 v->child->nscrolled = LINES - v->child->nlines;
1238 err = view_init_hsplit(v, v->child->begin_y);
1242 v->child->mode = v->mode;
1243 v->child->nlines = v->lines - v->child->begin_y;
1246 err = view_fullscreen(v);
1249 err = view_splitscreen(v->child);
1253 if (v->mode == TOG_VIEW_SPLIT_NONE)
1254 v->mode = TOG_VIEW_SPLIT_VERT;
1255 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1256 err = offset_selection_down(v);
1259 err = offset_selection_down(v->child);
1263 offset_selection_up(v);
1264 offset_selection_up(v->child);
1267 err = v->resize(v, 0);
1268 else if (v->child->resize)
1269 err = v->child->resize(v->child, 0);
1275 * Compute view->count from numeric input. Assign total to view->count and
1276 * return first non-numeric key entered.
1279 get_compound_key(struct tog_view *view, int c)
1281 struct tog_view *v = view;
1284 if (view_is_hsplit_top(view))
1286 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1290 cbreak(); /* block for input */
1291 nodelay(view->window, FALSE);
1292 wmove(v->window, v->nlines - 1, 0);
1293 wclrtoeol(v->window);
1294 waddch(v->window, ':');
1297 x = getcurx(v->window);
1298 if (x != ERR && x < view->ncols) {
1299 waddch(v->window, c);
1300 wrefresh(v->window);
1304 * Don't overflow. Max valid request should be the greatest
1305 * between the longest and total lines; cap at 10 million.
1310 n = n * 10 + (c - '0');
1311 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1313 if (c == 'G' || c == 'g') { /* nG key map */
1314 view->gline = view->hiline = n;
1319 /* Massage excessive or inapplicable values at the input handler. */
1325 static const struct got_error *
1326 view_input(struct tog_view **new, int *done, struct tog_view *view,
1327 struct tog_view_list_head *views)
1329 const struct got_error *err = NULL;
1335 /* Clear "no matches" indicator. */
1336 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1337 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1338 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1342 if (view->searching && !view->search_next_done) {
1343 errcode = pthread_mutex_unlock(&tog_mutex);
1345 return got_error_set_errno(errcode,
1346 "pthread_mutex_unlock");
1348 errcode = pthread_mutex_lock(&tog_mutex);
1350 return got_error_set_errno(errcode,
1351 "pthread_mutex_lock");
1352 view->search_next(view);
1356 /* Allow threads to make progress while we are waiting for input. */
1357 errcode = pthread_mutex_unlock(&tog_mutex);
1359 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1360 /* If we have an unfinished count, let C-g or backspace abort. */
1361 if (view->count && --view->count) {
1363 nodelay(view->window, TRUE);
1364 ch = wgetch(view->window);
1365 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1370 ch = wgetch(view->window);
1371 if (ch >= '1' && ch <= '9')
1372 view->ch = ch = get_compound_key(view, ch);
1374 if (view->hiline && ch != ERR && ch != 0)
1375 view->hiline = 0; /* key pressed, clear line highlight */
1376 nodelay(view->window, TRUE);
1377 errcode = pthread_mutex_lock(&tog_mutex);
1379 return got_error_set_errno(errcode, "pthread_mutex_lock");
1381 if (tog_sigwinch_received || tog_sigcont_received) {
1383 tog_sigwinch_received = 0;
1384 tog_sigcont_received = 0;
1385 TAILQ_FOREACH(v, views, entry) {
1386 err = view_resize(v);
1389 err = v->input(new, v, KEY_RESIZE);
1393 err = view_resize(v->child);
1396 err = v->child->input(new, v->child,
1400 if (v->child->resized_x || v->child->resized_y) {
1401 err = view_resize_split(v, 0);
1414 view->child->focussed = 1;
1415 view->focus_child = 1;
1416 } else if (view->parent) {
1418 view->parent->focussed = 1;
1419 view->parent->focus_child = 0;
1420 if (!view_is_splitscreen(view)) {
1421 if (view->parent->resize) {
1422 err = view->parent->resize(view->parent,
1427 offset_selection_up(view->parent);
1428 err = view_fullscreen(view->parent);
1435 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1436 if (view->parent->resize) {
1437 /* might need more commits to fill fullscreen */
1438 err = view->parent->resize(view->parent, 0);
1442 offset_selection_up(view->parent);
1444 err = view->input(new, view, ch);
1452 if (view_is_parent_view(view)) {
1453 if (view->child == NULL)
1455 if (view_is_splitscreen(view->child)) {
1457 view->child->focussed = 1;
1458 err = view_fullscreen(view->child);
1460 err = view_splitscreen(view->child);
1462 err = view_resize_split(view, 0);
1466 err = view->child->input(new, view->child,
1469 if (view_is_splitscreen(view)) {
1470 view->parent->focussed = 0;
1472 err = view_fullscreen(view);
1474 err = view_splitscreen(view);
1475 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1476 err = view_resize(view->parent);
1478 err = view_resize_split(view, 0);
1482 err = view->input(new, view, KEY_RESIZE);
1487 err = view->resize(view, 0);
1492 err = offset_selection_down(view->parent);
1494 err = offset_selection_down(view);
1498 err = switch_split(view);
1501 err = view_resize_split(view, -1);
1504 err = view_resize_split(view, 1);
1510 if (view->search_start)
1511 view_search_start(view);
1513 err = view->input(new, view, ch);
1517 if (view->search_started && view->search_next) {
1518 view->searching = (ch == 'n' ?
1519 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1520 view->search_next_done = 0;
1521 view->search_next(view);
1523 err = view->input(new, view, ch);
1526 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1527 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1529 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1530 TAILQ_FOREACH(v, views, entry) {
1536 if (v->child && v->child->reset) {
1537 err = v->child->reset(v->child);
1544 err = view->input(new, view, ch);
1552 view_needs_focus_indication(struct tog_view *view)
1554 if (view_is_parent_view(view)) {
1555 if (view->child == NULL || view->child->focussed)
1557 if (!view_is_splitscreen(view->child))
1559 } else if (!view_is_splitscreen(view))
1562 return view->focussed;
1565 static const struct got_error *
1566 view_loop(struct tog_view *view)
1568 const struct got_error *err = NULL;
1569 struct tog_view_list_head views;
1570 struct tog_view *new_view;
1572 int fast_refresh = 10;
1573 int done = 0, errcode;
1575 mode = getenv("TOG_VIEW_SPLIT_MODE");
1576 if (!mode || !(*mode == 'h' || *mode == 'H'))
1577 view->mode = TOG_VIEW_SPLIT_VERT;
1579 view->mode = TOG_VIEW_SPLIT_HRZN;
1581 errcode = pthread_mutex_lock(&tog_mutex);
1583 return got_error_set_errno(errcode, "pthread_mutex_lock");
1586 TAILQ_INSERT_HEAD(&views, view, entry);
1589 err = view->show(view);
1594 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1595 !tog_fatal_signal_received()) {
1596 /* Refresh fast during initialization, then become slower. */
1597 if (fast_refresh && fast_refresh-- == 0)
1598 halfdelay(10); /* switch to once per second */
1600 err = view_input(&new_view, &done, view, &views);
1604 struct tog_view *v, *prev = NULL;
1606 if (view_is_parent_view(view))
1607 prev = TAILQ_PREV(view, tog_view_list_head,
1609 else if (view->parent)
1610 prev = view->parent;
1613 view->parent->child = NULL;
1614 view->parent->focus_child = 0;
1615 /* Restore fullscreen line height. */
1616 view->parent->nlines = view->parent->lines;
1617 err = view_resize(view->parent);
1620 /* Make resized splits persist. */
1621 view_transfer_size(view->parent, view);
1623 TAILQ_REMOVE(&views, view, entry);
1625 err = view_close(view);
1630 TAILQ_FOREACH(v, &views, entry) {
1634 if (view == NULL && new_view == NULL) {
1635 /* No view has focus. Try to pick one. */
1638 else if (!TAILQ_EMPTY(&views)) {
1639 view = TAILQ_LAST(&views,
1640 tog_view_list_head);
1643 if (view->focus_child) {
1644 view->child->focussed = 1;
1652 struct tog_view *v, *t;
1653 /* Only allow one parent view per type. */
1654 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1655 if (v->type != new_view->type)
1657 TAILQ_REMOVE(&views, v, entry);
1658 err = view_close(v);
1663 TAILQ_INSERT_TAIL(&views, new_view, entry);
1667 if (view_is_parent_view(view)) {
1668 if (view->child && view->child->focussed)
1671 if (view->parent && view->parent->focussed)
1672 view = view->parent;
1674 show_panel(view->panel);
1675 if (view->child && view_is_splitscreen(view->child))
1676 show_panel(view->child->panel);
1677 if (view->parent && view_is_splitscreen(view)) {
1678 err = view->parent->show(view->parent);
1682 err = view->show(view);
1686 err = view->child->show(view->child);
1695 while (!TAILQ_EMPTY(&views)) {
1696 const struct got_error *close_err;
1697 view = TAILQ_FIRST(&views);
1698 TAILQ_REMOVE(&views, view, entry);
1699 close_err = view_close(view);
1700 if (close_err && err == NULL)
1704 errcode = pthread_mutex_unlock(&tog_mutex);
1705 if (errcode && err == NULL)
1706 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1716 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1721 /* Create newly allocated wide-character string equivalent to a byte string. */
1722 static const struct got_error *
1723 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1726 const struct got_error *err = NULL;
1729 *wlen = mbstowcs(NULL, s, 0);
1730 if (*wlen == (size_t)-1) {
1732 if (errno != EILSEQ)
1733 return got_error_from_errno("mbstowcs");
1735 /* byte string invalid in current encoding; try to "fix" it */
1736 err = got_mbsavis(&vis, &vislen, s);
1739 *wlen = mbstowcs(NULL, vis, 0);
1740 if (*wlen == (size_t)-1) {
1741 err = got_error_from_errno("mbstowcs"); /* give up */
1746 *ws = calloc(*wlen + 1, sizeof(**ws));
1748 err = got_error_from_errno("calloc");
1752 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1753 err = got_error_from_errno("mbstowcs");
1764 static const struct got_error *
1765 expand_tab(char **ptr, const char *src)
1768 size_t len, n, idx = 0, sz = 0;
1771 n = len = strlen(src);
1772 dst = malloc(n + 1);
1774 return got_error_from_errno("malloc");
1776 while (idx < len && src[idx]) {
1777 const char c = src[idx];
1780 size_t nb = TABSIZE - sz % TABSIZE;
1783 p = realloc(dst, n + nb);
1786 return got_error_from_errno("realloc");
1791 memset(dst + sz, ' ', nb);
1794 dst[sz++] = src[idx];
1804 * Advance at most n columns from wline starting at offset off.
1805 * Return the index to the first character after the span operation.
1806 * Return the combined column width of all spanned wide character in
1810 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1812 int width, i, cols = 0;
1819 for (i = off; wline[i] != L'\0'; ++i) {
1820 if (wline[i] == L'\t')
1821 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1823 width = wcwidth(wline[i]);
1830 if (cols + width > n)
1840 * Format a line for display, ensuring that it won't overflow a width limit.
1841 * With scrolling, the width returned refers to the scrolled version of the
1842 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1844 static const struct got_error *
1845 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1846 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1848 const struct got_error *err = NULL;
1850 wchar_t *wline = NULL;
1859 err = expand_tab(&exstr, line);
1864 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1869 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1871 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1872 wline[wlen - 1] = L'\0';
1875 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1876 wline[wlen - 1] = L'\0';
1880 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1886 *scrollxp = scrollx;
1894 static const struct got_error*
1895 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1896 struct got_object_id *id, struct got_repository *repo)
1898 static const struct got_error *err = NULL;
1899 struct got_reflist_entry *re;
1905 TAILQ_FOREACH(re, refs, entry) {
1906 struct got_tag_object *tag = NULL;
1907 struct got_object_id *ref_id;
1910 name = got_ref_get_name(re->ref);
1911 if (strcmp(name, GOT_REF_HEAD) == 0)
1913 if (strncmp(name, "refs/", 5) == 0)
1915 if (strncmp(name, "got/", 4) == 0 &&
1916 strncmp(name, "got/backup/", 11) != 0)
1918 if (strncmp(name, "heads/", 6) == 0)
1920 if (strncmp(name, "remotes/", 8) == 0) {
1922 s = strstr(name, "/" GOT_REF_HEAD);
1923 if (s != NULL && s[strlen(s)] == '\0')
1926 err = got_ref_resolve(&ref_id, repo, re->ref);
1929 if (strncmp(name, "tags/", 5) == 0) {
1930 err = got_object_open_as_tag(&tag, repo, ref_id);
1932 if (err->code != GOT_ERR_OBJ_TYPE) {
1936 /* Ref points at something other than a tag. */
1941 cmp = got_object_id_cmp(tag ?
1942 got_object_tag_get_object_id(tag) : ref_id, id);
1945 got_object_tag_close(tag);
1949 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1950 s ? ", " : "", name) == -1) {
1951 err = got_error_from_errno("asprintf");
1962 static const struct got_error *
1963 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1968 smallerthan = strchr(author, '<');
1969 if (smallerthan && smallerthan[1] != '\0')
1970 author = smallerthan + 1;
1971 author[strcspn(author, "@>")] = '\0';
1972 return format_line(wauthor, author_width, NULL, author, 0, limit,
1976 static const struct got_error *
1977 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1978 struct got_object_id *id, const size_t date_display_cols,
1979 int author_display_cols)
1981 struct tog_log_view_state *s = &view->state.log;
1982 const struct got_error *err = NULL;
1983 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1984 char *logmsg0 = NULL, *logmsg = NULL;
1985 char *author = NULL;
1986 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1987 int author_width, logmsg_width;
1988 char *newline, *line = NULL;
1989 int col, limit, scrollx;
1990 const int avail = view->ncols;
1992 time_t committer_time;
1993 struct tog_color *tc;
1995 committer_time = got_object_commit_get_committer_time(commit);
1996 if (gmtime_r(&committer_time, &tm) == NULL)
1997 return got_error_from_errno("gmtime_r");
1998 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1999 return got_error(GOT_ERR_NO_SPACE);
2001 if (avail <= date_display_cols)
2002 limit = MIN(sizeof(datebuf) - 1, avail);
2004 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2005 tc = get_color(&s->colors, TOG_COLOR_DATE);
2007 wattr_on(view->window,
2008 COLOR_PAIR(tc->colorpair), NULL);
2009 waddnstr(view->window, datebuf, limit);
2011 wattr_off(view->window,
2012 COLOR_PAIR(tc->colorpair), NULL);
2019 err = got_object_id_str(&id_str, id);
2022 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2024 wattr_on(view->window,
2025 COLOR_PAIR(tc->colorpair), NULL);
2026 wprintw(view->window, "%.8s ", id_str);
2028 wattr_off(view->window,
2029 COLOR_PAIR(tc->colorpair), NULL);
2036 if (s->use_committer)
2037 author = strdup(got_object_commit_get_committer(commit));
2039 author = strdup(got_object_commit_get_author(commit));
2040 if (author == NULL) {
2041 err = got_error_from_errno("strdup");
2044 err = format_author(&wauthor, &author_width, author, avail - col, col);
2047 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2049 wattr_on(view->window,
2050 COLOR_PAIR(tc->colorpair), NULL);
2051 waddwstr(view->window, wauthor);
2053 wattr_off(view->window,
2054 COLOR_PAIR(tc->colorpair), NULL);
2055 col += author_width;
2056 while (col < avail && author_width < author_display_cols + 2) {
2057 waddch(view->window, ' ');
2064 err = got_object_commit_get_logmsg(&logmsg0, commit);
2068 while (*logmsg == '\n')
2070 newline = strchr(logmsg, '\n');
2073 limit = avail - col;
2074 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2075 limit--; /* for the border */
2076 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2080 waddwstr(view->window, &wlogmsg[scrollx]);
2081 col += MAX(logmsg_width, 0);
2082 while (col < avail) {
2083 waddch(view->window, ' ');
2095 static struct commit_queue_entry *
2096 alloc_commit_queue_entry(struct got_commit_object *commit,
2097 struct got_object_id *id)
2099 struct commit_queue_entry *entry;
2101 entry = calloc(1, sizeof(*entry));
2106 entry->commit = commit;
2111 pop_commit(struct commit_queue *commits)
2113 struct commit_queue_entry *entry;
2115 entry = TAILQ_FIRST(&commits->head);
2116 TAILQ_REMOVE(&commits->head, entry, entry);
2117 got_object_commit_close(entry->commit);
2118 commits->ncommits--;
2119 /* Don't free entry->id! It is owned by the commit graph. */
2124 free_commits(struct commit_queue *commits)
2126 while (!TAILQ_EMPTY(&commits->head))
2127 pop_commit(commits);
2130 static const struct got_error *
2131 match_commit(int *have_match, struct got_object_id *id,
2132 struct got_commit_object *commit, regex_t *regex)
2134 const struct got_error *err = NULL;
2135 regmatch_t regmatch;
2136 char *id_str = NULL, *logmsg = NULL;
2140 err = got_object_id_str(&id_str, id);
2144 err = got_object_commit_get_logmsg(&logmsg, commit);
2148 if (regexec(regex, got_object_commit_get_author(commit), 1,
2149 ®match, 0) == 0 ||
2150 regexec(regex, got_object_commit_get_committer(commit), 1,
2151 ®match, 0) == 0 ||
2152 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2153 regexec(regex, logmsg, 1, ®match, 0) == 0)
2161 static const struct got_error *
2162 queue_commits(struct tog_log_thread_args *a)
2164 const struct got_error *err = NULL;
2167 * We keep all commits open throughout the lifetime of the log
2168 * view in order to avoid having to re-fetch commits from disk
2169 * while updating the display.
2172 struct got_object_id *id;
2173 struct got_commit_object *commit;
2174 struct commit_queue_entry *entry;
2177 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2179 if (err || id == NULL)
2182 err = got_object_open_as_commit(&commit, a->repo, id);
2185 entry = alloc_commit_queue_entry(commit, id);
2186 if (entry == NULL) {
2187 err = got_error_from_errno("alloc_commit_queue_entry");
2191 errcode = pthread_mutex_lock(&tog_mutex);
2193 err = got_error_set_errno(errcode,
2194 "pthread_mutex_lock");
2198 entry->idx = a->commits->ncommits;
2199 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2200 a->commits->ncommits++;
2202 if (*a->searching == TOG_SEARCH_FORWARD &&
2203 !*a->search_next_done) {
2205 err = match_commit(&have_match, id, commit, a->regex);
2209 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2212 errcode = pthread_mutex_unlock(&tog_mutex);
2213 if (errcode && err == NULL)
2214 err = got_error_set_errno(errcode,
2215 "pthread_mutex_unlock");
2218 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2224 select_commit(struct tog_log_view_state *s)
2226 struct commit_queue_entry *entry;
2229 entry = s->first_displayed_entry;
2231 if (ncommits == s->selected) {
2232 s->selected_entry = entry;
2235 entry = TAILQ_NEXT(entry, entry);
2240 static const struct got_error *
2241 draw_commits(struct tog_view *view)
2243 const struct got_error *err = NULL;
2244 struct tog_log_view_state *s = &view->state.log;
2245 struct commit_queue_entry *entry = s->selected_entry;
2246 const int limit = view->nlines;
2248 int ncommits, author_cols = 4;
2249 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2250 char *refs_str = NULL;
2252 struct tog_color *tc;
2253 static const size_t date_display_cols = 12;
2255 if (s->selected_entry &&
2256 !(view->searching && view->search_next_done == 0)) {
2257 struct got_reflist_head *refs;
2258 err = got_object_id_str(&id_str, s->selected_entry->id);
2261 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2262 s->selected_entry->id);
2264 err = build_refs_str(&refs_str, refs,
2265 s->selected_entry->id, s->repo);
2271 if (s->thread_args.commits_needed == 0)
2272 halfdelay(10); /* disable fast refresh */
2274 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2275 if (asprintf(&ncommits_str, " [%d/%d] %s",
2276 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2277 (view->searching && !view->search_next_done) ?
2278 "searching..." : "loading...") == -1) {
2279 err = got_error_from_errno("asprintf");
2283 const char *search_str = NULL;
2285 if (view->searching) {
2286 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2287 search_str = "no more matches";
2288 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2289 search_str = "no matches found";
2290 else if (!view->search_next_done)
2291 search_str = "searching...";
2294 if (asprintf(&ncommits_str, " [%d/%d] %s",
2295 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2296 search_str ? search_str :
2297 (refs_str ? refs_str : "")) == -1) {
2298 err = got_error_from_errno("asprintf");
2303 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2304 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2305 "........................................",
2306 s->in_repo_path, ncommits_str) == -1) {
2307 err = got_error_from_errno("asprintf");
2311 } else if (asprintf(&header, "commit %s%s",
2312 id_str ? id_str : "........................................",
2313 ncommits_str) == -1) {
2314 err = got_error_from_errno("asprintf");
2318 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2322 werase(view->window);
2324 if (view_needs_focus_indication(view))
2325 wstandout(view->window);
2326 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2328 wattr_on(view->window,
2329 COLOR_PAIR(tc->colorpair), NULL);
2330 waddwstr(view->window, wline);
2332 wattr_off(view->window,
2333 COLOR_PAIR(tc->colorpair), NULL);
2334 while (width < view->ncols) {
2335 waddch(view->window, ' ');
2338 if (view_needs_focus_indication(view))
2339 wstandend(view->window);
2344 /* Grow author column size if necessary, and set view->maxx. */
2345 entry = s->first_displayed_entry;
2349 struct got_commit_object *c = entry->commit;
2350 char *author, *eol, *msg, *msg0;
2351 wchar_t *wauthor, *wmsg;
2353 if (ncommits >= limit - 1)
2355 if (s->use_committer)
2356 author = strdup(got_object_commit_get_committer(c));
2358 author = strdup(got_object_commit_get_author(c));
2359 if (author == NULL) {
2360 err = got_error_from_errno("strdup");
2363 err = format_author(&wauthor, &width, author, COLS,
2365 if (author_cols < width)
2366 author_cols = width;
2371 err = got_object_commit_get_logmsg(&msg0, c);
2375 while (*msg == '\n')
2377 if ((eol = strchr(msg, '\n')))
2379 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2380 date_display_cols + author_cols, 0);
2383 view->maxx = MAX(view->maxx, width);
2387 entry = TAILQ_NEXT(entry, entry);
2390 entry = s->first_displayed_entry;
2391 s->last_displayed_entry = s->first_displayed_entry;
2394 if (ncommits >= limit - 1)
2396 if (ncommits == s->selected)
2397 wstandout(view->window);
2398 err = draw_commit(view, entry->commit, entry->id,
2399 date_display_cols, author_cols);
2400 if (ncommits == s->selected)
2401 wstandend(view->window);
2405 s->last_displayed_entry = entry;
2406 entry = TAILQ_NEXT(entry, entry);
2419 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2421 struct commit_queue_entry *entry;
2424 entry = TAILQ_FIRST(&s->commits.head);
2425 if (s->first_displayed_entry == entry)
2428 entry = s->first_displayed_entry;
2429 while (entry && nscrolled < maxscroll) {
2430 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2432 s->first_displayed_entry = entry;
2438 static const struct got_error *
2439 trigger_log_thread(struct tog_view *view, int wait)
2441 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2444 halfdelay(1); /* fast refresh while loading commits */
2446 while (!ta->log_complete && !tog_thread_error &&
2447 (ta->commits_needed > 0 || ta->load_all)) {
2448 /* Wake the log thread. */
2449 errcode = pthread_cond_signal(&ta->need_commits);
2451 return got_error_set_errno(errcode,
2452 "pthread_cond_signal");
2455 * The mutex will be released while the view loop waits
2456 * in wgetch(), at which time the log thread will run.
2461 /* Display progress update in log view. */
2462 show_log_view(view);
2466 /* Wait right here while next commit is being loaded. */
2467 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2469 return got_error_set_errno(errcode,
2470 "pthread_cond_wait");
2472 /* Display progress update in log view. */
2473 show_log_view(view);
2481 static const struct got_error *
2482 request_log_commits(struct tog_view *view)
2484 struct tog_log_view_state *state = &view->state.log;
2485 const struct got_error *err = NULL;
2487 if (state->thread_args.log_complete)
2490 state->thread_args.commits_needed += view->nscrolled;
2491 err = trigger_log_thread(view, 1);
2492 view->nscrolled = 0;
2497 static const struct got_error *
2498 log_scroll_down(struct tog_view *view, int maxscroll)
2500 struct tog_log_view_state *s = &view->state.log;
2501 const struct got_error *err = NULL;
2502 struct commit_queue_entry *pentry;
2503 int nscrolled = 0, ncommits_needed;
2505 if (s->last_displayed_entry == NULL)
2508 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2509 if (s->commits.ncommits < ncommits_needed &&
2510 !s->thread_args.log_complete) {
2512 * Ask the log thread for required amount of commits.
2514 s->thread_args.commits_needed +=
2515 ncommits_needed - s->commits.ncommits;
2516 err = trigger_log_thread(view, 1);
2522 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2523 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2526 s->last_displayed_entry = pentry ?
2527 pentry : s->last_displayed_entry;;
2529 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2532 s->first_displayed_entry = pentry;
2533 } while (++nscrolled < maxscroll);
2535 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2536 view->nscrolled += nscrolled;
2538 view->nscrolled = 0;
2543 static const struct got_error *
2544 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2545 struct got_commit_object *commit, struct got_object_id *commit_id,
2546 struct tog_view *log_view, struct got_repository *repo)
2548 const struct got_error *err;
2549 struct got_object_qid *parent_id;
2550 struct tog_view *diff_view;
2552 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2553 if (diff_view == NULL)
2554 return got_error_from_errno("view_open");
2556 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2557 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2558 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2560 *new_view = diff_view;
2564 static const struct got_error *
2565 tree_view_visit_subtree(struct tog_tree_view_state *s,
2566 struct got_tree_object *subtree)
2568 struct tog_parent_tree *parent;
2570 parent = calloc(1, sizeof(*parent));
2572 return got_error_from_errno("calloc");
2574 parent->tree = s->tree;
2575 parent->first_displayed_entry = s->first_displayed_entry;
2576 parent->selected_entry = s->selected_entry;
2577 parent->selected = s->selected;
2578 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2581 s->first_displayed_entry = NULL;
2585 static const struct got_error *
2586 tree_view_walk_path(struct tog_tree_view_state *s,
2587 struct got_commit_object *commit, const char *path)
2589 const struct got_error *err = NULL;
2590 struct got_tree_object *tree = NULL;
2592 char *slash, *subpath = NULL;
2594 /* Walk the path and open corresponding tree objects. */
2597 struct got_tree_entry *te;
2598 struct got_object_id *tree_id;
2604 /* Ensure the correct subtree entry is selected. */
2605 slash = strchr(p, '/');
2607 te_name = strdup(p);
2609 te_name = strndup(p, slash - p);
2610 if (te_name == NULL) {
2611 err = got_error_from_errno("strndup");
2614 te = got_object_tree_find_entry(s->tree, te_name);
2616 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2621 s->first_displayed_entry = s->selected_entry = te;
2623 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2624 break; /* jump to this file's entry */
2626 slash = strchr(p, '/');
2628 subpath = strndup(path, slash - path);
2630 subpath = strdup(path);
2631 if (subpath == NULL) {
2632 err = got_error_from_errno("strdup");
2636 err = got_object_id_by_path(&tree_id, s->repo, commit,
2641 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2646 err = tree_view_visit_subtree(s, tree);
2648 got_object_tree_close(tree);
2662 static const struct got_error *
2663 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2664 struct commit_queue_entry *entry, const char *path,
2665 const char *head_ref_name, struct got_repository *repo)
2667 const struct got_error *err = NULL;
2668 struct tog_tree_view_state *s;
2669 struct tog_view *tree_view;
2671 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2672 if (tree_view == NULL)
2673 return got_error_from_errno("view_open");
2675 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2678 s = &tree_view->state.tree;
2680 *new_view = tree_view;
2682 if (got_path_is_root_dir(path))
2685 return tree_view_walk_path(s, entry->commit, path);
2688 static const struct got_error *
2689 block_signals_used_by_main_thread(void)
2694 if (sigemptyset(&sigset) == -1)
2695 return got_error_from_errno("sigemptyset");
2697 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2698 if (sigaddset(&sigset, SIGWINCH) == -1)
2699 return got_error_from_errno("sigaddset");
2700 if (sigaddset(&sigset, SIGCONT) == -1)
2701 return got_error_from_errno("sigaddset");
2702 if (sigaddset(&sigset, SIGINT) == -1)
2703 return got_error_from_errno("sigaddset");
2704 if (sigaddset(&sigset, SIGTERM) == -1)
2705 return got_error_from_errno("sigaddset");
2707 /* ncurses handles SIGTSTP */
2708 if (sigaddset(&sigset, SIGTSTP) == -1)
2709 return got_error_from_errno("sigaddset");
2711 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2713 return got_error_set_errno(errcode, "pthread_sigmask");
2719 log_thread(void *arg)
2721 const struct got_error *err = NULL;
2723 struct tog_log_thread_args *a = arg;
2727 * Sync startup with main thread such that we begin our
2728 * work once view_input() has released the mutex.
2730 errcode = pthread_mutex_lock(&tog_mutex);
2732 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2736 err = block_signals_used_by_main_thread();
2738 pthread_mutex_unlock(&tog_mutex);
2742 while (!done && !err && !tog_fatal_signal_received()) {
2743 errcode = pthread_mutex_unlock(&tog_mutex);
2745 err = got_error_set_errno(errcode,
2746 "pthread_mutex_unlock");
2749 err = queue_commits(a);
2751 if (err->code != GOT_ERR_ITER_COMPLETED)
2755 } else if (a->commits_needed > 0 && !a->load_all)
2756 a->commits_needed--;
2758 errcode = pthread_mutex_lock(&tog_mutex);
2760 err = got_error_set_errno(errcode,
2761 "pthread_mutex_lock");
2763 } else if (*a->quit)
2765 else if (*a->first_displayed_entry == NULL) {
2766 *a->first_displayed_entry =
2767 TAILQ_FIRST(&a->commits->head);
2768 *a->selected_entry = *a->first_displayed_entry;
2771 errcode = pthread_cond_signal(&a->commit_loaded);
2773 err = got_error_set_errno(errcode,
2774 "pthread_cond_signal");
2775 pthread_mutex_unlock(&tog_mutex);
2780 a->commits_needed = 0;
2782 if (a->commits_needed == 0 && !a->load_all) {
2783 errcode = pthread_cond_wait(&a->need_commits,
2786 err = got_error_set_errno(errcode,
2787 "pthread_cond_wait");
2788 pthread_mutex_unlock(&tog_mutex);
2796 a->log_complete = 1;
2797 errcode = pthread_mutex_unlock(&tog_mutex);
2799 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2802 tog_thread_error = 1;
2803 pthread_cond_signal(&a->commit_loaded);
2808 static const struct got_error *
2809 stop_log_thread(struct tog_log_view_state *s)
2811 const struct got_error *err = NULL, *thread_err = NULL;
2816 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2818 return got_error_set_errno(errcode,
2819 "pthread_cond_signal");
2820 errcode = pthread_mutex_unlock(&tog_mutex);
2822 return got_error_set_errno(errcode,
2823 "pthread_mutex_unlock");
2824 errcode = pthread_join(s->thread, (void **)&thread_err);
2826 return got_error_set_errno(errcode, "pthread_join");
2827 errcode = pthread_mutex_lock(&tog_mutex);
2829 return got_error_set_errno(errcode,
2830 "pthread_mutex_lock");
2831 s->thread = 0; //NULL;
2834 if (s->thread_args.repo) {
2835 err = got_repo_close(s->thread_args.repo);
2836 s->thread_args.repo = NULL;
2839 if (s->thread_args.pack_fds) {
2840 const struct got_error *pack_err =
2841 got_repo_pack_fds_close(s->thread_args.pack_fds);
2844 s->thread_args.pack_fds = NULL;
2847 if (s->thread_args.graph) {
2848 got_commit_graph_close(s->thread_args.graph);
2849 s->thread_args.graph = NULL;
2852 return err ? err : thread_err;
2855 static const struct got_error *
2856 close_log_view(struct tog_view *view)
2858 const struct got_error *err = NULL;
2859 struct tog_log_view_state *s = &view->state.log;
2862 err = stop_log_thread(s);
2864 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2865 if (errcode && err == NULL)
2866 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2868 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2869 if (errcode && err == NULL)
2870 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2872 free_commits(&s->commits);
2873 free(s->in_repo_path);
2874 s->in_repo_path = NULL;
2877 free(s->head_ref_name);
2878 s->head_ref_name = NULL;
2882 static const struct got_error *
2883 search_start_log_view(struct tog_view *view)
2885 struct tog_log_view_state *s = &view->state.log;
2887 s->matched_entry = NULL;
2888 s->search_entry = NULL;
2892 static const struct got_error *
2893 search_next_log_view(struct tog_view *view)
2895 const struct got_error *err = NULL;
2896 struct tog_log_view_state *s = &view->state.log;
2897 struct commit_queue_entry *entry;
2899 /* Display progress update in log view. */
2900 show_log_view(view);
2904 if (s->search_entry) {
2906 errcode = pthread_mutex_unlock(&tog_mutex);
2908 return got_error_set_errno(errcode,
2909 "pthread_mutex_unlock");
2910 ch = wgetch(view->window);
2911 errcode = pthread_mutex_lock(&tog_mutex);
2913 return got_error_set_errno(errcode,
2914 "pthread_mutex_lock");
2915 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2916 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2919 if (view->searching == TOG_SEARCH_FORWARD)
2920 entry = TAILQ_NEXT(s->search_entry, entry);
2922 entry = TAILQ_PREV(s->search_entry,
2923 commit_queue_head, entry);
2924 } else if (s->matched_entry) {
2925 int matched_idx = s->matched_entry->idx;
2926 int selected_idx = s->selected_entry->idx;
2929 * If the user has moved the cursor after we hit a match,
2930 * the position from where we should continue searching
2931 * might have changed.
2933 if (view->searching == TOG_SEARCH_FORWARD) {
2934 if (matched_idx > selected_idx)
2935 entry = TAILQ_NEXT(s->selected_entry, entry);
2937 entry = TAILQ_NEXT(s->matched_entry, entry);
2939 if (matched_idx < selected_idx)
2940 entry = TAILQ_PREV(s->selected_entry,
2941 commit_queue_head, entry);
2943 entry = TAILQ_PREV(s->matched_entry,
2944 commit_queue_head, entry);
2947 entry = s->selected_entry;
2953 if (entry == NULL) {
2954 if (s->thread_args.log_complete ||
2955 view->searching == TOG_SEARCH_BACKWARD) {
2956 view->search_next_done =
2957 (s->matched_entry == NULL ?
2958 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2959 s->search_entry = NULL;
2963 * Poke the log thread for more commits and return,
2964 * allowing the main loop to make progress. Search
2965 * will resume at s->search_entry once we come back.
2967 s->thread_args.commits_needed++;
2968 return trigger_log_thread(view, 0);
2971 err = match_commit(&have_match, entry->id, entry->commit,
2976 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2977 s->matched_entry = entry;
2981 s->search_entry = entry;
2982 if (view->searching == TOG_SEARCH_FORWARD)
2983 entry = TAILQ_NEXT(entry, entry);
2985 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2988 if (s->matched_entry) {
2989 int cur = s->selected_entry->idx;
2990 while (cur < s->matched_entry->idx) {
2991 err = input_log_view(NULL, view, KEY_DOWN);
2996 while (cur > s->matched_entry->idx) {
2997 err = input_log_view(NULL, view, KEY_UP);
3004 s->search_entry = NULL;
3009 static const struct got_error *
3010 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3011 struct got_repository *repo, const char *head_ref_name,
3012 const char *in_repo_path, int log_branches)
3014 const struct got_error *err = NULL;
3015 struct tog_log_view_state *s = &view->state.log;
3016 struct got_repository *thread_repo = NULL;
3017 struct got_commit_graph *thread_graph = NULL;
3020 if (in_repo_path != s->in_repo_path) {
3021 free(s->in_repo_path);
3022 s->in_repo_path = strdup(in_repo_path);
3023 if (s->in_repo_path == NULL)
3024 return got_error_from_errno("strdup");
3027 /* The commit queue only contains commits being displayed. */
3028 TAILQ_INIT(&s->commits.head);
3029 s->commits.ncommits = 0;
3032 if (head_ref_name) {
3033 s->head_ref_name = strdup(head_ref_name);
3034 if (s->head_ref_name == NULL) {
3035 err = got_error_from_errno("strdup");
3039 s->start_id = got_object_id_dup(start_id);
3040 if (s->start_id == NULL) {
3041 err = got_error_from_errno("got_object_id_dup");
3044 s->log_branches = log_branches;
3046 STAILQ_INIT(&s->colors);
3047 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3048 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3049 get_color_value("TOG_COLOR_COMMIT"));
3052 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3053 get_color_value("TOG_COLOR_AUTHOR"));
3055 free_colors(&s->colors);
3058 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3059 get_color_value("TOG_COLOR_DATE"));
3061 free_colors(&s->colors);
3066 view->show = show_log_view;
3067 view->input = input_log_view;
3068 view->resize = resize_log_view;
3069 view->close = close_log_view;
3070 view->search_start = search_start_log_view;
3071 view->search_next = search_next_log_view;
3073 if (s->thread_args.pack_fds == NULL) {
3074 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3078 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3079 s->thread_args.pack_fds);
3082 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3086 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3087 s->repo, NULL, NULL);
3091 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3093 err = got_error_set_errno(errcode, "pthread_cond_init");
3096 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3098 err = got_error_set_errno(errcode, "pthread_cond_init");
3102 s->thread_args.commits_needed = view->nlines;
3103 s->thread_args.graph = thread_graph;
3104 s->thread_args.commits = &s->commits;
3105 s->thread_args.in_repo_path = s->in_repo_path;
3106 s->thread_args.start_id = s->start_id;
3107 s->thread_args.repo = thread_repo;
3108 s->thread_args.log_complete = 0;
3109 s->thread_args.quit = &s->quit;
3110 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3111 s->thread_args.selected_entry = &s->selected_entry;
3112 s->thread_args.searching = &view->searching;
3113 s->thread_args.search_next_done = &view->search_next_done;
3114 s->thread_args.regex = &view->regex;
3117 close_log_view(view);
3121 static const struct got_error *
3122 show_log_view(struct tog_view *view)
3124 const struct got_error *err;
3125 struct tog_log_view_state *s = &view->state.log;
3127 if (s->thread == 0) { //NULL) {
3128 int errcode = pthread_create(&s->thread, NULL, log_thread,
3131 return got_error_set_errno(errcode, "pthread_create");
3132 if (s->thread_args.commits_needed > 0) {
3133 err = trigger_log_thread(view, 1);
3139 return draw_commits(view);
3143 log_move_cursor_up(struct tog_view *view, int page, int home)
3145 struct tog_log_view_state *s = &view->state.log;
3147 if (s->selected_entry->idx == 0)
3149 if (s->first_displayed_entry == NULL)
3152 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3154 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3156 if (!page && !home && s->selected > 0)
3159 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3165 static const struct got_error *
3166 log_move_cursor_down(struct tog_view *view, int page)
3168 struct tog_log_view_state *s = &view->state.log;
3169 struct commit_queue_entry *first;
3170 const struct got_error *err = NULL;
3172 first = s->first_displayed_entry;
3173 if (first == NULL) {
3178 if (s->thread_args.log_complete &&
3179 s->selected_entry->idx >= s->commits.ncommits - 1)
3183 int eos = view->nlines - 2;
3185 if (view_is_hsplit_top(view))
3186 --eos; /* border consumes the last line */
3187 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3190 err = log_scroll_down(view, 1);
3191 } else if (s->thread_args.load_all) {
3192 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3193 s->selected += MIN(s->last_displayed_entry->idx -
3194 s->selected_entry->idx, page + 1);
3196 err = log_scroll_down(view, MIN(page,
3197 s->commits.ncommits - s->selected_entry->idx - 1));
3198 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3200 err = log_scroll_down(view, page);
3203 if (first == s->first_displayed_entry && s->selected <
3204 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3205 s->selected = MIN(s->commits.ncommits - 1, page);
3212 * We might necessarily overshoot in horizontal
3213 * splits; if so, select the last displayed commit.
3215 s->selected = MIN(s->selected,
3216 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3220 if (s->thread_args.log_complete &&
3221 s->selected_entry->idx == s->commits.ncommits - 1)
3228 view_get_split(struct tog_view *view, int *y, int *x)
3233 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3234 if (view->child && view->child->resized_y)
3235 *y = view->child->resized_y;
3236 else if (view->resized_y)
3237 *y = view->resized_y;
3239 *y = view_split_begin_y(view->lines);
3240 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3241 if (view->child && view->child->resized_x)
3242 *x = view->child->resized_x;
3243 else if (view->resized_x)
3244 *x = view->resized_x;
3246 *x = view_split_begin_x(view->begin_x);
3250 /* Split view horizontally at y and offset view->state->selected line. */
3251 static const struct got_error *
3252 view_init_hsplit(struct tog_view *view, int y)
3254 const struct got_error *err = NULL;
3258 err = view_resize(view);
3262 err = offset_selection_down(view);
3267 static const struct got_error *
3268 log_goto_line(struct tog_view *view, int nlines)
3270 const struct got_error *err = NULL;
3271 struct tog_log_view_state *s = &view->state.log;
3272 int g, idx = s->selected_entry->idx;
3277 if (g >= s->first_displayed_entry->idx + 1 &&
3278 g <= s->last_displayed_entry->idx + 1 &&
3279 g - s->first_displayed_entry->idx - 1 < nlines) {
3280 s->selected = g - s->first_displayed_entry->idx - 1;
3286 err = log_move_cursor_down(view, g - idx - 1);
3287 if (!err && g > s->selected_entry->idx + 1)
3288 err = log_move_cursor_down(view,
3289 g - s->first_displayed_entry->idx - 1);
3292 } else if (idx + 1 > g)
3293 log_move_cursor_up(view, idx - g + 1, 0);
3295 if (g < nlines && s->first_displayed_entry->idx == 0)
3296 s->selected = g - 1;
3303 static const struct got_error *
3304 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3306 const struct got_error *err = NULL;
3307 struct tog_log_view_state *s = &view->state.log;
3308 struct commit_queue_entry *entry;
3309 int eos, n, nscroll;
3311 if (s->thread_args.load_all) {
3312 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3313 s->thread_args.load_all = 0;
3314 else if (s->thread_args.log_complete) {
3315 err = log_move_cursor_down(view, s->commits.ncommits);
3316 s->thread_args.load_all = 0;
3321 eos = nscroll = view->nlines - 1;
3322 if (view_is_hsplit_top(view))
3326 return log_goto_line(view, eos);
3336 view->x = MAX(view->maxx - view->ncols / 2, 0);
3341 if (view->x + view->ncols / 2 < view->maxx)
3342 view->x += 2; /* move two columns right */
3348 view->x -= MIN(view->x, 2); /* move two columns back */
3357 log_move_cursor_up(view, 0, 0);
3361 log_move_cursor_up(view, 0, 1);
3371 log_move_cursor_up(view, nscroll, 0);
3378 err = log_move_cursor_down(view, 0);
3381 s->use_committer = !s->use_committer;
3385 /* We don't know yet how many commits, so we're forced to
3386 * traverse them all. */
3388 if (!s->thread_args.log_complete) {
3389 s->thread_args.load_all = 1;
3390 return trigger_log_thread(view, 0);
3394 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3395 for (n = 0; n < eos; n++) {
3398 s->first_displayed_entry = entry;
3399 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3402 s->selected = n - 1;
3414 err = log_move_cursor_down(view, nscroll);
3417 if (s->selected > view->nlines - 2)
3418 s->selected = view->nlines - 2;
3419 if (s->selected > s->commits.ncommits - 1)
3420 s->selected = s->commits.ncommits - 1;
3422 if (s->commits.ncommits < view->nlines - 1 &&
3423 !s->thread_args.log_complete) {
3424 s->thread_args.commits_needed += (view->nlines - 1) -
3425 s->commits.ncommits;
3426 err = trigger_log_thread(view, 1);
3432 if (s->selected_entry == NULL)
3434 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3438 if (s->selected_entry == NULL)
3440 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3446 if (ch == KEY_BACKSPACE &&
3447 got_path_is_root_dir(s->in_repo_path))
3449 err = stop_log_thread(s);
3452 if (ch == KEY_BACKSPACE) {
3454 err = got_path_dirname(&parent_path, s->in_repo_path);
3457 free(s->in_repo_path);
3458 s->in_repo_path = parent_path;
3459 s->thread_args.in_repo_path = s->in_repo_path;
3460 } else if (ch == CTRL('l')) {
3461 struct got_object_id *start_id;
3462 err = got_repo_match_object_id(&start_id, NULL,
3463 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3464 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3468 s->start_id = start_id;
3469 s->thread_args.start_id = s->start_id;
3471 s->log_branches = !s->log_branches;
3473 if (s->thread_args.pack_fds == NULL) {
3474 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3478 err = got_repo_open(&s->thread_args.repo,
3479 got_repo_get_path(s->repo), NULL,
3480 s->thread_args.pack_fds);
3484 err = tog_load_refs(s->repo, 0);
3487 err = got_commit_graph_open(&s->thread_args.graph,
3488 s->in_repo_path, !s->log_branches);
3491 err = got_commit_graph_iter_start(s->thread_args.graph,
3492 s->start_id, s->repo, NULL, NULL);
3495 free_commits(&s->commits);
3496 s->first_displayed_entry = NULL;
3497 s->last_displayed_entry = NULL;
3498 s->selected_entry = NULL;
3500 s->thread_args.log_complete = 0;
3502 s->thread_args.commits_needed = view->lines;
3503 s->matched_entry = NULL;
3504 s->search_entry = NULL;
3509 err = view_request_new(new_view, view, TOG_VIEW_REF);
3519 static const struct got_error *
3520 apply_unveil(const char *repo_path, const char *worktree_path)
3522 const struct got_error *error;
3525 if (unveil("gmon.out", "rwc") != 0)
3526 return got_error_from_errno2("unveil", "gmon.out");
3528 if (repo_path && unveil(repo_path, "r") != 0)
3529 return got_error_from_errno2("unveil", repo_path);
3531 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3532 return got_error_from_errno2("unveil", worktree_path);
3534 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3535 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3537 error = got_privsep_unveil_exec_helpers();
3541 if (unveil(NULL, NULL) != 0)
3542 return got_error_from_errno("unveil");
3551 * Override default signal handlers before starting ncurses.
3552 * This should prevent ncurses from installing its own
3553 * broken cleanup() signal handler.
3555 signal(SIGWINCH, tog_sigwinch);
3556 signal(SIGPIPE, tog_sigpipe);
3557 signal(SIGCONT, tog_sigcont);
3558 signal(SIGINT, tog_sigint);
3559 signal(SIGTERM, tog_sigterm);
3563 halfdelay(1); /* Do fast refresh while initial view is loading. */
3566 intrflush(stdscr, FALSE);
3567 keypad(stdscr, TRUE);
3569 if (getenv("TOG_COLORS") != NULL) {
3571 use_default_colors();
3575 static const struct got_error *
3576 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3577 struct got_repository *repo, struct got_worktree *worktree)
3579 const struct got_error *err = NULL;
3582 *in_repo_path = strdup("/");
3583 if (*in_repo_path == NULL)
3584 return got_error_from_errno("strdup");
3589 const char *prefix = got_worktree_get_path_prefix(worktree);
3592 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3595 if (asprintf(in_repo_path, "%s%s%s", prefix,
3596 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3598 err = got_error_from_errno("asprintf");
3599 *in_repo_path = NULL;
3603 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3608 static const struct got_error *
3609 cmd_log(int argc, char *argv[])
3611 const struct got_error *error;
3612 struct got_repository *repo = NULL;
3613 struct got_worktree *worktree = NULL;
3614 struct got_object_id *start_id = NULL;
3615 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3616 char *start_commit = NULL, *label = NULL;
3617 struct got_reference *ref = NULL;
3618 const char *head_ref_name = NULL;
3619 int ch, log_branches = 0;
3620 struct tog_view *view;
3621 int *pack_fds = NULL;
3623 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3629 start_commit = optarg;
3632 repo_path = realpath(optarg, NULL);
3633 if (repo_path == NULL)
3634 return got_error_from_errno2("realpath",
3649 error = got_repo_pack_fds_open(&pack_fds);
3653 if (repo_path == NULL) {
3654 cwd = getcwd(NULL, 0);
3656 return got_error_from_errno("getcwd");
3657 error = got_worktree_open(&worktree, cwd);
3658 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3662 strdup(got_worktree_get_repo_path(worktree));
3664 repo_path = strdup(cwd);
3665 if (repo_path == NULL) {
3666 error = got_error_from_errno("strdup");
3671 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3675 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3682 error = apply_unveil(got_repo_get_path(repo),
3683 worktree ? got_worktree_get_root_path(worktree) : NULL);
3687 /* already loaded by tog_log_with_path()? */
3688 if (TAILQ_EMPTY(&tog_refs)) {
3689 error = tog_load_refs(repo, 0);
3694 if (start_commit == NULL) {
3695 error = got_repo_match_object_id(&start_id, &label,
3696 worktree ? got_worktree_get_head_ref_name(worktree) :
3697 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3700 head_ref_name = label;
3702 error = got_ref_open(&ref, repo, start_commit, 0);
3704 head_ref_name = got_ref_get_name(ref);
3705 else if (error->code != GOT_ERR_NOT_REF)
3707 error = got_repo_match_object_id(&start_id, NULL,
3708 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3713 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3715 error = got_error_from_errno("view_open");
3718 error = open_log_view(view, start_id, repo, head_ref_name,
3719 in_repo_path, log_branches);
3723 /* Release work tree lock. */
3724 got_worktree_close(worktree);
3727 error = view_loop(view);
3737 const struct got_error *close_err = got_repo_close(repo);
3742 got_worktree_close(worktree);
3744 const struct got_error *pack_err =
3745 got_repo_pack_fds_close(pack_fds);
3757 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3758 "[-w] object1 object2\n", getprogname());
3763 match_line(const char *line, regex_t *regex, size_t nmatch,
3764 regmatch_t *regmatch)
3766 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3769 static struct tog_color *
3770 match_color(struct tog_colors *colors, const char *line)
3772 struct tog_color *tc = NULL;
3774 STAILQ_FOREACH(tc, colors, entry) {
3775 if (match_line(line, &tc->regex, 0, NULL))
3782 static const struct got_error *
3783 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3784 WINDOW *window, int skipcol, regmatch_t *regmatch)
3786 const struct got_error *err = NULL;
3788 wchar_t *wline = NULL;
3789 int rme, rms, n, width, scrollx;
3790 int width0 = 0, width1 = 0, width2 = 0;
3791 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3795 rms = regmatch->rm_so;
3796 rme = regmatch->rm_eo;
3798 err = expand_tab(&exstr, line);
3802 /* Split the line into 3 segments, according to match offsets. */
3803 seg0 = strndup(exstr, rms);
3805 err = got_error_from_errno("strndup");
3808 seg1 = strndup(exstr + rms, rme - rms);
3810 err = got_error_from_errno("strndup");
3813 seg2 = strdup(exstr + rme);
3815 err = got_error_from_errno("strndup");
3819 /* draw up to matched token if we haven't scrolled past it */
3820 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3824 n = MAX(width0 - skipcol, 0);
3827 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3828 wlimit, col_tab_align, 1);
3831 waddwstr(window, &wline[scrollx]);
3841 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3845 wlen = wcslen(wline);
3847 width = wcwidth(wline[i]);
3849 /* should not happen, tabs are expanded */
3850 err = got_error(GOT_ERR_RANGE);
3853 if (width0 + w + width > skipcol)
3858 /* draw (visible part of) matched token (if scrolled into it) */
3859 if (width1 - w > 0) {
3860 wattron(window, A_STANDOUT);
3861 waddwstr(window, &wline[i]);
3862 wattroff(window, A_STANDOUT);
3863 wlimit -= (width1 - w);
3864 *wtotal += (width1 - w);
3868 if (wlimit > 0) { /* draw rest of line */
3870 if (skipcol > width0 + width1) {
3871 err = format_line(&wline, &width2, &scrollx, seg2,
3872 skipcol - (width0 + width1), wlimit,
3876 waddwstr(window, &wline[scrollx]);
3878 err = format_line(&wline, &width2, NULL, seg2, 0,
3879 wlimit, col_tab_align, 1);
3882 waddwstr(window, wline);
3896 gotoline(struct tog_view *view, int *lineno, int *nprinted)
3899 int *eof, *first, *selected;
3901 if (view->type == TOG_VIEW_DIFF) {
3902 struct tog_diff_view_state *s = &view->state.diff;
3904 first = &s->first_displayed_line;
3908 } else if (view->type == TOG_VIEW_BLAME) {
3909 struct tog_blame_view_state *s = &view->state.blame;
3911 first = &s->first_displayed_line;
3912 selected = &s->selected_line;
3918 /* Center gline in the middle of the page like vi(1). */
3919 if (*lineno < view->gline - (view->nlines - 3) / 2)
3921 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3930 *selected = view->gline <= (view->nlines - 3) / 2 ?
3931 view->gline : (view->nlines - 3) / 2 + 1;
3937 static const struct got_error *
3938 draw_file(struct tog_view *view, const char *header)
3940 struct tog_diff_view_state *s = &view->state.diff;
3941 regmatch_t *regmatch = &view->regmatch;
3942 const struct got_error *err;
3945 size_t linesize = 0;
3949 int max_lines = view->nlines;
3950 int nlines = s->nlines;
3953 s->lineno = s->first_displayed_line - 1;
3954 line_offset = s->lines[s->first_displayed_line - 1].offset;
3955 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3956 return got_error_from_errno("fseek");
3958 werase(view->window);
3960 if (view->gline > s->nlines - 1)
3961 view->gline = s->nlines - 1;
3964 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3965 1 : view->gline - (view->nlines - 3) / 2 :
3966 s->lineno + s->selected_line;
3968 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3969 return got_error_from_errno("asprintf");
3970 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3976 if (view_needs_focus_indication(view))
3977 wstandout(view->window);
3978 waddwstr(view->window, wline);
3981 if (view_needs_focus_indication(view))
3982 wstandend(view->window);
3983 if (width <= view->ncols - 1)
3984 waddch(view->window, '\n');
3994 while (max_lines > 0 && nprinted < max_lines) {
3995 enum got_diff_line_type linetype;
3998 linelen = getline(&line, &linesize, s->f);
3999 if (linelen == -1) {
4005 return got_ferror(s->f, GOT_ERR_IO);
4008 if (++s->lineno < s->first_displayed_line)
4010 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4012 if (s->lineno == view->hiline)
4015 /* Set view->maxx based on full line length. */
4016 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4022 view->maxx = MAX(view->maxx, width);
4026 linetype = s->lines[s->lineno].type;
4027 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4028 linetype < GOT_DIFF_LINE_CONTEXT)
4029 attr |= COLOR_PAIR(linetype);
4031 wattron(view->window, attr);
4032 if (s->first_displayed_line + nprinted == s->matched_line &&
4033 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4034 err = add_matched_line(&width, line, view->ncols, 0,
4035 view->window, view->x, regmatch);
4042 err = format_line(&wline, &width, &skip, line,
4043 view->x, view->ncols, 0, view->x ? 1 : 0);
4048 waddwstr(view->window, &wline[skip]);
4052 if (s->lineno == view->hiline) {
4053 /* highlight full gline length */
4054 while (width++ < view->ncols)
4055 waddch(view->window, ' ');
4057 if (width <= view->ncols - 1)
4058 waddch(view->window, '\n');
4061 wattroff(view->window, attr);
4062 if (++nprinted == 1)
4063 s->first_displayed_line = s->lineno;
4067 s->last_displayed_line = s->first_displayed_line +
4070 s->last_displayed_line = s->first_displayed_line;
4075 while (nprinted < view->nlines) {
4076 waddch(view->window, '\n');
4080 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4086 wstandout(view->window);
4087 waddwstr(view->window, wline);
4090 wstandend(view->window);
4097 get_datestr(time_t *time, char *datebuf)
4099 struct tm mytm, *tm;
4102 tm = gmtime_r(time, &mytm);
4105 s = asctime_r(tm, datebuf);
4108 p = strchr(s, '\n');
4114 static const struct got_error *
4115 get_changed_paths(struct got_pathlist_head *paths,
4116 struct got_commit_object *commit, struct got_repository *repo)
4118 const struct got_error *err = NULL;
4119 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4120 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4121 struct got_object_qid *qid;
4123 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4125 struct got_commit_object *pcommit;
4126 err = got_object_open_as_commit(&pcommit, repo,
4131 tree_id1 = got_object_id_dup(
4132 got_object_commit_get_tree_id(pcommit));
4133 if (tree_id1 == NULL) {
4134 got_object_commit_close(pcommit);
4135 return got_error_from_errno("got_object_id_dup");
4137 got_object_commit_close(pcommit);
4142 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4147 tree_id2 = got_object_commit_get_tree_id(commit);
4148 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4152 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4153 got_diff_tree_collect_changed_paths, paths, 0);
4156 got_object_tree_close(tree1);
4158 got_object_tree_close(tree2);
4163 static const struct got_error *
4164 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4165 off_t off, uint8_t type)
4167 struct got_diff_line *p;
4169 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4171 return got_error_from_errno("reallocarray");
4173 (*lines)[*nlines].offset = off;
4174 (*lines)[*nlines].type = type;
4180 static const struct got_error *
4181 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4182 struct got_object_id *commit_id, struct got_reflist_head *refs,
4183 struct got_repository *repo, FILE *outfile)
4185 const struct got_error *err = NULL;
4186 char datebuf[26], *datestr;
4187 struct got_commit_object *commit;
4188 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4189 time_t committer_time;
4190 const char *author, *committer;
4191 char *refs_str = NULL;
4192 struct got_pathlist_head changed_paths;
4193 struct got_pathlist_entry *pe;
4197 TAILQ_INIT(&changed_paths);
4200 err = build_refs_str(&refs_str, refs, commit_id, repo);
4205 err = got_object_open_as_commit(&commit, repo, commit_id);
4209 err = got_object_id_str(&id_str, commit_id);
4211 err = got_error_from_errno("got_object_id_str");
4215 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4219 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4220 refs_str ? refs_str : "", refs_str ? ")" : "");
4222 err = got_error_from_errno("fprintf");
4226 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4230 n = fprintf(outfile, "from: %s\n",
4231 got_object_commit_get_author(commit));
4233 err = got_error_from_errno("fprintf");
4237 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4241 committer_time = got_object_commit_get_committer_time(commit);
4242 datestr = get_datestr(&committer_time, datebuf);
4244 n = fprintf(outfile, "date: %s UTC\n", datestr);
4246 err = got_error_from_errno("fprintf");
4250 err = add_line_metadata(lines, nlines, outoff,
4251 GOT_DIFF_LINE_DATE);
4255 author = got_object_commit_get_author(commit);
4256 committer = got_object_commit_get_committer(commit);
4257 if (strcmp(author, committer) != 0) {
4258 n = fprintf(outfile, "via: %s\n", committer);
4260 err = got_error_from_errno("fprintf");
4264 err = add_line_metadata(lines, nlines, outoff,
4265 GOT_DIFF_LINE_AUTHOR);
4269 if (got_object_commit_get_nparents(commit) > 1) {
4270 const struct got_object_id_queue *parent_ids;
4271 struct got_object_qid *qid;
4273 parent_ids = got_object_commit_get_parent_ids(commit);
4274 STAILQ_FOREACH(qid, parent_ids, entry) {
4275 err = got_object_id_str(&id_str, &qid->id);
4278 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4280 err = got_error_from_errno("fprintf");
4284 err = add_line_metadata(lines, nlines, outoff,
4285 GOT_DIFF_LINE_META);
4293 err = got_object_commit_get_logmsg(&logmsg, commit);
4297 while ((line = strsep(&s, "\n")) != NULL) {
4298 n = fprintf(outfile, "%s\n", line);
4300 err = got_error_from_errno("fprintf");
4304 err = add_line_metadata(lines, nlines, outoff,
4305 GOT_DIFF_LINE_LOGMSG);
4310 err = get_changed_paths(&changed_paths, commit, repo);
4313 TAILQ_FOREACH(pe, &changed_paths, entry) {
4314 struct got_diff_changed_path *cp = pe->data;
4315 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4317 err = got_error_from_errno("fprintf");
4321 err = add_line_metadata(lines, nlines, outoff,
4322 GOT_DIFF_LINE_CHANGES);
4325 free((char *)pe->path);
4329 fputc('\n', outfile);
4331 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4333 got_pathlist_free(&changed_paths);
4337 got_object_commit_close(commit);
4346 static const struct got_error *
4347 create_diff(struct tog_diff_view_state *s)
4349 const struct got_error *err = NULL;
4354 s->lines = malloc(sizeof(*s->lines));
4355 if (s->lines == NULL)
4356 return got_error_from_errno("malloc");
4361 err = got_error_from_errno("got_opentemp");
4364 if (s->f && fclose(s->f) == EOF) {
4365 err = got_error_from_errno("fclose");
4371 err = got_object_get_type(&obj_type, s->repo, s->id1);
4373 err = got_object_get_type(&obj_type, s->repo, s->id2);
4378 case GOT_OBJ_TYPE_BLOB:
4379 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4380 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4381 s->label1, s->label2, tog_diff_algo, s->diff_context,
4382 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4384 case GOT_OBJ_TYPE_TREE:
4385 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4386 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4387 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4388 s->force_text_diff, s->repo, s->f);
4390 case GOT_OBJ_TYPE_COMMIT: {
4391 const struct got_object_id_queue *parent_ids;
4392 struct got_object_qid *pid;
4393 struct got_commit_object *commit2;
4394 struct got_reflist_head *refs;
4396 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4399 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4400 /* Show commit info if we're diffing to a parent/root commit. */
4401 if (s->id1 == NULL) {
4402 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4403 refs, s->repo, s->f);
4407 parent_ids = got_object_commit_get_parent_ids(commit2);
4408 STAILQ_FOREACH(pid, parent_ids, entry) {
4409 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4410 err = write_commit_info(&s->lines,
4411 &s->nlines, s->id2, refs, s->repo,
4419 got_object_commit_close(commit2);
4421 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4422 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4423 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4424 s->force_text_diff, s->repo, s->f);
4428 err = got_error(GOT_ERR_OBJ_TYPE);
4432 if (s->f && fflush(s->f) != 0 && err == NULL)
4433 err = got_error_from_errno("fflush");
4438 diff_view_indicate_progress(struct tog_view *view)
4440 mvwaddstr(view->window, 0, 0, "diffing...");
4445 static const struct got_error *
4446 search_start_diff_view(struct tog_view *view)
4448 struct tog_diff_view_state *s = &view->state.diff;
4450 s->matched_line = 0;
4454 static const struct got_error *
4455 search_next_diff_view(struct tog_view *view)
4457 struct tog_diff_view_state *s = &view->state.diff;
4458 const struct got_error *err = NULL;
4461 size_t linesize = 0;
4464 if (!view->searching) {
4465 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4469 if (s->matched_line) {
4470 if (view->searching == TOG_SEARCH_FORWARD)
4471 lineno = s->matched_line + 1;
4473 lineno = s->matched_line - 1;
4475 lineno = s->first_displayed_line;
4480 if (lineno <= 0 || lineno > s->nlines) {
4481 if (s->matched_line == 0) {
4482 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4486 if (view->searching == TOG_SEARCH_FORWARD)
4492 offset = s->lines[lineno - 1].offset;
4493 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4495 return got_error_from_errno("fseeko");
4497 linelen = getline(&line, &linesize, s->f);
4498 if (linelen != -1) {
4500 err = expand_tab(&exstr, line);
4503 if (match_line(exstr, &view->regex, 1,
4505 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4506 s->matched_line = lineno;
4512 if (view->searching == TOG_SEARCH_FORWARD)
4519 if (s->matched_line) {
4520 s->first_displayed_line = s->matched_line;
4521 s->selected_line = 1;
4527 static const struct got_error *
4528 close_diff_view(struct tog_view *view)
4530 const struct got_error *err = NULL;
4531 struct tog_diff_view_state *s = &view->state.diff;
4537 if (s->f && fclose(s->f) == EOF)
4538 err = got_error_from_errno("fclose");
4540 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4541 err = got_error_from_errno("fclose");
4543 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4544 err = got_error_from_errno("fclose");
4546 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4547 err = got_error_from_errno("close");
4549 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4550 err = got_error_from_errno("close");
4558 static const struct got_error *
4559 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4560 struct got_object_id *id2, const char *label1, const char *label2,
4561 int diff_context, int ignore_whitespace, int force_text_diff,
4562 struct tog_view *parent_view, struct got_repository *repo)
4564 const struct got_error *err;
4565 struct tog_diff_view_state *s = &view->state.diff;
4567 memset(s, 0, sizeof(*s));
4571 if (id1 != NULL && id2 != NULL) {
4573 err = got_object_get_type(&type1, repo, id1);
4576 err = got_object_get_type(&type2, repo, id2);
4581 return got_error(GOT_ERR_OBJ_TYPE);
4583 s->first_displayed_line = 1;
4584 s->last_displayed_line = view->nlines;
4585 s->selected_line = 1;
4593 s->id1 = got_object_id_dup(id1);
4595 return got_error_from_errno("got_object_id_dup");
4599 s->id2 = got_object_id_dup(id2);
4600 if (s->id2 == NULL) {
4601 err = got_error_from_errno("got_object_id_dup");
4605 s->f1 = got_opentemp();
4606 if (s->f1 == NULL) {
4607 err = got_error_from_errno("got_opentemp");
4611 s->f2 = got_opentemp();
4612 if (s->f2 == NULL) {
4613 err = got_error_from_errno("got_opentemp");
4617 s->fd1 = got_opentempfd();
4619 err = got_error_from_errno("got_opentempfd");
4623 s->fd2 = got_opentempfd();
4625 err = got_error_from_errno("got_opentempfd");
4629 s->first_displayed_line = 1;
4630 s->last_displayed_line = view->nlines;
4631 s->diff_context = diff_context;
4632 s->ignore_whitespace = ignore_whitespace;
4633 s->force_text_diff = force_text_diff;
4634 s->parent_view = parent_view;
4637 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4640 rc = init_pair(GOT_DIFF_LINE_MINUS,
4641 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4643 rc = init_pair(GOT_DIFF_LINE_PLUS,
4644 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4646 rc = init_pair(GOT_DIFF_LINE_HUNK,
4647 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4649 rc = init_pair(GOT_DIFF_LINE_META,
4650 get_color_value("TOG_COLOR_DIFF_META"), -1);
4652 rc = init_pair(GOT_DIFF_LINE_CHANGES,
4653 get_color_value("TOG_COLOR_DIFF_META"), -1);
4655 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4656 get_color_value("TOG_COLOR_DIFF_META"), -1);
4658 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4659 get_color_value("TOG_COLOR_DIFF_META"), -1);
4661 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4662 get_color_value("TOG_COLOR_AUTHOR"), -1);
4664 rc = init_pair(GOT_DIFF_LINE_DATE,
4665 get_color_value("TOG_COLOR_DATE"), -1);
4667 err = got_error(GOT_ERR_RANGE);
4672 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4673 view_is_splitscreen(view))
4674 show_log_view(parent_view); /* draw border */
4675 diff_view_indicate_progress(view);
4677 err = create_diff(s);
4679 view->show = show_diff_view;
4680 view->input = input_diff_view;
4681 view->reset = reset_diff_view;
4682 view->close = close_diff_view;
4683 view->search_start = search_start_diff_view;
4684 view->search_next = search_next_diff_view;
4687 close_diff_view(view);
4691 static const struct got_error *
4692 show_diff_view(struct tog_view *view)
4694 const struct got_error *err;
4695 struct tog_diff_view_state *s = &view->state.diff;
4696 char *id_str1 = NULL, *id_str2, *header;
4697 const char *label1, *label2;
4700 err = got_object_id_str(&id_str1, s->id1);
4703 label1 = s->label1 ? : id_str1;
4705 label1 = "/dev/null";
4707 err = got_object_id_str(&id_str2, s->id2);
4710 label2 = s->label2 ? : id_str2;
4712 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4713 err = got_error_from_errno("asprintf");
4721 err = draw_file(view, header);
4726 static const struct got_error *
4727 set_selected_commit(struct tog_diff_view_state *s,
4728 struct commit_queue_entry *entry)
4730 const struct got_error *err;
4731 const struct got_object_id_queue *parent_ids;
4732 struct got_commit_object *selected_commit;
4733 struct got_object_qid *pid;
4736 s->id2 = got_object_id_dup(entry->id);
4738 return got_error_from_errno("got_object_id_dup");
4740 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4743 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4745 pid = STAILQ_FIRST(parent_ids);
4746 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4747 got_object_commit_close(selected_commit);
4751 static const struct got_error *
4752 reset_diff_view(struct tog_view *view)
4754 struct tog_diff_view_state *s = &view->state.diff;
4757 wclear(view->window);
4758 s->first_displayed_line = 1;
4759 s->last_displayed_line = view->nlines;
4760 s->matched_line = 0;
4761 diff_view_indicate_progress(view);
4762 return create_diff(s);
4766 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4770 i = start = s->first_displayed_line - 1;
4772 while (s->lines[i].type != type) {
4776 return; /* do nothing, requested type not in file */
4779 s->selected_line = 1;
4780 s->first_displayed_line = i;
4784 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4788 i = start = s->first_displayed_line + 1;
4790 while (s->lines[i].type != type) {
4791 if (i == s->nlines - 1)
4794 return; /* do nothing, requested type not in file */
4797 s->selected_line = 1;
4798 s->first_displayed_line = i;
4801 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4803 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4806 static const struct got_error *
4807 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4809 const struct got_error *err = NULL;
4810 struct tog_diff_view_state *s = &view->state.diff;
4811 struct tog_log_view_state *ls;
4812 struct commit_queue_entry *old_selected_entry;
4814 size_t linesize = 0;
4816 int i, nscroll = view->nlines - 1, up = 0;
4818 s->lineno = s->first_displayed_line - 1 + s->selected_line;
4825 view->x = MAX(view->maxx - view->ncols / 3, 0);
4830 if (view->x + view->ncols / 3 < view->maxx)
4831 view->x += 2; /* move two columns right */
4837 view->x -= MIN(view->x, 2); /* move two columns back */
4844 s->force_text_diff = !s->force_text_diff;
4846 s->ignore_whitespace = !s->ignore_whitespace;
4847 err = reset_diff_view(view);
4851 s->first_displayed_line = 1;
4860 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4866 if (s->first_displayed_line > 1)
4867 s->first_displayed_line--;
4878 if (s->first_displayed_line == 1) {
4883 while (i++ < nscroll && s->first_displayed_line > 1)
4884 s->first_displayed_line--;
4890 s->first_displayed_line++;
4907 while (!s->eof && i++ < nscroll) {
4908 linelen = getline(&line, &linesize, s->f);
4909 s->first_displayed_line++;
4910 if (linelen == -1) {
4914 err = got_ferror(s->f, GOT_ERR_IO);
4921 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
4924 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
4927 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
4930 diff_next_index(s, GOT_DIFF_LINE_HUNK);
4933 if (s->diff_context > 0) {
4935 s->matched_line = 0;
4936 diff_view_indicate_progress(view);
4937 err = create_diff(s);
4938 if (s->first_displayed_line + view->nlines - 1 >
4940 s->first_displayed_line = 1;
4941 s->last_displayed_line = view->nlines;
4947 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4949 s->matched_line = 0;
4950 diff_view_indicate_progress(view);
4951 err = create_diff(s);
4963 if (s->parent_view == NULL) {
4967 s->parent_view->count = view->count;
4969 if (s->parent_view->type == TOG_VIEW_LOG) {
4970 ls = &s->parent_view->state.log;
4971 old_selected_entry = ls->selected_entry;
4973 err = input_log_view(NULL, s->parent_view,
4974 up ? KEY_UP : KEY_DOWN);
4977 view->count = s->parent_view->count;
4979 if (old_selected_entry == ls->selected_entry)
4982 err = set_selected_commit(s, ls->selected_entry);
4985 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4986 struct tog_blame_view_state *bs;
4987 struct got_object_id *id, *prev_id;
4989 bs = &s->parent_view->state.blame;
4990 prev_id = get_annotation_for_line(bs->blame.lines,
4991 bs->blame.nlines, bs->last_diffed_line);
4993 err = input_blame_view(&view, s->parent_view,
4994 up ? KEY_UP : KEY_DOWN);
4997 view->count = s->parent_view->count;
4999 if (prev_id == NULL)
5001 id = get_selected_commit_id(bs->blame.lines,
5002 bs->blame.nlines, bs->first_displayed_line,
5007 if (!got_object_id_cmp(prev_id, id))
5010 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5014 s->first_displayed_line = 1;
5015 s->last_displayed_line = view->nlines;
5016 s->matched_line = 0;
5019 diff_view_indicate_progress(view);
5020 err = create_diff(s);
5030 static const struct got_error *
5031 cmd_diff(int argc, char *argv[])
5033 const struct got_error *error = NULL;
5034 struct got_repository *repo = NULL;
5035 struct got_worktree *worktree = NULL;
5036 struct got_object_id *id1 = NULL, *id2 = NULL;
5037 char *repo_path = NULL, *cwd = NULL;
5038 char *id_str1 = NULL, *id_str2 = NULL;
5039 char *label1 = NULL, *label2 = NULL;
5040 int diff_context = 3, ignore_whitespace = 0;
5041 int ch, force_text_diff = 0;
5043 struct tog_view *view;
5044 int *pack_fds = NULL;
5046 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5049 force_text_diff = 1;
5052 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5055 errx(1, "number of context lines is %s: %s",
5059 repo_path = realpath(optarg, NULL);
5060 if (repo_path == NULL)
5061 return got_error_from_errno2("realpath",
5063 got_path_strip_trailing_slashes(repo_path);
5066 ignore_whitespace = 1;
5078 usage_diff(); /* TODO show local worktree changes */
5079 } else if (argc == 2) {
5085 error = got_repo_pack_fds_open(&pack_fds);
5089 if (repo_path == NULL) {
5090 cwd = getcwd(NULL, 0);
5092 return got_error_from_errno("getcwd");
5093 error = got_worktree_open(&worktree, cwd);
5094 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5098 strdup(got_worktree_get_repo_path(worktree));
5100 repo_path = strdup(cwd);
5101 if (repo_path == NULL) {
5102 error = got_error_from_errno("strdup");
5107 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5113 error = apply_unveil(got_repo_get_path(repo), NULL);
5117 error = tog_load_refs(repo, 0);
5121 error = got_repo_match_object_id(&id1, &label1, id_str1,
5122 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5126 error = got_repo_match_object_id(&id2, &label2, id_str2,
5127 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5131 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5133 error = got_error_from_errno("view_open");
5136 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5137 ignore_whitespace, force_text_diff, NULL, repo);
5140 error = view_loop(view);
5147 const struct got_error *close_err = got_repo_close(repo);
5152 got_worktree_close(worktree);
5154 const struct got_error *pack_err =
5155 got_repo_pack_fds_close(pack_fds);
5168 "usage: %s blame [-c commit] [-r repository-path] path\n",
5173 struct tog_blame_line {
5175 struct got_object_id *id;
5178 static const struct got_error *
5179 draw_blame(struct tog_view *view)
5181 struct tog_blame_view_state *s = &view->state.blame;
5182 struct tog_blame *blame = &s->blame;
5183 regmatch_t *regmatch = &view->regmatch;
5184 const struct got_error *err;
5185 int lineno = 0, nprinted = 0;
5187 size_t linesize = 0;
5191 struct tog_blame_line *blame_line;
5192 struct got_object_id *prev_id = NULL;
5194 struct tog_color *tc;
5196 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5201 werase(view->window);
5203 if (asprintf(&line, "commit %s", id_str) == -1) {
5204 err = got_error_from_errno("asprintf");
5209 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5214 if (view_needs_focus_indication(view))
5215 wstandout(view->window);
5216 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5218 wattr_on(view->window,
5219 COLOR_PAIR(tc->colorpair), NULL);
5220 waddwstr(view->window, wline);
5222 wattr_off(view->window,
5223 COLOR_PAIR(tc->colorpair), NULL);
5224 if (view_needs_focus_indication(view))
5225 wstandend(view->window);
5228 if (width < view->ncols - 1)
5229 waddch(view->window, '\n');
5231 if (view->gline > blame->nlines)
5232 view->gline = blame->nlines;
5234 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5235 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5236 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5238 return got_error_from_errno("asprintf");
5241 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5246 waddwstr(view->window, wline);
5249 if (width < view->ncols - 1)
5250 waddch(view->window, '\n');
5254 while (nprinted < view->nlines - 2) {
5255 linelen = getline(&line, &linesize, blame->f);
5256 if (linelen == -1) {
5257 if (feof(blame->f)) {
5262 return got_ferror(blame->f, GOT_ERR_IO);
5264 if (++lineno < s->first_displayed_line)
5266 if (view->gline && !gotoline(view, &lineno, &nprinted))
5269 /* Set view->maxx based on full line length. */
5270 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5277 view->maxx = MAX(view->maxx, width);
5279 if (nprinted == s->selected_line - 1)
5280 wstandout(view->window);
5282 if (blame->nlines > 0) {
5283 blame_line = &blame->lines[lineno - 1];
5284 if (blame_line->annotated && prev_id &&
5285 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5286 !(nprinted == s->selected_line - 1)) {
5287 waddstr(view->window, " ");
5288 } else if (blame_line->annotated) {
5290 err = got_object_id_str(&id_str,
5296 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5298 wattr_on(view->window,
5299 COLOR_PAIR(tc->colorpair), NULL);
5300 wprintw(view->window, "%.8s", id_str);
5302 wattr_off(view->window,
5303 COLOR_PAIR(tc->colorpair), NULL);
5305 prev_id = blame_line->id;
5307 waddstr(view->window, "........");
5311 waddstr(view->window, "........");
5315 if (nprinted == s->selected_line - 1)
5316 wstandend(view->window);
5317 waddstr(view->window, " ");
5319 if (view->ncols <= 9) {
5321 } else if (s->first_displayed_line + nprinted ==
5323 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5324 err = add_matched_line(&width, line, view->ncols - 9, 9,
5325 view->window, view->x, regmatch);
5333 err = format_line(&wline, &width, &skip, line,
5334 view->x, view->ncols - 9, 9, 1);
5339 waddwstr(view->window, &wline[skip]);
5345 if (width <= view->ncols - 1)
5346 waddch(view->window, '\n');
5347 if (++nprinted == 1)
5348 s->first_displayed_line = lineno;
5351 s->last_displayed_line = lineno;
5358 static const struct got_error *
5359 blame_cb(void *arg, int nlines, int lineno,
5360 struct got_commit_object *commit, struct got_object_id *id)
5362 const struct got_error *err = NULL;
5363 struct tog_blame_cb_args *a = arg;
5364 struct tog_blame_line *line;
5367 if (nlines != a->nlines ||
5368 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5369 return got_error(GOT_ERR_RANGE);
5371 errcode = pthread_mutex_lock(&tog_mutex);
5373 return got_error_set_errno(errcode, "pthread_mutex_lock");
5375 if (*a->quit) { /* user has quit the blame view */
5376 err = got_error(GOT_ERR_ITER_COMPLETED);
5381 goto done; /* no change in this commit */
5383 line = &a->lines[lineno - 1];
5384 if (line->annotated)
5387 line->id = got_object_id_dup(id);
5388 if (line->id == NULL) {
5389 err = got_error_from_errno("got_object_id_dup");
5392 line->annotated = 1;
5394 errcode = pthread_mutex_unlock(&tog_mutex);
5396 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5401 blame_thread(void *arg)
5403 const struct got_error *err, *close_err;
5404 struct tog_blame_thread_args *ta = arg;
5405 struct tog_blame_cb_args *a = ta->cb_args;
5406 int errcode, fd1 = -1, fd2 = -1;
5407 FILE *f1 = NULL, *f2 = NULL;
5409 fd1 = got_opentempfd();
5411 return (void *)got_error_from_errno("got_opentempfd");
5413 fd2 = got_opentempfd();
5415 err = got_error_from_errno("got_opentempfd");
5419 f1 = got_opentemp();
5421 err = (void *)got_error_from_errno("got_opentemp");
5424 f2 = got_opentemp();
5426 err = (void *)got_error_from_errno("got_opentemp");
5430 err = block_signals_used_by_main_thread();
5434 err = got_blame(ta->path, a->commit_id, ta->repo,
5435 tog_diff_algo, blame_cb, ta->cb_args,
5436 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5437 if (err && err->code == GOT_ERR_CANCELLED)
5440 errcode = pthread_mutex_lock(&tog_mutex);
5442 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5446 close_err = got_repo_close(ta->repo);
5452 errcode = pthread_mutex_unlock(&tog_mutex);
5453 if (errcode && err == NULL)
5454 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5457 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5458 err = got_error_from_errno("close");
5459 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5460 err = got_error_from_errno("close");
5461 if (f1 && fclose(f1) == EOF && err == NULL)
5462 err = got_error_from_errno("fclose");
5463 if (f2 && fclose(f2) == EOF && err == NULL)
5464 err = got_error_from_errno("fclose");
5469 static struct got_object_id *
5470 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5471 int first_displayed_line, int selected_line)
5473 struct tog_blame_line *line;
5478 line = &lines[first_displayed_line - 1 + selected_line - 1];
5479 if (!line->annotated)
5485 static struct got_object_id *
5486 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5489 struct tog_blame_line *line;
5491 if (nlines <= 0 || lineno >= nlines)
5494 line = &lines[lineno - 1];
5495 if (!line->annotated)
5501 static const struct got_error *
5502 stop_blame(struct tog_blame *blame)
5504 const struct got_error *err = NULL;
5507 if (blame->thread) {
5509 errcode = pthread_mutex_unlock(&tog_mutex);
5511 return got_error_set_errno(errcode,
5512 "pthread_mutex_unlock");
5513 errcode = pthread_join(blame->thread, (void **)&err);
5515 return got_error_set_errno(errcode, "pthread_join");
5516 errcode = pthread_mutex_lock(&tog_mutex);
5518 return got_error_set_errno(errcode,
5519 "pthread_mutex_lock");
5520 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5522 blame->thread = 0; //NULL;
5524 if (blame->thread_args.repo) {
5525 const struct got_error *close_err;
5526 close_err = got_repo_close(blame->thread_args.repo);
5529 blame->thread_args.repo = NULL;
5532 if (fclose(blame->f) == EOF && err == NULL)
5533 err = got_error_from_errno("fclose");
5537 for (i = 0; i < blame->nlines; i++)
5538 free(blame->lines[i].id);
5540 blame->lines = NULL;
5542 free(blame->cb_args.commit_id);
5543 blame->cb_args.commit_id = NULL;
5544 if (blame->pack_fds) {
5545 const struct got_error *pack_err =
5546 got_repo_pack_fds_close(blame->pack_fds);
5549 blame->pack_fds = NULL;
5554 static const struct got_error *
5555 cancel_blame_view(void *arg)
5557 const struct got_error *err = NULL;
5561 errcode = pthread_mutex_lock(&tog_mutex);
5563 return got_error_set_errno(errcode,
5564 "pthread_mutex_unlock");
5567 err = got_error(GOT_ERR_CANCELLED);
5569 errcode = pthread_mutex_unlock(&tog_mutex);
5571 return got_error_set_errno(errcode,
5572 "pthread_mutex_lock");
5577 static const struct got_error *
5578 run_blame(struct tog_view *view)
5580 struct tog_blame_view_state *s = &view->state.blame;
5581 struct tog_blame *blame = &s->blame;
5582 const struct got_error *err = NULL;
5583 struct got_commit_object *commit = NULL;
5584 struct got_blob_object *blob = NULL;
5585 struct got_repository *thread_repo = NULL;
5586 struct got_object_id *obj_id = NULL;
5587 int obj_type, fd = -1;
5588 int *pack_fds = NULL;
5590 err = got_object_open_as_commit(&commit, s->repo,
5591 &s->blamed_commit->id);
5595 fd = got_opentempfd();
5597 err = got_error_from_errno("got_opentempfd");
5601 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5605 err = got_object_get_type(&obj_type, s->repo, obj_id);
5609 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5610 err = got_error(GOT_ERR_OBJ_TYPE);
5614 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5617 blame->f = got_opentemp();
5618 if (blame->f == NULL) {
5619 err = got_error_from_errno("got_opentemp");
5622 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5623 &blame->line_offsets, blame->f, blob);
5626 if (blame->nlines == 0) {
5627 s->blame_complete = 1;
5631 /* Don't include \n at EOF in the blame line count. */
5632 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5635 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5636 if (blame->lines == NULL) {
5637 err = got_error_from_errno("calloc");
5641 err = got_repo_pack_fds_open(&pack_fds);
5644 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5649 blame->pack_fds = pack_fds;
5650 blame->cb_args.view = view;
5651 blame->cb_args.lines = blame->lines;
5652 blame->cb_args.nlines = blame->nlines;
5653 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5654 if (blame->cb_args.commit_id == NULL) {
5655 err = got_error_from_errno("got_object_id_dup");
5658 blame->cb_args.quit = &s->done;
5660 blame->thread_args.path = s->path;
5661 blame->thread_args.repo = thread_repo;
5662 blame->thread_args.cb_args = &blame->cb_args;
5663 blame->thread_args.complete = &s->blame_complete;
5664 blame->thread_args.cancel_cb = cancel_blame_view;
5665 blame->thread_args.cancel_arg = &s->done;
5666 s->blame_complete = 0;
5668 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5669 s->first_displayed_line = 1;
5670 s->last_displayed_line = view->nlines;
5671 s->selected_line = 1;
5673 s->matched_line = 0;
5677 got_object_commit_close(commit);
5678 if (fd != -1 && close(fd) == -1 && err == NULL)
5679 err = got_error_from_errno("close");
5681 got_object_blob_close(blob);
5688 static const struct got_error *
5689 open_blame_view(struct tog_view *view, char *path,
5690 struct got_object_id *commit_id, struct got_repository *repo)
5692 const struct got_error *err = NULL;
5693 struct tog_blame_view_state *s = &view->state.blame;
5695 STAILQ_INIT(&s->blamed_commits);
5697 s->path = strdup(path);
5698 if (s->path == NULL)
5699 return got_error_from_errno("strdup");
5701 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5707 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5708 s->first_displayed_line = 1;
5709 s->last_displayed_line = view->nlines;
5710 s->selected_line = 1;
5711 s->blame_complete = 0;
5713 s->commit_id = commit_id;
5714 memset(&s->blame, 0, sizeof(s->blame));
5716 STAILQ_INIT(&s->colors);
5717 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5718 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5719 get_color_value("TOG_COLOR_COMMIT"));
5724 view->show = show_blame_view;
5725 view->input = input_blame_view;
5726 view->reset = reset_blame_view;
5727 view->close = close_blame_view;
5728 view->search_start = search_start_blame_view;
5729 view->search_next = search_next_blame_view;
5731 return run_blame(view);
5734 static const struct got_error *
5735 close_blame_view(struct tog_view *view)
5737 const struct got_error *err = NULL;
5738 struct tog_blame_view_state *s = &view->state.blame;
5740 if (s->blame.thread)
5741 err = stop_blame(&s->blame);
5743 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5744 struct got_object_qid *blamed_commit;
5745 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5746 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5747 got_object_qid_free(blamed_commit);
5751 free_colors(&s->colors);
5755 static const struct got_error *
5756 search_start_blame_view(struct tog_view *view)
5758 struct tog_blame_view_state *s = &view->state.blame;
5760 s->matched_line = 0;
5764 static const struct got_error *
5765 search_next_blame_view(struct tog_view *view)
5767 struct tog_blame_view_state *s = &view->state.blame;
5768 const struct got_error *err = NULL;
5771 size_t linesize = 0;
5774 if (!view->searching) {
5775 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5779 if (s->matched_line) {
5780 if (view->searching == TOG_SEARCH_FORWARD)
5781 lineno = s->matched_line + 1;
5783 lineno = s->matched_line - 1;
5785 lineno = s->first_displayed_line - 1 + s->selected_line;
5790 if (lineno <= 0 || lineno > s->blame.nlines) {
5791 if (s->matched_line == 0) {
5792 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5796 if (view->searching == TOG_SEARCH_FORWARD)
5799 lineno = s->blame.nlines;
5802 offset = s->blame.line_offsets[lineno - 1];
5803 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5805 return got_error_from_errno("fseeko");
5807 linelen = getline(&line, &linesize, s->blame.f);
5808 if (linelen != -1) {
5810 err = expand_tab(&exstr, line);
5813 if (match_line(exstr, &view->regex, 1,
5815 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5816 s->matched_line = lineno;
5822 if (view->searching == TOG_SEARCH_FORWARD)
5829 if (s->matched_line) {
5830 s->first_displayed_line = s->matched_line;
5831 s->selected_line = 1;
5837 static const struct got_error *
5838 show_blame_view(struct tog_view *view)
5840 const struct got_error *err = NULL;
5841 struct tog_blame_view_state *s = &view->state.blame;
5844 if (s->blame.thread == 0 && !s->blame_complete) {
5845 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5846 &s->blame.thread_args);
5848 return got_error_set_errno(errcode, "pthread_create");
5850 halfdelay(1); /* fast refresh while annotating */
5853 if (s->blame_complete)
5854 halfdelay(10); /* disable fast refresh */
5856 err = draw_blame(view);
5862 static const struct got_error *
5863 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5864 struct got_repository *repo, struct got_object_id *id)
5866 struct tog_view *log_view;
5867 const struct got_error *err = NULL;
5871 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5872 if (log_view == NULL)
5873 return got_error_from_errno("view_open");
5875 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5877 view_close(log_view);
5879 *new_view = log_view;
5884 static const struct got_error *
5885 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5887 const struct got_error *err = NULL, *thread_err = NULL;
5888 struct tog_view *diff_view;
5889 struct tog_blame_view_state *s = &view->state.blame;
5890 int eos, nscroll, begin_y = 0, begin_x = 0;
5892 eos = nscroll = view->nlines - 2;
5893 if (view_is_hsplit_top(view))
5901 view->x = MAX(view->maxx - view->ncols / 3, 0);
5906 if (view->x + view->ncols / 3 < view->maxx)
5907 view->x += 2; /* move two columns right */
5913 view->x -= MIN(view->x, 2); /* move two columns back */
5922 s->selected_line = 1;
5923 s->first_displayed_line = 1;
5928 if (s->blame.nlines < eos) {
5929 s->selected_line = s->blame.nlines;
5930 s->first_displayed_line = 1;
5932 s->selected_line = eos;
5933 s->first_displayed_line = s->blame.nlines - (eos - 1);
5940 if (s->selected_line > 1)
5942 else if (s->selected_line == 1 &&
5943 s->first_displayed_line > 1)
5944 s->first_displayed_line--;
5955 if (s->first_displayed_line == 1) {
5956 if (view->count > 1)
5958 s->selected_line = MAX(1, s->selected_line - nscroll);
5962 if (s->first_displayed_line > nscroll)
5963 s->first_displayed_line -= nscroll;
5965 s->first_displayed_line = 1;
5970 if (s->selected_line < eos && s->first_displayed_line +
5971 s->selected_line <= s->blame.nlines)
5973 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5974 s->first_displayed_line++;
5980 struct got_object_id *id = NULL;
5983 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5984 s->first_displayed_line, s->selected_line);
5988 struct got_commit_object *commit, *pcommit;
5989 struct got_object_qid *pid;
5990 struct got_object_id *blob_id = NULL;
5992 err = got_object_open_as_commit(&commit,
5997 got_object_commit_get_parent_ids(commit));
5999 got_object_commit_close(commit);
6002 /* Check if path history ends here. */
6003 err = got_object_open_as_commit(&pcommit,
6007 err = got_object_id_by_path(&blob_id, s->repo,
6009 got_object_commit_close(pcommit);
6011 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6013 got_object_commit_close(commit);
6016 err = got_object_get_type(&obj_type, s->repo,
6019 /* Can't blame non-blob type objects. */
6020 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6021 got_object_commit_close(commit);
6024 err = got_object_qid_alloc(&s->blamed_commit,
6026 got_object_commit_close(commit);
6028 if (got_object_id_cmp(id,
6029 &s->blamed_commit->id) == 0)
6031 err = got_object_qid_alloc(&s->blamed_commit,
6037 thread_err = stop_blame(&s->blame);
6041 STAILQ_INSERT_HEAD(&s->blamed_commits,
6042 s->blamed_commit, entry);
6043 err = run_blame(view);
6049 struct got_object_qid *first;
6052 first = STAILQ_FIRST(&s->blamed_commits);
6053 if (!got_object_id_cmp(&first->id, s->commit_id))
6056 thread_err = stop_blame(&s->blame);
6060 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6061 got_object_qid_free(s->blamed_commit);
6063 STAILQ_FIRST(&s->blamed_commits);
6064 err = run_blame(view);
6071 s->id_to_log = get_selected_commit_id(s->blame.lines,
6072 s->blame.nlines, s->first_displayed_line, s->selected_line);
6074 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6078 struct got_object_id *id = NULL;
6079 struct got_object_qid *pid;
6080 struct got_commit_object *commit = NULL;
6083 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6084 s->first_displayed_line, s->selected_line);
6087 err = got_object_open_as_commit(&commit, s->repo, id);
6090 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6092 /* traversed from diff view, release diff resources */
6093 err = close_diff_view(*new_view);
6096 diff_view = *new_view;
6098 if (view_is_parent_view(view))
6099 view_get_split(view, &begin_y, &begin_x);
6101 diff_view = view_open(0, 0, begin_y, begin_x,
6103 if (diff_view == NULL) {
6104 got_object_commit_close(commit);
6105 err = got_error_from_errno("view_open");
6109 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6110 id, NULL, NULL, 3, 0, 0, view, s->repo);
6111 got_object_commit_close(commit);
6113 view_close(diff_view);
6116 s->last_diffed_line = s->first_displayed_line - 1 +
6119 break; /* still open from active diff view */
6120 if (view_is_parent_view(view) &&
6121 view->mode == TOG_VIEW_SPLIT_HRZN) {
6122 err = view_init_hsplit(view, begin_y);
6128 diff_view->focussed = 1;
6129 diff_view->mode = view->mode;
6130 diff_view->nlines = view->lines - begin_y;
6131 if (view_is_parent_view(view)) {
6132 view_transfer_size(diff_view, view);
6133 err = view_close_child(view);
6136 err = view_set_child(view, diff_view);
6139 view->focus_child = 1;
6141 *new_view = diff_view;
6154 if (s->last_displayed_line >= s->blame.nlines &&
6155 s->selected_line >= MIN(s->blame.nlines,
6156 view->nlines - 2)) {
6160 if (s->last_displayed_line >= s->blame.nlines &&
6161 s->selected_line < view->nlines - 2) {
6163 MIN(nscroll, s->last_displayed_line -
6164 s->first_displayed_line - s->selected_line + 1);
6166 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6167 s->first_displayed_line += nscroll;
6169 s->first_displayed_line =
6170 s->blame.nlines - (view->nlines - 3);
6173 if (s->selected_line > view->nlines - 2) {
6174 s->selected_line = MIN(s->blame.nlines,
6182 return thread_err ? thread_err : err;
6185 static const struct got_error *
6186 reset_blame_view(struct tog_view *view)
6188 const struct got_error *err;
6189 struct tog_blame_view_state *s = &view->state.blame;
6193 err = stop_blame(&s->blame);
6197 return run_blame(view);
6200 static const struct got_error *
6201 cmd_blame(int argc, char *argv[])
6203 const struct got_error *error;
6204 struct got_repository *repo = NULL;
6205 struct got_worktree *worktree = NULL;
6206 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6207 char *link_target = NULL;
6208 struct got_object_id *commit_id = NULL;
6209 struct got_commit_object *commit = NULL;
6210 char *commit_id_str = NULL;
6212 struct tog_view *view;
6213 int *pack_fds = NULL;
6215 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6218 commit_id_str = optarg;
6221 repo_path = realpath(optarg, NULL);
6222 if (repo_path == NULL)
6223 return got_error_from_errno2("realpath",
6238 error = got_repo_pack_fds_open(&pack_fds);
6242 if (repo_path == NULL) {
6243 cwd = getcwd(NULL, 0);
6245 return got_error_from_errno("getcwd");
6246 error = got_worktree_open(&worktree, cwd);
6247 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6251 strdup(got_worktree_get_repo_path(worktree));
6253 repo_path = strdup(cwd);
6254 if (repo_path == NULL) {
6255 error = got_error_from_errno("strdup");
6260 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6264 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6271 error = apply_unveil(got_repo_get_path(repo), NULL);
6275 error = tog_load_refs(repo, 0);
6279 if (commit_id_str == NULL) {
6280 struct got_reference *head_ref;
6281 error = got_ref_open(&head_ref, repo, worktree ?
6282 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6285 error = got_ref_resolve(&commit_id, repo, head_ref);
6286 got_ref_close(head_ref);
6288 error = got_repo_match_object_id(&commit_id, NULL,
6289 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6294 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6296 error = got_error_from_errno("view_open");
6300 error = got_object_open_as_commit(&commit, repo, commit_id);
6304 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6309 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6314 /* Release work tree lock. */
6315 got_worktree_close(worktree);
6318 error = view_loop(view);
6326 got_object_commit_close(commit);
6328 got_worktree_close(worktree);
6330 const struct got_error *close_err = got_repo_close(repo);
6335 const struct got_error *pack_err =
6336 got_repo_pack_fds_close(pack_fds);
6344 static const struct got_error *
6345 draw_tree_entries(struct tog_view *view, const char *parent_path)
6347 struct tog_tree_view_state *s = &view->state.tree;
6348 const struct got_error *err = NULL;
6349 struct got_tree_entry *te;
6351 struct tog_color *tc;
6352 int width, n, nentries, i = 1;
6353 int limit = view->nlines;
6356 if (view_is_hsplit_top(view))
6357 --limit; /* border */
6359 werase(view->window);
6364 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6368 if (view_needs_focus_indication(view))
6369 wstandout(view->window);
6370 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6372 wattr_on(view->window,
6373 COLOR_PAIR(tc->colorpair), NULL);
6374 waddwstr(view->window, wline);
6376 wattr_off(view->window,
6377 COLOR_PAIR(tc->colorpair), NULL);
6378 if (view_needs_focus_indication(view))
6379 wstandend(view->window);
6383 if (s->selected_entry) {
6384 i = got_tree_entry_get_index(s->selected_entry);
6385 i += s->tree == s->root ? 1 : 2; /* account for ".." entry */
6387 nentries = got_object_tree_get_nentries(s->tree);
6388 wprintw(view->window, " [%d/%d]", i,
6389 nentries + (s->tree == s->root ? 0 : 1)); /* ".." in !root tree */
6391 if (width < view->ncols - 1)
6392 waddch(view->window, '\n');
6395 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6399 waddwstr(view->window, wline);
6402 if (width < view->ncols - 1)
6403 waddch(view->window, '\n');
6406 waddch(view->window, '\n');
6410 if (s->first_displayed_entry == NULL) {
6411 te = got_object_tree_get_first_entry(s->tree);
6412 if (s->selected == 0) {
6414 wstandout(view->window);
6415 s->selected_entry = NULL;
6417 waddstr(view->window, " ..\n"); /* parent directory */
6418 if (s->selected == 0 && view->focussed)
6419 wstandend(view->window);
6426 te = s->first_displayed_entry;
6429 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6430 char *line = NULL, *id_str = NULL, *link_target = NULL;
6431 const char *modestr = "";
6434 te = got_object_tree_get_entry(s->tree, i);
6435 mode = got_tree_entry_get_mode(te);
6438 err = got_object_id_str(&id_str,
6439 got_tree_entry_get_id(te));
6441 return got_error_from_errno(
6442 "got_object_id_str");
6444 if (got_object_tree_entry_is_submodule(te))
6446 else if (S_ISLNK(mode)) {
6449 err = got_tree_entry_get_symlink_target(&link_target,
6455 for (i = 0; i < strlen(link_target); i++) {
6456 if (!isprint((unsigned char)link_target[i]))
6457 link_target[i] = '?';
6461 else if (S_ISDIR(mode))
6463 else if (mode & S_IXUSR)
6465 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6466 got_tree_entry_get_name(te), modestr,
6467 link_target ? " -> ": "",
6468 link_target ? link_target : "") == -1) {
6471 return got_error_from_errno("asprintf");
6475 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6481 if (n == s->selected) {
6483 wstandout(view->window);
6484 s->selected_entry = te;
6486 tc = match_color(&s->colors, line);
6488 wattr_on(view->window,
6489 COLOR_PAIR(tc->colorpair), NULL);
6490 waddwstr(view->window, wline);
6492 wattr_off(view->window,
6493 COLOR_PAIR(tc->colorpair), NULL);
6494 if (width < view->ncols - 1)
6495 waddch(view->window, '\n');
6496 if (n == s->selected && view->focussed)
6497 wstandend(view->window);
6503 s->last_displayed_entry = te;
6512 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6514 struct got_tree_entry *te;
6515 int isroot = s->tree == s->root;
6518 if (s->first_displayed_entry == NULL)
6521 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6522 while (i++ < maxscroll) {
6525 s->first_displayed_entry = NULL;
6528 s->first_displayed_entry = te;
6529 te = got_tree_entry_get_prev(s->tree, te);
6533 static const struct got_error *
6534 tree_scroll_down(struct tog_view *view, int maxscroll)
6536 struct tog_tree_view_state *s = &view->state.tree;
6537 struct got_tree_entry *next, *last;
6540 if (s->first_displayed_entry)
6541 next = got_tree_entry_get_next(s->tree,
6542 s->first_displayed_entry);
6544 next = got_object_tree_get_first_entry(s->tree);
6546 last = s->last_displayed_entry;
6547 while (next && n++ < maxscroll) {
6549 s->last_displayed_entry = last;
6550 last = got_tree_entry_get_next(s->tree, last);
6552 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6553 s->first_displayed_entry = next;
6554 next = got_tree_entry_get_next(s->tree, next);
6561 static const struct got_error *
6562 tree_entry_path(char **path, struct tog_parent_trees *parents,
6563 struct got_tree_entry *te)
6565 const struct got_error *err = NULL;
6566 struct tog_parent_tree *pt;
6567 size_t len = 2; /* for leading slash and NUL */
6569 TAILQ_FOREACH(pt, parents, entry)
6570 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6573 len += strlen(got_tree_entry_get_name(te));
6575 *path = calloc(1, len);
6577 return got_error_from_errno("calloc");
6580 pt = TAILQ_LAST(parents, tog_parent_trees);
6582 const char *name = got_tree_entry_get_name(pt->selected_entry);
6583 if (strlcat(*path, name, len) >= len) {
6584 err = got_error(GOT_ERR_NO_SPACE);
6587 if (strlcat(*path, "/", len) >= len) {
6588 err = got_error(GOT_ERR_NO_SPACE);
6591 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6594 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6595 err = got_error(GOT_ERR_NO_SPACE);
6607 static const struct got_error *
6608 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6609 struct got_tree_entry *te, struct tog_parent_trees *parents,
6610 struct got_object_id *commit_id, struct got_repository *repo)
6612 const struct got_error *err = NULL;
6614 struct tog_view *blame_view;
6618 err = tree_entry_path(&path, parents, te);
6622 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6623 if (blame_view == NULL) {
6624 err = got_error_from_errno("view_open");
6628 err = open_blame_view(blame_view, path, commit_id, repo);
6630 if (err->code == GOT_ERR_CANCELLED)
6632 view_close(blame_view);
6634 *new_view = blame_view;
6640 static const struct got_error *
6641 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6642 struct tog_tree_view_state *s)
6644 struct tog_view *log_view;
6645 const struct got_error *err = NULL;
6650 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6651 if (log_view == NULL)
6652 return got_error_from_errno("view_open");
6654 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6658 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6661 view_close(log_view);
6663 *new_view = log_view;
6668 static const struct got_error *
6669 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6670 const char *head_ref_name, struct got_repository *repo)
6672 const struct got_error *err = NULL;
6673 char *commit_id_str = NULL;
6674 struct tog_tree_view_state *s = &view->state.tree;
6675 struct got_commit_object *commit = NULL;
6677 TAILQ_INIT(&s->parents);
6678 STAILQ_INIT(&s->colors);
6680 s->commit_id = got_object_id_dup(commit_id);
6681 if (s->commit_id == NULL)
6682 return got_error_from_errno("got_object_id_dup");
6684 err = got_object_open_as_commit(&commit, repo, commit_id);
6689 * The root is opened here and will be closed when the view is closed.
6690 * Any visited subtrees and their path-wise parents are opened and
6693 err = got_object_open_as_tree(&s->root, repo,
6694 got_object_commit_get_tree_id(commit));
6699 err = got_object_id_str(&commit_id_str, commit_id);
6703 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6704 err = got_error_from_errno("asprintf");
6708 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6709 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6710 if (head_ref_name) {
6711 s->head_ref_name = strdup(head_ref_name);
6712 if (s->head_ref_name == NULL) {
6713 err = got_error_from_errno("strdup");
6719 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6720 err = add_color(&s->colors, "\\$$",
6721 TOG_COLOR_TREE_SUBMODULE,
6722 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6725 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6726 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6729 err = add_color(&s->colors, "/$",
6730 TOG_COLOR_TREE_DIRECTORY,
6731 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6735 err = add_color(&s->colors, "\\*$",
6736 TOG_COLOR_TREE_EXECUTABLE,
6737 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6741 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6742 get_color_value("TOG_COLOR_COMMIT"));
6747 view->show = show_tree_view;
6748 view->input = input_tree_view;
6749 view->close = close_tree_view;
6750 view->search_start = search_start_tree_view;
6751 view->search_next = search_next_tree_view;
6753 free(commit_id_str);
6755 got_object_commit_close(commit);
6757 close_tree_view(view);
6761 static const struct got_error *
6762 close_tree_view(struct tog_view *view)
6764 struct tog_tree_view_state *s = &view->state.tree;
6766 free_colors(&s->colors);
6767 free(s->tree_label);
6768 s->tree_label = NULL;
6770 s->commit_id = NULL;
6771 free(s->head_ref_name);
6772 s->head_ref_name = NULL;
6773 while (!TAILQ_EMPTY(&s->parents)) {
6774 struct tog_parent_tree *parent;
6775 parent = TAILQ_FIRST(&s->parents);
6776 TAILQ_REMOVE(&s->parents, parent, entry);
6777 if (parent->tree != s->root)
6778 got_object_tree_close(parent->tree);
6782 if (s->tree != NULL && s->tree != s->root)
6783 got_object_tree_close(s->tree);
6785 got_object_tree_close(s->root);
6789 static const struct got_error *
6790 search_start_tree_view(struct tog_view *view)
6792 struct tog_tree_view_state *s = &view->state.tree;
6794 s->matched_entry = NULL;
6799 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6801 regmatch_t regmatch;
6803 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6807 static const struct got_error *
6808 search_next_tree_view(struct tog_view *view)
6810 struct tog_tree_view_state *s = &view->state.tree;
6811 struct got_tree_entry *te = NULL;
6813 if (!view->searching) {
6814 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6818 if (s->matched_entry) {
6819 if (view->searching == TOG_SEARCH_FORWARD) {
6820 if (s->selected_entry)
6821 te = got_tree_entry_get_next(s->tree,
6824 te = got_object_tree_get_first_entry(s->tree);
6826 if (s->selected_entry == NULL)
6827 te = got_object_tree_get_last_entry(s->tree);
6829 te = got_tree_entry_get_prev(s->tree,
6833 if (s->selected_entry)
6834 te = s->selected_entry;
6835 else if (view->searching == TOG_SEARCH_FORWARD)
6836 te = got_object_tree_get_first_entry(s->tree);
6838 te = got_object_tree_get_last_entry(s->tree);
6843 if (s->matched_entry == NULL) {
6844 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6847 if (view->searching == TOG_SEARCH_FORWARD)
6848 te = got_object_tree_get_first_entry(s->tree);
6850 te = got_object_tree_get_last_entry(s->tree);
6853 if (match_tree_entry(te, &view->regex)) {
6854 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6855 s->matched_entry = te;
6859 if (view->searching == TOG_SEARCH_FORWARD)
6860 te = got_tree_entry_get_next(s->tree, te);
6862 te = got_tree_entry_get_prev(s->tree, te);
6865 if (s->matched_entry) {
6866 s->first_displayed_entry = s->matched_entry;
6873 static const struct got_error *
6874 show_tree_view(struct tog_view *view)
6876 const struct got_error *err = NULL;
6877 struct tog_tree_view_state *s = &view->state.tree;
6880 err = tree_entry_path(&parent_path, &s->parents, NULL);
6884 err = draw_tree_entries(view, parent_path);
6891 static const struct got_error *
6892 tree_goto_line(struct tog_view *view, int nlines)
6894 const struct got_error *err = NULL;
6895 struct tog_tree_view_state *s = &view->state.tree;
6896 struct got_tree_entry **fte, **lte, **ste;
6897 int g, last, first = 1, i = 1;
6898 int root = s->tree == s->root;
6899 int off = root ? 1 : 2;
6906 else if (g > got_object_tree_get_nentries(s->tree))
6907 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6909 fte = &s->first_displayed_entry;
6910 lte = &s->last_displayed_entry;
6911 ste = &s->selected_entry;
6914 first = got_tree_entry_get_index(*fte);
6915 first += off; /* account for ".." */
6917 last = got_tree_entry_get_index(*lte);
6920 if (g >= first && g <= last && g - first < nlines) {
6921 s->selected = g - first;
6922 return NULL; /* gline is on the current page */
6926 i = got_tree_entry_get_index(*ste);
6931 err = tree_scroll_down(view, g - i);
6934 if (got_tree_entry_get_index(*lte) >=
6935 got_object_tree_get_nentries(s->tree) - 1 &&
6936 first + s->selected < g &&
6937 s->selected < s->ndisplayed - 1) {
6938 first = got_tree_entry_get_index(*fte);
6940 s->selected = g - first;
6943 tree_scroll_up(s, i - g);
6946 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6947 s->selected = g - 1;
6952 static const struct got_error *
6953 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6955 const struct got_error *err = NULL;
6956 struct tog_tree_view_state *s = &view->state.tree;
6957 struct got_tree_entry *te;
6958 int n, nscroll = view->nlines - 3;
6961 return tree_goto_line(view, nscroll);
6965 s->show_ids = !s->show_ids;
6970 if (!s->selected_entry)
6972 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6976 err = view_request_new(new_view, view, TOG_VIEW_REF);
6982 if (s->tree == s->root)
6983 s->first_displayed_entry =
6984 got_object_tree_get_first_entry(s->tree);
6986 s->first_displayed_entry = NULL;
6990 int eos = view->nlines - 3;
6992 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6996 te = got_object_tree_get_last_entry(s->tree);
6997 for (n = 0; n < eos; n++) {
6999 if (s->tree != s->root) {
7000 s->first_displayed_entry = NULL;
7005 s->first_displayed_entry = te;
7006 te = got_tree_entry_get_prev(s->tree, te);
7009 s->selected = n - 1;
7015 if (s->selected > 0) {
7019 tree_scroll_up(s, 1);
7020 if (s->selected_entry == NULL ||
7021 (s->tree == s->root && s->selected_entry ==
7022 got_object_tree_get_first_entry(s->tree)))
7032 if (s->tree == s->root) {
7033 if (got_object_tree_get_first_entry(s->tree) ==
7034 s->first_displayed_entry)
7035 s->selected -= MIN(s->selected, nscroll);
7037 if (s->first_displayed_entry == NULL)
7038 s->selected -= MIN(s->selected, nscroll);
7040 tree_scroll_up(s, MAX(0, nscroll));
7041 if (s->selected_entry == NULL ||
7042 (s->tree == s->root && s->selected_entry ==
7043 got_object_tree_get_first_entry(s->tree)))
7049 if (s->selected < s->ndisplayed - 1) {
7053 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7055 /* can't scroll any further */
7059 tree_scroll_down(view, 1);
7069 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7071 /* can't scroll any further; move cursor down */
7072 if (s->selected < s->ndisplayed - 1)
7073 s->selected += MIN(nscroll,
7074 s->ndisplayed - s->selected - 1);
7079 tree_scroll_down(view, nscroll);
7084 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7085 struct tog_parent_tree *parent;
7086 /* user selected '..' */
7087 if (s->tree == s->root) {
7091 parent = TAILQ_FIRST(&s->parents);
7092 TAILQ_REMOVE(&s->parents, parent,
7094 got_object_tree_close(s->tree);
7095 s->tree = parent->tree;
7096 s->first_displayed_entry =
7097 parent->first_displayed_entry;
7099 parent->selected_entry;
7100 s->selected = parent->selected;
7101 if (s->selected > view->nlines - 3) {
7102 err = offset_selection_down(view);
7107 } else if (S_ISDIR(got_tree_entry_get_mode(
7108 s->selected_entry))) {
7109 struct got_tree_object *subtree;
7111 err = got_object_open_as_tree(&subtree, s->repo,
7112 got_tree_entry_get_id(s->selected_entry));
7115 err = tree_view_visit_subtree(s, subtree);
7117 got_object_tree_close(subtree);
7120 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7121 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7124 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7125 s->selected = view->nlines - 4;
7141 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7146 static const struct got_error *
7147 cmd_tree(int argc, char *argv[])
7149 const struct got_error *error;
7150 struct got_repository *repo = NULL;
7151 struct got_worktree *worktree = NULL;
7152 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7153 struct got_object_id *commit_id = NULL;
7154 struct got_commit_object *commit = NULL;
7155 const char *commit_id_arg = NULL;
7157 struct got_reference *ref = NULL;
7158 const char *head_ref_name = NULL;
7160 struct tog_view *view;
7161 int *pack_fds = NULL;
7163 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7166 commit_id_arg = optarg;
7169 repo_path = realpath(optarg, NULL);
7170 if (repo_path == NULL)
7171 return got_error_from_errno2("realpath",
7186 error = got_repo_pack_fds_open(&pack_fds);
7190 if (repo_path == NULL) {
7191 cwd = getcwd(NULL, 0);
7193 return got_error_from_errno("getcwd");
7194 error = got_worktree_open(&worktree, cwd);
7195 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7199 strdup(got_worktree_get_repo_path(worktree));
7201 repo_path = strdup(cwd);
7202 if (repo_path == NULL) {
7203 error = got_error_from_errno("strdup");
7208 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7212 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7219 error = apply_unveil(got_repo_get_path(repo), NULL);
7223 error = tog_load_refs(repo, 0);
7227 if (commit_id_arg == NULL) {
7228 error = got_repo_match_object_id(&commit_id, &label,
7229 worktree ? got_worktree_get_head_ref_name(worktree) :
7230 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7233 head_ref_name = label;
7235 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7237 head_ref_name = got_ref_get_name(ref);
7238 else if (error->code != GOT_ERR_NOT_REF)
7240 error = got_repo_match_object_id(&commit_id, NULL,
7241 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7246 error = got_object_open_as_commit(&commit, repo, commit_id);
7250 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7252 error = got_error_from_errno("view_open");
7255 error = open_tree_view(view, commit_id, head_ref_name, repo);
7258 if (!got_path_is_root_dir(in_repo_path)) {
7259 error = tree_view_walk_path(&view->state.tree, commit,
7266 /* Release work tree lock. */
7267 got_worktree_close(worktree);
7270 error = view_loop(view);
7279 const struct got_error *close_err = got_repo_close(repo);
7284 const struct got_error *pack_err =
7285 got_repo_pack_fds_close(pack_fds);
7293 static const struct got_error *
7294 ref_view_load_refs(struct tog_ref_view_state *s)
7296 struct got_reflist_entry *sre;
7297 struct tog_reflist_entry *re;
7300 TAILQ_FOREACH(sre, &tog_refs, entry) {
7301 if (strncmp(got_ref_get_name(sre->ref),
7302 "refs/got/", 9) == 0 &&
7303 strncmp(got_ref_get_name(sre->ref),
7304 "refs/got/backup/", 16) != 0)
7307 re = malloc(sizeof(*re));
7309 return got_error_from_errno("malloc");
7311 re->ref = got_ref_dup(sre->ref);
7312 if (re->ref == NULL)
7313 return got_error_from_errno("got_ref_dup");
7314 re->idx = s->nrefs++;
7315 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7318 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7323 ref_view_free_refs(struct tog_ref_view_state *s)
7325 struct tog_reflist_entry *re;
7327 while (!TAILQ_EMPTY(&s->refs)) {
7328 re = TAILQ_FIRST(&s->refs);
7329 TAILQ_REMOVE(&s->refs, re, entry);
7330 got_ref_close(re->ref);
7335 static const struct got_error *
7336 open_ref_view(struct tog_view *view, struct got_repository *repo)
7338 const struct got_error *err = NULL;
7339 struct tog_ref_view_state *s = &view->state.ref;
7341 s->selected_entry = 0;
7344 TAILQ_INIT(&s->refs);
7345 STAILQ_INIT(&s->colors);
7347 err = ref_view_load_refs(s);
7351 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7352 err = add_color(&s->colors, "^refs/heads/",
7353 TOG_COLOR_REFS_HEADS,
7354 get_color_value("TOG_COLOR_REFS_HEADS"));
7358 err = add_color(&s->colors, "^refs/tags/",
7359 TOG_COLOR_REFS_TAGS,
7360 get_color_value("TOG_COLOR_REFS_TAGS"));
7364 err = add_color(&s->colors, "^refs/remotes/",
7365 TOG_COLOR_REFS_REMOTES,
7366 get_color_value("TOG_COLOR_REFS_REMOTES"));
7370 err = add_color(&s->colors, "^refs/got/backup/",
7371 TOG_COLOR_REFS_BACKUP,
7372 get_color_value("TOG_COLOR_REFS_BACKUP"));
7377 view->show = show_ref_view;
7378 view->input = input_ref_view;
7379 view->close = close_ref_view;
7380 view->search_start = search_start_ref_view;
7381 view->search_next = search_next_ref_view;
7384 free_colors(&s->colors);
7388 static const struct got_error *
7389 close_ref_view(struct tog_view *view)
7391 struct tog_ref_view_state *s = &view->state.ref;
7393 ref_view_free_refs(s);
7394 free_colors(&s->colors);
7399 static const struct got_error *
7400 resolve_reflist_entry(struct got_object_id **commit_id,
7401 struct tog_reflist_entry *re, struct got_repository *repo)
7403 const struct got_error *err = NULL;
7404 struct got_object_id *obj_id;
7405 struct got_tag_object *tag = NULL;
7410 err = got_ref_resolve(&obj_id, repo, re->ref);
7414 err = got_object_get_type(&obj_type, repo, obj_id);
7419 case GOT_OBJ_TYPE_COMMIT:
7420 *commit_id = obj_id;
7422 case GOT_OBJ_TYPE_TAG:
7423 err = got_object_open_as_tag(&tag, repo, obj_id);
7427 err = got_object_get_type(&obj_type, repo,
7428 got_object_tag_get_object_id(tag));
7431 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7432 err = got_error(GOT_ERR_OBJ_TYPE);
7435 *commit_id = got_object_id_dup(
7436 got_object_tag_get_object_id(tag));
7437 if (*commit_id == NULL) {
7438 err = got_error_from_errno("got_object_id_dup");
7443 err = got_error(GOT_ERR_OBJ_TYPE);
7449 got_object_tag_close(tag);
7457 static const struct got_error *
7458 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7459 struct tog_reflist_entry *re, struct got_repository *repo)
7461 struct tog_view *log_view;
7462 const struct got_error *err = NULL;
7463 struct got_object_id *commit_id = NULL;
7467 err = resolve_reflist_entry(&commit_id, re, repo);
7469 if (err->code != GOT_ERR_OBJ_TYPE)
7475 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7476 if (log_view == NULL) {
7477 err = got_error_from_errno("view_open");
7481 err = open_log_view(log_view, commit_id, repo,
7482 got_ref_get_name(re->ref), "", 0);
7485 view_close(log_view);
7487 *new_view = log_view;
7493 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7495 struct tog_reflist_entry *re;
7498 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7501 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7502 while (i++ < maxscroll) {
7505 s->first_displayed_entry = re;
7506 re = TAILQ_PREV(re, tog_reflist_head, entry);
7510 static const struct got_error *
7511 ref_scroll_down(struct tog_view *view, int maxscroll)
7513 struct tog_ref_view_state *s = &view->state.ref;
7514 struct tog_reflist_entry *next, *last;
7517 if (s->first_displayed_entry)
7518 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7520 next = TAILQ_FIRST(&s->refs);
7522 last = s->last_displayed_entry;
7523 while (next && n++ < maxscroll) {
7525 s->last_displayed_entry = last;
7526 last = TAILQ_NEXT(last, entry);
7528 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7529 s->first_displayed_entry = next;
7530 next = TAILQ_NEXT(next, entry);
7537 static const struct got_error *
7538 search_start_ref_view(struct tog_view *view)
7540 struct tog_ref_view_state *s = &view->state.ref;
7542 s->matched_entry = NULL;
7547 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7549 regmatch_t regmatch;
7551 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7555 static const struct got_error *
7556 search_next_ref_view(struct tog_view *view)
7558 struct tog_ref_view_state *s = &view->state.ref;
7559 struct tog_reflist_entry *re = NULL;
7561 if (!view->searching) {
7562 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7566 if (s->matched_entry) {
7567 if (view->searching == TOG_SEARCH_FORWARD) {
7568 if (s->selected_entry)
7569 re = TAILQ_NEXT(s->selected_entry, entry);
7571 re = TAILQ_PREV(s->selected_entry,
7572 tog_reflist_head, entry);
7574 if (s->selected_entry == NULL)
7575 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7577 re = TAILQ_PREV(s->selected_entry,
7578 tog_reflist_head, entry);
7581 if (s->selected_entry)
7582 re = s->selected_entry;
7583 else if (view->searching == TOG_SEARCH_FORWARD)
7584 re = TAILQ_FIRST(&s->refs);
7586 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7591 if (s->matched_entry == NULL) {
7592 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7595 if (view->searching == TOG_SEARCH_FORWARD)
7596 re = TAILQ_FIRST(&s->refs);
7598 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7601 if (match_reflist_entry(re, &view->regex)) {
7602 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7603 s->matched_entry = re;
7607 if (view->searching == TOG_SEARCH_FORWARD)
7608 re = TAILQ_NEXT(re, entry);
7610 re = TAILQ_PREV(re, tog_reflist_head, entry);
7613 if (s->matched_entry) {
7614 s->first_displayed_entry = s->matched_entry;
7621 static const struct got_error *
7622 show_ref_view(struct tog_view *view)
7624 const struct got_error *err = NULL;
7625 struct tog_ref_view_state *s = &view->state.ref;
7626 struct tog_reflist_entry *re;
7629 struct tog_color *tc;
7631 int limit = view->nlines;
7633 werase(view->window);
7636 if (view_is_hsplit_top(view))
7637 --limit; /* border */
7642 re = s->first_displayed_entry;
7644 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7646 return got_error_from_errno("asprintf");
7648 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7653 if (view_needs_focus_indication(view))
7654 wstandout(view->window);
7655 waddwstr(view->window, wline);
7656 if (view_needs_focus_indication(view))
7657 wstandend(view->window);
7662 if (width < view->ncols - 1)
7663 waddch(view->window, '\n');
7668 while (re && limit > 0) {
7670 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7673 struct got_commit_object *ci;
7674 struct got_tag_object *tag;
7675 struct got_object_id *id;
7679 err = got_ref_resolve(&id, s->repo, re->ref);
7682 err = got_object_open_as_tag(&tag, s->repo, id);
7684 if (err->code != GOT_ERR_OBJ_TYPE) {
7688 err = got_object_open_as_commit(&ci, s->repo,
7694 t = got_object_commit_get_committer_time(ci);
7695 got_object_commit_close(ci);
7697 t = got_object_tag_get_tagger_time(tag);
7698 got_object_tag_close(tag);
7701 if (gmtime_r(&t, &tm) == NULL)
7702 return got_error_from_errno("gmtime_r");
7703 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7704 return got_error(GOT_ERR_NO_SPACE);
7706 if (got_ref_is_symbolic(re->ref)) {
7707 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7708 ymd : "", got_ref_get_name(re->ref),
7709 got_ref_get_symref_target(re->ref)) == -1)
7710 return got_error_from_errno("asprintf");
7711 } else if (s->show_ids) {
7712 struct got_object_id *id;
7714 err = got_ref_resolve(&id, s->repo, re->ref);
7717 err = got_object_id_str(&id_str, id);
7722 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7723 got_ref_get_name(re->ref), id_str) == -1) {
7724 err = got_error_from_errno("asprintf");
7731 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7732 got_ref_get_name(re->ref)) == -1)
7733 return got_error_from_errno("asprintf");
7735 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7741 if (n == s->selected) {
7743 wstandout(view->window);
7744 s->selected_entry = re;
7746 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7748 wattr_on(view->window,
7749 COLOR_PAIR(tc->colorpair), NULL);
7750 waddwstr(view->window, wline);
7752 wattr_off(view->window,
7753 COLOR_PAIR(tc->colorpair), NULL);
7754 if (width < view->ncols - 1)
7755 waddch(view->window, '\n');
7756 if (n == s->selected && view->focussed)
7757 wstandend(view->window);
7763 s->last_displayed_entry = re;
7766 re = TAILQ_NEXT(re, entry);
7773 static const struct got_error *
7774 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7775 struct tog_reflist_entry *re, struct got_repository *repo)
7777 const struct got_error *err = NULL;
7778 struct got_object_id *commit_id = NULL;
7779 struct tog_view *tree_view;
7783 err = resolve_reflist_entry(&commit_id, re, repo);
7785 if (err->code != GOT_ERR_OBJ_TYPE)
7792 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7793 if (tree_view == NULL) {
7794 err = got_error_from_errno("view_open");
7798 err = open_tree_view(tree_view, commit_id,
7799 got_ref_get_name(re->ref), repo);
7803 *new_view = tree_view;
7809 static const struct got_error *
7810 ref_goto_line(struct tog_view *view, int nlines)
7812 const struct got_error *err = NULL;
7813 struct tog_ref_view_state *s = &view->state.ref;
7814 int g, idx = s->selected_entry->idx;
7821 else if (g > s->nrefs)
7824 if (g >= s->first_displayed_entry->idx + 1 &&
7825 g <= s->last_displayed_entry->idx + 1 &&
7826 g - s->first_displayed_entry->idx - 1 < nlines) {
7827 s->selected = g - s->first_displayed_entry->idx - 1;
7832 err = ref_scroll_down(view, g - idx - 1);
7835 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7836 s->first_displayed_entry->idx + s->selected < g &&
7837 s->selected < s->ndisplayed - 1)
7838 s->selected = g - s->first_displayed_entry->idx - 1;
7839 } else if (idx + 1 > g)
7840 ref_scroll_up(s, idx - g + 1);
7842 if (g < nlines && s->first_displayed_entry->idx == 0)
7843 s->selected = g - 1;
7849 static const struct got_error *
7850 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7852 const struct got_error *err = NULL;
7853 struct tog_ref_view_state *s = &view->state.ref;
7854 struct tog_reflist_entry *re;
7855 int n, nscroll = view->nlines - 1;
7858 return ref_goto_line(view, nscroll);
7862 s->show_ids = !s->show_ids;
7866 s->show_date = !s->show_date;
7870 s->sort_by_date = !s->sort_by_date;
7872 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7873 got_ref_cmp_by_commit_timestamp_descending :
7874 tog_ref_cmp_by_name, s->repo);
7877 got_reflist_object_id_map_free(tog_refs_idmap);
7878 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7879 &tog_refs, s->repo);
7882 ref_view_free_refs(s);
7883 err = ref_view_load_refs(s);
7888 if (!s->selected_entry)
7890 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7894 if (!s->selected_entry)
7896 err = view_request_new(new_view, view, TOG_VIEW_TREE);
7902 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7906 int eos = view->nlines - 1;
7908 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7912 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7913 for (n = 0; n < eos; n++) {
7916 s->first_displayed_entry = re;
7917 re = TAILQ_PREV(re, tog_reflist_head, entry);
7920 s->selected = n - 1;
7926 if (s->selected > 0) {
7930 ref_scroll_up(s, 1);
7931 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7941 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7942 s->selected -= MIN(nscroll, s->selected);
7943 ref_scroll_up(s, MAX(0, nscroll));
7944 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7950 if (s->selected < s->ndisplayed - 1) {
7954 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7955 /* can't scroll any further */
7959 ref_scroll_down(view, 1);
7969 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7970 /* can't scroll any further; move cursor down */
7971 if (s->selected < s->ndisplayed - 1)
7972 s->selected += MIN(nscroll,
7973 s->ndisplayed - s->selected - 1);
7974 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7975 s->selected += s->ndisplayed - s->selected - 1;
7979 ref_scroll_down(view, nscroll);
7984 err = tog_load_refs(s->repo, s->sort_by_date);
7987 ref_view_free_refs(s);
7988 err = ref_view_load_refs(s);
7991 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7992 s->selected = view->nlines - 2;
8006 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8011 static const struct got_error *
8012 cmd_ref(int argc, char *argv[])
8014 const struct got_error *error;
8015 struct got_repository *repo = NULL;
8016 struct got_worktree *worktree = NULL;
8017 char *cwd = NULL, *repo_path = NULL;
8019 struct tog_view *view;
8020 int *pack_fds = NULL;
8022 while ((ch = getopt(argc, argv, "r:")) != -1) {
8025 repo_path = realpath(optarg, NULL);
8026 if (repo_path == NULL)
8027 return got_error_from_errno2("realpath",
8042 error = got_repo_pack_fds_open(&pack_fds);
8046 if (repo_path == NULL) {
8047 cwd = getcwd(NULL, 0);
8049 return got_error_from_errno("getcwd");
8050 error = got_worktree_open(&worktree, cwd);
8051 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8055 strdup(got_worktree_get_repo_path(worktree));
8057 repo_path = strdup(cwd);
8058 if (repo_path == NULL) {
8059 error = got_error_from_errno("strdup");
8064 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8070 error = apply_unveil(got_repo_get_path(repo), NULL);
8074 error = tog_load_refs(repo, 0);
8078 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8080 error = got_error_from_errno("view_open");
8084 error = open_ref_view(view, repo);
8089 /* Release work tree lock. */
8090 got_worktree_close(worktree);
8093 error = view_loop(view);
8098 const struct got_error *close_err = got_repo_close(repo);
8103 const struct got_error *pack_err =
8104 got_repo_pack_fds_close(pack_fds);
8112 static const struct got_error *
8113 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8114 enum tog_view_type request, int y, int x)
8116 const struct got_error *err = NULL;
8122 if (view->type == TOG_VIEW_LOG) {
8123 struct tog_log_view_state *s = &view->state.log;
8125 err = open_diff_view_for_commit(new_view, y, x,
8126 s->selected_entry->commit, s->selected_entry->id,
8129 return got_error_msg(GOT_ERR_NOT_IMPL,
8130 "parent/child view pair not supported");
8132 case TOG_VIEW_BLAME:
8133 if (view->type == TOG_VIEW_TREE) {
8134 struct tog_tree_view_state *s = &view->state.tree;
8136 err = blame_tree_entry(new_view, y, x,
8137 s->selected_entry, &s->parents, s->commit_id,
8140 return got_error_msg(GOT_ERR_NOT_IMPL,
8141 "parent/child view pair not supported");
8144 if (view->type == TOG_VIEW_BLAME)
8145 err = log_annotated_line(new_view, y, x,
8146 view->state.blame.repo, view->state.blame.id_to_log);
8147 else if (view->type == TOG_VIEW_TREE)
8148 err = log_selected_tree_entry(new_view, y, x,
8150 else if (view->type == TOG_VIEW_REF)
8151 err = log_ref_entry(new_view, y, x,
8152 view->state.ref.selected_entry,
8153 view->state.ref.repo);
8155 return got_error_msg(GOT_ERR_NOT_IMPL,
8156 "parent/child view pair not supported");
8159 if (view->type == TOG_VIEW_LOG)
8160 err = browse_commit_tree(new_view, y, x,
8161 view->state.log.selected_entry,
8162 view->state.log.in_repo_path,
8163 view->state.log.head_ref_name,
8164 view->state.log.repo);
8165 else if (view->type == TOG_VIEW_REF)
8166 err = browse_ref_tree(new_view, y, x,
8167 view->state.ref.selected_entry,
8168 view->state.ref.repo);
8170 return got_error_msg(GOT_ERR_NOT_IMPL,
8171 "parent/child view pair not supported");
8174 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8175 if (*new_view == NULL)
8176 return got_error_from_errno("view_open");
8177 if (view->type == TOG_VIEW_LOG)
8178 err = open_ref_view(*new_view, view->state.log.repo);
8179 else if (view->type == TOG_VIEW_TREE)
8180 err = open_ref_view(*new_view, view->state.tree.repo);
8182 err = got_error_msg(GOT_ERR_NOT_IMPL,
8183 "parent/child view pair not supported");
8185 view_close(*new_view);
8188 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8195 * If view was scrolled down to move the selected line into view when opening a
8196 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8199 offset_selection_up(struct tog_view *view)
8201 switch (view->type) {
8202 case TOG_VIEW_BLAME: {
8203 struct tog_blame_view_state *s = &view->state.blame;
8204 if (s->first_displayed_line == 1) {
8205 s->selected_line = MAX(s->selected_line - view->offset,
8209 if (s->first_displayed_line > view->offset)
8210 s->first_displayed_line -= view->offset;
8212 s->first_displayed_line = 1;
8213 s->selected_line += view->offset;
8217 log_scroll_up(&view->state.log, view->offset);
8218 view->state.log.selected += view->offset;
8221 ref_scroll_up(&view->state.ref, view->offset);
8222 view->state.ref.selected += view->offset;
8225 tree_scroll_up(&view->state.tree, view->offset);
8226 view->state.tree.selected += view->offset;
8236 * If the selected line is in the section of screen covered by the bottom split,
8237 * scroll down offset lines to move it into view and index its new position.
8239 static const struct got_error *
8240 offset_selection_down(struct tog_view *view)
8242 const struct got_error *err = NULL;
8243 const struct got_error *(*scrolld)(struct tog_view *, int);
8244 int *selected = NULL;
8247 switch (view->type) {
8248 case TOG_VIEW_BLAME: {
8249 struct tog_blame_view_state *s = &view->state.blame;
8252 if (s->selected_line > view->nlines - header) {
8253 offset = abs(view->nlines - s->selected_line - header);
8254 s->first_displayed_line += offset;
8255 s->selected_line -= offset;
8256 view->offset = offset;
8260 case TOG_VIEW_LOG: {
8261 struct tog_log_view_state *s = &view->state.log;
8262 scrolld = &log_scroll_down;
8263 header = view_is_parent_view(view) ? 3 : 2;
8264 selected = &s->selected;
8267 case TOG_VIEW_REF: {
8268 struct tog_ref_view_state *s = &view->state.ref;
8269 scrolld = &ref_scroll_down;
8271 selected = &s->selected;
8274 case TOG_VIEW_TREE: {
8275 struct tog_tree_view_state *s = &view->state.tree;
8276 scrolld = &tree_scroll_down;
8278 selected = &s->selected;
8288 if (selected && *selected > view->nlines - header) {
8289 offset = abs(view->nlines - *selected - header);
8290 view->offset = offset;
8291 if (scrolld && offset) {
8292 err = scrolld(view, offset);
8293 *selected -= offset;
8301 list_commands(FILE *fp)
8305 fprintf(fp, "commands:");
8306 for (i = 0; i < nitems(tog_commands); i++) {
8307 const struct tog_cmd *cmd = &tog_commands[i];
8308 fprintf(fp, " %s", cmd->name);
8314 usage(int hflag, int status)
8316 FILE *fp = (status == 0) ? stdout : stderr;
8318 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8321 fprintf(fp, "lazy usage: %s path\n", getprogname());
8328 make_argv(int argc, ...)
8336 argv = calloc(argc, sizeof(char *));
8339 for (i = 0; i < argc; i++) {
8340 argv[i] = strdup(va_arg(ap, char *));
8341 if (argv[i] == NULL)
8350 * Try to convert 'tog path' into a 'tog log path' command.
8351 * The user could simply have mistyped the command rather than knowingly
8352 * provided a path. So check whether argv[0] can in fact be resolved
8353 * to a path in the HEAD commit and print a special error if not.
8354 * This hack is for mpi@ <3
8356 static const struct got_error *
8357 tog_log_with_path(int argc, char *argv[])
8359 const struct got_error *error = NULL, *close_err;
8360 const struct tog_cmd *cmd = NULL;
8361 struct got_repository *repo = NULL;
8362 struct got_worktree *worktree = NULL;
8363 struct got_object_id *commit_id = NULL, *id = NULL;
8364 struct got_commit_object *commit = NULL;
8365 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8366 char *commit_id_str = NULL, **cmd_argv = NULL;
8367 int *pack_fds = NULL;
8369 cwd = getcwd(NULL, 0);
8371 return got_error_from_errno("getcwd");
8373 error = got_repo_pack_fds_open(&pack_fds);
8377 error = got_worktree_open(&worktree, cwd);
8378 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8382 repo_path = strdup(got_worktree_get_repo_path(worktree));
8384 repo_path = strdup(cwd);
8385 if (repo_path == NULL) {
8386 error = got_error_from_errno("strdup");
8390 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8394 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8399 error = tog_load_refs(repo, 0);
8402 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8403 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8404 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8409 got_worktree_close(worktree);
8413 error = got_object_open_as_commit(&commit, repo, commit_id);
8417 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8419 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8421 fprintf(stderr, "%s: '%s' is no known command or path\n",
8422 getprogname(), argv[0]);
8427 error = got_object_id_str(&commit_id_str, commit_id);
8431 cmd = &tog_commands[0]; /* log */
8433 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8434 error = cmd->cmd_main(argc, cmd_argv);
8437 close_err = got_repo_close(repo);
8442 got_object_commit_close(commit);
8444 got_worktree_close(worktree);
8446 const struct got_error *pack_err =
8447 got_repo_pack_fds_close(pack_fds);
8452 free(commit_id_str);
8459 for (i = 0; i < argc; i++)
8468 main(int argc, char *argv[])
8470 const struct got_error *error = NULL;
8471 const struct tog_cmd *cmd = NULL;
8472 int ch, hflag = 0, Vflag = 0;
8473 char **cmd_argv = NULL;
8474 static const struct option longopts[] = {
8475 { "version", no_argument, NULL, 'V' },
8478 char *diff_algo_str = NULL;
8480 setlocale(LC_CTYPE, "");
8482 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8502 got_version_print_str();
8507 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8515 /* Build an argument vector which runs a default command. */
8516 cmd = &tog_commands[0];
8518 cmd_argv = make_argv(argc, cmd->name);
8522 /* Did the user specify a command? */
8523 for (i = 0; i < nitems(tog_commands); i++) {
8524 if (strncmp(tog_commands[i].name, argv[0],
8525 strlen(argv[0])) == 0) {
8526 cmd = &tog_commands[i];
8532 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8533 if (diff_algo_str) {
8534 if (strcasecmp(diff_algo_str, "patience") == 0)
8535 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8536 if (strcasecmp(diff_algo_str, "myers") == 0)
8537 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8543 /* No command specified; try log with a path */
8544 error = tog_log_with_path(argc, argv);
8549 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8556 for (i = 0; i < argc; i++)
8561 if (error && error->code != GOT_ERR_CANCELLED)
8562 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);