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;
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;
400 struct got_repository *repo;
401 struct got_tree_entry *matched_entry;
402 struct tog_colors colors;
405 struct tog_reflist_entry {
406 TAILQ_ENTRY(tog_reflist_entry) entry;
407 struct got_reference *ref;
411 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
413 struct tog_ref_view_state {
414 struct got_reflist_head simplerefs; /* SIMPLEQ */
415 struct tog_reflist_head refs; /* TAILQ */
416 struct tog_reflist_entry *first_displayed_entry;
417 struct tog_reflist_entry *last_displayed_entry;
418 struct tog_reflist_entry *selected_entry;
419 int nrefs, ndisplayed, selected, show_ids;
420 struct got_repository *repo;
421 struct tog_reflist_entry *matched_entry;
422 struct tog_colors colors;
426 * We implement two types of views: parent views and child views.
428 * The 'Tab' key switches focus between a parent view and its child view.
429 * Child views are shown side-by-side to their parent view, provided
430 * there is enough screen estate.
432 * When a new view is opened from within a parent view, this new view
433 * becomes a child view of the parent view, replacing any existing child.
435 * When a new view is opened from within a child view, this new view
436 * becomes a parent view which will obscure the views below until the
437 * user quits the new parent view by typing 'q'.
439 * This list of views contains parent views only.
440 * Child views are only pointed to by their parent view.
442 TAILQ_HEAD(tog_view_list_head, tog_view);
445 TAILQ_ENTRY(tog_view) entry;
448 int nlines, ncols, begin_y, begin_x;
449 int lines, cols; /* copies of LINES and COLS */
450 int focussed; /* Only set on one parent or child view at a time. */
452 struct tog_view *parent;
453 struct tog_view *child;
456 * This flag is initially set on parent views when a new child view
457 * is created. It gets toggled when the 'Tab' key switches focus
458 * between parent and child.
459 * The flag indicates whether focus should be passed on to our child
460 * view if this parent view gets picked for focus after another parent
461 * view was closed. This prevents child views from losing focus in such
466 /* type-specific state */
467 enum tog_view_type type;
469 struct tog_diff_view_state diff;
470 struct tog_log_view_state log;
471 struct tog_blame_view_state blame;
472 struct tog_tree_view_state tree;
473 struct tog_ref_view_state ref;
476 const struct got_error *(*show)(struct tog_view *);
477 const struct got_error *(*input)(struct tog_view **,
478 struct tog_view *, int);
479 const struct got_error *(*close)(struct tog_view *);
481 const struct got_error *(*search_start)(struct tog_view *);
482 const struct got_error *(*search_next)(struct tog_view *);
484 #define TOG_SEARCH_FORWARD 1
485 #define TOG_SEARCH_BACKWARD 2
486 int search_next_done;
487 #define TOG_SEARCH_HAVE_MORE 1
488 #define TOG_SEARCH_NO_MORE 2
489 #define TOG_SEARCH_HAVE_NONE 3
494 static const struct got_error *open_diff_view(struct tog_view *,
495 struct got_object_id *, struct got_object_id *,
496 const char *, const char *, int, int, int, struct tog_view *,
497 struct got_repository *);
498 static const struct got_error *show_diff_view(struct tog_view *);
499 static const struct got_error *input_diff_view(struct tog_view **,
500 struct tog_view *, int);
501 static const struct got_error* close_diff_view(struct tog_view *);
502 static const struct got_error *search_start_diff_view(struct tog_view *);
503 static const struct got_error *search_next_diff_view(struct tog_view *);
505 static const struct got_error *open_log_view(struct tog_view *,
506 struct got_object_id *, struct got_repository *,
507 const char *, const char *, int);
508 static const struct got_error * show_log_view(struct tog_view *);
509 static const struct got_error *input_log_view(struct tog_view **,
510 struct tog_view *, int);
511 static const struct got_error *close_log_view(struct tog_view *);
512 static const struct got_error *search_start_log_view(struct tog_view *);
513 static const struct got_error *search_next_log_view(struct tog_view *);
515 static const struct got_error *open_blame_view(struct tog_view *, char *,
516 struct got_object_id *, struct got_repository *);
517 static const struct got_error *show_blame_view(struct tog_view *);
518 static const struct got_error *input_blame_view(struct tog_view **,
519 struct tog_view *, int);
520 static const struct got_error *close_blame_view(struct tog_view *);
521 static const struct got_error *search_start_blame_view(struct tog_view *);
522 static const struct got_error *search_next_blame_view(struct tog_view *);
524 static const struct got_error *open_tree_view(struct tog_view *,
525 struct got_tree_object *, struct got_object_id *, const char *,
526 struct got_repository *);
527 static const struct got_error *show_tree_view(struct tog_view *);
528 static const struct got_error *input_tree_view(struct tog_view **,
529 struct tog_view *, int);
530 static const struct got_error *close_tree_view(struct tog_view *);
531 static const struct got_error *search_start_tree_view(struct tog_view *);
532 static const struct got_error *search_next_tree_view(struct tog_view *);
534 static const struct got_error *open_ref_view(struct tog_view *,
535 struct got_repository *);
536 static const struct got_error *show_ref_view(struct tog_view *);
537 static const struct got_error *input_ref_view(struct tog_view **,
538 struct tog_view *, int);
539 static const struct got_error *close_ref_view(struct tog_view *);
540 static const struct got_error *search_start_ref_view(struct tog_view *);
541 static const struct got_error *search_next_ref_view(struct tog_view *);
543 static volatile sig_atomic_t tog_sigwinch_received;
544 static volatile sig_atomic_t tog_sigpipe_received;
545 static volatile sig_atomic_t tog_sigcont_received;
548 tog_sigwinch(int signo)
550 tog_sigwinch_received = 1;
554 tog_sigpipe(int signo)
556 tog_sigpipe_received = 1;
560 tog_sigcont(int signo)
562 tog_sigcont_received = 1;
565 static const struct got_error *
566 view_close(struct tog_view *view)
568 const struct got_error *err = NULL;
571 view_close(view->child);
575 err = view->close(view);
577 del_panel(view->panel);
579 delwin(view->window);
584 static struct tog_view *
585 view_open(int nlines, int ncols, int begin_y, int begin_x,
586 enum tog_view_type type)
588 struct tog_view *view = calloc(1, sizeof(*view));
596 view->nlines = nlines ? nlines : LINES - begin_y;
597 view->ncols = ncols ? ncols : COLS - begin_x;
598 view->begin_y = begin_y;
599 view->begin_x = begin_x;
600 view->window = newwin(nlines, ncols, begin_y, begin_x);
601 if (view->window == NULL) {
605 view->panel = new_panel(view->window);
606 if (view->panel == NULL ||
607 set_panel_userptr(view->panel, view) != OK) {
612 keypad(view->window, TRUE);
617 view_split_begin_x(int begin_x)
619 if (begin_x > 0 || COLS < 120)
621 return (COLS - MAX(COLS / 2, 80));
624 static const struct got_error *view_resize(struct tog_view *);
626 static const struct got_error *
627 view_splitscreen(struct tog_view *view)
629 const struct got_error *err = NULL;
632 view->begin_x = view_split_begin_x(0);
633 view->nlines = LINES;
634 view->ncols = COLS - view->begin_x;
637 err = view_resize(view);
641 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
642 return got_error_from_errno("mvwin");
647 static const struct got_error *
648 view_fullscreen(struct tog_view *view)
650 const struct got_error *err = NULL;
654 view->nlines = LINES;
658 err = view_resize(view);
662 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
663 return got_error_from_errno("mvwin");
669 view_is_parent_view(struct tog_view *view)
671 return view->parent == NULL;
674 static const struct got_error *
675 view_resize(struct tog_view *view)
679 if (view->lines > LINES)
680 nlines = view->nlines - (view->lines - LINES);
682 nlines = view->nlines + (LINES - view->lines);
684 if (view->cols > COLS)
685 ncols = view->ncols - (view->cols - COLS);
687 ncols = view->ncols + (COLS - view->cols);
689 if (wresize(view->window, nlines, ncols) == ERR)
690 return got_error_from_errno("wresize");
691 if (replace_panel(view->panel, view->window) == ERR)
692 return got_error_from_errno("replace_panel");
693 wclear(view->window);
695 view->nlines = nlines;
701 view->child->begin_x = view_split_begin_x(view->begin_x);
702 if (view->child->begin_x == 0) {
703 view_fullscreen(view->child);
704 if (view->child->focussed)
705 show_panel(view->child->panel);
707 show_panel(view->panel);
709 view_splitscreen(view->child);
710 show_panel(view->child->panel);
717 static const struct got_error *
718 view_close_child(struct tog_view *view)
720 const struct got_error *err = NULL;
722 if (view->child == NULL)
725 err = view_close(view->child);
731 view_set_child(struct tog_view *view, struct tog_view *child)
734 child->parent = view;
738 view_is_splitscreen(struct tog_view *view)
740 return view->begin_x > 0;
749 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
750 cols = 80; /* Default */
756 resize_term(lines, cols);
759 static const struct got_error *
760 view_search_start(struct tog_view *view)
762 const struct got_error *err = NULL;
766 if (view->nlines < 1)
769 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
770 wclrtoeol(view->window);
774 ret = wgetnstr(view->window, pattern, sizeof(pattern));
780 if (view->searching) {
781 regfree(&view->regex);
785 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
786 err = view->search_start(view);
788 regfree(&view->regex);
791 view->searching = TOG_SEARCH_FORWARD;
792 view->search_next_done = 0;
793 view->search_next(view);
799 static const struct got_error *
800 view_input(struct tog_view **new, int *done, struct tog_view *view,
801 struct tog_view_list_head *views)
803 const struct got_error *err = NULL;
809 /* Clear "no matches" indicator. */
810 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
811 view->search_next_done == TOG_SEARCH_HAVE_NONE)
812 view->search_next_done = TOG_SEARCH_HAVE_MORE;
814 if (view->searching && !view->search_next_done) {
815 errcode = pthread_mutex_unlock(&tog_mutex);
817 return got_error_set_errno(errcode,
818 "pthread_mutex_unlock");
820 errcode = pthread_mutex_lock(&tog_mutex);
822 return got_error_set_errno(errcode,
823 "pthread_mutex_lock");
824 view->search_next(view);
828 nodelay(stdscr, FALSE);
829 /* Allow threads to make progress while we are waiting for input. */
830 errcode = pthread_mutex_unlock(&tog_mutex);
832 return got_error_set_errno(errcode, "pthread_mutex_unlock");
833 ch = wgetch(view->window);
834 errcode = pthread_mutex_lock(&tog_mutex);
836 return got_error_set_errno(errcode, "pthread_mutex_lock");
837 nodelay(stdscr, TRUE);
839 if (tog_sigwinch_received || tog_sigcont_received) {
841 tog_sigwinch_received = 0;
842 tog_sigcont_received = 0;
843 TAILQ_FOREACH(v, views, entry) {
844 err = view_resize(v);
847 err = v->input(new, v, KEY_RESIZE);
851 err = view_resize(v->child);
854 err = v->child->input(new, v->child,
868 view->child->focussed = 1;
869 view->focus_child = 1;
870 } else if (view->parent) {
872 view->parent->focussed = 1;
873 view->parent->focus_child = 0;
877 err = view->input(new, view, ch);
884 if (view_is_parent_view(view)) {
885 if (view->child == NULL)
887 if (view_is_splitscreen(view->child)) {
889 view->child->focussed = 1;
890 err = view_fullscreen(view->child);
892 err = view_splitscreen(view->child);
895 err = view->child->input(new, view->child,
898 if (view_is_splitscreen(view)) {
899 view->parent->focussed = 0;
901 err = view_fullscreen(view);
903 err = view_splitscreen(view);
907 err = view->input(new, view, KEY_RESIZE);
913 if (view->search_start)
914 view_search_start(view);
916 err = view->input(new, view, ch);
920 if (view->search_next) {
921 view->searching = (ch == 'n' ?
922 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
923 view->search_next_done = 0;
924 view->search_next(view);
926 err = view->input(new, view, ch);
929 err = view->input(new, view, ch);
937 view_vborder(struct tog_view *view)
940 struct tog_view *view_above;
943 return view_vborder(view->parent);
945 panel = panel_above(view->panel);
949 view_above = panel_userptr(panel);
950 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
951 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
955 view_needs_focus_indication(struct tog_view *view)
957 if (view_is_parent_view(view)) {
958 if (view->child == NULL || view->child->focussed)
960 if (!view_is_splitscreen(view->child))
962 } else if (!view_is_splitscreen(view))
965 return view->focussed;
968 static const struct got_error *
969 view_loop(struct tog_view *view)
971 const struct got_error *err = NULL;
972 struct tog_view_list_head views;
973 struct tog_view *new_view;
974 int fast_refresh = 10;
975 int done = 0, errcode;
977 errcode = pthread_mutex_lock(&tog_mutex);
979 return got_error_set_errno(errcode, "pthread_mutex_lock");
982 TAILQ_INSERT_HEAD(&views, view, entry);
985 err = view->show(view);
990 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
991 /* Refresh fast during initialization, then become slower. */
992 if (fast_refresh && fast_refresh-- == 0)
993 halfdelay(10); /* switch to once per second */
995 err = view_input(&new_view, &done, view, &views);
999 struct tog_view *v, *prev = NULL;
1001 if (view_is_parent_view(view))
1002 prev = TAILQ_PREV(view, tog_view_list_head,
1004 else if (view->parent)
1005 prev = view->parent;
1008 view->parent->child = NULL;
1009 view->parent->focus_child = 0;
1011 TAILQ_REMOVE(&views, view, entry);
1013 err = view_close(view);
1018 TAILQ_FOREACH(v, &views, entry) {
1022 if (view == NULL && new_view == NULL) {
1023 /* No view has focus. Try to pick one. */
1026 else if (!TAILQ_EMPTY(&views)) {
1027 view = TAILQ_LAST(&views,
1028 tog_view_list_head);
1031 if (view->focus_child) {
1032 view->child->focussed = 1;
1040 struct tog_view *v, *t;
1041 /* Only allow one parent view per type. */
1042 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1043 if (v->type != new_view->type)
1045 TAILQ_REMOVE(&views, v, entry);
1046 err = view_close(v);
1051 TAILQ_INSERT_TAIL(&views, new_view, entry);
1055 if (view_is_parent_view(view)) {
1056 if (view->child && view->child->focussed)
1059 if (view->parent && view->parent->focussed)
1060 view = view->parent;
1062 show_panel(view->panel);
1063 if (view->child && view_is_splitscreen(view->child))
1064 show_panel(view->child->panel);
1065 if (view->parent && view_is_splitscreen(view)) {
1066 err = view->parent->show(view->parent);
1070 err = view->show(view);
1074 err = view->child->show(view->child);
1083 while (!TAILQ_EMPTY(&views)) {
1084 view = TAILQ_FIRST(&views);
1085 TAILQ_REMOVE(&views, view, entry);
1089 errcode = pthread_mutex_unlock(&tog_mutex);
1090 if (errcode && err == NULL)
1091 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1101 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1106 /* Create newly allocated wide-character string equivalent to a byte string. */
1107 static const struct got_error *
1108 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1111 const struct got_error *err = NULL;
1114 *wlen = mbstowcs(NULL, s, 0);
1115 if (*wlen == (size_t)-1) {
1117 if (errno != EILSEQ)
1118 return got_error_from_errno("mbstowcs");
1120 /* byte string invalid in current encoding; try to "fix" it */
1121 err = got_mbsavis(&vis, &vislen, s);
1124 *wlen = mbstowcs(NULL, vis, 0);
1125 if (*wlen == (size_t)-1) {
1126 err = got_error_from_errno("mbstowcs"); /* give up */
1131 *ws = calloc(*wlen + 1, sizeof(**ws));
1133 err = got_error_from_errno("calloc");
1137 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1138 err = got_error_from_errno("mbstowcs");
1149 /* Format a line for display, ensuring that it won't overflow a width limit. */
1150 static const struct got_error *
1151 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1154 const struct got_error *err = NULL;
1156 wchar_t *wline = NULL;
1163 err = mbs2ws(&wline, &wlen, line);
1169 int width = wcwidth(wline[i]);
1176 if (width == 1 || width == 2) {
1177 if (cols + width > wlimit)
1181 } else if (width == -1) {
1182 if (wline[i] == L'\t') {
1184 ((cols + col_tab_align) % TABSIZE);
1185 if (cols + width > wlimit)
1191 err = got_error_from_errno("wcwidth");
1206 static const struct got_error*
1207 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1208 struct got_object_id *id, struct got_repository *repo)
1210 static const struct got_error *err = NULL;
1211 struct got_reflist_entry *re;
1217 SIMPLEQ_FOREACH(re, refs, entry) {
1218 struct got_tag_object *tag = NULL;
1219 struct got_object_id *ref_id;
1222 name = got_ref_get_name(re->ref);
1223 if (strcmp(name, GOT_REF_HEAD) == 0)
1225 if (strncmp(name, "refs/", 5) == 0)
1227 if (strncmp(name, "got/", 4) == 0)
1229 if (strncmp(name, "heads/", 6) == 0)
1231 if (strncmp(name, "remotes/", 8) == 0) {
1233 s = strstr(name, "/" GOT_REF_HEAD);
1234 if (s != NULL && s[strlen(s)] == '\0')
1237 err = got_ref_resolve(&ref_id, repo, re->ref);
1240 if (strncmp(name, "tags/", 5) == 0) {
1241 err = got_object_open_as_tag(&tag, repo, ref_id);
1243 if (err->code != GOT_ERR_OBJ_TYPE) {
1247 /* Ref points at something other than a tag. */
1252 cmp = got_object_id_cmp(tag ?
1253 got_object_tag_get_object_id(tag) : ref_id, id);
1256 got_object_tag_close(tag);
1260 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1261 s ? ", " : "", name) == -1) {
1262 err = got_error_from_errno("asprintf");
1273 static const struct got_error *
1274 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1277 char *smallerthan, *at;
1279 smallerthan = strchr(author, '<');
1280 if (smallerthan && smallerthan[1] != '\0')
1281 author = smallerthan + 1;
1282 at = strchr(author, '@');
1285 return format_line(wauthor, author_width, author, limit, col_tab_align);
1288 static const struct got_error *
1289 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1290 struct got_object_id *id, const size_t date_display_cols,
1291 int author_display_cols)
1293 struct tog_log_view_state *s = &view->state.log;
1294 const struct got_error *err = NULL;
1295 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1296 char *logmsg0 = NULL, *logmsg = NULL;
1297 char *author = NULL;
1298 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1299 int author_width, logmsg_width;
1300 char *newline, *line = NULL;
1302 const int avail = view->ncols;
1304 time_t committer_time;
1305 struct tog_color *tc;
1307 committer_time = got_object_commit_get_committer_time(commit);
1308 if (localtime_r(&committer_time, &tm) == NULL)
1309 return got_error_from_errno("localtime_r");
1310 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1312 return got_error(GOT_ERR_NO_SPACE);
1314 if (avail <= date_display_cols)
1315 limit = MIN(sizeof(datebuf) - 1, avail);
1317 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1318 tc = get_color(&s->colors, TOG_COLOR_DATE);
1320 wattr_on(view->window,
1321 COLOR_PAIR(tc->colorpair), NULL);
1322 waddnstr(view->window, datebuf, limit);
1324 wattr_off(view->window,
1325 COLOR_PAIR(tc->colorpair), NULL);
1332 err = got_object_id_str(&id_str, id);
1335 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1337 wattr_on(view->window,
1338 COLOR_PAIR(tc->colorpair), NULL);
1339 wprintw(view->window, "%.8s ", id_str);
1341 wattr_off(view->window,
1342 COLOR_PAIR(tc->colorpair), NULL);
1349 author = strdup(got_object_commit_get_author(commit));
1350 if (author == NULL) {
1351 err = got_error_from_errno("strdup");
1354 err = format_author(&wauthor, &author_width, author, avail - col, col);
1357 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1359 wattr_on(view->window,
1360 COLOR_PAIR(tc->colorpair), NULL);
1361 waddwstr(view->window, wauthor);
1363 wattr_off(view->window,
1364 COLOR_PAIR(tc->colorpair), NULL);
1365 col += author_width;
1366 while (col < avail && author_width < author_display_cols + 2) {
1367 waddch(view->window, ' ');
1374 err = got_object_commit_get_logmsg(&logmsg0, commit);
1378 while (*logmsg == '\n')
1380 newline = strchr(logmsg, '\n');
1383 limit = avail - col;
1384 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1387 waddwstr(view->window, wlogmsg);
1388 col += logmsg_width;
1389 while (col < avail) {
1390 waddch(view->window, ' ');
1402 static struct commit_queue_entry *
1403 alloc_commit_queue_entry(struct got_commit_object *commit,
1404 struct got_object_id *id)
1406 struct commit_queue_entry *entry;
1408 entry = calloc(1, sizeof(*entry));
1413 entry->commit = commit;
1418 pop_commit(struct commit_queue *commits)
1420 struct commit_queue_entry *entry;
1422 entry = TAILQ_FIRST(&commits->head);
1423 TAILQ_REMOVE(&commits->head, entry, entry);
1424 got_object_commit_close(entry->commit);
1425 commits->ncommits--;
1426 /* Don't free entry->id! It is owned by the commit graph. */
1431 free_commits(struct commit_queue *commits)
1433 while (!TAILQ_EMPTY(&commits->head))
1434 pop_commit(commits);
1437 static const struct got_error *
1438 match_commit(int *have_match, struct got_object_id *id,
1439 struct got_commit_object *commit, regex_t *regex)
1441 const struct got_error *err = NULL;
1442 regmatch_t regmatch;
1443 char *id_str = NULL, *logmsg = NULL;
1447 err = got_object_id_str(&id_str, id);
1451 err = got_object_commit_get_logmsg(&logmsg, commit);
1455 if (regexec(regex, got_object_commit_get_author(commit), 1,
1456 ®match, 0) == 0 ||
1457 regexec(regex, got_object_commit_get_committer(commit), 1,
1458 ®match, 0) == 0 ||
1459 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1460 regexec(regex, logmsg, 1, ®match, 0) == 0)
1468 static const struct got_error *
1469 queue_commits(struct tog_log_thread_args *a)
1471 const struct got_error *err = NULL;
1474 * We keep all commits open throughout the lifetime of the log
1475 * view in order to avoid having to re-fetch commits from disk
1476 * while updating the display.
1479 struct got_object_id *id;
1480 struct got_commit_object *commit;
1481 struct commit_queue_entry *entry;
1484 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1486 if (err || id == NULL)
1489 err = got_object_open_as_commit(&commit, a->repo, id);
1492 entry = alloc_commit_queue_entry(commit, id);
1493 if (entry == NULL) {
1494 err = got_error_from_errno("alloc_commit_queue_entry");
1498 errcode = pthread_mutex_lock(&tog_mutex);
1500 err = got_error_set_errno(errcode,
1501 "pthread_mutex_lock");
1505 entry->idx = a->commits->ncommits;
1506 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1507 a->commits->ncommits++;
1509 if (*a->searching == TOG_SEARCH_FORWARD &&
1510 !*a->search_next_done) {
1512 err = match_commit(&have_match, id, commit, a->regex);
1516 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1519 errcode = pthread_mutex_unlock(&tog_mutex);
1520 if (errcode && err == NULL)
1521 err = got_error_set_errno(errcode,
1522 "pthread_mutex_unlock");
1525 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1531 select_commit(struct tog_log_view_state *s)
1533 struct commit_queue_entry *entry;
1536 entry = s->first_displayed_entry;
1538 if (ncommits == s->selected) {
1539 s->selected_entry = entry;
1542 entry = TAILQ_NEXT(entry, entry);
1547 static const struct got_error *
1548 draw_commits(struct tog_view *view)
1550 const struct got_error *err = NULL;
1551 struct tog_log_view_state *s = &view->state.log;
1552 struct commit_queue_entry *entry = s->selected_entry;
1553 const int limit = view->nlines;
1555 int ncommits, author_cols = 4;
1556 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1557 char *refs_str = NULL;
1559 struct tog_color *tc;
1560 static const size_t date_display_cols = 12;
1562 if (s->selected_entry &&
1563 !(view->searching && view->search_next_done == 0)) {
1564 err = got_object_id_str(&id_str, s->selected_entry->id);
1567 err = build_refs_str(&refs_str, &s->refs,
1568 s->selected_entry->id, s->repo);
1573 if (s->thread_args.commits_needed == 0)
1574 halfdelay(10); /* disable fast refresh */
1576 if (s->thread_args.commits_needed > 0) {
1577 if (asprintf(&ncommits_str, " [%d/%d] %s",
1578 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1579 (view->searching && !view->search_next_done) ?
1580 "searching..." : "loading...") == -1) {
1581 err = got_error_from_errno("asprintf");
1585 const char *search_str = NULL;
1587 if (view->searching) {
1588 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1589 search_str = "no more matches";
1590 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1591 search_str = "no matches found";
1592 else if (!view->search_next_done)
1593 search_str = "searching...";
1596 if (asprintf(&ncommits_str, " [%d/%d] %s",
1597 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1598 search_str ? search_str :
1599 (refs_str ? refs_str : "")) == -1) {
1600 err = got_error_from_errno("asprintf");
1605 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1606 if (asprintf(&header, "commit %s %s%s",
1607 id_str ? id_str : "........................................",
1608 s->in_repo_path, ncommits_str) == -1) {
1609 err = got_error_from_errno("asprintf");
1613 } else if (asprintf(&header, "commit %s%s",
1614 id_str ? id_str : "........................................",
1615 ncommits_str) == -1) {
1616 err = got_error_from_errno("asprintf");
1620 err = format_line(&wline, &width, header, view->ncols, 0);
1624 werase(view->window);
1626 if (view_needs_focus_indication(view))
1627 wstandout(view->window);
1628 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1630 wattr_on(view->window,
1631 COLOR_PAIR(tc->colorpair), NULL);
1632 waddwstr(view->window, wline);
1634 wattr_off(view->window,
1635 COLOR_PAIR(tc->colorpair), NULL);
1636 while (width < view->ncols) {
1637 waddch(view->window, ' ');
1640 if (view_needs_focus_indication(view))
1641 wstandend(view->window);
1646 /* Grow author column size if necessary. */
1647 entry = s->first_displayed_entry;
1653 if (ncommits >= limit - 1)
1655 author = strdup(got_object_commit_get_author(entry->commit));
1656 if (author == NULL) {
1657 err = got_error_from_errno("strdup");
1660 err = format_author(&wauthor, &width, author, COLS,
1662 if (author_cols < width)
1663 author_cols = width;
1667 entry = TAILQ_NEXT(entry, entry);
1670 entry = s->first_displayed_entry;
1671 s->last_displayed_entry = s->first_displayed_entry;
1674 if (ncommits >= limit - 1)
1676 if (ncommits == s->selected)
1677 wstandout(view->window);
1678 err = draw_commit(view, entry->commit, entry->id,
1679 date_display_cols, author_cols);
1680 if (ncommits == s->selected)
1681 wstandend(view->window);
1685 s->last_displayed_entry = entry;
1686 entry = TAILQ_NEXT(entry, entry);
1699 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1701 struct commit_queue_entry *entry;
1704 entry = TAILQ_FIRST(&s->commits.head);
1705 if (s->first_displayed_entry == entry)
1708 entry = s->first_displayed_entry;
1709 while (entry && nscrolled < maxscroll) {
1710 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1712 s->first_displayed_entry = entry;
1718 static const struct got_error *
1719 trigger_log_thread(struct tog_view *view, int wait)
1721 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1724 halfdelay(1); /* fast refresh while loading commits */
1726 while (ta->commits_needed > 0) {
1727 if (ta->log_complete)
1730 /* Wake the log thread. */
1731 errcode = pthread_cond_signal(&ta->need_commits);
1733 return got_error_set_errno(errcode,
1734 "pthread_cond_signal");
1737 * The mutex will be released while the view loop waits
1738 * in wgetch(), at which time the log thread will run.
1743 /* Display progress update in log view. */
1744 show_log_view(view);
1748 /* Wait right here while next commit is being loaded. */
1749 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1751 return got_error_set_errno(errcode,
1752 "pthread_cond_wait");
1754 /* Display progress update in log view. */
1755 show_log_view(view);
1763 static const struct got_error *
1764 log_scroll_down(struct tog_view *view, int maxscroll)
1766 struct tog_log_view_state *s = &view->state.log;
1767 const struct got_error *err = NULL;
1768 struct commit_queue_entry *pentry;
1769 int nscrolled = 0, ncommits_needed;
1771 if (s->last_displayed_entry == NULL)
1774 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1775 if (s->commits.ncommits < ncommits_needed &&
1776 !s->thread_args.log_complete) {
1778 * Ask the log thread for required amount of commits.
1780 s->thread_args.commits_needed += maxscroll;
1781 err = trigger_log_thread(view, 1);
1787 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1791 s->last_displayed_entry = pentry;
1793 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1796 s->first_displayed_entry = pentry;
1797 } while (++nscrolled < maxscroll);
1802 static const struct got_error *
1803 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1804 struct got_commit_object *commit, struct got_object_id *commit_id,
1805 struct tog_view *log_view, struct got_repository *repo)
1807 const struct got_error *err;
1808 struct got_object_qid *parent_id;
1809 struct tog_view *diff_view;
1811 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1812 if (diff_view == NULL)
1813 return got_error_from_errno("view_open");
1815 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1816 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1817 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1819 *new_view = diff_view;
1823 static const struct got_error *
1824 tree_view_visit_subtree(struct tog_tree_view_state *s,
1825 struct got_tree_object *subtree)
1827 struct tog_parent_tree *parent;
1829 parent = calloc(1, sizeof(*parent));
1831 return got_error_from_errno("calloc");
1833 parent->tree = s->tree;
1834 parent->first_displayed_entry = s->first_displayed_entry;
1835 parent->selected_entry = s->selected_entry;
1836 parent->selected = s->selected;
1837 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1840 s->first_displayed_entry = NULL;
1844 static const struct got_error *
1845 tree_view_walk_path(struct tog_tree_view_state *s,
1846 struct got_object_id *commit_id, const char *path)
1848 const struct got_error *err = NULL;
1849 struct got_tree_object *tree = NULL;
1851 char *slash, *subpath = NULL;
1853 /* Walk the path and open corresponding tree objects. */
1856 struct got_tree_entry *te;
1857 struct got_object_id *tree_id;
1863 /* Ensure the correct subtree entry is selected. */
1864 slash = strchr(p, '/');
1866 te_name = strdup(p);
1868 te_name = strndup(p, slash - p);
1869 if (te_name == NULL) {
1870 err = got_error_from_errno("strndup");
1873 te = got_object_tree_find_entry(s->tree, te_name);
1875 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1880 s->first_displayed_entry = s->selected_entry = te;
1882 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1883 break; /* jump to this file's entry */
1885 slash = strchr(p, '/');
1887 subpath = strndup(path, slash - path);
1889 subpath = strdup(path);
1890 if (subpath == NULL) {
1891 err = got_error_from_errno("strdup");
1895 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1900 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1905 err = tree_view_visit_subtree(s, tree);
1907 got_object_tree_close(tree);
1921 static const struct got_error *
1922 browse_commit_tree(struct tog_view **new_view, int begin_x,
1923 struct commit_queue_entry *entry, const char *path,
1924 const char *head_ref_name, struct got_repository *repo)
1926 const struct got_error *err = NULL;
1927 struct got_tree_object *tree;
1928 struct tog_tree_view_state *s;
1929 struct tog_view *tree_view;
1931 err = got_object_open_as_tree(&tree, repo,
1932 got_object_commit_get_tree_id(entry->commit));
1936 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1937 if (tree_view == NULL)
1938 return got_error_from_errno("view_open");
1940 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1942 got_object_tree_close(tree);
1945 s = &tree_view->state.tree;
1947 *new_view = tree_view;
1949 if (got_path_is_root_dir(path))
1952 return tree_view_walk_path(s, entry->id, path);
1955 static const struct got_error *
1956 block_signals_used_by_main_thread(void)
1961 if (sigemptyset(&sigset) == -1)
1962 return got_error_from_errno("sigemptyset");
1964 /* tog handles SIGWINCH and SIGCONT */
1965 if (sigaddset(&sigset, SIGWINCH) == -1)
1966 return got_error_from_errno("sigaddset");
1967 if (sigaddset(&sigset, SIGCONT) == -1)
1968 return got_error_from_errno("sigaddset");
1970 /* ncurses handles SIGTSTP */
1971 if (sigaddset(&sigset, SIGTSTP) == -1)
1972 return got_error_from_errno("sigaddset");
1974 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1976 return got_error_set_errno(errcode, "pthread_sigmask");
1982 log_thread(void *arg)
1984 const struct got_error *err = NULL;
1986 struct tog_log_thread_args *a = arg;
1989 err = block_signals_used_by_main_thread();
1993 while (!done && !err && !tog_sigpipe_received) {
1994 err = queue_commits(a);
1996 if (err->code != GOT_ERR_ITER_COMPLETED)
2000 } else if (a->commits_needed > 0)
2001 a->commits_needed--;
2003 errcode = pthread_mutex_lock(&tog_mutex);
2005 err = got_error_set_errno(errcode,
2006 "pthread_mutex_lock");
2008 } else if (*a->quit)
2010 else if (*a->first_displayed_entry == NULL) {
2011 *a->first_displayed_entry =
2012 TAILQ_FIRST(&a->commits->head);
2013 *a->selected_entry = *a->first_displayed_entry;
2016 errcode = pthread_cond_signal(&a->commit_loaded);
2018 err = got_error_set_errno(errcode,
2019 "pthread_cond_signal");
2020 pthread_mutex_unlock(&tog_mutex);
2025 a->commits_needed = 0;
2027 if (a->commits_needed == 0) {
2028 errcode = pthread_cond_wait(&a->need_commits,
2031 err = got_error_set_errno(errcode,
2032 "pthread_cond_wait");
2038 errcode = pthread_mutex_unlock(&tog_mutex);
2039 if (errcode && err == NULL)
2040 err = got_error_set_errno(errcode,
2041 "pthread_mutex_unlock");
2043 a->log_complete = 1;
2047 static const struct got_error *
2048 stop_log_thread(struct tog_log_view_state *s)
2050 const struct got_error *err = NULL;
2055 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2057 return got_error_set_errno(errcode,
2058 "pthread_cond_signal");
2059 errcode = pthread_mutex_unlock(&tog_mutex);
2061 return got_error_set_errno(errcode,
2062 "pthread_mutex_unlock");
2063 errcode = pthread_join(s->thread, (void **)&err);
2065 return got_error_set_errno(errcode, "pthread_join");
2066 errcode = pthread_mutex_lock(&tog_mutex);
2068 return got_error_set_errno(errcode,
2069 "pthread_mutex_lock");
2073 if (s->thread_args.repo) {
2074 got_repo_close(s->thread_args.repo);
2075 s->thread_args.repo = NULL;
2078 if (s->thread_args.graph) {
2079 got_commit_graph_close(s->thread_args.graph);
2080 s->thread_args.graph = NULL;
2086 static const struct got_error *
2087 close_log_view(struct tog_view *view)
2089 const struct got_error *err = NULL;
2090 struct tog_log_view_state *s = &view->state.log;
2093 err = stop_log_thread(s);
2095 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2096 if (errcode && err == NULL)
2097 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2099 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2100 if (errcode && err == NULL)
2101 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2103 free_commits(&s->commits);
2104 free(s->in_repo_path);
2105 s->in_repo_path = NULL;
2108 free(s->head_ref_name);
2109 s->head_ref_name = NULL;
2110 got_ref_list_free(&s->refs);
2114 static const struct got_error *
2115 search_start_log_view(struct tog_view *view)
2117 struct tog_log_view_state *s = &view->state.log;
2119 s->matched_entry = NULL;
2120 s->search_entry = NULL;
2124 static const struct got_error *
2125 search_next_log_view(struct tog_view *view)
2127 const struct got_error *err = NULL;
2128 struct tog_log_view_state *s = &view->state.log;
2129 struct commit_queue_entry *entry;
2131 /* Display progress update in log view. */
2132 show_log_view(view);
2136 if (s->search_entry) {
2138 errcode = pthread_mutex_unlock(&tog_mutex);
2140 return got_error_set_errno(errcode,
2141 "pthread_mutex_unlock");
2142 ch = wgetch(view->window);
2143 errcode = pthread_mutex_lock(&tog_mutex);
2145 return got_error_set_errno(errcode,
2146 "pthread_mutex_lock");
2147 if (ch == KEY_BACKSPACE) {
2148 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2151 if (view->searching == TOG_SEARCH_FORWARD)
2152 entry = TAILQ_NEXT(s->search_entry, entry);
2154 entry = TAILQ_PREV(s->search_entry,
2155 commit_queue_head, entry);
2156 } else if (s->matched_entry) {
2157 if (view->searching == TOG_SEARCH_FORWARD)
2158 entry = TAILQ_NEXT(s->matched_entry, entry);
2160 entry = TAILQ_PREV(s->matched_entry,
2161 commit_queue_head, entry);
2163 if (view->searching == TOG_SEARCH_FORWARD)
2164 entry = TAILQ_FIRST(&s->commits.head);
2166 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2172 if (entry == NULL) {
2173 if (s->thread_args.log_complete ||
2174 view->searching == TOG_SEARCH_BACKWARD) {
2175 view->search_next_done =
2176 (s->matched_entry == NULL ?
2177 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2178 s->search_entry = NULL;
2182 * Poke the log thread for more commits and return,
2183 * allowing the main loop to make progress. Search
2184 * will resume at s->search_entry once we come back.
2186 s->thread_args.commits_needed++;
2187 return trigger_log_thread(view, 0);
2190 err = match_commit(&have_match, entry->id, entry->commit,
2195 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2196 s->matched_entry = entry;
2200 s->search_entry = entry;
2201 if (view->searching == TOG_SEARCH_FORWARD)
2202 entry = TAILQ_NEXT(entry, entry);
2204 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2207 if (s->matched_entry) {
2208 int cur = s->selected_entry->idx;
2209 while (cur < s->matched_entry->idx) {
2210 err = input_log_view(NULL, view, KEY_DOWN);
2215 while (cur > s->matched_entry->idx) {
2216 err = input_log_view(NULL, view, KEY_UP);
2223 s->search_entry = NULL;
2228 static const struct got_error *
2229 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2230 struct got_repository *repo, const char *head_ref_name,
2231 const char *in_repo_path, int log_branches)
2233 const struct got_error *err = NULL;
2234 struct tog_log_view_state *s = &view->state.log;
2235 struct got_repository *thread_repo = NULL;
2236 struct got_commit_graph *thread_graph = NULL;
2239 SIMPLEQ_INIT(&s->refs);
2241 if (in_repo_path != s->in_repo_path) {
2242 free(s->in_repo_path);
2243 s->in_repo_path = strdup(in_repo_path);
2244 if (s->in_repo_path == NULL)
2245 return got_error_from_errno("strdup");
2248 /* The commit queue only contains commits being displayed. */
2249 TAILQ_INIT(&s->commits.head);
2250 s->commits.ncommits = 0;
2252 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2257 if (head_ref_name) {
2258 s->head_ref_name = strdup(head_ref_name);
2259 if (s->head_ref_name == NULL) {
2260 err = got_error_from_errno("strdup");
2264 s->start_id = got_object_id_dup(start_id);
2265 if (s->start_id == NULL) {
2266 err = got_error_from_errno("got_object_id_dup");
2269 s->log_branches = log_branches;
2271 SIMPLEQ_INIT(&s->colors);
2272 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2273 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2274 get_color_value("TOG_COLOR_COMMIT"));
2277 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2278 get_color_value("TOG_COLOR_AUTHOR"));
2280 free_colors(&s->colors);
2283 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2284 get_color_value("TOG_COLOR_DATE"));
2286 free_colors(&s->colors);
2291 view->show = show_log_view;
2292 view->input = input_log_view;
2293 view->close = close_log_view;
2294 view->search_start = search_start_log_view;
2295 view->search_next = search_next_log_view;
2297 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2300 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2304 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2305 s->repo, NULL, NULL);
2309 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2311 err = got_error_set_errno(errcode, "pthread_cond_init");
2314 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2316 err = got_error_set_errno(errcode, "pthread_cond_init");
2320 s->thread_args.commits_needed = view->nlines;
2321 s->thread_args.graph = thread_graph;
2322 s->thread_args.commits = &s->commits;
2323 s->thread_args.in_repo_path = s->in_repo_path;
2324 s->thread_args.start_id = s->start_id;
2325 s->thread_args.repo = thread_repo;
2326 s->thread_args.log_complete = 0;
2327 s->thread_args.quit = &s->quit;
2328 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2329 s->thread_args.selected_entry = &s->selected_entry;
2330 s->thread_args.searching = &view->searching;
2331 s->thread_args.search_next_done = &view->search_next_done;
2332 s->thread_args.regex = &view->regex;
2335 close_log_view(view);
2339 static const struct got_error *
2340 show_log_view(struct tog_view *view)
2342 const struct got_error *err;
2343 struct tog_log_view_state *s = &view->state.log;
2345 if (s->thread == NULL) {
2346 int errcode = pthread_create(&s->thread, NULL, log_thread,
2349 return got_error_set_errno(errcode, "pthread_create");
2350 if (s->thread_args.commits_needed > 0) {
2351 err = trigger_log_thread(view, 1);
2357 return draw_commits(view);
2360 static const struct got_error *
2361 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2363 const struct got_error *err = NULL;
2364 struct tog_log_view_state *s = &view->state.log;
2365 struct tog_view *diff_view = NULL, *tree_view = NULL;
2366 struct tog_view *ref_view = NULL;
2377 if (s->first_displayed_entry == NULL)
2379 if (s->selected > 0)
2382 log_scroll_up(s, 1);
2387 if (s->first_displayed_entry == NULL)
2389 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2392 log_scroll_up(s, view->nlines - 1);
2399 if (s->first_displayed_entry == NULL)
2401 if (s->selected < MIN(view->nlines - 2,
2402 s->commits.ncommits - 1))
2405 err = log_scroll_down(view, 1);
2413 struct commit_queue_entry *first;
2414 first = s->first_displayed_entry;
2417 err = log_scroll_down(view, view->nlines - 1);
2420 if (first == s->first_displayed_entry &&
2421 s->selected < MIN(view->nlines - 2,
2422 s->commits.ncommits - 1)) {
2423 /* can't scroll further down */
2424 s->selected = MIN(view->nlines - 2,
2425 s->commits.ncommits - 1);
2431 if (s->selected > view->nlines - 2)
2432 s->selected = view->nlines - 2;
2433 if (s->selected > s->commits.ncommits - 1)
2434 s->selected = s->commits.ncommits - 1;
2436 if (s->commits.ncommits < view->nlines - 1 &&
2437 !s->thread_args.log_complete) {
2438 s->thread_args.commits_needed += (view->nlines - 1) -
2439 s->commits.ncommits;
2440 err = trigger_log_thread(view, 1);
2446 if (s->selected_entry == NULL)
2448 if (view_is_parent_view(view))
2449 begin_x = view_split_begin_x(view->begin_x);
2450 err = open_diff_view_for_commit(&diff_view, begin_x,
2451 s->selected_entry->commit, s->selected_entry->id,
2456 diff_view->focussed = 1;
2457 if (view_is_parent_view(view)) {
2458 err = view_close_child(view);
2461 view_set_child(view, diff_view);
2462 view->focus_child = 1;
2464 *new_view = diff_view;
2467 if (s->selected_entry == NULL)
2469 if (view_is_parent_view(view))
2470 begin_x = view_split_begin_x(view->begin_x);
2471 err = browse_commit_tree(&tree_view, begin_x,
2472 s->selected_entry, s->in_repo_path, s->head_ref_name,
2477 tree_view->focussed = 1;
2478 if (view_is_parent_view(view)) {
2479 err = view_close_child(view);
2482 view_set_child(view, tree_view);
2483 view->focus_child = 1;
2485 *new_view = tree_view;
2490 if (ch == KEY_BACKSPACE &&
2491 got_path_is_root_dir(s->in_repo_path))
2493 err = stop_log_thread(s);
2496 if (ch == KEY_BACKSPACE) {
2498 err = got_path_dirname(&parent_path, s->in_repo_path);
2501 free(s->in_repo_path);
2502 s->in_repo_path = parent_path;
2503 s->thread_args.in_repo_path = s->in_repo_path;
2504 } else if (ch == CTRL('l')) {
2505 struct got_object_id *start_id;
2506 err = got_repo_match_object_id(&start_id, NULL,
2507 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2508 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2512 s->start_id = start_id;
2513 s->thread_args.start_id = s->start_id;
2515 s->log_branches = !s->log_branches;
2517 err = got_repo_open(&s->thread_args.repo,
2518 got_repo_get_path(s->repo), NULL);
2521 got_ref_list_free(&s->refs);
2522 err = got_ref_list(&s->refs, s->repo, NULL,
2523 got_ref_cmp_by_name, NULL);
2526 err = got_commit_graph_open(&s->thread_args.graph,
2527 s->in_repo_path, !s->log_branches);
2530 err = got_commit_graph_iter_start(s->thread_args.graph,
2531 s->start_id, s->repo, NULL, NULL);
2534 free_commits(&s->commits);
2535 s->first_displayed_entry = NULL;
2536 s->last_displayed_entry = NULL;
2537 s->selected_entry = NULL;
2539 s->thread_args.log_complete = 0;
2541 s->thread_args.commits_needed = view->nlines;
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);
2556 ref_view->focussed = 1;
2557 if (view_is_parent_view(view)) {
2558 err = view_close_child(view);
2561 view_set_child(view, ref_view);
2562 view->focus_child = 1;
2564 *new_view = ref_view;
2573 static const struct got_error *
2574 apply_unveil(const char *repo_path, const char *worktree_path)
2576 const struct got_error *error;
2579 if (unveil("gmon.out", "rwc") != 0)
2580 return got_error_from_errno2("unveil", "gmon.out");
2582 if (repo_path && unveil(repo_path, "r") != 0)
2583 return got_error_from_errno2("unveil", repo_path);
2585 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2586 return got_error_from_errno2("unveil", worktree_path);
2588 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2589 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2591 error = got_privsep_unveil_exec_helpers();
2595 if (unveil(NULL, NULL) != 0)
2596 return got_error_from_errno("unveil");
2606 halfdelay(1); /* Do fast refresh while initial view is loading. */
2609 intrflush(stdscr, FALSE);
2610 keypad(stdscr, TRUE);
2612 if (getenv("TOG_COLORS") != NULL) {
2614 use_default_colors();
2616 signal(SIGWINCH, tog_sigwinch);
2617 signal(SIGPIPE, tog_sigpipe);
2618 signal(SIGCONT, tog_sigcont);
2621 static const struct got_error *
2622 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2623 struct got_repository *repo, struct got_worktree *worktree)
2625 const struct got_error *err = NULL;
2628 *in_repo_path = strdup("/");
2629 if (*in_repo_path == NULL)
2630 return got_error_from_errno("strdup");
2635 const char *prefix = got_worktree_get_path_prefix(worktree);
2638 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2641 if (asprintf(in_repo_path, "%s%s%s", prefix,
2642 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2644 err = got_error_from_errno("asprintf");
2645 *in_repo_path = NULL;
2649 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2654 static const struct got_error *
2655 cmd_log(int argc, char *argv[])
2657 const struct got_error *error;
2658 struct got_repository *repo = NULL;
2659 struct got_worktree *worktree = NULL;
2660 struct got_object_id *start_id = NULL;
2661 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2662 char *start_commit = NULL, *label = NULL;
2663 struct got_reference *ref = NULL;
2664 const char *head_ref_name = NULL;
2665 int ch, log_branches = 0;
2666 struct tog_view *view;
2668 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2674 start_commit = optarg;
2677 repo_path = realpath(optarg, NULL);
2678 if (repo_path == NULL)
2679 return got_error_from_errno2("realpath",
2694 cwd = getcwd(NULL, 0);
2696 return got_error_from_errno("getcwd");
2698 error = got_worktree_open(&worktree, cwd);
2699 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2702 if (repo_path == NULL) {
2705 strdup(got_worktree_get_repo_path(worktree));
2707 repo_path = strdup(cwd);
2709 if (repo_path == NULL) {
2710 error = got_error_from_errno("strdup");
2714 error = got_repo_open(&repo, repo_path, NULL);
2718 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2725 error = apply_unveil(got_repo_get_path(repo),
2726 worktree ? got_worktree_get_root_path(worktree) : NULL);
2730 if (start_commit == NULL) {
2731 error = got_repo_match_object_id(&start_id, &label,
2732 worktree ? got_worktree_get_head_ref_name(worktree) :
2733 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2736 head_ref_name = label;
2738 error = got_ref_open(&ref, repo, start_commit, 0);
2740 head_ref_name = got_ref_get_name(ref);
2741 else if (error->code != GOT_ERR_NOT_REF)
2743 error = got_repo_match_object_id(&start_id, NULL,
2744 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2749 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2751 error = got_error_from_errno("view_open");
2754 error = open_log_view(view, start_id, repo, head_ref_name,
2755 in_repo_path, log_branches);
2759 /* Release work tree lock. */
2760 got_worktree_close(worktree);
2763 error = view_loop(view);
2773 got_repo_close(repo);
2775 got_worktree_close(worktree);
2783 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2784 "[-w] object1 object2\n", getprogname());
2789 parse_next_line(FILE *f, size_t *len)
2794 const char delim[3] = { '\0', '\0', '\0'};
2796 line = fparseln(f, &linelen, &lineno, delim, 0);
2803 match_line(const char *line, regex_t *regex, size_t nmatch,
2804 regmatch_t *regmatch)
2806 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2810 match_color(struct tog_colors *colors, const char *line)
2812 struct tog_color *tc = NULL;
2814 SIMPLEQ_FOREACH(tc, colors, entry) {
2815 if (match_line(line, &tc->regex, 0, NULL))
2822 static const struct got_error *
2823 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2824 WINDOW *window, regmatch_t *regmatch)
2826 const struct got_error *err = NULL;
2833 s = strndup(line, regmatch->rm_so);
2835 return got_error_from_errno("strndup");
2837 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2842 waddwstr(window, wline);
2849 s = strndup(line + regmatch->rm_so,
2850 regmatch->rm_eo - regmatch->rm_so);
2852 err = got_error_from_errno("strndup");
2856 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2861 wattr_on(window, A_STANDOUT, NULL);
2862 waddwstr(window, wline);
2863 wattr_off(window, A_STANDOUT, NULL);
2870 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2871 err = format_line(&wline, &width,
2872 line + regmatch->rm_eo, wlimit, col_tab_align);
2875 waddwstr(window, wline);
2883 static const struct got_error *
2884 draw_file(struct tog_view *view, const char *header)
2886 struct tog_diff_view_state *s = &view->state.diff;
2887 regmatch_t *regmatch = &view->regmatch;
2888 const struct got_error *err;
2891 struct tog_color *tc;
2895 int max_lines = view->nlines;
2896 int nlines = s->nlines;
2899 line_offset = s->line_offsets[s->first_displayed_line - 1];
2900 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2901 return got_error_from_errno("fseek");
2903 werase(view->window);
2906 if (asprintf(&line, "[%d/%d] %s",
2907 s->first_displayed_line - 1 + s->selected_line, nlines,
2909 return got_error_from_errno("asprintf");
2910 err = format_line(&wline, &width, line, view->ncols, 0);
2915 if (view_needs_focus_indication(view))
2916 wstandout(view->window);
2917 waddwstr(view->window, wline);
2920 if (view_needs_focus_indication(view))
2921 wstandend(view->window);
2922 if (width <= view->ncols - 1)
2923 waddch(view->window, '\n');
2931 while (max_lines > 0 && nprinted < max_lines) {
2932 line = parse_next_line(s->f, &len);
2938 tc = match_color(&s->colors, line);
2940 wattr_on(view->window,
2941 COLOR_PAIR(tc->colorpair), NULL);
2942 if (s->first_displayed_line + nprinted == s->matched_line &&
2943 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2944 err = add_matched_line(&width, line, view->ncols, 0,
2945 view->window, regmatch);
2951 err = format_line(&wline, &width, line, view->ncols, 0);
2956 waddwstr(view->window, wline);
2961 wattr_off(view->window,
2962 COLOR_PAIR(tc->colorpair), NULL);
2963 if (width <= view->ncols - 1)
2964 waddch(view->window, '\n');
2969 s->last_displayed_line = s->first_displayed_line +
2972 s->last_displayed_line = s->first_displayed_line;
2977 while (nprinted < view->nlines) {
2978 waddch(view->window, '\n');
2982 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2987 wstandout(view->window);
2988 waddwstr(view->window, wline);
2991 wstandend(view->window);
2998 get_datestr(time_t *time, char *datebuf)
3000 struct tm mytm, *tm;
3003 tm = gmtime_r(time, &mytm);
3006 s = asctime_r(tm, datebuf);
3009 p = strchr(s, '\n');
3015 static const struct got_error *
3016 get_changed_paths(struct got_pathlist_head *paths,
3017 struct got_commit_object *commit, struct got_repository *repo)
3019 const struct got_error *err = NULL;
3020 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3021 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3022 struct got_object_qid *qid;
3024 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3026 struct got_commit_object *pcommit;
3027 err = got_object_open_as_commit(&pcommit, repo,
3032 tree_id1 = got_object_commit_get_tree_id(pcommit);
3033 got_object_commit_close(pcommit);
3038 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3043 tree_id2 = got_object_commit_get_tree_id(commit);
3044 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3048 err = got_diff_tree(tree1, tree2, "", "", repo,
3049 got_diff_tree_collect_changed_paths, paths, 0);
3052 got_object_tree_close(tree1);
3054 got_object_tree_close(tree2);
3058 static const struct got_error *
3059 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3063 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3065 return got_error_from_errno("reallocarray");
3067 (*line_offsets)[*nlines] = off;
3072 static const struct got_error *
3073 write_commit_info(off_t **line_offsets, size_t *nlines,
3074 struct got_object_id *commit_id, struct got_reflist_head *refs,
3075 struct got_repository *repo, FILE *outfile)
3077 const struct got_error *err = NULL;
3078 char datebuf[26], *datestr;
3079 struct got_commit_object *commit;
3080 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3081 time_t committer_time;
3082 const char *author, *committer;
3083 char *refs_str = NULL;
3084 struct got_pathlist_head changed_paths;
3085 struct got_pathlist_entry *pe;
3089 TAILQ_INIT(&changed_paths);
3092 err = build_refs_str(&refs_str, refs, commit_id, repo);
3097 err = got_object_open_as_commit(&commit, repo, commit_id);
3101 err = got_object_id_str(&id_str, commit_id);
3103 err = got_error_from_errno("got_object_id_str");
3107 err = add_line_offset(line_offsets, nlines, 0);
3111 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3112 refs_str ? refs_str : "", refs_str ? ")" : "");
3114 err = got_error_from_errno("fprintf");
3118 err = add_line_offset(line_offsets, nlines, outoff);
3122 n = fprintf(outfile, "from: %s\n",
3123 got_object_commit_get_author(commit));
3125 err = got_error_from_errno("fprintf");
3129 err = add_line_offset(line_offsets, nlines, outoff);
3133 committer_time = got_object_commit_get_committer_time(commit);
3134 datestr = get_datestr(&committer_time, datebuf);
3136 n = fprintf(outfile, "date: %s UTC\n", datestr);
3138 err = got_error_from_errno("fprintf");
3142 err = add_line_offset(line_offsets, nlines, outoff);
3146 author = got_object_commit_get_author(commit);
3147 committer = got_object_commit_get_committer(commit);
3148 if (strcmp(author, committer) != 0) {
3149 n = fprintf(outfile, "via: %s\n", committer);
3151 err = got_error_from_errno("fprintf");
3155 err = add_line_offset(line_offsets, nlines, outoff);
3159 err = got_object_commit_get_logmsg(&logmsg, commit);
3163 while ((line = strsep(&s, "\n")) != NULL) {
3164 n = fprintf(outfile, "%s\n", line);
3166 err = got_error_from_errno("fprintf");
3170 err = add_line_offset(line_offsets, nlines, outoff);
3175 err = get_changed_paths(&changed_paths, commit, repo);
3178 TAILQ_FOREACH(pe, &changed_paths, entry) {
3179 struct got_diff_changed_path *cp = pe->data;
3180 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3182 err = got_error_from_errno("fprintf");
3186 err = add_line_offset(line_offsets, nlines, outoff);
3189 free((char *)pe->path);
3193 fputc('\n', outfile);
3195 err = add_line_offset(line_offsets, nlines, outoff);
3197 got_pathlist_free(&changed_paths);
3201 got_object_commit_close(commit);
3203 free(*line_offsets);
3204 *line_offsets = NULL;
3210 static const struct got_error *
3211 create_diff(struct tog_diff_view_state *s)
3213 const struct got_error *err = NULL;
3217 free(s->line_offsets);
3218 s->line_offsets = malloc(sizeof(off_t));
3219 if (s->line_offsets == NULL)
3220 return got_error_from_errno("malloc");
3225 err = got_error_from_errno("got_opentemp");
3228 if (s->f && fclose(s->f) != 0) {
3229 err = got_error_from_errno("fclose");
3235 err = got_object_get_type(&obj_type, s->repo, s->id1);
3237 err = got_object_get_type(&obj_type, s->repo, s->id2);
3242 case GOT_OBJ_TYPE_BLOB:
3243 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3244 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3245 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3247 case GOT_OBJ_TYPE_TREE:
3248 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3249 s->id1, s->id2, "", "", s->diff_context,
3250 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3252 case GOT_OBJ_TYPE_COMMIT: {
3253 const struct got_object_id_queue *parent_ids;
3254 struct got_object_qid *pid;
3255 struct got_commit_object *commit2;
3257 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3260 /* Show commit info if we're diffing to a parent/root commit. */
3261 if (s->id1 == NULL) {
3262 err = write_commit_info(&s->line_offsets, &s->nlines,
3263 s->id2, &s->refs, s->repo, s->f);
3267 parent_ids = got_object_commit_get_parent_ids(commit2);
3268 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3269 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3270 err = write_commit_info(
3271 &s->line_offsets, &s->nlines,
3272 s->id2, &s->refs, s->repo, s->f);
3279 got_object_commit_close(commit2);
3281 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3282 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3283 s->force_text_diff, s->repo, s->f);
3287 err = got_error(GOT_ERR_OBJ_TYPE);
3293 if (s->f && fflush(s->f) != 0 && err == NULL)
3294 err = got_error_from_errno("fflush");
3299 diff_view_indicate_progress(struct tog_view *view)
3301 mvwaddstr(view->window, 0, 0, "diffing...");
3306 static const struct got_error *
3307 search_start_diff_view(struct tog_view *view)
3309 struct tog_diff_view_state *s = &view->state.diff;
3311 s->matched_line = 0;
3315 static const struct got_error *
3316 search_next_diff_view(struct tog_view *view)
3318 struct tog_diff_view_state *s = &view->state.diff;
3321 if (!view->searching) {
3322 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3326 if (s->matched_line) {
3327 if (view->searching == TOG_SEARCH_FORWARD)
3328 lineno = s->matched_line + 1;
3330 lineno = s->matched_line - 1;
3332 if (view->searching == TOG_SEARCH_FORWARD)
3343 if (lineno <= 0 || lineno > s->nlines) {
3344 if (s->matched_line == 0) {
3345 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3350 if (view->searching == TOG_SEARCH_FORWARD)
3356 offset = s->line_offsets[lineno - 1];
3357 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3359 return got_error_from_errno("fseeko");
3362 line = parse_next_line(s->f, &len);
3364 match_line(line, &view->regex, 1, &view->regmatch)) {
3365 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3366 s->matched_line = lineno;
3371 if (view->searching == TOG_SEARCH_FORWARD)
3377 if (s->matched_line) {
3378 s->first_displayed_line = s->matched_line;
3379 s->selected_line = 1;
3385 static const struct got_error *
3386 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3387 struct got_object_id *id2, const char *label1, const char *label2,
3388 int diff_context, int ignore_whitespace, int force_text_diff,
3389 struct tog_view *log_view, struct got_repository *repo)
3391 const struct got_error *err;
3392 struct tog_diff_view_state *s = &view->state.diff;
3394 SIMPLEQ_INIT(&s->refs);
3396 if (id1 != NULL && id2 != NULL) {
3398 err = got_object_get_type(&type1, repo, id1);
3401 err = got_object_get_type(&type2, repo, id2);
3406 return got_error(GOT_ERR_OBJ_TYPE);
3408 s->first_displayed_line = 1;
3409 s->last_displayed_line = view->nlines;
3410 s->selected_line = 1;
3418 s->id1 = got_object_id_dup(id1);
3420 return got_error_from_errno("got_object_id_dup");
3424 s->id2 = got_object_id_dup(id2);
3425 if (s->id2 == NULL) {
3428 return got_error_from_errno("got_object_id_dup");
3431 s->first_displayed_line = 1;
3432 s->last_displayed_line = view->nlines;
3433 s->diff_context = diff_context;
3434 s->ignore_whitespace = ignore_whitespace;
3435 s->force_text_diff = force_text_diff;
3436 s->log_view = log_view;
3439 SIMPLEQ_INIT(&s->colors);
3440 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3441 err = add_color(&s->colors,
3442 "^-", TOG_COLOR_DIFF_MINUS,
3443 get_color_value("TOG_COLOR_DIFF_MINUS"));
3446 err = add_color(&s->colors, "^\\+",
3447 TOG_COLOR_DIFF_PLUS,
3448 get_color_value("TOG_COLOR_DIFF_PLUS"));
3450 free_colors(&s->colors);
3453 err = add_color(&s->colors,
3454 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3455 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3457 free_colors(&s->colors);
3461 err = add_color(&s->colors,
3462 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3463 TOG_COLOR_DIFF_META,
3464 get_color_value("TOG_COLOR_DIFF_META"));
3466 free_colors(&s->colors);
3470 err = add_color(&s->colors,
3471 "^(from|via): ", TOG_COLOR_AUTHOR,
3472 get_color_value("TOG_COLOR_AUTHOR"));
3474 free_colors(&s->colors);
3478 err = add_color(&s->colors,
3479 "^date: ", TOG_COLOR_DATE,
3480 get_color_value("TOG_COLOR_DATE"));
3482 free_colors(&s->colors);
3487 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3493 free_colors(&s->colors);
3497 if (log_view && view_is_splitscreen(view))
3498 show_log_view(log_view); /* draw vborder */
3499 diff_view_indicate_progress(view);
3501 s->line_offsets = NULL;
3503 err = create_diff(s);
3509 free_colors(&s->colors);
3510 got_ref_list_free(&s->refs);
3514 view->show = show_diff_view;
3515 view->input = input_diff_view;
3516 view->close = close_diff_view;
3517 view->search_start = search_start_diff_view;
3518 view->search_next = search_next_diff_view;
3523 static const struct got_error *
3524 close_diff_view(struct tog_view *view)
3526 const struct got_error *err = NULL;
3527 struct tog_diff_view_state *s = &view->state.diff;
3533 if (s->f && fclose(s->f) == EOF)
3534 err = got_error_from_errno("fclose");
3535 free_colors(&s->colors);
3536 free(s->line_offsets);
3537 s->line_offsets = NULL;
3539 got_ref_list_free(&s->refs);
3543 static const struct got_error *
3544 show_diff_view(struct tog_view *view)
3546 const struct got_error *err;
3547 struct tog_diff_view_state *s = &view->state.diff;
3548 char *id_str1 = NULL, *id_str2, *header;
3549 const char *label1, *label2;
3552 err = got_object_id_str(&id_str1, s->id1);
3555 label1 = s->label1 ? : id_str1;
3557 label1 = "/dev/null";
3559 err = got_object_id_str(&id_str2, s->id2);
3562 label2 = s->label2 ? : id_str2;
3564 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3565 err = got_error_from_errno("asprintf");
3573 return draw_file(view, header);
3576 static const struct got_error *
3577 set_selected_commit(struct tog_diff_view_state *s,
3578 struct commit_queue_entry *entry)
3580 const struct got_error *err;
3581 const struct got_object_id_queue *parent_ids;
3582 struct got_commit_object *selected_commit;
3583 struct got_object_qid *pid;
3586 s->id2 = got_object_id_dup(entry->id);
3588 return got_error_from_errno("got_object_id_dup");
3590 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3593 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3595 pid = SIMPLEQ_FIRST(parent_ids);
3596 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3597 got_object_commit_close(selected_commit);
3601 static const struct got_error *
3602 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3604 const struct got_error *err = NULL;
3605 struct tog_diff_view_state *s = &view->state.diff;
3606 struct tog_log_view_state *ls;
3607 struct commit_queue_entry *old_selected_entry;
3614 s->force_text_diff = !s->force_text_diff;
3616 s->ignore_whitespace = !s->ignore_whitespace;
3617 wclear(view->window);
3618 s->first_displayed_line = 1;
3619 s->last_displayed_line = view->nlines;
3620 diff_view_indicate_progress(view);
3621 err = create_diff(s);
3625 if (s->first_displayed_line > 1)
3626 s->first_displayed_line--;
3630 if (s->first_displayed_line == 1)
3633 while (i++ < view->nlines - 1 &&
3634 s->first_displayed_line > 1)
3635 s->first_displayed_line--;
3640 s->first_displayed_line++;
3648 while (!s->eof && i++ < view->nlines - 1) {
3650 line = parse_next_line(s->f, NULL);
3651 s->first_displayed_line++;
3657 if (s->diff_context > 0) {
3659 diff_view_indicate_progress(view);
3660 err = create_diff(s);
3661 if (s->first_displayed_line + view->nlines - 1 >
3663 s->first_displayed_line = 1;
3664 s->last_displayed_line = view->nlines;
3669 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3671 diff_view_indicate_progress(view);
3672 err = create_diff(s);
3677 if (s->log_view == NULL)
3679 ls = &s->log_view->state.log;
3680 old_selected_entry = ls->selected_entry;
3682 err = input_log_view(NULL, s->log_view, KEY_UP);
3686 if (old_selected_entry == ls->selected_entry)
3689 err = set_selected_commit(s, ls->selected_entry);
3693 s->first_displayed_line = 1;
3694 s->last_displayed_line = view->nlines;
3696 diff_view_indicate_progress(view);
3697 err = create_diff(s);
3701 if (s->log_view == NULL)
3703 ls = &s->log_view->state.log;
3704 old_selected_entry = ls->selected_entry;
3706 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3710 if (old_selected_entry == ls->selected_entry)
3713 err = set_selected_commit(s, ls->selected_entry);
3717 s->first_displayed_line = 1;
3718 s->last_displayed_line = view->nlines;
3720 diff_view_indicate_progress(view);
3721 err = create_diff(s);
3730 static const struct got_error *
3731 cmd_diff(int argc, char *argv[])
3733 const struct got_error *error = NULL;
3734 struct got_repository *repo = NULL;
3735 struct got_worktree *worktree = NULL;
3736 struct got_object_id *id1 = NULL, *id2 = NULL;
3737 char *repo_path = NULL, *cwd = NULL;
3738 char *id_str1 = NULL, *id_str2 = NULL;
3739 char *label1 = NULL, *label2 = NULL;
3740 int diff_context = 3, ignore_whitespace = 0;
3741 int ch, force_text_diff = 0;
3743 struct tog_view *view;
3745 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3748 force_text_diff = 1;
3751 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3754 err(1, "-C option %s", errstr);
3757 repo_path = realpath(optarg, NULL);
3758 if (repo_path == NULL)
3759 return got_error_from_errno2("realpath",
3761 got_path_strip_trailing_slashes(repo_path);
3764 ignore_whitespace = 1;
3776 usage_diff(); /* TODO show local worktree changes */
3777 } else if (argc == 2) {
3783 cwd = getcwd(NULL, 0);
3785 return got_error_from_errno("getcwd");
3787 error = got_worktree_open(&worktree, cwd);
3788 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3791 if (repo_path == NULL) {
3794 strdup(got_worktree_get_repo_path(worktree));
3796 repo_path = strdup(cwd);
3798 if (repo_path == NULL) {
3799 error = got_error_from_errno("strdup");
3803 error = got_repo_open(&repo, repo_path, NULL);
3809 error = apply_unveil(got_repo_get_path(repo), NULL);
3813 error = got_repo_match_object_id(&id1, &label1, id_str1,
3814 GOT_OBJ_TYPE_ANY, 1, repo);
3818 error = got_repo_match_object_id(&id2, &label2, id_str2,
3819 GOT_OBJ_TYPE_ANY, 1, repo);
3823 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3825 error = got_error_from_errno("view_open");
3828 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3829 ignore_whitespace, force_text_diff, NULL, repo);
3832 error = view_loop(view);
3839 got_repo_close(repo);
3841 got_worktree_close(worktree);
3849 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3854 struct tog_blame_line {
3856 struct got_object_id *id;
3859 static const struct got_error *
3860 draw_blame(struct tog_view *view)
3862 struct tog_blame_view_state *s = &view->state.blame;
3863 struct tog_blame *blame = &s->blame;
3864 regmatch_t *regmatch = &view->regmatch;
3865 const struct got_error *err;
3866 int lineno = 0, nprinted = 0;
3871 struct tog_blame_line *blame_line;
3872 struct got_object_id *prev_id = NULL;
3874 struct tog_color *tc;
3876 err = got_object_id_str(&id_str, s->blamed_commit->id);
3881 werase(view->window);
3883 if (asprintf(&line, "commit %s", id_str) == -1) {
3884 err = got_error_from_errno("asprintf");
3889 err = format_line(&wline, &width, line, view->ncols, 0);
3894 if (view_needs_focus_indication(view))
3895 wstandout(view->window);
3896 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3898 wattr_on(view->window,
3899 COLOR_PAIR(tc->colorpair), NULL);
3900 waddwstr(view->window, wline);
3902 wattr_off(view->window,
3903 COLOR_PAIR(tc->colorpair), NULL);
3904 if (view_needs_focus_indication(view))
3905 wstandend(view->window);
3908 if (width < view->ncols - 1)
3909 waddch(view->window, '\n');
3911 if (asprintf(&line, "[%d/%d] %s%s",
3912 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3913 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3915 return got_error_from_errno("asprintf");
3918 err = format_line(&wline, &width, line, view->ncols, 0);
3923 waddwstr(view->window, wline);
3926 if (width < view->ncols - 1)
3927 waddch(view->window, '\n');
3930 while (nprinted < view->nlines - 2) {
3931 line = parse_next_line(blame->f, &len);
3936 if (++lineno < s->first_displayed_line) {
3941 if (view->focussed && nprinted == s->selected_line - 1)
3942 wstandout(view->window);
3944 if (blame->nlines > 0) {
3945 blame_line = &blame->lines[lineno - 1];
3946 if (blame_line->annotated && prev_id &&
3947 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3949 nprinted == s->selected_line - 1)) {
3950 waddstr(view->window, " ");
3951 } else if (blame_line->annotated) {
3953 err = got_object_id_str(&id_str, blame_line->id);
3958 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3960 wattr_on(view->window,
3961 COLOR_PAIR(tc->colorpair), NULL);
3962 wprintw(view->window, "%.8s", id_str);
3964 wattr_off(view->window,
3965 COLOR_PAIR(tc->colorpair), NULL);
3967 prev_id = blame_line->id;
3969 waddstr(view->window, "........");
3973 waddstr(view->window, "........");
3977 if (view->focussed && nprinted == s->selected_line - 1)
3978 wstandend(view->window);
3979 waddstr(view->window, " ");
3981 if (view->ncols <= 9) {
3983 wline = wcsdup(L"");
3984 if (wline == NULL) {
3985 err = got_error_from_errno("wcsdup");
3989 } else if (s->first_displayed_line + nprinted ==
3991 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3992 err = add_matched_line(&width, line, view->ncols - 9, 9,
3993 view->window, regmatch);
4000 err = format_line(&wline, &width, line,
4001 view->ncols - 9, 9);
4002 waddwstr(view->window, wline);
4008 if (width <= view->ncols - 1)
4009 waddch(view->window, '\n');
4010 if (++nprinted == 1)
4011 s->first_displayed_line = lineno;
4014 s->last_displayed_line = lineno;
4021 static const struct got_error *
4022 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4024 const struct got_error *err = NULL;
4025 struct tog_blame_cb_args *a = arg;
4026 struct tog_blame_line *line;
4029 if (nlines != a->nlines ||
4030 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4031 return got_error(GOT_ERR_RANGE);
4033 errcode = pthread_mutex_lock(&tog_mutex);
4035 return got_error_set_errno(errcode, "pthread_mutex_lock");
4037 if (*a->quit) { /* user has quit the blame view */
4038 err = got_error(GOT_ERR_ITER_COMPLETED);
4043 goto done; /* no change in this commit */
4045 line = &a->lines[lineno - 1];
4046 if (line->annotated)
4049 line->id = got_object_id_dup(id);
4050 if (line->id == NULL) {
4051 err = got_error_from_errno("got_object_id_dup");
4054 line->annotated = 1;
4056 errcode = pthread_mutex_unlock(&tog_mutex);
4058 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4063 blame_thread(void *arg)
4065 const struct got_error *err;
4066 struct tog_blame_thread_args *ta = arg;
4067 struct tog_blame_cb_args *a = ta->cb_args;
4070 err = block_signals_used_by_main_thread();
4074 err = got_blame(ta->path, a->commit_id, ta->repo,
4075 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4076 if (err && err->code == GOT_ERR_CANCELLED)
4079 errcode = pthread_mutex_lock(&tog_mutex);
4081 return (void *)got_error_set_errno(errcode,
4082 "pthread_mutex_lock");
4084 got_repo_close(ta->repo);
4088 errcode = pthread_mutex_unlock(&tog_mutex);
4089 if (errcode && err == NULL)
4090 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4095 static struct got_object_id *
4096 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4097 int first_displayed_line, int selected_line)
4099 struct tog_blame_line *line;
4104 line = &lines[first_displayed_line - 1 + selected_line - 1];
4105 if (!line->annotated)
4111 static const struct got_error *
4112 stop_blame(struct tog_blame *blame)
4114 const struct got_error *err = NULL;
4117 if (blame->thread) {
4119 errcode = pthread_mutex_unlock(&tog_mutex);
4121 return got_error_set_errno(errcode,
4122 "pthread_mutex_unlock");
4123 errcode = pthread_join(blame->thread, (void **)&err);
4125 return got_error_set_errno(errcode, "pthread_join");
4126 errcode = pthread_mutex_lock(&tog_mutex);
4128 return got_error_set_errno(errcode,
4129 "pthread_mutex_lock");
4130 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4132 blame->thread = NULL;
4134 if (blame->thread_args.repo) {
4135 got_repo_close(blame->thread_args.repo);
4136 blame->thread_args.repo = NULL;
4139 if (fclose(blame->f) != 0 && err == NULL)
4140 err = got_error_from_errno("fclose");
4144 for (i = 0; i < blame->nlines; i++)
4145 free(blame->lines[i].id);
4147 blame->lines = NULL;
4149 free(blame->cb_args.commit_id);
4150 blame->cb_args.commit_id = NULL;
4155 static const struct got_error *
4156 cancel_blame_view(void *arg)
4158 const struct got_error *err = NULL;
4162 errcode = pthread_mutex_lock(&tog_mutex);
4164 return got_error_set_errno(errcode,
4165 "pthread_mutex_unlock");
4168 err = got_error(GOT_ERR_CANCELLED);
4170 errcode = pthread_mutex_unlock(&tog_mutex);
4172 return got_error_set_errno(errcode,
4173 "pthread_mutex_lock");
4178 static const struct got_error *
4179 run_blame(struct tog_view *view)
4181 struct tog_blame_view_state *s = &view->state.blame;
4182 struct tog_blame *blame = &s->blame;
4183 const struct got_error *err = NULL;
4184 struct got_blob_object *blob = NULL;
4185 struct got_repository *thread_repo = NULL;
4186 struct got_object_id *obj_id = NULL;
4189 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4194 err = got_object_get_type(&obj_type, s->repo, obj_id);
4198 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4199 err = got_error(GOT_ERR_OBJ_TYPE);
4203 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4206 blame->f = got_opentemp();
4207 if (blame->f == NULL) {
4208 err = got_error_from_errno("got_opentemp");
4211 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4212 &blame->line_offsets, blame->f, blob);
4213 if (err || blame->nlines == 0)
4216 /* Don't include \n at EOF in the blame line count. */
4217 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4220 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4221 if (blame->lines == NULL) {
4222 err = got_error_from_errno("calloc");
4226 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4230 blame->cb_args.view = view;
4231 blame->cb_args.lines = blame->lines;
4232 blame->cb_args.nlines = blame->nlines;
4233 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4234 if (blame->cb_args.commit_id == NULL) {
4235 err = got_error_from_errno("got_object_id_dup");
4238 blame->cb_args.quit = &s->done;
4240 blame->thread_args.path = s->path;
4241 blame->thread_args.repo = thread_repo;
4242 blame->thread_args.cb_args = &blame->cb_args;
4243 blame->thread_args.complete = &s->blame_complete;
4244 blame->thread_args.cancel_cb = cancel_blame_view;
4245 blame->thread_args.cancel_arg = &s->done;
4246 s->blame_complete = 0;
4250 got_object_blob_close(blob);
4257 static const struct got_error *
4258 open_blame_view(struct tog_view *view, char *path,
4259 struct got_object_id *commit_id, struct got_repository *repo)
4261 const struct got_error *err = NULL;
4262 struct tog_blame_view_state *s = &view->state.blame;
4264 SIMPLEQ_INIT(&s->blamed_commits);
4266 s->path = strdup(path);
4267 if (s->path == NULL)
4268 return got_error_from_errno("strdup");
4270 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4276 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4277 s->first_displayed_line = 1;
4278 s->last_displayed_line = view->nlines;
4279 s->selected_line = 1;
4280 s->blame_complete = 0;
4282 s->commit_id = commit_id;
4283 memset(&s->blame, 0, sizeof(s->blame));
4285 SIMPLEQ_INIT(&s->colors);
4286 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4287 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4288 get_color_value("TOG_COLOR_COMMIT"));
4293 view->show = show_blame_view;
4294 view->input = input_blame_view;
4295 view->close = close_blame_view;
4296 view->search_start = search_start_blame_view;
4297 view->search_next = search_next_blame_view;
4299 return run_blame(view);
4302 static const struct got_error *
4303 close_blame_view(struct tog_view *view)
4305 const struct got_error *err = NULL;
4306 struct tog_blame_view_state *s = &view->state.blame;
4308 if (s->blame.thread)
4309 err = stop_blame(&s->blame);
4311 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4312 struct got_object_qid *blamed_commit;
4313 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4314 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4315 got_object_qid_free(blamed_commit);
4319 free_colors(&s->colors);
4324 static const struct got_error *
4325 search_start_blame_view(struct tog_view *view)
4327 struct tog_blame_view_state *s = &view->state.blame;
4329 s->matched_line = 0;
4333 static const struct got_error *
4334 search_next_blame_view(struct tog_view *view)
4336 struct tog_blame_view_state *s = &view->state.blame;
4339 if (!view->searching) {
4340 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4344 if (s->matched_line) {
4345 if (view->searching == TOG_SEARCH_FORWARD)
4346 lineno = s->matched_line + 1;
4348 lineno = s->matched_line - 1;
4350 if (view->searching == TOG_SEARCH_FORWARD)
4353 lineno = s->blame.nlines;
4361 if (lineno <= 0 || lineno > s->blame.nlines) {
4362 if (s->matched_line == 0) {
4363 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4368 if (view->searching == TOG_SEARCH_FORWARD)
4371 lineno = s->blame.nlines;
4374 offset = s->blame.line_offsets[lineno - 1];
4375 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4377 return got_error_from_errno("fseeko");
4380 line = parse_next_line(s->blame.f, &len);
4382 match_line(line, &view->regex, 1, &view->regmatch)) {
4383 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4384 s->matched_line = lineno;
4389 if (view->searching == TOG_SEARCH_FORWARD)
4395 if (s->matched_line) {
4396 s->first_displayed_line = s->matched_line;
4397 s->selected_line = 1;
4403 static const struct got_error *
4404 show_blame_view(struct tog_view *view)
4406 const struct got_error *err = NULL;
4407 struct tog_blame_view_state *s = &view->state.blame;
4410 if (s->blame.thread == NULL) {
4411 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4412 &s->blame.thread_args);
4414 return got_error_set_errno(errcode, "pthread_create");
4416 halfdelay(1); /* fast refresh while annotating */
4419 if (s->blame_complete)
4420 halfdelay(10); /* disable fast refresh */
4422 err = draw_blame(view);
4428 static const struct got_error *
4429 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4431 const struct got_error *err = NULL, *thread_err = NULL;
4432 struct tog_view *diff_view;
4433 struct tog_blame_view_state *s = &view->state.blame;
4442 if (s->selected_line > 1)
4444 else if (s->selected_line == 1 &&
4445 s->first_displayed_line > 1)
4446 s->first_displayed_line--;
4450 if (s->first_displayed_line == 1) {
4451 s->selected_line = 1;
4454 if (s->first_displayed_line > view->nlines - 2)
4455 s->first_displayed_line -=
4458 s->first_displayed_line = 1;
4462 if (s->selected_line < view->nlines - 2 &&
4463 s->first_displayed_line +
4464 s->selected_line <= s->blame.nlines)
4466 else if (s->last_displayed_line <
4468 s->first_displayed_line++;
4472 struct got_object_id *id = NULL;
4473 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4474 s->first_displayed_line, s->selected_line);
4478 struct got_commit_object *commit;
4479 struct got_object_qid *pid;
4480 struct got_object_id *blob_id = NULL;
4482 err = got_object_open_as_commit(&commit,
4486 pid = SIMPLEQ_FIRST(
4487 got_object_commit_get_parent_ids(commit));
4489 got_object_commit_close(commit);
4492 /* Check if path history ends here. */
4493 err = got_object_id_by_path(&blob_id, s->repo,
4496 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4498 got_object_commit_close(commit);
4501 err = got_object_get_type(&obj_type, s->repo,
4504 /* Can't blame non-blob type objects. */
4505 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4506 got_object_commit_close(commit);
4509 err = got_object_qid_alloc(&s->blamed_commit,
4511 got_object_commit_close(commit);
4513 if (got_object_id_cmp(id,
4514 s->blamed_commit->id) == 0)
4516 err = got_object_qid_alloc(&s->blamed_commit,
4522 thread_err = stop_blame(&s->blame);
4526 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4527 s->blamed_commit, entry);
4528 err = run_blame(view);
4534 struct got_object_qid *first;
4535 first = SIMPLEQ_FIRST(&s->blamed_commits);
4536 if (!got_object_id_cmp(first->id, s->commit_id))
4539 thread_err = stop_blame(&s->blame);
4543 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4544 got_object_qid_free(s->blamed_commit);
4546 SIMPLEQ_FIRST(&s->blamed_commits);
4547 err = run_blame(view);
4554 struct got_object_id *id = NULL;
4555 struct got_object_qid *pid;
4556 struct got_commit_object *commit = NULL;
4557 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4558 s->first_displayed_line, s->selected_line);
4561 err = got_object_open_as_commit(&commit, s->repo, id);
4564 pid = SIMPLEQ_FIRST(
4565 got_object_commit_get_parent_ids(commit));
4566 if (view_is_parent_view(view))
4567 begin_x = view_split_begin_x(view->begin_x);
4568 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4569 if (diff_view == NULL) {
4570 got_object_commit_close(commit);
4571 err = got_error_from_errno("view_open");
4574 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4575 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4576 got_object_commit_close(commit);
4578 view_close(diff_view);
4582 diff_view->focussed = 1;
4583 if (view_is_parent_view(view)) {
4584 err = view_close_child(view);
4587 view_set_child(view, diff_view);
4588 view->focus_child = 1;
4590 *new_view = diff_view;
4598 if (s->last_displayed_line >= s->blame.nlines &&
4599 s->selected_line >= MIN(s->blame.nlines,
4600 view->nlines - 2)) {
4603 if (s->last_displayed_line >= s->blame.nlines &&
4604 s->selected_line < view->nlines - 2) {
4605 s->selected_line = MIN(s->blame.nlines,
4609 if (s->last_displayed_line + view->nlines - 2
4611 s->first_displayed_line +=
4614 s->first_displayed_line =
4619 if (s->selected_line > view->nlines - 2) {
4620 s->selected_line = MIN(s->blame.nlines,
4627 return thread_err ? thread_err : err;
4630 static const struct got_error *
4631 cmd_blame(int argc, char *argv[])
4633 const struct got_error *error;
4634 struct got_repository *repo = NULL;
4635 struct got_worktree *worktree = NULL;
4636 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4637 char *link_target = NULL;
4638 struct got_object_id *commit_id = NULL;
4639 char *commit_id_str = NULL;
4641 struct tog_view *view;
4643 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4646 commit_id_str = optarg;
4649 repo_path = realpath(optarg, NULL);
4650 if (repo_path == NULL)
4651 return got_error_from_errno2("realpath",
4666 cwd = getcwd(NULL, 0);
4668 return got_error_from_errno("getcwd");
4670 error = got_worktree_open(&worktree, cwd);
4671 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4674 if (repo_path == NULL) {
4677 strdup(got_worktree_get_repo_path(worktree));
4679 repo_path = strdup(cwd);
4681 if (repo_path == NULL) {
4682 error = got_error_from_errno("strdup");
4686 error = got_repo_open(&repo, repo_path, NULL);
4690 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4697 error = apply_unveil(got_repo_get_path(repo), NULL);
4701 if (commit_id_str == NULL) {
4702 struct got_reference *head_ref;
4703 error = got_ref_open(&head_ref, repo, worktree ?
4704 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4707 error = got_ref_resolve(&commit_id, repo, head_ref);
4708 got_ref_close(head_ref);
4710 error = got_repo_match_object_id(&commit_id, NULL,
4711 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4716 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4718 error = got_error_from_errno("view_open");
4722 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4727 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4732 /* Release work tree lock. */
4733 got_worktree_close(worktree);
4736 error = view_loop(view);
4744 got_worktree_close(worktree);
4746 got_repo_close(repo);
4750 static const struct got_error *
4751 draw_tree_entries(struct tog_view *view, const char *parent_path)
4753 struct tog_tree_view_state *s = &view->state.tree;
4754 const struct got_error *err = NULL;
4755 struct got_tree_entry *te;
4757 struct tog_color *tc;
4758 int width, n, i, nentries;
4759 int limit = view->nlines;
4763 werase(view->window);
4768 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4771 if (view_needs_focus_indication(view))
4772 wstandout(view->window);
4773 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4775 wattr_on(view->window,
4776 COLOR_PAIR(tc->colorpair), NULL);
4777 waddwstr(view->window, wline);
4779 wattr_off(view->window,
4780 COLOR_PAIR(tc->colorpair), NULL);
4781 if (view_needs_focus_indication(view))
4782 wstandend(view->window);
4785 if (width < view->ncols - 1)
4786 waddch(view->window, '\n');
4789 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4792 waddwstr(view->window, wline);
4795 if (width < view->ncols - 1)
4796 waddch(view->window, '\n');
4799 waddch(view->window, '\n');
4803 if (s->first_displayed_entry == NULL) {
4804 te = got_object_tree_get_first_entry(s->tree);
4805 if (s->selected == 0) {
4807 wstandout(view->window);
4808 s->selected_entry = NULL;
4810 waddstr(view->window, " ..\n"); /* parent directory */
4811 if (s->selected == 0 && view->focussed)
4812 wstandend(view->window);
4819 te = s->first_displayed_entry;
4822 nentries = got_object_tree_get_nentries(s->tree);
4823 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4824 char *line = NULL, *id_str = NULL, *link_target = NULL;
4825 const char *modestr = "";
4828 te = got_object_tree_get_entry(s->tree, i);
4829 mode = got_tree_entry_get_mode(te);
4832 err = got_object_id_str(&id_str,
4833 got_tree_entry_get_id(te));
4835 return got_error_from_errno(
4836 "got_object_id_str");
4838 if (got_object_tree_entry_is_submodule(te))
4840 else if (S_ISLNK(mode)) {
4843 err = got_tree_entry_get_symlink_target(&link_target,
4849 for (i = 0; i < strlen(link_target); i++) {
4850 if (!isprint((unsigned char)link_target[i]))
4851 link_target[i] = '?';
4855 else if (S_ISDIR(mode))
4857 else if (mode & S_IXUSR)
4859 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4860 got_tree_entry_get_name(te), modestr,
4861 link_target ? " -> ": "",
4862 link_target ? link_target : "") == -1) {
4865 return got_error_from_errno("asprintf");
4869 err = format_line(&wline, &width, line, view->ncols, 0);
4874 if (n == s->selected) {
4876 wstandout(view->window);
4877 s->selected_entry = te;
4879 tc = match_color(&s->colors, line);
4881 wattr_on(view->window,
4882 COLOR_PAIR(tc->colorpair), NULL);
4883 waddwstr(view->window, wline);
4885 wattr_off(view->window,
4886 COLOR_PAIR(tc->colorpair), NULL);
4887 if (width < view->ncols - 1)
4888 waddch(view->window, '\n');
4889 if (n == s->selected && view->focussed)
4890 wstandend(view->window);
4896 s->last_displayed_entry = te;
4905 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4907 struct got_tree_entry *te;
4908 int isroot = s->tree == s->root;
4911 if (s->first_displayed_entry == NULL)
4914 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4915 while (i++ < maxscroll) {
4918 s->first_displayed_entry = NULL;
4921 s->first_displayed_entry = te;
4922 te = got_tree_entry_get_prev(s->tree, te);
4927 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4929 struct got_tree_entry *next, *last;
4932 if (s->first_displayed_entry)
4933 next = got_tree_entry_get_next(s->tree,
4934 s->first_displayed_entry);
4936 next = got_object_tree_get_first_entry(s->tree);
4938 last = s->last_displayed_entry;
4939 while (next && last && n++ < maxscroll) {
4940 last = got_tree_entry_get_next(s->tree, last);
4942 s->first_displayed_entry = next;
4943 next = got_tree_entry_get_next(s->tree, next);
4948 static const struct got_error *
4949 tree_entry_path(char **path, struct tog_parent_trees *parents,
4950 struct got_tree_entry *te)
4952 const struct got_error *err = NULL;
4953 struct tog_parent_tree *pt;
4954 size_t len = 2; /* for leading slash and NUL */
4956 TAILQ_FOREACH(pt, parents, entry)
4957 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4960 len += strlen(got_tree_entry_get_name(te));
4962 *path = calloc(1, len);
4964 return got_error_from_errno("calloc");
4967 pt = TAILQ_LAST(parents, tog_parent_trees);
4969 const char *name = got_tree_entry_get_name(pt->selected_entry);
4970 if (strlcat(*path, name, len) >= len) {
4971 err = got_error(GOT_ERR_NO_SPACE);
4974 if (strlcat(*path, "/", len) >= len) {
4975 err = got_error(GOT_ERR_NO_SPACE);
4978 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4981 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4982 err = got_error(GOT_ERR_NO_SPACE);
4994 static const struct got_error *
4995 blame_tree_entry(struct tog_view **new_view, int begin_x,
4996 struct got_tree_entry *te, struct tog_parent_trees *parents,
4997 struct got_object_id *commit_id, struct got_repository *repo)
4999 const struct got_error *err = NULL;
5001 struct tog_view *blame_view;
5005 err = tree_entry_path(&path, parents, te);
5009 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5010 if (blame_view == NULL) {
5011 err = got_error_from_errno("view_open");
5015 err = open_blame_view(blame_view, path, commit_id, repo);
5017 if (err->code == GOT_ERR_CANCELLED)
5019 view_close(blame_view);
5021 *new_view = blame_view;
5027 static const struct got_error *
5028 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5029 struct tog_tree_view_state *s)
5031 struct tog_view *log_view;
5032 const struct got_error *err = NULL;
5037 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5038 if (log_view == NULL)
5039 return got_error_from_errno("view_open");
5041 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5045 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5048 view_close(log_view);
5050 *new_view = log_view;
5055 static const struct got_error *
5056 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5057 struct got_object_id *commit_id, const char *head_ref_name,
5058 struct got_repository *repo)
5060 const struct got_error *err = NULL;
5061 char *commit_id_str = NULL;
5062 struct tog_tree_view_state *s = &view->state.tree;
5064 TAILQ_INIT(&s->parents);
5066 err = got_object_id_str(&commit_id_str, commit_id);
5070 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5071 err = got_error_from_errno("asprintf");
5075 s->root = s->tree = root;
5076 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5077 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5078 s->commit_id = got_object_id_dup(commit_id);
5079 if (s->commit_id == NULL) {
5080 err = got_error_from_errno("got_object_id_dup");
5083 if (head_ref_name) {
5084 s->head_ref_name = strdup(head_ref_name);
5085 if (s->head_ref_name == NULL) {
5086 err = got_error_from_errno("strdup");
5092 SIMPLEQ_INIT(&s->colors);
5094 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5095 err = add_color(&s->colors, "\\$$",
5096 TOG_COLOR_TREE_SUBMODULE,
5097 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5100 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5101 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5103 free_colors(&s->colors);
5106 err = add_color(&s->colors, "/$",
5107 TOG_COLOR_TREE_DIRECTORY,
5108 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5110 free_colors(&s->colors);
5114 err = add_color(&s->colors, "\\*$",
5115 TOG_COLOR_TREE_EXECUTABLE,
5116 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5118 free_colors(&s->colors);
5122 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5123 get_color_value("TOG_COLOR_COMMIT"));
5125 free_colors(&s->colors);
5130 view->show = show_tree_view;
5131 view->input = input_tree_view;
5132 view->close = close_tree_view;
5133 view->search_start = search_start_tree_view;
5134 view->search_next = search_next_tree_view;
5136 free(commit_id_str);
5138 free(s->tree_label);
5139 s->tree_label = NULL;
5144 static const struct got_error *
5145 close_tree_view(struct tog_view *view)
5147 struct tog_tree_view_state *s = &view->state.tree;
5149 free_colors(&s->colors);
5150 free(s->tree_label);
5151 s->tree_label = NULL;
5153 s->commit_id = NULL;
5154 free(s->head_ref_name);
5155 s->head_ref_name = NULL;
5156 while (!TAILQ_EMPTY(&s->parents)) {
5157 struct tog_parent_tree *parent;
5158 parent = TAILQ_FIRST(&s->parents);
5159 TAILQ_REMOVE(&s->parents, parent, entry);
5163 if (s->tree != s->root)
5164 got_object_tree_close(s->tree);
5165 got_object_tree_close(s->root);
5169 static const struct got_error *
5170 search_start_tree_view(struct tog_view *view)
5172 struct tog_tree_view_state *s = &view->state.tree;
5174 s->matched_entry = NULL;
5179 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5181 regmatch_t regmatch;
5183 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5187 static const struct got_error *
5188 search_next_tree_view(struct tog_view *view)
5190 struct tog_tree_view_state *s = &view->state.tree;
5191 struct got_tree_entry *te = NULL;
5193 if (!view->searching) {
5194 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5198 if (s->matched_entry) {
5199 if (view->searching == TOG_SEARCH_FORWARD) {
5200 if (s->selected_entry)
5201 te = got_tree_entry_get_next(s->tree,
5204 te = got_object_tree_get_first_entry(s->tree);
5206 if (s->selected_entry == NULL)
5207 te = got_object_tree_get_last_entry(s->tree);
5209 te = got_tree_entry_get_prev(s->tree,
5213 if (view->searching == TOG_SEARCH_FORWARD)
5214 te = got_object_tree_get_first_entry(s->tree);
5216 te = got_object_tree_get_last_entry(s->tree);
5221 if (s->matched_entry == NULL) {
5222 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5225 if (view->searching == TOG_SEARCH_FORWARD)
5226 te = got_object_tree_get_first_entry(s->tree);
5228 te = got_object_tree_get_last_entry(s->tree);
5231 if (match_tree_entry(te, &view->regex)) {
5232 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5233 s->matched_entry = te;
5237 if (view->searching == TOG_SEARCH_FORWARD)
5238 te = got_tree_entry_get_next(s->tree, te);
5240 te = got_tree_entry_get_prev(s->tree, te);
5243 if (s->matched_entry) {
5244 s->first_displayed_entry = s->matched_entry;
5251 static const struct got_error *
5252 show_tree_view(struct tog_view *view)
5254 const struct got_error *err = NULL;
5255 struct tog_tree_view_state *s = &view->state.tree;
5258 err = tree_entry_path(&parent_path, &s->parents, NULL);
5262 err = draw_tree_entries(view, parent_path);
5269 static const struct got_error *
5270 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5272 const struct got_error *err = NULL;
5273 struct tog_tree_view_state *s = &view->state.tree;
5274 struct tog_view *log_view, *ref_view;
5279 s->show_ids = !s->show_ids;
5282 if (!s->selected_entry)
5284 if (view_is_parent_view(view))
5285 begin_x = view_split_begin_x(view->begin_x);
5286 err = log_selected_tree_entry(&log_view, begin_x, s);
5288 log_view->focussed = 1;
5289 if (view_is_parent_view(view)) {
5290 err = view_close_child(view);
5293 view_set_child(view, log_view);
5294 view->focus_child = 1;
5296 *new_view = log_view;
5299 if (view_is_parent_view(view))
5300 begin_x = view_split_begin_x(view->begin_x);
5301 ref_view = view_open(view->nlines, view->ncols,
5302 view->begin_y, begin_x, TOG_VIEW_REF);
5303 if (ref_view == NULL)
5304 return got_error_from_errno("view_open");
5305 err = open_ref_view(ref_view, s->repo);
5307 view_close(ref_view);
5311 ref_view->focussed = 1;
5312 if (view_is_parent_view(view)) {
5313 err = view_close_child(view);
5316 view_set_child(view, ref_view);
5317 view->focus_child = 1;
5319 *new_view = ref_view;
5323 if (s->selected > 0) {
5327 tree_scroll_up(s, 1);
5331 if (s->tree == s->root) {
5332 if (got_object_tree_get_first_entry(s->tree) ==
5333 s->first_displayed_entry)
5336 if (s->first_displayed_entry == NULL)
5339 tree_scroll_up(s, MAX(0, view->nlines - 3));
5343 if (s->selected < s->ndisplayed - 1) {
5347 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5349 /* can't scroll any further */
5351 tree_scroll_down(s, 1);
5355 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5357 /* can't scroll any further; move cursor down */
5358 if (s->selected < s->ndisplayed - 1)
5359 s->selected = s->ndisplayed - 1;
5362 tree_scroll_down(s, view->nlines - 3);
5367 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5368 struct tog_parent_tree *parent;
5369 /* user selected '..' */
5370 if (s->tree == s->root)
5372 parent = TAILQ_FIRST(&s->parents);
5373 TAILQ_REMOVE(&s->parents, parent,
5375 got_object_tree_close(s->tree);
5376 s->tree = parent->tree;
5377 s->first_displayed_entry =
5378 parent->first_displayed_entry;
5380 parent->selected_entry;
5381 s->selected = parent->selected;
5383 } else if (S_ISDIR(got_tree_entry_get_mode(
5384 s->selected_entry))) {
5385 struct got_tree_object *subtree;
5386 err = got_object_open_as_tree(&subtree, s->repo,
5387 got_tree_entry_get_id(s->selected_entry));
5390 err = tree_view_visit_subtree(s, subtree);
5392 got_object_tree_close(subtree);
5395 } else if (S_ISREG(got_tree_entry_get_mode(
5396 s->selected_entry))) {
5397 struct tog_view *blame_view;
5398 int begin_x = view_is_parent_view(view) ?
5399 view_split_begin_x(view->begin_x) : 0;
5401 err = blame_tree_entry(&blame_view, begin_x,
5402 s->selected_entry, &s->parents,
5403 s->commit_id, s->repo);
5407 blame_view->focussed = 1;
5408 if (view_is_parent_view(view)) {
5409 err = view_close_child(view);
5412 view_set_child(view, blame_view);
5413 view->focus_child = 1;
5415 *new_view = blame_view;
5419 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5420 s->selected = view->nlines - 4;
5433 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5438 static const struct got_error *
5439 cmd_tree(int argc, char *argv[])
5441 const struct got_error *error;
5442 struct got_repository *repo = NULL;
5443 struct got_worktree *worktree = NULL;
5444 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5445 struct got_object_id *commit_id = NULL;
5446 const char *commit_id_arg = NULL;
5448 struct got_commit_object *commit = NULL;
5449 struct got_tree_object *tree = NULL;
5450 struct got_reference *ref = NULL;
5451 const char *head_ref_name = NULL;
5453 struct tog_view *view;
5455 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5458 commit_id_arg = optarg;
5461 repo_path = realpath(optarg, NULL);
5462 if (repo_path == NULL)
5463 return got_error_from_errno2("realpath",
5478 cwd = getcwd(NULL, 0);
5480 return got_error_from_errno("getcwd");
5482 error = got_worktree_open(&worktree, cwd);
5483 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5486 if (repo_path == NULL) {
5489 strdup(got_worktree_get_repo_path(worktree));
5491 repo_path = strdup(cwd);
5493 if (repo_path == NULL) {
5494 error = got_error_from_errno("strdup");
5498 error = got_repo_open(&repo, repo_path, NULL);
5502 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5509 error = apply_unveil(got_repo_get_path(repo), NULL);
5513 if (commit_id_arg == NULL) {
5514 error = got_repo_match_object_id(&commit_id, &label,
5515 worktree ? got_worktree_get_head_ref_name(worktree) :
5516 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5519 head_ref_name = label;
5521 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5523 head_ref_name = got_ref_get_name(ref);
5524 else if (error->code != GOT_ERR_NOT_REF)
5526 error = got_repo_match_object_id(&commit_id, NULL,
5527 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5532 error = got_object_open_as_commit(&commit, repo, commit_id);
5536 error = got_object_open_as_tree(&tree, repo,
5537 got_object_commit_get_tree_id(commit));
5541 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5543 error = got_error_from_errno("view_open");
5546 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5549 if (!got_path_is_root_dir(in_repo_path)) {
5550 error = tree_view_walk_path(&view->state.tree, commit_id,
5557 /* Release work tree lock. */
5558 got_worktree_close(worktree);
5561 error = view_loop(view);
5570 got_object_commit_close(commit);
5572 got_object_tree_close(tree);
5574 got_repo_close(repo);
5578 static const struct got_error *
5579 ref_view_load_refs(struct tog_ref_view_state *s)
5581 const struct got_error *err;
5582 struct got_reflist_entry *sre;
5583 struct tog_reflist_entry *re;
5585 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5586 got_ref_cmp_by_name, NULL);
5591 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5592 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5595 re = malloc(sizeof(*re));
5597 return got_error_from_errno("malloc");
5600 re->idx = s->nrefs++;
5601 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5608 ref_view_free_refs(struct tog_ref_view_state *s)
5610 struct tog_reflist_entry *re;
5612 while (!TAILQ_EMPTY(&s->refs)) {
5613 re = TAILQ_FIRST(&s->refs);
5614 TAILQ_REMOVE(&s->refs, re, entry);
5617 got_ref_list_free(&s->simplerefs);
5620 static const struct got_error *
5621 open_ref_view(struct tog_view *view, struct got_repository *repo)
5623 const struct got_error *err = NULL;
5624 struct tog_ref_view_state *s = &view->state.ref;
5626 s->selected_entry = 0;
5629 SIMPLEQ_INIT(&s->simplerefs);
5630 TAILQ_INIT(&s->refs);
5631 SIMPLEQ_INIT(&s->colors);
5633 err = ref_view_load_refs(s);
5637 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5639 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5640 err = add_color(&s->colors, "^refs/heads/",
5641 TOG_COLOR_REFS_HEADS,
5642 get_color_value("TOG_COLOR_REFS_HEADS"));
5646 err = add_color(&s->colors, "^refs/tags/",
5647 TOG_COLOR_REFS_TAGS,
5648 get_color_value("TOG_COLOR_REFS_TAGS"));
5652 err = add_color(&s->colors, "^refs/remotes/",
5653 TOG_COLOR_REFS_REMOTES,
5654 get_color_value("TOG_COLOR_REFS_REMOTES"));
5659 view->show = show_ref_view;
5660 view->input = input_ref_view;
5661 view->close = close_ref_view;
5662 view->search_start = search_start_ref_view;
5663 view->search_next = search_next_ref_view;
5666 free_colors(&s->colors);
5670 static const struct got_error *
5671 close_ref_view(struct tog_view *view)
5673 struct tog_ref_view_state *s = &view->state.ref;
5675 ref_view_free_refs(s);
5676 free_colors(&s->colors);
5681 static const struct got_error *
5682 resolve_reflist_entry(struct got_object_id **commit_id,
5683 struct tog_reflist_entry *re, struct got_repository *repo)
5685 const struct got_error *err = NULL;
5686 struct got_object_id *obj_id;
5687 struct got_tag_object *tag = NULL;
5692 err = got_ref_resolve(&obj_id, repo, re->ref);
5696 err = got_object_get_type(&obj_type, repo, obj_id);
5701 case GOT_OBJ_TYPE_COMMIT:
5702 *commit_id = obj_id;
5704 case GOT_OBJ_TYPE_TAG:
5705 err = got_object_open_as_tag(&tag, repo, obj_id);
5709 err = got_object_get_type(&obj_type, repo,
5710 got_object_tag_get_object_id(tag));
5713 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5714 err = got_error(GOT_ERR_OBJ_TYPE);
5717 *commit_id = got_object_id_dup(
5718 got_object_tag_get_object_id(tag));
5719 if (*commit_id == NULL) {
5720 err = got_error_from_errno("got_object_id_dup");
5725 err = got_error(GOT_ERR_OBJ_TYPE);
5731 got_object_tag_close(tag);
5739 static const struct got_error *
5740 log_ref_entry(struct tog_view **new_view, int begin_x,
5741 struct tog_reflist_entry *re, struct got_repository *repo)
5743 struct tog_view *log_view;
5744 const struct got_error *err = NULL;
5745 struct got_object_id *commit_id = NULL;
5749 err = resolve_reflist_entry(&commit_id, re, repo);
5751 if (err->code != GOT_ERR_OBJ_TYPE)
5757 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5758 if (log_view == NULL) {
5759 err = got_error_from_errno("view_open");
5763 err = open_log_view(log_view, commit_id, repo,
5764 got_ref_get_name(re->ref), "", 0);
5767 view_close(log_view);
5769 *new_view = log_view;
5775 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5777 struct tog_reflist_entry *re;
5780 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5783 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5784 while (i++ < maxscroll) {
5787 s->first_displayed_entry = re;
5788 re = TAILQ_PREV(re, tog_reflist_head, entry);
5793 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5795 struct tog_reflist_entry *next, *last;
5798 if (s->first_displayed_entry)
5799 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5801 next = TAILQ_FIRST(&s->refs);
5803 last = s->last_displayed_entry;
5804 while (next && last && n++ < maxscroll) {
5805 last = TAILQ_NEXT(last, entry);
5807 s->first_displayed_entry = next;
5808 next = TAILQ_NEXT(next, entry);
5813 static const struct got_error *
5814 search_start_ref_view(struct tog_view *view)
5816 struct tog_ref_view_state *s = &view->state.ref;
5818 s->matched_entry = NULL;
5823 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5825 regmatch_t regmatch;
5827 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5831 static const struct got_error *
5832 search_next_ref_view(struct tog_view *view)
5834 struct tog_ref_view_state *s = &view->state.ref;
5835 struct tog_reflist_entry *re = NULL;
5837 if (!view->searching) {
5838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5842 if (s->matched_entry) {
5843 if (view->searching == TOG_SEARCH_FORWARD) {
5844 if (s->selected_entry)
5845 re = TAILQ_NEXT(s->selected_entry, entry);
5847 re = TAILQ_PREV(s->selected_entry,
5848 tog_reflist_head, entry);
5850 if (s->selected_entry == NULL)
5851 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5853 re = TAILQ_PREV(s->selected_entry,
5854 tog_reflist_head, entry);
5857 if (view->searching == TOG_SEARCH_FORWARD)
5858 re = TAILQ_FIRST(&s->refs);
5860 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5865 if (s->matched_entry == NULL) {
5866 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5869 if (view->searching == TOG_SEARCH_FORWARD)
5870 re = TAILQ_FIRST(&s->refs);
5872 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5875 if (match_reflist_entry(re, &view->regex)) {
5876 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5877 s->matched_entry = re;
5881 if (view->searching == TOG_SEARCH_FORWARD)
5882 re = TAILQ_NEXT(re, entry);
5884 re = TAILQ_PREV(re, tog_reflist_head, entry);
5887 if (s->matched_entry) {
5888 s->first_displayed_entry = s->matched_entry;
5895 static const struct got_error *
5896 show_ref_view(struct tog_view *view)
5898 const struct got_error *err = NULL;
5899 struct tog_ref_view_state *s = &view->state.ref;
5900 struct tog_reflist_entry *re;
5903 struct tog_color *tc;
5905 int limit = view->nlines;
5907 werase(view->window);
5914 re = s->first_displayed_entry;
5916 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5918 return got_error_from_errno("asprintf");
5920 err = format_line(&wline, &width, line, view->ncols, 0);
5925 if (view_needs_focus_indication(view))
5926 wstandout(view->window);
5927 waddwstr(view->window, wline);
5928 if (view_needs_focus_indication(view))
5929 wstandend(view->window);
5934 if (width < view->ncols - 1)
5935 waddch(view->window, '\n');
5940 while (re && limit > 0) {
5943 if (got_ref_is_symbolic(re->ref)) {
5944 if (asprintf(&line, "%s -> %s",
5945 got_ref_get_name(re->ref),
5946 got_ref_get_symref_target(re->ref)) == -1)
5947 return got_error_from_errno("asprintf");
5948 } else if (s->show_ids) {
5949 struct got_object_id *id;
5951 err = got_ref_resolve(&id, s->repo, re->ref);
5954 err = got_object_id_str(&id_str, id);
5959 if (asprintf(&line, "%s: %s",
5960 got_ref_get_name(re->ref), id_str) == -1) {
5961 err = got_error_from_errno("asprintf");
5969 line = strdup(got_ref_get_name(re->ref));
5971 return got_error_from_errno("strdup");
5974 err = format_line(&wline, &width, line, view->ncols, 0);
5979 if (n == s->selected) {
5981 wstandout(view->window);
5982 s->selected_entry = re;
5984 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5986 wattr_on(view->window,
5987 COLOR_PAIR(tc->colorpair), NULL);
5988 waddwstr(view->window, wline);
5990 wattr_off(view->window,
5991 COLOR_PAIR(tc->colorpair), NULL);
5992 if (width < view->ncols - 1)
5993 waddch(view->window, '\n');
5994 if (n == s->selected && view->focussed)
5995 wstandend(view->window);
6001 s->last_displayed_entry = re;
6004 re = TAILQ_NEXT(re, entry);
6011 static const struct got_error *
6012 browse_ref_tree(struct tog_view **new_view, int begin_x,
6013 struct tog_reflist_entry *re, struct got_repository *repo)
6015 const struct got_error *err = NULL;
6016 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6017 struct got_tree_object *tree = NULL;
6018 struct tog_view *tree_view;
6022 err = resolve_reflist_entry(&commit_id, re, repo);
6024 if (err->code != GOT_ERR_OBJ_TYPE)
6030 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6034 err = got_object_open_as_tree(&tree, repo, tree_id);
6038 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6039 if (tree_view == NULL) {
6040 err = got_error_from_errno("view_open");
6044 err = open_tree_view(tree_view, tree, commit_id,
6045 got_ref_get_name(re->ref), repo);
6049 *new_view = tree_view;
6055 got_object_tree_close(tree);
6059 static const struct got_error *
6060 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6062 const struct got_error *err = NULL;
6063 struct tog_ref_view_state *s = &view->state.ref;
6064 struct tog_view *log_view, *tree_view;
6069 s->show_ids = !s->show_ids;
6073 if (!s->selected_entry)
6075 if (view_is_parent_view(view))
6076 begin_x = view_split_begin_x(view->begin_x);
6077 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6080 log_view->focussed = 1;
6081 if (view_is_parent_view(view)) {
6082 err = view_close_child(view);
6085 view_set_child(view, log_view);
6086 view->focus_child = 1;
6088 *new_view = log_view;
6091 if (!s->selected_entry)
6093 if (view_is_parent_view(view))
6094 begin_x = view_split_begin_x(view->begin_x);
6095 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6097 if (err || tree_view == NULL)
6100 tree_view->focussed = 1;
6101 if (view_is_parent_view(view)) {
6102 err = view_close_child(view);
6105 view_set_child(view, tree_view);
6106 view->focus_child = 1;
6108 *new_view = tree_view;
6112 if (s->selected > 0) {
6116 ref_scroll_up(s, 1);
6120 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6122 ref_scroll_up(s, MAX(0, view->nlines - 1));
6126 if (s->selected < s->ndisplayed - 1) {
6130 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6131 /* can't scroll any further */
6133 ref_scroll_down(s, 1);
6137 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6138 /* can't scroll any further; move cursor down */
6139 if (s->selected < s->ndisplayed - 1)
6140 s->selected = s->ndisplayed - 1;
6143 ref_scroll_down(s, view->nlines - 1);
6146 ref_view_free_refs(s);
6147 err = ref_view_load_refs(s);
6150 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6151 s->selected = view->nlines - 2;
6164 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6169 static const struct got_error *
6170 cmd_ref(int argc, char *argv[])
6172 const struct got_error *error;
6173 struct got_repository *repo = NULL;
6174 struct got_worktree *worktree = NULL;
6175 char *cwd = NULL, *repo_path = NULL;
6177 struct tog_view *view;
6179 while ((ch = getopt(argc, argv, "r:")) != -1) {
6182 repo_path = realpath(optarg, NULL);
6183 if (repo_path == NULL)
6184 return got_error_from_errno2("realpath",
6199 cwd = getcwd(NULL, 0);
6201 return got_error_from_errno("getcwd");
6203 error = got_worktree_open(&worktree, cwd);
6204 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6207 if (repo_path == NULL) {
6210 strdup(got_worktree_get_repo_path(worktree));
6212 repo_path = strdup(cwd);
6214 if (repo_path == NULL) {
6215 error = got_error_from_errno("strdup");
6219 error = got_repo_open(&repo, repo_path, NULL);
6225 error = apply_unveil(got_repo_get_path(repo), NULL);
6229 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6231 error = got_error_from_errno("view_open");
6235 error = open_ref_view(view, repo);
6240 /* Release work tree lock. */
6241 got_worktree_close(worktree);
6244 error = view_loop(view);
6249 got_repo_close(repo);
6254 list_commands(FILE *fp)
6258 fprintf(fp, "commands:");
6259 for (i = 0; i < nitems(tog_commands); i++) {
6260 struct tog_cmd *cmd = &tog_commands[i];
6261 fprintf(fp, " %s", cmd->name);
6267 usage(int hflag, int status)
6269 FILE *fp = (status == 0) ? stdout : stderr;
6271 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6274 fprintf(fp, "lazy usage: %s path\n", getprogname());
6281 make_argv(int argc, ...)
6289 argv = calloc(argc, sizeof(char *));
6292 for (i = 0; i < argc; i++) {
6293 argv[i] = strdup(va_arg(ap, char *));
6294 if (argv[i] == NULL)
6303 * Try to convert 'tog path' into a 'tog log path' command.
6304 * The user could simply have mistyped the command rather than knowingly
6305 * provided a path. So check whether argv[0] can in fact be resolved
6306 * to a path in the HEAD commit and print a special error if not.
6307 * This hack is for mpi@ <3
6309 static const struct got_error *
6310 tog_log_with_path(int argc, char *argv[])
6312 const struct got_error *error = NULL;
6313 struct tog_cmd *cmd = NULL;
6314 struct got_repository *repo = NULL;
6315 struct got_worktree *worktree = NULL;
6316 struct got_object_id *commit_id = NULL, *id = NULL;
6317 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6318 char *commit_id_str = NULL, **cmd_argv = NULL;
6320 cwd = getcwd(NULL, 0);
6322 return got_error_from_errno("getcwd");
6324 error = got_worktree_open(&worktree, cwd);
6325 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6329 repo_path = strdup(got_worktree_get_repo_path(worktree));
6331 repo_path = strdup(cwd);
6332 if (repo_path == NULL) {
6333 error = got_error_from_errno("strdup");
6337 error = got_repo_open(&repo, repo_path, NULL);
6341 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6346 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6347 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6348 GOT_OBJ_TYPE_COMMIT, 1, repo);
6353 got_worktree_close(worktree);
6357 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6359 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6361 fprintf(stderr, "%s: '%s' is no known command or path\n",
6362 getprogname(), argv[0]);
6367 got_repo_close(repo);
6370 error = got_object_id_str(&commit_id_str, commit_id);
6374 cmd = &tog_commands[0]; /* log */
6376 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6377 error = cmd->cmd_main(argc, cmd_argv);
6380 got_repo_close(repo);
6382 got_worktree_close(worktree);
6384 free(commit_id_str);
6391 for (i = 0; i < argc; i++)
6399 main(int argc, char *argv[])
6401 const struct got_error *error = NULL;
6402 struct tog_cmd *cmd = NULL;
6403 int ch, hflag = 0, Vflag = 0;
6404 char **cmd_argv = NULL;
6405 static struct option longopts[] = {
6406 { "version", no_argument, NULL, 'V' },
6410 setlocale(LC_CTYPE, "");
6412 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6432 got_version_print_str();
6437 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6445 /* Build an argument vector which runs a default command. */
6446 cmd = &tog_commands[0];
6448 cmd_argv = make_argv(argc, cmd->name);
6452 /* Did the user specify a command? */
6453 for (i = 0; i < nitems(tog_commands); i++) {
6454 if (strncmp(tog_commands[i].name, argv[0],
6455 strlen(argv[0])) == 0) {
6456 cmd = &tog_commands[i];
6465 /* No command specified; try log with a path */
6466 error = tog_log_with_path(argc, argv);
6471 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6478 for (i = 0; i < argc; i++)
6483 if (error && error->code != GOT_ERR_CANCELLED)
6484 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);