2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
123 SIMPLEQ_ENTRY(tog_color) entry;
127 SIMPLEQ_HEAD(tog_colors, tog_color);
129 static const struct got_error *
130 add_color(struct tog_colors *colors, const char *pattern,
131 int idx, short color)
133 const struct got_error *err = NULL;
134 struct tog_color *tc;
137 if (idx < 1 || idx > COLOR_PAIRS - 1)
140 init_pair(idx, color, -1);
142 tc = calloc(1, sizeof(*tc));
144 return got_error_from_errno("calloc");
145 regerr = regcomp(&tc->regex, pattern,
146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
148 static char regerr_msg[512];
149 static char err_msg[512];
150 regerror(regerr, &tc->regex, regerr_msg,
152 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
154 err = got_error_msg(GOT_ERR_REGEX, err_msg);
159 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
164 free_colors(struct tog_colors *colors)
166 struct tog_color *tc;
168 while (!SIMPLEQ_EMPTY(colors)) {
169 tc = SIMPLEQ_FIRST(colors);
170 SIMPLEQ_REMOVE_HEAD(colors, entry);
177 get_color(struct tog_colors *colors, int colorpair)
179 struct tog_color *tc = NULL;
181 SIMPLEQ_FOREACH(tc, colors, entry) {
182 if (tc->colorpair == colorpair)
190 default_color_value(const char *envvar)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 return COLOR_MAGENTA;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
196 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
198 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
200 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 return COLOR_MAGENTA;
202 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 return COLOR_MAGENTA;
204 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
206 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
208 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
210 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
212 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
214 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
216 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 return COLOR_MAGENTA;
218 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
225 get_color_value(const char *envvar)
227 const char *val = getenv(envvar);
230 return default_color_value(envvar);
232 if (strcasecmp(val, "black") == 0)
234 if (strcasecmp(val, "red") == 0)
236 if (strcasecmp(val, "green") == 0)
238 if (strcasecmp(val, "yellow") == 0)
240 if (strcasecmp(val, "blue") == 0)
242 if (strcasecmp(val, "magenta") == 0)
243 return COLOR_MAGENTA;
244 if (strcasecmp(val, "cyan") == 0)
246 if (strcasecmp(val, "white") == 0)
248 if (strcasecmp(val, "default") == 0)
251 return default_color_value(envvar);
255 struct tog_diff_view_state {
256 struct got_object_id *id1, *id2;
257 const char *label1, *label2;
259 int first_displayed_line;
260 int last_displayed_line;
263 int ignore_whitespace;
265 struct got_repository *repo;
266 struct got_reflist_head refs;
267 struct tog_colors colors;
273 /* passed from log view; may be NULL */
274 struct tog_view *log_view;
277 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 struct tog_log_thread_args {
280 pthread_cond_t need_commits;
281 pthread_cond_t commit_loaded;
283 struct got_commit_graph *graph;
284 struct commit_queue *commits;
285 const char *in_repo_path;
286 struct got_object_id *start_id;
287 struct got_repository *repo;
290 struct commit_queue_entry **first_displayed_entry;
291 struct commit_queue_entry **selected_entry;
293 int *search_next_done;
297 struct tog_log_view_state {
298 struct commit_queue commits;
299 struct commit_queue_entry *first_displayed_entry;
300 struct commit_queue_entry *last_displayed_entry;
301 struct commit_queue_entry *selected_entry;
304 const char *head_ref_name;
306 struct got_repository *repo;
307 struct got_reflist_head refs;
308 struct got_object_id *start_id;
311 struct tog_log_thread_args thread_args;
312 struct commit_queue_entry *matched_entry;
313 struct commit_queue_entry *search_entry;
314 struct tog_colors colors;
317 #define TOG_COLOR_DIFF_MINUS 1
318 #define TOG_COLOR_DIFF_PLUS 2
319 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 #define TOG_COLOR_DIFF_META 4
321 #define TOG_COLOR_TREE_SUBMODULE 5
322 #define TOG_COLOR_TREE_SYMLINK 6
323 #define TOG_COLOR_TREE_DIRECTORY 7
324 #define TOG_COLOR_TREE_EXECUTABLE 8
325 #define TOG_COLOR_COMMIT 9
326 #define TOG_COLOR_AUTHOR 10
327 #define TOG_COLOR_DATE 11
328 #define TOG_COLOR_REFS_HEADS 12
329 #define TOG_COLOR_REFS_TAGS 13
330 #define TOG_COLOR_REFS_REMOTES 14
332 struct tog_blame_cb_args {
333 struct tog_blame_line *lines; /* one per line */
336 struct tog_view *view;
337 struct got_object_id *commit_id;
341 struct tog_blame_thread_args {
343 struct got_repository *repo;
344 struct tog_blame_cb_args *cb_args;
346 got_cancel_cb cancel_cb;
353 struct tog_blame_line *lines;
357 struct tog_blame_thread_args thread_args;
358 struct tog_blame_cb_args cb_args;
362 struct tog_blame_view_state {
363 int first_displayed_line;
364 int last_displayed_line;
369 struct got_object_id_queue blamed_commits;
370 struct got_object_qid *blamed_commit;
372 struct got_repository *repo;
373 struct got_object_id *commit_id;
374 struct tog_blame blame;
376 struct tog_colors colors;
379 struct tog_parent_tree {
380 TAILQ_ENTRY(tog_parent_tree) entry;
381 struct got_tree_object *tree;
382 struct got_tree_entry *first_displayed_entry;
383 struct got_tree_entry *selected_entry;
387 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 struct tog_tree_view_state {
391 struct got_tree_object *root;
392 struct got_tree_object *tree;
393 struct got_tree_entry *first_displayed_entry;
394 struct got_tree_entry *last_displayed_entry;
395 struct got_tree_entry *selected_entry;
396 int ndisplayed, selected, show_ids;
397 struct tog_parent_trees parents;
398 struct got_object_id *commit_id;
399 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
441 TAILQ_HEAD(tog_view_list_head, tog_view);
444 TAILQ_ENTRY(tog_view) entry;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
450 struct tog_view *parent;
451 struct tog_view *child;
454 /* type-specific state */
455 enum tog_view_type type;
457 struct tog_diff_view_state diff;
458 struct tog_log_view_state log;
459 struct tog_blame_view_state blame;
460 struct tog_tree_view_state tree;
461 struct tog_ref_view_state ref;
464 const struct got_error *(*show)(struct tog_view *);
465 const struct got_error *(*input)(struct tog_view **,
466 struct tog_view **, struct tog_view**, struct tog_view *, int);
467 const struct got_error *(*close)(struct tog_view *);
469 const struct got_error *(*search_start)(struct tog_view *);
470 const struct got_error *(*search_next)(struct tog_view *);
472 #define TOG_SEARCH_FORWARD 1
473 #define TOG_SEARCH_BACKWARD 2
474 int search_next_done;
475 #define TOG_SEARCH_HAVE_MORE 1
476 #define TOG_SEARCH_NO_MORE 2
477 #define TOG_SEARCH_HAVE_NONE 3
482 static const struct got_error *open_diff_view(struct tog_view *,
483 struct got_object_id *, struct got_object_id *,
484 const char *, const char *, int, int, int, struct tog_view *,
485 struct got_repository *);
486 static const struct got_error *show_diff_view(struct tog_view *);
487 static const struct got_error *input_diff_view(struct tog_view **,
488 struct tog_view **, struct tog_view **, struct tog_view *, int);
489 static const struct got_error* close_diff_view(struct tog_view *);
490 static const struct got_error *search_start_diff_view(struct tog_view *);
491 static const struct got_error *search_next_diff_view(struct tog_view *);
493 static const struct got_error *open_log_view(struct tog_view *,
494 struct got_object_id *, struct got_repository *,
495 const char *, const char *, int);
496 static const struct got_error * show_log_view(struct tog_view *);
497 static const struct got_error *input_log_view(struct tog_view **,
498 struct tog_view **, struct tog_view **, struct tog_view *, int);
499 static const struct got_error *close_log_view(struct tog_view *);
500 static const struct got_error *search_start_log_view(struct tog_view *);
501 static const struct got_error *search_next_log_view(struct tog_view *);
503 static const struct got_error *open_blame_view(struct tog_view *, char *,
504 struct got_object_id *, struct got_repository *);
505 static const struct got_error *show_blame_view(struct tog_view *);
506 static const struct got_error *input_blame_view(struct tog_view **,
507 struct tog_view **, struct tog_view **, struct tog_view *, int);
508 static const struct got_error *close_blame_view(struct tog_view *);
509 static const struct got_error *search_start_blame_view(struct tog_view *);
510 static const struct got_error *search_next_blame_view(struct tog_view *);
512 static const struct got_error *open_tree_view(struct tog_view *,
513 struct got_tree_object *, struct got_object_id *, struct got_repository *);
514 static const struct got_error *show_tree_view(struct tog_view *);
515 static const struct got_error *input_tree_view(struct tog_view **,
516 struct tog_view **, struct tog_view **, struct tog_view *, int);
517 static const struct got_error *close_tree_view(struct tog_view *);
518 static const struct got_error *search_start_tree_view(struct tog_view *);
519 static const struct got_error *search_next_tree_view(struct tog_view *);
521 static const struct got_error *open_ref_view(struct tog_view *,
522 struct got_repository *);
523 static const struct got_error *show_ref_view(struct tog_view *);
524 static const struct got_error *input_ref_view(struct tog_view **,
525 struct tog_view **, struct tog_view **, struct tog_view *, int);
526 static const struct got_error *close_ref_view(struct tog_view *);
527 static const struct got_error *search_start_ref_view(struct tog_view *);
528 static const struct got_error *search_next_ref_view(struct tog_view *);
530 static volatile sig_atomic_t tog_sigwinch_received;
531 static volatile sig_atomic_t tog_sigpipe_received;
532 static volatile sig_atomic_t tog_sigcont_received;
535 tog_sigwinch(int signo)
537 tog_sigwinch_received = 1;
541 tog_sigpipe(int signo)
543 tog_sigpipe_received = 1;
547 tog_sigcont(int signo)
549 tog_sigcont_received = 1;
552 static const struct got_error *
553 view_close(struct tog_view *view)
555 const struct got_error *err = NULL;
558 view_close(view->child);
562 err = view->close(view);
564 del_panel(view->panel);
566 delwin(view->window);
571 static struct tog_view *
572 view_open(int nlines, int ncols, int begin_y, int begin_x,
573 enum tog_view_type type)
575 struct tog_view *view = calloc(1, sizeof(*view));
583 view->nlines = nlines ? nlines : LINES - begin_y;
584 view->ncols = ncols ? ncols : COLS - begin_x;
585 view->begin_y = begin_y;
586 view->begin_x = begin_x;
587 view->window = newwin(nlines, ncols, begin_y, begin_x);
588 if (view->window == NULL) {
592 view->panel = new_panel(view->window);
593 if (view->panel == NULL ||
594 set_panel_userptr(view->panel, view) != OK) {
599 keypad(view->window, TRUE);
604 view_split_begin_x(int begin_x)
606 if (begin_x > 0 || COLS < 120)
608 return (COLS - MAX(COLS / 2, 80));
611 static const struct got_error *view_resize(struct tog_view *);
613 static const struct got_error *
614 view_splitscreen(struct tog_view *view)
616 const struct got_error *err = NULL;
619 view->begin_x = view_split_begin_x(0);
620 view->nlines = LINES;
621 view->ncols = COLS - view->begin_x;
624 err = view_resize(view);
628 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
629 return got_error_from_errno("mvwin");
634 static const struct got_error *
635 view_fullscreen(struct tog_view *view)
637 const struct got_error *err = NULL;
641 view->nlines = LINES;
645 err = view_resize(view);
649 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
650 return got_error_from_errno("mvwin");
656 view_is_parent_view(struct tog_view *view)
658 return view->parent == NULL;
661 static const struct got_error *
662 view_resize(struct tog_view *view)
666 if (view->lines > LINES)
667 nlines = view->nlines - (view->lines - LINES);
669 nlines = view->nlines + (LINES - view->lines);
671 if (view->cols > COLS)
672 ncols = view->ncols - (view->cols - COLS);
674 ncols = view->ncols + (COLS - view->cols);
676 if (wresize(view->window, nlines, ncols) == ERR)
677 return got_error_from_errno("wresize");
678 if (replace_panel(view->panel, view->window) == ERR)
679 return got_error_from_errno("replace_panel");
680 wclear(view->window);
682 view->nlines = nlines;
688 view->child->begin_x = view_split_begin_x(view->begin_x);
689 if (view->child->begin_x == 0) {
690 view_fullscreen(view->child);
691 if (view->child->focussed)
692 show_panel(view->child->panel);
694 show_panel(view->panel);
696 view_splitscreen(view->child);
697 show_panel(view->child->panel);
704 static const struct got_error *
705 view_close_child(struct tog_view *view)
707 const struct got_error *err = NULL;
709 if (view->child == NULL)
712 err = view_close(view->child);
717 static const struct got_error *
718 view_set_child(struct tog_view *view, struct tog_view *child)
720 const struct got_error *err = NULL;
723 child->parent = view;
728 view_is_splitscreen(struct tog_view *view)
730 return view->begin_x > 0;
739 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
740 cols = 80; /* Default */
746 resize_term(lines, cols);
749 static const struct got_error *
750 view_search_start(struct tog_view *view)
752 const struct got_error *err = NULL;
756 if (view->nlines < 1)
759 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
760 wclrtoeol(view->window);
764 ret = wgetnstr(view->window, pattern, sizeof(pattern));
770 if (view->searching) {
771 regfree(&view->regex);
775 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
776 err = view->search_start(view);
778 regfree(&view->regex);
781 view->searching = TOG_SEARCH_FORWARD;
782 view->search_next_done = 0;
783 view->search_next(view);
789 static const struct got_error *
790 view_input(struct tog_view **new, struct tog_view **dead,
791 struct tog_view **focus, int *done, struct tog_view *view,
792 struct tog_view_list_head *views)
794 const struct got_error *err = NULL;
802 /* Clear "no matches" indicator. */
803 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
804 view->search_next_done == TOG_SEARCH_HAVE_NONE)
805 view->search_next_done = TOG_SEARCH_HAVE_MORE;
807 if (view->searching && !view->search_next_done) {
808 errcode = pthread_mutex_unlock(&tog_mutex);
810 return got_error_set_errno(errcode,
811 "pthread_mutex_unlock");
813 errcode = pthread_mutex_lock(&tog_mutex);
815 return got_error_set_errno(errcode,
816 "pthread_mutex_lock");
817 view->search_next(view);
821 nodelay(stdscr, FALSE);
822 /* Allow threads to make progress while we are waiting for input. */
823 errcode = pthread_mutex_unlock(&tog_mutex);
825 return got_error_set_errno(errcode, "pthread_mutex_unlock");
826 ch = wgetch(view->window);
827 errcode = pthread_mutex_lock(&tog_mutex);
829 return got_error_set_errno(errcode, "pthread_mutex_lock");
830 nodelay(stdscr, TRUE);
832 if (tog_sigwinch_received || tog_sigcont_received) {
834 tog_sigwinch_received = 0;
835 tog_sigcont_received = 0;
836 TAILQ_FOREACH(v, views, entry) {
837 err = view_resize(v);
840 err = v->input(new, dead, focus, v, KEY_RESIZE);
851 *focus = view->child;
852 view->child_focussed = 1;
853 } else if (view->parent) {
854 *focus = view->parent;
855 view->parent->child_focussed = 0;
859 err = view->input(new, dead, focus, view, ch);
866 if (view_is_parent_view(view)) {
867 if (view->child == NULL)
869 if (view_is_splitscreen(view->child)) {
870 *focus = view->child;
871 view->child_focussed = 1;
872 err = view_fullscreen(view->child);
874 err = view_splitscreen(view->child);
877 err = view->child->input(new, dead, focus,
878 view->child, KEY_RESIZE);
880 if (view_is_splitscreen(view)) {
882 view->parent->child_focussed = 1;
883 err = view_fullscreen(view);
885 err = view_splitscreen(view);
889 err = view->input(new, dead, focus, view,
896 if (view->search_start)
897 view_search_start(view);
899 err = view->input(new, dead, focus, view, ch);
903 if (view->search_next) {
904 view->searching = (ch == 'n' ?
905 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
906 view->search_next_done = 0;
907 view->search_next(view);
909 err = view->input(new, dead, focus, view, ch);
912 err = view->input(new, dead, focus, view, ch);
920 view_vborder(struct tog_view *view)
923 struct tog_view *view_above;
926 return view_vborder(view->parent);
928 panel = panel_above(view->panel);
932 view_above = panel_userptr(panel);
933 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
934 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
938 view_needs_focus_indication(struct tog_view *view)
940 if (view_is_parent_view(view)) {
941 if (view->child == NULL || view->child_focussed)
943 if (!view_is_splitscreen(view->child))
945 } else if (!view_is_splitscreen(view))
948 return view->focussed;
951 static const struct got_error *
952 view_loop(struct tog_view *view)
954 const struct got_error *err = NULL;
955 struct tog_view_list_head views;
956 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
957 int fast_refresh = 10;
958 int done = 0, errcode;
960 errcode = pthread_mutex_lock(&tog_mutex);
962 return got_error_set_errno(errcode, "pthread_mutex_lock");
965 TAILQ_INSERT_HEAD(&views, view, entry);
969 err = view->show(view);
974 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
975 /* Refresh fast during initialization, then become slower. */
976 if (fast_refresh && fast_refresh-- == 0)
977 halfdelay(10); /* switch to once per second */
979 err = view_input(&new_view, &dead_view, &focus_view, &done,
984 struct tog_view *prev = NULL;
986 if (view_is_parent_view(dead_view))
987 prev = TAILQ_PREV(dead_view,
988 tog_view_list_head, entry);
989 else if (view->parent != dead_view)
992 if (dead_view->parent)
993 dead_view->parent->child = NULL;
995 TAILQ_REMOVE(&views, dead_view, entry);
997 err = view_close(dead_view);
998 if (err || (dead_view == main_view && new_view == NULL))
1001 if (view == dead_view) {
1006 else if (!TAILQ_EMPTY(&views))
1007 view = TAILQ_LAST(&views,
1008 tog_view_list_head);
1012 if (view->child && view->child_focussed)
1013 focus_view = view->child;
1020 struct tog_view *v, *t;
1021 /* Only allow one parent view per type. */
1022 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1023 if (v->type != new_view->type)
1025 TAILQ_REMOVE(&views, v, entry);
1026 err = view_close(v);
1031 TAILQ_INSERT_TAIL(&views, new_view, entry);
1033 if (focus_view == NULL)
1034 focus_view = new_view;
1037 show_panel(focus_view->panel);
1040 focus_view->focussed = 1;
1043 show_panel(new_view->panel);
1044 if (view->child && view_is_splitscreen(view->child))
1045 show_panel(view->child->panel);
1048 if (focus_view == NULL) {
1050 show_panel(view->panel);
1051 if (view->child && view_is_splitscreen(view->child))
1052 show_panel(view->child->panel);
1056 err = view->parent->show(view->parent);
1060 err = view->show(view);
1064 err = view->child->show(view->child);
1073 while (!TAILQ_EMPTY(&views)) {
1074 view = TAILQ_FIRST(&views);
1075 TAILQ_REMOVE(&views, view, entry);
1079 errcode = pthread_mutex_unlock(&tog_mutex);
1080 if (errcode && err == NULL)
1081 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1091 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1096 /* Create newly allocated wide-character string equivalent to a byte string. */
1097 static const struct got_error *
1098 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1101 const struct got_error *err = NULL;
1104 *wlen = mbstowcs(NULL, s, 0);
1105 if (*wlen == (size_t)-1) {
1107 if (errno != EILSEQ)
1108 return got_error_from_errno("mbstowcs");
1110 /* byte string invalid in current encoding; try to "fix" it */
1111 err = got_mbsavis(&vis, &vislen, s);
1114 *wlen = mbstowcs(NULL, vis, 0);
1115 if (*wlen == (size_t)-1) {
1116 err = got_error_from_errno("mbstowcs"); /* give up */
1121 *ws = calloc(*wlen + 1, sizeof(**ws));
1123 err = got_error_from_errno("calloc");
1127 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1128 err = got_error_from_errno("mbstowcs");
1139 /* Format a line for display, ensuring that it won't overflow a width limit. */
1140 static const struct got_error *
1141 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1144 const struct got_error *err = NULL;
1146 wchar_t *wline = NULL;
1153 err = mbs2ws(&wline, &wlen, line);
1159 int width = wcwidth(wline[i]);
1166 if (width == 1 || width == 2) {
1167 if (cols + width > wlimit)
1171 } else if (width == -1) {
1172 if (wline[i] == L'\t') {
1174 ((cols + col_tab_align) % TABSIZE);
1175 if (cols + width > wlimit)
1181 err = got_error_from_errno("wcwidth");
1196 static const struct got_error*
1197 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1198 struct got_object_id *id, struct got_repository *repo)
1200 static const struct got_error *err = NULL;
1201 struct got_reflist_entry *re;
1207 SIMPLEQ_FOREACH(re, refs, entry) {
1208 struct got_tag_object *tag = NULL;
1209 struct got_object_id *ref_id;
1212 name = got_ref_get_name(re->ref);
1213 if (strcmp(name, GOT_REF_HEAD) == 0)
1215 if (strncmp(name, "refs/", 5) == 0)
1217 if (strncmp(name, "got/", 4) == 0)
1219 if (strncmp(name, "heads/", 6) == 0)
1221 if (strncmp(name, "remotes/", 8) == 0) {
1223 s = strstr(name, "/" GOT_REF_HEAD);
1224 if (s != NULL && s[strlen(s)] == '\0')
1227 err = got_ref_resolve(&ref_id, repo, re->ref);
1230 if (strncmp(name, "tags/", 5) == 0) {
1231 err = got_object_open_as_tag(&tag, repo, ref_id);
1233 if (err->code != GOT_ERR_OBJ_TYPE) {
1237 /* Ref points at something other than a tag. */
1242 cmp = got_object_id_cmp(tag ?
1243 got_object_tag_get_object_id(tag) : ref_id, id);
1246 got_object_tag_close(tag);
1250 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1251 s ? ", " : "", name) == -1) {
1252 err = got_error_from_errno("asprintf");
1263 static const struct got_error *
1264 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1267 char *smallerthan, *at;
1269 smallerthan = strchr(author, '<');
1270 if (smallerthan && smallerthan[1] != '\0')
1271 author = smallerthan + 1;
1272 at = strchr(author, '@');
1275 return format_line(wauthor, author_width, author, limit, col_tab_align);
1278 static const struct got_error *
1279 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1280 struct got_object_id *id, struct got_reflist_head *refs,
1281 const size_t date_display_cols, int author_display_cols,
1282 struct tog_colors *colors)
1284 const struct got_error *err = NULL;
1285 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1286 char *logmsg0 = NULL, *logmsg = NULL;
1287 char *author = NULL;
1288 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1289 int author_width, logmsg_width;
1290 char *newline, *line = NULL;
1292 const int avail = view->ncols;
1294 time_t committer_time;
1295 struct tog_color *tc;
1297 committer_time = got_object_commit_get_committer_time(commit);
1298 if (localtime_r(&committer_time, &tm) == NULL)
1299 return got_error_from_errno("localtime_r");
1300 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1302 return got_error(GOT_ERR_NO_SPACE);
1304 if (avail <= date_display_cols)
1305 limit = MIN(sizeof(datebuf) - 1, avail);
1307 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1308 tc = get_color(colors, TOG_COLOR_DATE);
1310 wattr_on(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1312 waddnstr(view->window, datebuf, limit);
1314 wattr_off(view->window,
1315 COLOR_PAIR(tc->colorpair), NULL);
1322 err = got_object_id_str(&id_str, id);
1325 tc = get_color(colors, TOG_COLOR_COMMIT);
1327 wattr_on(view->window,
1328 COLOR_PAIR(tc->colorpair), NULL);
1329 wprintw(view->window, "%.8s ", id_str);
1331 wattr_off(view->window,
1332 COLOR_PAIR(tc->colorpair), NULL);
1339 author = strdup(got_object_commit_get_author(commit));
1340 if (author == NULL) {
1341 err = got_error_from_errno("strdup");
1344 err = format_author(&wauthor, &author_width, author, avail - col, col);
1347 tc = get_color(colors, TOG_COLOR_AUTHOR);
1349 wattr_on(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1351 waddwstr(view->window, wauthor);
1353 wattr_off(view->window,
1354 COLOR_PAIR(tc->colorpair), NULL);
1355 col += author_width;
1356 while (col < avail && author_width < author_display_cols + 2) {
1357 waddch(view->window, ' ');
1364 err = got_object_commit_get_logmsg(&logmsg0, commit);
1368 while (*logmsg == '\n')
1370 newline = strchr(logmsg, '\n');
1373 limit = avail - col;
1374 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1377 waddwstr(view->window, wlogmsg);
1378 col += logmsg_width;
1379 while (col < avail) {
1380 waddch(view->window, ' ');
1392 static struct commit_queue_entry *
1393 alloc_commit_queue_entry(struct got_commit_object *commit,
1394 struct got_object_id *id)
1396 struct commit_queue_entry *entry;
1398 entry = calloc(1, sizeof(*entry));
1403 entry->commit = commit;
1408 pop_commit(struct commit_queue *commits)
1410 struct commit_queue_entry *entry;
1412 entry = TAILQ_FIRST(&commits->head);
1413 TAILQ_REMOVE(&commits->head, entry, entry);
1414 got_object_commit_close(entry->commit);
1415 commits->ncommits--;
1416 /* Don't free entry->id! It is owned by the commit graph. */
1421 free_commits(struct commit_queue *commits)
1423 while (!TAILQ_EMPTY(&commits->head))
1424 pop_commit(commits);
1427 static const struct got_error *
1428 match_commit(int *have_match, struct got_object_id *id,
1429 struct got_commit_object *commit, regex_t *regex)
1431 const struct got_error *err = NULL;
1432 regmatch_t regmatch;
1433 char *id_str = NULL, *logmsg = NULL;
1437 err = got_object_id_str(&id_str, id);
1441 err = got_object_commit_get_logmsg(&logmsg, commit);
1445 if (regexec(regex, got_object_commit_get_author(commit), 1,
1446 ®match, 0) == 0 ||
1447 regexec(regex, got_object_commit_get_committer(commit), 1,
1448 ®match, 0) == 0 ||
1449 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1450 regexec(regex, logmsg, 1, ®match, 0) == 0)
1458 static const struct got_error *
1459 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1460 int minqueue, struct got_repository *repo, const char *path,
1461 int *searching, int *search_next_done, regex_t *regex)
1463 const struct got_error *err = NULL;
1467 * We keep all commits open throughout the lifetime of the log
1468 * view in order to avoid having to re-fetch commits from disk
1469 * while updating the display.
1471 while (nqueued < minqueue ||
1472 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1473 struct got_object_id *id;
1474 struct got_commit_object *commit;
1475 struct commit_queue_entry *entry;
1478 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1479 if (err || id == NULL)
1482 err = got_object_open_as_commit(&commit, repo, id);
1485 entry = alloc_commit_queue_entry(commit, id);
1486 if (entry == NULL) {
1487 err = got_error_from_errno("alloc_commit_queue_entry");
1491 errcode = pthread_mutex_lock(&tog_mutex);
1493 err = got_error_set_errno(errcode,
1494 "pthread_mutex_lock");
1498 entry->idx = commits->ncommits;
1499 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1501 commits->ncommits++;
1503 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1505 err = match_commit(&have_match, id, commit, regex);
1509 *search_next_done = TOG_SEARCH_HAVE_MORE;
1512 errcode = pthread_mutex_unlock(&tog_mutex);
1513 if (errcode && err == NULL)
1514 err = got_error_set_errno(errcode,
1515 "pthread_mutex_unlock");
1523 static const struct got_error *
1524 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1525 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1526 struct commit_queue *commits, int selected_idx, int limit,
1527 struct got_reflist_head *refs, const char *path, int commits_needed,
1528 struct tog_colors *colors)
1530 const struct got_error *err = NULL;
1531 struct tog_log_view_state *s = &view->state.log;
1532 struct commit_queue_entry *entry;
1534 int ncommits, author_cols = 4;
1535 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1536 char *refs_str = NULL;
1538 struct tog_color *tc;
1539 static const size_t date_display_cols = 12;
1544 if (ncommits == selected_idx) {
1548 entry = TAILQ_NEXT(entry, entry);
1552 if (*selected && !(view->searching && view->search_next_done == 0)) {
1553 err = got_object_id_str(&id_str, (*selected)->id);
1557 err = build_refs_str(&refs_str, refs, (*selected)->id,
1564 if (commits_needed == 0)
1565 halfdelay(10); /* disable fast refresh */
1567 if (commits_needed > 0) {
1568 if (asprintf(&ncommits_str, " [%d/%d] %s",
1569 entry ? entry->idx + 1 : 0, commits->ncommits,
1570 (view->searching && !view->search_next_done) ?
1571 "searching..." : "loading...") == -1) {
1572 err = got_error_from_errno("asprintf");
1576 const char *search_str = NULL;
1578 if (view->searching) {
1579 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1580 search_str = "no more matches";
1581 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1582 search_str = "no matches found";
1583 else if (!view->search_next_done)
1584 search_str = "searching...";
1587 if (asprintf(&ncommits_str, " [%d/%d] %s",
1588 entry ? entry->idx + 1 : 0, commits->ncommits,
1589 search_str ? search_str :
1590 (refs_str ? refs_str : "")) == -1) {
1591 err = got_error_from_errno("asprintf");
1596 if (path && strcmp(path, "/") != 0) {
1597 if (asprintf(&header, "commit %s %s%s",
1598 id_str ? id_str : "........................................",
1599 path, ncommits_str) == -1) {
1600 err = got_error_from_errno("asprintf");
1604 } else if (asprintf(&header, "commit %s%s",
1605 id_str ? id_str : "........................................",
1606 ncommits_str) == -1) {
1607 err = got_error_from_errno("asprintf");
1611 err = format_line(&wline, &width, header, view->ncols, 0);
1615 werase(view->window);
1617 if (view_needs_focus_indication(view))
1618 wstandout(view->window);
1619 tc = get_color(colors, TOG_COLOR_COMMIT);
1621 wattr_on(view->window,
1622 COLOR_PAIR(tc->colorpair), NULL);
1623 waddwstr(view->window, wline);
1625 wattr_off(view->window,
1626 COLOR_PAIR(tc->colorpair), NULL);
1627 while (width < view->ncols) {
1628 waddch(view->window, ' ');
1631 if (view_needs_focus_indication(view))
1632 wstandend(view->window);
1637 /* Grow author column size if necessary. */
1644 if (ncommits >= limit - 1)
1646 author = strdup(got_object_commit_get_author(entry->commit));
1647 if (author == NULL) {
1648 err = got_error_from_errno("strdup");
1651 err = format_author(&wauthor, &width, author, COLS,
1653 if (author_cols < width)
1654 author_cols = width;
1658 entry = TAILQ_NEXT(entry, entry);
1665 if (ncommits >= limit - 1)
1667 if (ncommits == selected_idx)
1668 wstandout(view->window);
1669 err = draw_commit(view, entry->commit, entry->id, refs,
1670 date_display_cols, author_cols, colors);
1671 if (ncommits == selected_idx)
1672 wstandend(view->window);
1677 entry = TAILQ_NEXT(entry, entry);
1690 scroll_up(struct tog_view *view,
1691 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1692 struct commit_queue *commits)
1694 struct commit_queue_entry *entry;
1697 entry = TAILQ_FIRST(&commits->head);
1698 if (*first_displayed_entry == entry)
1701 entry = *first_displayed_entry;
1702 while (entry && nscrolled < maxscroll) {
1703 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1705 *first_displayed_entry = entry;
1711 static const struct got_error *
1712 trigger_log_thread(struct tog_view *log_view, int wait,
1713 int *commits_needed, int *log_complete,
1714 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1718 halfdelay(1); /* fast refresh while loading commits */
1720 while (*commits_needed > 0) {
1724 /* Wake the log thread. */
1725 errcode = pthread_cond_signal(need_commits);
1727 return got_error_set_errno(errcode,
1728 "pthread_cond_signal");
1731 * The mutex will be released while the view loop waits
1732 * in wgetch(), at which time the log thread will run.
1737 /* Display progress update in log view. */
1738 show_log_view(log_view);
1742 /* Wait right here while next commit is being loaded. */
1743 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1745 return got_error_set_errno(errcode,
1746 "pthread_cond_wait");
1748 /* Display progress update in log view. */
1749 show_log_view(log_view);
1757 static const struct got_error *
1758 scroll_down(struct tog_view *log_view,
1759 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1760 struct commit_queue_entry **last_displayed_entry,
1761 struct commit_queue *commits, int *log_complete, int *commits_needed,
1762 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1764 const struct got_error *err = NULL;
1765 struct commit_queue_entry *pentry;
1766 int nscrolled = 0, ncommits_needed;
1768 if (*last_displayed_entry == NULL)
1771 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1772 if (commits->ncommits < ncommits_needed && !*log_complete) {
1774 * Ask the log thread for required amount of commits.
1776 (*commits_needed) += maxscroll;
1777 err = trigger_log_thread(log_view, 1, commits_needed,
1778 log_complete, need_commits, commit_loaded);
1784 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1788 *last_displayed_entry = pentry;
1790 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1793 *first_displayed_entry = pentry;
1794 } while (++nscrolled < maxscroll);
1799 static const struct got_error *
1800 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1801 struct got_commit_object *commit, struct got_object_id *commit_id,
1802 struct tog_view *log_view, struct got_repository *repo)
1804 const struct got_error *err;
1805 struct got_object_qid *parent_id;
1806 struct tog_view *diff_view;
1808 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1809 if (diff_view == NULL)
1810 return got_error_from_errno("view_open");
1812 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1813 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1814 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1816 *new_view = diff_view;
1820 static const struct got_error *
1821 tree_view_visit_subtree(struct got_tree_object *subtree,
1822 struct tog_tree_view_state *s)
1824 struct tog_parent_tree *parent;
1826 parent = calloc(1, sizeof(*parent));
1828 return got_error_from_errno("calloc");
1830 parent->tree = s->tree;
1831 parent->first_displayed_entry = s->first_displayed_entry;
1832 parent->selected_entry = s->selected_entry;
1833 parent->selected = s->selected;
1834 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1837 s->first_displayed_entry = NULL;
1841 static const struct got_error *
1842 tree_view_walk_path(struct tog_tree_view_state *s,
1843 struct got_object_id *commit_id,
1844 const char *path, struct got_repository *repo)
1846 const struct got_error *err = NULL;
1847 struct got_tree_object *tree = NULL;
1849 char *slash, *subpath = NULL;
1851 /* Walk the path and open corresponding tree objects. */
1854 struct got_tree_entry *te;
1855 struct got_object_id *tree_id;
1861 /* Ensure the correct subtree entry is selected. */
1862 slash = strchr(p, '/');
1864 te_name = strdup(p);
1866 te_name = strndup(p, slash - p);
1867 if (te_name == NULL) {
1868 err = got_error_from_errno("strndup");
1871 te = got_object_tree_find_entry(s->tree, te_name);
1873 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1878 s->selected_entry = te;
1879 s->selected = got_tree_entry_get_index(te);
1880 if (s->tree != s->root)
1881 s->selected++; /* skip '..' */
1883 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1884 /* Jump to this file's entry. */
1885 s->first_displayed_entry = s->selected_entry;
1890 slash = strchr(p, '/');
1892 subpath = strndup(path, slash - path);
1894 subpath = strdup(path);
1895 if (subpath == NULL) {
1896 err = got_error_from_errno("strdup");
1900 err = got_object_id_by_path(&tree_id, repo, commit_id,
1905 err = got_object_open_as_tree(&tree, repo, tree_id);
1910 err = tree_view_visit_subtree(tree, s);
1912 got_object_tree_close(tree);
1926 static const struct got_error *
1927 browse_commit_tree(struct tog_view **new_view, int begin_x,
1928 struct commit_queue_entry *entry, const char *path,
1929 struct got_repository *repo)
1931 const struct got_error *err = NULL;
1932 struct got_tree_object *tree;
1933 struct tog_tree_view_state *s;
1934 struct tog_view *tree_view;
1936 err = got_object_open_as_tree(&tree, repo,
1937 got_object_commit_get_tree_id(entry->commit));
1941 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1942 if (tree_view == NULL)
1943 return got_error_from_errno("view_open");
1945 err = open_tree_view(tree_view, tree, entry->id, repo);
1947 got_object_tree_close(tree);
1950 s = &tree_view->state.tree;
1952 *new_view = tree_view;
1954 if (got_path_is_root_dir(path))
1957 return tree_view_walk_path(s, entry->id, path, repo);
1960 static const struct got_error *
1961 block_signals_used_by_main_thread(void)
1966 if (sigemptyset(&sigset) == -1)
1967 return got_error_from_errno("sigemptyset");
1969 /* tog handles SIGWINCH and SIGCONT */
1970 if (sigaddset(&sigset, SIGWINCH) == -1)
1971 return got_error_from_errno("sigaddset");
1972 if (sigaddset(&sigset, SIGCONT) == -1)
1973 return got_error_from_errno("sigaddset");
1975 /* ncurses handles SIGTSTP */
1976 if (sigaddset(&sigset, SIGTSTP) == -1)
1977 return got_error_from_errno("sigaddset");
1979 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1981 return got_error_set_errno(errcode, "pthread_sigmask");
1987 log_thread(void *arg)
1989 const struct got_error *err = NULL;
1991 struct tog_log_thread_args *a = arg;
1994 err = block_signals_used_by_main_thread();
1998 while (!done && !err && !tog_sigpipe_received) {
1999 err = queue_commits(a->graph, a->commits, 1, a->repo,
2000 a->in_repo_path, a->searching, a->search_next_done,
2003 if (err->code != GOT_ERR_ITER_COMPLETED)
2007 } else if (a->commits_needed > 0)
2008 a->commits_needed--;
2010 errcode = pthread_mutex_lock(&tog_mutex);
2012 err = got_error_set_errno(errcode,
2013 "pthread_mutex_lock");
2015 } else if (*a->quit)
2017 else if (*a->first_displayed_entry == NULL) {
2018 *a->first_displayed_entry =
2019 TAILQ_FIRST(&a->commits->head);
2020 *a->selected_entry = *a->first_displayed_entry;
2023 errcode = pthread_cond_signal(&a->commit_loaded);
2025 err = got_error_set_errno(errcode,
2026 "pthread_cond_signal");
2027 pthread_mutex_unlock(&tog_mutex);
2032 a->commits_needed = 0;
2034 if (a->commits_needed == 0) {
2035 errcode = pthread_cond_wait(&a->need_commits,
2038 err = got_error_set_errno(errcode,
2039 "pthread_cond_wait");
2043 errcode = pthread_mutex_unlock(&tog_mutex);
2044 if (errcode && err == NULL)
2045 err = got_error_set_errno(errcode,
2046 "pthread_mutex_unlock");
2048 a->log_complete = 1;
2052 static const struct got_error *
2053 stop_log_thread(struct tog_log_view_state *s)
2055 const struct got_error *err = NULL;
2060 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2062 return got_error_set_errno(errcode,
2063 "pthread_cond_signal");
2064 errcode = pthread_mutex_unlock(&tog_mutex);
2066 return got_error_set_errno(errcode,
2067 "pthread_mutex_unlock");
2068 errcode = pthread_join(s->thread, (void **)&err);
2070 return got_error_set_errno(errcode, "pthread_join");
2071 errcode = pthread_mutex_lock(&tog_mutex);
2073 return got_error_set_errno(errcode,
2074 "pthread_mutex_lock");
2078 if (s->thread_args.repo) {
2079 got_repo_close(s->thread_args.repo);
2080 s->thread_args.repo = NULL;
2083 if (s->thread_args.graph) {
2084 got_commit_graph_close(s->thread_args.graph);
2085 s->thread_args.graph = NULL;
2091 static const struct got_error *
2092 close_log_view(struct tog_view *view)
2094 const struct got_error *err = NULL;
2095 struct tog_log_view_state *s = &view->state.log;
2098 err = stop_log_thread(s);
2100 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2101 if (errcode && err == NULL)
2102 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2104 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2105 if (errcode && err == NULL)
2106 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2108 free_commits(&s->commits);
2109 free(s->in_repo_path);
2110 s->in_repo_path = NULL;
2113 got_ref_list_free(&s->refs);
2117 static const struct got_error *
2118 search_start_log_view(struct tog_view *view)
2120 struct tog_log_view_state *s = &view->state.log;
2122 s->matched_entry = NULL;
2123 s->search_entry = NULL;
2127 static const struct got_error *
2128 search_next_log_view(struct tog_view *view)
2130 const struct got_error *err = NULL;
2131 struct tog_log_view_state *s = &view->state.log;
2132 struct commit_queue_entry *entry;
2134 /* Display progress update in log view. */
2135 show_log_view(view);
2139 if (s->search_entry) {
2141 errcode = pthread_mutex_unlock(&tog_mutex);
2143 return got_error_set_errno(errcode,
2144 "pthread_mutex_unlock");
2145 ch = wgetch(view->window);
2146 errcode = pthread_mutex_lock(&tog_mutex);
2148 return got_error_set_errno(errcode,
2149 "pthread_mutex_lock");
2150 if (ch == KEY_BACKSPACE) {
2151 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2154 if (view->searching == TOG_SEARCH_FORWARD)
2155 entry = TAILQ_NEXT(s->search_entry, entry);
2157 entry = TAILQ_PREV(s->search_entry,
2158 commit_queue_head, entry);
2159 } else if (s->matched_entry) {
2160 if (view->searching == TOG_SEARCH_FORWARD)
2161 entry = TAILQ_NEXT(s->matched_entry, entry);
2163 entry = TAILQ_PREV(s->matched_entry,
2164 commit_queue_head, entry);
2166 if (view->searching == TOG_SEARCH_FORWARD)
2167 entry = TAILQ_FIRST(&s->commits.head);
2169 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2175 if (entry == NULL) {
2176 if (s->thread_args.log_complete ||
2177 view->searching == TOG_SEARCH_BACKWARD) {
2178 view->search_next_done =
2179 (s->matched_entry == NULL ?
2180 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2181 s->search_entry = NULL;
2185 * Poke the log thread for more commits and return,
2186 * allowing the main loop to make progress. Search
2187 * will resume at s->search_entry once we come back.
2189 s->thread_args.commits_needed++;
2190 return trigger_log_thread(view, 0,
2191 &s->thread_args.commits_needed,
2192 &s->thread_args.log_complete,
2193 &s->thread_args.need_commits,
2194 &s->thread_args.commit_loaded);
2197 err = match_commit(&have_match, entry->id, entry->commit,
2202 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2203 s->matched_entry = entry;
2207 s->search_entry = entry;
2208 if (view->searching == TOG_SEARCH_FORWARD)
2209 entry = TAILQ_NEXT(entry, entry);
2211 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2214 if (s->matched_entry) {
2215 int cur = s->selected_entry->idx;
2216 while (cur < s->matched_entry->idx) {
2217 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2222 while (cur > s->matched_entry->idx) {
2223 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2230 s->search_entry = NULL;
2235 static const struct got_error *
2236 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2237 struct got_repository *repo, const char *head_ref_name,
2238 const char *in_repo_path, int log_branches)
2240 const struct got_error *err = NULL;
2241 struct tog_log_view_state *s = &view->state.log;
2242 struct got_repository *thread_repo = NULL;
2243 struct got_commit_graph *thread_graph = NULL;
2246 SIMPLEQ_INIT(&s->refs);
2248 if (in_repo_path != s->in_repo_path) {
2249 free(s->in_repo_path);
2250 s->in_repo_path = strdup(in_repo_path);
2251 if (s->in_repo_path == NULL)
2252 return got_error_from_errno("strdup");
2255 /* The commit queue only contains commits being displayed. */
2256 TAILQ_INIT(&s->commits.head);
2257 s->commits.ncommits = 0;
2259 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2264 s->head_ref_name = head_ref_name;
2265 s->start_id = got_object_id_dup(start_id);
2266 if (s->start_id == NULL) {
2267 err = got_error_from_errno("got_object_id_dup");
2270 s->log_branches = log_branches;
2272 SIMPLEQ_INIT(&s->colors);
2273 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2274 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2275 get_color_value("TOG_COLOR_COMMIT"));
2278 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2279 get_color_value("TOG_COLOR_AUTHOR"));
2281 free_colors(&s->colors);
2284 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2285 get_color_value("TOG_COLOR_DATE"));
2287 free_colors(&s->colors);
2292 view->show = show_log_view;
2293 view->input = input_log_view;
2294 view->close = close_log_view;
2295 view->search_start = search_start_log_view;
2296 view->search_next = search_next_log_view;
2298 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2301 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2305 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2306 s->repo, NULL, NULL);
2310 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2312 err = got_error_set_errno(errcode, "pthread_cond_init");
2315 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2317 err = got_error_set_errno(errcode, "pthread_cond_init");
2321 s->thread_args.commits_needed = view->nlines;
2322 s->thread_args.graph = thread_graph;
2323 s->thread_args.commits = &s->commits;
2324 s->thread_args.in_repo_path = s->in_repo_path;
2325 s->thread_args.start_id = s->start_id;
2326 s->thread_args.repo = thread_repo;
2327 s->thread_args.log_complete = 0;
2328 s->thread_args.quit = &s->quit;
2329 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2330 s->thread_args.selected_entry = &s->selected_entry;
2331 s->thread_args.searching = &view->searching;
2332 s->thread_args.search_next_done = &view->search_next_done;
2333 s->thread_args.regex = &view->regex;
2336 close_log_view(view);
2340 static const struct got_error *
2341 show_log_view(struct tog_view *view)
2343 const struct got_error *err;
2344 struct tog_log_view_state *s = &view->state.log;
2346 if (s->thread == NULL) {
2347 int errcode = pthread_create(&s->thread, NULL, log_thread,
2350 return got_error_set_errno(errcode, "pthread_create");
2351 if (s->thread_args.commits_needed > 0) {
2352 err = trigger_log_thread(view, 1,
2353 &s->thread_args.commits_needed,
2354 &s->thread_args.log_complete,
2355 &s->thread_args.need_commits,
2356 &s->thread_args.commit_loaded);
2362 return draw_commits(view, &s->last_displayed_entry,
2363 &s->selected_entry, s->first_displayed_entry,
2364 &s->commits, s->selected, view->nlines, &s->refs,
2365 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2368 static const struct got_error *
2369 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2370 struct tog_view **focus_view, struct tog_view *view, int ch)
2372 const struct got_error *err = NULL;
2373 struct tog_log_view_state *s = &view->state.log;
2374 char *parent_path, *in_repo_path = NULL;
2375 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2376 struct tog_view *ref_view = NULL;
2378 struct got_object_id *start_id;
2388 if (s->first_displayed_entry == NULL)
2390 if (s->selected > 0)
2393 scroll_up(view, &s->first_displayed_entry, 1,
2398 if (s->first_displayed_entry == NULL)
2400 if (TAILQ_FIRST(&s->commits.head) ==
2401 s->first_displayed_entry) {
2405 scroll_up(view, &s->first_displayed_entry,
2406 view->nlines - 1, &s->commits);
2412 if (s->first_displayed_entry == NULL)
2414 if (s->selected < MIN(view->nlines - 2,
2415 s->commits.ncommits - 1)) {
2419 err = scroll_down(view, &s->first_displayed_entry, 1,
2420 &s->last_displayed_entry, &s->commits,
2421 &s->thread_args.log_complete,
2422 &s->thread_args.commits_needed,
2423 &s->thread_args.need_commits,
2424 &s->thread_args.commit_loaded);
2428 struct commit_queue_entry *first;
2429 first = s->first_displayed_entry;
2432 err = scroll_down(view, &s->first_displayed_entry,
2433 view->nlines - 1, &s->last_displayed_entry,
2434 &s->commits, &s->thread_args.log_complete,
2435 &s->thread_args.commits_needed,
2436 &s->thread_args.need_commits,
2437 &s->thread_args.commit_loaded);
2440 if (first == s->first_displayed_entry &&
2441 s->selected < MIN(view->nlines - 2,
2442 s->commits.ncommits - 1)) {
2443 /* can't scroll further down */
2444 s->selected = MIN(view->nlines - 2,
2445 s->commits.ncommits - 1);
2451 if (s->selected > view->nlines - 2)
2452 s->selected = view->nlines - 2;
2453 if (s->selected > s->commits.ncommits - 1)
2454 s->selected = s->commits.ncommits - 1;
2459 if (s->selected_entry == NULL)
2461 if (view_is_parent_view(view))
2462 begin_x = view_split_begin_x(view->begin_x);
2463 err = open_diff_view_for_commit(&diff_view, begin_x,
2464 s->selected_entry->commit, s->selected_entry->id,
2468 if (view_is_parent_view(view)) {
2469 err = view_close_child(view);
2472 err = view_set_child(view, diff_view);
2474 view_close(diff_view);
2477 *focus_view = diff_view;
2478 view->child_focussed = 1;
2480 *new_view = diff_view;
2483 if (s->selected_entry == NULL)
2485 if (view_is_parent_view(view))
2486 begin_x = view_split_begin_x(view->begin_x);
2487 err = browse_commit_tree(&tree_view, begin_x,
2488 s->selected_entry, s->in_repo_path, s->repo);
2491 if (view_is_parent_view(view)) {
2492 err = view_close_child(view);
2495 err = view_set_child(view, tree_view);
2497 view_close(tree_view);
2500 *focus_view = tree_view;
2501 view->child_focussed = 1;
2503 *new_view = tree_view;
2506 if (got_path_cmp(s->in_repo_path, "/",
2507 strlen(s->in_repo_path), 1) == 0)
2509 err = got_path_dirname(&parent_path, s->in_repo_path);
2512 err = stop_log_thread(s);
2517 lv = view_open(view->nlines, view->ncols,
2518 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2521 return got_error_from_errno("view_open");
2523 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2524 parent_path, s->log_branches);
2528 if (view_is_parent_view(view))
2531 view_set_child(view->parent, lv);
2536 err = stop_log_thread(s);
2539 lv = view_open(view->nlines, view->ncols,
2540 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2542 return got_error_from_errno("view_open");
2543 err = got_repo_match_object_id(&start_id, NULL,
2544 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2545 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2550 in_repo_path = strdup(s->in_repo_path);
2551 if (in_repo_path == NULL) {
2554 return got_error_from_errno("strdup");
2556 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2557 in_repo_path, s->log_branches);
2567 s->log_branches = !s->log_branches;
2568 err = stop_log_thread(s);
2571 lv = view_open(view->nlines, view->ncols,
2572 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2574 return got_error_from_errno("view_open");
2575 err = open_log_view(lv, s->start_id, s->repo,
2576 s->head_ref_name, s->in_repo_path, s->log_branches);
2585 if (view_is_parent_view(view))
2586 begin_x = view_split_begin_x(view->begin_x);
2587 ref_view = view_open(view->nlines, view->ncols,
2588 view->begin_y, begin_x, TOG_VIEW_REF);
2589 if (ref_view == NULL)
2590 return got_error_from_errno("view_open");
2591 err = open_ref_view(ref_view, s->repo);
2593 view_close(ref_view);
2596 if (view_is_parent_view(view)) {
2597 err = view_close_child(view);
2600 err = view_set_child(view, ref_view);
2602 view_close(ref_view);
2605 *focus_view = ref_view;
2606 view->child_focussed = 1;
2608 *new_view = ref_view;
2617 static const struct got_error *
2618 apply_unveil(const char *repo_path, const char *worktree_path)
2620 const struct got_error *error;
2623 if (unveil("gmon.out", "rwc") != 0)
2624 return got_error_from_errno2("unveil", "gmon.out");
2626 if (repo_path && unveil(repo_path, "r") != 0)
2627 return got_error_from_errno2("unveil", repo_path);
2629 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2630 return got_error_from_errno2("unveil", worktree_path);
2632 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2633 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2635 error = got_privsep_unveil_exec_helpers();
2639 if (unveil(NULL, NULL) != 0)
2640 return got_error_from_errno("unveil");
2650 halfdelay(1); /* Do fast refresh while initial view is loading. */
2653 intrflush(stdscr, FALSE);
2654 keypad(stdscr, TRUE);
2656 if (getenv("TOG_COLORS") != NULL) {
2658 use_default_colors();
2660 signal(SIGWINCH, tog_sigwinch);
2661 signal(SIGPIPE, tog_sigpipe);
2662 signal(SIGCONT, tog_sigcont);
2665 static const struct got_error *
2666 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2667 struct got_repository *repo, struct got_worktree *worktree)
2669 const struct got_error *err = NULL;
2672 *in_repo_path = strdup("/");
2673 if (*in_repo_path == NULL)
2674 return got_error_from_errno("strdup");
2679 const char *prefix = got_worktree_get_path_prefix(worktree);
2682 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2685 if (asprintf(in_repo_path, "%s%s%s", prefix,
2686 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2688 err = got_error_from_errno("asprintf");
2689 *in_repo_path = NULL;
2693 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2698 static const struct got_error *
2699 cmd_log(int argc, char *argv[])
2701 const struct got_error *error;
2702 struct got_repository *repo = NULL;
2703 struct got_worktree *worktree = NULL;
2704 struct got_object_id *start_id = NULL;
2705 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2706 char *start_commit = NULL, *head_ref_name = NULL;
2707 int ch, log_branches = 0;
2708 struct tog_view *view;
2711 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2716 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2722 start_commit = optarg;
2725 repo_path = realpath(optarg, NULL);
2726 if (repo_path == NULL)
2727 return got_error_from_errno2("realpath",
2742 cwd = getcwd(NULL, 0);
2744 return got_error_from_errno("getcwd");
2746 error = got_worktree_open(&worktree, cwd);
2747 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2750 if (repo_path == NULL) {
2753 strdup(got_worktree_get_repo_path(worktree));
2755 repo_path = strdup(cwd);
2757 if (repo_path == NULL) {
2758 error = got_error_from_errno("strdup");
2762 error = got_repo_open(&repo, repo_path, NULL);
2766 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2773 error = apply_unveil(got_repo_get_path(repo),
2774 worktree ? got_worktree_get_root_path(worktree) : NULL);
2778 if (start_commit == NULL)
2779 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2780 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2781 GOT_OBJ_TYPE_COMMIT, 1, repo);
2783 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2784 GOT_OBJ_TYPE_COMMIT, 1, repo);
2788 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2790 error = got_error_from_errno("view_open");
2794 head_ref_name = strdup(
2795 got_worktree_get_head_ref_name(worktree));
2796 if (head_ref_name == NULL) {
2797 error = got_error_from_errno("strdup");
2801 error = open_log_view(view, start_id, repo, head_ref_name,
2802 in_repo_path, log_branches);
2806 /* Release work tree lock. */
2807 got_worktree_close(worktree);
2810 error = view_loop(view);
2816 free(head_ref_name);
2818 got_repo_close(repo);
2820 got_worktree_close(worktree);
2828 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2829 "[-w] object1 object2\n", getprogname());
2834 parse_next_line(FILE *f, size_t *len)
2839 const char delim[3] = { '\0', '\0', '\0'};
2841 line = fparseln(f, &linelen, &lineno, delim, 0);
2848 match_line(const char *line, regex_t *regex, size_t nmatch,
2849 regmatch_t *regmatch)
2851 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2855 match_color(struct tog_colors *colors, const char *line)
2857 struct tog_color *tc = NULL;
2859 SIMPLEQ_FOREACH(tc, colors, entry) {
2860 if (match_line(line, &tc->regex, 0, NULL))
2867 static const struct got_error *
2868 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2869 WINDOW *window, regmatch_t *regmatch)
2871 const struct got_error *err = NULL;
2878 s = strndup(line, regmatch->rm_so);
2880 return got_error_from_errno("strndup");
2882 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2887 waddwstr(window, wline);
2894 s = strndup(line + regmatch->rm_so,
2895 regmatch->rm_eo - regmatch->rm_so);
2897 err = got_error_from_errno("strndup");
2901 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2906 wattr_on(window, A_STANDOUT, NULL);
2907 waddwstr(window, wline);
2908 wattr_off(window, A_STANDOUT, NULL);
2915 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2916 err = format_line(&wline, &width,
2917 line + regmatch->rm_eo, wlimit, col_tab_align);
2920 waddwstr(window, wline);
2928 static const struct got_error *
2929 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2930 off_t *line_offsets, int selected_line, int max_lines,
2931 int *last_displayed_line, int *eof, const char *header,
2932 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2934 const struct got_error *err;
2937 struct tog_color *tc;
2943 line_offset = line_offsets[first_displayed_line - 1];
2944 if (fseeko(f, line_offset, SEEK_SET) == -1)
2945 return got_error_from_errno("fseek");
2947 werase(view->window);
2950 if (asprintf(&line, "[%d/%d] %s",
2951 first_displayed_line - 1 + selected_line, nlines,
2953 return got_error_from_errno("asprintf");
2954 err = format_line(&wline, &width, line, view->ncols, 0);
2959 if (view_needs_focus_indication(view))
2960 wstandout(view->window);
2961 waddwstr(view->window, wline);
2964 if (view_needs_focus_indication(view))
2965 wstandend(view->window);
2966 if (width <= view->ncols - 1)
2967 waddch(view->window, '\n');
2975 while (max_lines > 0 && nprinted < max_lines) {
2976 line = parse_next_line(f, &len);
2982 tc = match_color(colors, line);
2984 wattr_on(view->window,
2985 COLOR_PAIR(tc->colorpair), NULL);
2986 if (first_displayed_line + nprinted == matched_line &&
2987 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2988 err = add_matched_line(&width, line, view->ncols, 0,
2989 view->window, regmatch);
2995 err = format_line(&wline, &width, line, view->ncols, 0);
3000 waddwstr(view->window, wline);
3005 wattr_off(view->window,
3006 COLOR_PAIR(tc->colorpair), NULL);
3007 if (width <= view->ncols - 1)
3008 waddch(view->window, '\n');
3013 *last_displayed_line = first_displayed_line + (nprinted - 1);
3015 *last_displayed_line = first_displayed_line;
3020 while (nprinted < view->nlines) {
3021 waddch(view->window, '\n');
3025 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3030 wstandout(view->window);
3031 waddwstr(view->window, wline);
3034 wstandend(view->window);
3041 get_datestr(time_t *time, char *datebuf)
3043 struct tm mytm, *tm;
3046 tm = gmtime_r(time, &mytm);
3049 s = asctime_r(tm, datebuf);
3052 p = strchr(s, '\n');
3058 static const struct got_error *
3059 get_changed_paths(struct got_pathlist_head *paths,
3060 struct got_commit_object *commit, struct got_repository *repo)
3062 const struct got_error *err = NULL;
3063 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3064 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3065 struct got_object_qid *qid;
3067 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3069 struct got_commit_object *pcommit;
3070 err = got_object_open_as_commit(&pcommit, repo,
3075 tree_id1 = got_object_commit_get_tree_id(pcommit);
3076 got_object_commit_close(pcommit);
3081 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3086 tree_id2 = got_object_commit_get_tree_id(commit);
3087 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3091 err = got_diff_tree(tree1, tree2, "", "", repo,
3092 got_diff_tree_collect_changed_paths, paths, 0);
3095 got_object_tree_close(tree1);
3097 got_object_tree_close(tree2);
3101 static const struct got_error *
3102 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3106 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3108 return got_error_from_errno("reallocarray");
3110 (*line_offsets)[*nlines] = off;
3115 static const struct got_error *
3116 write_commit_info(off_t **line_offsets, size_t *nlines,
3117 struct got_object_id *commit_id, struct got_reflist_head *refs,
3118 struct got_repository *repo, FILE *outfile)
3120 const struct got_error *err = NULL;
3121 char datebuf[26], *datestr;
3122 struct got_commit_object *commit;
3123 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3124 time_t committer_time;
3125 const char *author, *committer;
3126 char *refs_str = NULL;
3127 struct got_pathlist_head changed_paths;
3128 struct got_pathlist_entry *pe;
3132 TAILQ_INIT(&changed_paths);
3135 err = build_refs_str(&refs_str, refs, commit_id, repo);
3140 err = got_object_open_as_commit(&commit, repo, commit_id);
3144 err = got_object_id_str(&id_str, commit_id);
3146 err = got_error_from_errno("got_object_id_str");
3150 err = add_line_offset(line_offsets, nlines, 0);
3154 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3155 refs_str ? refs_str : "", refs_str ? ")" : "");
3157 err = got_error_from_errno("fprintf");
3161 err = add_line_offset(line_offsets, nlines, outoff);
3165 n = fprintf(outfile, "from: %s\n",
3166 got_object_commit_get_author(commit));
3168 err = got_error_from_errno("fprintf");
3172 err = add_line_offset(line_offsets, nlines, outoff);
3176 committer_time = got_object_commit_get_committer_time(commit);
3177 datestr = get_datestr(&committer_time, datebuf);
3179 n = fprintf(outfile, "date: %s UTC\n", datestr);
3181 err = got_error_from_errno("fprintf");
3185 err = add_line_offset(line_offsets, nlines, outoff);
3189 author = got_object_commit_get_author(commit);
3190 committer = got_object_commit_get_committer(commit);
3191 if (strcmp(author, committer) != 0) {
3192 n = fprintf(outfile, "via: %s\n", committer);
3194 err = got_error_from_errno("fprintf");
3198 err = add_line_offset(line_offsets, nlines, outoff);
3202 err = got_object_commit_get_logmsg(&logmsg, commit);
3206 while ((line = strsep(&s, "\n")) != NULL) {
3207 n = fprintf(outfile, "%s\n", line);
3209 err = got_error_from_errno("fprintf");
3213 err = add_line_offset(line_offsets, nlines, outoff);
3218 err = get_changed_paths(&changed_paths, commit, repo);
3221 TAILQ_FOREACH(pe, &changed_paths, entry) {
3222 struct got_diff_changed_path *cp = pe->data;
3223 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3225 err = got_error_from_errno("fprintf");
3229 err = add_line_offset(line_offsets, nlines, outoff);
3232 free((char *)pe->path);
3236 fputc('\n', outfile);
3238 err = add_line_offset(line_offsets, nlines, outoff);
3240 got_pathlist_free(&changed_paths);
3244 got_object_commit_close(commit);
3246 free(*line_offsets);
3247 *line_offsets = NULL;
3253 static const struct got_error *
3254 create_diff(struct tog_diff_view_state *s)
3256 const struct got_error *err = NULL;
3260 free(s->line_offsets);
3261 s->line_offsets = malloc(sizeof(off_t));
3262 if (s->line_offsets == NULL)
3263 return got_error_from_errno("malloc");
3268 err = got_error_from_errno("got_opentemp");
3271 if (s->f && fclose(s->f) != 0) {
3272 err = got_error_from_errno("fclose");
3278 err = got_object_get_type(&obj_type, s->repo, s->id1);
3280 err = got_object_get_type(&obj_type, s->repo, s->id2);
3285 case GOT_OBJ_TYPE_BLOB:
3286 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3287 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3288 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3290 case GOT_OBJ_TYPE_TREE:
3291 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3292 s->id1, s->id2, "", "", s->diff_context,
3293 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3295 case GOT_OBJ_TYPE_COMMIT: {
3296 const struct got_object_id_queue *parent_ids;
3297 struct got_object_qid *pid;
3298 struct got_commit_object *commit2;
3300 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3303 /* Show commit info if we're diffing to a parent/root commit. */
3304 if (s->id1 == NULL) {
3305 err = write_commit_info(&s->line_offsets, &s->nlines,
3306 s->id2, &s->refs, s->repo, s->f);
3310 parent_ids = got_object_commit_get_parent_ids(commit2);
3311 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3312 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3313 err = write_commit_info(
3314 &s->line_offsets, &s->nlines,
3315 s->id2, &s->refs, s->repo, s->f);
3322 got_object_commit_close(commit2);
3324 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3325 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3326 s->force_text_diff, s->repo, s->f);
3330 err = got_error(GOT_ERR_OBJ_TYPE);
3336 if (s->f && fflush(s->f) != 0 && err == NULL)
3337 err = got_error_from_errno("fflush");
3342 diff_view_indicate_progress(struct tog_view *view)
3344 mvwaddstr(view->window, 0, 0, "diffing...");
3349 static const struct got_error *
3350 search_start_diff_view(struct tog_view *view)
3352 struct tog_diff_view_state *s = &view->state.diff;
3354 s->matched_line = 0;
3358 static const struct got_error *
3359 search_next_diff_view(struct tog_view *view)
3361 struct tog_diff_view_state *s = &view->state.diff;
3364 if (!view->searching) {
3365 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3369 if (s->matched_line) {
3370 if (view->searching == TOG_SEARCH_FORWARD)
3371 lineno = s->matched_line + 1;
3373 lineno = s->matched_line - 1;
3375 if (view->searching == TOG_SEARCH_FORWARD)
3386 if (lineno <= 0 || lineno > s->nlines) {
3387 if (s->matched_line == 0) {
3388 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3393 if (view->searching == TOG_SEARCH_FORWARD)
3399 offset = s->line_offsets[lineno - 1];
3400 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3402 return got_error_from_errno("fseeko");
3405 line = parse_next_line(s->f, &len);
3407 match_line(line, &view->regex, 1, &view->regmatch)) {
3408 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3409 s->matched_line = lineno;
3414 if (view->searching == TOG_SEARCH_FORWARD)
3420 if (s->matched_line) {
3421 s->first_displayed_line = s->matched_line;
3422 s->selected_line = 1;
3428 static const struct got_error *
3429 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3430 struct got_object_id *id2, const char *label1, const char *label2,
3431 int diff_context, int ignore_whitespace, int force_text_diff,
3432 struct tog_view *log_view, struct got_repository *repo)
3434 const struct got_error *err;
3435 struct tog_diff_view_state *s = &view->state.diff;
3437 SIMPLEQ_INIT(&s->refs);
3439 if (id1 != NULL && id2 != NULL) {
3441 err = got_object_get_type(&type1, repo, id1);
3444 err = got_object_get_type(&type2, repo, id2);
3449 return got_error(GOT_ERR_OBJ_TYPE);
3451 s->first_displayed_line = 1;
3452 s->last_displayed_line = view->nlines;
3453 s->selected_line = 1;
3461 s->id1 = got_object_id_dup(id1);
3463 return got_error_from_errno("got_object_id_dup");
3467 s->id2 = got_object_id_dup(id2);
3468 if (s->id2 == NULL) {
3471 return got_error_from_errno("got_object_id_dup");
3474 s->first_displayed_line = 1;
3475 s->last_displayed_line = view->nlines;
3476 s->diff_context = diff_context;
3477 s->ignore_whitespace = ignore_whitespace;
3478 s->force_text_diff = force_text_diff;
3479 s->log_view = log_view;
3482 SIMPLEQ_INIT(&s->colors);
3483 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3484 err = add_color(&s->colors,
3485 "^-", TOG_COLOR_DIFF_MINUS,
3486 get_color_value("TOG_COLOR_DIFF_MINUS"));
3489 err = add_color(&s->colors, "^\\+",
3490 TOG_COLOR_DIFF_PLUS,
3491 get_color_value("TOG_COLOR_DIFF_PLUS"));
3493 free_colors(&s->colors);
3496 err = add_color(&s->colors,
3497 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3498 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3500 free_colors(&s->colors);
3504 err = add_color(&s->colors,
3505 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3506 TOG_COLOR_DIFF_META,
3507 get_color_value("TOG_COLOR_DIFF_META"));
3509 free_colors(&s->colors);
3513 err = add_color(&s->colors,
3514 "^(from|via): ", TOG_COLOR_AUTHOR,
3515 get_color_value("TOG_COLOR_AUTHOR"));
3517 free_colors(&s->colors);
3521 err = add_color(&s->colors,
3522 "^date: ", TOG_COLOR_DATE,
3523 get_color_value("TOG_COLOR_DATE"));
3525 free_colors(&s->colors);
3530 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3536 free_colors(&s->colors);
3540 if (log_view && view_is_splitscreen(view))
3541 show_log_view(log_view); /* draw vborder */
3542 diff_view_indicate_progress(view);
3544 s->line_offsets = NULL;
3546 err = create_diff(s);
3552 free_colors(&s->colors);
3553 got_ref_list_free(&s->refs);
3557 view->show = show_diff_view;
3558 view->input = input_diff_view;
3559 view->close = close_diff_view;
3560 view->search_start = search_start_diff_view;
3561 view->search_next = search_next_diff_view;
3566 static const struct got_error *
3567 close_diff_view(struct tog_view *view)
3569 const struct got_error *err = NULL;
3570 struct tog_diff_view_state *s = &view->state.diff;
3576 if (s->f && fclose(s->f) == EOF)
3577 err = got_error_from_errno("fclose");
3578 free_colors(&s->colors);
3579 free(s->line_offsets);
3580 s->line_offsets = NULL;
3582 got_ref_list_free(&s->refs);
3586 static const struct got_error *
3587 show_diff_view(struct tog_view *view)
3589 const struct got_error *err;
3590 struct tog_diff_view_state *s = &view->state.diff;
3591 char *id_str1 = NULL, *id_str2, *header;
3592 const char *label1, *label2;
3595 err = got_object_id_str(&id_str1, s->id1);
3598 label1 = s->label1 ? : id_str1;
3600 label1 = "/dev/null";
3602 err = got_object_id_str(&id_str2, s->id2);
3605 label2 = s->label2 ? : id_str2;
3607 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3608 err = got_error_from_errno("asprintf");
3616 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3617 s->line_offsets, s->selected_line, view->nlines,
3618 &s->last_displayed_line, &s->eof, header, &s->colors,
3619 s->matched_line, &view->regmatch);
3622 static const struct got_error *
3623 set_selected_commit(struct tog_diff_view_state *s,
3624 struct commit_queue_entry *entry)
3626 const struct got_error *err;
3627 const struct got_object_id_queue *parent_ids;
3628 struct got_commit_object *selected_commit;
3629 struct got_object_qid *pid;
3632 s->id2 = got_object_id_dup(entry->id);
3634 return got_error_from_errno("got_object_id_dup");
3636 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3639 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3641 pid = SIMPLEQ_FIRST(parent_ids);
3642 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3643 got_object_commit_close(selected_commit);
3647 static const struct got_error *
3648 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3649 struct tog_view **focus_view, struct tog_view *view, int ch)
3651 const struct got_error *err = NULL;
3652 struct tog_diff_view_state *s = &view->state.diff;
3653 struct tog_log_view_state *ls;
3654 struct commit_queue_entry *entry;
3661 s->force_text_diff = !s->force_text_diff;
3663 s->ignore_whitespace = !s->ignore_whitespace;
3664 wclear(view->window);
3665 s->first_displayed_line = 1;
3666 s->last_displayed_line = view->nlines;
3667 diff_view_indicate_progress(view);
3668 err = create_diff(s);
3672 if (s->first_displayed_line > 1)
3673 s->first_displayed_line--;
3677 if (s->first_displayed_line == 1)
3680 while (i++ < view->nlines - 1 &&
3681 s->first_displayed_line > 1)
3682 s->first_displayed_line--;
3687 s->first_displayed_line++;
3695 while (!s->eof && i++ < view->nlines - 1) {
3697 line = parse_next_line(s->f, NULL);
3698 s->first_displayed_line++;
3704 if (s->diff_context > 0) {
3706 diff_view_indicate_progress(view);
3707 err = create_diff(s);
3708 if (s->first_displayed_line + view->nlines - 1 >
3710 s->first_displayed_line = 1;
3711 s->last_displayed_line = view->nlines;
3716 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3718 diff_view_indicate_progress(view);
3719 err = create_diff(s);
3724 if (s->log_view == NULL)
3726 ls = &s->log_view->state.log;
3727 entry = TAILQ_PREV(ls->selected_entry,
3728 commit_queue_head, entry);
3732 err = input_log_view(NULL, NULL, NULL, s->log_view,
3737 err = set_selected_commit(s, entry);
3741 s->first_displayed_line = 1;
3742 s->last_displayed_line = view->nlines;
3744 diff_view_indicate_progress(view);
3745 err = create_diff(s);
3749 if (s->log_view == NULL)
3751 ls = &s->log_view->state.log;
3753 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3754 ls->thread_args.commits_needed++;
3755 err = trigger_log_thread(s->log_view, 1,
3756 &ls->thread_args.commits_needed,
3757 &ls->thread_args.log_complete,
3758 &ls->thread_args.need_commits,
3759 &ls->thread_args.commit_loaded);
3763 err = input_log_view(NULL, NULL, NULL, s->log_view,
3768 entry = TAILQ_NEXT(ls->selected_entry, entry);
3772 err = set_selected_commit(s, entry);
3776 s->first_displayed_line = 1;
3777 s->last_displayed_line = view->nlines;
3779 diff_view_indicate_progress(view);
3780 err = create_diff(s);
3789 static const struct got_error *
3790 cmd_diff(int argc, char *argv[])
3792 const struct got_error *error = NULL;
3793 struct got_repository *repo = NULL;
3794 struct got_worktree *worktree = NULL;
3795 struct got_object_id *id1 = NULL, *id2 = NULL;
3796 char *repo_path = NULL, *cwd = NULL;
3797 char *id_str1 = NULL, *id_str2 = NULL;
3798 char *label1 = NULL, *label2 = NULL;
3799 int diff_context = 3, ignore_whitespace = 0;
3800 int ch, force_text_diff = 0;
3802 struct tog_view *view;
3805 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3809 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3812 force_text_diff = 1;
3815 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3818 err(1, "-C option %s", errstr);
3821 repo_path = realpath(optarg, NULL);
3822 if (repo_path == NULL)
3823 return got_error_from_errno2("realpath",
3825 got_path_strip_trailing_slashes(repo_path);
3828 ignore_whitespace = 1;
3840 usage_diff(); /* TODO show local worktree changes */
3841 } else if (argc == 2) {
3847 cwd = getcwd(NULL, 0);
3849 return got_error_from_errno("getcwd");
3851 error = got_worktree_open(&worktree, cwd);
3852 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3855 if (repo_path == NULL) {
3858 strdup(got_worktree_get_repo_path(worktree));
3860 repo_path = strdup(cwd);
3862 if (repo_path == NULL) {
3863 error = got_error_from_errno("strdup");
3867 error = got_repo_open(&repo, repo_path, NULL);
3873 error = apply_unveil(got_repo_get_path(repo), NULL);
3877 error = got_repo_match_object_id(&id1, &label1, id_str1,
3878 GOT_OBJ_TYPE_ANY, 1, repo);
3882 error = got_repo_match_object_id(&id2, &label2, id_str2,
3883 GOT_OBJ_TYPE_ANY, 1, repo);
3887 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3889 error = got_error_from_errno("view_open");
3892 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3893 ignore_whitespace, force_text_diff, NULL, repo);
3896 error = view_loop(view);
3903 got_repo_close(repo);
3905 got_worktree_close(worktree);
3913 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3918 struct tog_blame_line {
3920 struct got_object_id *id;
3923 static const struct got_error *
3924 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3925 const char *path, struct tog_blame_line *lines, int nlines,
3926 int blame_complete, int selected_line, int *first_displayed_line,
3927 int *last_displayed_line, int *eof, int max_lines,
3928 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3930 const struct got_error *err;
3931 int lineno = 0, nprinted = 0;
3936 struct tog_blame_line *blame_line;
3937 struct got_object_id *prev_id = NULL;
3939 struct tog_color *tc;
3941 err = got_object_id_str(&id_str, id);
3946 werase(view->window);
3948 if (asprintf(&line, "commit %s", id_str) == -1) {
3949 err = got_error_from_errno("asprintf");
3954 err = format_line(&wline, &width, line, view->ncols, 0);
3959 if (view_needs_focus_indication(view))
3960 wstandout(view->window);
3961 tc = get_color(colors, TOG_COLOR_COMMIT);
3963 wattr_on(view->window,
3964 COLOR_PAIR(tc->colorpair), NULL);
3965 waddwstr(view->window, wline);
3967 wattr_off(view->window,
3968 COLOR_PAIR(tc->colorpair), NULL);
3969 if (view_needs_focus_indication(view))
3970 wstandend(view->window);
3973 if (width < view->ncols - 1)
3974 waddch(view->window, '\n');
3976 if (asprintf(&line, "[%d/%d] %s%s",
3977 *first_displayed_line - 1 + selected_line, nlines,
3978 blame_complete ? "" : "annotating... ", path) == -1) {
3980 return got_error_from_errno("asprintf");
3983 err = format_line(&wline, &width, line, view->ncols, 0);
3988 waddwstr(view->window, wline);
3991 if (width < view->ncols - 1)
3992 waddch(view->window, '\n');
3995 while (nprinted < max_lines - 2) {
3996 line = parse_next_line(f, &len);
4001 if (++lineno < *first_displayed_line) {
4006 if (view->focussed && nprinted == selected_line - 1)
4007 wstandout(view->window);
4010 blame_line = &lines[lineno - 1];
4011 if (blame_line->annotated && prev_id &&
4012 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4014 nprinted == selected_line - 1)) {
4015 waddstr(view->window, " ");
4016 } else if (blame_line->annotated) {
4018 err = got_object_id_str(&id_str, blame_line->id);
4023 tc = get_color(colors, TOG_COLOR_COMMIT);
4025 wattr_on(view->window,
4026 COLOR_PAIR(tc->colorpair), NULL);
4027 wprintw(view->window, "%.8s", id_str);
4029 wattr_off(view->window,
4030 COLOR_PAIR(tc->colorpair), NULL);
4032 prev_id = blame_line->id;
4034 waddstr(view->window, "........");
4038 waddstr(view->window, "........");
4042 if (view->focussed && nprinted == selected_line - 1)
4043 wstandend(view->window);
4044 waddstr(view->window, " ");
4046 if (view->ncols <= 9) {
4048 wline = wcsdup(L"");
4049 if (wline == NULL) {
4050 err = got_error_from_errno("wcsdup");
4054 } else if (*first_displayed_line + nprinted == matched_line &&
4055 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4056 err = add_matched_line(&width, line, view->ncols - 9, 9,
4057 view->window, regmatch);
4064 err = format_line(&wline, &width, line,
4065 view->ncols - 9, 9);
4066 waddwstr(view->window, wline);
4072 if (width <= view->ncols - 1)
4073 waddch(view->window, '\n');
4074 if (++nprinted == 1)
4075 *first_displayed_line = lineno;
4078 *last_displayed_line = lineno;
4085 static const struct got_error *
4086 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4088 const struct got_error *err = NULL;
4089 struct tog_blame_cb_args *a = arg;
4090 struct tog_blame_line *line;
4093 if (nlines != a->nlines ||
4094 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4095 return got_error(GOT_ERR_RANGE);
4097 errcode = pthread_mutex_lock(&tog_mutex);
4099 return got_error_set_errno(errcode, "pthread_mutex_lock");
4101 if (*a->quit) { /* user has quit the blame view */
4102 err = got_error(GOT_ERR_ITER_COMPLETED);
4107 goto done; /* no change in this commit */
4109 line = &a->lines[lineno - 1];
4110 if (line->annotated)
4113 line->id = got_object_id_dup(id);
4114 if (line->id == NULL) {
4115 err = got_error_from_errno("got_object_id_dup");
4118 line->annotated = 1;
4120 errcode = pthread_mutex_unlock(&tog_mutex);
4122 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4127 blame_thread(void *arg)
4129 const struct got_error *err;
4130 struct tog_blame_thread_args *ta = arg;
4131 struct tog_blame_cb_args *a = ta->cb_args;
4134 err = block_signals_used_by_main_thread();
4138 err = got_blame(ta->path, a->commit_id, ta->repo,
4139 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4140 if (err && err->code == GOT_ERR_CANCELLED)
4143 errcode = pthread_mutex_lock(&tog_mutex);
4145 return (void *)got_error_set_errno(errcode,
4146 "pthread_mutex_lock");
4148 got_repo_close(ta->repo);
4152 errcode = pthread_mutex_unlock(&tog_mutex);
4153 if (errcode && err == NULL)
4154 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4159 static struct got_object_id *
4160 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4161 int first_displayed_line, int selected_line)
4163 struct tog_blame_line *line;
4168 line = &lines[first_displayed_line - 1 + selected_line - 1];
4169 if (!line->annotated)
4175 static const struct got_error *
4176 stop_blame(struct tog_blame *blame)
4178 const struct got_error *err = NULL;
4181 if (blame->thread) {
4183 errcode = pthread_mutex_unlock(&tog_mutex);
4185 return got_error_set_errno(errcode,
4186 "pthread_mutex_unlock");
4187 errcode = pthread_join(blame->thread, (void **)&err);
4189 return got_error_set_errno(errcode, "pthread_join");
4190 errcode = pthread_mutex_lock(&tog_mutex);
4192 return got_error_set_errno(errcode,
4193 "pthread_mutex_lock");
4194 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4196 blame->thread = NULL;
4198 if (blame->thread_args.repo) {
4199 got_repo_close(blame->thread_args.repo);
4200 blame->thread_args.repo = NULL;
4203 if (fclose(blame->f) != 0 && err == NULL)
4204 err = got_error_from_errno("fclose");
4208 for (i = 0; i < blame->nlines; i++)
4209 free(blame->lines[i].id);
4211 blame->lines = NULL;
4213 free(blame->cb_args.commit_id);
4214 blame->cb_args.commit_id = NULL;
4219 static const struct got_error *
4220 cancel_blame_view(void *arg)
4222 const struct got_error *err = NULL;
4226 errcode = pthread_mutex_lock(&tog_mutex);
4228 return got_error_set_errno(errcode,
4229 "pthread_mutex_unlock");
4232 err = got_error(GOT_ERR_CANCELLED);
4234 errcode = pthread_mutex_unlock(&tog_mutex);
4236 return got_error_set_errno(errcode,
4237 "pthread_mutex_lock");
4242 static const struct got_error *
4243 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4244 int *first_displayed_line, int *last_displayed_line, int *selected_line,
4245 int *done, int *eof, const char *path, struct got_object_id *commit_id,
4246 struct got_repository *repo)
4248 const struct got_error *err = NULL;
4249 struct got_blob_object *blob = NULL;
4250 struct got_repository *thread_repo = NULL;
4251 struct got_object_id *obj_id = NULL;
4254 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4258 err = got_object_get_type(&obj_type, repo, obj_id);
4262 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4263 err = got_error(GOT_ERR_OBJ_TYPE);
4267 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4270 blame->f = got_opentemp();
4271 if (blame->f == NULL) {
4272 err = got_error_from_errno("got_opentemp");
4275 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4276 &blame->line_offsets, blame->f, blob);
4277 if (err || blame->nlines == 0)
4280 /* Don't include \n at EOF in the blame line count. */
4281 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4284 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4285 if (blame->lines == NULL) {
4286 err = got_error_from_errno("calloc");
4290 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4294 blame->cb_args.view = view;
4295 blame->cb_args.lines = blame->lines;
4296 blame->cb_args.nlines = blame->nlines;
4297 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4298 if (blame->cb_args.commit_id == NULL) {
4299 err = got_error_from_errno("got_object_id_dup");
4302 blame->cb_args.quit = done;
4304 blame->thread_args.path = path;
4305 blame->thread_args.repo = thread_repo;
4306 blame->thread_args.cb_args = &blame->cb_args;
4307 blame->thread_args.complete = blame_complete;
4308 blame->thread_args.cancel_cb = cancel_blame_view;
4309 blame->thread_args.cancel_arg = done;
4310 *blame_complete = 0;
4314 got_object_blob_close(blob);
4321 static const struct got_error *
4322 open_blame_view(struct tog_view *view, char *path,
4323 struct got_object_id *commit_id, struct got_repository *repo)
4325 const struct got_error *err = NULL;
4326 struct tog_blame_view_state *s = &view->state.blame;
4328 SIMPLEQ_INIT(&s->blamed_commits);
4330 s->path = strdup(path);
4331 if (s->path == NULL)
4332 return got_error_from_errno("strdup");
4334 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4340 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4341 s->first_displayed_line = 1;
4342 s->last_displayed_line = view->nlines;
4343 s->selected_line = 1;
4344 s->blame_complete = 0;
4346 s->commit_id = commit_id;
4347 memset(&s->blame, 0, sizeof(s->blame));
4349 SIMPLEQ_INIT(&s->colors);
4350 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4351 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4352 get_color_value("TOG_COLOR_COMMIT"));
4357 view->show = show_blame_view;
4358 view->input = input_blame_view;
4359 view->close = close_blame_view;
4360 view->search_start = search_start_blame_view;
4361 view->search_next = search_next_blame_view;
4363 return run_blame(&s->blame, view, &s->blame_complete,
4364 &s->first_displayed_line, &s->last_displayed_line,
4365 &s->selected_line, &s->done, &s->eof, s->path,
4366 s->blamed_commit->id, s->repo);
4369 static const struct got_error *
4370 close_blame_view(struct tog_view *view)
4372 const struct got_error *err = NULL;
4373 struct tog_blame_view_state *s = &view->state.blame;
4375 if (s->blame.thread)
4376 err = stop_blame(&s->blame);
4378 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4379 struct got_object_qid *blamed_commit;
4380 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4381 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4382 got_object_qid_free(blamed_commit);
4386 free_colors(&s->colors);
4391 static const struct got_error *
4392 search_start_blame_view(struct tog_view *view)
4394 struct tog_blame_view_state *s = &view->state.blame;
4396 s->matched_line = 0;
4400 static const struct got_error *
4401 search_next_blame_view(struct tog_view *view)
4403 struct tog_blame_view_state *s = &view->state.blame;
4406 if (!view->searching) {
4407 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4411 if (s->matched_line) {
4412 if (view->searching == TOG_SEARCH_FORWARD)
4413 lineno = s->matched_line + 1;
4415 lineno = s->matched_line - 1;
4417 if (view->searching == TOG_SEARCH_FORWARD)
4420 lineno = s->blame.nlines;
4428 if (lineno <= 0 || lineno > s->blame.nlines) {
4429 if (s->matched_line == 0) {
4430 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4435 if (view->searching == TOG_SEARCH_FORWARD)
4438 lineno = s->blame.nlines;
4441 offset = s->blame.line_offsets[lineno - 1];
4442 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4444 return got_error_from_errno("fseeko");
4447 line = parse_next_line(s->blame.f, &len);
4449 match_line(line, &view->regex, 1, &view->regmatch)) {
4450 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4451 s->matched_line = lineno;
4456 if (view->searching == TOG_SEARCH_FORWARD)
4462 if (s->matched_line) {
4463 s->first_displayed_line = s->matched_line;
4464 s->selected_line = 1;
4470 static const struct got_error *
4471 show_blame_view(struct tog_view *view)
4473 const struct got_error *err = NULL;
4474 struct tog_blame_view_state *s = &view->state.blame;
4477 if (s->blame.thread == NULL) {
4478 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4479 &s->blame.thread_args);
4481 return got_error_set_errno(errcode, "pthread_create");
4483 halfdelay(1); /* fast refresh while annotating */
4486 if (s->blame_complete)
4487 halfdelay(10); /* disable fast refresh */
4489 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4490 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4491 s->selected_line, &s->first_displayed_line,
4492 &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4493 s->matched_line, &view->regmatch);
4499 static const struct got_error *
4500 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4501 struct tog_view **focus_view, struct tog_view *view, int ch)
4503 const struct got_error *err = NULL, *thread_err = NULL;
4504 struct tog_view *diff_view;
4505 struct tog_blame_view_state *s = &view->state.blame;
4514 if (s->selected_line > 1)
4516 else if (s->selected_line == 1 &&
4517 s->first_displayed_line > 1)
4518 s->first_displayed_line--;
4522 if (s->first_displayed_line == 1) {
4523 s->selected_line = 1;
4526 if (s->first_displayed_line > view->nlines - 2)
4527 s->first_displayed_line -=
4530 s->first_displayed_line = 1;
4534 if (s->selected_line < view->nlines - 2 &&
4535 s->first_displayed_line +
4536 s->selected_line <= s->blame.nlines)
4538 else if (s->last_displayed_line <
4540 s->first_displayed_line++;
4544 struct got_object_id *id = NULL;
4545 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4546 s->first_displayed_line, s->selected_line);
4550 struct got_commit_object *commit;
4551 struct got_object_qid *pid;
4552 struct got_object_id *blob_id = NULL;
4554 err = got_object_open_as_commit(&commit,
4558 pid = SIMPLEQ_FIRST(
4559 got_object_commit_get_parent_ids(commit));
4561 got_object_commit_close(commit);
4564 /* Check if path history ends here. */
4565 err = got_object_id_by_path(&blob_id, s->repo,
4568 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4570 got_object_commit_close(commit);
4573 err = got_object_get_type(&obj_type, s->repo,
4576 /* Can't blame non-blob type objects. */
4577 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4578 got_object_commit_close(commit);
4581 err = got_object_qid_alloc(&s->blamed_commit,
4583 got_object_commit_close(commit);
4585 if (got_object_id_cmp(id,
4586 s->blamed_commit->id) == 0)
4588 err = got_object_qid_alloc(&s->blamed_commit,
4594 thread_err = stop_blame(&s->blame);
4598 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4599 s->blamed_commit, entry);
4600 err = run_blame(&s->blame, view, &s->blame_complete,
4601 &s->first_displayed_line, &s->last_displayed_line,
4602 &s->selected_line, &s->done, &s->eof,
4603 s->path, s->blamed_commit->id, s->repo);
4609 struct got_object_qid *first;
4610 first = SIMPLEQ_FIRST(&s->blamed_commits);
4611 if (!got_object_id_cmp(first->id, s->commit_id))
4614 thread_err = stop_blame(&s->blame);
4618 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4619 got_object_qid_free(s->blamed_commit);
4621 SIMPLEQ_FIRST(&s->blamed_commits);
4622 err = run_blame(&s->blame, view, &s->blame_complete,
4623 &s->first_displayed_line, &s->last_displayed_line,
4624 &s->selected_line, &s->done, &s->eof, s->path,
4625 s->blamed_commit->id, s->repo);
4632 struct got_object_id *id = NULL;
4633 struct got_object_qid *pid;
4634 struct got_commit_object *commit = NULL;
4635 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4636 s->first_displayed_line, s->selected_line);
4639 err = got_object_open_as_commit(&commit, s->repo, id);
4642 pid = SIMPLEQ_FIRST(
4643 got_object_commit_get_parent_ids(commit));
4644 if (view_is_parent_view(view))
4645 begin_x = view_split_begin_x(view->begin_x);
4646 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4647 if (diff_view == NULL) {
4648 got_object_commit_close(commit);
4649 err = got_error_from_errno("view_open");
4652 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4653 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4654 got_object_commit_close(commit);
4656 view_close(diff_view);
4659 if (view_is_parent_view(view)) {
4660 err = view_close_child(view);
4663 err = view_set_child(view, diff_view);
4665 view_close(diff_view);
4668 *focus_view = diff_view;
4669 view->child_focussed = 1;
4671 *new_view = diff_view;
4679 if (s->last_displayed_line >= s->blame.nlines &&
4680 s->selected_line >= MIN(s->blame.nlines,
4681 view->nlines - 2)) {
4684 if (s->last_displayed_line >= s->blame.nlines &&
4685 s->selected_line < view->nlines - 2) {
4686 s->selected_line = MIN(s->blame.nlines,
4690 if (s->last_displayed_line + view->nlines - 2
4692 s->first_displayed_line +=
4695 s->first_displayed_line =
4700 if (s->selected_line > view->nlines - 2) {
4701 s->selected_line = MIN(s->blame.nlines,
4708 return thread_err ? thread_err : err;
4711 static const struct got_error *
4712 cmd_blame(int argc, char *argv[])
4714 const struct got_error *error;
4715 struct got_repository *repo = NULL;
4716 struct got_worktree *worktree = NULL;
4717 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4718 char *link_target = NULL;
4719 struct got_object_id *commit_id = NULL;
4720 char *commit_id_str = NULL;
4722 struct tog_view *view;
4725 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4730 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4733 commit_id_str = optarg;
4736 repo_path = realpath(optarg, NULL);
4737 if (repo_path == NULL)
4738 return got_error_from_errno2("realpath",
4753 cwd = getcwd(NULL, 0);
4755 return got_error_from_errno("getcwd");
4757 error = got_worktree_open(&worktree, cwd);
4758 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4761 if (repo_path == NULL) {
4764 strdup(got_worktree_get_repo_path(worktree));
4766 repo_path = strdup(cwd);
4768 if (repo_path == NULL) {
4769 error = got_error_from_errno("strdup");
4773 error = got_repo_open(&repo, repo_path, NULL);
4777 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4784 error = apply_unveil(got_repo_get_path(repo), NULL);
4788 if (commit_id_str == NULL) {
4789 struct got_reference *head_ref;
4790 error = got_ref_open(&head_ref, repo, worktree ?
4791 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4794 error = got_ref_resolve(&commit_id, repo, head_ref);
4795 got_ref_close(head_ref);
4797 error = got_repo_match_object_id(&commit_id, NULL,
4798 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4803 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4805 error = got_error_from_errno("view_open");
4809 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4814 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4819 /* Release work tree lock. */
4820 got_worktree_close(worktree);
4823 error = view_loop(view);
4831 got_worktree_close(worktree);
4833 got_repo_close(repo);
4837 static const struct got_error *
4838 draw_tree_entries(struct tog_view *view,
4839 struct got_tree_entry **first_displayed_entry,
4840 struct got_tree_entry **last_displayed_entry,
4841 struct got_tree_entry **selected_entry, int *ndisplayed,
4842 const char *label, int show_ids, const char *parent_path,
4843 struct got_tree_object *tree, int selected, int limit,
4844 int isroot, struct tog_colors *colors, struct got_repository *repo)
4846 const struct got_error *err = NULL;
4847 struct got_tree_entry *te;
4849 struct tog_color *tc;
4850 int width, n, i, nentries;
4854 werase(view->window);
4859 err = format_line(&wline, &width, label, view->ncols, 0);
4862 if (view_needs_focus_indication(view))
4863 wstandout(view->window);
4864 tc = get_color(colors, TOG_COLOR_COMMIT);
4866 wattr_on(view->window,
4867 COLOR_PAIR(tc->colorpair), NULL);
4868 waddwstr(view->window, wline);
4870 wattr_off(view->window,
4871 COLOR_PAIR(tc->colorpair), NULL);
4872 if (view_needs_focus_indication(view))
4873 wstandend(view->window);
4876 if (width < view->ncols - 1)
4877 waddch(view->window, '\n');
4880 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4883 waddwstr(view->window, wline);
4886 if (width < view->ncols - 1)
4887 waddch(view->window, '\n');
4890 waddch(view->window, '\n');
4894 if (*first_displayed_entry == NULL) {
4895 te = got_object_tree_get_first_entry(tree);
4896 if (selected == 0) {
4898 wstandout(view->window);
4899 *selected_entry = NULL;
4901 waddstr(view->window, " ..\n"); /* parent directory */
4902 if (selected == 0 && view->focussed)
4903 wstandend(view->window);
4910 te = *first_displayed_entry;
4913 nentries = got_object_tree_get_nentries(tree);
4914 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4915 char *line = NULL, *id_str = NULL, *link_target = NULL;
4916 const char *modestr = "";
4919 te = got_object_tree_get_entry(tree, i);
4920 mode = got_tree_entry_get_mode(te);
4923 err = got_object_id_str(&id_str,
4924 got_tree_entry_get_id(te));
4926 return got_error_from_errno(
4927 "got_object_id_str");
4929 if (got_object_tree_entry_is_submodule(te))
4931 else if (S_ISLNK(mode)) {
4934 err = got_tree_entry_get_symlink_target(&link_target,
4940 for (i = 0; i < strlen(link_target); i++) {
4941 if (!isprint((unsigned char)link_target[i]))
4942 link_target[i] = '?';
4946 else if (S_ISDIR(mode))
4948 else if (mode & S_IXUSR)
4950 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4951 got_tree_entry_get_name(te), modestr,
4952 link_target ? " -> ": "",
4953 link_target ? link_target : "") == -1) {
4956 return got_error_from_errno("asprintf");
4960 err = format_line(&wline, &width, line, view->ncols, 0);
4965 if (n == selected) {
4967 wstandout(view->window);
4968 *selected_entry = te;
4970 tc = match_color(colors, line);
4972 wattr_on(view->window,
4973 COLOR_PAIR(tc->colorpair), NULL);
4974 waddwstr(view->window, wline);
4976 wattr_off(view->window,
4977 COLOR_PAIR(tc->colorpair), NULL);
4978 if (width < view->ncols - 1)
4979 waddch(view->window, '\n');
4980 if (n == selected && view->focussed)
4981 wstandend(view->window);
4987 *last_displayed_entry = te;
4996 tree_scroll_up(struct tog_view *view,
4997 struct got_tree_entry **first_displayed_entry, int maxscroll,
4998 struct got_tree_object *tree, int isroot)
5000 struct got_tree_entry *te;
5003 if (*first_displayed_entry == NULL)
5006 te = got_object_tree_get_entry(tree, 0);
5007 if (*first_displayed_entry == te) {
5009 *first_displayed_entry = NULL;
5014 while (*first_displayed_entry && i < maxscroll) {
5015 *first_displayed_entry = got_tree_entry_get_prev(tree,
5016 *first_displayed_entry);
5019 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
5020 *first_displayed_entry = NULL;
5024 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
5025 struct got_tree_entry *last_displayed_entry,
5026 struct got_tree_object *tree)
5028 struct got_tree_entry *next, *last;
5031 if (*first_displayed_entry)
5032 next = got_tree_entry_get_next(tree, *first_displayed_entry);
5034 next = got_object_tree_get_first_entry(tree);
5036 last = last_displayed_entry;
5037 while (next && last && n++ < maxscroll) {
5038 last = got_tree_entry_get_next(tree, last);
5040 *first_displayed_entry = next;
5041 next = got_tree_entry_get_next(tree, next);
5047 static const struct got_error *
5048 tree_entry_path(char **path, struct tog_parent_trees *parents,
5049 struct got_tree_entry *te)
5051 const struct got_error *err = NULL;
5052 struct tog_parent_tree *pt;
5053 size_t len = 2; /* for leading slash and NUL */
5055 TAILQ_FOREACH(pt, parents, entry)
5056 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5059 len += strlen(got_tree_entry_get_name(te));
5061 *path = calloc(1, len);
5063 return got_error_from_errno("calloc");
5066 pt = TAILQ_LAST(parents, tog_parent_trees);
5068 const char *name = got_tree_entry_get_name(pt->selected_entry);
5069 if (strlcat(*path, name, len) >= len) {
5070 err = got_error(GOT_ERR_NO_SPACE);
5073 if (strlcat(*path, "/", len) >= len) {
5074 err = got_error(GOT_ERR_NO_SPACE);
5077 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5080 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5081 err = got_error(GOT_ERR_NO_SPACE);
5093 static const struct got_error *
5094 blame_tree_entry(struct tog_view **new_view, int begin_x,
5095 struct got_tree_entry *te, struct tog_parent_trees *parents,
5096 struct got_object_id *commit_id, struct got_repository *repo)
5098 const struct got_error *err = NULL;
5100 struct tog_view *blame_view;
5104 err = tree_entry_path(&path, parents, te);
5108 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5109 if (blame_view == NULL) {
5110 err = got_error_from_errno("view_open");
5114 err = open_blame_view(blame_view, path, commit_id, repo);
5116 if (err->code == GOT_ERR_CANCELLED)
5118 view_close(blame_view);
5120 *new_view = blame_view;
5126 static const struct got_error *
5127 log_tree_entry(struct tog_view **new_view, int begin_x,
5128 struct got_tree_entry *te, struct tog_parent_trees *parents,
5129 struct got_object_id *commit_id, struct got_repository *repo)
5131 struct tog_view *log_view;
5132 const struct got_error *err = NULL;
5137 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5138 if (log_view == NULL)
5139 return got_error_from_errno("view_open");
5141 err = tree_entry_path(&path, parents, te);
5145 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5147 view_close(log_view);
5149 *new_view = log_view;
5154 static const struct got_error *
5155 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5156 struct got_object_id *commit_id, struct got_repository *repo)
5158 const struct got_error *err = NULL;
5159 char *commit_id_str = NULL;
5160 struct tog_tree_view_state *s = &view->state.tree;
5162 TAILQ_INIT(&s->parents);
5164 err = got_object_id_str(&commit_id_str, commit_id);
5168 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5169 err = got_error_from_errno("asprintf");
5173 s->root = s->tree = root;
5174 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5175 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5176 s->commit_id = got_object_id_dup(commit_id);
5177 if (s->commit_id == NULL) {
5178 err = got_error_from_errno("got_object_id_dup");
5183 SIMPLEQ_INIT(&s->colors);
5185 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5186 err = add_color(&s->colors, "\\$$",
5187 TOG_COLOR_TREE_SUBMODULE,
5188 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5191 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5192 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5194 free_colors(&s->colors);
5197 err = add_color(&s->colors, "/$",
5198 TOG_COLOR_TREE_DIRECTORY,
5199 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5201 free_colors(&s->colors);
5205 err = add_color(&s->colors, "\\*$",
5206 TOG_COLOR_TREE_EXECUTABLE,
5207 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5209 free_colors(&s->colors);
5213 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5214 get_color_value("TOG_COLOR_COMMIT"));
5216 free_colors(&s->colors);
5221 view->show = show_tree_view;
5222 view->input = input_tree_view;
5223 view->close = close_tree_view;
5224 view->search_start = search_start_tree_view;
5225 view->search_next = search_next_tree_view;
5227 free(commit_id_str);
5229 free(s->tree_label);
5230 s->tree_label = NULL;
5235 static const struct got_error *
5236 close_tree_view(struct tog_view *view)
5238 struct tog_tree_view_state *s = &view->state.tree;
5240 free_colors(&s->colors);
5241 free(s->tree_label);
5242 s->tree_label = NULL;
5244 s->commit_id = NULL;
5245 while (!TAILQ_EMPTY(&s->parents)) {
5246 struct tog_parent_tree *parent;
5247 parent = TAILQ_FIRST(&s->parents);
5248 TAILQ_REMOVE(&s->parents, parent, entry);
5252 if (s->tree != s->root)
5253 got_object_tree_close(s->tree);
5254 got_object_tree_close(s->root);
5258 static const struct got_error *
5259 search_start_tree_view(struct tog_view *view)
5261 struct tog_tree_view_state *s = &view->state.tree;
5263 s->matched_entry = NULL;
5268 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5270 regmatch_t regmatch;
5272 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5276 static const struct got_error *
5277 search_next_tree_view(struct tog_view *view)
5279 struct tog_tree_view_state *s = &view->state.tree;
5280 struct got_tree_entry *te = NULL;
5282 if (!view->searching) {
5283 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5287 if (s->matched_entry) {
5288 if (view->searching == TOG_SEARCH_FORWARD) {
5289 if (s->selected_entry)
5290 te = got_tree_entry_get_next(s->tree,
5293 te = got_object_tree_get_first_entry(s->tree);
5295 if (s->selected_entry == NULL)
5296 te = got_object_tree_get_last_entry(s->tree);
5298 te = got_tree_entry_get_prev(s->tree,
5302 if (view->searching == TOG_SEARCH_FORWARD)
5303 te = got_object_tree_get_first_entry(s->tree);
5305 te = got_object_tree_get_last_entry(s->tree);
5310 if (s->matched_entry == NULL) {
5311 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5314 if (view->searching == TOG_SEARCH_FORWARD)
5315 te = got_object_tree_get_first_entry(s->tree);
5317 te = got_object_tree_get_last_entry(s->tree);
5320 if (match_tree_entry(te, &view->regex)) {
5321 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5322 s->matched_entry = te;
5326 if (view->searching == TOG_SEARCH_FORWARD)
5327 te = got_tree_entry_get_next(s->tree, te);
5329 te = got_tree_entry_get_prev(s->tree, te);
5332 if (s->matched_entry) {
5333 s->first_displayed_entry = s->matched_entry;
5340 static const struct got_error *
5341 show_tree_view(struct tog_view *view)
5343 const struct got_error *err = NULL;
5344 struct tog_tree_view_state *s = &view->state.tree;
5347 err = tree_entry_path(&parent_path, &s->parents, NULL);
5351 err = draw_tree_entries(view, &s->first_displayed_entry,
5352 &s->last_displayed_entry, &s->selected_entry,
5353 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5354 s->tree, s->selected, view->nlines, s->tree == s->root,
5355 &s->colors, s->repo);
5362 static const struct got_error *
5363 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5364 struct tog_view **focus_view, struct tog_view *view, int ch)
5366 const struct got_error *err = NULL;
5367 struct tog_tree_view_state *s = &view->state.tree;
5368 struct tog_view *log_view;
5369 int begin_x = 0, nscrolled;
5373 s->show_ids = !s->show_ids;
5376 if (!s->selected_entry)
5378 if (view_is_parent_view(view))
5379 begin_x = view_split_begin_x(view->begin_x);
5380 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5381 &s->parents, s->commit_id, s->repo);
5382 if (view_is_parent_view(view)) {
5383 err = view_close_child(view);
5386 err = view_set_child(view, log_view);
5388 view_close(log_view);
5391 *focus_view = log_view;
5392 view->child_focussed = 1;
5394 *new_view = log_view;
5398 if (s->selected > 0) {
5400 if (s->selected == 0)
5403 if (s->selected > 0)
5405 tree_scroll_up(view, &s->first_displayed_entry, 1,
5406 s->tree, s->tree == s->root);
5410 tree_scroll_up(view, &s->first_displayed_entry,
5411 MAX(0, view->nlines - 4 - s->selected), s->tree,
5412 s->tree == s->root);
5414 if (got_object_tree_get_first_entry(s->tree) ==
5415 s->first_displayed_entry && s->tree != s->root)
5416 s->first_displayed_entry = NULL;
5420 if (s->selected < s->ndisplayed - 1) {
5424 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5426 /* can't scroll any further */
5428 tree_scroll_down(&s->first_displayed_entry, 1,
5429 s->last_displayed_entry, s->tree);
5433 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5435 /* can't scroll any further; move cursor down */
5436 if (s->selected < s->ndisplayed - 1)
5437 s->selected = s->ndisplayed - 1;
5440 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5441 view->nlines, s->last_displayed_entry, s->tree);
5442 if (nscrolled < view->nlines) {
5444 struct got_tree_entry *te;
5445 te = s->first_displayed_entry;
5448 te = got_tree_entry_get_next(s->tree, te);
5450 s->selected = ndisplayed - 1;
5456 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5457 struct tog_parent_tree *parent;
5458 /* user selected '..' */
5459 if (s->tree == s->root)
5461 parent = TAILQ_FIRST(&s->parents);
5462 TAILQ_REMOVE(&s->parents, parent,
5464 got_object_tree_close(s->tree);
5465 s->tree = parent->tree;
5466 s->first_displayed_entry =
5467 parent->first_displayed_entry;
5469 parent->selected_entry;
5470 s->selected = parent->selected;
5472 } else if (S_ISDIR(got_tree_entry_get_mode(
5473 s->selected_entry))) {
5474 struct got_tree_object *subtree;
5475 err = got_object_open_as_tree(&subtree, s->repo,
5476 got_tree_entry_get_id(s->selected_entry));
5479 err = tree_view_visit_subtree(subtree, s);
5481 got_object_tree_close(subtree);
5484 } else if (S_ISREG(got_tree_entry_get_mode(
5485 s->selected_entry))) {
5486 struct tog_view *blame_view;
5487 int begin_x = view_is_parent_view(view) ?
5488 view_split_begin_x(view->begin_x) : 0;
5490 err = blame_tree_entry(&blame_view, begin_x,
5491 s->selected_entry, &s->parents,
5492 s->commit_id, s->repo);
5495 if (view_is_parent_view(view)) {
5496 err = view_close_child(view);
5499 err = view_set_child(view, blame_view);
5501 view_close(blame_view);
5504 *focus_view = blame_view;
5505 view->child_focussed = 1;
5507 *new_view = blame_view;
5511 if (s->selected > view->nlines)
5512 s->selected = s->ndisplayed - 1;
5525 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5530 static const struct got_error *
5531 cmd_tree(int argc, char *argv[])
5533 const struct got_error *error;
5534 struct got_repository *repo = NULL;
5535 struct got_worktree *worktree = NULL;
5536 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5537 struct got_object_id *commit_id = NULL;
5538 char *commit_id_arg = NULL;
5539 struct got_commit_object *commit = NULL;
5540 struct got_tree_object *tree = NULL;
5542 struct tog_view *view;
5545 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5550 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5553 commit_id_arg = optarg;
5556 repo_path = realpath(optarg, NULL);
5557 if (repo_path == NULL)
5558 return got_error_from_errno2("realpath",
5573 cwd = getcwd(NULL, 0);
5575 return got_error_from_errno("getcwd");
5577 error = got_worktree_open(&worktree, cwd);
5578 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5581 if (repo_path == NULL) {
5584 strdup(got_worktree_get_repo_path(worktree));
5586 repo_path = strdup(cwd);
5588 if (repo_path == NULL) {
5589 error = got_error_from_errno("strdup");
5593 error = got_repo_open(&repo, repo_path, NULL);
5597 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5604 error = apply_unveil(got_repo_get_path(repo), NULL);
5608 error = got_repo_match_object_id(&commit_id, NULL,
5609 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5610 GOT_OBJ_TYPE_COMMIT, 1, repo);
5614 error = got_object_open_as_commit(&commit, repo, commit_id);
5618 error = got_object_open_as_tree(&tree, repo,
5619 got_object_commit_get_tree_id(commit));
5623 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5625 error = got_error_from_errno("view_open");
5628 error = open_tree_view(view, tree, commit_id, repo);
5631 if (!got_path_is_root_dir(in_repo_path)) {
5632 error = tree_view_walk_path(&view->state.tree, commit_id,
5633 in_repo_path, repo);
5639 /* Release work tree lock. */
5640 got_worktree_close(worktree);
5643 error = view_loop(view);
5649 got_object_commit_close(commit);
5651 got_object_tree_close(tree);
5653 got_repo_close(repo);
5657 static const struct got_error *
5658 ref_view_load_refs(struct tog_ref_view_state *s)
5660 const struct got_error *err;
5661 struct got_reflist_entry *sre;
5662 struct tog_reflist_entry *re;
5664 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5665 got_ref_cmp_by_name, NULL);
5670 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5671 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5674 re = malloc(sizeof(*re));
5676 return got_error_from_errno("malloc");
5679 re->idx = s->nrefs++;
5680 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5687 ref_view_free_refs(struct tog_ref_view_state *s)
5689 struct tog_reflist_entry *re;
5691 while (!TAILQ_EMPTY(&s->refs)) {
5692 re = TAILQ_FIRST(&s->refs);
5693 TAILQ_REMOVE(&s->refs, re, entry);
5696 got_ref_list_free(&s->simplerefs);
5699 static const struct got_error *
5700 open_ref_view(struct tog_view *view, struct got_repository *repo)
5702 const struct got_error *err = NULL;
5703 struct tog_ref_view_state *s = &view->state.ref;
5705 s->first_displayed_entry = 0;
5706 s->selected_entry = 0;
5709 SIMPLEQ_INIT(&s->simplerefs);
5710 TAILQ_INIT(&s->refs);
5711 SIMPLEQ_INIT(&s->colors);
5713 err = ref_view_load_refs(s);
5717 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5718 err = add_color(&s->colors, "^refs/heads/",
5719 TOG_COLOR_REFS_HEADS,
5720 get_color_value("TOG_COLOR_REFS_HEADS"));
5724 err = add_color(&s->colors, "^refs/tags/",
5725 TOG_COLOR_REFS_TAGS,
5726 get_color_value("TOG_COLOR_REFS_TAGS"));
5730 err = add_color(&s->colors, "^refs/remotes/",
5731 TOG_COLOR_REFS_REMOTES,
5732 get_color_value("TOG_COLOR_REFS_REMOTES"));
5737 view->show = show_ref_view;
5738 view->input = input_ref_view;
5739 view->close = close_ref_view;
5740 view->search_start = search_start_ref_view;
5741 view->search_next = search_next_ref_view;
5744 free_colors(&s->colors);
5748 static const struct got_error *
5749 close_ref_view(struct tog_view *view)
5751 struct tog_ref_view_state *s = &view->state.ref;
5753 ref_view_free_refs(s);
5754 free_colors(&s->colors);
5759 static const struct got_error *
5760 resolve_reflist_entry(struct got_object_id **commit_id,
5761 struct tog_reflist_entry *re, struct got_repository *repo)
5763 const struct got_error *err = NULL;
5764 struct got_object_id *obj_id;
5765 struct got_tag_object *tag = NULL;
5770 err = got_ref_resolve(&obj_id, repo, re->ref);
5774 err = got_object_get_type(&obj_type, repo, obj_id);
5779 case GOT_OBJ_TYPE_COMMIT:
5780 *commit_id = obj_id;
5782 case GOT_OBJ_TYPE_TAG:
5783 err = got_object_open_as_tag(&tag, repo, obj_id);
5787 err = got_object_get_type(&obj_type, repo,
5788 got_object_tag_get_object_id(tag));
5791 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5792 err = got_error(GOT_ERR_OBJ_TYPE);
5795 *commit_id = got_object_id_dup(
5796 got_object_tag_get_object_id(tag));
5797 if (*commit_id == NULL) {
5798 err = got_error_from_errno("got_object_id_dup");
5803 err = got_error(GOT_ERR_OBJ_TYPE);
5809 got_object_tag_close(tag);
5817 static const struct got_error *
5818 log_ref_entry(struct tog_view **new_view, int begin_x,
5819 struct tog_reflist_entry *re, struct got_repository *repo)
5821 struct tog_view *log_view;
5822 const struct got_error *err = NULL;
5823 struct got_object_id *commit_id = NULL;
5827 err = resolve_reflist_entry(&commit_id, re, repo);
5829 if (err->code != GOT_ERR_OBJ_TYPE)
5835 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5836 if (log_view == NULL) {
5837 err = got_error_from_errno("view_open");
5841 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5844 view_close(log_view);
5846 *new_view = log_view;
5852 ref_scroll_up(struct tog_view *view,
5853 struct tog_reflist_entry **first_displayed_entry, int maxscroll,
5854 struct tog_reflist_head *refs)
5858 if (*first_displayed_entry == TAILQ_FIRST(refs))
5862 while (*first_displayed_entry && i < maxscroll) {
5863 *first_displayed_entry = TAILQ_PREV(*first_displayed_entry,
5864 tog_reflist_head, entry);
5870 ref_scroll_down(struct tog_reflist_entry **first_displayed_entry, int maxscroll,
5871 struct tog_reflist_entry *last_displayed_entry,
5872 struct tog_reflist_head *refs)
5874 struct tog_reflist_entry *next, *last;
5877 if (*first_displayed_entry)
5878 next = TAILQ_NEXT(*first_displayed_entry, entry);
5880 next = TAILQ_FIRST(refs);
5882 last = last_displayed_entry;
5883 while (next && last && n++ < maxscroll) {
5884 last = TAILQ_NEXT(last, entry);
5886 *first_displayed_entry = next;
5887 next = TAILQ_NEXT(next, entry);
5893 static const struct got_error *
5894 search_start_ref_view(struct tog_view *view)
5896 struct tog_ref_view_state *s = &view->state.ref;
5898 s->matched_entry = NULL;
5903 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5905 regmatch_t regmatch;
5907 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5911 static const struct got_error *
5912 search_next_ref_view(struct tog_view *view)
5914 struct tog_ref_view_state *s = &view->state.ref;
5915 struct tog_reflist_entry *re = NULL;
5917 if (!view->searching) {
5918 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5922 if (s->matched_entry) {
5923 if (view->searching == TOG_SEARCH_FORWARD) {
5924 if (s->selected_entry)
5925 re = TAILQ_NEXT(s->selected_entry, entry);
5927 re = TAILQ_PREV(s->selected_entry,
5928 tog_reflist_head, entry);
5930 if (s->selected_entry == NULL)
5931 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5933 re = TAILQ_PREV(s->selected_entry,
5934 tog_reflist_head, entry);
5937 if (view->searching == TOG_SEARCH_FORWARD)
5938 re = TAILQ_FIRST(&s->refs);
5940 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5945 if (s->matched_entry == NULL) {
5946 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5949 if (view->searching == TOG_SEARCH_FORWARD)
5950 re = TAILQ_FIRST(&s->refs);
5952 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5955 if (match_reflist_entry(re, &view->regex)) {
5956 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5957 s->matched_entry = re;
5961 if (view->searching == TOG_SEARCH_FORWARD)
5962 re = TAILQ_NEXT(re, entry);
5964 re = TAILQ_PREV(re, tog_reflist_head, entry);
5967 if (s->matched_entry) {
5968 s->first_displayed_entry = s->matched_entry;
5975 static const struct got_error *
5976 show_ref_view(struct tog_view *view)
5978 const struct got_error *err = NULL;
5979 struct tog_ref_view_state *s = &view->state.ref;
5980 struct tog_reflist_entry *re;
5983 struct tog_color *tc;
5985 int limit = view->nlines;
5987 werase(view->window);
5994 if (s->first_displayed_entry)
5995 re = s->first_displayed_entry;
5997 re = TAILQ_FIRST(&s->refs);
5999 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6001 return got_error_from_errno("asprintf");
6003 err = format_line(&wline, &width, line, view->ncols, 0);
6008 if (view_needs_focus_indication(view))
6009 wstandout(view->window);
6010 waddwstr(view->window, wline);
6011 if (view_needs_focus_indication(view))
6012 wstandend(view->window);
6017 if (width < view->ncols - 1)
6018 waddch(view->window, '\n');
6023 while (re && limit > 0) {
6026 if (got_ref_is_symbolic(re->ref)) {
6027 if (asprintf(&line, "%s -> %s",
6028 got_ref_get_name(re->ref),
6029 got_ref_get_symref_target(re->ref)) == -1)
6030 return got_error_from_errno("asprintf");
6031 } else if (s->show_ids) {
6032 struct got_object_id *id;
6034 err = got_ref_resolve(&id, s->repo, re->ref);
6037 err = got_object_id_str(&id_str, id);
6042 if (asprintf(&line, "%s: %s",
6043 got_ref_get_name(re->ref), id_str) == -1) {
6044 err = got_error_from_errno("asprintf");
6052 line = strdup(got_ref_get_name(re->ref));
6054 return got_error_from_errno("strdup");
6057 err = format_line(&wline, &width, line, view->ncols, 0);
6062 if (n == s->selected) {
6064 wstandout(view->window);
6065 s->selected_entry = re;
6067 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6069 wattr_on(view->window,
6070 COLOR_PAIR(tc->colorpair), NULL);
6071 waddwstr(view->window, wline);
6073 wattr_off(view->window,
6074 COLOR_PAIR(tc->colorpair), NULL);
6075 if (width < view->ncols - 1)
6076 waddch(view->window, '\n');
6077 if (n == s->selected && view->focussed)
6078 wstandend(view->window);
6084 s->last_displayed_entry = re;
6087 re = TAILQ_NEXT(re, entry);
6094 static const struct got_error *
6095 browse_ref_tree(struct tog_view **new_view, int begin_x,
6096 struct tog_reflist_entry *re, struct got_repository *repo)
6098 const struct got_error *err = NULL;
6099 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6100 struct got_tree_object *tree = NULL;
6101 struct tog_view *tree_view;
6105 err = resolve_reflist_entry(&commit_id, re, repo);
6107 if (err->code != GOT_ERR_OBJ_TYPE)
6113 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6117 err = got_object_open_as_tree(&tree, repo, tree_id);
6121 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6122 if (tree_view == NULL) {
6123 err = got_error_from_errno("view_open");
6127 err = open_tree_view(tree_view, tree, commit_id, repo);
6131 *new_view = tree_view;
6137 got_object_tree_close(tree);
6141 static const struct got_error *
6142 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6143 struct tog_view **focus_view, struct tog_view *view, int ch)
6145 const struct got_error *err = NULL;
6146 struct tog_ref_view_state *s = &view->state.ref;
6147 struct tog_view *log_view, *tree_view;
6148 int begin_x = 0, nscrolled;
6152 s->show_ids = !s->show_ids;
6156 if (!s->selected_entry)
6158 if (view_is_parent_view(view))
6159 begin_x = view_split_begin_x(view->begin_x);
6160 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6162 if (view_is_parent_view(view)) {
6163 err = view_close_child(view);
6166 err = view_set_child(view, log_view);
6168 view_close(log_view);
6171 *focus_view = log_view;
6172 view->child_focussed = 1;
6174 *new_view = log_view;
6177 if (!s->selected_entry)
6179 if (view_is_parent_view(view))
6180 begin_x = view_split_begin_x(view->begin_x);
6181 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6183 if (err || tree_view == NULL)
6185 if (view_is_parent_view(view)) {
6186 err = view_close_child(view);
6189 err = view_set_child(view, tree_view);
6191 view_close(tree_view);
6194 *focus_view = tree_view;
6195 view->child_focussed = 1;
6197 *new_view = tree_view;
6201 if (s->selected > 0) {
6203 if (s->selected == 0)
6206 if (s->selected > 0)
6208 ref_scroll_up(view, &s->first_displayed_entry, 1, &s->refs);
6212 ref_scroll_up(view, &s->first_displayed_entry,
6213 MAX(0, view->nlines - 4 - s->selected), &s->refs);
6218 if (s->selected < s->ndisplayed - 1) {
6222 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6223 /* can't scroll any further */
6225 ref_scroll_down(&s->first_displayed_entry, 1,
6226 s->last_displayed_entry, &s->refs);
6230 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6231 /* can't scroll any further; move cursor down */
6232 if (s->selected < s->ndisplayed - 1)
6233 s->selected = s->ndisplayed - 1;
6236 nscrolled = ref_scroll_down(&s->first_displayed_entry,
6237 view->nlines, s->last_displayed_entry, &s->refs);
6238 if (nscrolled < view->nlines) {
6240 struct tog_reflist_entry *re;
6241 re = s->first_displayed_entry;
6244 re = TAILQ_NEXT(re, entry);
6246 s->selected = ndisplayed - 1;
6250 ref_view_free_refs(s);
6251 err = ref_view_load_refs(s);
6254 if (s->selected > view->nlines)
6255 s->selected = s->ndisplayed - 1;
6268 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6273 static const struct got_error *
6274 cmd_ref(int argc, char *argv[])
6276 const struct got_error *error;
6277 struct got_repository *repo = NULL;
6278 struct got_worktree *worktree = NULL;
6279 char *cwd = NULL, *repo_path = NULL;
6281 struct tog_view *view;
6284 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6289 while ((ch = getopt(argc, argv, "r:")) != -1) {
6292 repo_path = realpath(optarg, NULL);
6293 if (repo_path == NULL)
6294 return got_error_from_errno2("realpath",
6309 cwd = getcwd(NULL, 0);
6311 return got_error_from_errno("getcwd");
6313 error = got_worktree_open(&worktree, cwd);
6314 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6317 if (repo_path == NULL) {
6320 strdup(got_worktree_get_repo_path(worktree));
6322 repo_path = strdup(cwd);
6324 if (repo_path == NULL) {
6325 error = got_error_from_errno("strdup");
6329 error = got_repo_open(&repo, repo_path, NULL);
6335 error = apply_unveil(got_repo_get_path(repo), NULL);
6339 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6341 error = got_error_from_errno("view_open");
6345 error = open_ref_view(view, repo);
6350 /* Release work tree lock. */
6351 got_worktree_close(worktree);
6354 error = view_loop(view);
6359 got_repo_close(repo);
6364 list_commands(FILE *fp)
6368 fprintf(fp, "commands:");
6369 for (i = 0; i < nitems(tog_commands); i++) {
6370 struct tog_cmd *cmd = &tog_commands[i];
6371 fprintf(fp, " %s", cmd->name);
6377 usage(int hflag, int status)
6379 FILE *fp = (status == 0) ? stdout : stderr;
6381 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6384 fprintf(fp, "lazy usage: %s path\n", getprogname());
6391 make_argv(int argc, ...)
6399 argv = calloc(argc, sizeof(char *));
6402 for (i = 0; i < argc; i++) {
6403 argv[i] = strdup(va_arg(ap, char *));
6404 if (argv[i] == NULL)
6413 * Try to convert 'tog path' into a 'tog log path' command.
6414 * The user could simply have mistyped the command rather than knowingly
6415 * provided a path. So check whether argv[0] can in fact be resolved
6416 * to a path in the HEAD commit and print a special error if not.
6417 * This hack is for mpi@ <3
6419 static const struct got_error *
6420 tog_log_with_path(int argc, char *argv[])
6422 const struct got_error *error = NULL;
6423 struct tog_cmd *cmd = NULL;
6424 struct got_repository *repo = NULL;
6425 struct got_worktree *worktree = NULL;
6426 struct got_object_id *commit_id = NULL, *id = NULL;
6427 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6428 char *commit_id_str = NULL, **cmd_argv = NULL;
6430 cwd = getcwd(NULL, 0);
6432 return got_error_from_errno("getcwd");
6434 error = got_worktree_open(&worktree, cwd);
6435 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6439 repo_path = strdup(got_worktree_get_repo_path(worktree));
6441 repo_path = strdup(cwd);
6442 if (repo_path == NULL) {
6443 error = got_error_from_errno("strdup");
6447 error = got_repo_open(&repo, repo_path, NULL);
6451 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6456 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6457 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6458 GOT_OBJ_TYPE_COMMIT, 1, repo);
6463 got_worktree_close(worktree);
6467 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6469 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6471 fprintf(stderr, "%s: '%s' is no known command or path\n",
6472 getprogname(), argv[0]);
6477 got_repo_close(repo);
6480 error = got_object_id_str(&commit_id_str, commit_id);
6484 cmd = &tog_commands[0]; /* log */
6486 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6487 error = cmd->cmd_main(argc, cmd_argv);
6490 got_repo_close(repo);
6492 got_worktree_close(worktree);
6494 free(commit_id_str);
6501 for (i = 0; i < argc; i++)
6509 main(int argc, char *argv[])
6511 const struct got_error *error = NULL;
6512 struct tog_cmd *cmd = NULL;
6513 int ch, hflag = 0, Vflag = 0;
6514 char **cmd_argv = NULL;
6515 static struct option longopts[] = {
6516 { "version", no_argument, NULL, 'V' },
6520 setlocale(LC_CTYPE, "");
6522 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6542 got_version_print_str();
6549 /* Build an argument vector which runs a default command. */
6550 cmd = &tog_commands[0];
6552 cmd_argv = make_argv(argc, cmd->name);
6556 /* Did the user specify a command? */
6557 for (i = 0; i < nitems(tog_commands); i++) {
6558 if (strncmp(tog_commands[i].name, argv[0],
6559 strlen(argv[0])) == 0) {
6560 cmd = &tog_commands[i];
6569 /* No command specified; try log with a path */
6570 error = tog_log_with_path(argc, argv);
6575 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6582 for (i = 0; i < argc; i++)
6587 if (error && error->code != GOT_ERR_CANCELLED)
6588 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);