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->first_displayed_entry = s->selected_entry = te;
1880 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1881 break; /* jump to this file's entry */
1883 slash = strchr(p, '/');
1885 subpath = strndup(path, slash - path);
1887 subpath = strdup(path);
1888 if (subpath == NULL) {
1889 err = got_error_from_errno("strdup");
1893 err = got_object_id_by_path(&tree_id, repo, commit_id,
1898 err = got_object_open_as_tree(&tree, repo, tree_id);
1903 err = tree_view_visit_subtree(tree, s);
1905 got_object_tree_close(tree);
1919 static const struct got_error *
1920 browse_commit_tree(struct tog_view **new_view, int begin_x,
1921 struct commit_queue_entry *entry, const char *path,
1922 struct got_repository *repo)
1924 const struct got_error *err = NULL;
1925 struct got_tree_object *tree;
1926 struct tog_tree_view_state *s;
1927 struct tog_view *tree_view;
1929 err = got_object_open_as_tree(&tree, repo,
1930 got_object_commit_get_tree_id(entry->commit));
1934 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1935 if (tree_view == NULL)
1936 return got_error_from_errno("view_open");
1938 err = open_tree_view(tree_view, tree, entry->id, repo);
1940 got_object_tree_close(tree);
1943 s = &tree_view->state.tree;
1945 *new_view = tree_view;
1947 if (got_path_is_root_dir(path))
1950 return tree_view_walk_path(s, entry->id, path, repo);
1953 static const struct got_error *
1954 block_signals_used_by_main_thread(void)
1959 if (sigemptyset(&sigset) == -1)
1960 return got_error_from_errno("sigemptyset");
1962 /* tog handles SIGWINCH and SIGCONT */
1963 if (sigaddset(&sigset, SIGWINCH) == -1)
1964 return got_error_from_errno("sigaddset");
1965 if (sigaddset(&sigset, SIGCONT) == -1)
1966 return got_error_from_errno("sigaddset");
1968 /* ncurses handles SIGTSTP */
1969 if (sigaddset(&sigset, SIGTSTP) == -1)
1970 return got_error_from_errno("sigaddset");
1972 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1974 return got_error_set_errno(errcode, "pthread_sigmask");
1980 log_thread(void *arg)
1982 const struct got_error *err = NULL;
1984 struct tog_log_thread_args *a = arg;
1987 err = block_signals_used_by_main_thread();
1991 while (!done && !err && !tog_sigpipe_received) {
1992 err = queue_commits(a->graph, a->commits, 1, a->repo,
1993 a->in_repo_path, a->searching, a->search_next_done,
1996 if (err->code != GOT_ERR_ITER_COMPLETED)
2000 } else if (a->commits_needed > 0)
2001 a->commits_needed--;
2003 errcode = pthread_mutex_lock(&tog_mutex);
2005 err = got_error_set_errno(errcode,
2006 "pthread_mutex_lock");
2008 } else if (*a->quit)
2010 else if (*a->first_displayed_entry == NULL) {
2011 *a->first_displayed_entry =
2012 TAILQ_FIRST(&a->commits->head);
2013 *a->selected_entry = *a->first_displayed_entry;
2016 errcode = pthread_cond_signal(&a->commit_loaded);
2018 err = got_error_set_errno(errcode,
2019 "pthread_cond_signal");
2020 pthread_mutex_unlock(&tog_mutex);
2025 a->commits_needed = 0;
2027 if (a->commits_needed == 0) {
2028 errcode = pthread_cond_wait(&a->need_commits,
2031 err = got_error_set_errno(errcode,
2032 "pthread_cond_wait");
2036 errcode = pthread_mutex_unlock(&tog_mutex);
2037 if (errcode && err == NULL)
2038 err = got_error_set_errno(errcode,
2039 "pthread_mutex_unlock");
2041 a->log_complete = 1;
2045 static const struct got_error *
2046 stop_log_thread(struct tog_log_view_state *s)
2048 const struct got_error *err = NULL;
2053 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2055 return got_error_set_errno(errcode,
2056 "pthread_cond_signal");
2057 errcode = pthread_mutex_unlock(&tog_mutex);
2059 return got_error_set_errno(errcode,
2060 "pthread_mutex_unlock");
2061 errcode = pthread_join(s->thread, (void **)&err);
2063 return got_error_set_errno(errcode, "pthread_join");
2064 errcode = pthread_mutex_lock(&tog_mutex);
2066 return got_error_set_errno(errcode,
2067 "pthread_mutex_lock");
2071 if (s->thread_args.repo) {
2072 got_repo_close(s->thread_args.repo);
2073 s->thread_args.repo = NULL;
2076 if (s->thread_args.graph) {
2077 got_commit_graph_close(s->thread_args.graph);
2078 s->thread_args.graph = NULL;
2084 static const struct got_error *
2085 close_log_view(struct tog_view *view)
2087 const struct got_error *err = NULL;
2088 struct tog_log_view_state *s = &view->state.log;
2091 err = stop_log_thread(s);
2093 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2094 if (errcode && err == NULL)
2095 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2097 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2098 if (errcode && err == NULL)
2099 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2101 free_commits(&s->commits);
2102 free(s->in_repo_path);
2103 s->in_repo_path = NULL;
2106 got_ref_list_free(&s->refs);
2110 static const struct got_error *
2111 search_start_log_view(struct tog_view *view)
2113 struct tog_log_view_state *s = &view->state.log;
2115 s->matched_entry = NULL;
2116 s->search_entry = NULL;
2120 static const struct got_error *
2121 search_next_log_view(struct tog_view *view)
2123 const struct got_error *err = NULL;
2124 struct tog_log_view_state *s = &view->state.log;
2125 struct commit_queue_entry *entry;
2127 /* Display progress update in log view. */
2128 show_log_view(view);
2132 if (s->search_entry) {
2134 errcode = pthread_mutex_unlock(&tog_mutex);
2136 return got_error_set_errno(errcode,
2137 "pthread_mutex_unlock");
2138 ch = wgetch(view->window);
2139 errcode = pthread_mutex_lock(&tog_mutex);
2141 return got_error_set_errno(errcode,
2142 "pthread_mutex_lock");
2143 if (ch == KEY_BACKSPACE) {
2144 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2147 if (view->searching == TOG_SEARCH_FORWARD)
2148 entry = TAILQ_NEXT(s->search_entry, entry);
2150 entry = TAILQ_PREV(s->search_entry,
2151 commit_queue_head, entry);
2152 } else if (s->matched_entry) {
2153 if (view->searching == TOG_SEARCH_FORWARD)
2154 entry = TAILQ_NEXT(s->matched_entry, entry);
2156 entry = TAILQ_PREV(s->matched_entry,
2157 commit_queue_head, entry);
2159 if (view->searching == TOG_SEARCH_FORWARD)
2160 entry = TAILQ_FIRST(&s->commits.head);
2162 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2168 if (entry == NULL) {
2169 if (s->thread_args.log_complete ||
2170 view->searching == TOG_SEARCH_BACKWARD) {
2171 view->search_next_done =
2172 (s->matched_entry == NULL ?
2173 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2174 s->search_entry = NULL;
2178 * Poke the log thread for more commits and return,
2179 * allowing the main loop to make progress. Search
2180 * will resume at s->search_entry once we come back.
2182 s->thread_args.commits_needed++;
2183 return trigger_log_thread(view, 0,
2184 &s->thread_args.commits_needed,
2185 &s->thread_args.log_complete,
2186 &s->thread_args.need_commits,
2187 &s->thread_args.commit_loaded);
2190 err = match_commit(&have_match, entry->id, entry->commit,
2195 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2196 s->matched_entry = entry;
2200 s->search_entry = entry;
2201 if (view->searching == TOG_SEARCH_FORWARD)
2202 entry = TAILQ_NEXT(entry, entry);
2204 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2207 if (s->matched_entry) {
2208 int cur = s->selected_entry->idx;
2209 while (cur < s->matched_entry->idx) {
2210 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2215 while (cur > s->matched_entry->idx) {
2216 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2223 s->search_entry = NULL;
2228 static const struct got_error *
2229 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2230 struct got_repository *repo, const char *head_ref_name,
2231 const char *in_repo_path, int log_branches)
2233 const struct got_error *err = NULL;
2234 struct tog_log_view_state *s = &view->state.log;
2235 struct got_repository *thread_repo = NULL;
2236 struct got_commit_graph *thread_graph = NULL;
2239 SIMPLEQ_INIT(&s->refs);
2241 if (in_repo_path != s->in_repo_path) {
2242 free(s->in_repo_path);
2243 s->in_repo_path = strdup(in_repo_path);
2244 if (s->in_repo_path == NULL)
2245 return got_error_from_errno("strdup");
2248 /* The commit queue only contains commits being displayed. */
2249 TAILQ_INIT(&s->commits.head);
2250 s->commits.ncommits = 0;
2252 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2257 s->head_ref_name = head_ref_name;
2258 s->start_id = got_object_id_dup(start_id);
2259 if (s->start_id == NULL) {
2260 err = got_error_from_errno("got_object_id_dup");
2263 s->log_branches = log_branches;
2265 SIMPLEQ_INIT(&s->colors);
2266 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2267 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2268 get_color_value("TOG_COLOR_COMMIT"));
2271 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2272 get_color_value("TOG_COLOR_AUTHOR"));
2274 free_colors(&s->colors);
2277 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2278 get_color_value("TOG_COLOR_DATE"));
2280 free_colors(&s->colors);
2285 view->show = show_log_view;
2286 view->input = input_log_view;
2287 view->close = close_log_view;
2288 view->search_start = search_start_log_view;
2289 view->search_next = search_next_log_view;
2291 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2294 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2298 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2299 s->repo, NULL, NULL);
2303 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2305 err = got_error_set_errno(errcode, "pthread_cond_init");
2308 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2310 err = got_error_set_errno(errcode, "pthread_cond_init");
2314 s->thread_args.commits_needed = view->nlines;
2315 s->thread_args.graph = thread_graph;
2316 s->thread_args.commits = &s->commits;
2317 s->thread_args.in_repo_path = s->in_repo_path;
2318 s->thread_args.start_id = s->start_id;
2319 s->thread_args.repo = thread_repo;
2320 s->thread_args.log_complete = 0;
2321 s->thread_args.quit = &s->quit;
2322 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2323 s->thread_args.selected_entry = &s->selected_entry;
2324 s->thread_args.searching = &view->searching;
2325 s->thread_args.search_next_done = &view->search_next_done;
2326 s->thread_args.regex = &view->regex;
2329 close_log_view(view);
2333 static const struct got_error *
2334 show_log_view(struct tog_view *view)
2336 const struct got_error *err;
2337 struct tog_log_view_state *s = &view->state.log;
2339 if (s->thread == NULL) {
2340 int errcode = pthread_create(&s->thread, NULL, log_thread,
2343 return got_error_set_errno(errcode, "pthread_create");
2344 if (s->thread_args.commits_needed > 0) {
2345 err = trigger_log_thread(view, 1,
2346 &s->thread_args.commits_needed,
2347 &s->thread_args.log_complete,
2348 &s->thread_args.need_commits,
2349 &s->thread_args.commit_loaded);
2355 return draw_commits(view, &s->last_displayed_entry,
2356 &s->selected_entry, s->first_displayed_entry,
2357 &s->commits, s->selected, view->nlines, &s->refs,
2358 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2361 static const struct got_error *
2362 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2363 struct tog_view **focus_view, struct tog_view *view, int ch)
2365 const struct got_error *err = NULL;
2366 struct tog_log_view_state *s = &view->state.log;
2367 char *parent_path, *in_repo_path = NULL;
2368 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2369 struct tog_view *ref_view = NULL;
2371 struct got_object_id *start_id;
2381 if (s->first_displayed_entry == NULL)
2383 if (s->selected > 0)
2386 scroll_up(view, &s->first_displayed_entry, 1,
2391 if (s->first_displayed_entry == NULL)
2393 if (TAILQ_FIRST(&s->commits.head) ==
2394 s->first_displayed_entry) {
2398 scroll_up(view, &s->first_displayed_entry,
2399 view->nlines - 1, &s->commits);
2405 if (s->first_displayed_entry == NULL)
2407 if (s->selected < MIN(view->nlines - 2,
2408 s->commits.ncommits - 1)) {
2412 err = scroll_down(view, &s->first_displayed_entry, 1,
2413 &s->last_displayed_entry, &s->commits,
2414 &s->thread_args.log_complete,
2415 &s->thread_args.commits_needed,
2416 &s->thread_args.need_commits,
2417 &s->thread_args.commit_loaded);
2421 struct commit_queue_entry *first;
2422 first = s->first_displayed_entry;
2425 err = scroll_down(view, &s->first_displayed_entry,
2426 view->nlines - 1, &s->last_displayed_entry,
2427 &s->commits, &s->thread_args.log_complete,
2428 &s->thread_args.commits_needed,
2429 &s->thread_args.need_commits,
2430 &s->thread_args.commit_loaded);
2433 if (first == s->first_displayed_entry &&
2434 s->selected < MIN(view->nlines - 2,
2435 s->commits.ncommits - 1)) {
2436 /* can't scroll further down */
2437 s->selected = MIN(view->nlines - 2,
2438 s->commits.ncommits - 1);
2444 if (s->selected > view->nlines - 2)
2445 s->selected = view->nlines - 2;
2446 if (s->selected > s->commits.ncommits - 1)
2447 s->selected = s->commits.ncommits - 1;
2452 if (s->selected_entry == NULL)
2454 if (view_is_parent_view(view))
2455 begin_x = view_split_begin_x(view->begin_x);
2456 err = open_diff_view_for_commit(&diff_view, begin_x,
2457 s->selected_entry->commit, s->selected_entry->id,
2461 if (view_is_parent_view(view)) {
2462 err = view_close_child(view);
2465 err = view_set_child(view, diff_view);
2467 view_close(diff_view);
2470 *focus_view = diff_view;
2471 view->child_focussed = 1;
2473 *new_view = diff_view;
2476 if (s->selected_entry == NULL)
2478 if (view_is_parent_view(view))
2479 begin_x = view_split_begin_x(view->begin_x);
2480 err = browse_commit_tree(&tree_view, begin_x,
2481 s->selected_entry, s->in_repo_path, s->repo);
2484 if (view_is_parent_view(view)) {
2485 err = view_close_child(view);
2488 err = view_set_child(view, tree_view);
2490 view_close(tree_view);
2493 *focus_view = tree_view;
2494 view->child_focussed = 1;
2496 *new_view = tree_view;
2499 if (got_path_cmp(s->in_repo_path, "/",
2500 strlen(s->in_repo_path), 1) == 0)
2502 err = got_path_dirname(&parent_path, s->in_repo_path);
2505 err = stop_log_thread(s);
2510 lv = view_open(view->nlines, view->ncols,
2511 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2514 return got_error_from_errno("view_open");
2516 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2517 parent_path, s->log_branches);
2521 if (view_is_parent_view(view))
2524 view_set_child(view->parent, lv);
2529 err = stop_log_thread(s);
2532 lv = view_open(view->nlines, view->ncols,
2533 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2535 return got_error_from_errno("view_open");
2536 err = got_repo_match_object_id(&start_id, NULL,
2537 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2538 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2543 in_repo_path = strdup(s->in_repo_path);
2544 if (in_repo_path == NULL) {
2547 return got_error_from_errno("strdup");
2549 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2550 in_repo_path, s->log_branches);
2560 s->log_branches = !s->log_branches;
2561 err = stop_log_thread(s);
2564 lv = view_open(view->nlines, view->ncols,
2565 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2567 return got_error_from_errno("view_open");
2568 err = open_log_view(lv, s->start_id, s->repo,
2569 s->head_ref_name, s->in_repo_path, s->log_branches);
2578 if (view_is_parent_view(view))
2579 begin_x = view_split_begin_x(view->begin_x);
2580 ref_view = view_open(view->nlines, view->ncols,
2581 view->begin_y, begin_x, TOG_VIEW_REF);
2582 if (ref_view == NULL)
2583 return got_error_from_errno("view_open");
2584 err = open_ref_view(ref_view, s->repo);
2586 view_close(ref_view);
2589 if (view_is_parent_view(view)) {
2590 err = view_close_child(view);
2593 err = view_set_child(view, ref_view);
2595 view_close(ref_view);
2598 *focus_view = ref_view;
2599 view->child_focussed = 1;
2601 *new_view = ref_view;
2610 static const struct got_error *
2611 apply_unveil(const char *repo_path, const char *worktree_path)
2613 const struct got_error *error;
2616 if (unveil("gmon.out", "rwc") != 0)
2617 return got_error_from_errno2("unveil", "gmon.out");
2619 if (repo_path && unveil(repo_path, "r") != 0)
2620 return got_error_from_errno2("unveil", repo_path);
2622 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2623 return got_error_from_errno2("unveil", worktree_path);
2625 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2626 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2628 error = got_privsep_unveil_exec_helpers();
2632 if (unveil(NULL, NULL) != 0)
2633 return got_error_from_errno("unveil");
2643 halfdelay(1); /* Do fast refresh while initial view is loading. */
2646 intrflush(stdscr, FALSE);
2647 keypad(stdscr, TRUE);
2649 if (getenv("TOG_COLORS") != NULL) {
2651 use_default_colors();
2653 signal(SIGWINCH, tog_sigwinch);
2654 signal(SIGPIPE, tog_sigpipe);
2655 signal(SIGCONT, tog_sigcont);
2658 static const struct got_error *
2659 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2660 struct got_repository *repo, struct got_worktree *worktree)
2662 const struct got_error *err = NULL;
2665 *in_repo_path = strdup("/");
2666 if (*in_repo_path == NULL)
2667 return got_error_from_errno("strdup");
2672 const char *prefix = got_worktree_get_path_prefix(worktree);
2675 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2678 if (asprintf(in_repo_path, "%s%s%s", prefix,
2679 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2681 err = got_error_from_errno("asprintf");
2682 *in_repo_path = NULL;
2686 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2691 static const struct got_error *
2692 cmd_log(int argc, char *argv[])
2694 const struct got_error *error;
2695 struct got_repository *repo = NULL;
2696 struct got_worktree *worktree = NULL;
2697 struct got_object_id *start_id = NULL;
2698 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2699 char *start_commit = NULL, *head_ref_name = NULL;
2700 int ch, log_branches = 0;
2701 struct tog_view *view;
2704 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2709 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2715 start_commit = optarg;
2718 repo_path = realpath(optarg, NULL);
2719 if (repo_path == NULL)
2720 return got_error_from_errno2("realpath",
2735 cwd = getcwd(NULL, 0);
2737 return got_error_from_errno("getcwd");
2739 error = got_worktree_open(&worktree, cwd);
2740 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2743 if (repo_path == NULL) {
2746 strdup(got_worktree_get_repo_path(worktree));
2748 repo_path = strdup(cwd);
2750 if (repo_path == NULL) {
2751 error = got_error_from_errno("strdup");
2755 error = got_repo_open(&repo, repo_path, NULL);
2759 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2766 error = apply_unveil(got_repo_get_path(repo),
2767 worktree ? got_worktree_get_root_path(worktree) : NULL);
2771 if (start_commit == NULL)
2772 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2773 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2774 GOT_OBJ_TYPE_COMMIT, 1, repo);
2776 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2777 GOT_OBJ_TYPE_COMMIT, 1, repo);
2781 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2783 error = got_error_from_errno("view_open");
2787 head_ref_name = strdup(
2788 got_worktree_get_head_ref_name(worktree));
2789 if (head_ref_name == NULL) {
2790 error = got_error_from_errno("strdup");
2794 error = open_log_view(view, start_id, repo, head_ref_name,
2795 in_repo_path, log_branches);
2799 /* Release work tree lock. */
2800 got_worktree_close(worktree);
2803 error = view_loop(view);
2809 free(head_ref_name);
2811 got_repo_close(repo);
2813 got_worktree_close(worktree);
2821 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2822 "[-w] object1 object2\n", getprogname());
2827 parse_next_line(FILE *f, size_t *len)
2832 const char delim[3] = { '\0', '\0', '\0'};
2834 line = fparseln(f, &linelen, &lineno, delim, 0);
2841 match_line(const char *line, regex_t *regex, size_t nmatch,
2842 regmatch_t *regmatch)
2844 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2848 match_color(struct tog_colors *colors, const char *line)
2850 struct tog_color *tc = NULL;
2852 SIMPLEQ_FOREACH(tc, colors, entry) {
2853 if (match_line(line, &tc->regex, 0, NULL))
2860 static const struct got_error *
2861 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2862 WINDOW *window, regmatch_t *regmatch)
2864 const struct got_error *err = NULL;
2871 s = strndup(line, regmatch->rm_so);
2873 return got_error_from_errno("strndup");
2875 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2880 waddwstr(window, wline);
2887 s = strndup(line + regmatch->rm_so,
2888 regmatch->rm_eo - regmatch->rm_so);
2890 err = got_error_from_errno("strndup");
2894 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2899 wattr_on(window, A_STANDOUT, NULL);
2900 waddwstr(window, wline);
2901 wattr_off(window, A_STANDOUT, NULL);
2908 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2909 err = format_line(&wline, &width,
2910 line + regmatch->rm_eo, wlimit, col_tab_align);
2913 waddwstr(window, wline);
2921 static const struct got_error *
2922 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2923 off_t *line_offsets, int selected_line, int max_lines,
2924 int *last_displayed_line, int *eof, const char *header,
2925 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2927 const struct got_error *err;
2930 struct tog_color *tc;
2936 line_offset = line_offsets[first_displayed_line - 1];
2937 if (fseeko(f, line_offset, SEEK_SET) == -1)
2938 return got_error_from_errno("fseek");
2940 werase(view->window);
2943 if (asprintf(&line, "[%d/%d] %s",
2944 first_displayed_line - 1 + selected_line, nlines,
2946 return got_error_from_errno("asprintf");
2947 err = format_line(&wline, &width, line, view->ncols, 0);
2952 if (view_needs_focus_indication(view))
2953 wstandout(view->window);
2954 waddwstr(view->window, wline);
2957 if (view_needs_focus_indication(view))
2958 wstandend(view->window);
2959 if (width <= view->ncols - 1)
2960 waddch(view->window, '\n');
2968 while (max_lines > 0 && nprinted < max_lines) {
2969 line = parse_next_line(f, &len);
2975 tc = match_color(colors, line);
2977 wattr_on(view->window,
2978 COLOR_PAIR(tc->colorpair), NULL);
2979 if (first_displayed_line + nprinted == matched_line &&
2980 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2981 err = add_matched_line(&width, line, view->ncols, 0,
2982 view->window, regmatch);
2988 err = format_line(&wline, &width, line, view->ncols, 0);
2993 waddwstr(view->window, wline);
2998 wattr_off(view->window,
2999 COLOR_PAIR(tc->colorpair), NULL);
3000 if (width <= view->ncols - 1)
3001 waddch(view->window, '\n');
3006 *last_displayed_line = first_displayed_line + (nprinted - 1);
3008 *last_displayed_line = first_displayed_line;
3013 while (nprinted < view->nlines) {
3014 waddch(view->window, '\n');
3018 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3023 wstandout(view->window);
3024 waddwstr(view->window, wline);
3027 wstandend(view->window);
3034 get_datestr(time_t *time, char *datebuf)
3036 struct tm mytm, *tm;
3039 tm = gmtime_r(time, &mytm);
3042 s = asctime_r(tm, datebuf);
3045 p = strchr(s, '\n');
3051 static const struct got_error *
3052 get_changed_paths(struct got_pathlist_head *paths,
3053 struct got_commit_object *commit, struct got_repository *repo)
3055 const struct got_error *err = NULL;
3056 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3057 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3058 struct got_object_qid *qid;
3060 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3062 struct got_commit_object *pcommit;
3063 err = got_object_open_as_commit(&pcommit, repo,
3068 tree_id1 = got_object_commit_get_tree_id(pcommit);
3069 got_object_commit_close(pcommit);
3074 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3079 tree_id2 = got_object_commit_get_tree_id(commit);
3080 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3084 err = got_diff_tree(tree1, tree2, "", "", repo,
3085 got_diff_tree_collect_changed_paths, paths, 0);
3088 got_object_tree_close(tree1);
3090 got_object_tree_close(tree2);
3094 static const struct got_error *
3095 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3099 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3101 return got_error_from_errno("reallocarray");
3103 (*line_offsets)[*nlines] = off;
3108 static const struct got_error *
3109 write_commit_info(off_t **line_offsets, size_t *nlines,
3110 struct got_object_id *commit_id, struct got_reflist_head *refs,
3111 struct got_repository *repo, FILE *outfile)
3113 const struct got_error *err = NULL;
3114 char datebuf[26], *datestr;
3115 struct got_commit_object *commit;
3116 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3117 time_t committer_time;
3118 const char *author, *committer;
3119 char *refs_str = NULL;
3120 struct got_pathlist_head changed_paths;
3121 struct got_pathlist_entry *pe;
3125 TAILQ_INIT(&changed_paths);
3128 err = build_refs_str(&refs_str, refs, commit_id, repo);
3133 err = got_object_open_as_commit(&commit, repo, commit_id);
3137 err = got_object_id_str(&id_str, commit_id);
3139 err = got_error_from_errno("got_object_id_str");
3143 err = add_line_offset(line_offsets, nlines, 0);
3147 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3148 refs_str ? refs_str : "", refs_str ? ")" : "");
3150 err = got_error_from_errno("fprintf");
3154 err = add_line_offset(line_offsets, nlines, outoff);
3158 n = fprintf(outfile, "from: %s\n",
3159 got_object_commit_get_author(commit));
3161 err = got_error_from_errno("fprintf");
3165 err = add_line_offset(line_offsets, nlines, outoff);
3169 committer_time = got_object_commit_get_committer_time(commit);
3170 datestr = get_datestr(&committer_time, datebuf);
3172 n = fprintf(outfile, "date: %s UTC\n", datestr);
3174 err = got_error_from_errno("fprintf");
3178 err = add_line_offset(line_offsets, nlines, outoff);
3182 author = got_object_commit_get_author(commit);
3183 committer = got_object_commit_get_committer(commit);
3184 if (strcmp(author, committer) != 0) {
3185 n = fprintf(outfile, "via: %s\n", committer);
3187 err = got_error_from_errno("fprintf");
3191 err = add_line_offset(line_offsets, nlines, outoff);
3195 err = got_object_commit_get_logmsg(&logmsg, commit);
3199 while ((line = strsep(&s, "\n")) != NULL) {
3200 n = fprintf(outfile, "%s\n", line);
3202 err = got_error_from_errno("fprintf");
3206 err = add_line_offset(line_offsets, nlines, outoff);
3211 err = get_changed_paths(&changed_paths, commit, repo);
3214 TAILQ_FOREACH(pe, &changed_paths, entry) {
3215 struct got_diff_changed_path *cp = pe->data;
3216 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3218 err = got_error_from_errno("fprintf");
3222 err = add_line_offset(line_offsets, nlines, outoff);
3225 free((char *)pe->path);
3229 fputc('\n', outfile);
3231 err = add_line_offset(line_offsets, nlines, outoff);
3233 got_pathlist_free(&changed_paths);
3237 got_object_commit_close(commit);
3239 free(*line_offsets);
3240 *line_offsets = NULL;
3246 static const struct got_error *
3247 create_diff(struct tog_diff_view_state *s)
3249 const struct got_error *err = NULL;
3253 free(s->line_offsets);
3254 s->line_offsets = malloc(sizeof(off_t));
3255 if (s->line_offsets == NULL)
3256 return got_error_from_errno("malloc");
3261 err = got_error_from_errno("got_opentemp");
3264 if (s->f && fclose(s->f) != 0) {
3265 err = got_error_from_errno("fclose");
3271 err = got_object_get_type(&obj_type, s->repo, s->id1);
3273 err = got_object_get_type(&obj_type, s->repo, s->id2);
3278 case GOT_OBJ_TYPE_BLOB:
3279 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3280 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3281 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3283 case GOT_OBJ_TYPE_TREE:
3284 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3285 s->id1, s->id2, "", "", s->diff_context,
3286 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3288 case GOT_OBJ_TYPE_COMMIT: {
3289 const struct got_object_id_queue *parent_ids;
3290 struct got_object_qid *pid;
3291 struct got_commit_object *commit2;
3293 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3296 /* Show commit info if we're diffing to a parent/root commit. */
3297 if (s->id1 == NULL) {
3298 err = write_commit_info(&s->line_offsets, &s->nlines,
3299 s->id2, &s->refs, s->repo, s->f);
3303 parent_ids = got_object_commit_get_parent_ids(commit2);
3304 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3305 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3306 err = write_commit_info(
3307 &s->line_offsets, &s->nlines,
3308 s->id2, &s->refs, s->repo, s->f);
3315 got_object_commit_close(commit2);
3317 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3318 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3319 s->force_text_diff, s->repo, s->f);
3323 err = got_error(GOT_ERR_OBJ_TYPE);
3329 if (s->f && fflush(s->f) != 0 && err == NULL)
3330 err = got_error_from_errno("fflush");
3335 diff_view_indicate_progress(struct tog_view *view)
3337 mvwaddstr(view->window, 0, 0, "diffing...");
3342 static const struct got_error *
3343 search_start_diff_view(struct tog_view *view)
3345 struct tog_diff_view_state *s = &view->state.diff;
3347 s->matched_line = 0;
3351 static const struct got_error *
3352 search_next_diff_view(struct tog_view *view)
3354 struct tog_diff_view_state *s = &view->state.diff;
3357 if (!view->searching) {
3358 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3362 if (s->matched_line) {
3363 if (view->searching == TOG_SEARCH_FORWARD)
3364 lineno = s->matched_line + 1;
3366 lineno = s->matched_line - 1;
3368 if (view->searching == TOG_SEARCH_FORWARD)
3379 if (lineno <= 0 || lineno > s->nlines) {
3380 if (s->matched_line == 0) {
3381 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3386 if (view->searching == TOG_SEARCH_FORWARD)
3392 offset = s->line_offsets[lineno - 1];
3393 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3395 return got_error_from_errno("fseeko");
3398 line = parse_next_line(s->f, &len);
3400 match_line(line, &view->regex, 1, &view->regmatch)) {
3401 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3402 s->matched_line = lineno;
3407 if (view->searching == TOG_SEARCH_FORWARD)
3413 if (s->matched_line) {
3414 s->first_displayed_line = s->matched_line;
3415 s->selected_line = 1;
3421 static const struct got_error *
3422 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3423 struct got_object_id *id2, const char *label1, const char *label2,
3424 int diff_context, int ignore_whitespace, int force_text_diff,
3425 struct tog_view *log_view, struct got_repository *repo)
3427 const struct got_error *err;
3428 struct tog_diff_view_state *s = &view->state.diff;
3430 SIMPLEQ_INIT(&s->refs);
3432 if (id1 != NULL && id2 != NULL) {
3434 err = got_object_get_type(&type1, repo, id1);
3437 err = got_object_get_type(&type2, repo, id2);
3442 return got_error(GOT_ERR_OBJ_TYPE);
3444 s->first_displayed_line = 1;
3445 s->last_displayed_line = view->nlines;
3446 s->selected_line = 1;
3454 s->id1 = got_object_id_dup(id1);
3456 return got_error_from_errno("got_object_id_dup");
3460 s->id2 = got_object_id_dup(id2);
3461 if (s->id2 == NULL) {
3464 return got_error_from_errno("got_object_id_dup");
3467 s->first_displayed_line = 1;
3468 s->last_displayed_line = view->nlines;
3469 s->diff_context = diff_context;
3470 s->ignore_whitespace = ignore_whitespace;
3471 s->force_text_diff = force_text_diff;
3472 s->log_view = log_view;
3475 SIMPLEQ_INIT(&s->colors);
3476 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3477 err = add_color(&s->colors,
3478 "^-", TOG_COLOR_DIFF_MINUS,
3479 get_color_value("TOG_COLOR_DIFF_MINUS"));
3482 err = add_color(&s->colors, "^\\+",
3483 TOG_COLOR_DIFF_PLUS,
3484 get_color_value("TOG_COLOR_DIFF_PLUS"));
3486 free_colors(&s->colors);
3489 err = add_color(&s->colors,
3490 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3491 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3493 free_colors(&s->colors);
3497 err = add_color(&s->colors,
3498 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3499 TOG_COLOR_DIFF_META,
3500 get_color_value("TOG_COLOR_DIFF_META"));
3502 free_colors(&s->colors);
3506 err = add_color(&s->colors,
3507 "^(from|via): ", TOG_COLOR_AUTHOR,
3508 get_color_value("TOG_COLOR_AUTHOR"));
3510 free_colors(&s->colors);
3514 err = add_color(&s->colors,
3515 "^date: ", TOG_COLOR_DATE,
3516 get_color_value("TOG_COLOR_DATE"));
3518 free_colors(&s->colors);
3523 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3529 free_colors(&s->colors);
3533 if (log_view && view_is_splitscreen(view))
3534 show_log_view(log_view); /* draw vborder */
3535 diff_view_indicate_progress(view);
3537 s->line_offsets = NULL;
3539 err = create_diff(s);
3545 free_colors(&s->colors);
3546 got_ref_list_free(&s->refs);
3550 view->show = show_diff_view;
3551 view->input = input_diff_view;
3552 view->close = close_diff_view;
3553 view->search_start = search_start_diff_view;
3554 view->search_next = search_next_diff_view;
3559 static const struct got_error *
3560 close_diff_view(struct tog_view *view)
3562 const struct got_error *err = NULL;
3563 struct tog_diff_view_state *s = &view->state.diff;
3569 if (s->f && fclose(s->f) == EOF)
3570 err = got_error_from_errno("fclose");
3571 free_colors(&s->colors);
3572 free(s->line_offsets);
3573 s->line_offsets = NULL;
3575 got_ref_list_free(&s->refs);
3579 static const struct got_error *
3580 show_diff_view(struct tog_view *view)
3582 const struct got_error *err;
3583 struct tog_diff_view_state *s = &view->state.diff;
3584 char *id_str1 = NULL, *id_str2, *header;
3585 const char *label1, *label2;
3588 err = got_object_id_str(&id_str1, s->id1);
3591 label1 = s->label1 ? : id_str1;
3593 label1 = "/dev/null";
3595 err = got_object_id_str(&id_str2, s->id2);
3598 label2 = s->label2 ? : id_str2;
3600 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3601 err = got_error_from_errno("asprintf");
3609 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3610 s->line_offsets, s->selected_line, view->nlines,
3611 &s->last_displayed_line, &s->eof, header, &s->colors,
3612 s->matched_line, &view->regmatch);
3615 static const struct got_error *
3616 set_selected_commit(struct tog_diff_view_state *s,
3617 struct commit_queue_entry *entry)
3619 const struct got_error *err;
3620 const struct got_object_id_queue *parent_ids;
3621 struct got_commit_object *selected_commit;
3622 struct got_object_qid *pid;
3625 s->id2 = got_object_id_dup(entry->id);
3627 return got_error_from_errno("got_object_id_dup");
3629 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3632 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3634 pid = SIMPLEQ_FIRST(parent_ids);
3635 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3636 got_object_commit_close(selected_commit);
3640 static const struct got_error *
3641 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3642 struct tog_view **focus_view, struct tog_view *view, int ch)
3644 const struct got_error *err = NULL;
3645 struct tog_diff_view_state *s = &view->state.diff;
3646 struct tog_log_view_state *ls;
3647 struct commit_queue_entry *entry;
3654 s->force_text_diff = !s->force_text_diff;
3656 s->ignore_whitespace = !s->ignore_whitespace;
3657 wclear(view->window);
3658 s->first_displayed_line = 1;
3659 s->last_displayed_line = view->nlines;
3660 diff_view_indicate_progress(view);
3661 err = create_diff(s);
3665 if (s->first_displayed_line > 1)
3666 s->first_displayed_line--;
3670 if (s->first_displayed_line == 1)
3673 while (i++ < view->nlines - 1 &&
3674 s->first_displayed_line > 1)
3675 s->first_displayed_line--;
3680 s->first_displayed_line++;
3688 while (!s->eof && i++ < view->nlines - 1) {
3690 line = parse_next_line(s->f, NULL);
3691 s->first_displayed_line++;
3697 if (s->diff_context > 0) {
3699 diff_view_indicate_progress(view);
3700 err = create_diff(s);
3701 if (s->first_displayed_line + view->nlines - 1 >
3703 s->first_displayed_line = 1;
3704 s->last_displayed_line = view->nlines;
3709 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3711 diff_view_indicate_progress(view);
3712 err = create_diff(s);
3717 if (s->log_view == NULL)
3719 ls = &s->log_view->state.log;
3720 entry = TAILQ_PREV(ls->selected_entry,
3721 commit_queue_head, entry);
3725 err = input_log_view(NULL, NULL, NULL, s->log_view,
3730 err = set_selected_commit(s, entry);
3734 s->first_displayed_line = 1;
3735 s->last_displayed_line = view->nlines;
3737 diff_view_indicate_progress(view);
3738 err = create_diff(s);
3742 if (s->log_view == NULL)
3744 ls = &s->log_view->state.log;
3746 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3747 ls->thread_args.commits_needed++;
3748 err = trigger_log_thread(s->log_view, 1,
3749 &ls->thread_args.commits_needed,
3750 &ls->thread_args.log_complete,
3751 &ls->thread_args.need_commits,
3752 &ls->thread_args.commit_loaded);
3756 err = input_log_view(NULL, NULL, NULL, s->log_view,
3761 entry = TAILQ_NEXT(ls->selected_entry, entry);
3765 err = set_selected_commit(s, entry);
3769 s->first_displayed_line = 1;
3770 s->last_displayed_line = view->nlines;
3772 diff_view_indicate_progress(view);
3773 err = create_diff(s);
3782 static const struct got_error *
3783 cmd_diff(int argc, char *argv[])
3785 const struct got_error *error = NULL;
3786 struct got_repository *repo = NULL;
3787 struct got_worktree *worktree = NULL;
3788 struct got_object_id *id1 = NULL, *id2 = NULL;
3789 char *repo_path = NULL, *cwd = NULL;
3790 char *id_str1 = NULL, *id_str2 = NULL;
3791 char *label1 = NULL, *label2 = NULL;
3792 int diff_context = 3, ignore_whitespace = 0;
3793 int ch, force_text_diff = 0;
3795 struct tog_view *view;
3798 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3802 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3805 force_text_diff = 1;
3808 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3811 err(1, "-C option %s", errstr);
3814 repo_path = realpath(optarg, NULL);
3815 if (repo_path == NULL)
3816 return got_error_from_errno2("realpath",
3818 got_path_strip_trailing_slashes(repo_path);
3821 ignore_whitespace = 1;
3833 usage_diff(); /* TODO show local worktree changes */
3834 } else if (argc == 2) {
3840 cwd = getcwd(NULL, 0);
3842 return got_error_from_errno("getcwd");
3844 error = got_worktree_open(&worktree, cwd);
3845 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3848 if (repo_path == NULL) {
3851 strdup(got_worktree_get_repo_path(worktree));
3853 repo_path = strdup(cwd);
3855 if (repo_path == NULL) {
3856 error = got_error_from_errno("strdup");
3860 error = got_repo_open(&repo, repo_path, NULL);
3866 error = apply_unveil(got_repo_get_path(repo), NULL);
3870 error = got_repo_match_object_id(&id1, &label1, id_str1,
3871 GOT_OBJ_TYPE_ANY, 1, repo);
3875 error = got_repo_match_object_id(&id2, &label2, id_str2,
3876 GOT_OBJ_TYPE_ANY, 1, repo);
3880 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3882 error = got_error_from_errno("view_open");
3885 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3886 ignore_whitespace, force_text_diff, NULL, repo);
3889 error = view_loop(view);
3896 got_repo_close(repo);
3898 got_worktree_close(worktree);
3906 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3911 struct tog_blame_line {
3913 struct got_object_id *id;
3916 static const struct got_error *
3917 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3918 const char *path, struct tog_blame_line *lines, int nlines,
3919 int blame_complete, int selected_line, int *first_displayed_line,
3920 int *last_displayed_line, int *eof, int max_lines,
3921 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3923 const struct got_error *err;
3924 int lineno = 0, nprinted = 0;
3929 struct tog_blame_line *blame_line;
3930 struct got_object_id *prev_id = NULL;
3932 struct tog_color *tc;
3934 err = got_object_id_str(&id_str, id);
3939 werase(view->window);
3941 if (asprintf(&line, "commit %s", id_str) == -1) {
3942 err = got_error_from_errno("asprintf");
3947 err = format_line(&wline, &width, line, view->ncols, 0);
3952 if (view_needs_focus_indication(view))
3953 wstandout(view->window);
3954 tc = get_color(colors, TOG_COLOR_COMMIT);
3956 wattr_on(view->window,
3957 COLOR_PAIR(tc->colorpair), NULL);
3958 waddwstr(view->window, wline);
3960 wattr_off(view->window,
3961 COLOR_PAIR(tc->colorpair), NULL);
3962 if (view_needs_focus_indication(view))
3963 wstandend(view->window);
3966 if (width < view->ncols - 1)
3967 waddch(view->window, '\n');
3969 if (asprintf(&line, "[%d/%d] %s%s",
3970 *first_displayed_line - 1 + selected_line, nlines,
3971 blame_complete ? "" : "annotating... ", path) == -1) {
3973 return got_error_from_errno("asprintf");
3976 err = format_line(&wline, &width, line, view->ncols, 0);
3981 waddwstr(view->window, wline);
3984 if (width < view->ncols - 1)
3985 waddch(view->window, '\n');
3988 while (nprinted < max_lines - 2) {
3989 line = parse_next_line(f, &len);
3994 if (++lineno < *first_displayed_line) {
3999 if (view->focussed && nprinted == selected_line - 1)
4000 wstandout(view->window);
4003 blame_line = &lines[lineno - 1];
4004 if (blame_line->annotated && prev_id &&
4005 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4007 nprinted == selected_line - 1)) {
4008 waddstr(view->window, " ");
4009 } else if (blame_line->annotated) {
4011 err = got_object_id_str(&id_str, blame_line->id);
4016 tc = get_color(colors, TOG_COLOR_COMMIT);
4018 wattr_on(view->window,
4019 COLOR_PAIR(tc->colorpair), NULL);
4020 wprintw(view->window, "%.8s", id_str);
4022 wattr_off(view->window,
4023 COLOR_PAIR(tc->colorpair), NULL);
4025 prev_id = blame_line->id;
4027 waddstr(view->window, "........");
4031 waddstr(view->window, "........");
4035 if (view->focussed && nprinted == selected_line - 1)
4036 wstandend(view->window);
4037 waddstr(view->window, " ");
4039 if (view->ncols <= 9) {
4041 wline = wcsdup(L"");
4042 if (wline == NULL) {
4043 err = got_error_from_errno("wcsdup");
4047 } else if (*first_displayed_line + nprinted == matched_line &&
4048 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4049 err = add_matched_line(&width, line, view->ncols - 9, 9,
4050 view->window, regmatch);
4057 err = format_line(&wline, &width, line,
4058 view->ncols - 9, 9);
4059 waddwstr(view->window, wline);
4065 if (width <= view->ncols - 1)
4066 waddch(view->window, '\n');
4067 if (++nprinted == 1)
4068 *first_displayed_line = lineno;
4071 *last_displayed_line = lineno;
4078 static const struct got_error *
4079 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4081 const struct got_error *err = NULL;
4082 struct tog_blame_cb_args *a = arg;
4083 struct tog_blame_line *line;
4086 if (nlines != a->nlines ||
4087 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4088 return got_error(GOT_ERR_RANGE);
4090 errcode = pthread_mutex_lock(&tog_mutex);
4092 return got_error_set_errno(errcode, "pthread_mutex_lock");
4094 if (*a->quit) { /* user has quit the blame view */
4095 err = got_error(GOT_ERR_ITER_COMPLETED);
4100 goto done; /* no change in this commit */
4102 line = &a->lines[lineno - 1];
4103 if (line->annotated)
4106 line->id = got_object_id_dup(id);
4107 if (line->id == NULL) {
4108 err = got_error_from_errno("got_object_id_dup");
4111 line->annotated = 1;
4113 errcode = pthread_mutex_unlock(&tog_mutex);
4115 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4120 blame_thread(void *arg)
4122 const struct got_error *err;
4123 struct tog_blame_thread_args *ta = arg;
4124 struct tog_blame_cb_args *a = ta->cb_args;
4127 err = block_signals_used_by_main_thread();
4131 err = got_blame(ta->path, a->commit_id, ta->repo,
4132 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4133 if (err && err->code == GOT_ERR_CANCELLED)
4136 errcode = pthread_mutex_lock(&tog_mutex);
4138 return (void *)got_error_set_errno(errcode,
4139 "pthread_mutex_lock");
4141 got_repo_close(ta->repo);
4145 errcode = pthread_mutex_unlock(&tog_mutex);
4146 if (errcode && err == NULL)
4147 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4152 static struct got_object_id *
4153 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4154 int first_displayed_line, int selected_line)
4156 struct tog_blame_line *line;
4161 line = &lines[first_displayed_line - 1 + selected_line - 1];
4162 if (!line->annotated)
4168 static const struct got_error *
4169 stop_blame(struct tog_blame *blame)
4171 const struct got_error *err = NULL;
4174 if (blame->thread) {
4176 errcode = pthread_mutex_unlock(&tog_mutex);
4178 return got_error_set_errno(errcode,
4179 "pthread_mutex_unlock");
4180 errcode = pthread_join(blame->thread, (void **)&err);
4182 return got_error_set_errno(errcode, "pthread_join");
4183 errcode = pthread_mutex_lock(&tog_mutex);
4185 return got_error_set_errno(errcode,
4186 "pthread_mutex_lock");
4187 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4189 blame->thread = NULL;
4191 if (blame->thread_args.repo) {
4192 got_repo_close(blame->thread_args.repo);
4193 blame->thread_args.repo = NULL;
4196 if (fclose(blame->f) != 0 && err == NULL)
4197 err = got_error_from_errno("fclose");
4201 for (i = 0; i < blame->nlines; i++)
4202 free(blame->lines[i].id);
4204 blame->lines = NULL;
4206 free(blame->cb_args.commit_id);
4207 blame->cb_args.commit_id = NULL;
4212 static const struct got_error *
4213 cancel_blame_view(void *arg)
4215 const struct got_error *err = NULL;
4219 errcode = pthread_mutex_lock(&tog_mutex);
4221 return got_error_set_errno(errcode,
4222 "pthread_mutex_unlock");
4225 err = got_error(GOT_ERR_CANCELLED);
4227 errcode = pthread_mutex_unlock(&tog_mutex);
4229 return got_error_set_errno(errcode,
4230 "pthread_mutex_lock");
4235 static const struct got_error *
4236 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4237 int *first_displayed_line, int *last_displayed_line, int *selected_line,
4238 int *done, int *eof, const char *path, struct got_object_id *commit_id,
4239 struct got_repository *repo)
4241 const struct got_error *err = NULL;
4242 struct got_blob_object *blob = NULL;
4243 struct got_repository *thread_repo = NULL;
4244 struct got_object_id *obj_id = NULL;
4247 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4251 err = got_object_get_type(&obj_type, repo, obj_id);
4255 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4256 err = got_error(GOT_ERR_OBJ_TYPE);
4260 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4263 blame->f = got_opentemp();
4264 if (blame->f == NULL) {
4265 err = got_error_from_errno("got_opentemp");
4268 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4269 &blame->line_offsets, blame->f, blob);
4270 if (err || blame->nlines == 0)
4273 /* Don't include \n at EOF in the blame line count. */
4274 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4277 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4278 if (blame->lines == NULL) {
4279 err = got_error_from_errno("calloc");
4283 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4287 blame->cb_args.view = view;
4288 blame->cb_args.lines = blame->lines;
4289 blame->cb_args.nlines = blame->nlines;
4290 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4291 if (blame->cb_args.commit_id == NULL) {
4292 err = got_error_from_errno("got_object_id_dup");
4295 blame->cb_args.quit = done;
4297 blame->thread_args.path = path;
4298 blame->thread_args.repo = thread_repo;
4299 blame->thread_args.cb_args = &blame->cb_args;
4300 blame->thread_args.complete = blame_complete;
4301 blame->thread_args.cancel_cb = cancel_blame_view;
4302 blame->thread_args.cancel_arg = done;
4303 *blame_complete = 0;
4307 got_object_blob_close(blob);
4314 static const struct got_error *
4315 open_blame_view(struct tog_view *view, char *path,
4316 struct got_object_id *commit_id, struct got_repository *repo)
4318 const struct got_error *err = NULL;
4319 struct tog_blame_view_state *s = &view->state.blame;
4321 SIMPLEQ_INIT(&s->blamed_commits);
4323 s->path = strdup(path);
4324 if (s->path == NULL)
4325 return got_error_from_errno("strdup");
4327 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4333 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4334 s->first_displayed_line = 1;
4335 s->last_displayed_line = view->nlines;
4336 s->selected_line = 1;
4337 s->blame_complete = 0;
4339 s->commit_id = commit_id;
4340 memset(&s->blame, 0, sizeof(s->blame));
4342 SIMPLEQ_INIT(&s->colors);
4343 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4344 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4345 get_color_value("TOG_COLOR_COMMIT"));
4350 view->show = show_blame_view;
4351 view->input = input_blame_view;
4352 view->close = close_blame_view;
4353 view->search_start = search_start_blame_view;
4354 view->search_next = search_next_blame_view;
4356 return run_blame(&s->blame, view, &s->blame_complete,
4357 &s->first_displayed_line, &s->last_displayed_line,
4358 &s->selected_line, &s->done, &s->eof, s->path,
4359 s->blamed_commit->id, s->repo);
4362 static const struct got_error *
4363 close_blame_view(struct tog_view *view)
4365 const struct got_error *err = NULL;
4366 struct tog_blame_view_state *s = &view->state.blame;
4368 if (s->blame.thread)
4369 err = stop_blame(&s->blame);
4371 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4372 struct got_object_qid *blamed_commit;
4373 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4374 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4375 got_object_qid_free(blamed_commit);
4379 free_colors(&s->colors);
4384 static const struct got_error *
4385 search_start_blame_view(struct tog_view *view)
4387 struct tog_blame_view_state *s = &view->state.blame;
4389 s->matched_line = 0;
4393 static const struct got_error *
4394 search_next_blame_view(struct tog_view *view)
4396 struct tog_blame_view_state *s = &view->state.blame;
4399 if (!view->searching) {
4400 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4404 if (s->matched_line) {
4405 if (view->searching == TOG_SEARCH_FORWARD)
4406 lineno = s->matched_line + 1;
4408 lineno = s->matched_line - 1;
4410 if (view->searching == TOG_SEARCH_FORWARD)
4413 lineno = s->blame.nlines;
4421 if (lineno <= 0 || lineno > s->blame.nlines) {
4422 if (s->matched_line == 0) {
4423 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4428 if (view->searching == TOG_SEARCH_FORWARD)
4431 lineno = s->blame.nlines;
4434 offset = s->blame.line_offsets[lineno - 1];
4435 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4437 return got_error_from_errno("fseeko");
4440 line = parse_next_line(s->blame.f, &len);
4442 match_line(line, &view->regex, 1, &view->regmatch)) {
4443 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4444 s->matched_line = lineno;
4449 if (view->searching == TOG_SEARCH_FORWARD)
4455 if (s->matched_line) {
4456 s->first_displayed_line = s->matched_line;
4457 s->selected_line = 1;
4463 static const struct got_error *
4464 show_blame_view(struct tog_view *view)
4466 const struct got_error *err = NULL;
4467 struct tog_blame_view_state *s = &view->state.blame;
4470 if (s->blame.thread == NULL) {
4471 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4472 &s->blame.thread_args);
4474 return got_error_set_errno(errcode, "pthread_create");
4476 halfdelay(1); /* fast refresh while annotating */
4479 if (s->blame_complete)
4480 halfdelay(10); /* disable fast refresh */
4482 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4483 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4484 s->selected_line, &s->first_displayed_line,
4485 &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4486 s->matched_line, &view->regmatch);
4492 static const struct got_error *
4493 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4494 struct tog_view **focus_view, struct tog_view *view, int ch)
4496 const struct got_error *err = NULL, *thread_err = NULL;
4497 struct tog_view *diff_view;
4498 struct tog_blame_view_state *s = &view->state.blame;
4507 if (s->selected_line > 1)
4509 else if (s->selected_line == 1 &&
4510 s->first_displayed_line > 1)
4511 s->first_displayed_line--;
4515 if (s->first_displayed_line == 1) {
4516 s->selected_line = 1;
4519 if (s->first_displayed_line > view->nlines - 2)
4520 s->first_displayed_line -=
4523 s->first_displayed_line = 1;
4527 if (s->selected_line < view->nlines - 2 &&
4528 s->first_displayed_line +
4529 s->selected_line <= s->blame.nlines)
4531 else if (s->last_displayed_line <
4533 s->first_displayed_line++;
4537 struct got_object_id *id = NULL;
4538 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4539 s->first_displayed_line, s->selected_line);
4543 struct got_commit_object *commit;
4544 struct got_object_qid *pid;
4545 struct got_object_id *blob_id = NULL;
4547 err = got_object_open_as_commit(&commit,
4551 pid = SIMPLEQ_FIRST(
4552 got_object_commit_get_parent_ids(commit));
4554 got_object_commit_close(commit);
4557 /* Check if path history ends here. */
4558 err = got_object_id_by_path(&blob_id, s->repo,
4561 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4563 got_object_commit_close(commit);
4566 err = got_object_get_type(&obj_type, s->repo,
4569 /* Can't blame non-blob type objects. */
4570 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4571 got_object_commit_close(commit);
4574 err = got_object_qid_alloc(&s->blamed_commit,
4576 got_object_commit_close(commit);
4578 if (got_object_id_cmp(id,
4579 s->blamed_commit->id) == 0)
4581 err = got_object_qid_alloc(&s->blamed_commit,
4587 thread_err = stop_blame(&s->blame);
4591 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4592 s->blamed_commit, entry);
4593 err = run_blame(&s->blame, view, &s->blame_complete,
4594 &s->first_displayed_line, &s->last_displayed_line,
4595 &s->selected_line, &s->done, &s->eof,
4596 s->path, s->blamed_commit->id, s->repo);
4602 struct got_object_qid *first;
4603 first = SIMPLEQ_FIRST(&s->blamed_commits);
4604 if (!got_object_id_cmp(first->id, s->commit_id))
4607 thread_err = stop_blame(&s->blame);
4611 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4612 got_object_qid_free(s->blamed_commit);
4614 SIMPLEQ_FIRST(&s->blamed_commits);
4615 err = run_blame(&s->blame, view, &s->blame_complete,
4616 &s->first_displayed_line, &s->last_displayed_line,
4617 &s->selected_line, &s->done, &s->eof, s->path,
4618 s->blamed_commit->id, s->repo);
4625 struct got_object_id *id = NULL;
4626 struct got_object_qid *pid;
4627 struct got_commit_object *commit = NULL;
4628 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4629 s->first_displayed_line, s->selected_line);
4632 err = got_object_open_as_commit(&commit, s->repo, id);
4635 pid = SIMPLEQ_FIRST(
4636 got_object_commit_get_parent_ids(commit));
4637 if (view_is_parent_view(view))
4638 begin_x = view_split_begin_x(view->begin_x);
4639 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4640 if (diff_view == NULL) {
4641 got_object_commit_close(commit);
4642 err = got_error_from_errno("view_open");
4645 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4646 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4647 got_object_commit_close(commit);
4649 view_close(diff_view);
4652 if (view_is_parent_view(view)) {
4653 err = view_close_child(view);
4656 err = view_set_child(view, diff_view);
4658 view_close(diff_view);
4661 *focus_view = diff_view;
4662 view->child_focussed = 1;
4664 *new_view = diff_view;
4672 if (s->last_displayed_line >= s->blame.nlines &&
4673 s->selected_line >= MIN(s->blame.nlines,
4674 view->nlines - 2)) {
4677 if (s->last_displayed_line >= s->blame.nlines &&
4678 s->selected_line < view->nlines - 2) {
4679 s->selected_line = MIN(s->blame.nlines,
4683 if (s->last_displayed_line + view->nlines - 2
4685 s->first_displayed_line +=
4688 s->first_displayed_line =
4693 if (s->selected_line > view->nlines - 2) {
4694 s->selected_line = MIN(s->blame.nlines,
4701 return thread_err ? thread_err : err;
4704 static const struct got_error *
4705 cmd_blame(int argc, char *argv[])
4707 const struct got_error *error;
4708 struct got_repository *repo = NULL;
4709 struct got_worktree *worktree = NULL;
4710 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4711 char *link_target = NULL;
4712 struct got_object_id *commit_id = NULL;
4713 char *commit_id_str = NULL;
4715 struct tog_view *view;
4718 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4723 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4726 commit_id_str = optarg;
4729 repo_path = realpath(optarg, NULL);
4730 if (repo_path == NULL)
4731 return got_error_from_errno2("realpath",
4746 cwd = getcwd(NULL, 0);
4748 return got_error_from_errno("getcwd");
4750 error = got_worktree_open(&worktree, cwd);
4751 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4754 if (repo_path == NULL) {
4757 strdup(got_worktree_get_repo_path(worktree));
4759 repo_path = strdup(cwd);
4761 if (repo_path == NULL) {
4762 error = got_error_from_errno("strdup");
4766 error = got_repo_open(&repo, repo_path, NULL);
4770 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4777 error = apply_unveil(got_repo_get_path(repo), NULL);
4781 if (commit_id_str == NULL) {
4782 struct got_reference *head_ref;
4783 error = got_ref_open(&head_ref, repo, worktree ?
4784 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4787 error = got_ref_resolve(&commit_id, repo, head_ref);
4788 got_ref_close(head_ref);
4790 error = got_repo_match_object_id(&commit_id, NULL,
4791 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4796 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4798 error = got_error_from_errno("view_open");
4802 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4807 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4812 /* Release work tree lock. */
4813 got_worktree_close(worktree);
4816 error = view_loop(view);
4824 got_worktree_close(worktree);
4826 got_repo_close(repo);
4830 static const struct got_error *
4831 draw_tree_entries(struct tog_view *view,
4832 struct got_tree_entry **first_displayed_entry,
4833 struct got_tree_entry **last_displayed_entry,
4834 struct got_tree_entry **selected_entry, int *ndisplayed,
4835 const char *label, int show_ids, const char *parent_path,
4836 struct got_tree_object *tree, int selected, int limit,
4837 int isroot, struct tog_colors *colors, struct got_repository *repo)
4839 const struct got_error *err = NULL;
4840 struct got_tree_entry *te;
4842 struct tog_color *tc;
4843 int width, n, i, nentries;
4847 werase(view->window);
4852 err = format_line(&wline, &width, label, view->ncols, 0);
4855 if (view_needs_focus_indication(view))
4856 wstandout(view->window);
4857 tc = get_color(colors, TOG_COLOR_COMMIT);
4859 wattr_on(view->window,
4860 COLOR_PAIR(tc->colorpair), NULL);
4861 waddwstr(view->window, wline);
4863 wattr_off(view->window,
4864 COLOR_PAIR(tc->colorpair), NULL);
4865 if (view_needs_focus_indication(view))
4866 wstandend(view->window);
4869 if (width < view->ncols - 1)
4870 waddch(view->window, '\n');
4873 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4876 waddwstr(view->window, wline);
4879 if (width < view->ncols - 1)
4880 waddch(view->window, '\n');
4883 waddch(view->window, '\n');
4887 if (*first_displayed_entry == NULL) {
4888 te = got_object_tree_get_first_entry(tree);
4889 if (selected == 0) {
4891 wstandout(view->window);
4892 *selected_entry = NULL;
4894 waddstr(view->window, " ..\n"); /* parent directory */
4895 if (selected == 0 && view->focussed)
4896 wstandend(view->window);
4903 te = *first_displayed_entry;
4906 nentries = got_object_tree_get_nentries(tree);
4907 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4908 char *line = NULL, *id_str = NULL, *link_target = NULL;
4909 const char *modestr = "";
4912 te = got_object_tree_get_entry(tree, i);
4913 mode = got_tree_entry_get_mode(te);
4916 err = got_object_id_str(&id_str,
4917 got_tree_entry_get_id(te));
4919 return got_error_from_errno(
4920 "got_object_id_str");
4922 if (got_object_tree_entry_is_submodule(te))
4924 else if (S_ISLNK(mode)) {
4927 err = got_tree_entry_get_symlink_target(&link_target,
4933 for (i = 0; i < strlen(link_target); i++) {
4934 if (!isprint((unsigned char)link_target[i]))
4935 link_target[i] = '?';
4939 else if (S_ISDIR(mode))
4941 else if (mode & S_IXUSR)
4943 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4944 got_tree_entry_get_name(te), modestr,
4945 link_target ? " -> ": "",
4946 link_target ? link_target : "") == -1) {
4949 return got_error_from_errno("asprintf");
4953 err = format_line(&wline, &width, line, view->ncols, 0);
4958 if (n == selected) {
4960 wstandout(view->window);
4961 *selected_entry = te;
4963 tc = match_color(colors, line);
4965 wattr_on(view->window,
4966 COLOR_PAIR(tc->colorpair), NULL);
4967 waddwstr(view->window, wline);
4969 wattr_off(view->window,
4970 COLOR_PAIR(tc->colorpair), NULL);
4971 if (width < view->ncols - 1)
4972 waddch(view->window, '\n');
4973 if (n == selected && view->focussed)
4974 wstandend(view->window);
4980 *last_displayed_entry = te;
4989 tree_scroll_up(struct tog_view *view, int maxscroll)
4991 struct tog_tree_view_state *s = &view->state.tree;
4992 struct got_tree_entry *te;
4993 int isroot = s->tree == s->root;
4996 if (s->first_displayed_entry == NULL)
4999 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5000 while (i++ < maxscroll) {
5003 s->first_displayed_entry = NULL;
5006 s->first_displayed_entry = te;
5007 te = got_tree_entry_get_prev(s->tree, te);
5012 tree_scroll_down(struct tog_view *view, int maxscroll)
5014 struct tog_tree_view_state *s = &view->state.tree;
5015 struct got_tree_entry *next, *last;
5018 if (s->first_displayed_entry)
5019 next = got_tree_entry_get_next(s->tree,
5020 s->first_displayed_entry);
5022 next = got_object_tree_get_first_entry(s->tree);
5024 last = s->last_displayed_entry;
5025 while (next && last && n++ < maxscroll) {
5026 last = got_tree_entry_get_next(s->tree, last);
5028 s->first_displayed_entry = next;
5029 next = got_tree_entry_get_next(s->tree, next);
5034 static const struct got_error *
5035 tree_entry_path(char **path, struct tog_parent_trees *parents,
5036 struct got_tree_entry *te)
5038 const struct got_error *err = NULL;
5039 struct tog_parent_tree *pt;
5040 size_t len = 2; /* for leading slash and NUL */
5042 TAILQ_FOREACH(pt, parents, entry)
5043 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5046 len += strlen(got_tree_entry_get_name(te));
5048 *path = calloc(1, len);
5050 return got_error_from_errno("calloc");
5053 pt = TAILQ_LAST(parents, tog_parent_trees);
5055 const char *name = got_tree_entry_get_name(pt->selected_entry);
5056 if (strlcat(*path, name, len) >= len) {
5057 err = got_error(GOT_ERR_NO_SPACE);
5060 if (strlcat(*path, "/", len) >= len) {
5061 err = got_error(GOT_ERR_NO_SPACE);
5064 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5067 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5068 err = got_error(GOT_ERR_NO_SPACE);
5080 static const struct got_error *
5081 blame_tree_entry(struct tog_view **new_view, int begin_x,
5082 struct got_tree_entry *te, struct tog_parent_trees *parents,
5083 struct got_object_id *commit_id, struct got_repository *repo)
5085 const struct got_error *err = NULL;
5087 struct tog_view *blame_view;
5091 err = tree_entry_path(&path, parents, te);
5095 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5096 if (blame_view == NULL) {
5097 err = got_error_from_errno("view_open");
5101 err = open_blame_view(blame_view, path, commit_id, repo);
5103 if (err->code == GOT_ERR_CANCELLED)
5105 view_close(blame_view);
5107 *new_view = blame_view;
5113 static const struct got_error *
5114 log_tree_entry(struct tog_view **new_view, int begin_x,
5115 struct got_tree_entry *te, struct tog_parent_trees *parents,
5116 struct got_object_id *commit_id, struct got_repository *repo)
5118 struct tog_view *log_view;
5119 const struct got_error *err = NULL;
5124 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5125 if (log_view == NULL)
5126 return got_error_from_errno("view_open");
5128 err = tree_entry_path(&path, parents, te);
5132 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5134 view_close(log_view);
5136 *new_view = log_view;
5141 static const struct got_error *
5142 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5143 struct got_object_id *commit_id, struct got_repository *repo)
5145 const struct got_error *err = NULL;
5146 char *commit_id_str = NULL;
5147 struct tog_tree_view_state *s = &view->state.tree;
5149 TAILQ_INIT(&s->parents);
5151 err = got_object_id_str(&commit_id_str, commit_id);
5155 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5156 err = got_error_from_errno("asprintf");
5160 s->root = s->tree = root;
5161 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5162 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5163 s->commit_id = got_object_id_dup(commit_id);
5164 if (s->commit_id == NULL) {
5165 err = got_error_from_errno("got_object_id_dup");
5170 SIMPLEQ_INIT(&s->colors);
5172 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5173 err = add_color(&s->colors, "\\$$",
5174 TOG_COLOR_TREE_SUBMODULE,
5175 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5178 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5179 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5181 free_colors(&s->colors);
5184 err = add_color(&s->colors, "/$",
5185 TOG_COLOR_TREE_DIRECTORY,
5186 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5188 free_colors(&s->colors);
5192 err = add_color(&s->colors, "\\*$",
5193 TOG_COLOR_TREE_EXECUTABLE,
5194 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5196 free_colors(&s->colors);
5200 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5201 get_color_value("TOG_COLOR_COMMIT"));
5203 free_colors(&s->colors);
5208 view->show = show_tree_view;
5209 view->input = input_tree_view;
5210 view->close = close_tree_view;
5211 view->search_start = search_start_tree_view;
5212 view->search_next = search_next_tree_view;
5214 free(commit_id_str);
5216 free(s->tree_label);
5217 s->tree_label = NULL;
5222 static const struct got_error *
5223 close_tree_view(struct tog_view *view)
5225 struct tog_tree_view_state *s = &view->state.tree;
5227 free_colors(&s->colors);
5228 free(s->tree_label);
5229 s->tree_label = NULL;
5231 s->commit_id = NULL;
5232 while (!TAILQ_EMPTY(&s->parents)) {
5233 struct tog_parent_tree *parent;
5234 parent = TAILQ_FIRST(&s->parents);
5235 TAILQ_REMOVE(&s->parents, parent, entry);
5239 if (s->tree != s->root)
5240 got_object_tree_close(s->tree);
5241 got_object_tree_close(s->root);
5245 static const struct got_error *
5246 search_start_tree_view(struct tog_view *view)
5248 struct tog_tree_view_state *s = &view->state.tree;
5250 s->matched_entry = NULL;
5255 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5257 regmatch_t regmatch;
5259 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5263 static const struct got_error *
5264 search_next_tree_view(struct tog_view *view)
5266 struct tog_tree_view_state *s = &view->state.tree;
5267 struct got_tree_entry *te = NULL;
5269 if (!view->searching) {
5270 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5274 if (s->matched_entry) {
5275 if (view->searching == TOG_SEARCH_FORWARD) {
5276 if (s->selected_entry)
5277 te = got_tree_entry_get_next(s->tree,
5280 te = got_object_tree_get_first_entry(s->tree);
5282 if (s->selected_entry == NULL)
5283 te = got_object_tree_get_last_entry(s->tree);
5285 te = got_tree_entry_get_prev(s->tree,
5289 if (view->searching == TOG_SEARCH_FORWARD)
5290 te = got_object_tree_get_first_entry(s->tree);
5292 te = got_object_tree_get_last_entry(s->tree);
5297 if (s->matched_entry == NULL) {
5298 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5301 if (view->searching == TOG_SEARCH_FORWARD)
5302 te = got_object_tree_get_first_entry(s->tree);
5304 te = got_object_tree_get_last_entry(s->tree);
5307 if (match_tree_entry(te, &view->regex)) {
5308 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5309 s->matched_entry = te;
5313 if (view->searching == TOG_SEARCH_FORWARD)
5314 te = got_tree_entry_get_next(s->tree, te);
5316 te = got_tree_entry_get_prev(s->tree, te);
5319 if (s->matched_entry) {
5320 s->first_displayed_entry = s->matched_entry;
5327 static const struct got_error *
5328 show_tree_view(struct tog_view *view)
5330 const struct got_error *err = NULL;
5331 struct tog_tree_view_state *s = &view->state.tree;
5334 err = tree_entry_path(&parent_path, &s->parents, NULL);
5338 err = draw_tree_entries(view, &s->first_displayed_entry,
5339 &s->last_displayed_entry, &s->selected_entry,
5340 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5341 s->tree, s->selected, view->nlines, s->tree == s->root,
5342 &s->colors, s->repo);
5349 static const struct got_error *
5350 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5351 struct tog_view **focus_view, struct tog_view *view, int ch)
5353 const struct got_error *err = NULL;
5354 struct tog_tree_view_state *s = &view->state.tree;
5355 struct tog_view *log_view, *ref_view;
5360 s->show_ids = !s->show_ids;
5363 if (!s->selected_entry)
5365 if (view_is_parent_view(view))
5366 begin_x = view_split_begin_x(view->begin_x);
5367 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5368 &s->parents, s->commit_id, s->repo);
5369 if (view_is_parent_view(view)) {
5370 err = view_close_child(view);
5373 err = view_set_child(view, log_view);
5375 view_close(log_view);
5378 *focus_view = log_view;
5379 view->child_focussed = 1;
5381 *new_view = log_view;
5384 if (view_is_parent_view(view))
5385 begin_x = view_split_begin_x(view->begin_x);
5386 ref_view = view_open(view->nlines, view->ncols,
5387 view->begin_y, begin_x, TOG_VIEW_REF);
5388 if (ref_view == NULL)
5389 return got_error_from_errno("view_open");
5390 err = open_ref_view(ref_view, s->repo);
5392 view_close(ref_view);
5395 if (view_is_parent_view(view)) {
5396 err = view_close_child(view);
5399 err = view_set_child(view, ref_view);
5401 view_close(ref_view);
5404 *focus_view = ref_view;
5405 view->child_focussed = 1;
5407 *new_view = ref_view;
5411 if (s->selected > 0) {
5415 tree_scroll_up(view, 1);
5419 if (s->tree == s->root) {
5420 if (got_object_tree_get_first_entry(s->tree) ==
5421 s->first_displayed_entry)
5424 if (s->first_displayed_entry == NULL)
5427 tree_scroll_up(view, MAX(0, view->nlines - 3));
5431 if (s->selected < s->ndisplayed - 1) {
5435 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5437 /* can't scroll any further */
5439 tree_scroll_down(view, 1);
5443 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5445 /* can't scroll any further; move cursor down */
5446 if (s->selected < s->ndisplayed - 1)
5447 s->selected = s->ndisplayed - 1;
5450 tree_scroll_down(view, view->nlines - 3);
5455 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5456 struct tog_parent_tree *parent;
5457 /* user selected '..' */
5458 if (s->tree == s->root)
5460 parent = TAILQ_FIRST(&s->parents);
5461 TAILQ_REMOVE(&s->parents, parent,
5463 got_object_tree_close(s->tree);
5464 s->tree = parent->tree;
5465 s->first_displayed_entry =
5466 parent->first_displayed_entry;
5468 parent->selected_entry;
5469 s->selected = parent->selected;
5471 } else if (S_ISDIR(got_tree_entry_get_mode(
5472 s->selected_entry))) {
5473 struct got_tree_object *subtree;
5474 err = got_object_open_as_tree(&subtree, s->repo,
5475 got_tree_entry_get_id(s->selected_entry));
5478 err = tree_view_visit_subtree(subtree, s);
5480 got_object_tree_close(subtree);
5483 } else if (S_ISREG(got_tree_entry_get_mode(
5484 s->selected_entry))) {
5485 struct tog_view *blame_view;
5486 int begin_x = view_is_parent_view(view) ?
5487 view_split_begin_x(view->begin_x) : 0;
5489 err = blame_tree_entry(&blame_view, begin_x,
5490 s->selected_entry, &s->parents,
5491 s->commit_id, s->repo);
5494 if (view_is_parent_view(view)) {
5495 err = view_close_child(view);
5498 err = view_set_child(view, blame_view);
5500 view_close(blame_view);
5503 *focus_view = blame_view;
5504 view->child_focussed = 1;
5506 *new_view = blame_view;
5510 if (s->selected > view->nlines)
5511 s->selected = s->ndisplayed - 1;
5524 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5529 static const struct got_error *
5530 cmd_tree(int argc, char *argv[])
5532 const struct got_error *error;
5533 struct got_repository *repo = NULL;
5534 struct got_worktree *worktree = NULL;
5535 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5536 struct got_object_id *commit_id = NULL;
5537 char *commit_id_arg = NULL;
5538 struct got_commit_object *commit = NULL;
5539 struct got_tree_object *tree = NULL;
5541 struct tog_view *view;
5544 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5549 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5552 commit_id_arg = optarg;
5555 repo_path = realpath(optarg, NULL);
5556 if (repo_path == NULL)
5557 return got_error_from_errno2("realpath",
5572 cwd = getcwd(NULL, 0);
5574 return got_error_from_errno("getcwd");
5576 error = got_worktree_open(&worktree, cwd);
5577 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5580 if (repo_path == NULL) {
5583 strdup(got_worktree_get_repo_path(worktree));
5585 repo_path = strdup(cwd);
5587 if (repo_path == NULL) {
5588 error = got_error_from_errno("strdup");
5592 error = got_repo_open(&repo, repo_path, NULL);
5596 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5603 error = apply_unveil(got_repo_get_path(repo), NULL);
5607 error = got_repo_match_object_id(&commit_id, NULL,
5608 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5609 GOT_OBJ_TYPE_COMMIT, 1, repo);
5613 error = got_object_open_as_commit(&commit, repo, commit_id);
5617 error = got_object_open_as_tree(&tree, repo,
5618 got_object_commit_get_tree_id(commit));
5622 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5624 error = got_error_from_errno("view_open");
5627 error = open_tree_view(view, tree, commit_id, repo);
5630 if (!got_path_is_root_dir(in_repo_path)) {
5631 error = tree_view_walk_path(&view->state.tree, commit_id,
5632 in_repo_path, repo);
5638 /* Release work tree lock. */
5639 got_worktree_close(worktree);
5642 error = view_loop(view);
5648 got_object_commit_close(commit);
5650 got_object_tree_close(tree);
5652 got_repo_close(repo);
5656 static const struct got_error *
5657 ref_view_load_refs(struct tog_ref_view_state *s)
5659 const struct got_error *err;
5660 struct got_reflist_entry *sre;
5661 struct tog_reflist_entry *re;
5663 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5664 got_ref_cmp_by_name, NULL);
5669 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5670 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5673 re = malloc(sizeof(*re));
5675 return got_error_from_errno("malloc");
5678 re->idx = s->nrefs++;
5679 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5686 ref_view_free_refs(struct tog_ref_view_state *s)
5688 struct tog_reflist_entry *re;
5690 while (!TAILQ_EMPTY(&s->refs)) {
5691 re = TAILQ_FIRST(&s->refs);
5692 TAILQ_REMOVE(&s->refs, re, entry);
5695 got_ref_list_free(&s->simplerefs);
5698 static const struct got_error *
5699 open_ref_view(struct tog_view *view, struct got_repository *repo)
5701 const struct got_error *err = NULL;
5702 struct tog_ref_view_state *s = &view->state.ref;
5704 s->selected_entry = 0;
5707 SIMPLEQ_INIT(&s->simplerefs);
5708 TAILQ_INIT(&s->refs);
5709 SIMPLEQ_INIT(&s->colors);
5711 err = ref_view_load_refs(s);
5715 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
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, int maxscroll)
5854 struct tog_ref_view_state *s = &view->state.ref;
5855 struct tog_reflist_entry *re;
5858 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5861 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5862 while (i++ < maxscroll) {
5865 s->first_displayed_entry = re;
5866 re = TAILQ_PREV(re, tog_reflist_head, entry);
5871 ref_scroll_down(struct tog_view *view, int maxscroll)
5873 struct tog_ref_view_state *s = &view->state.ref;
5874 struct tog_reflist_entry *next, *last;
5877 if (s->first_displayed_entry)
5878 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5880 next = TAILQ_FIRST(&s->refs);
5882 last = s->last_displayed_entry;
5883 while (next && last && n++ < maxscroll) {
5884 last = TAILQ_NEXT(last, entry);
5886 s->first_displayed_entry = next;
5887 next = TAILQ_NEXT(next, entry);
5892 static const struct got_error *
5893 search_start_ref_view(struct tog_view *view)
5895 struct tog_ref_view_state *s = &view->state.ref;
5897 s->matched_entry = NULL;
5902 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5904 regmatch_t regmatch;
5906 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5910 static const struct got_error *
5911 search_next_ref_view(struct tog_view *view)
5913 struct tog_ref_view_state *s = &view->state.ref;
5914 struct tog_reflist_entry *re = NULL;
5916 if (!view->searching) {
5917 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5921 if (s->matched_entry) {
5922 if (view->searching == TOG_SEARCH_FORWARD) {
5923 if (s->selected_entry)
5924 re = TAILQ_NEXT(s->selected_entry, entry);
5926 re = TAILQ_PREV(s->selected_entry,
5927 tog_reflist_head, entry);
5929 if (s->selected_entry == NULL)
5930 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5932 re = TAILQ_PREV(s->selected_entry,
5933 tog_reflist_head, entry);
5936 if (view->searching == TOG_SEARCH_FORWARD)
5937 re = TAILQ_FIRST(&s->refs);
5939 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5944 if (s->matched_entry == NULL) {
5945 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5948 if (view->searching == TOG_SEARCH_FORWARD)
5949 re = TAILQ_FIRST(&s->refs);
5951 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5954 if (match_reflist_entry(re, &view->regex)) {
5955 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5956 s->matched_entry = re;
5960 if (view->searching == TOG_SEARCH_FORWARD)
5961 re = TAILQ_NEXT(re, entry);
5963 re = TAILQ_PREV(re, tog_reflist_head, entry);
5966 if (s->matched_entry) {
5967 s->first_displayed_entry = s->matched_entry;
5974 static const struct got_error *
5975 show_ref_view(struct tog_view *view)
5977 const struct got_error *err = NULL;
5978 struct tog_ref_view_state *s = &view->state.ref;
5979 struct tog_reflist_entry *re;
5982 struct tog_color *tc;
5984 int limit = view->nlines;
5986 werase(view->window);
5993 re = s->first_displayed_entry;
5995 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5997 return got_error_from_errno("asprintf");
5999 err = format_line(&wline, &width, line, view->ncols, 0);
6004 if (view_needs_focus_indication(view))
6005 wstandout(view->window);
6006 waddwstr(view->window, wline);
6007 if (view_needs_focus_indication(view))
6008 wstandend(view->window);
6013 if (width < view->ncols - 1)
6014 waddch(view->window, '\n');
6019 while (re && limit > 0) {
6022 if (got_ref_is_symbolic(re->ref)) {
6023 if (asprintf(&line, "%s -> %s",
6024 got_ref_get_name(re->ref),
6025 got_ref_get_symref_target(re->ref)) == -1)
6026 return got_error_from_errno("asprintf");
6027 } else if (s->show_ids) {
6028 struct got_object_id *id;
6030 err = got_ref_resolve(&id, s->repo, re->ref);
6033 err = got_object_id_str(&id_str, id);
6038 if (asprintf(&line, "%s: %s",
6039 got_ref_get_name(re->ref), id_str) == -1) {
6040 err = got_error_from_errno("asprintf");
6048 line = strdup(got_ref_get_name(re->ref));
6050 return got_error_from_errno("strdup");
6053 err = format_line(&wline, &width, line, view->ncols, 0);
6058 if (n == s->selected) {
6060 wstandout(view->window);
6061 s->selected_entry = re;
6063 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6065 wattr_on(view->window,
6066 COLOR_PAIR(tc->colorpair), NULL);
6067 waddwstr(view->window, wline);
6069 wattr_off(view->window,
6070 COLOR_PAIR(tc->colorpair), NULL);
6071 if (width < view->ncols - 1)
6072 waddch(view->window, '\n');
6073 if (n == s->selected && view->focussed)
6074 wstandend(view->window);
6080 s->last_displayed_entry = re;
6083 re = TAILQ_NEXT(re, entry);
6090 static const struct got_error *
6091 browse_ref_tree(struct tog_view **new_view, int begin_x,
6092 struct tog_reflist_entry *re, struct got_repository *repo)
6094 const struct got_error *err = NULL;
6095 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6096 struct got_tree_object *tree = NULL;
6097 struct tog_view *tree_view;
6101 err = resolve_reflist_entry(&commit_id, re, repo);
6103 if (err->code != GOT_ERR_OBJ_TYPE)
6109 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6113 err = got_object_open_as_tree(&tree, repo, tree_id);
6117 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6118 if (tree_view == NULL) {
6119 err = got_error_from_errno("view_open");
6123 err = open_tree_view(tree_view, tree, commit_id, repo);
6127 *new_view = tree_view;
6133 got_object_tree_close(tree);
6137 static const struct got_error *
6138 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6139 struct tog_view **focus_view, struct tog_view *view, int ch)
6141 const struct got_error *err = NULL;
6142 struct tog_ref_view_state *s = &view->state.ref;
6143 struct tog_view *log_view, *tree_view;
6148 s->show_ids = !s->show_ids;
6152 if (!s->selected_entry)
6154 if (view_is_parent_view(view))
6155 begin_x = view_split_begin_x(view->begin_x);
6156 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6158 if (view_is_parent_view(view)) {
6159 err = view_close_child(view);
6162 err = view_set_child(view, log_view);
6164 view_close(log_view);
6167 *focus_view = log_view;
6168 view->child_focussed = 1;
6170 *new_view = log_view;
6173 if (!s->selected_entry)
6175 if (view_is_parent_view(view))
6176 begin_x = view_split_begin_x(view->begin_x);
6177 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6179 if (err || tree_view == NULL)
6181 if (view_is_parent_view(view)) {
6182 err = view_close_child(view);
6185 err = view_set_child(view, tree_view);
6187 view_close(tree_view);
6190 *focus_view = tree_view;
6191 view->child_focussed = 1;
6193 *new_view = tree_view;
6197 if (s->selected > 0) {
6201 ref_scroll_up(view, 1);
6205 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6207 ref_scroll_up(view, MAX(0, view->nlines - 1));
6211 if (s->selected < s->ndisplayed - 1) {
6215 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6216 /* can't scroll any further */
6218 ref_scroll_down(view, 1);
6222 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6223 /* can't scroll any further; move cursor down */
6224 if (s->selected < s->ndisplayed - 1)
6225 s->selected = s->ndisplayed - 1;
6228 ref_scroll_down(view, view->nlines - 1);
6231 ref_view_free_refs(s);
6232 err = ref_view_load_refs(s);
6235 if (s->selected > view->nlines)
6236 s->selected = s->ndisplayed - 1;
6249 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6254 static const struct got_error *
6255 cmd_ref(int argc, char *argv[])
6257 const struct got_error *error;
6258 struct got_repository *repo = NULL;
6259 struct got_worktree *worktree = NULL;
6260 char *cwd = NULL, *repo_path = NULL;
6262 struct tog_view *view;
6265 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6270 while ((ch = getopt(argc, argv, "r:")) != -1) {
6273 repo_path = realpath(optarg, NULL);
6274 if (repo_path == NULL)
6275 return got_error_from_errno2("realpath",
6290 cwd = getcwd(NULL, 0);
6292 return got_error_from_errno("getcwd");
6294 error = got_worktree_open(&worktree, cwd);
6295 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6298 if (repo_path == NULL) {
6301 strdup(got_worktree_get_repo_path(worktree));
6303 repo_path = strdup(cwd);
6305 if (repo_path == NULL) {
6306 error = got_error_from_errno("strdup");
6310 error = got_repo_open(&repo, repo_path, NULL);
6316 error = apply_unveil(got_repo_get_path(repo), NULL);
6320 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6322 error = got_error_from_errno("view_open");
6326 error = open_ref_view(view, repo);
6331 /* Release work tree lock. */
6332 got_worktree_close(worktree);
6335 error = view_loop(view);
6340 got_repo_close(repo);
6345 list_commands(FILE *fp)
6349 fprintf(fp, "commands:");
6350 for (i = 0; i < nitems(tog_commands); i++) {
6351 struct tog_cmd *cmd = &tog_commands[i];
6352 fprintf(fp, " %s", cmd->name);
6358 usage(int hflag, int status)
6360 FILE *fp = (status == 0) ? stdout : stderr;
6362 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6365 fprintf(fp, "lazy usage: %s path\n", getprogname());
6372 make_argv(int argc, ...)
6380 argv = calloc(argc, sizeof(char *));
6383 for (i = 0; i < argc; i++) {
6384 argv[i] = strdup(va_arg(ap, char *));
6385 if (argv[i] == NULL)
6394 * Try to convert 'tog path' into a 'tog log path' command.
6395 * The user could simply have mistyped the command rather than knowingly
6396 * provided a path. So check whether argv[0] can in fact be resolved
6397 * to a path in the HEAD commit and print a special error if not.
6398 * This hack is for mpi@ <3
6400 static const struct got_error *
6401 tog_log_with_path(int argc, char *argv[])
6403 const struct got_error *error = NULL;
6404 struct tog_cmd *cmd = NULL;
6405 struct got_repository *repo = NULL;
6406 struct got_worktree *worktree = NULL;
6407 struct got_object_id *commit_id = NULL, *id = NULL;
6408 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6409 char *commit_id_str = NULL, **cmd_argv = NULL;
6411 cwd = getcwd(NULL, 0);
6413 return got_error_from_errno("getcwd");
6415 error = got_worktree_open(&worktree, cwd);
6416 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6420 repo_path = strdup(got_worktree_get_repo_path(worktree));
6422 repo_path = strdup(cwd);
6423 if (repo_path == NULL) {
6424 error = got_error_from_errno("strdup");
6428 error = got_repo_open(&repo, repo_path, NULL);
6432 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6437 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6438 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6439 GOT_OBJ_TYPE_COMMIT, 1, repo);
6444 got_worktree_close(worktree);
6448 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6450 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6452 fprintf(stderr, "%s: '%s' is no known command or path\n",
6453 getprogname(), argv[0]);
6458 got_repo_close(repo);
6461 error = got_object_id_str(&commit_id_str, commit_id);
6465 cmd = &tog_commands[0]; /* log */
6467 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6468 error = cmd->cmd_main(argc, cmd_argv);
6471 got_repo_close(repo);
6473 got_worktree_close(worktree);
6475 free(commit_id_str);
6482 for (i = 0; i < argc; i++)
6490 main(int argc, char *argv[])
6492 const struct got_error *error = NULL;
6493 struct tog_cmd *cmd = NULL;
6494 int ch, hflag = 0, Vflag = 0;
6495 char **cmd_argv = NULL;
6496 static struct option longopts[] = {
6497 { "version", no_argument, NULL, 'V' },
6501 setlocale(LC_CTYPE, "");
6503 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6523 got_version_print_str();
6530 /* Build an argument vector which runs a default command. */
6531 cmd = &tog_commands[0];
6533 cmd_argv = make_argv(argc, cmd->name);
6537 /* Did the user specify a command? */
6538 for (i = 0; i < nitems(tog_commands); i++) {
6539 if (strncmp(tog_commands[i].name, argv[0],
6540 strlen(argv[0])) == 0) {
6541 cmd = &tog_commands[i];
6550 /* No command specified; try log with a path */
6551 error = tog_log_with_path(argc, argv);
6556 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6563 for (i = 0; i < argc; i++)
6568 if (error && error->code != GOT_ERR_CANCELLED)
6569 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);