2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/ioctl.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
52 #include "got_opentemp.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
59 #include "got_worktree.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #define CTRL(x) ((x) & 0x1f)
74 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
83 __dead static void usage(int, int);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_ref(void);
90 static const struct got_error* cmd_log(int, char *[]);
91 static const struct got_error* cmd_diff(int, char *[]);
92 static const struct got_error* cmd_blame(int, char *[]);
93 static const struct got_error* cmd_tree(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
96 static const struct tog_cmd tog_commands[] = {
97 { "log", cmd_log, usage_log },
98 { "diff", cmd_diff, usage_diff },
99 { "blame", cmd_blame, usage_blame },
100 { "tree", cmd_tree, usage_tree },
101 { "ref", cmd_ref, usage_ref },
118 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 #define TOG_EOF_STRING "(END)"
122 struct commit_queue_entry {
123 TAILQ_ENTRY(commit_queue_entry) entry;
124 struct got_object_id *id;
125 struct got_commit_object *commit;
128 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
129 struct commit_queue {
131 struct commit_queue_head head;
135 STAILQ_ENTRY(tog_color) entry;
139 STAILQ_HEAD(tog_colors, tog_color);
141 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
142 static struct got_reflist_object_id_map *tog_refs_idmap;
143 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 static const struct got_error *
146 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
147 struct got_reference* re2)
149 const char *name1 = got_ref_get_name(re1);
150 const char *name2 = got_ref_get_name(re2);
151 int isbackup1, isbackup2;
153 /* Sort backup refs towards the bottom of the list. */
154 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
155 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
156 if (!isbackup1 && isbackup2) {
159 } else if (isbackup1 && !isbackup2) {
164 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
168 static const struct got_error *
169 tog_load_refs(struct got_repository *repo, int sort_by_date)
171 const struct got_error *err;
173 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
174 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
179 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
186 if (tog_refs_idmap) {
187 got_reflist_object_id_map_free(tog_refs_idmap);
188 tog_refs_idmap = NULL;
190 got_ref_list_free(&tog_refs);
193 static const struct got_error *
194 add_color(struct tog_colors *colors, const char *pattern,
195 int idx, short color)
197 const struct got_error *err = NULL;
198 struct tog_color *tc;
201 if (idx < 1 || idx > COLOR_PAIRS - 1)
204 init_pair(idx, color, -1);
206 tc = calloc(1, sizeof(*tc));
208 return got_error_from_errno("calloc");
209 regerr = regcomp(&tc->regex, pattern,
210 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 static char regerr_msg[512];
213 static char err_msg[512];
214 regerror(regerr, &tc->regex, regerr_msg,
216 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 err = got_error_msg(GOT_ERR_REGEX, err_msg);
223 STAILQ_INSERT_HEAD(colors, tc, entry);
228 free_colors(struct tog_colors *colors)
230 struct tog_color *tc;
232 while (!STAILQ_EMPTY(colors)) {
233 tc = STAILQ_FIRST(colors);
234 STAILQ_REMOVE_HEAD(colors, entry);
240 static struct tog_color *
241 get_color(struct tog_colors *colors, int colorpair)
243 struct tog_color *tc = NULL;
245 STAILQ_FOREACH(tc, colors, entry) {
246 if (tc->colorpair == colorpair)
254 default_color_value(const char *envvar)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
257 return COLOR_MAGENTA;
258 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
265 return COLOR_MAGENTA;
266 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
291 get_color_value(const char *envvar)
293 const char *val = getenv(envvar);
296 return default_color_value(envvar);
298 if (strcasecmp(val, "black") == 0)
300 if (strcasecmp(val, "red") == 0)
302 if (strcasecmp(val, "green") == 0)
304 if (strcasecmp(val, "yellow") == 0)
306 if (strcasecmp(val, "blue") == 0)
308 if (strcasecmp(val, "magenta") == 0)
309 return COLOR_MAGENTA;
310 if (strcasecmp(val, "cyan") == 0)
312 if (strcasecmp(val, "white") == 0)
314 if (strcasecmp(val, "default") == 0)
317 return default_color_value(envvar);
321 struct tog_diff_view_state {
322 struct got_object_id *id1, *id2;
323 const char *label1, *label2;
326 int first_displayed_line;
327 int last_displayed_line;
330 int ignore_whitespace;
332 struct got_repository *repo;
333 struct tog_colors colors;
339 /* passed from log or blame view; may be NULL */
340 struct tog_view *parent_view;
343 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 static volatile sig_atomic_t tog_thread_error;
346 struct tog_log_thread_args {
347 pthread_cond_t need_commits;
348 pthread_cond_t commit_loaded;
351 struct got_commit_graph *graph;
352 struct commit_queue *commits;
353 const char *in_repo_path;
354 struct got_object_id *start_id;
355 struct got_repository *repo;
359 struct commit_queue_entry **first_displayed_entry;
360 struct commit_queue_entry **selected_entry;
362 int *search_next_done;
366 struct tog_log_view_state {
367 struct commit_queue commits;
368 struct commit_queue_entry *first_displayed_entry;
369 struct commit_queue_entry *last_displayed_entry;
370 struct commit_queue_entry *selected_entry;
375 struct got_repository *repo;
376 struct got_object_id *start_id;
379 struct tog_log_thread_args thread_args;
380 struct commit_queue_entry *matched_entry;
381 struct commit_queue_entry *search_entry;
382 struct tog_colors colors;
385 #define TOG_COLOR_DIFF_MINUS 1
386 #define TOG_COLOR_DIFF_PLUS 2
387 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
388 #define TOG_COLOR_DIFF_META 4
389 #define TOG_COLOR_TREE_SUBMODULE 5
390 #define TOG_COLOR_TREE_SYMLINK 6
391 #define TOG_COLOR_TREE_DIRECTORY 7
392 #define TOG_COLOR_TREE_EXECUTABLE 8
393 #define TOG_COLOR_COMMIT 9
394 #define TOG_COLOR_AUTHOR 10
395 #define TOG_COLOR_DATE 11
396 #define TOG_COLOR_REFS_HEADS 12
397 #define TOG_COLOR_REFS_TAGS 13
398 #define TOG_COLOR_REFS_REMOTES 14
399 #define TOG_COLOR_REFS_BACKUP 15
401 struct tog_blame_cb_args {
402 struct tog_blame_line *lines; /* one per line */
405 struct tog_view *view;
406 struct got_object_id *commit_id;
410 struct tog_blame_thread_args {
412 struct got_repository *repo;
413 struct tog_blame_cb_args *cb_args;
415 got_cancel_cb cancel_cb;
422 struct tog_blame_line *lines;
426 struct tog_blame_thread_args thread_args;
427 struct tog_blame_cb_args cb_args;
432 struct tog_blame_view_state {
433 int first_displayed_line;
434 int last_displayed_line;
436 int last_diffed_line;
440 struct got_object_id_queue blamed_commits;
441 struct got_object_qid *blamed_commit;
443 struct got_repository *repo;
444 struct got_object_id *commit_id;
445 struct tog_blame blame;
447 struct tog_colors colors;
450 struct tog_parent_tree {
451 TAILQ_ENTRY(tog_parent_tree) entry;
452 struct got_tree_object *tree;
453 struct got_tree_entry *first_displayed_entry;
454 struct got_tree_entry *selected_entry;
458 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
460 struct tog_tree_view_state {
462 struct got_object_id *commit_id;/* commit which this tree belongs to */
463 struct got_tree_object *root; /* the commit's root tree entry */
464 struct got_tree_object *tree; /* currently displayed (sub-)tree */
465 struct got_tree_entry *first_displayed_entry;
466 struct got_tree_entry *last_displayed_entry;
467 struct got_tree_entry *selected_entry;
468 int ndisplayed, selected, show_ids;
469 struct tog_parent_trees parents; /* parent trees of current sub-tree */
471 struct got_repository *repo;
472 struct got_tree_entry *matched_entry;
473 struct tog_colors colors;
476 struct tog_reflist_entry {
477 TAILQ_ENTRY(tog_reflist_entry) entry;
478 struct got_reference *ref;
482 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
484 struct tog_ref_view_state {
485 struct tog_reflist_head refs;
486 struct tog_reflist_entry *first_displayed_entry;
487 struct tog_reflist_entry *last_displayed_entry;
488 struct tog_reflist_entry *selected_entry;
489 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
490 struct got_repository *repo;
491 struct tog_reflist_entry *matched_entry;
492 struct tog_colors colors;
496 * We implement two types of views: parent views and child views.
498 * The 'Tab' key switches focus between a parent view and its child view.
499 * Child views are shown side-by-side to their parent view, provided
500 * there is enough screen estate.
502 * When a new view is opened from within a parent view, this new view
503 * becomes a child view of the parent view, replacing any existing child.
505 * When a new view is opened from within a child view, this new view
506 * becomes a parent view which will obscure the views below until the
507 * user quits the new parent view by typing 'q'.
509 * This list of views contains parent views only.
510 * Child views are only pointed to by their parent view.
512 TAILQ_HEAD(tog_view_list_head, tog_view);
515 TAILQ_ENTRY(tog_view) entry;
518 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
519 int resized_y, resized_x; /* begin_y/x based on user resizing */
520 int maxx, x; /* max column and current start column */
521 int lines, cols; /* copies of LINES and COLS */
522 int nscrolled, offset; /* lines scrolled and hsplit line offset */
523 int ch, count; /* current keymap and count prefix */
524 int resize; /* set when in a resize event */
525 int focussed; /* Only set on one parent or child view at a time. */
527 struct tog_view *parent;
528 struct tog_view *child;
531 * This flag is initially set on parent views when a new child view
532 * is created. It gets toggled when the 'Tab' key switches focus
533 * between parent and child.
534 * The flag indicates whether focus should be passed on to our child
535 * view if this parent view gets picked for focus after another parent
536 * view was closed. This prevents child views from losing focus in such
541 enum tog_view_mode mode;
542 /* type-specific state */
543 enum tog_view_type type;
545 struct tog_diff_view_state diff;
546 struct tog_log_view_state log;
547 struct tog_blame_view_state blame;
548 struct tog_tree_view_state tree;
549 struct tog_ref_view_state ref;
552 const struct got_error *(*show)(struct tog_view *);
553 const struct got_error *(*input)(struct tog_view **,
554 struct tog_view *, int);
555 const struct got_error *(*reset)(struct tog_view *);
556 const struct got_error *(*close)(struct tog_view *);
558 const struct got_error *(*search_start)(struct tog_view *);
559 const struct got_error *(*search_next)(struct tog_view *);
562 #define TOG_SEARCH_FORWARD 1
563 #define TOG_SEARCH_BACKWARD 2
564 int search_next_done;
565 #define TOG_SEARCH_HAVE_MORE 1
566 #define TOG_SEARCH_NO_MORE 2
567 #define TOG_SEARCH_HAVE_NONE 3
572 static const struct got_error *open_diff_view(struct tog_view *,
573 struct got_object_id *, struct got_object_id *,
574 const char *, const char *, int, int, int, struct tog_view *,
575 struct got_repository *);
576 static const struct got_error *show_diff_view(struct tog_view *);
577 static const struct got_error *input_diff_view(struct tog_view **,
578 struct tog_view *, int);
579 static const struct got_error *reset_diff_view(struct tog_view *);
580 static const struct got_error* close_diff_view(struct tog_view *);
581 static const struct got_error *search_start_diff_view(struct tog_view *);
582 static const struct got_error *search_next_diff_view(struct tog_view *);
584 static const struct got_error *open_log_view(struct tog_view *,
585 struct got_object_id *, struct got_repository *,
586 const char *, const char *, int);
587 static const struct got_error * show_log_view(struct tog_view *);
588 static const struct got_error *input_log_view(struct tog_view **,
589 struct tog_view *, int);
590 static const struct got_error *close_log_view(struct tog_view *);
591 static const struct got_error *search_start_log_view(struct tog_view *);
592 static const struct got_error *search_next_log_view(struct tog_view *);
594 static const struct got_error *open_blame_view(struct tog_view *, char *,
595 struct got_object_id *, struct got_repository *);
596 static const struct got_error *show_blame_view(struct tog_view *);
597 static const struct got_error *input_blame_view(struct tog_view **,
598 struct tog_view *, int);
599 static const struct got_error *reset_blame_view(struct tog_view *);
600 static const struct got_error *close_blame_view(struct tog_view *);
601 static const struct got_error *search_start_blame_view(struct tog_view *);
602 static const struct got_error *search_next_blame_view(struct tog_view *);
604 static const struct got_error *open_tree_view(struct tog_view *,
605 struct got_object_id *, const char *, struct got_repository *);
606 static const struct got_error *show_tree_view(struct tog_view *);
607 static const struct got_error *input_tree_view(struct tog_view **,
608 struct tog_view *, int);
609 static const struct got_error *close_tree_view(struct tog_view *);
610 static const struct got_error *search_start_tree_view(struct tog_view *);
611 static const struct got_error *search_next_tree_view(struct tog_view *);
613 static const struct got_error *open_ref_view(struct tog_view *,
614 struct got_repository *);
615 static const struct got_error *show_ref_view(struct tog_view *);
616 static const struct got_error *input_ref_view(struct tog_view **,
617 struct tog_view *, int);
618 static const struct got_error *close_ref_view(struct tog_view *);
619 static const struct got_error *search_start_ref_view(struct tog_view *);
620 static const struct got_error *search_next_ref_view(struct tog_view *);
622 static volatile sig_atomic_t tog_sigwinch_received;
623 static volatile sig_atomic_t tog_sigpipe_received;
624 static volatile sig_atomic_t tog_sigcont_received;
625 static volatile sig_atomic_t tog_sigint_received;
626 static volatile sig_atomic_t tog_sigterm_received;
629 tog_sigwinch(int signo)
631 tog_sigwinch_received = 1;
635 tog_sigpipe(int signo)
637 tog_sigpipe_received = 1;
641 tog_sigcont(int signo)
643 tog_sigcont_received = 1;
647 tog_sigint(int signo)
649 tog_sigint_received = 1;
653 tog_sigterm(int signo)
655 tog_sigterm_received = 1;
659 tog_fatal_signal_received(void)
661 return (tog_sigpipe_received ||
662 tog_sigint_received || tog_sigint_received);
665 static const struct got_error *
666 view_close(struct tog_view *view)
668 const struct got_error *err = NULL, *child_err = NULL;
671 child_err = view_close(view->child);
675 err = view->close(view);
677 del_panel(view->panel);
679 delwin(view->window);
681 return err ? err : child_err;
684 static struct tog_view *
685 view_open(int nlines, int ncols, int begin_y, int begin_x,
686 enum tog_view_type type)
688 struct tog_view *view = calloc(1, sizeof(*view));
696 view->nlines = nlines ? nlines : LINES - begin_y;
697 view->ncols = ncols ? ncols : COLS - begin_x;
698 view->begin_y = begin_y;
699 view->begin_x = begin_x;
700 view->window = newwin(nlines, ncols, begin_y, begin_x);
701 if (view->window == NULL) {
705 view->panel = new_panel(view->window);
706 if (view->panel == NULL ||
707 set_panel_userptr(view->panel, view) != OK) {
712 keypad(view->window, TRUE);
717 view_split_begin_x(int begin_x)
719 if (begin_x > 0 || COLS < 120)
721 return (COLS - MAX(COLS / 2, 80));
724 /* XXX Stub till we decide what to do. */
726 view_split_begin_y(int lines)
728 return lines * HSPLIT_SCALE;
731 static const struct got_error *view_resize(struct tog_view *);
733 static const struct got_error *
734 view_splitscreen(struct tog_view *view)
736 const struct got_error *err = NULL;
738 if (!view->resize && view->mode == TOG_VIEW_SPLIT_HRZN) {
739 if (view->resized_y && view->resized_y < view->lines)
740 view->begin_y = view->resized_y;
742 view->begin_y = view_split_begin_y(view->nlines);
744 } else if (!view->resize) {
745 if (view->resized_x && view->resized_x < view->cols - 1 &&
747 view->begin_x = view->resized_x;
749 view->begin_x = view_split_begin_x(0);
752 view->nlines = LINES - view->begin_y;
753 view->ncols = COLS - view->begin_x;
756 err = view_resize(view);
760 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
761 view->parent->nlines = view->begin_y;
763 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
764 return got_error_from_errno("mvwin");
769 static const struct got_error *
770 view_fullscreen(struct tog_view *view)
772 const struct got_error *err = NULL;
775 view->begin_y = view->resize ? view->begin_y : 0;
776 view->nlines = view->resize ? view->nlines : LINES;
780 err = view_resize(view);
784 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
785 return got_error_from_errno("mvwin");
791 view_is_parent_view(struct tog_view *view)
793 return view->parent == NULL;
797 view_is_splitscreen(struct tog_view *view)
799 return view->begin_x > 0 || view->begin_y > 0;
803 view_is_fullscreen(struct tog_view *view)
805 return view->nlines == LINES && view->ncols == COLS;
809 view_is_hsplit_top(struct tog_view *view)
811 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
812 view_is_splitscreen(view->child);
816 view_border(struct tog_view *view)
819 const struct tog_view *view_above;
822 return view_border(view->parent);
824 panel = panel_above(view->panel);
828 view_above = panel_userptr(panel);
829 if (view->mode == TOG_VIEW_SPLIT_HRZN)
830 mvwhline(view->window, view_above->begin_y - 1,
831 view->begin_x, got_locale_is_utf8() ?
832 ACS_HLINE : '-', view->ncols);
834 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
835 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
838 static const struct got_error *view_init_hsplit(struct tog_view *, int);
839 static const struct got_error *request_log_commits(struct tog_view *);
840 static const struct got_error *offset_selection_down(struct tog_view *);
841 static void offset_selection_up(struct tog_view *);
842 static void view_get_split(struct tog_view *, int *, int *);
844 static const struct got_error *
845 view_resize(struct tog_view *view)
847 const struct got_error *err = NULL;
848 int dif, nlines, ncols;
850 dif = LINES - view->lines; /* line difference */
852 if (view->lines > LINES)
853 nlines = view->nlines - (view->lines - LINES);
855 nlines = view->nlines + (LINES - view->lines);
856 if (view->cols > COLS)
857 ncols = view->ncols - (view->cols - COLS);
859 ncols = view->ncols + (COLS - view->cols);
862 int hs = view->child->begin_y;
864 if (!view_is_fullscreen(view))
865 view->child->begin_x = view_split_begin_x(view->begin_x);
866 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
867 view->child->begin_x == 0) {
870 view_fullscreen(view->child);
871 if (view->child->focussed)
872 show_panel(view->child->panel);
874 show_panel(view->panel);
876 ncols = view->child->begin_x;
878 view_splitscreen(view->child);
879 show_panel(view->child->panel);
882 * Request commits if terminal height was increased in a log
883 * view so we have enough commits loaded to populate the view.
885 if (view->type == TOG_VIEW_LOG && dif > 0) {
886 struct tog_log_view_state *ts = &view->state.log;
888 if (ts->commits.ncommits < ts->selected_entry->idx +
889 view->lines - ts->selected) {
890 view->nscrolled = ts->selected_entry->idx +
891 view->lines - ts->selected -
892 ts->commits.ncommits + dif;
893 err = request_log_commits(view);
900 * XXX This is ugly and needs to be moved into the above
901 * logic but "works" for now and my attempts at moving it
902 * break either 'tab' or 'F' key maps in horizontal splits.
905 err = view_splitscreen(view->child);
908 if (dif < 0) { /* top split decreased */
909 err = offset_selection_down(view);
916 show_panel(view->child->panel);
917 nlines = view->nlines;
919 } else if (view->parent == NULL)
922 if (wresize(view->window, nlines, ncols) == ERR)
923 return got_error_from_errno("wresize");
924 if (replace_panel(view->panel, view->window) == ERR)
925 return got_error_from_errno("replace_panel");
926 wclear(view->window);
928 view->nlines = nlines;
937 view_adjust_offset(struct tog_view *view, int n)
942 if (view->parent && view->parent->offset) {
943 if (view->parent->offset + n >= 0)
944 view->parent->offset += n;
946 view->parent->offset = 0;
947 } else if (view->offset) {
948 if (view->offset - n >= 0)
955 static const struct got_error *
956 view_resize_split(struct tog_view *view, int resize)
958 const struct got_error *err = NULL;
959 struct tog_view *v = NULL;
966 if (!v->child || !view_is_splitscreen(v->child))
969 v->resize = v->child->resize = resize; /* lock for resize event */
971 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
972 int y = v->child->begin_y;
974 if (v->child->resized_y)
975 v->child->begin_y = v->child->resized_y;
977 v->child->begin_y -= resize;
979 v->child->begin_y += resize;
980 if (v->child->begin_y < 3) {
982 v->child->begin_y = 3;
983 } else if (v->child->begin_y > LINES - 1) {
985 v->child->begin_y = LINES - 1;
988 v->child->ncols = COLS;
989 view_adjust_offset(view, resize);
990 err = view_init_hsplit(v, v->child->begin_y);
993 v->child->resized_y = v->child->begin_y;
994 if (y > v->child->begin_y) /* split increased */
995 v->child->nscrolled = y - v->child->begin_y;
997 if (v->child->resized_x)
998 v->child->begin_x = v->child->resized_x;
1000 v->child->begin_x -= resize;
1002 v->child->begin_x += resize;
1003 if (v->child->begin_x < 11) {
1005 v->child->begin_x = 11;
1006 } else if (v->child->begin_x > COLS - 1) {
1008 v->child->begin_x = COLS - 1;
1010 v->child->resized_x = v->child->begin_x;
1013 v->child->mode = v->mode;
1014 v->child->nlines = v->lines - v->child->begin_y;
1015 v->child->ncols = v->cols - v->child->begin_x;
1018 err = view_fullscreen(v);
1021 err = view_splitscreen(v->child);
1025 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1026 err = offset_selection_down(v->child);
1031 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1032 err = request_log_commits(v);
1033 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1034 err = request_log_commits(v->child);
1036 v->resize = v->child->resize = 0;
1042 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1044 struct tog_view *v = src->child ? src->child : src;
1046 dst->resized_x = v->resized_x;
1047 dst->resized_y = v->resized_y;
1050 static const struct got_error *
1051 view_close_child(struct tog_view *view)
1053 const struct got_error *err = NULL;
1055 if (view->child == NULL)
1058 err = view_close(view->child);
1063 static const struct got_error *
1064 view_set_child(struct tog_view *view, struct tog_view *child)
1066 const struct got_error *err = NULL;
1068 view->child = child;
1069 child->parent = view;
1071 err = view_resize(view);
1075 if (view->child->resized_x || view->child->resized_y)
1076 err = view_resize_split(view, 0);
1082 tog_resizeterm(void)
1085 struct winsize size;
1087 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1088 cols = 80; /* Default */
1092 lines = size.ws_row;
1094 resize_term(lines, cols);
1097 static const struct got_error *
1098 view_search_start(struct tog_view *view)
1100 const struct got_error *err = NULL;
1101 struct tog_view *v = view;
1105 if (view->search_started) {
1106 regfree(&view->regex);
1107 view->searching = 0;
1108 memset(&view->regmatch, 0, sizeof(view->regmatch));
1110 view->search_started = 0;
1112 if (view->nlines < 1)
1115 if (view_is_hsplit_top(view))
1118 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1119 wclrtoeol(v->window);
1121 nodelay(view->window, FALSE); /* block for search term input */
1124 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1125 wrefresh(v->window);
1128 nodelay(view->window, TRUE);
1132 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1133 err = view->search_start(view);
1135 regfree(&view->regex);
1138 view->search_started = 1;
1139 view->searching = TOG_SEARCH_FORWARD;
1140 view->search_next_done = 0;
1141 view->search_next(view);
1147 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1148 static const struct got_error *
1149 switch_split(struct tog_view *view)
1151 const struct got_error *err = NULL;
1152 struct tog_view *v = NULL;
1159 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1160 v->mode = TOG_VIEW_SPLIT_VERT;
1162 v->mode = TOG_VIEW_SPLIT_HRZN;
1166 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1167 v->mode = TOG_VIEW_SPLIT_NONE;
1169 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1170 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1171 v->child->begin_y = v->child->resized_y;
1172 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1173 v->child->begin_x = v->child->resized_x;
1176 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1178 v->child->ncols = COLS;
1179 v->child->nscrolled = LINES - v->child->nlines;
1181 err = view_init_hsplit(v, v->child->begin_y);
1185 v->child->mode = v->mode;
1186 v->child->nlines = v->lines - v->child->begin_y;
1189 err = view_fullscreen(v);
1192 err = view_splitscreen(v->child);
1196 if (v->mode == TOG_VIEW_SPLIT_NONE)
1197 v->mode = TOG_VIEW_SPLIT_VERT;
1198 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1199 err = offset_selection_down(v);
1200 err = offset_selection_down(v->child);
1202 offset_selection_up(v);
1203 offset_selection_up(v->child);
1205 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1206 err = request_log_commits(v);
1207 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1208 err = request_log_commits(v->child);
1214 * Compute view->count from numeric input. Assign total to view->count and
1215 * return first non-numeric key entered.
1218 get_compound_key(struct tog_view *view, int c)
1220 struct tog_view *v = view;
1223 if (view_is_hsplit_top(view))
1225 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1229 cbreak(); /* block for input */
1230 wmove(v->window, v->nlines - 1, 0);
1231 wclrtoeol(v->window);
1232 waddch(v->window, ':');
1235 x = getcurx(v->window);
1236 if (x != ERR && x < view->ncols) {
1237 waddch(v->window, c);
1238 wrefresh(v->window);
1242 * Don't overflow. Max valid request should be the greatest
1243 * between the longest and total lines; cap at 10 million.
1248 n = n * 10 + (c - '0');
1249 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1251 /* Massage excessive or inapplicable values at the input handler. */
1257 static const struct got_error *
1258 view_input(struct tog_view **new, int *done, struct tog_view *view,
1259 struct tog_view_list_head *views)
1261 const struct got_error *err = NULL;
1267 /* Clear "no matches" indicator. */
1268 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1269 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1270 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1274 if (view->searching && !view->search_next_done) {
1275 errcode = pthread_mutex_unlock(&tog_mutex);
1277 return got_error_set_errno(errcode,
1278 "pthread_mutex_unlock");
1280 errcode = pthread_mutex_lock(&tog_mutex);
1282 return got_error_set_errno(errcode,
1283 "pthread_mutex_lock");
1284 view->search_next(view);
1288 nodelay(view->window, FALSE);
1289 /* Allow threads to make progress while we are waiting for input. */
1290 errcode = pthread_mutex_unlock(&tog_mutex);
1292 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1293 /* If we have an unfinished count, let C-g or backspace abort. */
1294 if (view->count && --view->count) {
1296 nodelay(view->window, TRUE);
1297 ch = wgetch(view->window);
1298 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1303 ch = wgetch(view->window);
1304 if (ch >= '1' && ch <= '9')
1305 view->ch = ch = get_compound_key(view, ch);
1307 errcode = pthread_mutex_lock(&tog_mutex);
1309 return got_error_set_errno(errcode, "pthread_mutex_lock");
1310 nodelay(view->window, TRUE);
1312 if (tog_sigwinch_received || tog_sigcont_received) {
1314 tog_sigwinch_received = 0;
1315 tog_sigcont_received = 0;
1316 TAILQ_FOREACH(v, views, entry) {
1317 err = view_resize(v);
1320 err = v->input(new, v, KEY_RESIZE);
1324 err = view_resize(v->child);
1327 err = v->child->input(new, v->child,
1331 if (v->child->resized_x || v->child->resized_y) {
1332 err = view_resize_split(v, 0);
1345 view->child->focussed = 1;
1346 view->focus_child = 1;
1347 } else if (view->parent) {
1349 view->parent->focussed = 1;
1350 view->parent->focus_child = 0;
1351 if (!view_is_splitscreen(view)) {
1352 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1353 view->parent->type == TOG_VIEW_LOG) {
1354 err = request_log_commits(view->parent);
1358 offset_selection_up(view->parent);
1359 err = view_fullscreen(view->parent);
1366 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1367 if (view->parent->type == TOG_VIEW_LOG) {
1368 /* might need more commits to fill fullscreen */
1369 err = request_log_commits(view->parent);
1373 offset_selection_up(view->parent);
1375 err = view->input(new, view, ch);
1383 if (view_is_parent_view(view)) {
1384 if (view->child == NULL)
1386 if (view_is_splitscreen(view->child)) {
1388 view->child->focussed = 1;
1389 err = view_fullscreen(view->child);
1391 err = view_splitscreen(view->child);
1393 err = view_resize_split(view, 0);
1397 err = view->child->input(new, view->child,
1400 if (view_is_splitscreen(view)) {
1401 view->parent->focussed = 0;
1403 err = view_fullscreen(view);
1405 err = view_splitscreen(view);
1406 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1407 err = view_resize(view->parent);
1409 err = view_resize_split(view, 0);
1413 err = view->input(new, view, KEY_RESIZE);
1417 if (view->type == TOG_VIEW_LOG) {
1418 err = request_log_commits(view);
1423 err = offset_selection_down(view->parent);
1425 err = offset_selection_down(view);
1429 err = switch_split(view);
1432 err = view_resize_split(view, -1);
1435 err = view_resize_split(view, 1);
1441 if (view->search_start)
1442 view_search_start(view);
1444 err = view->input(new, view, ch);
1448 if (view->search_started && view->search_next) {
1449 view->searching = (ch == 'n' ?
1450 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1451 view->search_next_done = 0;
1452 view->search_next(view);
1454 err = view->input(new, view, ch);
1457 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1458 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1460 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1461 TAILQ_FOREACH(v, views, entry) {
1467 if (v->child && v->child->reset) {
1468 err = v->child->reset(v->child);
1475 err = view->input(new, view, ch);
1483 view_needs_focus_indication(struct tog_view *view)
1485 if (view_is_parent_view(view)) {
1486 if (view->child == NULL || view->child->focussed)
1488 if (!view_is_splitscreen(view->child))
1490 } else if (!view_is_splitscreen(view))
1493 return view->focussed;
1496 static const struct got_error *
1497 view_loop(struct tog_view *view)
1499 const struct got_error *err = NULL;
1500 struct tog_view_list_head views;
1501 struct tog_view *new_view;
1503 int fast_refresh = 10;
1504 int done = 0, errcode;
1506 mode = getenv("TOG_VIEW_SPLIT_MODE");
1507 if (!mode || !(*mode == 'h' || *mode == 'H'))
1508 view->mode = TOG_VIEW_SPLIT_VERT;
1510 view->mode = TOG_VIEW_SPLIT_HRZN;
1512 errcode = pthread_mutex_lock(&tog_mutex);
1514 return got_error_set_errno(errcode, "pthread_mutex_lock");
1517 TAILQ_INSERT_HEAD(&views, view, entry);
1520 err = view->show(view);
1525 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1526 !tog_fatal_signal_received()) {
1527 /* Refresh fast during initialization, then become slower. */
1528 if (fast_refresh && fast_refresh-- == 0)
1529 halfdelay(10); /* switch to once per second */
1531 err = view_input(&new_view, &done, view, &views);
1535 struct tog_view *v, *prev = NULL;
1537 if (view_is_parent_view(view))
1538 prev = TAILQ_PREV(view, tog_view_list_head,
1540 else if (view->parent)
1541 prev = view->parent;
1544 view->parent->child = NULL;
1545 view->parent->focus_child = 0;
1546 /* Restore fullscreen line height. */
1547 view->parent->nlines = view->parent->lines;
1548 err = view_resize(view->parent);
1551 /* Make resized splits persist. */
1552 view_transfer_size(view->parent, view);
1554 TAILQ_REMOVE(&views, view, entry);
1556 err = view_close(view);
1561 TAILQ_FOREACH(v, &views, entry) {
1565 if (view == NULL && new_view == NULL) {
1566 /* No view has focus. Try to pick one. */
1569 else if (!TAILQ_EMPTY(&views)) {
1570 view = TAILQ_LAST(&views,
1571 tog_view_list_head);
1574 if (view->focus_child) {
1575 view->child->focussed = 1;
1583 struct tog_view *v, *t;
1584 /* Only allow one parent view per type. */
1585 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1586 if (v->type != new_view->type)
1588 TAILQ_REMOVE(&views, v, entry);
1589 err = view_close(v);
1594 TAILQ_INSERT_TAIL(&views, new_view, entry);
1598 if (view_is_parent_view(view)) {
1599 if (view->child && view->child->focussed)
1602 if (view->parent && view->parent->focussed)
1603 view = view->parent;
1605 show_panel(view->panel);
1606 if (view->child && view_is_splitscreen(view->child))
1607 show_panel(view->child->panel);
1608 if (view->parent && view_is_splitscreen(view)) {
1609 err = view->parent->show(view->parent);
1613 err = view->show(view);
1617 err = view->child->show(view->child);
1626 while (!TAILQ_EMPTY(&views)) {
1627 const struct got_error *close_err;
1628 view = TAILQ_FIRST(&views);
1629 TAILQ_REMOVE(&views, view, entry);
1630 close_err = view_close(view);
1631 if (close_err && err == NULL)
1635 errcode = pthread_mutex_unlock(&tog_mutex);
1636 if (errcode && err == NULL)
1637 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1647 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1652 /* Create newly allocated wide-character string equivalent to a byte string. */
1653 static const struct got_error *
1654 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1657 const struct got_error *err = NULL;
1660 *wlen = mbstowcs(NULL, s, 0);
1661 if (*wlen == (size_t)-1) {
1663 if (errno != EILSEQ)
1664 return got_error_from_errno("mbstowcs");
1666 /* byte string invalid in current encoding; try to "fix" it */
1667 err = got_mbsavis(&vis, &vislen, s);
1670 *wlen = mbstowcs(NULL, vis, 0);
1671 if (*wlen == (size_t)-1) {
1672 err = got_error_from_errno("mbstowcs"); /* give up */
1677 *ws = calloc(*wlen + 1, sizeof(**ws));
1679 err = got_error_from_errno("calloc");
1683 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1684 err = got_error_from_errno("mbstowcs");
1695 static const struct got_error *
1696 expand_tab(char **ptr, const char *src)
1699 size_t len, n, idx = 0, sz = 0;
1702 n = len = strlen(src);
1703 dst = malloc(n + 1);
1705 return got_error_from_errno("malloc");
1707 while (idx < len && src[idx]) {
1708 const char c = src[idx];
1711 size_t nb = TABSIZE - sz % TABSIZE;
1714 p = realloc(dst, n + nb);
1717 return got_error_from_errno("realloc");
1722 memset(dst + sz, ' ', nb);
1725 dst[sz++] = src[idx];
1735 * Advance at most n columns from wline starting at offset off.
1736 * Return the index to the first character after the span operation.
1737 * Return the combined column width of all spanned wide character in
1741 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1743 int width, i, cols = 0;
1750 for (i = off; wline[i] != L'\0'; ++i) {
1751 if (wline[i] == L'\t')
1752 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1754 width = wcwidth(wline[i]);
1761 if (cols + width > n)
1771 * Format a line for display, ensuring that it won't overflow a width limit.
1772 * With scrolling, the width returned refers to the scrolled version of the
1773 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1775 static const struct got_error *
1776 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1777 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1779 const struct got_error *err = NULL;
1781 wchar_t *wline = NULL;
1790 err = expand_tab(&exstr, line);
1795 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1800 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1802 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1803 wline[wlen - 1] = L'\0';
1806 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1807 wline[wlen - 1] = L'\0';
1811 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1817 *scrollxp = scrollx;
1825 static const struct got_error*
1826 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1827 struct got_object_id *id, struct got_repository *repo)
1829 static const struct got_error *err = NULL;
1830 struct got_reflist_entry *re;
1836 TAILQ_FOREACH(re, refs, entry) {
1837 struct got_tag_object *tag = NULL;
1838 struct got_object_id *ref_id;
1841 name = got_ref_get_name(re->ref);
1842 if (strcmp(name, GOT_REF_HEAD) == 0)
1844 if (strncmp(name, "refs/", 5) == 0)
1846 if (strncmp(name, "got/", 4) == 0 &&
1847 strncmp(name, "got/backup/", 11) != 0)
1849 if (strncmp(name, "heads/", 6) == 0)
1851 if (strncmp(name, "remotes/", 8) == 0) {
1853 s = strstr(name, "/" GOT_REF_HEAD);
1854 if (s != NULL && s[strlen(s)] == '\0')
1857 err = got_ref_resolve(&ref_id, repo, re->ref);
1860 if (strncmp(name, "tags/", 5) == 0) {
1861 err = got_object_open_as_tag(&tag, repo, ref_id);
1863 if (err->code != GOT_ERR_OBJ_TYPE) {
1867 /* Ref points at something other than a tag. */
1872 cmp = got_object_id_cmp(tag ?
1873 got_object_tag_get_object_id(tag) : ref_id, id);
1876 got_object_tag_close(tag);
1880 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1881 s ? ", " : "", name) == -1) {
1882 err = got_error_from_errno("asprintf");
1893 static const struct got_error *
1894 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1899 smallerthan = strchr(author, '<');
1900 if (smallerthan && smallerthan[1] != '\0')
1901 author = smallerthan + 1;
1902 author[strcspn(author, "@>")] = '\0';
1903 return format_line(wauthor, author_width, NULL, author, 0, limit,
1907 static const struct got_error *
1908 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1909 struct got_object_id *id, const size_t date_display_cols,
1910 int author_display_cols)
1912 struct tog_log_view_state *s = &view->state.log;
1913 const struct got_error *err = NULL;
1914 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1915 char *logmsg0 = NULL, *logmsg = NULL;
1916 char *author = NULL;
1917 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1918 int author_width, logmsg_width;
1919 char *newline, *line = NULL;
1920 int col, limit, scrollx;
1921 const int avail = view->ncols;
1923 time_t committer_time;
1924 struct tog_color *tc;
1926 committer_time = got_object_commit_get_committer_time(commit);
1927 if (gmtime_r(&committer_time, &tm) == NULL)
1928 return got_error_from_errno("gmtime_r");
1929 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1930 return got_error(GOT_ERR_NO_SPACE);
1932 if (avail <= date_display_cols)
1933 limit = MIN(sizeof(datebuf) - 1, avail);
1935 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1936 tc = get_color(&s->colors, TOG_COLOR_DATE);
1938 wattr_on(view->window,
1939 COLOR_PAIR(tc->colorpair), NULL);
1940 waddnstr(view->window, datebuf, limit);
1942 wattr_off(view->window,
1943 COLOR_PAIR(tc->colorpair), NULL);
1950 err = got_object_id_str(&id_str, id);
1953 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1955 wattr_on(view->window,
1956 COLOR_PAIR(tc->colorpair), NULL);
1957 wprintw(view->window, "%.8s ", id_str);
1959 wattr_off(view->window,
1960 COLOR_PAIR(tc->colorpair), NULL);
1967 author = strdup(got_object_commit_get_author(commit));
1968 if (author == NULL) {
1969 err = got_error_from_errno("strdup");
1972 err = format_author(&wauthor, &author_width, author, avail - col, col);
1975 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1977 wattr_on(view->window,
1978 COLOR_PAIR(tc->colorpair), NULL);
1979 waddwstr(view->window, wauthor);
1981 wattr_off(view->window,
1982 COLOR_PAIR(tc->colorpair), NULL);
1983 col += author_width;
1984 while (col < avail && author_width < author_display_cols + 2) {
1985 waddch(view->window, ' ');
1992 err = got_object_commit_get_logmsg(&logmsg0, commit);
1996 while (*logmsg == '\n')
1998 newline = strchr(logmsg, '\n');
2001 limit = avail - col;
2002 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2003 limit--; /* for the border */
2004 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2008 waddwstr(view->window, &wlogmsg[scrollx]);
2009 col += MAX(logmsg_width, 0);
2010 while (col < avail) {
2011 waddch(view->window, ' ');
2023 static struct commit_queue_entry *
2024 alloc_commit_queue_entry(struct got_commit_object *commit,
2025 struct got_object_id *id)
2027 struct commit_queue_entry *entry;
2029 entry = calloc(1, sizeof(*entry));
2034 entry->commit = commit;
2039 pop_commit(struct commit_queue *commits)
2041 struct commit_queue_entry *entry;
2043 entry = TAILQ_FIRST(&commits->head);
2044 TAILQ_REMOVE(&commits->head, entry, entry);
2045 got_object_commit_close(entry->commit);
2046 commits->ncommits--;
2047 /* Don't free entry->id! It is owned by the commit graph. */
2052 free_commits(struct commit_queue *commits)
2054 while (!TAILQ_EMPTY(&commits->head))
2055 pop_commit(commits);
2058 static const struct got_error *
2059 match_commit(int *have_match, struct got_object_id *id,
2060 struct got_commit_object *commit, regex_t *regex)
2062 const struct got_error *err = NULL;
2063 regmatch_t regmatch;
2064 char *id_str = NULL, *logmsg = NULL;
2068 err = got_object_id_str(&id_str, id);
2072 err = got_object_commit_get_logmsg(&logmsg, commit);
2076 if (regexec(regex, got_object_commit_get_author(commit), 1,
2077 ®match, 0) == 0 ||
2078 regexec(regex, got_object_commit_get_committer(commit), 1,
2079 ®match, 0) == 0 ||
2080 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2081 regexec(regex, logmsg, 1, ®match, 0) == 0)
2089 static const struct got_error *
2090 queue_commits(struct tog_log_thread_args *a)
2092 const struct got_error *err = NULL;
2095 * We keep all commits open throughout the lifetime of the log
2096 * view in order to avoid having to re-fetch commits from disk
2097 * while updating the display.
2100 struct got_object_id *id;
2101 struct got_commit_object *commit;
2102 struct commit_queue_entry *entry;
2105 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2107 if (err || id == NULL)
2110 err = got_object_open_as_commit(&commit, a->repo, id);
2113 entry = alloc_commit_queue_entry(commit, id);
2114 if (entry == NULL) {
2115 err = got_error_from_errno("alloc_commit_queue_entry");
2119 errcode = pthread_mutex_lock(&tog_mutex);
2121 err = got_error_set_errno(errcode,
2122 "pthread_mutex_lock");
2126 entry->idx = a->commits->ncommits;
2127 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2128 a->commits->ncommits++;
2130 if (*a->searching == TOG_SEARCH_FORWARD &&
2131 !*a->search_next_done) {
2133 err = match_commit(&have_match, id, commit, a->regex);
2137 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2140 errcode = pthread_mutex_unlock(&tog_mutex);
2141 if (errcode && err == NULL)
2142 err = got_error_set_errno(errcode,
2143 "pthread_mutex_unlock");
2146 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2152 select_commit(struct tog_log_view_state *s)
2154 struct commit_queue_entry *entry;
2157 entry = s->first_displayed_entry;
2159 if (ncommits == s->selected) {
2160 s->selected_entry = entry;
2163 entry = TAILQ_NEXT(entry, entry);
2168 static const struct got_error *
2169 draw_commits(struct tog_view *view)
2171 const struct got_error *err = NULL;
2172 struct tog_log_view_state *s = &view->state.log;
2173 struct commit_queue_entry *entry = s->selected_entry;
2174 const int limit = view->nlines;
2176 int ncommits, author_cols = 4;
2177 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2178 char *refs_str = NULL;
2180 struct tog_color *tc;
2181 static const size_t date_display_cols = 12;
2183 if (s->selected_entry &&
2184 !(view->searching && view->search_next_done == 0)) {
2185 struct got_reflist_head *refs;
2186 err = got_object_id_str(&id_str, s->selected_entry->id);
2189 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2190 s->selected_entry->id);
2192 err = build_refs_str(&refs_str, refs,
2193 s->selected_entry->id, s->repo);
2199 if (s->thread_args.commits_needed == 0)
2200 halfdelay(10); /* disable fast refresh */
2202 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2203 if (asprintf(&ncommits_str, " [%d/%d] %s",
2204 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2205 (view->searching && !view->search_next_done) ?
2206 "searching..." : "loading...") == -1) {
2207 err = got_error_from_errno("asprintf");
2211 const char *search_str = NULL;
2213 if (view->searching) {
2214 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2215 search_str = "no more matches";
2216 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2217 search_str = "no matches found";
2218 else if (!view->search_next_done)
2219 search_str = "searching...";
2222 if (asprintf(&ncommits_str, " [%d/%d] %s",
2223 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2224 search_str ? search_str :
2225 (refs_str ? refs_str : "")) == -1) {
2226 err = got_error_from_errno("asprintf");
2231 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2232 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2233 "........................................",
2234 s->in_repo_path, ncommits_str) == -1) {
2235 err = got_error_from_errno("asprintf");
2239 } else if (asprintf(&header, "commit %s%s",
2240 id_str ? id_str : "........................................",
2241 ncommits_str) == -1) {
2242 err = got_error_from_errno("asprintf");
2246 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2250 werase(view->window);
2252 if (view_needs_focus_indication(view))
2253 wstandout(view->window);
2254 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2256 wattr_on(view->window,
2257 COLOR_PAIR(tc->colorpair), NULL);
2258 waddwstr(view->window, wline);
2260 wattr_off(view->window,
2261 COLOR_PAIR(tc->colorpair), NULL);
2262 while (width < view->ncols) {
2263 waddch(view->window, ' ');
2266 if (view_needs_focus_indication(view))
2267 wstandend(view->window);
2272 /* Grow author column size if necessary, and set view->maxx. */
2273 entry = s->first_displayed_entry;
2277 char *author, *eol, *msg, *msg0;
2278 wchar_t *wauthor, *wmsg;
2280 if (ncommits >= limit - 1)
2282 author = strdup(got_object_commit_get_author(entry->commit));
2283 if (author == NULL) {
2284 err = got_error_from_errno("strdup");
2287 err = format_author(&wauthor, &width, author, COLS,
2289 if (author_cols < width)
2290 author_cols = width;
2293 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2297 while (*msg == '\n')
2299 if ((eol = strchr(msg, '\n')))
2301 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2302 date_display_cols + author_cols, 0);
2305 view->maxx = MAX(view->maxx, width);
2309 entry = TAILQ_NEXT(entry, entry);
2312 entry = s->first_displayed_entry;
2313 s->last_displayed_entry = s->first_displayed_entry;
2316 if (ncommits >= limit - 1)
2318 if (ncommits == s->selected)
2319 wstandout(view->window);
2320 err = draw_commit(view, entry->commit, entry->id,
2321 date_display_cols, author_cols);
2322 if (ncommits == s->selected)
2323 wstandend(view->window);
2327 s->last_displayed_entry = entry;
2328 entry = TAILQ_NEXT(entry, entry);
2341 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2343 struct commit_queue_entry *entry;
2346 entry = TAILQ_FIRST(&s->commits.head);
2347 if (s->first_displayed_entry == entry)
2350 entry = s->first_displayed_entry;
2351 while (entry && nscrolled < maxscroll) {
2352 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2354 s->first_displayed_entry = entry;
2360 static const struct got_error *
2361 trigger_log_thread(struct tog_view *view, int wait)
2363 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2366 halfdelay(1); /* fast refresh while loading commits */
2368 while (!ta->log_complete && !tog_thread_error &&
2369 (ta->commits_needed > 0 || ta->load_all)) {
2370 /* Wake the log thread. */
2371 errcode = pthread_cond_signal(&ta->need_commits);
2373 return got_error_set_errno(errcode,
2374 "pthread_cond_signal");
2377 * The mutex will be released while the view loop waits
2378 * in wgetch(), at which time the log thread will run.
2383 /* Display progress update in log view. */
2384 show_log_view(view);
2388 /* Wait right here while next commit is being loaded. */
2389 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2391 return got_error_set_errno(errcode,
2392 "pthread_cond_wait");
2394 /* Display progress update in log view. */
2395 show_log_view(view);
2403 static const struct got_error *
2404 request_log_commits(struct tog_view *view)
2406 struct tog_log_view_state *state = &view->state.log;
2407 const struct got_error *err = NULL;
2409 state->thread_args.commits_needed += view->nscrolled;
2410 err = trigger_log_thread(view, 1);
2411 view->nscrolled = 0;
2416 static const struct got_error *
2417 log_scroll_down(struct tog_view *view, int maxscroll)
2419 struct tog_log_view_state *s = &view->state.log;
2420 const struct got_error *err = NULL;
2421 struct commit_queue_entry *pentry;
2422 int nscrolled = 0, ncommits_needed;
2424 if (s->last_displayed_entry == NULL)
2427 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2428 if (s->commits.ncommits < ncommits_needed &&
2429 !s->thread_args.log_complete) {
2431 * Ask the log thread for required amount of commits.
2433 s->thread_args.commits_needed += maxscroll;
2434 err = trigger_log_thread(view, 1);
2440 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2441 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2444 s->last_displayed_entry = pentry ?
2445 pentry : s->last_displayed_entry;;
2447 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2450 s->first_displayed_entry = pentry;
2451 } while (++nscrolled < maxscroll);
2453 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2454 view->nscrolled += nscrolled;
2456 view->nscrolled = 0;
2461 static const struct got_error *
2462 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2463 struct got_commit_object *commit, struct got_object_id *commit_id,
2464 struct tog_view *log_view, struct got_repository *repo)
2466 const struct got_error *err;
2467 struct got_object_qid *parent_id;
2468 struct tog_view *diff_view;
2470 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2471 if (diff_view == NULL)
2472 return got_error_from_errno("view_open");
2474 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2475 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2476 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2478 *new_view = diff_view;
2482 static const struct got_error *
2483 tree_view_visit_subtree(struct tog_tree_view_state *s,
2484 struct got_tree_object *subtree)
2486 struct tog_parent_tree *parent;
2488 parent = calloc(1, sizeof(*parent));
2490 return got_error_from_errno("calloc");
2492 parent->tree = s->tree;
2493 parent->first_displayed_entry = s->first_displayed_entry;
2494 parent->selected_entry = s->selected_entry;
2495 parent->selected = s->selected;
2496 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2499 s->first_displayed_entry = NULL;
2503 static const struct got_error *
2504 tree_view_walk_path(struct tog_tree_view_state *s,
2505 struct got_commit_object *commit, const char *path)
2507 const struct got_error *err = NULL;
2508 struct got_tree_object *tree = NULL;
2510 char *slash, *subpath = NULL;
2512 /* Walk the path and open corresponding tree objects. */
2515 struct got_tree_entry *te;
2516 struct got_object_id *tree_id;
2522 /* Ensure the correct subtree entry is selected. */
2523 slash = strchr(p, '/');
2525 te_name = strdup(p);
2527 te_name = strndup(p, slash - p);
2528 if (te_name == NULL) {
2529 err = got_error_from_errno("strndup");
2532 te = got_object_tree_find_entry(s->tree, te_name);
2534 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2539 s->first_displayed_entry = s->selected_entry = te;
2541 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2542 break; /* jump to this file's entry */
2544 slash = strchr(p, '/');
2546 subpath = strndup(path, slash - path);
2548 subpath = strdup(path);
2549 if (subpath == NULL) {
2550 err = got_error_from_errno("strdup");
2554 err = got_object_id_by_path(&tree_id, s->repo, commit,
2559 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2564 err = tree_view_visit_subtree(s, tree);
2566 got_object_tree_close(tree);
2580 static const struct got_error *
2581 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2582 struct commit_queue_entry *entry, const char *path,
2583 const char *head_ref_name, struct got_repository *repo)
2585 const struct got_error *err = NULL;
2586 struct tog_tree_view_state *s;
2587 struct tog_view *tree_view;
2589 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2590 if (tree_view == NULL)
2591 return got_error_from_errno("view_open");
2593 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2596 s = &tree_view->state.tree;
2598 *new_view = tree_view;
2600 if (got_path_is_root_dir(path))
2603 return tree_view_walk_path(s, entry->commit, path);
2606 static const struct got_error *
2607 block_signals_used_by_main_thread(void)
2612 if (sigemptyset(&sigset) == -1)
2613 return got_error_from_errno("sigemptyset");
2615 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2616 if (sigaddset(&sigset, SIGWINCH) == -1)
2617 return got_error_from_errno("sigaddset");
2618 if (sigaddset(&sigset, SIGCONT) == -1)
2619 return got_error_from_errno("sigaddset");
2620 if (sigaddset(&sigset, SIGINT) == -1)
2621 return got_error_from_errno("sigaddset");
2622 if (sigaddset(&sigset, SIGTERM) == -1)
2623 return got_error_from_errno("sigaddset");
2625 /* ncurses handles SIGTSTP */
2626 if (sigaddset(&sigset, SIGTSTP) == -1)
2627 return got_error_from_errno("sigaddset");
2629 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2631 return got_error_set_errno(errcode, "pthread_sigmask");
2637 log_thread(void *arg)
2639 const struct got_error *err = NULL;
2641 struct tog_log_thread_args *a = arg;
2645 * Sync startup with main thread such that we begin our
2646 * work once view_input() has released the mutex.
2648 errcode = pthread_mutex_lock(&tog_mutex);
2650 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2654 err = block_signals_used_by_main_thread();
2656 pthread_mutex_unlock(&tog_mutex);
2660 while (!done && !err && !tog_fatal_signal_received()) {
2661 errcode = pthread_mutex_unlock(&tog_mutex);
2663 err = got_error_set_errno(errcode,
2664 "pthread_mutex_unlock");
2667 err = queue_commits(a);
2669 if (err->code != GOT_ERR_ITER_COMPLETED)
2673 } else if (a->commits_needed > 0 && !a->load_all)
2674 a->commits_needed--;
2676 errcode = pthread_mutex_lock(&tog_mutex);
2678 err = got_error_set_errno(errcode,
2679 "pthread_mutex_lock");
2681 } else if (*a->quit)
2683 else if (*a->first_displayed_entry == NULL) {
2684 *a->first_displayed_entry =
2685 TAILQ_FIRST(&a->commits->head);
2686 *a->selected_entry = *a->first_displayed_entry;
2689 errcode = pthread_cond_signal(&a->commit_loaded);
2691 err = got_error_set_errno(errcode,
2692 "pthread_cond_signal");
2693 pthread_mutex_unlock(&tog_mutex);
2698 a->commits_needed = 0;
2700 if (a->commits_needed == 0 && !a->load_all) {
2701 errcode = pthread_cond_wait(&a->need_commits,
2704 err = got_error_set_errno(errcode,
2705 "pthread_cond_wait");
2706 pthread_mutex_unlock(&tog_mutex);
2714 a->log_complete = 1;
2715 errcode = pthread_mutex_unlock(&tog_mutex);
2717 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2720 tog_thread_error = 1;
2721 pthread_cond_signal(&a->commit_loaded);
2726 static const struct got_error *
2727 stop_log_thread(struct tog_log_view_state *s)
2729 const struct got_error *err = NULL, *thread_err = NULL;
2734 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2736 return got_error_set_errno(errcode,
2737 "pthread_cond_signal");
2738 errcode = pthread_mutex_unlock(&tog_mutex);
2740 return got_error_set_errno(errcode,
2741 "pthread_mutex_unlock");
2742 errcode = pthread_join(s->thread, (void **)&thread_err);
2744 return got_error_set_errno(errcode, "pthread_join");
2745 errcode = pthread_mutex_lock(&tog_mutex);
2747 return got_error_set_errno(errcode,
2748 "pthread_mutex_lock");
2749 s->thread = 0; //NULL;
2752 if (s->thread_args.repo) {
2753 err = got_repo_close(s->thread_args.repo);
2754 s->thread_args.repo = NULL;
2757 if (s->thread_args.pack_fds) {
2758 const struct got_error *pack_err =
2759 got_repo_pack_fds_close(s->thread_args.pack_fds);
2762 s->thread_args.pack_fds = NULL;
2765 if (s->thread_args.graph) {
2766 got_commit_graph_close(s->thread_args.graph);
2767 s->thread_args.graph = NULL;
2770 return err ? err : thread_err;
2773 static const struct got_error *
2774 close_log_view(struct tog_view *view)
2776 const struct got_error *err = NULL;
2777 struct tog_log_view_state *s = &view->state.log;
2780 err = stop_log_thread(s);
2782 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2783 if (errcode && err == NULL)
2784 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2786 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2787 if (errcode && err == NULL)
2788 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2790 free_commits(&s->commits);
2791 free(s->in_repo_path);
2792 s->in_repo_path = NULL;
2795 free(s->head_ref_name);
2796 s->head_ref_name = NULL;
2800 static const struct got_error *
2801 search_start_log_view(struct tog_view *view)
2803 struct tog_log_view_state *s = &view->state.log;
2805 s->matched_entry = NULL;
2806 s->search_entry = NULL;
2810 static const struct got_error *
2811 search_next_log_view(struct tog_view *view)
2813 const struct got_error *err = NULL;
2814 struct tog_log_view_state *s = &view->state.log;
2815 struct commit_queue_entry *entry;
2817 /* Display progress update in log view. */
2818 show_log_view(view);
2822 if (s->search_entry) {
2824 errcode = pthread_mutex_unlock(&tog_mutex);
2826 return got_error_set_errno(errcode,
2827 "pthread_mutex_unlock");
2828 ch = wgetch(view->window);
2829 errcode = pthread_mutex_lock(&tog_mutex);
2831 return got_error_set_errno(errcode,
2832 "pthread_mutex_lock");
2833 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2834 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2837 if (view->searching == TOG_SEARCH_FORWARD)
2838 entry = TAILQ_NEXT(s->search_entry, entry);
2840 entry = TAILQ_PREV(s->search_entry,
2841 commit_queue_head, entry);
2842 } else if (s->matched_entry) {
2843 int matched_idx = s->matched_entry->idx;
2844 int selected_idx = s->selected_entry->idx;
2847 * If the user has moved the cursor after we hit a match,
2848 * the position from where we should continue searching
2849 * might have changed.
2851 if (view->searching == TOG_SEARCH_FORWARD) {
2852 if (matched_idx > selected_idx)
2853 entry = TAILQ_NEXT(s->selected_entry, entry);
2855 entry = TAILQ_NEXT(s->matched_entry, entry);
2857 if (matched_idx < selected_idx)
2858 entry = TAILQ_PREV(s->selected_entry,
2859 commit_queue_head, entry);
2861 entry = TAILQ_PREV(s->matched_entry,
2862 commit_queue_head, entry);
2865 entry = s->selected_entry;
2871 if (entry == NULL) {
2872 if (s->thread_args.log_complete ||
2873 view->searching == TOG_SEARCH_BACKWARD) {
2874 view->search_next_done =
2875 (s->matched_entry == NULL ?
2876 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2877 s->search_entry = NULL;
2881 * Poke the log thread for more commits and return,
2882 * allowing the main loop to make progress. Search
2883 * will resume at s->search_entry once we come back.
2885 s->thread_args.commits_needed++;
2886 return trigger_log_thread(view, 0);
2889 err = match_commit(&have_match, entry->id, entry->commit,
2894 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2895 s->matched_entry = entry;
2899 s->search_entry = entry;
2900 if (view->searching == TOG_SEARCH_FORWARD)
2901 entry = TAILQ_NEXT(entry, entry);
2903 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2906 if (s->matched_entry) {
2907 int cur = s->selected_entry->idx;
2908 while (cur < s->matched_entry->idx) {
2909 err = input_log_view(NULL, view, KEY_DOWN);
2914 while (cur > s->matched_entry->idx) {
2915 err = input_log_view(NULL, view, KEY_UP);
2922 s->search_entry = NULL;
2927 static const struct got_error *
2928 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2929 struct got_repository *repo, const char *head_ref_name,
2930 const char *in_repo_path, int log_branches)
2932 const struct got_error *err = NULL;
2933 struct tog_log_view_state *s = &view->state.log;
2934 struct got_repository *thread_repo = NULL;
2935 struct got_commit_graph *thread_graph = NULL;
2938 if (in_repo_path != s->in_repo_path) {
2939 free(s->in_repo_path);
2940 s->in_repo_path = strdup(in_repo_path);
2941 if (s->in_repo_path == NULL)
2942 return got_error_from_errno("strdup");
2945 /* The commit queue only contains commits being displayed. */
2946 TAILQ_INIT(&s->commits.head);
2947 s->commits.ncommits = 0;
2950 if (head_ref_name) {
2951 s->head_ref_name = strdup(head_ref_name);
2952 if (s->head_ref_name == NULL) {
2953 err = got_error_from_errno("strdup");
2957 s->start_id = got_object_id_dup(start_id);
2958 if (s->start_id == NULL) {
2959 err = got_error_from_errno("got_object_id_dup");
2962 s->log_branches = log_branches;
2964 STAILQ_INIT(&s->colors);
2965 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2966 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2967 get_color_value("TOG_COLOR_COMMIT"));
2970 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2971 get_color_value("TOG_COLOR_AUTHOR"));
2973 free_colors(&s->colors);
2976 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2977 get_color_value("TOG_COLOR_DATE"));
2979 free_colors(&s->colors);
2984 view->show = show_log_view;
2985 view->input = input_log_view;
2986 view->close = close_log_view;
2987 view->search_start = search_start_log_view;
2988 view->search_next = search_next_log_view;
2990 if (s->thread_args.pack_fds == NULL) {
2991 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2995 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2996 s->thread_args.pack_fds);
2999 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3003 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3004 s->repo, NULL, NULL);
3008 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3010 err = got_error_set_errno(errcode, "pthread_cond_init");
3013 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3015 err = got_error_set_errno(errcode, "pthread_cond_init");
3019 s->thread_args.commits_needed = view->nlines;
3020 s->thread_args.graph = thread_graph;
3021 s->thread_args.commits = &s->commits;
3022 s->thread_args.in_repo_path = s->in_repo_path;
3023 s->thread_args.start_id = s->start_id;
3024 s->thread_args.repo = thread_repo;
3025 s->thread_args.log_complete = 0;
3026 s->thread_args.quit = &s->quit;
3027 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3028 s->thread_args.selected_entry = &s->selected_entry;
3029 s->thread_args.searching = &view->searching;
3030 s->thread_args.search_next_done = &view->search_next_done;
3031 s->thread_args.regex = &view->regex;
3034 close_log_view(view);
3038 static const struct got_error *
3039 show_log_view(struct tog_view *view)
3041 const struct got_error *err;
3042 struct tog_log_view_state *s = &view->state.log;
3044 if (s->thread == 0) { //NULL) {
3045 int errcode = pthread_create(&s->thread, NULL, log_thread,
3048 return got_error_set_errno(errcode, "pthread_create");
3049 if (s->thread_args.commits_needed > 0) {
3050 err = trigger_log_thread(view, 1);
3056 return draw_commits(view);
3060 log_move_cursor_up(struct tog_view *view, int page, int home)
3062 struct tog_log_view_state *s = &view->state.log;
3064 if (s->selected_entry->idx == 0)
3066 if (s->first_displayed_entry == NULL)
3069 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3071 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3073 if (!page && !home && s->selected > 0)
3076 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3082 static const struct got_error *
3083 log_move_cursor_down(struct tog_view *view, int page)
3085 struct tog_log_view_state *s = &view->state.log;
3086 struct commit_queue_entry *first;
3087 const struct got_error *err = NULL;
3089 first = s->first_displayed_entry;
3090 if (first == NULL) {
3095 if (s->thread_args.log_complete &&
3096 s->selected_entry->idx >= s->commits.ncommits - 1)
3100 int eos = view->nlines - 2;
3102 if (view_is_hsplit_top(view))
3103 --eos; /* border consumes the last line */
3104 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3107 err = log_scroll_down(view, 1);
3108 } else if (s->thread_args.load_all) {
3109 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3110 s->selected += MIN(s->last_displayed_entry->idx -
3111 s->selected_entry->idx, page + 1);
3113 err = log_scroll_down(view, MIN(page,
3114 s->commits.ncommits - s->selected_entry->idx - 1));
3115 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3117 err = log_scroll_down(view, page);
3120 if (first == s->first_displayed_entry && s->selected <
3121 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3122 s->selected = MIN(s->commits.ncommits - 1, page);
3129 * We might necessarily overshoot in horizontal
3130 * splits; if so, select the last displayed commit.
3132 s->selected = MIN(s->selected,
3133 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3137 if (s->thread_args.log_complete &&
3138 s->selected_entry->idx == s->commits.ncommits - 1)
3145 view_get_split(struct tog_view *view, int *y, int *x)
3150 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3151 if (view->child && view->child->resized_y)
3152 *y = view->child->resized_y;
3153 else if (view->resized_y)
3154 *y = view->resized_y;
3156 *y = view_split_begin_y(view->lines);
3157 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3158 if (view->child && view->child->resized_x)
3159 *x = view->child->resized_x;
3160 else if (view->resized_x)
3161 *x = view->resized_x;
3163 *x = view_split_begin_x(view->begin_x);
3167 /* Split view horizontally at y and offset view->state->selected line. */
3168 static const struct got_error *
3169 view_init_hsplit(struct tog_view *view, int y)
3171 const struct got_error *err = NULL;
3175 err = view_resize(view);
3179 err = offset_selection_down(view);
3184 static const struct got_error *
3185 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3187 const struct got_error *err = NULL;
3188 struct tog_log_view_state *s = &view->state.log;
3189 struct tog_view *diff_view = NULL, *tree_view = NULL;
3190 struct tog_view *ref_view = NULL;
3191 struct commit_queue_entry *entry;
3192 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3194 if (s->thread_args.load_all) {
3195 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3196 s->thread_args.load_all = 0;
3197 else if (s->thread_args.log_complete) {
3198 err = log_move_cursor_down(view, s->commits.ncommits);
3199 s->thread_args.load_all = 0;
3204 eos = nscroll = view->nlines - 1;
3205 if (view_is_hsplit_top(view))
3216 view->x = MAX(view->maxx - view->ncols / 2, 0);
3221 if (view->x + view->ncols / 2 < view->maxx)
3222 view->x += 2; /* move two columns right */
3228 view->x -= MIN(view->x, 2); /* move two columns back */
3237 log_move_cursor_up(view, 0, 0);
3241 log_move_cursor_up(view, 0, 1);
3251 log_move_cursor_up(view, nscroll, 0);
3258 err = log_move_cursor_down(view, 0);
3262 /* We don't know yet how many commits, so we're forced to
3263 * traverse them all. */
3265 if (!s->thread_args.log_complete) {
3266 s->thread_args.load_all = 1;
3267 return trigger_log_thread(view, 0);
3271 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3272 for (n = 0; n < eos; n++) {
3275 s->first_displayed_entry = entry;
3276 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3279 s->selected = n - 1;
3291 err = log_move_cursor_down(view, nscroll);
3294 if (s->selected > view->nlines - 2)
3295 s->selected = view->nlines - 2;
3296 if (s->selected > s->commits.ncommits - 1)
3297 s->selected = s->commits.ncommits - 1;
3299 if (s->commits.ncommits < view->nlines - 1 &&
3300 !s->thread_args.log_complete) {
3301 s->thread_args.commits_needed += (view->nlines - 1) -
3302 s->commits.ncommits;
3303 err = trigger_log_thread(view, 1);
3309 if (s->selected_entry == NULL)
3312 /* get dimensions--don't split till initialisation succeeds */
3313 if (view_is_parent_view(view))
3314 view_get_split(view, &begin_y, &begin_x);
3316 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3317 s->selected_entry->commit, s->selected_entry->id,
3322 if (view_is_parent_view(view) &&
3323 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3324 err = view_init_hsplit(view, begin_y);
3330 diff_view->focussed = 1;
3331 diff_view->mode = view->mode;
3332 diff_view->nlines = view->lines - begin_y;
3334 if (view_is_parent_view(view)) {
3335 view_transfer_size(diff_view, view);
3336 err = view_close_child(view);
3339 err = view_set_child(view, diff_view);
3342 view->focus_child = 1;
3344 *new_view = diff_view;
3348 if (s->selected_entry == NULL)
3350 if (view_is_parent_view(view))
3351 view_get_split(view, &begin_y, &begin_x);
3352 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3353 s->selected_entry, s->in_repo_path, s->head_ref_name,
3357 if (view_is_parent_view(view) &&
3358 view->mode == TOG_VIEW_SPLIT_HRZN) {
3359 err = view_init_hsplit(view, begin_y);
3364 tree_view->focussed = 1;
3365 tree_view->mode = view->mode;
3366 tree_view->nlines = view->lines - begin_y;
3367 if (view_is_parent_view(view)) {
3368 view_transfer_size(tree_view, view);
3369 err = view_close_child(view);
3372 err = view_set_child(view, tree_view);
3375 view->focus_child = 1;
3377 *new_view = tree_view;
3383 if (ch == KEY_BACKSPACE &&
3384 got_path_is_root_dir(s->in_repo_path))
3386 err = stop_log_thread(s);
3389 if (ch == KEY_BACKSPACE) {
3391 err = got_path_dirname(&parent_path, s->in_repo_path);
3394 free(s->in_repo_path);
3395 s->in_repo_path = parent_path;
3396 s->thread_args.in_repo_path = s->in_repo_path;
3397 } else if (ch == CTRL('l')) {
3398 struct got_object_id *start_id;
3399 err = got_repo_match_object_id(&start_id, NULL,
3400 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3401 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3405 s->start_id = start_id;
3406 s->thread_args.start_id = s->start_id;
3408 s->log_branches = !s->log_branches;
3410 if (s->thread_args.pack_fds == NULL) {
3411 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3415 err = got_repo_open(&s->thread_args.repo,
3416 got_repo_get_path(s->repo), NULL,
3417 s->thread_args.pack_fds);
3421 err = tog_load_refs(s->repo, 0);
3424 err = got_commit_graph_open(&s->thread_args.graph,
3425 s->in_repo_path, !s->log_branches);
3428 err = got_commit_graph_iter_start(s->thread_args.graph,
3429 s->start_id, s->repo, NULL, NULL);
3432 free_commits(&s->commits);
3433 s->first_displayed_entry = NULL;
3434 s->last_displayed_entry = NULL;
3435 s->selected_entry = NULL;
3437 s->thread_args.log_complete = 0;
3439 s->thread_args.commits_needed = view->lines;
3440 s->matched_entry = NULL;
3441 s->search_entry = NULL;
3445 if (view_is_parent_view(view))
3446 view_get_split(view, &begin_y, &begin_x);
3447 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3448 if (ref_view == NULL)
3449 return got_error_from_errno("view_open");
3450 err = open_ref_view(ref_view, s->repo);
3452 view_close(ref_view);
3455 if (view_is_parent_view(view) &&
3456 view->mode == TOG_VIEW_SPLIT_HRZN) {
3457 err = view_init_hsplit(view, begin_y);
3462 ref_view->focussed = 1;
3463 ref_view->mode = view->mode;
3464 ref_view->nlines = view->lines - begin_y;
3465 if (view_is_parent_view(view)) {
3466 view_transfer_size(ref_view, view);
3467 err = view_close_child(view);
3470 err = view_set_child(view, ref_view);
3473 view->focus_child = 1;
3475 *new_view = ref_view;
3485 static const struct got_error *
3486 apply_unveil(const char *repo_path, const char *worktree_path)
3488 const struct got_error *error;
3491 if (unveil("gmon.out", "rwc") != 0)
3492 return got_error_from_errno2("unveil", "gmon.out");
3494 if (repo_path && unveil(repo_path, "r") != 0)
3495 return got_error_from_errno2("unveil", repo_path);
3497 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3498 return got_error_from_errno2("unveil", worktree_path);
3500 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3501 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3503 error = got_privsep_unveil_exec_helpers();
3507 if (unveil(NULL, NULL) != 0)
3508 return got_error_from_errno("unveil");
3517 * Override default signal handlers before starting ncurses.
3518 * This should prevent ncurses from installing its own
3519 * broken cleanup() signal handler.
3521 signal(SIGWINCH, tog_sigwinch);
3522 signal(SIGPIPE, tog_sigpipe);
3523 signal(SIGCONT, tog_sigcont);
3524 signal(SIGINT, tog_sigint);
3525 signal(SIGTERM, tog_sigterm);
3529 halfdelay(1); /* Do fast refresh while initial view is loading. */
3532 intrflush(stdscr, FALSE);
3533 keypad(stdscr, TRUE);
3535 if (getenv("TOG_COLORS") != NULL) {
3537 use_default_colors();
3541 static const struct got_error *
3542 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3543 struct got_repository *repo, struct got_worktree *worktree)
3545 const struct got_error *err = NULL;
3548 *in_repo_path = strdup("/");
3549 if (*in_repo_path == NULL)
3550 return got_error_from_errno("strdup");
3555 const char *prefix = got_worktree_get_path_prefix(worktree);
3558 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3561 if (asprintf(in_repo_path, "%s%s%s", prefix,
3562 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3564 err = got_error_from_errno("asprintf");
3565 *in_repo_path = NULL;
3569 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3574 static const struct got_error *
3575 cmd_log(int argc, char *argv[])
3577 const struct got_error *error;
3578 struct got_repository *repo = NULL;
3579 struct got_worktree *worktree = NULL;
3580 struct got_object_id *start_id = NULL;
3581 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3582 char *start_commit = NULL, *label = NULL;
3583 struct got_reference *ref = NULL;
3584 const char *head_ref_name = NULL;
3585 int ch, log_branches = 0;
3586 struct tog_view *view;
3587 int *pack_fds = NULL;
3589 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3595 start_commit = optarg;
3598 repo_path = realpath(optarg, NULL);
3599 if (repo_path == NULL)
3600 return got_error_from_errno2("realpath",
3615 error = got_repo_pack_fds_open(&pack_fds);
3619 if (repo_path == NULL) {
3620 cwd = getcwd(NULL, 0);
3622 return got_error_from_errno("getcwd");
3623 error = got_worktree_open(&worktree, cwd);
3624 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3628 strdup(got_worktree_get_repo_path(worktree));
3630 repo_path = strdup(cwd);
3631 if (repo_path == NULL) {
3632 error = got_error_from_errno("strdup");
3637 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3641 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3648 error = apply_unveil(got_repo_get_path(repo),
3649 worktree ? got_worktree_get_root_path(worktree) : NULL);
3653 /* already loaded by tog_log_with_path()? */
3654 if (TAILQ_EMPTY(&tog_refs)) {
3655 error = tog_load_refs(repo, 0);
3660 if (start_commit == NULL) {
3661 error = got_repo_match_object_id(&start_id, &label,
3662 worktree ? got_worktree_get_head_ref_name(worktree) :
3663 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3666 head_ref_name = label;
3668 error = got_ref_open(&ref, repo, start_commit, 0);
3670 head_ref_name = got_ref_get_name(ref);
3671 else if (error->code != GOT_ERR_NOT_REF)
3673 error = got_repo_match_object_id(&start_id, NULL,
3674 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3679 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3681 error = got_error_from_errno("view_open");
3684 error = open_log_view(view, start_id, repo, head_ref_name,
3685 in_repo_path, log_branches);
3689 /* Release work tree lock. */
3690 got_worktree_close(worktree);
3693 error = view_loop(view);
3703 const struct got_error *close_err = got_repo_close(repo);
3708 got_worktree_close(worktree);
3710 const struct got_error *pack_err =
3711 got_repo_pack_fds_close(pack_fds);
3723 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3724 "[-w] object1 object2\n", getprogname());
3729 match_line(const char *line, regex_t *regex, size_t nmatch,
3730 regmatch_t *regmatch)
3732 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3735 static struct tog_color *
3736 match_color(struct tog_colors *colors, const char *line)
3738 struct tog_color *tc = NULL;
3740 STAILQ_FOREACH(tc, colors, entry) {
3741 if (match_line(line, &tc->regex, 0, NULL))
3748 static const struct got_error *
3749 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3750 WINDOW *window, int skipcol, regmatch_t *regmatch)
3752 const struct got_error *err = NULL;
3754 wchar_t *wline = NULL;
3755 int rme, rms, n, width, scrollx;
3756 int width0 = 0, width1 = 0, width2 = 0;
3757 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3761 rms = regmatch->rm_so;
3762 rme = regmatch->rm_eo;
3764 err = expand_tab(&exstr, line);
3768 /* Split the line into 3 segments, according to match offsets. */
3769 seg0 = strndup(exstr, rms);
3771 err = got_error_from_errno("strndup");
3774 seg1 = strndup(exstr + rms, rme - rms);
3776 err = got_error_from_errno("strndup");
3779 seg2 = strdup(exstr + rme);
3781 err = got_error_from_errno("strndup");
3785 /* draw up to matched token if we haven't scrolled past it */
3786 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3790 n = MAX(width0 - skipcol, 0);
3793 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3794 wlimit, col_tab_align, 1);
3797 waddwstr(window, &wline[scrollx]);
3807 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3811 wlen = wcslen(wline);
3813 width = wcwidth(wline[i]);
3815 /* should not happen, tabs are expanded */
3816 err = got_error(GOT_ERR_RANGE);
3819 if (width0 + w + width > skipcol)
3824 /* draw (visible part of) matched token (if scrolled into it) */
3825 if (width1 - w > 0) {
3826 wattron(window, A_STANDOUT);
3827 waddwstr(window, &wline[i]);
3828 wattroff(window, A_STANDOUT);
3829 wlimit -= (width1 - w);
3830 *wtotal += (width1 - w);
3834 if (wlimit > 0) { /* draw rest of line */
3836 if (skipcol > width0 + width1) {
3837 err = format_line(&wline, &width2, &scrollx, seg2,
3838 skipcol - (width0 + width1), wlimit,
3842 waddwstr(window, &wline[scrollx]);
3844 err = format_line(&wline, &width2, NULL, seg2, 0,
3845 wlimit, col_tab_align, 1);
3848 waddwstr(window, wline);
3861 static const struct got_error *
3862 draw_file(struct tog_view *view, const char *header)
3864 struct tog_diff_view_state *s = &view->state.diff;
3865 regmatch_t *regmatch = &view->regmatch;
3866 const struct got_error *err;
3869 size_t linesize = 0;
3871 struct tog_color *tc;
3874 int max_lines = view->nlines;
3875 int nlines = s->nlines;
3878 line_offset = s->line_offsets[s->first_displayed_line - 1];
3879 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3880 return got_error_from_errno("fseek");
3882 werase(view->window);
3885 if (asprintf(&line, "[%d/%d] %s",
3886 s->first_displayed_line - 1 + s->selected_line, nlines,
3888 return got_error_from_errno("asprintf");
3889 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3895 if (view_needs_focus_indication(view))
3896 wstandout(view->window);
3897 waddwstr(view->window, wline);
3900 if (view_needs_focus_indication(view))
3901 wstandend(view->window);
3902 if (width <= view->ncols - 1)
3903 waddch(view->window, '\n');
3913 while (max_lines > 0 && nprinted < max_lines) {
3914 linelen = getline(&line, &linesize, s->f);
3915 if (linelen == -1) {
3921 return got_ferror(s->f, GOT_ERR_IO);
3924 /* Set view->maxx based on full line length. */
3925 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3931 view->maxx = MAX(view->maxx, width);
3935 tc = match_color(&s->colors, line);
3937 wattr_on(view->window,
3938 COLOR_PAIR(tc->colorpair), NULL);
3939 if (s->first_displayed_line + nprinted == s->matched_line &&
3940 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3941 err = add_matched_line(&width, line, view->ncols, 0,
3942 view->window, view->x, regmatch);
3949 err = format_line(&wline, &width, &skip, line,
3950 view->x, view->ncols, 0, view->x ? 1 : 0);
3955 waddwstr(view->window, &wline[skip]);
3960 wattr_off(view->window,
3961 COLOR_PAIR(tc->colorpair), NULL);
3962 if (width <= view->ncols - 1)
3963 waddch(view->window, '\n');
3968 s->last_displayed_line = s->first_displayed_line +
3971 s->last_displayed_line = s->first_displayed_line;
3976 while (nprinted < view->nlines) {
3977 waddch(view->window, '\n');
3981 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3987 wstandout(view->window);
3988 waddwstr(view->window, wline);
3991 wstandend(view->window);
3998 get_datestr(time_t *time, char *datebuf)
4000 struct tm mytm, *tm;
4003 tm = gmtime_r(time, &mytm);
4006 s = asctime_r(tm, datebuf);
4009 p = strchr(s, '\n');
4015 static const struct got_error *
4016 get_changed_paths(struct got_pathlist_head *paths,
4017 struct got_commit_object *commit, struct got_repository *repo)
4019 const struct got_error *err = NULL;
4020 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4021 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4022 struct got_object_qid *qid;
4024 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4026 struct got_commit_object *pcommit;
4027 err = got_object_open_as_commit(&pcommit, repo,
4032 tree_id1 = got_object_id_dup(
4033 got_object_commit_get_tree_id(pcommit));
4034 if (tree_id1 == NULL) {
4035 got_object_commit_close(pcommit);
4036 return got_error_from_errno("got_object_id_dup");
4038 got_object_commit_close(pcommit);
4043 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4048 tree_id2 = got_object_commit_get_tree_id(commit);
4049 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4053 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4054 got_diff_tree_collect_changed_paths, paths, 0);
4057 got_object_tree_close(tree1);
4059 got_object_tree_close(tree2);
4064 static const struct got_error *
4065 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4069 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4071 return got_error_from_errno("reallocarray");
4073 (*line_offsets)[*nlines] = off;
4078 static const struct got_error *
4079 write_commit_info(off_t **line_offsets, size_t *nlines,
4080 struct got_object_id *commit_id, struct got_reflist_head *refs,
4081 struct got_repository *repo, FILE *outfile)
4083 const struct got_error *err = NULL;
4084 char datebuf[26], *datestr;
4085 struct got_commit_object *commit;
4086 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4087 time_t committer_time;
4088 const char *author, *committer;
4089 char *refs_str = NULL;
4090 struct got_pathlist_head changed_paths;
4091 struct got_pathlist_entry *pe;
4095 TAILQ_INIT(&changed_paths);
4098 err = build_refs_str(&refs_str, refs, commit_id, repo);
4103 err = got_object_open_as_commit(&commit, repo, commit_id);
4107 err = got_object_id_str(&id_str, commit_id);
4109 err = got_error_from_errno("got_object_id_str");
4113 err = add_line_offset(line_offsets, nlines, 0);
4117 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4118 refs_str ? refs_str : "", refs_str ? ")" : "");
4120 err = got_error_from_errno("fprintf");
4124 err = add_line_offset(line_offsets, nlines, outoff);
4128 n = fprintf(outfile, "from: %s\n",
4129 got_object_commit_get_author(commit));
4131 err = got_error_from_errno("fprintf");
4135 err = add_line_offset(line_offsets, nlines, outoff);
4139 committer_time = got_object_commit_get_committer_time(commit);
4140 datestr = get_datestr(&committer_time, datebuf);
4142 n = fprintf(outfile, "date: %s UTC\n", datestr);
4144 err = got_error_from_errno("fprintf");
4148 err = add_line_offset(line_offsets, nlines, outoff);
4152 author = got_object_commit_get_author(commit);
4153 committer = got_object_commit_get_committer(commit);
4154 if (strcmp(author, committer) != 0) {
4155 n = fprintf(outfile, "via: %s\n", committer);
4157 err = got_error_from_errno("fprintf");
4161 err = add_line_offset(line_offsets, nlines, outoff);
4165 if (got_object_commit_get_nparents(commit) > 1) {
4166 const struct got_object_id_queue *parent_ids;
4167 struct got_object_qid *qid;
4169 parent_ids = got_object_commit_get_parent_ids(commit);
4170 STAILQ_FOREACH(qid, parent_ids, entry) {
4171 err = got_object_id_str(&id_str, &qid->id);
4174 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4176 err = got_error_from_errno("fprintf");
4180 err = add_line_offset(line_offsets, nlines, outoff);
4188 err = got_object_commit_get_logmsg(&logmsg, commit);
4192 while ((line = strsep(&s, "\n")) != NULL) {
4193 n = fprintf(outfile, "%s\n", line);
4195 err = got_error_from_errno("fprintf");
4199 err = add_line_offset(line_offsets, nlines, outoff);
4204 err = get_changed_paths(&changed_paths, commit, repo);
4207 TAILQ_FOREACH(pe, &changed_paths, entry) {
4208 struct got_diff_changed_path *cp = pe->data;
4209 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4211 err = got_error_from_errno("fprintf");
4215 err = add_line_offset(line_offsets, nlines, outoff);
4218 free((char *)pe->path);
4222 fputc('\n', outfile);
4224 err = add_line_offset(line_offsets, nlines, outoff);
4226 got_pathlist_free(&changed_paths);
4230 got_object_commit_close(commit);
4232 free(*line_offsets);
4233 *line_offsets = NULL;
4239 static const struct got_error *
4240 create_diff(struct tog_diff_view_state *s)
4242 const struct got_error *err = NULL;
4246 free(s->line_offsets);
4247 s->line_offsets = malloc(sizeof(off_t));
4248 if (s->line_offsets == NULL)
4249 return got_error_from_errno("malloc");
4254 err = got_error_from_errno("got_opentemp");
4257 if (s->f && fclose(s->f) == EOF) {
4258 err = got_error_from_errno("fclose");
4264 err = got_object_get_type(&obj_type, s->repo, s->id1);
4266 err = got_object_get_type(&obj_type, s->repo, s->id2);
4271 case GOT_OBJ_TYPE_BLOB:
4272 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4273 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4274 s->label1, s->label2, tog_diff_algo, s->diff_context,
4275 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4277 case GOT_OBJ_TYPE_TREE:
4278 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4279 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4280 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4281 s->force_text_diff, s->repo, s->f);
4283 case GOT_OBJ_TYPE_COMMIT: {
4284 const struct got_object_id_queue *parent_ids;
4285 struct got_object_qid *pid;
4286 struct got_commit_object *commit2;
4287 struct got_reflist_head *refs;
4289 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4292 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4293 /* Show commit info if we're diffing to a parent/root commit. */
4294 if (s->id1 == NULL) {
4295 err = write_commit_info(&s->line_offsets, &s->nlines,
4296 s->id2, refs, s->repo, s->f);
4300 parent_ids = got_object_commit_get_parent_ids(commit2);
4301 STAILQ_FOREACH(pid, parent_ids, entry) {
4302 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4303 err = write_commit_info(
4304 &s->line_offsets, &s->nlines,
4305 s->id2, refs, s->repo, s->f);
4312 got_object_commit_close(commit2);
4314 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4315 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4316 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4317 s->force_text_diff, s->repo, s->f);
4321 err = got_error(GOT_ERR_OBJ_TYPE);
4327 if (s->f && fflush(s->f) != 0 && err == NULL)
4328 err = got_error_from_errno("fflush");
4333 diff_view_indicate_progress(struct tog_view *view)
4335 mvwaddstr(view->window, 0, 0, "diffing...");
4340 static const struct got_error *
4341 search_start_diff_view(struct tog_view *view)
4343 struct tog_diff_view_state *s = &view->state.diff;
4345 s->matched_line = 0;
4349 static const struct got_error *
4350 search_next_diff_view(struct tog_view *view)
4352 struct tog_diff_view_state *s = &view->state.diff;
4353 const struct got_error *err = NULL;
4356 size_t linesize = 0;
4359 if (!view->searching) {
4360 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4364 if (s->matched_line) {
4365 if (view->searching == TOG_SEARCH_FORWARD)
4366 lineno = s->matched_line + 1;
4368 lineno = s->matched_line - 1;
4370 lineno = s->first_displayed_line;
4375 if (lineno <= 0 || lineno > s->nlines) {
4376 if (s->matched_line == 0) {
4377 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4381 if (view->searching == TOG_SEARCH_FORWARD)
4387 offset = s->line_offsets[lineno - 1];
4388 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4390 return got_error_from_errno("fseeko");
4392 linelen = getline(&line, &linesize, s->f);
4393 if (linelen != -1) {
4395 err = expand_tab(&exstr, line);
4398 if (match_line(exstr, &view->regex, 1,
4400 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 s->matched_line = lineno;
4407 if (view->searching == TOG_SEARCH_FORWARD)
4414 if (s->matched_line) {
4415 s->first_displayed_line = s->matched_line;
4416 s->selected_line = 1;
4422 static const struct got_error *
4423 close_diff_view(struct tog_view *view)
4425 const struct got_error *err = NULL;
4426 struct tog_diff_view_state *s = &view->state.diff;
4432 if (s->f && fclose(s->f) == EOF)
4433 err = got_error_from_errno("fclose");
4435 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4436 err = got_error_from_errno("fclose");
4438 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4439 err = got_error_from_errno("fclose");
4441 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4442 err = got_error_from_errno("close");
4444 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4445 err = got_error_from_errno("close");
4447 free_colors(&s->colors);
4448 free(s->line_offsets);
4449 s->line_offsets = NULL;
4454 static const struct got_error *
4455 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4456 struct got_object_id *id2, const char *label1, const char *label2,
4457 int diff_context, int ignore_whitespace, int force_text_diff,
4458 struct tog_view *parent_view, struct got_repository *repo)
4460 const struct got_error *err;
4461 struct tog_diff_view_state *s = &view->state.diff;
4463 memset(s, 0, sizeof(*s));
4467 if (id1 != NULL && id2 != NULL) {
4469 err = got_object_get_type(&type1, repo, id1);
4472 err = got_object_get_type(&type2, repo, id2);
4477 return got_error(GOT_ERR_OBJ_TYPE);
4479 s->first_displayed_line = 1;
4480 s->last_displayed_line = view->nlines;
4481 s->selected_line = 1;
4489 s->id1 = got_object_id_dup(id1);
4491 return got_error_from_errno("got_object_id_dup");
4495 s->id2 = got_object_id_dup(id2);
4496 if (s->id2 == NULL) {
4497 err = got_error_from_errno("got_object_id_dup");
4501 s->f1 = got_opentemp();
4502 if (s->f1 == NULL) {
4503 err = got_error_from_errno("got_opentemp");
4507 s->f2 = got_opentemp();
4508 if (s->f2 == NULL) {
4509 err = got_error_from_errno("got_opentemp");
4513 s->fd1 = got_opentempfd();
4515 err = got_error_from_errno("got_opentempfd");
4519 s->fd2 = got_opentempfd();
4521 err = got_error_from_errno("got_opentempfd");
4525 s->first_displayed_line = 1;
4526 s->last_displayed_line = view->nlines;
4527 s->diff_context = diff_context;
4528 s->ignore_whitespace = ignore_whitespace;
4529 s->force_text_diff = force_text_diff;
4530 s->parent_view = parent_view;
4533 STAILQ_INIT(&s->colors);
4534 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4535 err = add_color(&s->colors,
4536 "^-", TOG_COLOR_DIFF_MINUS,
4537 get_color_value("TOG_COLOR_DIFF_MINUS"));
4540 err = add_color(&s->colors, "^\\+",
4541 TOG_COLOR_DIFF_PLUS,
4542 get_color_value("TOG_COLOR_DIFF_PLUS"));
4545 err = add_color(&s->colors,
4546 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4547 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4551 err = add_color(&s->colors,
4552 "^(commit [0-9a-f]|parent [0-9]|"
4553 "(blob|file|tree|commit) [-+] |"
4554 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4555 get_color_value("TOG_COLOR_DIFF_META"));
4559 err = add_color(&s->colors,
4560 "^(from|via): ", TOG_COLOR_AUTHOR,
4561 get_color_value("TOG_COLOR_AUTHOR"));
4565 err = add_color(&s->colors,
4566 "^date: ", TOG_COLOR_DATE,
4567 get_color_value("TOG_COLOR_DATE"));
4572 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4573 view_is_splitscreen(view))
4574 show_log_view(parent_view); /* draw border */
4575 diff_view_indicate_progress(view);
4577 err = create_diff(s);
4579 view->show = show_diff_view;
4580 view->input = input_diff_view;
4581 view->reset = reset_diff_view;
4582 view->close = close_diff_view;
4583 view->search_start = search_start_diff_view;
4584 view->search_next = search_next_diff_view;
4587 close_diff_view(view);
4591 static const struct got_error *
4592 show_diff_view(struct tog_view *view)
4594 const struct got_error *err;
4595 struct tog_diff_view_state *s = &view->state.diff;
4596 char *id_str1 = NULL, *id_str2, *header;
4597 const char *label1, *label2;
4600 err = got_object_id_str(&id_str1, s->id1);
4603 label1 = s->label1 ? : id_str1;
4605 label1 = "/dev/null";
4607 err = got_object_id_str(&id_str2, s->id2);
4610 label2 = s->label2 ? : id_str2;
4612 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4613 err = got_error_from_errno("asprintf");
4621 err = draw_file(view, header);
4626 static const struct got_error *
4627 set_selected_commit(struct tog_diff_view_state *s,
4628 struct commit_queue_entry *entry)
4630 const struct got_error *err;
4631 const struct got_object_id_queue *parent_ids;
4632 struct got_commit_object *selected_commit;
4633 struct got_object_qid *pid;
4636 s->id2 = got_object_id_dup(entry->id);
4638 return got_error_from_errno("got_object_id_dup");
4640 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4643 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4645 pid = STAILQ_FIRST(parent_ids);
4646 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4647 got_object_commit_close(selected_commit);
4651 static const struct got_error *
4652 reset_diff_view(struct tog_view *view)
4654 struct tog_diff_view_state *s = &view->state.diff;
4657 wclear(view->window);
4658 s->first_displayed_line = 1;
4659 s->last_displayed_line = view->nlines;
4660 s->matched_line = 0;
4661 diff_view_indicate_progress(view);
4662 return create_diff(s);
4665 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4667 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4670 static const struct got_error *
4671 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4673 const struct got_error *err = NULL;
4674 struct tog_diff_view_state *s = &view->state.diff;
4675 struct tog_log_view_state *ls;
4676 struct commit_queue_entry *old_selected_entry;
4678 size_t linesize = 0;
4680 int i, nscroll = view->nlines - 1, up = 0;
4687 view->x = MAX(view->maxx - view->ncols / 3, 0);
4692 if (view->x + view->ncols / 3 < view->maxx)
4693 view->x += 2; /* move two columns right */
4699 view->x -= MIN(view->x, 2); /* move two columns back */
4706 s->force_text_diff = !s->force_text_diff;
4708 s->ignore_whitespace = !s->ignore_whitespace;
4709 err = reset_diff_view(view);
4713 s->first_displayed_line = 1;
4722 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4728 if (s->first_displayed_line > 1)
4729 s->first_displayed_line--;
4740 if (s->first_displayed_line == 1) {
4745 while (i++ < nscroll && s->first_displayed_line > 1)
4746 s->first_displayed_line--;
4752 s->first_displayed_line++;
4769 while (!s->eof && i++ < nscroll) {
4770 linelen = getline(&line, &linesize, s->f);
4771 s->first_displayed_line++;
4772 if (linelen == -1) {
4776 err = got_ferror(s->f, GOT_ERR_IO);
4783 if (s->diff_context > 0) {
4785 s->matched_line = 0;
4786 diff_view_indicate_progress(view);
4787 err = create_diff(s);
4788 if (s->first_displayed_line + view->nlines - 1 >
4790 s->first_displayed_line = 1;
4791 s->last_displayed_line = view->nlines;
4797 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4799 s->matched_line = 0;
4800 diff_view_indicate_progress(view);
4801 err = create_diff(s);
4811 if (s->parent_view == NULL) {
4815 s->parent_view->count = view->count;
4817 if (s->parent_view->type == TOG_VIEW_LOG) {
4818 ls = &s->parent_view->state.log;
4819 old_selected_entry = ls->selected_entry;
4821 err = input_log_view(NULL, s->parent_view,
4822 up ? KEY_UP : KEY_DOWN);
4825 view->count = s->parent_view->count;
4827 if (old_selected_entry == ls->selected_entry)
4830 err = set_selected_commit(s, ls->selected_entry);
4833 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4834 struct tog_blame_view_state *bs;
4835 struct got_object_id *id, *prev_id;
4837 bs = &s->parent_view->state.blame;
4838 prev_id = get_annotation_for_line(bs->blame.lines,
4839 bs->blame.nlines, bs->last_diffed_line);
4841 err = input_blame_view(&view, s->parent_view,
4842 up ? KEY_UP : KEY_DOWN);
4845 view->count = s->parent_view->count;
4847 if (prev_id == NULL)
4849 id = get_selected_commit_id(bs->blame.lines,
4850 bs->blame.nlines, bs->first_displayed_line,
4855 if (!got_object_id_cmp(prev_id, id))
4858 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4862 s->first_displayed_line = 1;
4863 s->last_displayed_line = view->nlines;
4864 s->matched_line = 0;
4867 diff_view_indicate_progress(view);
4868 err = create_diff(s);
4878 static const struct got_error *
4879 cmd_diff(int argc, char *argv[])
4881 const struct got_error *error = NULL;
4882 struct got_repository *repo = NULL;
4883 struct got_worktree *worktree = NULL;
4884 struct got_object_id *id1 = NULL, *id2 = NULL;
4885 char *repo_path = NULL, *cwd = NULL;
4886 char *id_str1 = NULL, *id_str2 = NULL;
4887 char *label1 = NULL, *label2 = NULL;
4888 int diff_context = 3, ignore_whitespace = 0;
4889 int ch, force_text_diff = 0;
4891 struct tog_view *view;
4892 int *pack_fds = NULL;
4894 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4897 force_text_diff = 1;
4900 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4903 errx(1, "number of context lines is %s: %s",
4907 repo_path = realpath(optarg, NULL);
4908 if (repo_path == NULL)
4909 return got_error_from_errno2("realpath",
4911 got_path_strip_trailing_slashes(repo_path);
4914 ignore_whitespace = 1;
4926 usage_diff(); /* TODO show local worktree changes */
4927 } else if (argc == 2) {
4933 error = got_repo_pack_fds_open(&pack_fds);
4937 if (repo_path == NULL) {
4938 cwd = getcwd(NULL, 0);
4940 return got_error_from_errno("getcwd");
4941 error = got_worktree_open(&worktree, cwd);
4942 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4946 strdup(got_worktree_get_repo_path(worktree));
4948 repo_path = strdup(cwd);
4949 if (repo_path == NULL) {
4950 error = got_error_from_errno("strdup");
4955 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4961 error = apply_unveil(got_repo_get_path(repo), NULL);
4965 error = tog_load_refs(repo, 0);
4969 error = got_repo_match_object_id(&id1, &label1, id_str1,
4970 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4974 error = got_repo_match_object_id(&id2, &label2, id_str2,
4975 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4979 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4981 error = got_error_from_errno("view_open");
4984 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4985 ignore_whitespace, force_text_diff, NULL, repo);
4988 error = view_loop(view);
4995 const struct got_error *close_err = got_repo_close(repo);
5000 got_worktree_close(worktree);
5002 const struct got_error *pack_err =
5003 got_repo_pack_fds_close(pack_fds);
5016 "usage: %s blame [-c commit] [-r repository-path] path\n",
5021 struct tog_blame_line {
5023 struct got_object_id *id;
5026 static const struct got_error *
5027 draw_blame(struct tog_view *view)
5029 struct tog_blame_view_state *s = &view->state.blame;
5030 struct tog_blame *blame = &s->blame;
5031 regmatch_t *regmatch = &view->regmatch;
5032 const struct got_error *err;
5033 int lineno = 0, nprinted = 0;
5035 size_t linesize = 0;
5039 struct tog_blame_line *blame_line;
5040 struct got_object_id *prev_id = NULL;
5042 struct tog_color *tc;
5044 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5049 werase(view->window);
5051 if (asprintf(&line, "commit %s", id_str) == -1) {
5052 err = got_error_from_errno("asprintf");
5057 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5062 if (view_needs_focus_indication(view))
5063 wstandout(view->window);
5064 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5066 wattr_on(view->window,
5067 COLOR_PAIR(tc->colorpair), NULL);
5068 waddwstr(view->window, wline);
5070 wattr_off(view->window,
5071 COLOR_PAIR(tc->colorpair), NULL);
5072 if (view_needs_focus_indication(view))
5073 wstandend(view->window);
5076 if (width < view->ncols - 1)
5077 waddch(view->window, '\n');
5079 if (asprintf(&line, "[%d/%d] %s%s",
5080 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5081 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5083 return got_error_from_errno("asprintf");
5086 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5091 waddwstr(view->window, wline);
5094 if (width < view->ncols - 1)
5095 waddch(view->window, '\n');
5099 while (nprinted < view->nlines - 2) {
5100 linelen = getline(&line, &linesize, blame->f);
5101 if (linelen == -1) {
5102 if (feof(blame->f)) {
5107 return got_ferror(blame->f, GOT_ERR_IO);
5109 if (++lineno < s->first_displayed_line)
5112 /* Set view->maxx based on full line length. */
5113 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5120 view->maxx = MAX(view->maxx, width);
5122 if (nprinted == s->selected_line - 1)
5123 wstandout(view->window);
5125 if (blame->nlines > 0) {
5126 blame_line = &blame->lines[lineno - 1];
5127 if (blame_line->annotated && prev_id &&
5128 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5129 !(nprinted == s->selected_line - 1)) {
5130 waddstr(view->window, " ");
5131 } else if (blame_line->annotated) {
5133 err = got_object_id_str(&id_str,
5139 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5141 wattr_on(view->window,
5142 COLOR_PAIR(tc->colorpair), NULL);
5143 wprintw(view->window, "%.8s", id_str);
5145 wattr_off(view->window,
5146 COLOR_PAIR(tc->colorpair), NULL);
5148 prev_id = blame_line->id;
5150 waddstr(view->window, "........");
5154 waddstr(view->window, "........");
5158 if (nprinted == s->selected_line - 1)
5159 wstandend(view->window);
5160 waddstr(view->window, " ");
5162 if (view->ncols <= 9) {
5164 } else if (s->first_displayed_line + nprinted ==
5166 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5167 err = add_matched_line(&width, line, view->ncols - 9, 9,
5168 view->window, view->x, regmatch);
5176 err = format_line(&wline, &width, &skip, line,
5177 view->x, view->ncols - 9, 9, 1);
5182 waddwstr(view->window, &wline[skip]);
5188 if (width <= view->ncols - 1)
5189 waddch(view->window, '\n');
5190 if (++nprinted == 1)
5191 s->first_displayed_line = lineno;
5194 s->last_displayed_line = lineno;
5201 static const struct got_error *
5202 blame_cb(void *arg, int nlines, int lineno,
5203 struct got_commit_object *commit, struct got_object_id *id)
5205 const struct got_error *err = NULL;
5206 struct tog_blame_cb_args *a = arg;
5207 struct tog_blame_line *line;
5210 if (nlines != a->nlines ||
5211 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5212 return got_error(GOT_ERR_RANGE);
5214 errcode = pthread_mutex_lock(&tog_mutex);
5216 return got_error_set_errno(errcode, "pthread_mutex_lock");
5218 if (*a->quit) { /* user has quit the blame view */
5219 err = got_error(GOT_ERR_ITER_COMPLETED);
5224 goto done; /* no change in this commit */
5226 line = &a->lines[lineno - 1];
5227 if (line->annotated)
5230 line->id = got_object_id_dup(id);
5231 if (line->id == NULL) {
5232 err = got_error_from_errno("got_object_id_dup");
5235 line->annotated = 1;
5237 errcode = pthread_mutex_unlock(&tog_mutex);
5239 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5244 blame_thread(void *arg)
5246 const struct got_error *err, *close_err;
5247 struct tog_blame_thread_args *ta = arg;
5248 struct tog_blame_cb_args *a = ta->cb_args;
5249 int errcode, fd1 = -1, fd2 = -1;
5250 FILE *f1 = NULL, *f2 = NULL;
5252 fd1 = got_opentempfd();
5254 return (void *)got_error_from_errno("got_opentempfd");
5256 fd2 = got_opentempfd();
5258 err = got_error_from_errno("got_opentempfd");
5262 f1 = got_opentemp();
5264 err = (void *)got_error_from_errno("got_opentemp");
5267 f2 = got_opentemp();
5269 err = (void *)got_error_from_errno("got_opentemp");
5273 err = block_signals_used_by_main_thread();
5277 err = got_blame(ta->path, a->commit_id, ta->repo,
5278 tog_diff_algo, blame_cb, ta->cb_args,
5279 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5280 if (err && err->code == GOT_ERR_CANCELLED)
5283 errcode = pthread_mutex_lock(&tog_mutex);
5285 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5289 close_err = got_repo_close(ta->repo);
5295 errcode = pthread_mutex_unlock(&tog_mutex);
5296 if (errcode && err == NULL)
5297 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5300 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5301 err = got_error_from_errno("close");
5302 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5303 err = got_error_from_errno("close");
5304 if (f1 && fclose(f1) == EOF && err == NULL)
5305 err = got_error_from_errno("fclose");
5306 if (f2 && fclose(f2) == EOF && err == NULL)
5307 err = got_error_from_errno("fclose");
5312 static struct got_object_id *
5313 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5314 int first_displayed_line, int selected_line)
5316 struct tog_blame_line *line;
5321 line = &lines[first_displayed_line - 1 + selected_line - 1];
5322 if (!line->annotated)
5328 static struct got_object_id *
5329 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5332 struct tog_blame_line *line;
5334 if (nlines <= 0 || lineno >= nlines)
5337 line = &lines[lineno - 1];
5338 if (!line->annotated)
5344 static const struct got_error *
5345 stop_blame(struct tog_blame *blame)
5347 const struct got_error *err = NULL;
5350 if (blame->thread) {
5352 errcode = pthread_mutex_unlock(&tog_mutex);
5354 return got_error_set_errno(errcode,
5355 "pthread_mutex_unlock");
5356 errcode = pthread_join(blame->thread, (void **)&err);
5358 return got_error_set_errno(errcode, "pthread_join");
5359 errcode = pthread_mutex_lock(&tog_mutex);
5361 return got_error_set_errno(errcode,
5362 "pthread_mutex_lock");
5363 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5365 blame->thread = 0; //NULL;
5367 if (blame->thread_args.repo) {
5368 const struct got_error *close_err;
5369 close_err = got_repo_close(blame->thread_args.repo);
5372 blame->thread_args.repo = NULL;
5375 if (fclose(blame->f) == EOF && err == NULL)
5376 err = got_error_from_errno("fclose");
5380 for (i = 0; i < blame->nlines; i++)
5381 free(blame->lines[i].id);
5383 blame->lines = NULL;
5385 free(blame->cb_args.commit_id);
5386 blame->cb_args.commit_id = NULL;
5387 if (blame->pack_fds) {
5388 const struct got_error *pack_err =
5389 got_repo_pack_fds_close(blame->pack_fds);
5392 blame->pack_fds = NULL;
5397 static const struct got_error *
5398 cancel_blame_view(void *arg)
5400 const struct got_error *err = NULL;
5404 errcode = pthread_mutex_lock(&tog_mutex);
5406 return got_error_set_errno(errcode,
5407 "pthread_mutex_unlock");
5410 err = got_error(GOT_ERR_CANCELLED);
5412 errcode = pthread_mutex_unlock(&tog_mutex);
5414 return got_error_set_errno(errcode,
5415 "pthread_mutex_lock");
5420 static const struct got_error *
5421 run_blame(struct tog_view *view)
5423 struct tog_blame_view_state *s = &view->state.blame;
5424 struct tog_blame *blame = &s->blame;
5425 const struct got_error *err = NULL;
5426 struct got_commit_object *commit = NULL;
5427 struct got_blob_object *blob = NULL;
5428 struct got_repository *thread_repo = NULL;
5429 struct got_object_id *obj_id = NULL;
5430 int obj_type, fd = -1;
5431 int *pack_fds = NULL;
5433 err = got_object_open_as_commit(&commit, s->repo,
5434 &s->blamed_commit->id);
5438 fd = got_opentempfd();
5440 err = got_error_from_errno("got_opentempfd");
5444 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5448 err = got_object_get_type(&obj_type, s->repo, obj_id);
5452 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5453 err = got_error(GOT_ERR_OBJ_TYPE);
5457 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5460 blame->f = got_opentemp();
5461 if (blame->f == NULL) {
5462 err = got_error_from_errno("got_opentemp");
5465 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5466 &blame->line_offsets, blame->f, blob);
5469 if (blame->nlines == 0) {
5470 s->blame_complete = 1;
5474 /* Don't include \n at EOF in the blame line count. */
5475 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5478 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5479 if (blame->lines == NULL) {
5480 err = got_error_from_errno("calloc");
5484 err = got_repo_pack_fds_open(&pack_fds);
5487 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5492 blame->pack_fds = pack_fds;
5493 blame->cb_args.view = view;
5494 blame->cb_args.lines = blame->lines;
5495 blame->cb_args.nlines = blame->nlines;
5496 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5497 if (blame->cb_args.commit_id == NULL) {
5498 err = got_error_from_errno("got_object_id_dup");
5501 blame->cb_args.quit = &s->done;
5503 blame->thread_args.path = s->path;
5504 blame->thread_args.repo = thread_repo;
5505 blame->thread_args.cb_args = &blame->cb_args;
5506 blame->thread_args.complete = &s->blame_complete;
5507 blame->thread_args.cancel_cb = cancel_blame_view;
5508 blame->thread_args.cancel_arg = &s->done;
5509 s->blame_complete = 0;
5511 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5512 s->first_displayed_line = 1;
5513 s->last_displayed_line = view->nlines;
5514 s->selected_line = 1;
5516 s->matched_line = 0;
5520 got_object_commit_close(commit);
5521 if (fd != -1 && close(fd) == -1 && err == NULL)
5522 err = got_error_from_errno("close");
5524 got_object_blob_close(blob);
5531 static const struct got_error *
5532 open_blame_view(struct tog_view *view, char *path,
5533 struct got_object_id *commit_id, struct got_repository *repo)
5535 const struct got_error *err = NULL;
5536 struct tog_blame_view_state *s = &view->state.blame;
5538 STAILQ_INIT(&s->blamed_commits);
5540 s->path = strdup(path);
5541 if (s->path == NULL)
5542 return got_error_from_errno("strdup");
5544 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5550 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5551 s->first_displayed_line = 1;
5552 s->last_displayed_line = view->nlines;
5553 s->selected_line = 1;
5554 s->blame_complete = 0;
5556 s->commit_id = commit_id;
5557 memset(&s->blame, 0, sizeof(s->blame));
5559 STAILQ_INIT(&s->colors);
5560 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5561 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5562 get_color_value("TOG_COLOR_COMMIT"));
5567 view->show = show_blame_view;
5568 view->input = input_blame_view;
5569 view->reset = reset_blame_view;
5570 view->close = close_blame_view;
5571 view->search_start = search_start_blame_view;
5572 view->search_next = search_next_blame_view;
5574 return run_blame(view);
5577 static const struct got_error *
5578 close_blame_view(struct tog_view *view)
5580 const struct got_error *err = NULL;
5581 struct tog_blame_view_state *s = &view->state.blame;
5583 if (s->blame.thread)
5584 err = stop_blame(&s->blame);
5586 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5587 struct got_object_qid *blamed_commit;
5588 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5589 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5590 got_object_qid_free(blamed_commit);
5594 free_colors(&s->colors);
5598 static const struct got_error *
5599 search_start_blame_view(struct tog_view *view)
5601 struct tog_blame_view_state *s = &view->state.blame;
5603 s->matched_line = 0;
5607 static const struct got_error *
5608 search_next_blame_view(struct tog_view *view)
5610 struct tog_blame_view_state *s = &view->state.blame;
5611 const struct got_error *err = NULL;
5614 size_t linesize = 0;
5617 if (!view->searching) {
5618 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5622 if (s->matched_line) {
5623 if (view->searching == TOG_SEARCH_FORWARD)
5624 lineno = s->matched_line + 1;
5626 lineno = s->matched_line - 1;
5628 lineno = s->first_displayed_line - 1 + s->selected_line;
5633 if (lineno <= 0 || lineno > s->blame.nlines) {
5634 if (s->matched_line == 0) {
5635 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5639 if (view->searching == TOG_SEARCH_FORWARD)
5642 lineno = s->blame.nlines;
5645 offset = s->blame.line_offsets[lineno - 1];
5646 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5648 return got_error_from_errno("fseeko");
5650 linelen = getline(&line, &linesize, s->blame.f);
5651 if (linelen != -1) {
5653 err = expand_tab(&exstr, line);
5656 if (match_line(exstr, &view->regex, 1,
5658 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5659 s->matched_line = lineno;
5665 if (view->searching == TOG_SEARCH_FORWARD)
5672 if (s->matched_line) {
5673 s->first_displayed_line = s->matched_line;
5674 s->selected_line = 1;
5680 static const struct got_error *
5681 show_blame_view(struct tog_view *view)
5683 const struct got_error *err = NULL;
5684 struct tog_blame_view_state *s = &view->state.blame;
5687 if (s->blame.thread == 0 && !s->blame_complete) {
5688 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5689 &s->blame.thread_args);
5691 return got_error_set_errno(errcode, "pthread_create");
5693 halfdelay(1); /* fast refresh while annotating */
5696 if (s->blame_complete)
5697 halfdelay(10); /* disable fast refresh */
5699 err = draw_blame(view);
5705 static const struct got_error *
5706 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5708 const struct got_error *err = NULL, *thread_err = NULL;
5709 struct tog_view *diff_view;
5710 struct tog_blame_view_state *s = &view->state.blame;
5711 int eos, nscroll, begin_y = 0, begin_x = 0;
5713 eos = nscroll = view->nlines - 2;
5714 if (view_is_hsplit_top(view))
5722 view->x = MAX(view->maxx - view->ncols / 3, 0);
5727 if (view->x + view->ncols / 3 < view->maxx)
5728 view->x += 2; /* move two columns right */
5734 view->x -= MIN(view->x, 2); /* move two columns back */
5743 s->selected_line = 1;
5744 s->first_displayed_line = 1;
5749 if (s->blame.nlines < eos) {
5750 s->selected_line = s->blame.nlines;
5751 s->first_displayed_line = 1;
5753 s->selected_line = eos;
5754 s->first_displayed_line = s->blame.nlines - (eos - 1);
5761 if (s->selected_line > 1)
5763 else if (s->selected_line == 1 &&
5764 s->first_displayed_line > 1)
5765 s->first_displayed_line--;
5776 if (s->first_displayed_line == 1) {
5777 if (view->count > 1)
5779 s->selected_line = MAX(1, s->selected_line - nscroll);
5783 if (s->first_displayed_line > nscroll)
5784 s->first_displayed_line -= nscroll;
5786 s->first_displayed_line = 1;
5791 if (s->selected_line < eos && s->first_displayed_line +
5792 s->selected_line <= s->blame.nlines)
5794 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5795 s->first_displayed_line++;
5801 struct got_object_id *id = NULL;
5804 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5805 s->first_displayed_line, s->selected_line);
5809 struct got_commit_object *commit, *pcommit;
5810 struct got_object_qid *pid;
5811 struct got_object_id *blob_id = NULL;
5813 err = got_object_open_as_commit(&commit,
5818 got_object_commit_get_parent_ids(commit));
5820 got_object_commit_close(commit);
5823 /* Check if path history ends here. */
5824 err = got_object_open_as_commit(&pcommit,
5828 err = got_object_id_by_path(&blob_id, s->repo,
5830 got_object_commit_close(pcommit);
5832 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5834 got_object_commit_close(commit);
5837 err = got_object_get_type(&obj_type, s->repo,
5840 /* Can't blame non-blob type objects. */
5841 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5842 got_object_commit_close(commit);
5845 err = got_object_qid_alloc(&s->blamed_commit,
5847 got_object_commit_close(commit);
5849 if (got_object_id_cmp(id,
5850 &s->blamed_commit->id) == 0)
5852 err = got_object_qid_alloc(&s->blamed_commit,
5858 thread_err = stop_blame(&s->blame);
5862 STAILQ_INSERT_HEAD(&s->blamed_commits,
5863 s->blamed_commit, entry);
5864 err = run_blame(view);
5870 struct got_object_qid *first;
5873 first = STAILQ_FIRST(&s->blamed_commits);
5874 if (!got_object_id_cmp(&first->id, s->commit_id))
5877 thread_err = stop_blame(&s->blame);
5881 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5882 got_object_qid_free(s->blamed_commit);
5884 STAILQ_FIRST(&s->blamed_commits);
5885 err = run_blame(view);
5892 struct got_object_id *id = NULL;
5893 struct got_object_qid *pid;
5894 struct got_commit_object *commit = NULL;
5897 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5898 s->first_displayed_line, s->selected_line);
5901 err = got_object_open_as_commit(&commit, s->repo, id);
5904 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5906 /* traversed from diff view, release diff resources */
5907 err = close_diff_view(*new_view);
5910 diff_view = *new_view;
5912 if (view_is_parent_view(view))
5913 view_get_split(view, &begin_y, &begin_x);
5915 diff_view = view_open(0, 0, begin_y, begin_x,
5917 if (diff_view == NULL) {
5918 got_object_commit_close(commit);
5919 err = got_error_from_errno("view_open");
5923 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5924 id, NULL, NULL, 3, 0, 0, view, s->repo);
5925 got_object_commit_close(commit);
5927 view_close(diff_view);
5930 s->last_diffed_line = s->first_displayed_line - 1 +
5933 break; /* still open from active diff view */
5934 if (view_is_parent_view(view) &&
5935 view->mode == TOG_VIEW_SPLIT_HRZN) {
5936 err = view_init_hsplit(view, begin_y);
5942 diff_view->focussed = 1;
5943 diff_view->mode = view->mode;
5944 diff_view->nlines = view->lines - begin_y;
5945 if (view_is_parent_view(view)) {
5946 view_transfer_size(diff_view, view);
5947 err = view_close_child(view);
5950 err = view_set_child(view, diff_view);
5953 view->focus_child = 1;
5955 *new_view = diff_view;
5968 if (s->last_displayed_line >= s->blame.nlines &&
5969 s->selected_line >= MIN(s->blame.nlines,
5970 view->nlines - 2)) {
5974 if (s->last_displayed_line >= s->blame.nlines &&
5975 s->selected_line < view->nlines - 2) {
5977 MIN(nscroll, s->last_displayed_line -
5978 s->first_displayed_line - s->selected_line + 1);
5980 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5981 s->first_displayed_line += nscroll;
5983 s->first_displayed_line =
5984 s->blame.nlines - (view->nlines - 3);
5987 if (s->selected_line > view->nlines - 2) {
5988 s->selected_line = MIN(s->blame.nlines,
5996 return thread_err ? thread_err : err;
5999 static const struct got_error *
6000 reset_blame_view(struct tog_view *view)
6002 const struct got_error *err;
6003 struct tog_blame_view_state *s = &view->state.blame;
6007 err = stop_blame(&s->blame);
6011 return run_blame(view);
6014 static const struct got_error *
6015 cmd_blame(int argc, char *argv[])
6017 const struct got_error *error;
6018 struct got_repository *repo = NULL;
6019 struct got_worktree *worktree = NULL;
6020 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6021 char *link_target = NULL;
6022 struct got_object_id *commit_id = NULL;
6023 struct got_commit_object *commit = NULL;
6024 char *commit_id_str = NULL;
6026 struct tog_view *view;
6027 int *pack_fds = NULL;
6029 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6032 commit_id_str = optarg;
6035 repo_path = realpath(optarg, NULL);
6036 if (repo_path == NULL)
6037 return got_error_from_errno2("realpath",
6052 error = got_repo_pack_fds_open(&pack_fds);
6056 if (repo_path == NULL) {
6057 cwd = getcwd(NULL, 0);
6059 return got_error_from_errno("getcwd");
6060 error = got_worktree_open(&worktree, cwd);
6061 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6065 strdup(got_worktree_get_repo_path(worktree));
6067 repo_path = strdup(cwd);
6068 if (repo_path == NULL) {
6069 error = got_error_from_errno("strdup");
6074 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6078 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6085 error = apply_unveil(got_repo_get_path(repo), NULL);
6089 error = tog_load_refs(repo, 0);
6093 if (commit_id_str == NULL) {
6094 struct got_reference *head_ref;
6095 error = got_ref_open(&head_ref, repo, worktree ?
6096 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6099 error = got_ref_resolve(&commit_id, repo, head_ref);
6100 got_ref_close(head_ref);
6102 error = got_repo_match_object_id(&commit_id, NULL,
6103 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6108 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6110 error = got_error_from_errno("view_open");
6114 error = got_object_open_as_commit(&commit, repo, commit_id);
6118 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6123 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6128 /* Release work tree lock. */
6129 got_worktree_close(worktree);
6132 error = view_loop(view);
6140 got_object_commit_close(commit);
6142 got_worktree_close(worktree);
6144 const struct got_error *close_err = got_repo_close(repo);
6149 const struct got_error *pack_err =
6150 got_repo_pack_fds_close(pack_fds);
6158 static const struct got_error *
6159 draw_tree_entries(struct tog_view *view, const char *parent_path)
6161 struct tog_tree_view_state *s = &view->state.tree;
6162 const struct got_error *err = NULL;
6163 struct got_tree_entry *te;
6165 struct tog_color *tc;
6166 int width, n, i, nentries;
6167 int limit = view->nlines;
6170 if (view_is_hsplit_top(view))
6171 --limit; /* border */
6173 werase(view->window);
6178 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6182 if (view_needs_focus_indication(view))
6183 wstandout(view->window);
6184 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6186 wattr_on(view->window,
6187 COLOR_PAIR(tc->colorpair), NULL);
6188 waddwstr(view->window, wline);
6190 wattr_off(view->window,
6191 COLOR_PAIR(tc->colorpair), NULL);
6192 if (view_needs_focus_indication(view))
6193 wstandend(view->window);
6196 if (width < view->ncols - 1)
6197 waddch(view->window, '\n');
6200 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6204 waddwstr(view->window, wline);
6207 if (width < view->ncols - 1)
6208 waddch(view->window, '\n');
6211 waddch(view->window, '\n');
6215 if (s->first_displayed_entry == NULL) {
6216 te = got_object_tree_get_first_entry(s->tree);
6217 if (s->selected == 0) {
6219 wstandout(view->window);
6220 s->selected_entry = NULL;
6222 waddstr(view->window, " ..\n"); /* parent directory */
6223 if (s->selected == 0 && view->focussed)
6224 wstandend(view->window);
6231 te = s->first_displayed_entry;
6234 nentries = got_object_tree_get_nentries(s->tree);
6235 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6236 char *line = NULL, *id_str = NULL, *link_target = NULL;
6237 const char *modestr = "";
6240 te = got_object_tree_get_entry(s->tree, i);
6241 mode = got_tree_entry_get_mode(te);
6244 err = got_object_id_str(&id_str,
6245 got_tree_entry_get_id(te));
6247 return got_error_from_errno(
6248 "got_object_id_str");
6250 if (got_object_tree_entry_is_submodule(te))
6252 else if (S_ISLNK(mode)) {
6255 err = got_tree_entry_get_symlink_target(&link_target,
6261 for (i = 0; i < strlen(link_target); i++) {
6262 if (!isprint((unsigned char)link_target[i]))
6263 link_target[i] = '?';
6267 else if (S_ISDIR(mode))
6269 else if (mode & S_IXUSR)
6271 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6272 got_tree_entry_get_name(te), modestr,
6273 link_target ? " -> ": "",
6274 link_target ? link_target : "") == -1) {
6277 return got_error_from_errno("asprintf");
6281 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6287 if (n == s->selected) {
6289 wstandout(view->window);
6290 s->selected_entry = te;
6292 tc = match_color(&s->colors, line);
6294 wattr_on(view->window,
6295 COLOR_PAIR(tc->colorpair), NULL);
6296 waddwstr(view->window, wline);
6298 wattr_off(view->window,
6299 COLOR_PAIR(tc->colorpair), NULL);
6300 if (width < view->ncols - 1)
6301 waddch(view->window, '\n');
6302 if (n == s->selected && view->focussed)
6303 wstandend(view->window);
6309 s->last_displayed_entry = te;
6318 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6320 struct got_tree_entry *te;
6321 int isroot = s->tree == s->root;
6324 if (s->first_displayed_entry == NULL)
6327 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6328 while (i++ < maxscroll) {
6331 s->first_displayed_entry = NULL;
6334 s->first_displayed_entry = te;
6335 te = got_tree_entry_get_prev(s->tree, te);
6339 static const struct got_error *
6340 tree_scroll_down(struct tog_view *view, int maxscroll)
6342 struct tog_tree_view_state *s = &view->state.tree;
6343 struct got_tree_entry *next, *last;
6346 if (s->first_displayed_entry)
6347 next = got_tree_entry_get_next(s->tree,
6348 s->first_displayed_entry);
6350 next = got_object_tree_get_first_entry(s->tree);
6352 last = s->last_displayed_entry;
6353 while (next && n++ < maxscroll) {
6355 last = got_tree_entry_get_next(s->tree, last);
6356 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6357 s->first_displayed_entry = next;
6358 next = got_tree_entry_get_next(s->tree, next);
6365 static const struct got_error *
6366 tree_entry_path(char **path, struct tog_parent_trees *parents,
6367 struct got_tree_entry *te)
6369 const struct got_error *err = NULL;
6370 struct tog_parent_tree *pt;
6371 size_t len = 2; /* for leading slash and NUL */
6373 TAILQ_FOREACH(pt, parents, entry)
6374 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6377 len += strlen(got_tree_entry_get_name(te));
6379 *path = calloc(1, len);
6381 return got_error_from_errno("calloc");
6384 pt = TAILQ_LAST(parents, tog_parent_trees);
6386 const char *name = got_tree_entry_get_name(pt->selected_entry);
6387 if (strlcat(*path, name, len) >= len) {
6388 err = got_error(GOT_ERR_NO_SPACE);
6391 if (strlcat(*path, "/", len) >= len) {
6392 err = got_error(GOT_ERR_NO_SPACE);
6395 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6398 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6399 err = got_error(GOT_ERR_NO_SPACE);
6411 static const struct got_error *
6412 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6413 struct got_tree_entry *te, struct tog_parent_trees *parents,
6414 struct got_object_id *commit_id, struct got_repository *repo)
6416 const struct got_error *err = NULL;
6418 struct tog_view *blame_view;
6422 err = tree_entry_path(&path, parents, te);
6426 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6427 if (blame_view == NULL) {
6428 err = got_error_from_errno("view_open");
6432 err = open_blame_view(blame_view, path, commit_id, repo);
6434 if (err->code == GOT_ERR_CANCELLED)
6436 view_close(blame_view);
6438 *new_view = blame_view;
6444 static const struct got_error *
6445 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6446 struct tog_tree_view_state *s)
6448 struct tog_view *log_view;
6449 const struct got_error *err = NULL;
6454 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6455 if (log_view == NULL)
6456 return got_error_from_errno("view_open");
6458 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6462 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6465 view_close(log_view);
6467 *new_view = log_view;
6472 static const struct got_error *
6473 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6474 const char *head_ref_name, struct got_repository *repo)
6476 const struct got_error *err = NULL;
6477 char *commit_id_str = NULL;
6478 struct tog_tree_view_state *s = &view->state.tree;
6479 struct got_commit_object *commit = NULL;
6481 TAILQ_INIT(&s->parents);
6482 STAILQ_INIT(&s->colors);
6484 s->commit_id = got_object_id_dup(commit_id);
6485 if (s->commit_id == NULL)
6486 return got_error_from_errno("got_object_id_dup");
6488 err = got_object_open_as_commit(&commit, repo, commit_id);
6493 * The root is opened here and will be closed when the view is closed.
6494 * Any visited subtrees and their path-wise parents are opened and
6497 err = got_object_open_as_tree(&s->root, repo,
6498 got_object_commit_get_tree_id(commit));
6503 err = got_object_id_str(&commit_id_str, commit_id);
6507 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6508 err = got_error_from_errno("asprintf");
6512 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6513 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6514 if (head_ref_name) {
6515 s->head_ref_name = strdup(head_ref_name);
6516 if (s->head_ref_name == NULL) {
6517 err = got_error_from_errno("strdup");
6523 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6524 err = add_color(&s->colors, "\\$$",
6525 TOG_COLOR_TREE_SUBMODULE,
6526 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6529 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6530 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6533 err = add_color(&s->colors, "/$",
6534 TOG_COLOR_TREE_DIRECTORY,
6535 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6539 err = add_color(&s->colors, "\\*$",
6540 TOG_COLOR_TREE_EXECUTABLE,
6541 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6545 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6546 get_color_value("TOG_COLOR_COMMIT"));
6551 view->show = show_tree_view;
6552 view->input = input_tree_view;
6553 view->close = close_tree_view;
6554 view->search_start = search_start_tree_view;
6555 view->search_next = search_next_tree_view;
6557 free(commit_id_str);
6559 got_object_commit_close(commit);
6561 close_tree_view(view);
6565 static const struct got_error *
6566 close_tree_view(struct tog_view *view)
6568 struct tog_tree_view_state *s = &view->state.tree;
6570 free_colors(&s->colors);
6571 free(s->tree_label);
6572 s->tree_label = NULL;
6574 s->commit_id = NULL;
6575 free(s->head_ref_name);
6576 s->head_ref_name = NULL;
6577 while (!TAILQ_EMPTY(&s->parents)) {
6578 struct tog_parent_tree *parent;
6579 parent = TAILQ_FIRST(&s->parents);
6580 TAILQ_REMOVE(&s->parents, parent, entry);
6581 if (parent->tree != s->root)
6582 got_object_tree_close(parent->tree);
6586 if (s->tree != NULL && s->tree != s->root)
6587 got_object_tree_close(s->tree);
6589 got_object_tree_close(s->root);
6593 static const struct got_error *
6594 search_start_tree_view(struct tog_view *view)
6596 struct tog_tree_view_state *s = &view->state.tree;
6598 s->matched_entry = NULL;
6603 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6605 regmatch_t regmatch;
6607 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6611 static const struct got_error *
6612 search_next_tree_view(struct tog_view *view)
6614 struct tog_tree_view_state *s = &view->state.tree;
6615 struct got_tree_entry *te = NULL;
6617 if (!view->searching) {
6618 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6622 if (s->matched_entry) {
6623 if (view->searching == TOG_SEARCH_FORWARD) {
6624 if (s->selected_entry)
6625 te = got_tree_entry_get_next(s->tree,
6628 te = got_object_tree_get_first_entry(s->tree);
6630 if (s->selected_entry == NULL)
6631 te = got_object_tree_get_last_entry(s->tree);
6633 te = got_tree_entry_get_prev(s->tree,
6637 if (s->selected_entry)
6638 te = s->selected_entry;
6639 else if (view->searching == TOG_SEARCH_FORWARD)
6640 te = got_object_tree_get_first_entry(s->tree);
6642 te = got_object_tree_get_last_entry(s->tree);
6647 if (s->matched_entry == NULL) {
6648 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6651 if (view->searching == TOG_SEARCH_FORWARD)
6652 te = got_object_tree_get_first_entry(s->tree);
6654 te = got_object_tree_get_last_entry(s->tree);
6657 if (match_tree_entry(te, &view->regex)) {
6658 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6659 s->matched_entry = te;
6663 if (view->searching == TOG_SEARCH_FORWARD)
6664 te = got_tree_entry_get_next(s->tree, te);
6666 te = got_tree_entry_get_prev(s->tree, te);
6669 if (s->matched_entry) {
6670 s->first_displayed_entry = s->matched_entry;
6677 static const struct got_error *
6678 show_tree_view(struct tog_view *view)
6680 const struct got_error *err = NULL;
6681 struct tog_tree_view_state *s = &view->state.tree;
6684 err = tree_entry_path(&parent_path, &s->parents, NULL);
6688 err = draw_tree_entries(view, parent_path);
6695 static const struct got_error *
6696 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6698 const struct got_error *err = NULL;
6699 struct tog_tree_view_state *s = &view->state.tree;
6700 struct tog_view *log_view, *ref_view;
6701 struct got_tree_entry *te;
6702 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6706 s->show_ids = !s->show_ids;
6711 if (!s->selected_entry)
6713 if (view_is_parent_view(view))
6714 view_get_split(view, &begin_y, &begin_x);
6715 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6716 if (view_is_parent_view(view) &&
6717 view->mode == TOG_VIEW_SPLIT_HRZN) {
6718 err = view_init_hsplit(view, begin_y);
6723 log_view->focussed = 1;
6724 log_view->mode = view->mode;
6725 log_view->nlines = view->lines - begin_y;
6726 if (view_is_parent_view(view)) {
6727 view_transfer_size(log_view, view);
6728 err = view_close_child(view);
6731 err = view_set_child(view, log_view);
6734 view->focus_child = 1;
6736 *new_view = log_view;
6740 if (view_is_parent_view(view))
6741 view_get_split(view, &begin_y, &begin_x);
6742 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6743 if (ref_view == NULL)
6744 return got_error_from_errno("view_open");
6745 err = open_ref_view(ref_view, s->repo);
6747 view_close(ref_view);
6750 if (view_is_parent_view(view) &&
6751 view->mode == TOG_VIEW_SPLIT_HRZN) {
6752 err = view_init_hsplit(view, begin_y);
6757 ref_view->focussed = 1;
6758 ref_view->mode = view->mode;
6759 ref_view->nlines = view->lines - begin_y;
6760 if (view_is_parent_view(view)) {
6761 view_transfer_size(ref_view, view);
6762 err = view_close_child(view);
6765 err = view_set_child(view, ref_view);
6768 view->focus_child = 1;
6770 *new_view = ref_view;
6776 if (s->tree == s->root)
6777 s->first_displayed_entry =
6778 got_object_tree_get_first_entry(s->tree);
6780 s->first_displayed_entry = NULL;
6784 int eos = view->nlines - 3;
6786 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6790 te = got_object_tree_get_last_entry(s->tree);
6791 for (n = 0; n < eos; n++) {
6793 if(s->tree != s->root) {
6794 s->first_displayed_entry = NULL;
6799 s->first_displayed_entry = te;
6800 te = got_tree_entry_get_prev(s->tree, te);
6803 s->selected = n - 1;
6809 if (s->selected > 0) {
6813 tree_scroll_up(s, 1);
6814 if (s->selected_entry == NULL ||
6815 (s->tree == s->root && s->selected_entry ==
6816 got_object_tree_get_first_entry(s->tree)))
6826 if (s->tree == s->root) {
6827 if (got_object_tree_get_first_entry(s->tree) ==
6828 s->first_displayed_entry)
6829 s->selected -= MIN(s->selected, nscroll);
6831 if (s->first_displayed_entry == NULL)
6832 s->selected -= MIN(s->selected, nscroll);
6834 tree_scroll_up(s, MAX(0, nscroll));
6835 if (s->selected_entry == NULL ||
6836 (s->tree == s->root && s->selected_entry ==
6837 got_object_tree_get_first_entry(s->tree)))
6843 if (s->selected < s->ndisplayed - 1) {
6847 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6849 /* can't scroll any further */
6853 tree_scroll_down(view, 1);
6863 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6865 /* can't scroll any further; move cursor down */
6866 if (s->selected < s->ndisplayed - 1)
6867 s->selected += MIN(nscroll,
6868 s->ndisplayed - s->selected - 1);
6873 tree_scroll_down(view, nscroll);
6878 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6879 struct tog_parent_tree *parent;
6880 /* user selected '..' */
6881 if (s->tree == s->root) {
6885 parent = TAILQ_FIRST(&s->parents);
6886 TAILQ_REMOVE(&s->parents, parent,
6888 got_object_tree_close(s->tree);
6889 s->tree = parent->tree;
6890 s->first_displayed_entry =
6891 parent->first_displayed_entry;
6893 parent->selected_entry;
6894 s->selected = parent->selected;
6895 if (s->selected > view->nlines - 3) {
6896 err = offset_selection_down(view);
6901 } else if (S_ISDIR(got_tree_entry_get_mode(
6902 s->selected_entry))) {
6903 struct got_tree_object *subtree;
6905 err = got_object_open_as_tree(&subtree, s->repo,
6906 got_tree_entry_get_id(s->selected_entry));
6909 err = tree_view_visit_subtree(s, subtree);
6911 got_object_tree_close(subtree);
6914 } else if (S_ISREG(got_tree_entry_get_mode(
6915 s->selected_entry))) {
6916 struct tog_view *blame_view;
6917 int begin_x = 0, begin_y = 0;
6919 if (view_is_parent_view(view))
6920 view_get_split(view, &begin_y, &begin_x);
6922 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6923 s->selected_entry, &s->parents,
6924 s->commit_id, s->repo);
6928 if (view_is_parent_view(view) &&
6929 view->mode == TOG_VIEW_SPLIT_HRZN) {
6930 err = view_init_hsplit(view, begin_y);
6937 blame_view->focussed = 1;
6938 blame_view->mode = view->mode;
6939 blame_view->nlines = view->lines - begin_y;
6940 if (view_is_parent_view(view)) {
6941 view_transfer_size(blame_view, view);
6942 err = view_close_child(view);
6945 err = view_set_child(view, blame_view);
6948 view->focus_child = 1;
6950 *new_view = blame_view;
6954 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6955 s->selected = view->nlines - 4;
6971 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6976 static const struct got_error *
6977 cmd_tree(int argc, char *argv[])
6979 const struct got_error *error;
6980 struct got_repository *repo = NULL;
6981 struct got_worktree *worktree = NULL;
6982 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6983 struct got_object_id *commit_id = NULL;
6984 struct got_commit_object *commit = NULL;
6985 const char *commit_id_arg = NULL;
6987 struct got_reference *ref = NULL;
6988 const char *head_ref_name = NULL;
6990 struct tog_view *view;
6991 int *pack_fds = NULL;
6993 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6996 commit_id_arg = optarg;
6999 repo_path = realpath(optarg, NULL);
7000 if (repo_path == NULL)
7001 return got_error_from_errno2("realpath",
7016 error = got_repo_pack_fds_open(&pack_fds);
7020 if (repo_path == NULL) {
7021 cwd = getcwd(NULL, 0);
7023 return got_error_from_errno("getcwd");
7024 error = got_worktree_open(&worktree, cwd);
7025 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7029 strdup(got_worktree_get_repo_path(worktree));
7031 repo_path = strdup(cwd);
7032 if (repo_path == NULL) {
7033 error = got_error_from_errno("strdup");
7038 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7042 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7049 error = apply_unveil(got_repo_get_path(repo), NULL);
7053 error = tog_load_refs(repo, 0);
7057 if (commit_id_arg == NULL) {
7058 error = got_repo_match_object_id(&commit_id, &label,
7059 worktree ? got_worktree_get_head_ref_name(worktree) :
7060 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7063 head_ref_name = label;
7065 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7067 head_ref_name = got_ref_get_name(ref);
7068 else if (error->code != GOT_ERR_NOT_REF)
7070 error = got_repo_match_object_id(&commit_id, NULL,
7071 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7076 error = got_object_open_as_commit(&commit, repo, commit_id);
7080 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7082 error = got_error_from_errno("view_open");
7085 error = open_tree_view(view, commit_id, head_ref_name, repo);
7088 if (!got_path_is_root_dir(in_repo_path)) {
7089 error = tree_view_walk_path(&view->state.tree, commit,
7096 /* Release work tree lock. */
7097 got_worktree_close(worktree);
7100 error = view_loop(view);
7109 const struct got_error *close_err = got_repo_close(repo);
7114 const struct got_error *pack_err =
7115 got_repo_pack_fds_close(pack_fds);
7123 static const struct got_error *
7124 ref_view_load_refs(struct tog_ref_view_state *s)
7126 struct got_reflist_entry *sre;
7127 struct tog_reflist_entry *re;
7130 TAILQ_FOREACH(sre, &tog_refs, entry) {
7131 if (strncmp(got_ref_get_name(sre->ref),
7132 "refs/got/", 9) == 0 &&
7133 strncmp(got_ref_get_name(sre->ref),
7134 "refs/got/backup/", 16) != 0)
7137 re = malloc(sizeof(*re));
7139 return got_error_from_errno("malloc");
7141 re->ref = got_ref_dup(sre->ref);
7142 if (re->ref == NULL)
7143 return got_error_from_errno("got_ref_dup");
7144 re->idx = s->nrefs++;
7145 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7148 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7153 ref_view_free_refs(struct tog_ref_view_state *s)
7155 struct tog_reflist_entry *re;
7157 while (!TAILQ_EMPTY(&s->refs)) {
7158 re = TAILQ_FIRST(&s->refs);
7159 TAILQ_REMOVE(&s->refs, re, entry);
7160 got_ref_close(re->ref);
7165 static const struct got_error *
7166 open_ref_view(struct tog_view *view, struct got_repository *repo)
7168 const struct got_error *err = NULL;
7169 struct tog_ref_view_state *s = &view->state.ref;
7171 s->selected_entry = 0;
7174 TAILQ_INIT(&s->refs);
7175 STAILQ_INIT(&s->colors);
7177 err = ref_view_load_refs(s);
7181 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7182 err = add_color(&s->colors, "^refs/heads/",
7183 TOG_COLOR_REFS_HEADS,
7184 get_color_value("TOG_COLOR_REFS_HEADS"));
7188 err = add_color(&s->colors, "^refs/tags/",
7189 TOG_COLOR_REFS_TAGS,
7190 get_color_value("TOG_COLOR_REFS_TAGS"));
7194 err = add_color(&s->colors, "^refs/remotes/",
7195 TOG_COLOR_REFS_REMOTES,
7196 get_color_value("TOG_COLOR_REFS_REMOTES"));
7200 err = add_color(&s->colors, "^refs/got/backup/",
7201 TOG_COLOR_REFS_BACKUP,
7202 get_color_value("TOG_COLOR_REFS_BACKUP"));
7207 view->show = show_ref_view;
7208 view->input = input_ref_view;
7209 view->close = close_ref_view;
7210 view->search_start = search_start_ref_view;
7211 view->search_next = search_next_ref_view;
7214 free_colors(&s->colors);
7218 static const struct got_error *
7219 close_ref_view(struct tog_view *view)
7221 struct tog_ref_view_state *s = &view->state.ref;
7223 ref_view_free_refs(s);
7224 free_colors(&s->colors);
7229 static const struct got_error *
7230 resolve_reflist_entry(struct got_object_id **commit_id,
7231 struct tog_reflist_entry *re, struct got_repository *repo)
7233 const struct got_error *err = NULL;
7234 struct got_object_id *obj_id;
7235 struct got_tag_object *tag = NULL;
7240 err = got_ref_resolve(&obj_id, repo, re->ref);
7244 err = got_object_get_type(&obj_type, repo, obj_id);
7249 case GOT_OBJ_TYPE_COMMIT:
7250 *commit_id = obj_id;
7252 case GOT_OBJ_TYPE_TAG:
7253 err = got_object_open_as_tag(&tag, repo, obj_id);
7257 err = got_object_get_type(&obj_type, repo,
7258 got_object_tag_get_object_id(tag));
7261 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7262 err = got_error(GOT_ERR_OBJ_TYPE);
7265 *commit_id = got_object_id_dup(
7266 got_object_tag_get_object_id(tag));
7267 if (*commit_id == NULL) {
7268 err = got_error_from_errno("got_object_id_dup");
7273 err = got_error(GOT_ERR_OBJ_TYPE);
7279 got_object_tag_close(tag);
7287 static const struct got_error *
7288 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7289 struct tog_reflist_entry *re, struct got_repository *repo)
7291 struct tog_view *log_view;
7292 const struct got_error *err = NULL;
7293 struct got_object_id *commit_id = NULL;
7297 err = resolve_reflist_entry(&commit_id, re, repo);
7299 if (err->code != GOT_ERR_OBJ_TYPE)
7305 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7306 if (log_view == NULL) {
7307 err = got_error_from_errno("view_open");
7311 err = open_log_view(log_view, commit_id, repo,
7312 got_ref_get_name(re->ref), "", 0);
7315 view_close(log_view);
7317 *new_view = log_view;
7323 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7325 struct tog_reflist_entry *re;
7328 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7331 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7332 while (i++ < maxscroll) {
7335 s->first_displayed_entry = re;
7336 re = TAILQ_PREV(re, tog_reflist_head, entry);
7340 static const struct got_error *
7341 ref_scroll_down(struct tog_view *view, int maxscroll)
7343 struct tog_ref_view_state *s = &view->state.ref;
7344 struct tog_reflist_entry *next, *last;
7347 if (s->first_displayed_entry)
7348 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7350 next = TAILQ_FIRST(&s->refs);
7352 last = s->last_displayed_entry;
7353 while (next && n++ < maxscroll) {
7355 last = TAILQ_NEXT(last, entry);
7356 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7357 s->first_displayed_entry = next;
7358 next = TAILQ_NEXT(next, entry);
7365 static const struct got_error *
7366 search_start_ref_view(struct tog_view *view)
7368 struct tog_ref_view_state *s = &view->state.ref;
7370 s->matched_entry = NULL;
7375 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7377 regmatch_t regmatch;
7379 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7383 static const struct got_error *
7384 search_next_ref_view(struct tog_view *view)
7386 struct tog_ref_view_state *s = &view->state.ref;
7387 struct tog_reflist_entry *re = NULL;
7389 if (!view->searching) {
7390 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7394 if (s->matched_entry) {
7395 if (view->searching == TOG_SEARCH_FORWARD) {
7396 if (s->selected_entry)
7397 re = TAILQ_NEXT(s->selected_entry, entry);
7399 re = TAILQ_PREV(s->selected_entry,
7400 tog_reflist_head, entry);
7402 if (s->selected_entry == NULL)
7403 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7405 re = TAILQ_PREV(s->selected_entry,
7406 tog_reflist_head, entry);
7409 if (s->selected_entry)
7410 re = s->selected_entry;
7411 else if (view->searching == TOG_SEARCH_FORWARD)
7412 re = TAILQ_FIRST(&s->refs);
7414 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7419 if (s->matched_entry == NULL) {
7420 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7423 if (view->searching == TOG_SEARCH_FORWARD)
7424 re = TAILQ_FIRST(&s->refs);
7426 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7429 if (match_reflist_entry(re, &view->regex)) {
7430 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7431 s->matched_entry = re;
7435 if (view->searching == TOG_SEARCH_FORWARD)
7436 re = TAILQ_NEXT(re, entry);
7438 re = TAILQ_PREV(re, tog_reflist_head, entry);
7441 if (s->matched_entry) {
7442 s->first_displayed_entry = s->matched_entry;
7449 static const struct got_error *
7450 show_ref_view(struct tog_view *view)
7452 const struct got_error *err = NULL;
7453 struct tog_ref_view_state *s = &view->state.ref;
7454 struct tog_reflist_entry *re;
7457 struct tog_color *tc;
7459 int limit = view->nlines;
7461 werase(view->window);
7464 if (view_is_hsplit_top(view))
7465 --limit; /* border */
7470 re = s->first_displayed_entry;
7472 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7474 return got_error_from_errno("asprintf");
7476 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7481 if (view_needs_focus_indication(view))
7482 wstandout(view->window);
7483 waddwstr(view->window, wline);
7484 if (view_needs_focus_indication(view))
7485 wstandend(view->window);
7490 if (width < view->ncols - 1)
7491 waddch(view->window, '\n');
7496 while (re && limit > 0) {
7498 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7501 struct got_commit_object *ci;
7502 struct got_tag_object *tag;
7503 struct got_object_id *id;
7507 err = got_ref_resolve(&id, s->repo, re->ref);
7510 err = got_object_open_as_tag(&tag, s->repo, id);
7512 if (err->code != GOT_ERR_OBJ_TYPE) {
7516 err = got_object_open_as_commit(&ci, s->repo,
7522 t = got_object_commit_get_committer_time(ci);
7523 got_object_commit_close(ci);
7525 t = got_object_tag_get_tagger_time(tag);
7526 got_object_tag_close(tag);
7529 if (gmtime_r(&t, &tm) == NULL)
7530 return got_error_from_errno("gmtime_r");
7531 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7532 return got_error(GOT_ERR_NO_SPACE);
7534 if (got_ref_is_symbolic(re->ref)) {
7535 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7536 ymd : "", got_ref_get_name(re->ref),
7537 got_ref_get_symref_target(re->ref)) == -1)
7538 return got_error_from_errno("asprintf");
7539 } else if (s->show_ids) {
7540 struct got_object_id *id;
7542 err = got_ref_resolve(&id, s->repo, re->ref);
7545 err = got_object_id_str(&id_str, id);
7550 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7551 got_ref_get_name(re->ref), id_str) == -1) {
7552 err = got_error_from_errno("asprintf");
7559 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7560 got_ref_get_name(re->ref)) == -1)
7561 return got_error_from_errno("asprintf");
7563 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7569 if (n == s->selected) {
7571 wstandout(view->window);
7572 s->selected_entry = re;
7574 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7576 wattr_on(view->window,
7577 COLOR_PAIR(tc->colorpair), NULL);
7578 waddwstr(view->window, wline);
7580 wattr_off(view->window,
7581 COLOR_PAIR(tc->colorpair), NULL);
7582 if (width < view->ncols - 1)
7583 waddch(view->window, '\n');
7584 if (n == s->selected && view->focussed)
7585 wstandend(view->window);
7591 s->last_displayed_entry = re;
7594 re = TAILQ_NEXT(re, entry);
7601 static const struct got_error *
7602 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7603 struct tog_reflist_entry *re, struct got_repository *repo)
7605 const struct got_error *err = NULL;
7606 struct got_object_id *commit_id = NULL;
7607 struct tog_view *tree_view;
7611 err = resolve_reflist_entry(&commit_id, re, repo);
7613 if (err->code != GOT_ERR_OBJ_TYPE)
7620 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7621 if (tree_view == NULL) {
7622 err = got_error_from_errno("view_open");
7626 err = open_tree_view(tree_view, commit_id,
7627 got_ref_get_name(re->ref), repo);
7631 *new_view = tree_view;
7636 static const struct got_error *
7637 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7639 const struct got_error *err = NULL;
7640 struct tog_ref_view_state *s = &view->state.ref;
7641 struct tog_view *log_view, *tree_view;
7642 struct tog_reflist_entry *re;
7643 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7647 s->show_ids = !s->show_ids;
7651 s->show_date = !s->show_date;
7655 s->sort_by_date = !s->sort_by_date;
7657 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7658 got_ref_cmp_by_commit_timestamp_descending :
7659 tog_ref_cmp_by_name, s->repo);
7662 got_reflist_object_id_map_free(tog_refs_idmap);
7663 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7664 &tog_refs, s->repo);
7667 ref_view_free_refs(s);
7668 err = ref_view_load_refs(s);
7673 if (!s->selected_entry)
7675 if (view_is_parent_view(view))
7676 view_get_split(view, &begin_y, &begin_x);
7678 err = log_ref_entry(&log_view, begin_y, begin_x,
7679 s->selected_entry, s->repo);
7683 if (view_is_parent_view(view) &&
7684 view->mode == TOG_VIEW_SPLIT_HRZN) {
7685 err = view_init_hsplit(view, begin_y);
7691 log_view->focussed = 1;
7692 log_view->mode = view->mode;
7693 log_view->nlines = view->lines - begin_y;
7694 if (view_is_parent_view(view)) {
7695 view_transfer_size(log_view, view);
7696 err = view_close_child(view);
7699 err = view_set_child(view, log_view);
7702 view->focus_child = 1;
7704 *new_view = log_view;
7708 if (!s->selected_entry)
7710 if (view_is_parent_view(view))
7711 view_get_split(view, &begin_y, &begin_x);
7712 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7713 s->selected_entry, s->repo);
7714 if (err || tree_view == NULL)
7716 if (view_is_parent_view(view) &&
7717 view->mode == TOG_VIEW_SPLIT_HRZN) {
7718 err = view_init_hsplit(view, begin_y);
7723 tree_view->focussed = 1;
7724 tree_view->mode = view->mode;
7725 tree_view->nlines = view->lines - begin_y;
7726 if (view_is_parent_view(view)) {
7727 view_transfer_size(tree_view, view);
7728 err = view_close_child(view);
7731 err = view_set_child(view, tree_view);
7734 view->focus_child = 1;
7736 *new_view = tree_view;
7742 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7746 int eos = view->nlines - 1;
7748 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7752 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7753 for (n = 0; n < eos; n++) {
7756 s->first_displayed_entry = re;
7757 re = TAILQ_PREV(re, tog_reflist_head, entry);
7760 s->selected = n - 1;
7766 if (s->selected > 0) {
7770 ref_scroll_up(s, 1);
7771 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7781 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7782 s->selected -= MIN(nscroll, s->selected);
7783 ref_scroll_up(s, MAX(0, nscroll));
7784 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7790 if (s->selected < s->ndisplayed - 1) {
7794 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7795 /* can't scroll any further */
7799 ref_scroll_down(view, 1);
7809 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7810 /* can't scroll any further; move cursor down */
7811 if (s->selected < s->ndisplayed - 1)
7812 s->selected += MIN(nscroll,
7813 s->ndisplayed - s->selected - 1);
7814 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7815 s->selected += s->ndisplayed - s->selected - 1;
7819 ref_scroll_down(view, nscroll);
7824 err = tog_load_refs(s->repo, s->sort_by_date);
7827 ref_view_free_refs(s);
7828 err = ref_view_load_refs(s);
7831 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7832 s->selected = view->nlines - 2;
7846 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7851 static const struct got_error *
7852 cmd_ref(int argc, char *argv[])
7854 const struct got_error *error;
7855 struct got_repository *repo = NULL;
7856 struct got_worktree *worktree = NULL;
7857 char *cwd = NULL, *repo_path = NULL;
7859 struct tog_view *view;
7860 int *pack_fds = NULL;
7862 while ((ch = getopt(argc, argv, "r:")) != -1) {
7865 repo_path = realpath(optarg, NULL);
7866 if (repo_path == NULL)
7867 return got_error_from_errno2("realpath",
7882 error = got_repo_pack_fds_open(&pack_fds);
7886 if (repo_path == NULL) {
7887 cwd = getcwd(NULL, 0);
7889 return got_error_from_errno("getcwd");
7890 error = got_worktree_open(&worktree, cwd);
7891 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7895 strdup(got_worktree_get_repo_path(worktree));
7897 repo_path = strdup(cwd);
7898 if (repo_path == NULL) {
7899 error = got_error_from_errno("strdup");
7904 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7910 error = apply_unveil(got_repo_get_path(repo), NULL);
7914 error = tog_load_refs(repo, 0);
7918 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7920 error = got_error_from_errno("view_open");
7924 error = open_ref_view(view, repo);
7929 /* Release work tree lock. */
7930 got_worktree_close(worktree);
7933 error = view_loop(view);
7938 const struct got_error *close_err = got_repo_close(repo);
7943 const struct got_error *pack_err =
7944 got_repo_pack_fds_close(pack_fds);
7953 * If view was scrolled down to move the selected line into view when opening a
7954 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7957 offset_selection_up(struct tog_view *view)
7959 switch (view->type) {
7960 case TOG_VIEW_BLAME: {
7961 struct tog_blame_view_state *s = &view->state.blame;
7962 if (s->first_displayed_line == 1) {
7963 s->selected_line = MAX(s->selected_line - view->offset,
7967 if (s->first_displayed_line > view->offset)
7968 s->first_displayed_line -= view->offset;
7970 s->first_displayed_line = 1;
7971 s->selected_line += view->offset;
7975 log_scroll_up(&view->state.log, view->offset);
7976 view->state.log.selected += view->offset;
7979 ref_scroll_up(&view->state.ref, view->offset);
7980 view->state.ref.selected += view->offset;
7983 tree_scroll_up(&view->state.tree, view->offset);
7984 view->state.tree.selected += view->offset;
7994 * If the selected line is in the section of screen covered by the bottom split,
7995 * scroll down offset lines to move it into view and index its new position.
7997 static const struct got_error *
7998 offset_selection_down(struct tog_view *view)
8000 const struct got_error *err = NULL;
8001 const struct got_error *(*scrolld)(struct tog_view *, int);
8002 int *selected = NULL;
8005 switch (view->type) {
8006 case TOG_VIEW_BLAME: {
8007 struct tog_blame_view_state *s = &view->state.blame;
8010 if (s->selected_line > view->nlines - header) {
8011 offset = abs(view->nlines - s->selected_line - header);
8012 s->first_displayed_line += offset;
8013 s->selected_line -= offset;
8014 view->offset = offset;
8018 case TOG_VIEW_LOG: {
8019 struct tog_log_view_state *s = &view->state.log;
8020 scrolld = &log_scroll_down;
8021 header = view_is_parent_view(view) ? 3 : 2;
8022 selected = &s->selected;
8025 case TOG_VIEW_REF: {
8026 struct tog_ref_view_state *s = &view->state.ref;
8027 scrolld = &ref_scroll_down;
8029 selected = &s->selected;
8032 case TOG_VIEW_TREE: {
8033 struct tog_tree_view_state *s = &view->state.tree;
8034 scrolld = &tree_scroll_down;
8036 selected = &s->selected;
8046 if (selected && *selected > view->nlines - header) {
8047 offset = abs(view->nlines - *selected - header);
8048 view->offset = offset;
8049 if (scrolld && offset) {
8050 err = scrolld(view, offset);
8051 *selected -= offset;
8059 list_commands(FILE *fp)
8063 fprintf(fp, "commands:");
8064 for (i = 0; i < nitems(tog_commands); i++) {
8065 const struct tog_cmd *cmd = &tog_commands[i];
8066 fprintf(fp, " %s", cmd->name);
8072 usage(int hflag, int status)
8074 FILE *fp = (status == 0) ? stdout : stderr;
8076 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8079 fprintf(fp, "lazy usage: %s path\n", getprogname());
8086 make_argv(int argc, ...)
8094 argv = calloc(argc, sizeof(char *));
8097 for (i = 0; i < argc; i++) {
8098 argv[i] = strdup(va_arg(ap, char *));
8099 if (argv[i] == NULL)
8108 * Try to convert 'tog path' into a 'tog log path' command.
8109 * The user could simply have mistyped the command rather than knowingly
8110 * provided a path. So check whether argv[0] can in fact be resolved
8111 * to a path in the HEAD commit and print a special error if not.
8112 * This hack is for mpi@ <3
8114 static const struct got_error *
8115 tog_log_with_path(int argc, char *argv[])
8117 const struct got_error *error = NULL, *close_err;
8118 const struct tog_cmd *cmd = NULL;
8119 struct got_repository *repo = NULL;
8120 struct got_worktree *worktree = NULL;
8121 struct got_object_id *commit_id = NULL, *id = NULL;
8122 struct got_commit_object *commit = NULL;
8123 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8124 char *commit_id_str = NULL, **cmd_argv = NULL;
8125 int *pack_fds = NULL;
8127 cwd = getcwd(NULL, 0);
8129 return got_error_from_errno("getcwd");
8131 error = got_repo_pack_fds_open(&pack_fds);
8135 error = got_worktree_open(&worktree, cwd);
8136 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8140 repo_path = strdup(got_worktree_get_repo_path(worktree));
8142 repo_path = strdup(cwd);
8143 if (repo_path == NULL) {
8144 error = got_error_from_errno("strdup");
8148 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8152 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8157 error = tog_load_refs(repo, 0);
8160 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8161 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8162 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8167 got_worktree_close(worktree);
8171 error = got_object_open_as_commit(&commit, repo, commit_id);
8175 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8177 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8179 fprintf(stderr, "%s: '%s' is no known command or path\n",
8180 getprogname(), argv[0]);
8185 close_err = got_repo_close(repo);
8190 error = got_object_id_str(&commit_id_str, commit_id);
8194 cmd = &tog_commands[0]; /* log */
8196 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8197 error = cmd->cmd_main(argc, cmd_argv);
8200 close_err = got_repo_close(repo);
8205 got_object_commit_close(commit);
8207 got_worktree_close(worktree);
8209 const struct got_error *pack_err =
8210 got_repo_pack_fds_close(pack_fds);
8215 free(commit_id_str);
8222 for (i = 0; i < argc; i++)
8231 main(int argc, char *argv[])
8233 const struct got_error *error = NULL;
8234 const struct tog_cmd *cmd = NULL;
8235 int ch, hflag = 0, Vflag = 0;
8236 char **cmd_argv = NULL;
8237 static const struct option longopts[] = {
8238 { "version", no_argument, NULL, 'V' },
8241 char *diff_algo_str = NULL;
8243 setlocale(LC_CTYPE, "");
8245 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8265 got_version_print_str();
8270 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8278 /* Build an argument vector which runs a default command. */
8279 cmd = &tog_commands[0];
8281 cmd_argv = make_argv(argc, cmd->name);
8285 /* Did the user specify a command? */
8286 for (i = 0; i < nitems(tog_commands); i++) {
8287 if (strncmp(tog_commands[i].name, argv[0],
8288 strlen(argv[0])) == 0) {
8289 cmd = &tog_commands[i];
8295 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8296 if (diff_algo_str) {
8297 if (strcasecmp(diff_algo_str, "patience") == 0)
8298 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8299 if (strcasecmp(diff_algo_str, "myers") == 0)
8300 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8306 /* No command specified; try log with a path */
8307 error = tog_log_with_path(argc, argv);
8312 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8319 for (i = 0; i < argc; i++)
8324 if (error && error->code != GOT_ERR_CANCELLED)
8325 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);