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;
453 /* type-specific state */
454 enum tog_view_type type;
456 struct tog_diff_view_state diff;
457 struct tog_log_view_state log;
458 struct tog_blame_view_state blame;
459 struct tog_tree_view_state tree;
460 struct tog_ref_view_state ref;
463 const struct got_error *(*show)(struct tog_view *);
464 const struct got_error *(*input)(struct tog_view **,
465 struct tog_view **, struct tog_view**, struct tog_view *, int);
466 const struct got_error *(*close)(struct tog_view *);
468 const struct got_error *(*search_start)(struct tog_view *);
469 const struct got_error *(*search_next)(struct tog_view *);
471 #define TOG_SEARCH_FORWARD 1
472 #define TOG_SEARCH_BACKWARD 2
473 int search_next_done;
474 #define TOG_SEARCH_HAVE_MORE 1
475 #define TOG_SEARCH_NO_MORE 2
476 #define TOG_SEARCH_HAVE_NONE 3
481 static const struct got_error *open_diff_view(struct tog_view *,
482 struct got_object_id *, struct got_object_id *,
483 const char *, const char *, int, int, int, struct tog_view *,
484 struct got_repository *);
485 static const struct got_error *show_diff_view(struct tog_view *);
486 static const struct got_error *input_diff_view(struct tog_view **,
487 struct tog_view **, struct tog_view **, struct tog_view *, int);
488 static const struct got_error* close_diff_view(struct tog_view *);
489 static const struct got_error *search_start_diff_view(struct tog_view *);
490 static const struct got_error *search_next_diff_view(struct tog_view *);
492 static const struct got_error *open_log_view(struct tog_view *,
493 struct got_object_id *, struct got_repository *,
494 const char *, const char *, int);
495 static const struct got_error * show_log_view(struct tog_view *);
496 static const struct got_error *input_log_view(struct tog_view **,
497 struct tog_view **, struct tog_view **, struct tog_view *, int);
498 static const struct got_error *close_log_view(struct tog_view *);
499 static const struct got_error *search_start_log_view(struct tog_view *);
500 static const struct got_error *search_next_log_view(struct tog_view *);
502 static const struct got_error *open_blame_view(struct tog_view *, char *,
503 struct got_object_id *, struct got_repository *);
504 static const struct got_error *show_blame_view(struct tog_view *);
505 static const struct got_error *input_blame_view(struct tog_view **,
506 struct tog_view **, struct tog_view **, struct tog_view *, int);
507 static const struct got_error *close_blame_view(struct tog_view *);
508 static const struct got_error *search_start_blame_view(struct tog_view *);
509 static const struct got_error *search_next_blame_view(struct tog_view *);
511 static const struct got_error *open_tree_view(struct tog_view *,
512 struct got_tree_object *, struct got_object_id *, struct got_repository *);
513 static const struct got_error *show_tree_view(struct tog_view *);
514 static const struct got_error *input_tree_view(struct tog_view **,
515 struct tog_view **, struct tog_view **, struct tog_view *, int);
516 static const struct got_error *close_tree_view(struct tog_view *);
517 static const struct got_error *search_start_tree_view(struct tog_view *);
518 static const struct got_error *search_next_tree_view(struct tog_view *);
520 static const struct got_error *open_ref_view(struct tog_view *,
521 struct got_repository *);
522 static const struct got_error *show_ref_view(struct tog_view *);
523 static const struct got_error *input_ref_view(struct tog_view **,
524 struct tog_view **, struct tog_view **, struct tog_view *, int);
525 static const struct got_error *close_ref_view(struct tog_view *);
526 static const struct got_error *search_start_ref_view(struct tog_view *);
527 static const struct got_error *search_next_ref_view(struct tog_view *);
529 static volatile sig_atomic_t tog_sigwinch_received;
530 static volatile sig_atomic_t tog_sigpipe_received;
531 static volatile sig_atomic_t tog_sigcont_received;
534 tog_sigwinch(int signo)
536 tog_sigwinch_received = 1;
540 tog_sigpipe(int signo)
542 tog_sigpipe_received = 1;
546 tog_sigcont(int signo)
548 tog_sigcont_received = 1;
551 static const struct got_error *
552 view_close(struct tog_view *view)
554 const struct got_error *err = NULL;
557 view_close(view->child);
561 err = view->close(view);
563 del_panel(view->panel);
565 delwin(view->window);
570 static struct tog_view *
571 view_open(int nlines, int ncols, int begin_y, int begin_x,
572 enum tog_view_type type)
574 struct tog_view *view = calloc(1, sizeof(*view));
582 view->nlines = nlines ? nlines : LINES - begin_y;
583 view->ncols = ncols ? ncols : COLS - begin_x;
584 view->begin_y = begin_y;
585 view->begin_x = begin_x;
586 view->window = newwin(nlines, ncols, begin_y, begin_x);
587 if (view->window == NULL) {
591 view->panel = new_panel(view->window);
592 if (view->panel == NULL ||
593 set_panel_userptr(view->panel, view) != OK) {
598 keypad(view->window, TRUE);
603 view_split_begin_x(int begin_x)
605 if (begin_x > 0 || COLS < 120)
607 return (COLS - MAX(COLS / 2, 80));
610 static const struct got_error *view_resize(struct tog_view *);
612 static const struct got_error *
613 view_splitscreen(struct tog_view *view)
615 const struct got_error *err = NULL;
618 view->begin_x = view_split_begin_x(0);
619 view->nlines = LINES;
620 view->ncols = COLS - view->begin_x;
623 err = view_resize(view);
627 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
628 return got_error_from_errno("mvwin");
633 static const struct got_error *
634 view_fullscreen(struct tog_view *view)
636 const struct got_error *err = NULL;
640 view->nlines = LINES;
644 err = view_resize(view);
648 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
649 return got_error_from_errno("mvwin");
655 view_is_parent_view(struct tog_view *view)
657 return view->parent == NULL;
660 static const struct got_error *
661 view_resize(struct tog_view *view)
665 if (view->lines > LINES)
666 nlines = view->nlines - (view->lines - LINES);
668 nlines = view->nlines + (LINES - view->lines);
670 if (view->cols > COLS)
671 ncols = view->ncols - (view->cols - COLS);
673 ncols = view->ncols + (COLS - view->cols);
675 if (wresize(view->window, nlines, ncols) == ERR)
676 return got_error_from_errno("wresize");
677 if (replace_panel(view->panel, view->window) == ERR)
678 return got_error_from_errno("replace_panel");
679 wclear(view->window);
681 view->nlines = nlines;
687 view->child->begin_x = view_split_begin_x(view->begin_x);
688 if (view->child->begin_x == 0) {
689 view_fullscreen(view->child);
690 if (view->child->focussed)
691 show_panel(view->child->panel);
693 show_panel(view->panel);
695 view_splitscreen(view->child);
696 show_panel(view->child->panel);
703 static const struct got_error *
704 view_close_child(struct tog_view *view)
706 const struct got_error *err = NULL;
708 if (view->child == NULL)
711 err = view_close(view->child);
716 static const struct got_error *
717 view_set_child(struct tog_view *view, struct tog_view *child)
719 const struct got_error *err = NULL;
722 child->parent = view;
727 view_is_splitscreen(struct tog_view *view)
729 return view->begin_x > 0;
738 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
739 cols = 80; /* Default */
745 resize_term(lines, cols);
748 static const struct got_error *
749 view_search_start(struct tog_view *view)
751 const struct got_error *err = NULL;
755 if (view->nlines < 1)
758 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
759 wclrtoeol(view->window);
763 ret = wgetnstr(view->window, pattern, sizeof(pattern));
769 if (view->searching) {
770 regfree(&view->regex);
774 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
775 err = view->search_start(view);
777 regfree(&view->regex);
780 view->searching = TOG_SEARCH_FORWARD;
781 view->search_next_done = 0;
782 view->search_next(view);
788 static const struct got_error *
789 view_input(struct tog_view **new, struct tog_view **dead,
790 struct tog_view **focus, int *done, struct tog_view *view,
791 struct tog_view_list_head *views)
793 const struct got_error *err = NULL;
801 /* Clear "no matches" indicator. */
802 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
803 view->search_next_done == TOG_SEARCH_HAVE_NONE)
804 view->search_next_done = TOG_SEARCH_HAVE_MORE;
806 if (view->searching && !view->search_next_done) {
807 errcode = pthread_mutex_unlock(&tog_mutex);
809 return got_error_set_errno(errcode,
810 "pthread_mutex_unlock");
812 errcode = pthread_mutex_lock(&tog_mutex);
814 return got_error_set_errno(errcode,
815 "pthread_mutex_lock");
816 view->search_next(view);
820 nodelay(stdscr, FALSE);
821 /* Allow threads to make progress while we are waiting for input. */
822 errcode = pthread_mutex_unlock(&tog_mutex);
824 return got_error_set_errno(errcode, "pthread_mutex_unlock");
825 ch = wgetch(view->window);
826 errcode = pthread_mutex_lock(&tog_mutex);
828 return got_error_set_errno(errcode, "pthread_mutex_lock");
829 nodelay(stdscr, TRUE);
831 if (tog_sigwinch_received || tog_sigcont_received) {
833 tog_sigwinch_received = 0;
834 tog_sigcont_received = 0;
835 TAILQ_FOREACH(v, views, entry) {
836 err = view_resize(v);
839 err = v->input(new, dead, focus, v, KEY_RESIZE);
850 *focus = view->child;
851 } else if (view->parent) {
852 *focus = view->parent;
856 err = view->input(new, dead, focus, view, ch);
863 if (view_is_parent_view(view)) {
864 if (view->child == NULL)
866 if (view_is_splitscreen(view->child)) {
867 *focus = view->child;
868 err = view_fullscreen(view->child);
870 err = view_splitscreen(view->child);
873 err = view->child->input(new, dead, focus,
874 view->child, KEY_RESIZE);
876 if (view_is_splitscreen(view)) {
878 err = view_fullscreen(view);
880 err = view_splitscreen(view);
884 err = view->input(new, dead, focus, view,
891 if (view->search_start)
892 view_search_start(view);
894 err = view->input(new, dead, focus, view, ch);
898 if (view->search_next) {
899 view->searching = (ch == 'n' ?
900 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
901 view->search_next_done = 0;
902 view->search_next(view);
904 err = view->input(new, dead, focus, view, ch);
907 err = view->input(new, dead, focus, view, ch);
915 view_vborder(struct tog_view *view)
918 struct tog_view *view_above;
921 return view_vborder(view->parent);
923 panel = panel_above(view->panel);
927 view_above = panel_userptr(panel);
928 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
929 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
933 view_needs_focus_indication(struct tog_view *view)
935 if (view_is_parent_view(view)) {
936 if (view->child == NULL || view->child->focussed)
938 if (!view_is_splitscreen(view->child))
940 } else if (!view_is_splitscreen(view))
943 return view->focussed;
946 static const struct got_error *
947 view_loop(struct tog_view *view)
949 const struct got_error *err = NULL;
950 struct tog_view_list_head views;
951 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
952 int fast_refresh = 10;
953 int done = 0, errcode;
955 errcode = pthread_mutex_lock(&tog_mutex);
957 return got_error_set_errno(errcode, "pthread_mutex_lock");
960 TAILQ_INSERT_HEAD(&views, view, entry);
964 err = view->show(view);
969 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
970 /* Refresh fast during initialization, then become slower. */
971 if (fast_refresh && fast_refresh-- == 0)
972 halfdelay(10); /* switch to once per second */
974 err = view_input(&new_view, &dead_view, &focus_view, &done,
979 struct tog_view *prev = NULL;
981 if (view_is_parent_view(dead_view))
982 prev = TAILQ_PREV(dead_view,
983 tog_view_list_head, entry);
984 else if (view->parent != dead_view)
987 if (dead_view->parent)
988 dead_view->parent->child = NULL;
990 TAILQ_REMOVE(&views, dead_view, entry);
992 err = view_close(dead_view);
993 if (err || (dead_view == main_view && new_view == NULL))
996 if (view == dead_view) {
1001 else if (!TAILQ_EMPTY(&views))
1002 view = TAILQ_LAST(&views,
1003 tog_view_list_head);
1008 view->child->focussed)
1009 focus_view = view->child;
1016 struct tog_view *v, *t;
1017 /* Only allow one parent view per type. */
1018 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1019 if (v->type != new_view->type)
1021 TAILQ_REMOVE(&views, v, entry);
1022 err = view_close(v);
1027 TAILQ_INSERT_TAIL(&views, new_view, entry);
1029 if (focus_view == NULL)
1030 focus_view = new_view;
1033 show_panel(focus_view->panel);
1036 focus_view->focussed = 1;
1039 show_panel(new_view->panel);
1040 if (view->child && view_is_splitscreen(view->child))
1041 show_panel(view->child->panel);
1044 if (focus_view == NULL) {
1046 show_panel(view->panel);
1047 if (view->child && view_is_splitscreen(view->child))
1048 show_panel(view->child->panel);
1052 err = view->parent->show(view->parent);
1056 err = view->show(view);
1060 err = view->child->show(view->child);
1069 while (!TAILQ_EMPTY(&views)) {
1070 view = TAILQ_FIRST(&views);
1071 TAILQ_REMOVE(&views, view, entry);
1075 errcode = pthread_mutex_unlock(&tog_mutex);
1076 if (errcode && err == NULL)
1077 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1087 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1092 /* Create newly allocated wide-character string equivalent to a byte string. */
1093 static const struct got_error *
1094 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1097 const struct got_error *err = NULL;
1100 *wlen = mbstowcs(NULL, s, 0);
1101 if (*wlen == (size_t)-1) {
1103 if (errno != EILSEQ)
1104 return got_error_from_errno("mbstowcs");
1106 /* byte string invalid in current encoding; try to "fix" it */
1107 err = got_mbsavis(&vis, &vislen, s);
1110 *wlen = mbstowcs(NULL, vis, 0);
1111 if (*wlen == (size_t)-1) {
1112 err = got_error_from_errno("mbstowcs"); /* give up */
1117 *ws = calloc(*wlen + 1, sizeof(**ws));
1119 err = got_error_from_errno("calloc");
1123 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1124 err = got_error_from_errno("mbstowcs");
1135 /* Format a line for display, ensuring that it won't overflow a width limit. */
1136 static const struct got_error *
1137 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1140 const struct got_error *err = NULL;
1142 wchar_t *wline = NULL;
1149 err = mbs2ws(&wline, &wlen, line);
1155 int width = wcwidth(wline[i]);
1162 if (width == 1 || width == 2) {
1163 if (cols + width > wlimit)
1167 } else if (width == -1) {
1168 if (wline[i] == L'\t') {
1170 ((cols + col_tab_align) % TABSIZE);
1171 if (cols + width > wlimit)
1177 err = got_error_from_errno("wcwidth");
1192 static const struct got_error*
1193 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1194 struct got_object_id *id, struct got_repository *repo)
1196 static const struct got_error *err = NULL;
1197 struct got_reflist_entry *re;
1203 SIMPLEQ_FOREACH(re, refs, entry) {
1204 struct got_tag_object *tag = NULL;
1205 struct got_object_id *ref_id;
1208 name = got_ref_get_name(re->ref);
1209 if (strcmp(name, GOT_REF_HEAD) == 0)
1211 if (strncmp(name, "refs/", 5) == 0)
1213 if (strncmp(name, "got/", 4) == 0)
1215 if (strncmp(name, "heads/", 6) == 0)
1217 if (strncmp(name, "remotes/", 8) == 0) {
1219 s = strstr(name, "/" GOT_REF_HEAD);
1220 if (s != NULL && s[strlen(s)] == '\0')
1223 err = got_ref_resolve(&ref_id, repo, re->ref);
1226 if (strncmp(name, "tags/", 5) == 0) {
1227 err = got_object_open_as_tag(&tag, repo, ref_id);
1229 if (err->code != GOT_ERR_OBJ_TYPE) {
1233 /* Ref points at something other than a tag. */
1238 cmp = got_object_id_cmp(tag ?
1239 got_object_tag_get_object_id(tag) : ref_id, id);
1242 got_object_tag_close(tag);
1246 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1247 s ? ", " : "", name) == -1) {
1248 err = got_error_from_errno("asprintf");
1259 static const struct got_error *
1260 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1263 char *smallerthan, *at;
1265 smallerthan = strchr(author, '<');
1266 if (smallerthan && smallerthan[1] != '\0')
1267 author = smallerthan + 1;
1268 at = strchr(author, '@');
1271 return format_line(wauthor, author_width, author, limit, col_tab_align);
1274 static const struct got_error *
1275 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1276 struct got_object_id *id, const size_t date_display_cols,
1277 int author_display_cols)
1279 struct tog_log_view_state *s = &view->state.log;
1280 const struct got_error *err = NULL;
1281 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1282 char *logmsg0 = NULL, *logmsg = NULL;
1283 char *author = NULL;
1284 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1285 int author_width, logmsg_width;
1286 char *newline, *line = NULL;
1288 const int avail = view->ncols;
1290 time_t committer_time;
1291 struct tog_color *tc;
1293 committer_time = got_object_commit_get_committer_time(commit);
1294 if (localtime_r(&committer_time, &tm) == NULL)
1295 return got_error_from_errno("localtime_r");
1296 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1298 return got_error(GOT_ERR_NO_SPACE);
1300 if (avail <= date_display_cols)
1301 limit = MIN(sizeof(datebuf) - 1, avail);
1303 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1304 tc = get_color(&s->colors, TOG_COLOR_DATE);
1306 wattr_on(view->window,
1307 COLOR_PAIR(tc->colorpair), NULL);
1308 waddnstr(view->window, datebuf, limit);
1310 wattr_off(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1318 err = got_object_id_str(&id_str, id);
1321 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1323 wattr_on(view->window,
1324 COLOR_PAIR(tc->colorpair), NULL);
1325 wprintw(view->window, "%.8s ", id_str);
1327 wattr_off(view->window,
1328 COLOR_PAIR(tc->colorpair), NULL);
1335 author = strdup(got_object_commit_get_author(commit));
1336 if (author == NULL) {
1337 err = got_error_from_errno("strdup");
1340 err = format_author(&wauthor, &author_width, author, avail - col, col);
1343 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1345 wattr_on(view->window,
1346 COLOR_PAIR(tc->colorpair), NULL);
1347 waddwstr(view->window, wauthor);
1349 wattr_off(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1351 col += author_width;
1352 while (col < avail && author_width < author_display_cols + 2) {
1353 waddch(view->window, ' ');
1360 err = got_object_commit_get_logmsg(&logmsg0, commit);
1364 while (*logmsg == '\n')
1366 newline = strchr(logmsg, '\n');
1369 limit = avail - col;
1370 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1373 waddwstr(view->window, wlogmsg);
1374 col += logmsg_width;
1375 while (col < avail) {
1376 waddch(view->window, ' ');
1388 static struct commit_queue_entry *
1389 alloc_commit_queue_entry(struct got_commit_object *commit,
1390 struct got_object_id *id)
1392 struct commit_queue_entry *entry;
1394 entry = calloc(1, sizeof(*entry));
1399 entry->commit = commit;
1404 pop_commit(struct commit_queue *commits)
1406 struct commit_queue_entry *entry;
1408 entry = TAILQ_FIRST(&commits->head);
1409 TAILQ_REMOVE(&commits->head, entry, entry);
1410 got_object_commit_close(entry->commit);
1411 commits->ncommits--;
1412 /* Don't free entry->id! It is owned by the commit graph. */
1417 free_commits(struct commit_queue *commits)
1419 while (!TAILQ_EMPTY(&commits->head))
1420 pop_commit(commits);
1423 static const struct got_error *
1424 match_commit(int *have_match, struct got_object_id *id,
1425 struct got_commit_object *commit, regex_t *regex)
1427 const struct got_error *err = NULL;
1428 regmatch_t regmatch;
1429 char *id_str = NULL, *logmsg = NULL;
1433 err = got_object_id_str(&id_str, id);
1437 err = got_object_commit_get_logmsg(&logmsg, commit);
1441 if (regexec(regex, got_object_commit_get_author(commit), 1,
1442 ®match, 0) == 0 ||
1443 regexec(regex, got_object_commit_get_committer(commit), 1,
1444 ®match, 0) == 0 ||
1445 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1446 regexec(regex, logmsg, 1, ®match, 0) == 0)
1454 static const struct got_error *
1455 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1456 int minqueue, struct got_repository *repo, const char *path,
1457 int *searching, int *search_next_done, regex_t *regex)
1459 const struct got_error *err = NULL;
1463 * We keep all commits open throughout the lifetime of the log
1464 * view in order to avoid having to re-fetch commits from disk
1465 * while updating the display.
1467 while (nqueued < minqueue ||
1468 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1469 struct got_object_id *id;
1470 struct got_commit_object *commit;
1471 struct commit_queue_entry *entry;
1474 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1475 if (err || id == NULL)
1478 err = got_object_open_as_commit(&commit, repo, id);
1481 entry = alloc_commit_queue_entry(commit, id);
1482 if (entry == NULL) {
1483 err = got_error_from_errno("alloc_commit_queue_entry");
1487 errcode = pthread_mutex_lock(&tog_mutex);
1489 err = got_error_set_errno(errcode,
1490 "pthread_mutex_lock");
1494 entry->idx = commits->ncommits;
1495 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1497 commits->ncommits++;
1499 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1501 err = match_commit(&have_match, id, commit, regex);
1505 *search_next_done = TOG_SEARCH_HAVE_MORE;
1508 errcode = pthread_mutex_unlock(&tog_mutex);
1509 if (errcode && err == NULL)
1510 err = got_error_set_errno(errcode,
1511 "pthread_mutex_unlock");
1519 static const struct got_error *
1520 draw_commits(struct tog_view *view)
1522 const struct got_error *err = NULL;
1523 struct tog_log_view_state *s = &view->state.log;
1524 struct commit_queue_entry *entry;
1525 const int limit = view->nlines;
1527 int ncommits, author_cols = 4;
1528 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1529 char *refs_str = NULL;
1531 struct tog_color *tc;
1532 static const size_t date_display_cols = 12;
1534 entry = s->first_displayed_entry;
1537 if (ncommits == s->selected) {
1538 s->selected_entry = entry;
1541 entry = TAILQ_NEXT(entry, entry);
1545 if (s->selected_entry &&
1546 !(view->searching && view->search_next_done == 0)) {
1547 err = got_object_id_str(&id_str, s->selected_entry->id);
1550 err = build_refs_str(&refs_str, &s->refs,
1551 s->selected_entry->id, s->repo);
1556 if (s->thread_args.commits_needed == 0)
1557 halfdelay(10); /* disable fast refresh */
1559 if (s->thread_args.commits_needed > 0) {
1560 if (asprintf(&ncommits_str, " [%d/%d] %s",
1561 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1562 (view->searching && !view->search_next_done) ?
1563 "searching..." : "loading...") == -1) {
1564 err = got_error_from_errno("asprintf");
1568 const char *search_str = NULL;
1570 if (view->searching) {
1571 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1572 search_str = "no more matches";
1573 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1574 search_str = "no matches found";
1575 else if (!view->search_next_done)
1576 search_str = "searching...";
1579 if (asprintf(&ncommits_str, " [%d/%d] %s",
1580 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1581 search_str ? search_str :
1582 (refs_str ? refs_str : "")) == -1) {
1583 err = got_error_from_errno("asprintf");
1588 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1589 if (asprintf(&header, "commit %s %s%s",
1590 id_str ? id_str : "........................................",
1591 s->in_repo_path, ncommits_str) == -1) {
1592 err = got_error_from_errno("asprintf");
1596 } else if (asprintf(&header, "commit %s%s",
1597 id_str ? id_str : "........................................",
1598 ncommits_str) == -1) {
1599 err = got_error_from_errno("asprintf");
1603 err = format_line(&wline, &width, header, view->ncols, 0);
1607 werase(view->window);
1609 if (view_needs_focus_indication(view))
1610 wstandout(view->window);
1611 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1613 wattr_on(view->window,
1614 COLOR_PAIR(tc->colorpair), NULL);
1615 waddwstr(view->window, wline);
1617 wattr_off(view->window,
1618 COLOR_PAIR(tc->colorpair), NULL);
1619 while (width < view->ncols) {
1620 waddch(view->window, ' ');
1623 if (view_needs_focus_indication(view))
1624 wstandend(view->window);
1629 /* Grow author column size if necessary. */
1630 entry = s->first_displayed_entry;
1636 if (ncommits >= limit - 1)
1638 author = strdup(got_object_commit_get_author(entry->commit));
1639 if (author == NULL) {
1640 err = got_error_from_errno("strdup");
1643 err = format_author(&wauthor, &width, author, COLS,
1645 if (author_cols < width)
1646 author_cols = width;
1650 entry = TAILQ_NEXT(entry, entry);
1653 entry = s->first_displayed_entry;
1654 s->last_displayed_entry = s->first_displayed_entry;
1657 if (ncommits >= limit - 1)
1659 if (ncommits == s->selected)
1660 wstandout(view->window);
1661 err = draw_commit(view, entry->commit, entry->id,
1662 date_display_cols, author_cols);
1663 if (ncommits == s->selected)
1664 wstandend(view->window);
1668 s->last_displayed_entry = entry;
1669 entry = TAILQ_NEXT(entry, entry);
1682 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1684 struct commit_queue_entry *entry;
1687 entry = TAILQ_FIRST(&s->commits.head);
1688 if (s->first_displayed_entry == entry)
1691 entry = s->first_displayed_entry;
1692 while (entry && nscrolled < maxscroll) {
1693 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1695 s->first_displayed_entry = entry;
1701 static const struct got_error *
1702 trigger_log_thread(struct tog_view *view, int wait)
1704 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1707 halfdelay(1); /* fast refresh while loading commits */
1709 while (ta->commits_needed > 0) {
1710 if (ta->log_complete)
1713 /* Wake the log thread. */
1714 errcode = pthread_cond_signal(&ta->need_commits);
1716 return got_error_set_errno(errcode,
1717 "pthread_cond_signal");
1720 * The mutex will be released while the view loop waits
1721 * in wgetch(), at which time the log thread will run.
1726 /* Display progress update in log view. */
1727 show_log_view(view);
1731 /* Wait right here while next commit is being loaded. */
1732 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1734 return got_error_set_errno(errcode,
1735 "pthread_cond_wait");
1737 /* Display progress update in log view. */
1738 show_log_view(view);
1746 static const struct got_error *
1747 log_scroll_down(struct tog_view *view, int maxscroll)
1749 struct tog_log_view_state *s = &view->state.log;
1750 const struct got_error *err = NULL;
1751 struct commit_queue_entry *pentry;
1752 int nscrolled = 0, ncommits_needed;
1754 if (s->last_displayed_entry == NULL)
1757 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1758 if (s->commits.ncommits < ncommits_needed &&
1759 !s->thread_args.log_complete) {
1761 * Ask the log thread for required amount of commits.
1763 s->thread_args.commits_needed += maxscroll;
1764 err = trigger_log_thread(view, 1);
1770 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1774 s->last_displayed_entry = pentry;
1776 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1779 s->first_displayed_entry = pentry;
1780 } while (++nscrolled < maxscroll);
1785 static const struct got_error *
1786 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1787 struct got_commit_object *commit, struct got_object_id *commit_id,
1788 struct tog_view *log_view, struct got_repository *repo)
1790 const struct got_error *err;
1791 struct got_object_qid *parent_id;
1792 struct tog_view *diff_view;
1794 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1795 if (diff_view == NULL)
1796 return got_error_from_errno("view_open");
1798 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1799 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1800 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1802 *new_view = diff_view;
1806 static const struct got_error *
1807 tree_view_visit_subtree(struct tog_tree_view_state *s,
1808 struct got_tree_object *subtree)
1810 struct tog_parent_tree *parent;
1812 parent = calloc(1, sizeof(*parent));
1814 return got_error_from_errno("calloc");
1816 parent->tree = s->tree;
1817 parent->first_displayed_entry = s->first_displayed_entry;
1818 parent->selected_entry = s->selected_entry;
1819 parent->selected = s->selected;
1820 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1823 s->first_displayed_entry = NULL;
1827 static const struct got_error *
1828 tree_view_walk_path(struct tog_tree_view_state *s,
1829 struct got_object_id *commit_id, const char *path)
1831 const struct got_error *err = NULL;
1832 struct got_tree_object *tree = NULL;
1834 char *slash, *subpath = NULL;
1836 /* Walk the path and open corresponding tree objects. */
1839 struct got_tree_entry *te;
1840 struct got_object_id *tree_id;
1846 /* Ensure the correct subtree entry is selected. */
1847 slash = strchr(p, '/');
1849 te_name = strdup(p);
1851 te_name = strndup(p, slash - p);
1852 if (te_name == NULL) {
1853 err = got_error_from_errno("strndup");
1856 te = got_object_tree_find_entry(s->tree, te_name);
1858 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1863 s->first_displayed_entry = s->selected_entry = te;
1865 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1866 break; /* jump to this file's entry */
1868 slash = strchr(p, '/');
1870 subpath = strndup(path, slash - path);
1872 subpath = strdup(path);
1873 if (subpath == NULL) {
1874 err = got_error_from_errno("strdup");
1878 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1883 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1888 err = tree_view_visit_subtree(s, tree);
1890 got_object_tree_close(tree);
1904 static const struct got_error *
1905 browse_commit_tree(struct tog_view **new_view, int begin_x,
1906 struct commit_queue_entry *entry, const char *path,
1907 struct got_repository *repo)
1909 const struct got_error *err = NULL;
1910 struct got_tree_object *tree;
1911 struct tog_tree_view_state *s;
1912 struct tog_view *tree_view;
1914 err = got_object_open_as_tree(&tree, repo,
1915 got_object_commit_get_tree_id(entry->commit));
1919 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1920 if (tree_view == NULL)
1921 return got_error_from_errno("view_open");
1923 err = open_tree_view(tree_view, tree, entry->id, repo);
1925 got_object_tree_close(tree);
1928 s = &tree_view->state.tree;
1930 *new_view = tree_view;
1932 if (got_path_is_root_dir(path))
1935 return tree_view_walk_path(s, entry->id, path);
1938 static const struct got_error *
1939 block_signals_used_by_main_thread(void)
1944 if (sigemptyset(&sigset) == -1)
1945 return got_error_from_errno("sigemptyset");
1947 /* tog handles SIGWINCH and SIGCONT */
1948 if (sigaddset(&sigset, SIGWINCH) == -1)
1949 return got_error_from_errno("sigaddset");
1950 if (sigaddset(&sigset, SIGCONT) == -1)
1951 return got_error_from_errno("sigaddset");
1953 /* ncurses handles SIGTSTP */
1954 if (sigaddset(&sigset, SIGTSTP) == -1)
1955 return got_error_from_errno("sigaddset");
1957 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1959 return got_error_set_errno(errcode, "pthread_sigmask");
1965 log_thread(void *arg)
1967 const struct got_error *err = NULL;
1969 struct tog_log_thread_args *a = arg;
1972 err = block_signals_used_by_main_thread();
1976 while (!done && !err && !tog_sigpipe_received) {
1977 err = queue_commits(a->graph, a->commits, 1, a->repo,
1978 a->in_repo_path, a->searching, a->search_next_done,
1981 if (err->code != GOT_ERR_ITER_COMPLETED)
1985 } else if (a->commits_needed > 0)
1986 a->commits_needed--;
1988 errcode = pthread_mutex_lock(&tog_mutex);
1990 err = got_error_set_errno(errcode,
1991 "pthread_mutex_lock");
1993 } else if (*a->quit)
1995 else if (*a->first_displayed_entry == NULL) {
1996 *a->first_displayed_entry =
1997 TAILQ_FIRST(&a->commits->head);
1998 *a->selected_entry = *a->first_displayed_entry;
2001 errcode = pthread_cond_signal(&a->commit_loaded);
2003 err = got_error_set_errno(errcode,
2004 "pthread_cond_signal");
2005 pthread_mutex_unlock(&tog_mutex);
2010 a->commits_needed = 0;
2012 if (a->commits_needed == 0) {
2013 errcode = pthread_cond_wait(&a->need_commits,
2016 err = got_error_set_errno(errcode,
2017 "pthread_cond_wait");
2021 errcode = pthread_mutex_unlock(&tog_mutex);
2022 if (errcode && err == NULL)
2023 err = got_error_set_errno(errcode,
2024 "pthread_mutex_unlock");
2026 a->log_complete = 1;
2030 static const struct got_error *
2031 stop_log_thread(struct tog_log_view_state *s)
2033 const struct got_error *err = NULL;
2038 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2040 return got_error_set_errno(errcode,
2041 "pthread_cond_signal");
2042 errcode = pthread_mutex_unlock(&tog_mutex);
2044 return got_error_set_errno(errcode,
2045 "pthread_mutex_unlock");
2046 errcode = pthread_join(s->thread, (void **)&err);
2048 return got_error_set_errno(errcode, "pthread_join");
2049 errcode = pthread_mutex_lock(&tog_mutex);
2051 return got_error_set_errno(errcode,
2052 "pthread_mutex_lock");
2056 if (s->thread_args.repo) {
2057 got_repo_close(s->thread_args.repo);
2058 s->thread_args.repo = NULL;
2061 if (s->thread_args.graph) {
2062 got_commit_graph_close(s->thread_args.graph);
2063 s->thread_args.graph = NULL;
2069 static const struct got_error *
2070 close_log_view(struct tog_view *view)
2072 const struct got_error *err = NULL;
2073 struct tog_log_view_state *s = &view->state.log;
2076 err = stop_log_thread(s);
2078 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2079 if (errcode && err == NULL)
2080 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2082 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2083 if (errcode && err == NULL)
2084 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2086 free_commits(&s->commits);
2087 free(s->in_repo_path);
2088 s->in_repo_path = NULL;
2091 got_ref_list_free(&s->refs);
2095 static const struct got_error *
2096 search_start_log_view(struct tog_view *view)
2098 struct tog_log_view_state *s = &view->state.log;
2100 s->matched_entry = NULL;
2101 s->search_entry = NULL;
2105 static const struct got_error *
2106 search_next_log_view(struct tog_view *view)
2108 const struct got_error *err = NULL;
2109 struct tog_log_view_state *s = &view->state.log;
2110 struct commit_queue_entry *entry;
2112 /* Display progress update in log view. */
2113 show_log_view(view);
2117 if (s->search_entry) {
2119 errcode = pthread_mutex_unlock(&tog_mutex);
2121 return got_error_set_errno(errcode,
2122 "pthread_mutex_unlock");
2123 ch = wgetch(view->window);
2124 errcode = pthread_mutex_lock(&tog_mutex);
2126 return got_error_set_errno(errcode,
2127 "pthread_mutex_lock");
2128 if (ch == KEY_BACKSPACE) {
2129 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2132 if (view->searching == TOG_SEARCH_FORWARD)
2133 entry = TAILQ_NEXT(s->search_entry, entry);
2135 entry = TAILQ_PREV(s->search_entry,
2136 commit_queue_head, entry);
2137 } else if (s->matched_entry) {
2138 if (view->searching == TOG_SEARCH_FORWARD)
2139 entry = TAILQ_NEXT(s->matched_entry, entry);
2141 entry = TAILQ_PREV(s->matched_entry,
2142 commit_queue_head, entry);
2144 if (view->searching == TOG_SEARCH_FORWARD)
2145 entry = TAILQ_FIRST(&s->commits.head);
2147 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2153 if (entry == NULL) {
2154 if (s->thread_args.log_complete ||
2155 view->searching == TOG_SEARCH_BACKWARD) {
2156 view->search_next_done =
2157 (s->matched_entry == NULL ?
2158 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2159 s->search_entry = NULL;
2163 * Poke the log thread for more commits and return,
2164 * allowing the main loop to make progress. Search
2165 * will resume at s->search_entry once we come back.
2167 s->thread_args.commits_needed++;
2168 return trigger_log_thread(view, 0);
2171 err = match_commit(&have_match, entry->id, entry->commit,
2176 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2177 s->matched_entry = entry;
2181 s->search_entry = entry;
2182 if (view->searching == TOG_SEARCH_FORWARD)
2183 entry = TAILQ_NEXT(entry, entry);
2185 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2188 if (s->matched_entry) {
2189 int cur = s->selected_entry->idx;
2190 while (cur < s->matched_entry->idx) {
2191 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2196 while (cur > s->matched_entry->idx) {
2197 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2204 s->search_entry = NULL;
2209 static const struct got_error *
2210 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2211 struct got_repository *repo, const char *head_ref_name,
2212 const char *in_repo_path, int log_branches)
2214 const struct got_error *err = NULL;
2215 struct tog_log_view_state *s = &view->state.log;
2216 struct got_repository *thread_repo = NULL;
2217 struct got_commit_graph *thread_graph = NULL;
2220 SIMPLEQ_INIT(&s->refs);
2222 if (in_repo_path != s->in_repo_path) {
2223 free(s->in_repo_path);
2224 s->in_repo_path = strdup(in_repo_path);
2225 if (s->in_repo_path == NULL)
2226 return got_error_from_errno("strdup");
2229 /* The commit queue only contains commits being displayed. */
2230 TAILQ_INIT(&s->commits.head);
2231 s->commits.ncommits = 0;
2233 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2238 s->head_ref_name = head_ref_name;
2239 s->start_id = got_object_id_dup(start_id);
2240 if (s->start_id == NULL) {
2241 err = got_error_from_errno("got_object_id_dup");
2244 s->log_branches = log_branches;
2246 SIMPLEQ_INIT(&s->colors);
2247 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2248 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2249 get_color_value("TOG_COLOR_COMMIT"));
2252 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2253 get_color_value("TOG_COLOR_AUTHOR"));
2255 free_colors(&s->colors);
2258 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2259 get_color_value("TOG_COLOR_DATE"));
2261 free_colors(&s->colors);
2266 view->show = show_log_view;
2267 view->input = input_log_view;
2268 view->close = close_log_view;
2269 view->search_start = search_start_log_view;
2270 view->search_next = search_next_log_view;
2272 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2275 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2279 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2280 s->repo, NULL, NULL);
2284 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2286 err = got_error_set_errno(errcode, "pthread_cond_init");
2289 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2291 err = got_error_set_errno(errcode, "pthread_cond_init");
2295 s->thread_args.commits_needed = view->nlines;
2296 s->thread_args.graph = thread_graph;
2297 s->thread_args.commits = &s->commits;
2298 s->thread_args.in_repo_path = s->in_repo_path;
2299 s->thread_args.start_id = s->start_id;
2300 s->thread_args.repo = thread_repo;
2301 s->thread_args.log_complete = 0;
2302 s->thread_args.quit = &s->quit;
2303 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2304 s->thread_args.selected_entry = &s->selected_entry;
2305 s->thread_args.searching = &view->searching;
2306 s->thread_args.search_next_done = &view->search_next_done;
2307 s->thread_args.regex = &view->regex;
2310 close_log_view(view);
2314 static const struct got_error *
2315 show_log_view(struct tog_view *view)
2317 const struct got_error *err;
2318 struct tog_log_view_state *s = &view->state.log;
2320 if (s->thread == NULL) {
2321 int errcode = pthread_create(&s->thread, NULL, log_thread,
2324 return got_error_set_errno(errcode, "pthread_create");
2325 if (s->thread_args.commits_needed > 0) {
2326 err = trigger_log_thread(view, 1);
2332 return draw_commits(view);
2335 static const struct got_error *
2336 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2337 struct tog_view **focus_view, struct tog_view *view, int ch)
2339 const struct got_error *err = NULL;
2340 struct tog_log_view_state *s = &view->state.log;
2341 char *parent_path, *in_repo_path = NULL;
2342 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2343 struct tog_view *ref_view = NULL;
2345 struct got_object_id *start_id;
2355 if (s->first_displayed_entry == NULL)
2357 if (s->selected > 0)
2360 log_scroll_up(s, 1);
2364 if (s->first_displayed_entry == NULL)
2366 if (TAILQ_FIRST(&s->commits.head) ==
2367 s->first_displayed_entry) {
2371 log_scroll_up(s, view->nlines - 1);
2377 if (s->first_displayed_entry == NULL)
2379 if (s->selected < MIN(view->nlines - 2,
2380 s->commits.ncommits - 1)) {
2384 err = log_scroll_down(view, 1);
2388 struct commit_queue_entry *first;
2389 first = s->first_displayed_entry;
2392 err = log_scroll_down(view, view->nlines - 1);
2395 if (first == s->first_displayed_entry &&
2396 s->selected < MIN(view->nlines - 2,
2397 s->commits.ncommits - 1)) {
2398 /* can't scroll further down */
2399 s->selected = MIN(view->nlines - 2,
2400 s->commits.ncommits - 1);
2406 if (s->selected > view->nlines - 2)
2407 s->selected = view->nlines - 2;
2408 if (s->selected > s->commits.ncommits - 1)
2409 s->selected = s->commits.ncommits - 1;
2410 if (s->commits.ncommits < view->nlines - 1 &&
2411 !s->thread_args.log_complete) {
2412 s->thread_args.commits_needed += (view->nlines - 1) -
2413 s->commits.ncommits;
2414 err = trigger_log_thread(view, 1);
2420 if (s->selected_entry == NULL)
2422 if (view_is_parent_view(view))
2423 begin_x = view_split_begin_x(view->begin_x);
2424 err = open_diff_view_for_commit(&diff_view, begin_x,
2425 s->selected_entry->commit, s->selected_entry->id,
2429 if (view_is_parent_view(view)) {
2430 err = view_close_child(view);
2433 err = view_set_child(view, diff_view);
2435 view_close(diff_view);
2438 *focus_view = diff_view;
2440 *new_view = diff_view;
2443 if (s->selected_entry == NULL)
2445 if (view_is_parent_view(view))
2446 begin_x = view_split_begin_x(view->begin_x);
2447 err = browse_commit_tree(&tree_view, begin_x,
2448 s->selected_entry, s->in_repo_path, s->repo);
2451 if (view_is_parent_view(view)) {
2452 err = view_close_child(view);
2455 err = view_set_child(view, tree_view);
2457 view_close(tree_view);
2460 *focus_view = tree_view;
2462 *new_view = tree_view;
2465 if (got_path_cmp(s->in_repo_path, "/",
2466 strlen(s->in_repo_path), 1) == 0)
2468 err = got_path_dirname(&parent_path, s->in_repo_path);
2471 err = stop_log_thread(s);
2476 lv = view_open(view->nlines, view->ncols,
2477 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2480 return got_error_from_errno("view_open");
2482 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2483 parent_path, s->log_branches);
2487 if (view_is_parent_view(view))
2490 view_set_child(view->parent, lv);
2495 err = stop_log_thread(s);
2498 lv = view_open(view->nlines, view->ncols,
2499 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2501 return got_error_from_errno("view_open");
2502 err = got_repo_match_object_id(&start_id, NULL,
2503 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2504 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2509 in_repo_path = strdup(s->in_repo_path);
2510 if (in_repo_path == NULL) {
2513 return got_error_from_errno("strdup");
2515 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2516 in_repo_path, s->log_branches);
2526 s->log_branches = !s->log_branches;
2527 err = stop_log_thread(s);
2530 lv = view_open(view->nlines, view->ncols,
2531 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2533 return got_error_from_errno("view_open");
2534 err = open_log_view(lv, s->start_id, s->repo,
2535 s->head_ref_name, s->in_repo_path, s->log_branches);
2544 if (view_is_parent_view(view))
2545 begin_x = view_split_begin_x(view->begin_x);
2546 ref_view = view_open(view->nlines, view->ncols,
2547 view->begin_y, begin_x, TOG_VIEW_REF);
2548 if (ref_view == NULL)
2549 return got_error_from_errno("view_open");
2550 err = open_ref_view(ref_view, s->repo);
2552 view_close(ref_view);
2555 if (view_is_parent_view(view)) {
2556 err = view_close_child(view);
2559 err = view_set_child(view, ref_view);
2561 view_close(ref_view);
2564 *focus_view = ref_view;
2566 *new_view = ref_view;
2575 static const struct got_error *
2576 apply_unveil(const char *repo_path, const char *worktree_path)
2578 const struct got_error *error;
2581 if (unveil("gmon.out", "rwc") != 0)
2582 return got_error_from_errno2("unveil", "gmon.out");
2584 if (repo_path && unveil(repo_path, "r") != 0)
2585 return got_error_from_errno2("unveil", repo_path);
2587 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2588 return got_error_from_errno2("unveil", worktree_path);
2590 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2591 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2593 error = got_privsep_unveil_exec_helpers();
2597 if (unveil(NULL, NULL) != 0)
2598 return got_error_from_errno("unveil");
2608 halfdelay(1); /* Do fast refresh while initial view is loading. */
2611 intrflush(stdscr, FALSE);
2612 keypad(stdscr, TRUE);
2614 if (getenv("TOG_COLORS") != NULL) {
2616 use_default_colors();
2618 signal(SIGWINCH, tog_sigwinch);
2619 signal(SIGPIPE, tog_sigpipe);
2620 signal(SIGCONT, tog_sigcont);
2623 static const struct got_error *
2624 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2625 struct got_repository *repo, struct got_worktree *worktree)
2627 const struct got_error *err = NULL;
2630 *in_repo_path = strdup("/");
2631 if (*in_repo_path == NULL)
2632 return got_error_from_errno("strdup");
2637 const char *prefix = got_worktree_get_path_prefix(worktree);
2640 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2643 if (asprintf(in_repo_path, "%s%s%s", prefix,
2644 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2646 err = got_error_from_errno("asprintf");
2647 *in_repo_path = NULL;
2651 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2656 static const struct got_error *
2657 cmd_log(int argc, char *argv[])
2659 const struct got_error *error;
2660 struct got_repository *repo = NULL;
2661 struct got_worktree *worktree = NULL;
2662 struct got_object_id *start_id = NULL;
2663 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2664 char *start_commit = NULL, *head_ref_name = NULL;
2665 int ch, log_branches = 0;
2666 struct tog_view *view;
2669 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2674 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2680 start_commit = optarg;
2683 repo_path = realpath(optarg, NULL);
2684 if (repo_path == NULL)
2685 return got_error_from_errno2("realpath",
2700 cwd = getcwd(NULL, 0);
2702 return got_error_from_errno("getcwd");
2704 error = got_worktree_open(&worktree, cwd);
2705 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2708 if (repo_path == NULL) {
2711 strdup(got_worktree_get_repo_path(worktree));
2713 repo_path = strdup(cwd);
2715 if (repo_path == NULL) {
2716 error = got_error_from_errno("strdup");
2720 error = got_repo_open(&repo, repo_path, NULL);
2724 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2731 error = apply_unveil(got_repo_get_path(repo),
2732 worktree ? got_worktree_get_root_path(worktree) : NULL);
2736 if (start_commit == NULL)
2737 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2738 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2739 GOT_OBJ_TYPE_COMMIT, 1, repo);
2741 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2742 GOT_OBJ_TYPE_COMMIT, 1, repo);
2746 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2748 error = got_error_from_errno("view_open");
2752 head_ref_name = strdup(
2753 got_worktree_get_head_ref_name(worktree));
2754 if (head_ref_name == NULL) {
2755 error = got_error_from_errno("strdup");
2759 error = open_log_view(view, start_id, repo, head_ref_name,
2760 in_repo_path, log_branches);
2764 /* Release work tree lock. */
2765 got_worktree_close(worktree);
2768 error = view_loop(view);
2774 free(head_ref_name);
2776 got_repo_close(repo);
2778 got_worktree_close(worktree);
2786 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2787 "[-w] object1 object2\n", getprogname());
2792 parse_next_line(FILE *f, size_t *len)
2797 const char delim[3] = { '\0', '\0', '\0'};
2799 line = fparseln(f, &linelen, &lineno, delim, 0);
2806 match_line(const char *line, regex_t *regex, size_t nmatch,
2807 regmatch_t *regmatch)
2809 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2813 match_color(struct tog_colors *colors, const char *line)
2815 struct tog_color *tc = NULL;
2817 SIMPLEQ_FOREACH(tc, colors, entry) {
2818 if (match_line(line, &tc->regex, 0, NULL))
2825 static const struct got_error *
2826 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2827 WINDOW *window, regmatch_t *regmatch)
2829 const struct got_error *err = NULL;
2836 s = strndup(line, regmatch->rm_so);
2838 return got_error_from_errno("strndup");
2840 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2845 waddwstr(window, wline);
2852 s = strndup(line + regmatch->rm_so,
2853 regmatch->rm_eo - regmatch->rm_so);
2855 err = got_error_from_errno("strndup");
2859 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2864 wattr_on(window, A_STANDOUT, NULL);
2865 waddwstr(window, wline);
2866 wattr_off(window, A_STANDOUT, NULL);
2873 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2874 err = format_line(&wline, &width,
2875 line + regmatch->rm_eo, wlimit, col_tab_align);
2878 waddwstr(window, wline);
2886 static const struct got_error *
2887 draw_file(struct tog_view *view, const char *header)
2889 struct tog_diff_view_state *s = &view->state.diff;
2890 regmatch_t *regmatch = &view->regmatch;
2891 const struct got_error *err;
2894 struct tog_color *tc;
2898 int max_lines = view->nlines;
2899 int nlines = s->nlines;
2902 line_offset = s->line_offsets[s->first_displayed_line - 1];
2903 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2904 return got_error_from_errno("fseek");
2906 werase(view->window);
2909 if (asprintf(&line, "[%d/%d] %s",
2910 s->first_displayed_line - 1 + s->selected_line, nlines,
2912 return got_error_from_errno("asprintf");
2913 err = format_line(&wline, &width, line, view->ncols, 0);
2918 if (view_needs_focus_indication(view))
2919 wstandout(view->window);
2920 waddwstr(view->window, wline);
2923 if (view_needs_focus_indication(view))
2924 wstandend(view->window);
2925 if (width <= view->ncols - 1)
2926 waddch(view->window, '\n');
2934 while (max_lines > 0 && nprinted < max_lines) {
2935 line = parse_next_line(s->f, &len);
2941 tc = match_color(&s->colors, line);
2943 wattr_on(view->window,
2944 COLOR_PAIR(tc->colorpair), NULL);
2945 if (s->first_displayed_line + nprinted == s->matched_line &&
2946 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2947 err = add_matched_line(&width, line, view->ncols, 0,
2948 view->window, regmatch);
2954 err = format_line(&wline, &width, line, view->ncols, 0);
2959 waddwstr(view->window, wline);
2964 wattr_off(view->window,
2965 COLOR_PAIR(tc->colorpair), NULL);
2966 if (width <= view->ncols - 1)
2967 waddch(view->window, '\n');
2972 s->last_displayed_line = s->first_displayed_line +
2975 s->last_displayed_line = s->first_displayed_line;
2980 while (nprinted < view->nlines) {
2981 waddch(view->window, '\n');
2985 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2990 wstandout(view->window);
2991 waddwstr(view->window, wline);
2994 wstandend(view->window);
3001 get_datestr(time_t *time, char *datebuf)
3003 struct tm mytm, *tm;
3006 tm = gmtime_r(time, &mytm);
3009 s = asctime_r(tm, datebuf);
3012 p = strchr(s, '\n');
3018 static const struct got_error *
3019 get_changed_paths(struct got_pathlist_head *paths,
3020 struct got_commit_object *commit, struct got_repository *repo)
3022 const struct got_error *err = NULL;
3023 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3024 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3025 struct got_object_qid *qid;
3027 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3029 struct got_commit_object *pcommit;
3030 err = got_object_open_as_commit(&pcommit, repo,
3035 tree_id1 = got_object_commit_get_tree_id(pcommit);
3036 got_object_commit_close(pcommit);
3041 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3046 tree_id2 = got_object_commit_get_tree_id(commit);
3047 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3051 err = got_diff_tree(tree1, tree2, "", "", repo,
3052 got_diff_tree_collect_changed_paths, paths, 0);
3055 got_object_tree_close(tree1);
3057 got_object_tree_close(tree2);
3061 static const struct got_error *
3062 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3066 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3068 return got_error_from_errno("reallocarray");
3070 (*line_offsets)[*nlines] = off;
3075 static const struct got_error *
3076 write_commit_info(off_t **line_offsets, size_t *nlines,
3077 struct got_object_id *commit_id, struct got_reflist_head *refs,
3078 struct got_repository *repo, FILE *outfile)
3080 const struct got_error *err = NULL;
3081 char datebuf[26], *datestr;
3082 struct got_commit_object *commit;
3083 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3084 time_t committer_time;
3085 const char *author, *committer;
3086 char *refs_str = NULL;
3087 struct got_pathlist_head changed_paths;
3088 struct got_pathlist_entry *pe;
3092 TAILQ_INIT(&changed_paths);
3095 err = build_refs_str(&refs_str, refs, commit_id, repo);
3100 err = got_object_open_as_commit(&commit, repo, commit_id);
3104 err = got_object_id_str(&id_str, commit_id);
3106 err = got_error_from_errno("got_object_id_str");
3110 err = add_line_offset(line_offsets, nlines, 0);
3114 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3115 refs_str ? refs_str : "", refs_str ? ")" : "");
3117 err = got_error_from_errno("fprintf");
3121 err = add_line_offset(line_offsets, nlines, outoff);
3125 n = fprintf(outfile, "from: %s\n",
3126 got_object_commit_get_author(commit));
3128 err = got_error_from_errno("fprintf");
3132 err = add_line_offset(line_offsets, nlines, outoff);
3136 committer_time = got_object_commit_get_committer_time(commit);
3137 datestr = get_datestr(&committer_time, datebuf);
3139 n = fprintf(outfile, "date: %s UTC\n", datestr);
3141 err = got_error_from_errno("fprintf");
3145 err = add_line_offset(line_offsets, nlines, outoff);
3149 author = got_object_commit_get_author(commit);
3150 committer = got_object_commit_get_committer(commit);
3151 if (strcmp(author, committer) != 0) {
3152 n = fprintf(outfile, "via: %s\n", committer);
3154 err = got_error_from_errno("fprintf");
3158 err = add_line_offset(line_offsets, nlines, outoff);
3162 err = got_object_commit_get_logmsg(&logmsg, commit);
3166 while ((line = strsep(&s, "\n")) != NULL) {
3167 n = fprintf(outfile, "%s\n", line);
3169 err = got_error_from_errno("fprintf");
3173 err = add_line_offset(line_offsets, nlines, outoff);
3178 err = get_changed_paths(&changed_paths, commit, repo);
3181 TAILQ_FOREACH(pe, &changed_paths, entry) {
3182 struct got_diff_changed_path *cp = pe->data;
3183 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3185 err = got_error_from_errno("fprintf");
3189 err = add_line_offset(line_offsets, nlines, outoff);
3192 free((char *)pe->path);
3196 fputc('\n', outfile);
3198 err = add_line_offset(line_offsets, nlines, outoff);
3200 got_pathlist_free(&changed_paths);
3204 got_object_commit_close(commit);
3206 free(*line_offsets);
3207 *line_offsets = NULL;
3213 static const struct got_error *
3214 create_diff(struct tog_diff_view_state *s)
3216 const struct got_error *err = NULL;
3220 free(s->line_offsets);
3221 s->line_offsets = malloc(sizeof(off_t));
3222 if (s->line_offsets == NULL)
3223 return got_error_from_errno("malloc");
3228 err = got_error_from_errno("got_opentemp");
3231 if (s->f && fclose(s->f) != 0) {
3232 err = got_error_from_errno("fclose");
3238 err = got_object_get_type(&obj_type, s->repo, s->id1);
3240 err = got_object_get_type(&obj_type, s->repo, s->id2);
3245 case GOT_OBJ_TYPE_BLOB:
3246 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3247 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3248 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3250 case GOT_OBJ_TYPE_TREE:
3251 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3252 s->id1, s->id2, "", "", s->diff_context,
3253 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3255 case GOT_OBJ_TYPE_COMMIT: {
3256 const struct got_object_id_queue *parent_ids;
3257 struct got_object_qid *pid;
3258 struct got_commit_object *commit2;
3260 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3263 /* Show commit info if we're diffing to a parent/root commit. */
3264 if (s->id1 == NULL) {
3265 err = write_commit_info(&s->line_offsets, &s->nlines,
3266 s->id2, &s->refs, s->repo, s->f);
3270 parent_ids = got_object_commit_get_parent_ids(commit2);
3271 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3272 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3273 err = write_commit_info(
3274 &s->line_offsets, &s->nlines,
3275 s->id2, &s->refs, s->repo, s->f);
3282 got_object_commit_close(commit2);
3284 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3285 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3286 s->force_text_diff, s->repo, s->f);
3290 err = got_error(GOT_ERR_OBJ_TYPE);
3296 if (s->f && fflush(s->f) != 0 && err == NULL)
3297 err = got_error_from_errno("fflush");
3302 diff_view_indicate_progress(struct tog_view *view)
3304 mvwaddstr(view->window, 0, 0, "diffing...");
3309 static const struct got_error *
3310 search_start_diff_view(struct tog_view *view)
3312 struct tog_diff_view_state *s = &view->state.diff;
3314 s->matched_line = 0;
3318 static const struct got_error *
3319 search_next_diff_view(struct tog_view *view)
3321 struct tog_diff_view_state *s = &view->state.diff;
3324 if (!view->searching) {
3325 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3329 if (s->matched_line) {
3330 if (view->searching == TOG_SEARCH_FORWARD)
3331 lineno = s->matched_line + 1;
3333 lineno = s->matched_line - 1;
3335 if (view->searching == TOG_SEARCH_FORWARD)
3346 if (lineno <= 0 || lineno > s->nlines) {
3347 if (s->matched_line == 0) {
3348 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3353 if (view->searching == TOG_SEARCH_FORWARD)
3359 offset = s->line_offsets[lineno - 1];
3360 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3362 return got_error_from_errno("fseeko");
3365 line = parse_next_line(s->f, &len);
3367 match_line(line, &view->regex, 1, &view->regmatch)) {
3368 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3369 s->matched_line = lineno;
3374 if (view->searching == TOG_SEARCH_FORWARD)
3380 if (s->matched_line) {
3381 s->first_displayed_line = s->matched_line;
3382 s->selected_line = 1;
3388 static const struct got_error *
3389 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3390 struct got_object_id *id2, const char *label1, const char *label2,
3391 int diff_context, int ignore_whitespace, int force_text_diff,
3392 struct tog_view *log_view, struct got_repository *repo)
3394 const struct got_error *err;
3395 struct tog_diff_view_state *s = &view->state.diff;
3397 SIMPLEQ_INIT(&s->refs);
3399 if (id1 != NULL && id2 != NULL) {
3401 err = got_object_get_type(&type1, repo, id1);
3404 err = got_object_get_type(&type2, repo, id2);
3409 return got_error(GOT_ERR_OBJ_TYPE);
3411 s->first_displayed_line = 1;
3412 s->last_displayed_line = view->nlines;
3413 s->selected_line = 1;
3421 s->id1 = got_object_id_dup(id1);
3423 return got_error_from_errno("got_object_id_dup");
3427 s->id2 = got_object_id_dup(id2);
3428 if (s->id2 == NULL) {
3431 return got_error_from_errno("got_object_id_dup");
3434 s->first_displayed_line = 1;
3435 s->last_displayed_line = view->nlines;
3436 s->diff_context = diff_context;
3437 s->ignore_whitespace = ignore_whitespace;
3438 s->force_text_diff = force_text_diff;
3439 s->log_view = log_view;
3442 SIMPLEQ_INIT(&s->colors);
3443 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3444 err = add_color(&s->colors,
3445 "^-", TOG_COLOR_DIFF_MINUS,
3446 get_color_value("TOG_COLOR_DIFF_MINUS"));
3449 err = add_color(&s->colors, "^\\+",
3450 TOG_COLOR_DIFF_PLUS,
3451 get_color_value("TOG_COLOR_DIFF_PLUS"));
3453 free_colors(&s->colors);
3456 err = add_color(&s->colors,
3457 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3458 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3460 free_colors(&s->colors);
3464 err = add_color(&s->colors,
3465 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3466 TOG_COLOR_DIFF_META,
3467 get_color_value("TOG_COLOR_DIFF_META"));
3469 free_colors(&s->colors);
3473 err = add_color(&s->colors,
3474 "^(from|via): ", TOG_COLOR_AUTHOR,
3475 get_color_value("TOG_COLOR_AUTHOR"));
3477 free_colors(&s->colors);
3481 err = add_color(&s->colors,
3482 "^date: ", TOG_COLOR_DATE,
3483 get_color_value("TOG_COLOR_DATE"));
3485 free_colors(&s->colors);
3490 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3496 free_colors(&s->colors);
3500 if (log_view && view_is_splitscreen(view))
3501 show_log_view(log_view); /* draw vborder */
3502 diff_view_indicate_progress(view);
3504 s->line_offsets = NULL;
3506 err = create_diff(s);
3512 free_colors(&s->colors);
3513 got_ref_list_free(&s->refs);
3517 view->show = show_diff_view;
3518 view->input = input_diff_view;
3519 view->close = close_diff_view;
3520 view->search_start = search_start_diff_view;
3521 view->search_next = search_next_diff_view;
3526 static const struct got_error *
3527 close_diff_view(struct tog_view *view)
3529 const struct got_error *err = NULL;
3530 struct tog_diff_view_state *s = &view->state.diff;
3536 if (s->f && fclose(s->f) == EOF)
3537 err = got_error_from_errno("fclose");
3538 free_colors(&s->colors);
3539 free(s->line_offsets);
3540 s->line_offsets = NULL;
3542 got_ref_list_free(&s->refs);
3546 static const struct got_error *
3547 show_diff_view(struct tog_view *view)
3549 const struct got_error *err;
3550 struct tog_diff_view_state *s = &view->state.diff;
3551 char *id_str1 = NULL, *id_str2, *header;
3552 const char *label1, *label2;
3555 err = got_object_id_str(&id_str1, s->id1);
3558 label1 = s->label1 ? : id_str1;
3560 label1 = "/dev/null";
3562 err = got_object_id_str(&id_str2, s->id2);
3565 label2 = s->label2 ? : id_str2;
3567 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3568 err = got_error_from_errno("asprintf");
3576 return draw_file(view, header);
3579 static const struct got_error *
3580 set_selected_commit(struct tog_diff_view_state *s,
3581 struct commit_queue_entry *entry)
3583 const struct got_error *err;
3584 const struct got_object_id_queue *parent_ids;
3585 struct got_commit_object *selected_commit;
3586 struct got_object_qid *pid;
3589 s->id2 = got_object_id_dup(entry->id);
3591 return got_error_from_errno("got_object_id_dup");
3593 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3596 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3598 pid = SIMPLEQ_FIRST(parent_ids);
3599 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3600 got_object_commit_close(selected_commit);
3604 static const struct got_error *
3605 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3606 struct tog_view **focus_view, struct tog_view *view, int ch)
3608 const struct got_error *err = NULL;
3609 struct tog_diff_view_state *s = &view->state.diff;
3610 struct tog_log_view_state *ls;
3611 struct commit_queue_entry *entry;
3618 s->force_text_diff = !s->force_text_diff;
3620 s->ignore_whitespace = !s->ignore_whitespace;
3621 wclear(view->window);
3622 s->first_displayed_line = 1;
3623 s->last_displayed_line = view->nlines;
3624 diff_view_indicate_progress(view);
3625 err = create_diff(s);
3629 if (s->first_displayed_line > 1)
3630 s->first_displayed_line--;
3634 if (s->first_displayed_line == 1)
3637 while (i++ < view->nlines - 1 &&
3638 s->first_displayed_line > 1)
3639 s->first_displayed_line--;
3644 s->first_displayed_line++;
3652 while (!s->eof && i++ < view->nlines - 1) {
3654 line = parse_next_line(s->f, NULL);
3655 s->first_displayed_line++;
3661 if (s->diff_context > 0) {
3663 diff_view_indicate_progress(view);
3664 err = create_diff(s);
3665 if (s->first_displayed_line + view->nlines - 1 >
3667 s->first_displayed_line = 1;
3668 s->last_displayed_line = view->nlines;
3673 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3675 diff_view_indicate_progress(view);
3676 err = create_diff(s);
3681 if (s->log_view == NULL)
3683 ls = &s->log_view->state.log;
3684 entry = TAILQ_PREV(ls->selected_entry,
3685 commit_queue_head, entry);
3689 err = input_log_view(NULL, NULL, NULL, s->log_view,
3694 err = set_selected_commit(s, entry);
3698 s->first_displayed_line = 1;
3699 s->last_displayed_line = view->nlines;
3701 diff_view_indicate_progress(view);
3702 err = create_diff(s);
3706 if (s->log_view == NULL)
3708 ls = &s->log_view->state.log;
3710 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3711 ls->thread_args.commits_needed++;
3712 err = trigger_log_thread(s->log_view, 1);
3716 err = input_log_view(NULL, NULL, NULL, s->log_view,
3721 entry = TAILQ_NEXT(ls->selected_entry, entry);
3725 err = set_selected_commit(s, entry);
3729 s->first_displayed_line = 1;
3730 s->last_displayed_line = view->nlines;
3732 diff_view_indicate_progress(view);
3733 err = create_diff(s);
3742 static const struct got_error *
3743 cmd_diff(int argc, char *argv[])
3745 const struct got_error *error = NULL;
3746 struct got_repository *repo = NULL;
3747 struct got_worktree *worktree = NULL;
3748 struct got_object_id *id1 = NULL, *id2 = NULL;
3749 char *repo_path = NULL, *cwd = NULL;
3750 char *id_str1 = NULL, *id_str2 = NULL;
3751 char *label1 = NULL, *label2 = NULL;
3752 int diff_context = 3, ignore_whitespace = 0;
3753 int ch, force_text_diff = 0;
3755 struct tog_view *view;
3758 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3762 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3765 force_text_diff = 1;
3768 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3771 err(1, "-C option %s", errstr);
3774 repo_path = realpath(optarg, NULL);
3775 if (repo_path == NULL)
3776 return got_error_from_errno2("realpath",
3778 got_path_strip_trailing_slashes(repo_path);
3781 ignore_whitespace = 1;
3793 usage_diff(); /* TODO show local worktree changes */
3794 } else if (argc == 2) {
3800 cwd = getcwd(NULL, 0);
3802 return got_error_from_errno("getcwd");
3804 error = got_worktree_open(&worktree, cwd);
3805 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3808 if (repo_path == NULL) {
3811 strdup(got_worktree_get_repo_path(worktree));
3813 repo_path = strdup(cwd);
3815 if (repo_path == NULL) {
3816 error = got_error_from_errno("strdup");
3820 error = got_repo_open(&repo, repo_path, NULL);
3826 error = apply_unveil(got_repo_get_path(repo), NULL);
3830 error = got_repo_match_object_id(&id1, &label1, id_str1,
3831 GOT_OBJ_TYPE_ANY, 1, repo);
3835 error = got_repo_match_object_id(&id2, &label2, id_str2,
3836 GOT_OBJ_TYPE_ANY, 1, repo);
3840 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3842 error = got_error_from_errno("view_open");
3845 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3846 ignore_whitespace, force_text_diff, NULL, repo);
3849 error = view_loop(view);
3856 got_repo_close(repo);
3858 got_worktree_close(worktree);
3866 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3871 struct tog_blame_line {
3873 struct got_object_id *id;
3876 static const struct got_error *
3877 draw_blame(struct tog_view *view)
3879 struct tog_blame_view_state *s = &view->state.blame;
3880 struct tog_blame *blame = &s->blame;
3881 regmatch_t *regmatch = &view->regmatch;
3882 const struct got_error *err;
3883 int lineno = 0, nprinted = 0;
3888 struct tog_blame_line *blame_line;
3889 struct got_object_id *prev_id = NULL;
3891 struct tog_color *tc;
3893 err = got_object_id_str(&id_str, s->blamed_commit->id);
3898 werase(view->window);
3900 if (asprintf(&line, "commit %s", id_str) == -1) {
3901 err = got_error_from_errno("asprintf");
3906 err = format_line(&wline, &width, line, view->ncols, 0);
3911 if (view_needs_focus_indication(view))
3912 wstandout(view->window);
3913 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3915 wattr_on(view->window,
3916 COLOR_PAIR(tc->colorpair), NULL);
3917 waddwstr(view->window, wline);
3919 wattr_off(view->window,
3920 COLOR_PAIR(tc->colorpair), NULL);
3921 if (view_needs_focus_indication(view))
3922 wstandend(view->window);
3925 if (width < view->ncols - 1)
3926 waddch(view->window, '\n');
3928 if (asprintf(&line, "[%d/%d] %s%s",
3929 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3930 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3932 return got_error_from_errno("asprintf");
3935 err = format_line(&wline, &width, line, view->ncols, 0);
3940 waddwstr(view->window, wline);
3943 if (width < view->ncols - 1)
3944 waddch(view->window, '\n');
3947 while (nprinted < view->nlines - 2) {
3948 line = parse_next_line(blame->f, &len);
3953 if (++lineno < s->first_displayed_line) {
3958 if (view->focussed && nprinted == s->selected_line - 1)
3959 wstandout(view->window);
3961 if (blame->nlines > 0) {
3962 blame_line = &blame->lines[lineno - 1];
3963 if (blame_line->annotated && prev_id &&
3964 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3966 nprinted == s->selected_line - 1)) {
3967 waddstr(view->window, " ");
3968 } else if (blame_line->annotated) {
3970 err = got_object_id_str(&id_str, blame_line->id);
3975 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3977 wattr_on(view->window,
3978 COLOR_PAIR(tc->colorpair), NULL);
3979 wprintw(view->window, "%.8s", id_str);
3981 wattr_off(view->window,
3982 COLOR_PAIR(tc->colorpair), NULL);
3984 prev_id = blame_line->id;
3986 waddstr(view->window, "........");
3990 waddstr(view->window, "........");
3994 if (view->focussed && nprinted == s->selected_line - 1)
3995 wstandend(view->window);
3996 waddstr(view->window, " ");
3998 if (view->ncols <= 9) {
4000 wline = wcsdup(L"");
4001 if (wline == NULL) {
4002 err = got_error_from_errno("wcsdup");
4006 } else if (s->first_displayed_line + nprinted ==
4008 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4009 err = add_matched_line(&width, line, view->ncols - 9, 9,
4010 view->window, regmatch);
4017 err = format_line(&wline, &width, line,
4018 view->ncols - 9, 9);
4019 waddwstr(view->window, wline);
4025 if (width <= view->ncols - 1)
4026 waddch(view->window, '\n');
4027 if (++nprinted == 1)
4028 s->first_displayed_line = lineno;
4031 s->last_displayed_line = lineno;
4038 static const struct got_error *
4039 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4041 const struct got_error *err = NULL;
4042 struct tog_blame_cb_args *a = arg;
4043 struct tog_blame_line *line;
4046 if (nlines != a->nlines ||
4047 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4048 return got_error(GOT_ERR_RANGE);
4050 errcode = pthread_mutex_lock(&tog_mutex);
4052 return got_error_set_errno(errcode, "pthread_mutex_lock");
4054 if (*a->quit) { /* user has quit the blame view */
4055 err = got_error(GOT_ERR_ITER_COMPLETED);
4060 goto done; /* no change in this commit */
4062 line = &a->lines[lineno - 1];
4063 if (line->annotated)
4066 line->id = got_object_id_dup(id);
4067 if (line->id == NULL) {
4068 err = got_error_from_errno("got_object_id_dup");
4071 line->annotated = 1;
4073 errcode = pthread_mutex_unlock(&tog_mutex);
4075 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4080 blame_thread(void *arg)
4082 const struct got_error *err;
4083 struct tog_blame_thread_args *ta = arg;
4084 struct tog_blame_cb_args *a = ta->cb_args;
4087 err = block_signals_used_by_main_thread();
4091 err = got_blame(ta->path, a->commit_id, ta->repo,
4092 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4093 if (err && err->code == GOT_ERR_CANCELLED)
4096 errcode = pthread_mutex_lock(&tog_mutex);
4098 return (void *)got_error_set_errno(errcode,
4099 "pthread_mutex_lock");
4101 got_repo_close(ta->repo);
4105 errcode = pthread_mutex_unlock(&tog_mutex);
4106 if (errcode && err == NULL)
4107 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4112 static struct got_object_id *
4113 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4114 int first_displayed_line, int selected_line)
4116 struct tog_blame_line *line;
4121 line = &lines[first_displayed_line - 1 + selected_line - 1];
4122 if (!line->annotated)
4128 static const struct got_error *
4129 stop_blame(struct tog_blame *blame)
4131 const struct got_error *err = NULL;
4134 if (blame->thread) {
4136 errcode = pthread_mutex_unlock(&tog_mutex);
4138 return got_error_set_errno(errcode,
4139 "pthread_mutex_unlock");
4140 errcode = pthread_join(blame->thread, (void **)&err);
4142 return got_error_set_errno(errcode, "pthread_join");
4143 errcode = pthread_mutex_lock(&tog_mutex);
4145 return got_error_set_errno(errcode,
4146 "pthread_mutex_lock");
4147 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4149 blame->thread = NULL;
4151 if (blame->thread_args.repo) {
4152 got_repo_close(blame->thread_args.repo);
4153 blame->thread_args.repo = NULL;
4156 if (fclose(blame->f) != 0 && err == NULL)
4157 err = got_error_from_errno("fclose");
4161 for (i = 0; i < blame->nlines; i++)
4162 free(blame->lines[i].id);
4164 blame->lines = NULL;
4166 free(blame->cb_args.commit_id);
4167 blame->cb_args.commit_id = NULL;
4172 static const struct got_error *
4173 cancel_blame_view(void *arg)
4175 const struct got_error *err = NULL;
4179 errcode = pthread_mutex_lock(&tog_mutex);
4181 return got_error_set_errno(errcode,
4182 "pthread_mutex_unlock");
4185 err = got_error(GOT_ERR_CANCELLED);
4187 errcode = pthread_mutex_unlock(&tog_mutex);
4189 return got_error_set_errno(errcode,
4190 "pthread_mutex_lock");
4195 static const struct got_error *
4196 run_blame(struct tog_view *view)
4198 struct tog_blame_view_state *s = &view->state.blame;
4199 struct tog_blame *blame = &s->blame;
4200 const struct got_error *err = NULL;
4201 struct got_blob_object *blob = NULL;
4202 struct got_repository *thread_repo = NULL;
4203 struct got_object_id *obj_id = NULL;
4206 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4211 err = got_object_get_type(&obj_type, s->repo, obj_id);
4215 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4216 err = got_error(GOT_ERR_OBJ_TYPE);
4220 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4223 blame->f = got_opentemp();
4224 if (blame->f == NULL) {
4225 err = got_error_from_errno("got_opentemp");
4228 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4229 &blame->line_offsets, blame->f, blob);
4230 if (err || blame->nlines == 0)
4233 /* Don't include \n at EOF in the blame line count. */
4234 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4237 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4238 if (blame->lines == NULL) {
4239 err = got_error_from_errno("calloc");
4243 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4247 blame->cb_args.view = view;
4248 blame->cb_args.lines = blame->lines;
4249 blame->cb_args.nlines = blame->nlines;
4250 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4251 if (blame->cb_args.commit_id == NULL) {
4252 err = got_error_from_errno("got_object_id_dup");
4255 blame->cb_args.quit = &s->done;
4257 blame->thread_args.path = s->path;
4258 blame->thread_args.repo = thread_repo;
4259 blame->thread_args.cb_args = &blame->cb_args;
4260 blame->thread_args.complete = &s->blame_complete;
4261 blame->thread_args.cancel_cb = cancel_blame_view;
4262 blame->thread_args.cancel_arg = &s->done;
4263 s->blame_complete = 0;
4267 got_object_blob_close(blob);
4274 static const struct got_error *
4275 open_blame_view(struct tog_view *view, char *path,
4276 struct got_object_id *commit_id, struct got_repository *repo)
4278 const struct got_error *err = NULL;
4279 struct tog_blame_view_state *s = &view->state.blame;
4281 SIMPLEQ_INIT(&s->blamed_commits);
4283 s->path = strdup(path);
4284 if (s->path == NULL)
4285 return got_error_from_errno("strdup");
4287 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4293 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4294 s->first_displayed_line = 1;
4295 s->last_displayed_line = view->nlines;
4296 s->selected_line = 1;
4297 s->blame_complete = 0;
4299 s->commit_id = commit_id;
4300 memset(&s->blame, 0, sizeof(s->blame));
4302 SIMPLEQ_INIT(&s->colors);
4303 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4304 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4305 get_color_value("TOG_COLOR_COMMIT"));
4310 view->show = show_blame_view;
4311 view->input = input_blame_view;
4312 view->close = close_blame_view;
4313 view->search_start = search_start_blame_view;
4314 view->search_next = search_next_blame_view;
4316 return run_blame(view);
4319 static const struct got_error *
4320 close_blame_view(struct tog_view *view)
4322 const struct got_error *err = NULL;
4323 struct tog_blame_view_state *s = &view->state.blame;
4325 if (s->blame.thread)
4326 err = stop_blame(&s->blame);
4328 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4329 struct got_object_qid *blamed_commit;
4330 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4331 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4332 got_object_qid_free(blamed_commit);
4336 free_colors(&s->colors);
4341 static const struct got_error *
4342 search_start_blame_view(struct tog_view *view)
4344 struct tog_blame_view_state *s = &view->state.blame;
4346 s->matched_line = 0;
4350 static const struct got_error *
4351 search_next_blame_view(struct tog_view *view)
4353 struct tog_blame_view_state *s = &view->state.blame;
4356 if (!view->searching) {
4357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4361 if (s->matched_line) {
4362 if (view->searching == TOG_SEARCH_FORWARD)
4363 lineno = s->matched_line + 1;
4365 lineno = s->matched_line - 1;
4367 if (view->searching == TOG_SEARCH_FORWARD)
4370 lineno = s->blame.nlines;
4378 if (lineno <= 0 || lineno > s->blame.nlines) {
4379 if (s->matched_line == 0) {
4380 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4385 if (view->searching == TOG_SEARCH_FORWARD)
4388 lineno = s->blame.nlines;
4391 offset = s->blame.line_offsets[lineno - 1];
4392 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4394 return got_error_from_errno("fseeko");
4397 line = parse_next_line(s->blame.f, &len);
4399 match_line(line, &view->regex, 1, &view->regmatch)) {
4400 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 s->matched_line = lineno;
4406 if (view->searching == TOG_SEARCH_FORWARD)
4412 if (s->matched_line) {
4413 s->first_displayed_line = s->matched_line;
4414 s->selected_line = 1;
4420 static const struct got_error *
4421 show_blame_view(struct tog_view *view)
4423 const struct got_error *err = NULL;
4424 struct tog_blame_view_state *s = &view->state.blame;
4427 if (s->blame.thread == NULL) {
4428 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4429 &s->blame.thread_args);
4431 return got_error_set_errno(errcode, "pthread_create");
4433 halfdelay(1); /* fast refresh while annotating */
4436 if (s->blame_complete)
4437 halfdelay(10); /* disable fast refresh */
4439 err = draw_blame(view);
4445 static const struct got_error *
4446 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4447 struct tog_view **focus_view, struct tog_view *view, int ch)
4449 const struct got_error *err = NULL, *thread_err = NULL;
4450 struct tog_view *diff_view;
4451 struct tog_blame_view_state *s = &view->state.blame;
4460 if (s->selected_line > 1)
4462 else if (s->selected_line == 1 &&
4463 s->first_displayed_line > 1)
4464 s->first_displayed_line--;
4468 if (s->first_displayed_line == 1) {
4469 s->selected_line = 1;
4472 if (s->first_displayed_line > view->nlines - 2)
4473 s->first_displayed_line -=
4476 s->first_displayed_line = 1;
4480 if (s->selected_line < view->nlines - 2 &&
4481 s->first_displayed_line +
4482 s->selected_line <= s->blame.nlines)
4484 else if (s->last_displayed_line <
4486 s->first_displayed_line++;
4490 struct got_object_id *id = NULL;
4491 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4492 s->first_displayed_line, s->selected_line);
4496 struct got_commit_object *commit;
4497 struct got_object_qid *pid;
4498 struct got_object_id *blob_id = NULL;
4500 err = got_object_open_as_commit(&commit,
4504 pid = SIMPLEQ_FIRST(
4505 got_object_commit_get_parent_ids(commit));
4507 got_object_commit_close(commit);
4510 /* Check if path history ends here. */
4511 err = got_object_id_by_path(&blob_id, s->repo,
4514 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4516 got_object_commit_close(commit);
4519 err = got_object_get_type(&obj_type, s->repo,
4522 /* Can't blame non-blob type objects. */
4523 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4524 got_object_commit_close(commit);
4527 err = got_object_qid_alloc(&s->blamed_commit,
4529 got_object_commit_close(commit);
4531 if (got_object_id_cmp(id,
4532 s->blamed_commit->id) == 0)
4534 err = got_object_qid_alloc(&s->blamed_commit,
4540 thread_err = stop_blame(&s->blame);
4544 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4545 s->blamed_commit, entry);
4546 err = run_blame(view);
4552 struct got_object_qid *first;
4553 first = SIMPLEQ_FIRST(&s->blamed_commits);
4554 if (!got_object_id_cmp(first->id, s->commit_id))
4557 thread_err = stop_blame(&s->blame);
4561 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4562 got_object_qid_free(s->blamed_commit);
4564 SIMPLEQ_FIRST(&s->blamed_commits);
4565 err = run_blame(view);
4572 struct got_object_id *id = NULL;
4573 struct got_object_qid *pid;
4574 struct got_commit_object *commit = NULL;
4575 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4576 s->first_displayed_line, s->selected_line);
4579 err = got_object_open_as_commit(&commit, s->repo, id);
4582 pid = SIMPLEQ_FIRST(
4583 got_object_commit_get_parent_ids(commit));
4584 if (view_is_parent_view(view))
4585 begin_x = view_split_begin_x(view->begin_x);
4586 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4587 if (diff_view == NULL) {
4588 got_object_commit_close(commit);
4589 err = got_error_from_errno("view_open");
4592 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4593 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4594 got_object_commit_close(commit);
4596 view_close(diff_view);
4599 if (view_is_parent_view(view)) {
4600 err = view_close_child(view);
4603 err = view_set_child(view, diff_view);
4605 view_close(diff_view);
4608 *focus_view = diff_view;
4610 *new_view = diff_view;
4618 if (s->last_displayed_line >= s->blame.nlines &&
4619 s->selected_line >= MIN(s->blame.nlines,
4620 view->nlines - 2)) {
4623 if (s->last_displayed_line >= s->blame.nlines &&
4624 s->selected_line < view->nlines - 2) {
4625 s->selected_line = MIN(s->blame.nlines,
4629 if (s->last_displayed_line + view->nlines - 2
4631 s->first_displayed_line +=
4634 s->first_displayed_line =
4639 if (s->selected_line > view->nlines - 2) {
4640 s->selected_line = MIN(s->blame.nlines,
4647 return thread_err ? thread_err : err;
4650 static const struct got_error *
4651 cmd_blame(int argc, char *argv[])
4653 const struct got_error *error;
4654 struct got_repository *repo = NULL;
4655 struct got_worktree *worktree = NULL;
4656 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4657 char *link_target = NULL;
4658 struct got_object_id *commit_id = NULL;
4659 char *commit_id_str = NULL;
4661 struct tog_view *view;
4664 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4669 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4672 commit_id_str = optarg;
4675 repo_path = realpath(optarg, NULL);
4676 if (repo_path == NULL)
4677 return got_error_from_errno2("realpath",
4692 cwd = getcwd(NULL, 0);
4694 return got_error_from_errno("getcwd");
4696 error = got_worktree_open(&worktree, cwd);
4697 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4700 if (repo_path == NULL) {
4703 strdup(got_worktree_get_repo_path(worktree));
4705 repo_path = strdup(cwd);
4707 if (repo_path == NULL) {
4708 error = got_error_from_errno("strdup");
4712 error = got_repo_open(&repo, repo_path, NULL);
4716 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4723 error = apply_unveil(got_repo_get_path(repo), NULL);
4727 if (commit_id_str == NULL) {
4728 struct got_reference *head_ref;
4729 error = got_ref_open(&head_ref, repo, worktree ?
4730 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4733 error = got_ref_resolve(&commit_id, repo, head_ref);
4734 got_ref_close(head_ref);
4736 error = got_repo_match_object_id(&commit_id, NULL,
4737 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4742 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4744 error = got_error_from_errno("view_open");
4748 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4753 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4758 /* Release work tree lock. */
4759 got_worktree_close(worktree);
4762 error = view_loop(view);
4770 got_worktree_close(worktree);
4772 got_repo_close(repo);
4776 static const struct got_error *
4777 draw_tree_entries(struct tog_view *view, const char *parent_path)
4779 struct tog_tree_view_state *s = &view->state.tree;
4780 const struct got_error *err = NULL;
4781 struct got_tree_entry *te;
4783 struct tog_color *tc;
4784 int width, n, i, nentries;
4785 int limit = view->nlines;
4789 werase(view->window);
4794 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4797 if (view_needs_focus_indication(view))
4798 wstandout(view->window);
4799 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4801 wattr_on(view->window,
4802 COLOR_PAIR(tc->colorpair), NULL);
4803 waddwstr(view->window, wline);
4805 wattr_off(view->window,
4806 COLOR_PAIR(tc->colorpair), NULL);
4807 if (view_needs_focus_indication(view))
4808 wstandend(view->window);
4811 if (width < view->ncols - 1)
4812 waddch(view->window, '\n');
4815 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4818 waddwstr(view->window, wline);
4821 if (width < view->ncols - 1)
4822 waddch(view->window, '\n');
4825 waddch(view->window, '\n');
4829 if (s->first_displayed_entry == NULL) {
4830 te = got_object_tree_get_first_entry(s->tree);
4831 if (s->selected == 0) {
4833 wstandout(view->window);
4834 s->selected_entry = NULL;
4836 waddstr(view->window, " ..\n"); /* parent directory */
4837 if (s->selected == 0 && view->focussed)
4838 wstandend(view->window);
4845 te = s->first_displayed_entry;
4848 nentries = got_object_tree_get_nentries(s->tree);
4849 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4850 char *line = NULL, *id_str = NULL, *link_target = NULL;
4851 const char *modestr = "";
4854 te = got_object_tree_get_entry(s->tree, i);
4855 mode = got_tree_entry_get_mode(te);
4858 err = got_object_id_str(&id_str,
4859 got_tree_entry_get_id(te));
4861 return got_error_from_errno(
4862 "got_object_id_str");
4864 if (got_object_tree_entry_is_submodule(te))
4866 else if (S_ISLNK(mode)) {
4869 err = got_tree_entry_get_symlink_target(&link_target,
4875 for (i = 0; i < strlen(link_target); i++) {
4876 if (!isprint((unsigned char)link_target[i]))
4877 link_target[i] = '?';
4881 else if (S_ISDIR(mode))
4883 else if (mode & S_IXUSR)
4885 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4886 got_tree_entry_get_name(te), modestr,
4887 link_target ? " -> ": "",
4888 link_target ? link_target : "") == -1) {
4891 return got_error_from_errno("asprintf");
4895 err = format_line(&wline, &width, line, view->ncols, 0);
4900 if (n == s->selected) {
4902 wstandout(view->window);
4903 s->selected_entry = te;
4905 tc = match_color(&s->colors, line);
4907 wattr_on(view->window,
4908 COLOR_PAIR(tc->colorpair), NULL);
4909 waddwstr(view->window, wline);
4911 wattr_off(view->window,
4912 COLOR_PAIR(tc->colorpair), NULL);
4913 if (width < view->ncols - 1)
4914 waddch(view->window, '\n');
4915 if (n == s->selected && view->focussed)
4916 wstandend(view->window);
4922 s->last_displayed_entry = te;
4931 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4933 struct got_tree_entry *te;
4934 int isroot = s->tree == s->root;
4937 if (s->first_displayed_entry == NULL)
4940 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4941 while (i++ < maxscroll) {
4944 s->first_displayed_entry = NULL;
4947 s->first_displayed_entry = te;
4948 te = got_tree_entry_get_prev(s->tree, te);
4953 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4955 struct got_tree_entry *next, *last;
4958 if (s->first_displayed_entry)
4959 next = got_tree_entry_get_next(s->tree,
4960 s->first_displayed_entry);
4962 next = got_object_tree_get_first_entry(s->tree);
4964 last = s->last_displayed_entry;
4965 while (next && last && n++ < maxscroll) {
4966 last = got_tree_entry_get_next(s->tree, last);
4968 s->first_displayed_entry = next;
4969 next = got_tree_entry_get_next(s->tree, next);
4974 static const struct got_error *
4975 tree_entry_path(char **path, struct tog_parent_trees *parents,
4976 struct got_tree_entry *te)
4978 const struct got_error *err = NULL;
4979 struct tog_parent_tree *pt;
4980 size_t len = 2; /* for leading slash and NUL */
4982 TAILQ_FOREACH(pt, parents, entry)
4983 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4986 len += strlen(got_tree_entry_get_name(te));
4988 *path = calloc(1, len);
4990 return got_error_from_errno("calloc");
4993 pt = TAILQ_LAST(parents, tog_parent_trees);
4995 const char *name = got_tree_entry_get_name(pt->selected_entry);
4996 if (strlcat(*path, name, len) >= len) {
4997 err = got_error(GOT_ERR_NO_SPACE);
5000 if (strlcat(*path, "/", len) >= len) {
5001 err = got_error(GOT_ERR_NO_SPACE);
5004 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5007 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5008 err = got_error(GOT_ERR_NO_SPACE);
5020 static const struct got_error *
5021 blame_tree_entry(struct tog_view **new_view, int begin_x,
5022 struct got_tree_entry *te, struct tog_parent_trees *parents,
5023 struct got_object_id *commit_id, struct got_repository *repo)
5025 const struct got_error *err = NULL;
5027 struct tog_view *blame_view;
5031 err = tree_entry_path(&path, parents, te);
5035 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5036 if (blame_view == NULL) {
5037 err = got_error_from_errno("view_open");
5041 err = open_blame_view(blame_view, path, commit_id, repo);
5043 if (err->code == GOT_ERR_CANCELLED)
5045 view_close(blame_view);
5047 *new_view = blame_view;
5053 static const struct got_error *
5054 log_tree_entry(struct tog_view **new_view, int begin_x,
5055 struct got_tree_entry *te, struct tog_parent_trees *parents,
5056 struct got_object_id *commit_id, struct got_repository *repo)
5058 struct tog_view *log_view;
5059 const struct got_error *err = NULL;
5064 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5065 if (log_view == NULL)
5066 return got_error_from_errno("view_open");
5068 err = tree_entry_path(&path, parents, te);
5072 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5074 view_close(log_view);
5076 *new_view = log_view;
5081 static const struct got_error *
5082 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5083 struct got_object_id *commit_id, struct got_repository *repo)
5085 const struct got_error *err = NULL;
5086 char *commit_id_str = NULL;
5087 struct tog_tree_view_state *s = &view->state.tree;
5089 TAILQ_INIT(&s->parents);
5091 err = got_object_id_str(&commit_id_str, commit_id);
5095 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5096 err = got_error_from_errno("asprintf");
5100 s->root = s->tree = root;
5101 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5102 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5103 s->commit_id = got_object_id_dup(commit_id);
5104 if (s->commit_id == NULL) {
5105 err = got_error_from_errno("got_object_id_dup");
5110 SIMPLEQ_INIT(&s->colors);
5112 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5113 err = add_color(&s->colors, "\\$$",
5114 TOG_COLOR_TREE_SUBMODULE,
5115 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5118 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5119 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5121 free_colors(&s->colors);
5124 err = add_color(&s->colors, "/$",
5125 TOG_COLOR_TREE_DIRECTORY,
5126 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5128 free_colors(&s->colors);
5132 err = add_color(&s->colors, "\\*$",
5133 TOG_COLOR_TREE_EXECUTABLE,
5134 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5136 free_colors(&s->colors);
5140 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5141 get_color_value("TOG_COLOR_COMMIT"));
5143 free_colors(&s->colors);
5148 view->show = show_tree_view;
5149 view->input = input_tree_view;
5150 view->close = close_tree_view;
5151 view->search_start = search_start_tree_view;
5152 view->search_next = search_next_tree_view;
5154 free(commit_id_str);
5156 free(s->tree_label);
5157 s->tree_label = NULL;
5162 static const struct got_error *
5163 close_tree_view(struct tog_view *view)
5165 struct tog_tree_view_state *s = &view->state.tree;
5167 free_colors(&s->colors);
5168 free(s->tree_label);
5169 s->tree_label = NULL;
5171 s->commit_id = NULL;
5172 while (!TAILQ_EMPTY(&s->parents)) {
5173 struct tog_parent_tree *parent;
5174 parent = TAILQ_FIRST(&s->parents);
5175 TAILQ_REMOVE(&s->parents, parent, entry);
5179 if (s->tree != s->root)
5180 got_object_tree_close(s->tree);
5181 got_object_tree_close(s->root);
5185 static const struct got_error *
5186 search_start_tree_view(struct tog_view *view)
5188 struct tog_tree_view_state *s = &view->state.tree;
5190 s->matched_entry = NULL;
5195 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5197 regmatch_t regmatch;
5199 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5203 static const struct got_error *
5204 search_next_tree_view(struct tog_view *view)
5206 struct tog_tree_view_state *s = &view->state.tree;
5207 struct got_tree_entry *te = NULL;
5209 if (!view->searching) {
5210 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5214 if (s->matched_entry) {
5215 if (view->searching == TOG_SEARCH_FORWARD) {
5216 if (s->selected_entry)
5217 te = got_tree_entry_get_next(s->tree,
5220 te = got_object_tree_get_first_entry(s->tree);
5222 if (s->selected_entry == NULL)
5223 te = got_object_tree_get_last_entry(s->tree);
5225 te = got_tree_entry_get_prev(s->tree,
5229 if (view->searching == TOG_SEARCH_FORWARD)
5230 te = got_object_tree_get_first_entry(s->tree);
5232 te = got_object_tree_get_last_entry(s->tree);
5237 if (s->matched_entry == NULL) {
5238 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5241 if (view->searching == TOG_SEARCH_FORWARD)
5242 te = got_object_tree_get_first_entry(s->tree);
5244 te = got_object_tree_get_last_entry(s->tree);
5247 if (match_tree_entry(te, &view->regex)) {
5248 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5249 s->matched_entry = te;
5253 if (view->searching == TOG_SEARCH_FORWARD)
5254 te = got_tree_entry_get_next(s->tree, te);
5256 te = got_tree_entry_get_prev(s->tree, te);
5259 if (s->matched_entry) {
5260 s->first_displayed_entry = s->matched_entry;
5267 static const struct got_error *
5268 show_tree_view(struct tog_view *view)
5270 const struct got_error *err = NULL;
5271 struct tog_tree_view_state *s = &view->state.tree;
5274 err = tree_entry_path(&parent_path, &s->parents, NULL);
5278 err = draw_tree_entries(view, parent_path);
5285 static const struct got_error *
5286 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5287 struct tog_view **focus_view, struct tog_view *view, int ch)
5289 const struct got_error *err = NULL;
5290 struct tog_tree_view_state *s = &view->state.tree;
5291 struct tog_view *log_view, *ref_view;
5296 s->show_ids = !s->show_ids;
5299 if (!s->selected_entry)
5301 if (view_is_parent_view(view))
5302 begin_x = view_split_begin_x(view->begin_x);
5303 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5304 &s->parents, s->commit_id, s->repo);
5305 if (view_is_parent_view(view)) {
5306 err = view_close_child(view);
5309 err = view_set_child(view, log_view);
5311 view_close(log_view);
5314 *focus_view = log_view;
5316 *new_view = log_view;
5319 if (view_is_parent_view(view))
5320 begin_x = view_split_begin_x(view->begin_x);
5321 ref_view = view_open(view->nlines, view->ncols,
5322 view->begin_y, begin_x, TOG_VIEW_REF);
5323 if (ref_view == NULL)
5324 return got_error_from_errno("view_open");
5325 err = open_ref_view(ref_view, s->repo);
5327 view_close(ref_view);
5330 if (view_is_parent_view(view)) {
5331 err = view_close_child(view);
5334 err = view_set_child(view, ref_view);
5336 view_close(ref_view);
5339 *focus_view = ref_view;
5341 *new_view = ref_view;
5345 if (s->selected > 0) {
5349 tree_scroll_up(s, 1);
5353 if (s->tree == s->root) {
5354 if (got_object_tree_get_first_entry(s->tree) ==
5355 s->first_displayed_entry)
5358 if (s->first_displayed_entry == NULL)
5361 tree_scroll_up(s, MAX(0, view->nlines - 3));
5365 if (s->selected < s->ndisplayed - 1) {
5369 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5371 /* can't scroll any further */
5373 tree_scroll_down(s, 1);
5377 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5379 /* can't scroll any further; move cursor down */
5380 if (s->selected < s->ndisplayed - 1)
5381 s->selected = s->ndisplayed - 1;
5384 tree_scroll_down(s, view->nlines - 3);
5389 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5390 struct tog_parent_tree *parent;
5391 /* user selected '..' */
5392 if (s->tree == s->root)
5394 parent = TAILQ_FIRST(&s->parents);
5395 TAILQ_REMOVE(&s->parents, parent,
5397 got_object_tree_close(s->tree);
5398 s->tree = parent->tree;
5399 s->first_displayed_entry =
5400 parent->first_displayed_entry;
5402 parent->selected_entry;
5403 s->selected = parent->selected;
5405 } else if (S_ISDIR(got_tree_entry_get_mode(
5406 s->selected_entry))) {
5407 struct got_tree_object *subtree;
5408 err = got_object_open_as_tree(&subtree, s->repo,
5409 got_tree_entry_get_id(s->selected_entry));
5412 err = tree_view_visit_subtree(s, subtree);
5414 got_object_tree_close(subtree);
5417 } else if (S_ISREG(got_tree_entry_get_mode(
5418 s->selected_entry))) {
5419 struct tog_view *blame_view;
5420 int begin_x = view_is_parent_view(view) ?
5421 view_split_begin_x(view->begin_x) : 0;
5423 err = blame_tree_entry(&blame_view, begin_x,
5424 s->selected_entry, &s->parents,
5425 s->commit_id, s->repo);
5428 if (view_is_parent_view(view)) {
5429 err = view_close_child(view);
5432 err = view_set_child(view, blame_view);
5434 view_close(blame_view);
5437 *focus_view = blame_view;
5439 *new_view = blame_view;
5443 if (s->selected > view->nlines)
5444 s->selected = s->ndisplayed - 1;
5457 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5462 static const struct got_error *
5463 cmd_tree(int argc, char *argv[])
5465 const struct got_error *error;
5466 struct got_repository *repo = NULL;
5467 struct got_worktree *worktree = NULL;
5468 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5469 struct got_object_id *commit_id = NULL;
5470 char *commit_id_arg = NULL;
5471 struct got_commit_object *commit = NULL;
5472 struct got_tree_object *tree = NULL;
5474 struct tog_view *view;
5477 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5482 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5485 commit_id_arg = optarg;
5488 repo_path = realpath(optarg, NULL);
5489 if (repo_path == NULL)
5490 return got_error_from_errno2("realpath",
5505 cwd = getcwd(NULL, 0);
5507 return got_error_from_errno("getcwd");
5509 error = got_worktree_open(&worktree, cwd);
5510 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5513 if (repo_path == NULL) {
5516 strdup(got_worktree_get_repo_path(worktree));
5518 repo_path = strdup(cwd);
5520 if (repo_path == NULL) {
5521 error = got_error_from_errno("strdup");
5525 error = got_repo_open(&repo, repo_path, NULL);
5529 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5536 error = apply_unveil(got_repo_get_path(repo), NULL);
5540 error = got_repo_match_object_id(&commit_id, NULL,
5541 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5542 GOT_OBJ_TYPE_COMMIT, 1, repo);
5546 error = got_object_open_as_commit(&commit, repo, commit_id);
5550 error = got_object_open_as_tree(&tree, repo,
5551 got_object_commit_get_tree_id(commit));
5555 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5557 error = got_error_from_errno("view_open");
5560 error = open_tree_view(view, tree, commit_id, repo);
5563 if (!got_path_is_root_dir(in_repo_path)) {
5564 error = tree_view_walk_path(&view->state.tree, commit_id,
5571 /* Release work tree lock. */
5572 got_worktree_close(worktree);
5575 error = view_loop(view);
5581 got_object_commit_close(commit);
5583 got_object_tree_close(tree);
5585 got_repo_close(repo);
5589 static const struct got_error *
5590 ref_view_load_refs(struct tog_ref_view_state *s)
5592 const struct got_error *err;
5593 struct got_reflist_entry *sre;
5594 struct tog_reflist_entry *re;
5596 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5597 got_ref_cmp_by_name, NULL);
5602 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5603 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5606 re = malloc(sizeof(*re));
5608 return got_error_from_errno("malloc");
5611 re->idx = s->nrefs++;
5612 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5619 ref_view_free_refs(struct tog_ref_view_state *s)
5621 struct tog_reflist_entry *re;
5623 while (!TAILQ_EMPTY(&s->refs)) {
5624 re = TAILQ_FIRST(&s->refs);
5625 TAILQ_REMOVE(&s->refs, re, entry);
5628 got_ref_list_free(&s->simplerefs);
5631 static const struct got_error *
5632 open_ref_view(struct tog_view *view, struct got_repository *repo)
5634 const struct got_error *err = NULL;
5635 struct tog_ref_view_state *s = &view->state.ref;
5637 s->selected_entry = 0;
5640 SIMPLEQ_INIT(&s->simplerefs);
5641 TAILQ_INIT(&s->refs);
5642 SIMPLEQ_INIT(&s->colors);
5644 err = ref_view_load_refs(s);
5648 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5650 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5651 err = add_color(&s->colors, "^refs/heads/",
5652 TOG_COLOR_REFS_HEADS,
5653 get_color_value("TOG_COLOR_REFS_HEADS"));
5657 err = add_color(&s->colors, "^refs/tags/",
5658 TOG_COLOR_REFS_TAGS,
5659 get_color_value("TOG_COLOR_REFS_TAGS"));
5663 err = add_color(&s->colors, "^refs/remotes/",
5664 TOG_COLOR_REFS_REMOTES,
5665 get_color_value("TOG_COLOR_REFS_REMOTES"));
5670 view->show = show_ref_view;
5671 view->input = input_ref_view;
5672 view->close = close_ref_view;
5673 view->search_start = search_start_ref_view;
5674 view->search_next = search_next_ref_view;
5677 free_colors(&s->colors);
5681 static const struct got_error *
5682 close_ref_view(struct tog_view *view)
5684 struct tog_ref_view_state *s = &view->state.ref;
5686 ref_view_free_refs(s);
5687 free_colors(&s->colors);
5692 static const struct got_error *
5693 resolve_reflist_entry(struct got_object_id **commit_id,
5694 struct tog_reflist_entry *re, struct got_repository *repo)
5696 const struct got_error *err = NULL;
5697 struct got_object_id *obj_id;
5698 struct got_tag_object *tag = NULL;
5703 err = got_ref_resolve(&obj_id, repo, re->ref);
5707 err = got_object_get_type(&obj_type, repo, obj_id);
5712 case GOT_OBJ_TYPE_COMMIT:
5713 *commit_id = obj_id;
5715 case GOT_OBJ_TYPE_TAG:
5716 err = got_object_open_as_tag(&tag, repo, obj_id);
5720 err = got_object_get_type(&obj_type, repo,
5721 got_object_tag_get_object_id(tag));
5724 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5725 err = got_error(GOT_ERR_OBJ_TYPE);
5728 *commit_id = got_object_id_dup(
5729 got_object_tag_get_object_id(tag));
5730 if (*commit_id == NULL) {
5731 err = got_error_from_errno("got_object_id_dup");
5736 err = got_error(GOT_ERR_OBJ_TYPE);
5742 got_object_tag_close(tag);
5750 static const struct got_error *
5751 log_ref_entry(struct tog_view **new_view, int begin_x,
5752 struct tog_reflist_entry *re, struct got_repository *repo)
5754 struct tog_view *log_view;
5755 const struct got_error *err = NULL;
5756 struct got_object_id *commit_id = NULL;
5760 err = resolve_reflist_entry(&commit_id, re, repo);
5762 if (err->code != GOT_ERR_OBJ_TYPE)
5768 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5769 if (log_view == NULL) {
5770 err = got_error_from_errno("view_open");
5774 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5777 view_close(log_view);
5779 *new_view = log_view;
5785 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5787 struct tog_reflist_entry *re;
5790 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5793 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5794 while (i++ < maxscroll) {
5797 s->first_displayed_entry = re;
5798 re = TAILQ_PREV(re, tog_reflist_head, entry);
5803 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5805 struct tog_reflist_entry *next, *last;
5808 if (s->first_displayed_entry)
5809 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5811 next = TAILQ_FIRST(&s->refs);
5813 last = s->last_displayed_entry;
5814 while (next && last && n++ < maxscroll) {
5815 last = TAILQ_NEXT(last, entry);
5817 s->first_displayed_entry = next;
5818 next = TAILQ_NEXT(next, entry);
5823 static const struct got_error *
5824 search_start_ref_view(struct tog_view *view)
5826 struct tog_ref_view_state *s = &view->state.ref;
5828 s->matched_entry = NULL;
5833 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5835 regmatch_t regmatch;
5837 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5841 static const struct got_error *
5842 search_next_ref_view(struct tog_view *view)
5844 struct tog_ref_view_state *s = &view->state.ref;
5845 struct tog_reflist_entry *re = NULL;
5847 if (!view->searching) {
5848 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5852 if (s->matched_entry) {
5853 if (view->searching == TOG_SEARCH_FORWARD) {
5854 if (s->selected_entry)
5855 re = TAILQ_NEXT(s->selected_entry, entry);
5857 re = TAILQ_PREV(s->selected_entry,
5858 tog_reflist_head, entry);
5860 if (s->selected_entry == NULL)
5861 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5863 re = TAILQ_PREV(s->selected_entry,
5864 tog_reflist_head, entry);
5867 if (view->searching == TOG_SEARCH_FORWARD)
5868 re = TAILQ_FIRST(&s->refs);
5870 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5875 if (s->matched_entry == NULL) {
5876 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5879 if (view->searching == TOG_SEARCH_FORWARD)
5880 re = TAILQ_FIRST(&s->refs);
5882 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5885 if (match_reflist_entry(re, &view->regex)) {
5886 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5887 s->matched_entry = re;
5891 if (view->searching == TOG_SEARCH_FORWARD)
5892 re = TAILQ_NEXT(re, entry);
5894 re = TAILQ_PREV(re, tog_reflist_head, entry);
5897 if (s->matched_entry) {
5898 s->first_displayed_entry = s->matched_entry;
5905 static const struct got_error *
5906 show_ref_view(struct tog_view *view)
5908 const struct got_error *err = NULL;
5909 struct tog_ref_view_state *s = &view->state.ref;
5910 struct tog_reflist_entry *re;
5913 struct tog_color *tc;
5915 int limit = view->nlines;
5917 werase(view->window);
5924 re = s->first_displayed_entry;
5926 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5928 return got_error_from_errno("asprintf");
5930 err = format_line(&wline, &width, line, view->ncols, 0);
5935 if (view_needs_focus_indication(view))
5936 wstandout(view->window);
5937 waddwstr(view->window, wline);
5938 if (view_needs_focus_indication(view))
5939 wstandend(view->window);
5944 if (width < view->ncols - 1)
5945 waddch(view->window, '\n');
5950 while (re && limit > 0) {
5953 if (got_ref_is_symbolic(re->ref)) {
5954 if (asprintf(&line, "%s -> %s",
5955 got_ref_get_name(re->ref),
5956 got_ref_get_symref_target(re->ref)) == -1)
5957 return got_error_from_errno("asprintf");
5958 } else if (s->show_ids) {
5959 struct got_object_id *id;
5961 err = got_ref_resolve(&id, s->repo, re->ref);
5964 err = got_object_id_str(&id_str, id);
5969 if (asprintf(&line, "%s: %s",
5970 got_ref_get_name(re->ref), id_str) == -1) {
5971 err = got_error_from_errno("asprintf");
5979 line = strdup(got_ref_get_name(re->ref));
5981 return got_error_from_errno("strdup");
5984 err = format_line(&wline, &width, line, view->ncols, 0);
5989 if (n == s->selected) {
5991 wstandout(view->window);
5992 s->selected_entry = re;
5994 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5996 wattr_on(view->window,
5997 COLOR_PAIR(tc->colorpair), NULL);
5998 waddwstr(view->window, wline);
6000 wattr_off(view->window,
6001 COLOR_PAIR(tc->colorpair), NULL);
6002 if (width < view->ncols - 1)
6003 waddch(view->window, '\n');
6004 if (n == s->selected && view->focussed)
6005 wstandend(view->window);
6011 s->last_displayed_entry = re;
6014 re = TAILQ_NEXT(re, entry);
6021 static const struct got_error *
6022 browse_ref_tree(struct tog_view **new_view, int begin_x,
6023 struct tog_reflist_entry *re, struct got_repository *repo)
6025 const struct got_error *err = NULL;
6026 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6027 struct got_tree_object *tree = NULL;
6028 struct tog_view *tree_view;
6032 err = resolve_reflist_entry(&commit_id, re, repo);
6034 if (err->code != GOT_ERR_OBJ_TYPE)
6040 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6044 err = got_object_open_as_tree(&tree, repo, tree_id);
6048 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6049 if (tree_view == NULL) {
6050 err = got_error_from_errno("view_open");
6054 err = open_tree_view(tree_view, tree, commit_id, repo);
6058 *new_view = tree_view;
6064 got_object_tree_close(tree);
6068 static const struct got_error *
6069 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6070 struct tog_view **focus_view, struct tog_view *view, int ch)
6072 const struct got_error *err = NULL;
6073 struct tog_ref_view_state *s = &view->state.ref;
6074 struct tog_view *log_view, *tree_view;
6079 s->show_ids = !s->show_ids;
6083 if (!s->selected_entry)
6085 if (view_is_parent_view(view))
6086 begin_x = view_split_begin_x(view->begin_x);
6087 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6089 if (view_is_parent_view(view)) {
6090 err = view_close_child(view);
6093 err = view_set_child(view, log_view);
6095 view_close(log_view);
6098 *focus_view = log_view;
6100 *new_view = log_view;
6103 if (!s->selected_entry)
6105 if (view_is_parent_view(view))
6106 begin_x = view_split_begin_x(view->begin_x);
6107 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6109 if (err || tree_view == NULL)
6111 if (view_is_parent_view(view)) {
6112 err = view_close_child(view);
6115 err = view_set_child(view, tree_view);
6117 view_close(tree_view);
6120 *focus_view = tree_view;
6122 *new_view = tree_view;
6126 if (s->selected > 0) {
6130 ref_scroll_up(s, 1);
6134 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6136 ref_scroll_up(s, MAX(0, view->nlines - 1));
6140 if (s->selected < s->ndisplayed - 1) {
6144 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6145 /* can't scroll any further */
6147 ref_scroll_down(s, 1);
6151 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6152 /* can't scroll any further; move cursor down */
6153 if (s->selected < s->ndisplayed - 1)
6154 s->selected = s->ndisplayed - 1;
6157 ref_scroll_down(s, view->nlines - 1);
6160 ref_view_free_refs(s);
6161 err = ref_view_load_refs(s);
6164 if (s->selected > view->nlines)
6165 s->selected = s->ndisplayed - 1;
6178 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6183 static const struct got_error *
6184 cmd_ref(int argc, char *argv[])
6186 const struct got_error *error;
6187 struct got_repository *repo = NULL;
6188 struct got_worktree *worktree = NULL;
6189 char *cwd = NULL, *repo_path = NULL;
6191 struct tog_view *view;
6194 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6199 while ((ch = getopt(argc, argv, "r:")) != -1) {
6202 repo_path = realpath(optarg, NULL);
6203 if (repo_path == NULL)
6204 return got_error_from_errno2("realpath",
6219 cwd = getcwd(NULL, 0);
6221 return got_error_from_errno("getcwd");
6223 error = got_worktree_open(&worktree, cwd);
6224 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6227 if (repo_path == NULL) {
6230 strdup(got_worktree_get_repo_path(worktree));
6232 repo_path = strdup(cwd);
6234 if (repo_path == NULL) {
6235 error = got_error_from_errno("strdup");
6239 error = got_repo_open(&repo, repo_path, NULL);
6245 error = apply_unveil(got_repo_get_path(repo), NULL);
6249 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6251 error = got_error_from_errno("view_open");
6255 error = open_ref_view(view, repo);
6260 /* Release work tree lock. */
6261 got_worktree_close(worktree);
6264 error = view_loop(view);
6269 got_repo_close(repo);
6274 list_commands(FILE *fp)
6278 fprintf(fp, "commands:");
6279 for (i = 0; i < nitems(tog_commands); i++) {
6280 struct tog_cmd *cmd = &tog_commands[i];
6281 fprintf(fp, " %s", cmd->name);
6287 usage(int hflag, int status)
6289 FILE *fp = (status == 0) ? stdout : stderr;
6291 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6294 fprintf(fp, "lazy usage: %s path\n", getprogname());
6301 make_argv(int argc, ...)
6309 argv = calloc(argc, sizeof(char *));
6312 for (i = 0; i < argc; i++) {
6313 argv[i] = strdup(va_arg(ap, char *));
6314 if (argv[i] == NULL)
6323 * Try to convert 'tog path' into a 'tog log path' command.
6324 * The user could simply have mistyped the command rather than knowingly
6325 * provided a path. So check whether argv[0] can in fact be resolved
6326 * to a path in the HEAD commit and print a special error if not.
6327 * This hack is for mpi@ <3
6329 static const struct got_error *
6330 tog_log_with_path(int argc, char *argv[])
6332 const struct got_error *error = NULL;
6333 struct tog_cmd *cmd = NULL;
6334 struct got_repository *repo = NULL;
6335 struct got_worktree *worktree = NULL;
6336 struct got_object_id *commit_id = NULL, *id = NULL;
6337 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6338 char *commit_id_str = NULL, **cmd_argv = NULL;
6340 cwd = getcwd(NULL, 0);
6342 return got_error_from_errno("getcwd");
6344 error = got_worktree_open(&worktree, cwd);
6345 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6349 repo_path = strdup(got_worktree_get_repo_path(worktree));
6351 repo_path = strdup(cwd);
6352 if (repo_path == NULL) {
6353 error = got_error_from_errno("strdup");
6357 error = got_repo_open(&repo, repo_path, NULL);
6361 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6366 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6367 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6368 GOT_OBJ_TYPE_COMMIT, 1, repo);
6373 got_worktree_close(worktree);
6377 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6379 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6381 fprintf(stderr, "%s: '%s' is no known command or path\n",
6382 getprogname(), argv[0]);
6387 got_repo_close(repo);
6390 error = got_object_id_str(&commit_id_str, commit_id);
6394 cmd = &tog_commands[0]; /* log */
6396 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6397 error = cmd->cmd_main(argc, cmd_argv);
6400 got_repo_close(repo);
6402 got_worktree_close(worktree);
6404 free(commit_id_str);
6411 for (i = 0; i < argc; i++)
6419 main(int argc, char *argv[])
6421 const struct got_error *error = NULL;
6422 struct tog_cmd *cmd = NULL;
6423 int ch, hflag = 0, Vflag = 0;
6424 char **cmd_argv = NULL;
6425 static struct option longopts[] = {
6426 { "version", no_argument, NULL, 'V' },
6430 setlocale(LC_CTYPE, "");
6432 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6452 got_version_print_str();
6459 /* Build an argument vector which runs a default command. */
6460 cmd = &tog_commands[0];
6462 cmd_argv = make_argv(argc, cmd->name);
6466 /* Did the user specify a command? */
6467 for (i = 0; i < nitems(tog_commands); i++) {
6468 if (strncmp(tog_commands[i].name, argv[0],
6469 strlen(argv[0])) == 0) {
6470 cmd = &tog_commands[i];
6479 /* No command specified; try log with a path */
6480 error = tog_log_with_path(argc, argv);
6485 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6492 for (i = 0; i < argc; i++)
6497 if (error && error->code != GOT_ERR_CANCELLED)
6498 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);