2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
123 SIMPLEQ_ENTRY(tog_color) entry;
127 SIMPLEQ_HEAD(tog_colors, tog_color);
129 static const struct got_error *
130 add_color(struct tog_colors *colors, const char *pattern,
131 int idx, short color)
133 const struct got_error *err = NULL;
134 struct tog_color *tc;
137 if (idx < 1 || idx > COLOR_PAIRS - 1)
140 init_pair(idx, color, -1);
142 tc = calloc(1, sizeof(*tc));
144 return got_error_from_errno("calloc");
145 regerr = regcomp(&tc->regex, pattern,
146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
148 static char regerr_msg[512];
149 static char err_msg[512];
150 regerror(regerr, &tc->regex, regerr_msg,
152 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
154 err = got_error_msg(GOT_ERR_REGEX, err_msg);
159 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
164 free_colors(struct tog_colors *colors)
166 struct tog_color *tc;
168 while (!SIMPLEQ_EMPTY(colors)) {
169 tc = SIMPLEQ_FIRST(colors);
170 SIMPLEQ_REMOVE_HEAD(colors, entry);
177 get_color(struct tog_colors *colors, int colorpair)
179 struct tog_color *tc = NULL;
181 SIMPLEQ_FOREACH(tc, colors, entry) {
182 if (tc->colorpair == colorpair)
190 default_color_value(const char *envvar)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 return COLOR_MAGENTA;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
196 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
198 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
200 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 return COLOR_MAGENTA;
202 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 return COLOR_MAGENTA;
204 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
206 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
208 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
210 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
212 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
214 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
216 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 return COLOR_MAGENTA;
218 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
225 get_color_value(const char *envvar)
227 const char *val = getenv(envvar);
230 return default_color_value(envvar);
232 if (strcasecmp(val, "black") == 0)
234 if (strcasecmp(val, "red") == 0)
236 if (strcasecmp(val, "green") == 0)
238 if (strcasecmp(val, "yellow") == 0)
240 if (strcasecmp(val, "blue") == 0)
242 if (strcasecmp(val, "magenta") == 0)
243 return COLOR_MAGENTA;
244 if (strcasecmp(val, "cyan") == 0)
246 if (strcasecmp(val, "white") == 0)
248 if (strcasecmp(val, "default") == 0)
251 return default_color_value(envvar);
255 struct tog_diff_view_state {
256 struct got_object_id *id1, *id2;
257 const char *label1, *label2;
259 int first_displayed_line;
260 int last_displayed_line;
263 int ignore_whitespace;
265 struct got_repository *repo;
266 struct got_reflist_head refs;
267 struct tog_colors colors;
273 /* passed from log view; may be NULL */
274 struct tog_view *log_view;
277 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 struct tog_log_thread_args {
280 pthread_cond_t need_commits;
281 pthread_cond_t commit_loaded;
283 struct got_commit_graph *graph;
284 struct commit_queue *commits;
285 const char *in_repo_path;
286 struct got_object_id *start_id;
287 struct got_repository *repo;
290 struct commit_queue_entry **first_displayed_entry;
291 struct commit_queue_entry **selected_entry;
293 int *search_next_done;
297 struct tog_log_view_state {
298 struct commit_queue commits;
299 struct commit_queue_entry *first_displayed_entry;
300 struct commit_queue_entry *last_displayed_entry;
301 struct commit_queue_entry *selected_entry;
304 const char *head_ref_name;
306 struct got_repository *repo;
307 struct got_reflist_head refs;
308 struct got_object_id *start_id;
311 struct tog_log_thread_args thread_args;
312 struct commit_queue_entry *matched_entry;
313 struct commit_queue_entry *search_entry;
314 struct tog_colors colors;
317 #define TOG_COLOR_DIFF_MINUS 1
318 #define TOG_COLOR_DIFF_PLUS 2
319 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 #define TOG_COLOR_DIFF_META 4
321 #define TOG_COLOR_TREE_SUBMODULE 5
322 #define TOG_COLOR_TREE_SYMLINK 6
323 #define TOG_COLOR_TREE_DIRECTORY 7
324 #define TOG_COLOR_TREE_EXECUTABLE 8
325 #define TOG_COLOR_COMMIT 9
326 #define TOG_COLOR_AUTHOR 10
327 #define TOG_COLOR_DATE 11
328 #define TOG_COLOR_REFS_HEADS 12
329 #define TOG_COLOR_REFS_TAGS 13
330 #define TOG_COLOR_REFS_REMOTES 14
332 struct tog_blame_cb_args {
333 struct tog_blame_line *lines; /* one per line */
336 struct tog_view *view;
337 struct got_object_id *commit_id;
341 struct tog_blame_thread_args {
343 struct got_repository *repo;
344 struct tog_blame_cb_args *cb_args;
346 got_cancel_cb cancel_cb;
353 struct tog_blame_line *lines;
357 struct tog_blame_thread_args thread_args;
358 struct tog_blame_cb_args cb_args;
362 struct tog_blame_view_state {
363 int first_displayed_line;
364 int last_displayed_line;
369 struct got_object_id_queue blamed_commits;
370 struct got_object_qid *blamed_commit;
372 struct got_repository *repo;
373 struct got_object_id *commit_id;
374 struct tog_blame blame;
376 struct tog_colors colors;
379 struct tog_parent_tree {
380 TAILQ_ENTRY(tog_parent_tree) entry;
381 struct got_tree_object *tree;
382 struct got_tree_entry *first_displayed_entry;
383 struct got_tree_entry *selected_entry;
387 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 struct tog_tree_view_state {
391 struct got_tree_object *root;
392 struct got_tree_object *tree;
393 struct got_tree_entry *first_displayed_entry;
394 struct got_tree_entry *last_displayed_entry;
395 struct got_tree_entry *selected_entry;
396 int ndisplayed, selected, show_ids;
397 struct tog_parent_trees parents;
398 struct got_object_id *commit_id;
399 const char *head_ref_name;
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 got_ref_list_free(&s->refs);
2112 static const struct got_error *
2113 search_start_log_view(struct tog_view *view)
2115 struct tog_log_view_state *s = &view->state.log;
2117 s->matched_entry = NULL;
2118 s->search_entry = NULL;
2122 static const struct got_error *
2123 search_next_log_view(struct tog_view *view)
2125 const struct got_error *err = NULL;
2126 struct tog_log_view_state *s = &view->state.log;
2127 struct commit_queue_entry *entry;
2129 /* Display progress update in log view. */
2130 show_log_view(view);
2134 if (s->search_entry) {
2136 errcode = pthread_mutex_unlock(&tog_mutex);
2138 return got_error_set_errno(errcode,
2139 "pthread_mutex_unlock");
2140 ch = wgetch(view->window);
2141 errcode = pthread_mutex_lock(&tog_mutex);
2143 return got_error_set_errno(errcode,
2144 "pthread_mutex_lock");
2145 if (ch == KEY_BACKSPACE) {
2146 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2149 if (view->searching == TOG_SEARCH_FORWARD)
2150 entry = TAILQ_NEXT(s->search_entry, entry);
2152 entry = TAILQ_PREV(s->search_entry,
2153 commit_queue_head, entry);
2154 } else if (s->matched_entry) {
2155 if (view->searching == TOG_SEARCH_FORWARD)
2156 entry = TAILQ_NEXT(s->matched_entry, entry);
2158 entry = TAILQ_PREV(s->matched_entry,
2159 commit_queue_head, entry);
2161 if (view->searching == TOG_SEARCH_FORWARD)
2162 entry = TAILQ_FIRST(&s->commits.head);
2164 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2170 if (entry == NULL) {
2171 if (s->thread_args.log_complete ||
2172 view->searching == TOG_SEARCH_BACKWARD) {
2173 view->search_next_done =
2174 (s->matched_entry == NULL ?
2175 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2176 s->search_entry = NULL;
2180 * Poke the log thread for more commits and return,
2181 * allowing the main loop to make progress. Search
2182 * will resume at s->search_entry once we come back.
2184 s->thread_args.commits_needed++;
2185 return trigger_log_thread(view, 0);
2188 err = match_commit(&have_match, entry->id, entry->commit,
2193 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2194 s->matched_entry = entry;
2198 s->search_entry = entry;
2199 if (view->searching == TOG_SEARCH_FORWARD)
2200 entry = TAILQ_NEXT(entry, entry);
2202 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2205 if (s->matched_entry) {
2206 int cur = s->selected_entry->idx;
2207 while (cur < s->matched_entry->idx) {
2208 err = input_log_view(NULL, view, KEY_DOWN);
2213 while (cur > s->matched_entry->idx) {
2214 err = input_log_view(NULL, view, KEY_UP);
2221 s->search_entry = NULL;
2226 static const struct got_error *
2227 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2228 struct got_repository *repo, const char *head_ref_name,
2229 const char *in_repo_path, int log_branches)
2231 const struct got_error *err = NULL;
2232 struct tog_log_view_state *s = &view->state.log;
2233 struct got_repository *thread_repo = NULL;
2234 struct got_commit_graph *thread_graph = NULL;
2237 SIMPLEQ_INIT(&s->refs);
2239 if (in_repo_path != s->in_repo_path) {
2240 free(s->in_repo_path);
2241 s->in_repo_path = strdup(in_repo_path);
2242 if (s->in_repo_path == NULL)
2243 return got_error_from_errno("strdup");
2246 /* The commit queue only contains commits being displayed. */
2247 TAILQ_INIT(&s->commits.head);
2248 s->commits.ncommits = 0;
2250 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2255 s->head_ref_name = head_ref_name;
2256 s->start_id = got_object_id_dup(start_id);
2257 if (s->start_id == NULL) {
2258 err = got_error_from_errno("got_object_id_dup");
2261 s->log_branches = log_branches;
2263 SIMPLEQ_INIT(&s->colors);
2264 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2265 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2266 get_color_value("TOG_COLOR_COMMIT"));
2269 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2270 get_color_value("TOG_COLOR_AUTHOR"));
2272 free_colors(&s->colors);
2275 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2276 get_color_value("TOG_COLOR_DATE"));
2278 free_colors(&s->colors);
2283 view->show = show_log_view;
2284 view->input = input_log_view;
2285 view->close = close_log_view;
2286 view->search_start = search_start_log_view;
2287 view->search_next = search_next_log_view;
2289 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2292 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2296 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2297 s->repo, NULL, NULL);
2301 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2303 err = got_error_set_errno(errcode, "pthread_cond_init");
2306 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2308 err = got_error_set_errno(errcode, "pthread_cond_init");
2312 s->thread_args.commits_needed = view->nlines;
2313 s->thread_args.graph = thread_graph;
2314 s->thread_args.commits = &s->commits;
2315 s->thread_args.in_repo_path = s->in_repo_path;
2316 s->thread_args.start_id = s->start_id;
2317 s->thread_args.repo = thread_repo;
2318 s->thread_args.log_complete = 0;
2319 s->thread_args.quit = &s->quit;
2320 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2321 s->thread_args.selected_entry = &s->selected_entry;
2322 s->thread_args.searching = &view->searching;
2323 s->thread_args.search_next_done = &view->search_next_done;
2324 s->thread_args.regex = &view->regex;
2327 close_log_view(view);
2331 static const struct got_error *
2332 show_log_view(struct tog_view *view)
2334 const struct got_error *err;
2335 struct tog_log_view_state *s = &view->state.log;
2337 if (s->thread == NULL) {
2338 int errcode = pthread_create(&s->thread, NULL, log_thread,
2341 return got_error_set_errno(errcode, "pthread_create");
2342 if (s->thread_args.commits_needed > 0) {
2343 err = trigger_log_thread(view, 1);
2349 return draw_commits(view);
2352 static const struct got_error *
2353 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2355 const struct got_error *err = NULL;
2356 struct tog_log_view_state *s = &view->state.log;
2357 struct tog_view *diff_view = NULL, *tree_view = NULL;
2358 struct tog_view *ref_view = NULL;
2369 if (s->first_displayed_entry == NULL)
2371 if (s->selected > 0)
2374 log_scroll_up(s, 1);
2379 if (s->first_displayed_entry == NULL)
2381 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2384 log_scroll_up(s, view->nlines - 1);
2391 if (s->first_displayed_entry == NULL)
2393 if (s->selected < MIN(view->nlines - 2,
2394 s->commits.ncommits - 1))
2397 err = log_scroll_down(view, 1);
2405 struct commit_queue_entry *first;
2406 first = s->first_displayed_entry;
2409 err = log_scroll_down(view, view->nlines - 1);
2412 if (first == s->first_displayed_entry &&
2413 s->selected < MIN(view->nlines - 2,
2414 s->commits.ncommits - 1)) {
2415 /* can't scroll further down */
2416 s->selected = MIN(view->nlines - 2,
2417 s->commits.ncommits - 1);
2423 if (s->selected > view->nlines - 2)
2424 s->selected = view->nlines - 2;
2425 if (s->selected > s->commits.ncommits - 1)
2426 s->selected = s->commits.ncommits - 1;
2428 if (s->commits.ncommits < view->nlines - 1 &&
2429 !s->thread_args.log_complete) {
2430 s->thread_args.commits_needed += (view->nlines - 1) -
2431 s->commits.ncommits;
2432 err = trigger_log_thread(view, 1);
2438 if (s->selected_entry == NULL)
2440 if (view_is_parent_view(view))
2441 begin_x = view_split_begin_x(view->begin_x);
2442 err = open_diff_view_for_commit(&diff_view, begin_x,
2443 s->selected_entry->commit, s->selected_entry->id,
2448 diff_view->focussed = 1;
2449 if (view_is_parent_view(view)) {
2450 err = view_close_child(view);
2453 view_set_child(view, diff_view);
2454 view->focus_child = 1;
2456 *new_view = diff_view;
2459 if (s->selected_entry == NULL)
2461 if (view_is_parent_view(view))
2462 begin_x = view_split_begin_x(view->begin_x);
2463 err = browse_commit_tree(&tree_view, begin_x,
2464 s->selected_entry, s->in_repo_path, s->head_ref_name,
2469 tree_view->focussed = 1;
2470 if (view_is_parent_view(view)) {
2471 err = view_close_child(view);
2474 view_set_child(view, tree_view);
2475 view->focus_child = 1;
2477 *new_view = tree_view;
2482 if (ch == KEY_BACKSPACE &&
2483 got_path_is_root_dir(s->in_repo_path))
2485 err = stop_log_thread(s);
2488 if (ch == KEY_BACKSPACE) {
2490 err = got_path_dirname(&parent_path, s->in_repo_path);
2493 free(s->in_repo_path);
2494 s->in_repo_path = parent_path;
2495 s->thread_args.in_repo_path = s->in_repo_path;
2496 } else if (ch == CTRL('l')) {
2497 struct got_object_id *start_id;
2498 err = got_repo_match_object_id(&start_id, NULL,
2499 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2500 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2504 s->start_id = start_id;
2505 s->thread_args.start_id = s->start_id;
2507 s->log_branches = !s->log_branches;
2509 err = got_repo_open(&s->thread_args.repo,
2510 got_repo_get_path(s->repo), NULL);
2513 got_ref_list_free(&s->refs);
2514 err = got_ref_list(&s->refs, s->repo, NULL,
2515 got_ref_cmp_by_name, NULL);
2518 err = got_commit_graph_open(&s->thread_args.graph,
2519 s->in_repo_path, !s->log_branches);
2522 err = got_commit_graph_iter_start(s->thread_args.graph,
2523 s->start_id, s->repo, NULL, NULL);
2526 free_commits(&s->commits);
2527 s->first_displayed_entry = NULL;
2528 s->last_displayed_entry = NULL;
2529 s->selected_entry = NULL;
2531 s->thread_args.log_complete = 0;
2533 s->thread_args.commits_needed = view->nlines;
2536 if (view_is_parent_view(view))
2537 begin_x = view_split_begin_x(view->begin_x);
2538 ref_view = view_open(view->nlines, view->ncols,
2539 view->begin_y, begin_x, TOG_VIEW_REF);
2540 if (ref_view == NULL)
2541 return got_error_from_errno("view_open");
2542 err = open_ref_view(ref_view, s->repo);
2544 view_close(ref_view);
2548 ref_view->focussed = 1;
2549 if (view_is_parent_view(view)) {
2550 err = view_close_child(view);
2553 view_set_child(view, ref_view);
2554 view->focus_child = 1;
2556 *new_view = ref_view;
2565 static const struct got_error *
2566 apply_unveil(const char *repo_path, const char *worktree_path)
2568 const struct got_error *error;
2571 if (unveil("gmon.out", "rwc") != 0)
2572 return got_error_from_errno2("unveil", "gmon.out");
2574 if (repo_path && unveil(repo_path, "r") != 0)
2575 return got_error_from_errno2("unveil", repo_path);
2577 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2578 return got_error_from_errno2("unveil", worktree_path);
2580 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2581 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2583 error = got_privsep_unveil_exec_helpers();
2587 if (unveil(NULL, NULL) != 0)
2588 return got_error_from_errno("unveil");
2598 halfdelay(1); /* Do fast refresh while initial view is loading. */
2601 intrflush(stdscr, FALSE);
2602 keypad(stdscr, TRUE);
2604 if (getenv("TOG_COLORS") != NULL) {
2606 use_default_colors();
2608 signal(SIGWINCH, tog_sigwinch);
2609 signal(SIGPIPE, tog_sigpipe);
2610 signal(SIGCONT, tog_sigcont);
2613 static const struct got_error *
2614 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2615 struct got_repository *repo, struct got_worktree *worktree)
2617 const struct got_error *err = NULL;
2620 *in_repo_path = strdup("/");
2621 if (*in_repo_path == NULL)
2622 return got_error_from_errno("strdup");
2627 const char *prefix = got_worktree_get_path_prefix(worktree);
2630 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2633 if (asprintf(in_repo_path, "%s%s%s", prefix,
2634 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2636 err = got_error_from_errno("asprintf");
2637 *in_repo_path = NULL;
2641 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2646 static const struct got_error *
2647 cmd_log(int argc, char *argv[])
2649 const struct got_error *error;
2650 struct got_repository *repo = NULL;
2651 struct got_worktree *worktree = NULL;
2652 struct got_object_id *start_id = NULL;
2653 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2654 char *start_commit = NULL, *label = NULL;
2655 struct got_reference *ref = NULL;
2656 const char *head_ref_name = NULL;
2657 int ch, log_branches = 0;
2658 struct tog_view *view;
2660 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2666 start_commit = optarg;
2669 repo_path = realpath(optarg, NULL);
2670 if (repo_path == NULL)
2671 return got_error_from_errno2("realpath",
2686 cwd = getcwd(NULL, 0);
2688 return got_error_from_errno("getcwd");
2690 error = got_worktree_open(&worktree, cwd);
2691 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2694 if (repo_path == NULL) {
2697 strdup(got_worktree_get_repo_path(worktree));
2699 repo_path = strdup(cwd);
2701 if (repo_path == NULL) {
2702 error = got_error_from_errno("strdup");
2706 error = got_repo_open(&repo, repo_path, NULL);
2710 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2717 error = apply_unveil(got_repo_get_path(repo),
2718 worktree ? got_worktree_get_root_path(worktree) : NULL);
2722 if (start_commit == NULL) {
2723 error = got_repo_match_object_id(&start_id, &label,
2724 worktree ? got_worktree_get_head_ref_name(worktree) :
2725 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2728 head_ref_name = label;
2730 error = got_ref_open(&ref, repo, start_commit, 0);
2732 head_ref_name = got_ref_get_name(ref);
2733 else if (error->code != GOT_ERR_NOT_REF)
2735 error = got_repo_match_object_id(&start_id, NULL,
2736 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2741 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2743 error = got_error_from_errno("view_open");
2746 error = open_log_view(view, start_id, repo, head_ref_name,
2747 in_repo_path, log_branches);
2751 /* Release work tree lock. */
2752 got_worktree_close(worktree);
2755 error = view_loop(view);
2765 got_repo_close(repo);
2767 got_worktree_close(worktree);
2775 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2776 "[-w] object1 object2\n", getprogname());
2781 parse_next_line(FILE *f, size_t *len)
2786 const char delim[3] = { '\0', '\0', '\0'};
2788 line = fparseln(f, &linelen, &lineno, delim, 0);
2795 match_line(const char *line, regex_t *regex, size_t nmatch,
2796 regmatch_t *regmatch)
2798 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2802 match_color(struct tog_colors *colors, const char *line)
2804 struct tog_color *tc = NULL;
2806 SIMPLEQ_FOREACH(tc, colors, entry) {
2807 if (match_line(line, &tc->regex, 0, NULL))
2814 static const struct got_error *
2815 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2816 WINDOW *window, regmatch_t *regmatch)
2818 const struct got_error *err = NULL;
2825 s = strndup(line, regmatch->rm_so);
2827 return got_error_from_errno("strndup");
2829 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2834 waddwstr(window, wline);
2841 s = strndup(line + regmatch->rm_so,
2842 regmatch->rm_eo - regmatch->rm_so);
2844 err = got_error_from_errno("strndup");
2848 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2853 wattr_on(window, A_STANDOUT, NULL);
2854 waddwstr(window, wline);
2855 wattr_off(window, A_STANDOUT, NULL);
2862 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2863 err = format_line(&wline, &width,
2864 line + regmatch->rm_eo, wlimit, col_tab_align);
2867 waddwstr(window, wline);
2875 static const struct got_error *
2876 draw_file(struct tog_view *view, const char *header)
2878 struct tog_diff_view_state *s = &view->state.diff;
2879 regmatch_t *regmatch = &view->regmatch;
2880 const struct got_error *err;
2883 struct tog_color *tc;
2887 int max_lines = view->nlines;
2888 int nlines = s->nlines;
2891 line_offset = s->line_offsets[s->first_displayed_line - 1];
2892 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2893 return got_error_from_errno("fseek");
2895 werase(view->window);
2898 if (asprintf(&line, "[%d/%d] %s",
2899 s->first_displayed_line - 1 + s->selected_line, nlines,
2901 return got_error_from_errno("asprintf");
2902 err = format_line(&wline, &width, line, view->ncols, 0);
2907 if (view_needs_focus_indication(view))
2908 wstandout(view->window);
2909 waddwstr(view->window, wline);
2912 if (view_needs_focus_indication(view))
2913 wstandend(view->window);
2914 if (width <= view->ncols - 1)
2915 waddch(view->window, '\n');
2923 while (max_lines > 0 && nprinted < max_lines) {
2924 line = parse_next_line(s->f, &len);
2930 tc = match_color(&s->colors, line);
2932 wattr_on(view->window,
2933 COLOR_PAIR(tc->colorpair), NULL);
2934 if (s->first_displayed_line + nprinted == s->matched_line &&
2935 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2936 err = add_matched_line(&width, line, view->ncols, 0,
2937 view->window, regmatch);
2943 err = format_line(&wline, &width, line, view->ncols, 0);
2948 waddwstr(view->window, wline);
2953 wattr_off(view->window,
2954 COLOR_PAIR(tc->colorpair), NULL);
2955 if (width <= view->ncols - 1)
2956 waddch(view->window, '\n');
2961 s->last_displayed_line = s->first_displayed_line +
2964 s->last_displayed_line = s->first_displayed_line;
2969 while (nprinted < view->nlines) {
2970 waddch(view->window, '\n');
2974 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2979 wstandout(view->window);
2980 waddwstr(view->window, wline);
2983 wstandend(view->window);
2990 get_datestr(time_t *time, char *datebuf)
2992 struct tm mytm, *tm;
2995 tm = gmtime_r(time, &mytm);
2998 s = asctime_r(tm, datebuf);
3001 p = strchr(s, '\n');
3007 static const struct got_error *
3008 get_changed_paths(struct got_pathlist_head *paths,
3009 struct got_commit_object *commit, struct got_repository *repo)
3011 const struct got_error *err = NULL;
3012 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3013 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3014 struct got_object_qid *qid;
3016 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3018 struct got_commit_object *pcommit;
3019 err = got_object_open_as_commit(&pcommit, repo,
3024 tree_id1 = got_object_commit_get_tree_id(pcommit);
3025 got_object_commit_close(pcommit);
3030 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3035 tree_id2 = got_object_commit_get_tree_id(commit);
3036 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3040 err = got_diff_tree(tree1, tree2, "", "", repo,
3041 got_diff_tree_collect_changed_paths, paths, 0);
3044 got_object_tree_close(tree1);
3046 got_object_tree_close(tree2);
3050 static const struct got_error *
3051 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3055 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3057 return got_error_from_errno("reallocarray");
3059 (*line_offsets)[*nlines] = off;
3064 static const struct got_error *
3065 write_commit_info(off_t **line_offsets, size_t *nlines,
3066 struct got_object_id *commit_id, struct got_reflist_head *refs,
3067 struct got_repository *repo, FILE *outfile)
3069 const struct got_error *err = NULL;
3070 char datebuf[26], *datestr;
3071 struct got_commit_object *commit;
3072 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3073 time_t committer_time;
3074 const char *author, *committer;
3075 char *refs_str = NULL;
3076 struct got_pathlist_head changed_paths;
3077 struct got_pathlist_entry *pe;
3081 TAILQ_INIT(&changed_paths);
3084 err = build_refs_str(&refs_str, refs, commit_id, repo);
3089 err = got_object_open_as_commit(&commit, repo, commit_id);
3093 err = got_object_id_str(&id_str, commit_id);
3095 err = got_error_from_errno("got_object_id_str");
3099 err = add_line_offset(line_offsets, nlines, 0);
3103 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3104 refs_str ? refs_str : "", refs_str ? ")" : "");
3106 err = got_error_from_errno("fprintf");
3110 err = add_line_offset(line_offsets, nlines, outoff);
3114 n = fprintf(outfile, "from: %s\n",
3115 got_object_commit_get_author(commit));
3117 err = got_error_from_errno("fprintf");
3121 err = add_line_offset(line_offsets, nlines, outoff);
3125 committer_time = got_object_commit_get_committer_time(commit);
3126 datestr = get_datestr(&committer_time, datebuf);
3128 n = fprintf(outfile, "date: %s UTC\n", datestr);
3130 err = got_error_from_errno("fprintf");
3134 err = add_line_offset(line_offsets, nlines, outoff);
3138 author = got_object_commit_get_author(commit);
3139 committer = got_object_commit_get_committer(commit);
3140 if (strcmp(author, committer) != 0) {
3141 n = fprintf(outfile, "via: %s\n", committer);
3143 err = got_error_from_errno("fprintf");
3147 err = add_line_offset(line_offsets, nlines, outoff);
3151 err = got_object_commit_get_logmsg(&logmsg, commit);
3155 while ((line = strsep(&s, "\n")) != NULL) {
3156 n = fprintf(outfile, "%s\n", line);
3158 err = got_error_from_errno("fprintf");
3162 err = add_line_offset(line_offsets, nlines, outoff);
3167 err = get_changed_paths(&changed_paths, commit, repo);
3170 TAILQ_FOREACH(pe, &changed_paths, entry) {
3171 struct got_diff_changed_path *cp = pe->data;
3172 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3174 err = got_error_from_errno("fprintf");
3178 err = add_line_offset(line_offsets, nlines, outoff);
3181 free((char *)pe->path);
3185 fputc('\n', outfile);
3187 err = add_line_offset(line_offsets, nlines, outoff);
3189 got_pathlist_free(&changed_paths);
3193 got_object_commit_close(commit);
3195 free(*line_offsets);
3196 *line_offsets = NULL;
3202 static const struct got_error *
3203 create_diff(struct tog_diff_view_state *s)
3205 const struct got_error *err = NULL;
3209 free(s->line_offsets);
3210 s->line_offsets = malloc(sizeof(off_t));
3211 if (s->line_offsets == NULL)
3212 return got_error_from_errno("malloc");
3217 err = got_error_from_errno("got_opentemp");
3220 if (s->f && fclose(s->f) != 0) {
3221 err = got_error_from_errno("fclose");
3227 err = got_object_get_type(&obj_type, s->repo, s->id1);
3229 err = got_object_get_type(&obj_type, s->repo, s->id2);
3234 case GOT_OBJ_TYPE_BLOB:
3235 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3236 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3237 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3239 case GOT_OBJ_TYPE_TREE:
3240 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3241 s->id1, s->id2, "", "", s->diff_context,
3242 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3244 case GOT_OBJ_TYPE_COMMIT: {
3245 const struct got_object_id_queue *parent_ids;
3246 struct got_object_qid *pid;
3247 struct got_commit_object *commit2;
3249 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3252 /* Show commit info if we're diffing to a parent/root commit. */
3253 if (s->id1 == NULL) {
3254 err = write_commit_info(&s->line_offsets, &s->nlines,
3255 s->id2, &s->refs, s->repo, s->f);
3259 parent_ids = got_object_commit_get_parent_ids(commit2);
3260 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3261 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3262 err = write_commit_info(
3263 &s->line_offsets, &s->nlines,
3264 s->id2, &s->refs, s->repo, s->f);
3271 got_object_commit_close(commit2);
3273 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3274 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3275 s->force_text_diff, s->repo, s->f);
3279 err = got_error(GOT_ERR_OBJ_TYPE);
3285 if (s->f && fflush(s->f) != 0 && err == NULL)
3286 err = got_error_from_errno("fflush");
3291 diff_view_indicate_progress(struct tog_view *view)
3293 mvwaddstr(view->window, 0, 0, "diffing...");
3298 static const struct got_error *
3299 search_start_diff_view(struct tog_view *view)
3301 struct tog_diff_view_state *s = &view->state.diff;
3303 s->matched_line = 0;
3307 static const struct got_error *
3308 search_next_diff_view(struct tog_view *view)
3310 struct tog_diff_view_state *s = &view->state.diff;
3313 if (!view->searching) {
3314 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3318 if (s->matched_line) {
3319 if (view->searching == TOG_SEARCH_FORWARD)
3320 lineno = s->matched_line + 1;
3322 lineno = s->matched_line - 1;
3324 if (view->searching == TOG_SEARCH_FORWARD)
3335 if (lineno <= 0 || lineno > s->nlines) {
3336 if (s->matched_line == 0) {
3337 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3342 if (view->searching == TOG_SEARCH_FORWARD)
3348 offset = s->line_offsets[lineno - 1];
3349 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3351 return got_error_from_errno("fseeko");
3354 line = parse_next_line(s->f, &len);
3356 match_line(line, &view->regex, 1, &view->regmatch)) {
3357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3358 s->matched_line = lineno;
3363 if (view->searching == TOG_SEARCH_FORWARD)
3369 if (s->matched_line) {
3370 s->first_displayed_line = s->matched_line;
3371 s->selected_line = 1;
3377 static const struct got_error *
3378 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3379 struct got_object_id *id2, const char *label1, const char *label2,
3380 int diff_context, int ignore_whitespace, int force_text_diff,
3381 struct tog_view *log_view, struct got_repository *repo)
3383 const struct got_error *err;
3384 struct tog_diff_view_state *s = &view->state.diff;
3386 SIMPLEQ_INIT(&s->refs);
3388 if (id1 != NULL && id2 != NULL) {
3390 err = got_object_get_type(&type1, repo, id1);
3393 err = got_object_get_type(&type2, repo, id2);
3398 return got_error(GOT_ERR_OBJ_TYPE);
3400 s->first_displayed_line = 1;
3401 s->last_displayed_line = view->nlines;
3402 s->selected_line = 1;
3410 s->id1 = got_object_id_dup(id1);
3412 return got_error_from_errno("got_object_id_dup");
3416 s->id2 = got_object_id_dup(id2);
3417 if (s->id2 == NULL) {
3420 return got_error_from_errno("got_object_id_dup");
3423 s->first_displayed_line = 1;
3424 s->last_displayed_line = view->nlines;
3425 s->diff_context = diff_context;
3426 s->ignore_whitespace = ignore_whitespace;
3427 s->force_text_diff = force_text_diff;
3428 s->log_view = log_view;
3431 SIMPLEQ_INIT(&s->colors);
3432 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3433 err = add_color(&s->colors,
3434 "^-", TOG_COLOR_DIFF_MINUS,
3435 get_color_value("TOG_COLOR_DIFF_MINUS"));
3438 err = add_color(&s->colors, "^\\+",
3439 TOG_COLOR_DIFF_PLUS,
3440 get_color_value("TOG_COLOR_DIFF_PLUS"));
3442 free_colors(&s->colors);
3445 err = add_color(&s->colors,
3446 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3447 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3449 free_colors(&s->colors);
3453 err = add_color(&s->colors,
3454 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3455 TOG_COLOR_DIFF_META,
3456 get_color_value("TOG_COLOR_DIFF_META"));
3458 free_colors(&s->colors);
3462 err = add_color(&s->colors,
3463 "^(from|via): ", TOG_COLOR_AUTHOR,
3464 get_color_value("TOG_COLOR_AUTHOR"));
3466 free_colors(&s->colors);
3470 err = add_color(&s->colors,
3471 "^date: ", TOG_COLOR_DATE,
3472 get_color_value("TOG_COLOR_DATE"));
3474 free_colors(&s->colors);
3479 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3485 free_colors(&s->colors);
3489 if (log_view && view_is_splitscreen(view))
3490 show_log_view(log_view); /* draw vborder */
3491 diff_view_indicate_progress(view);
3493 s->line_offsets = NULL;
3495 err = create_diff(s);
3501 free_colors(&s->colors);
3502 got_ref_list_free(&s->refs);
3506 view->show = show_diff_view;
3507 view->input = input_diff_view;
3508 view->close = close_diff_view;
3509 view->search_start = search_start_diff_view;
3510 view->search_next = search_next_diff_view;
3515 static const struct got_error *
3516 close_diff_view(struct tog_view *view)
3518 const struct got_error *err = NULL;
3519 struct tog_diff_view_state *s = &view->state.diff;
3525 if (s->f && fclose(s->f) == EOF)
3526 err = got_error_from_errno("fclose");
3527 free_colors(&s->colors);
3528 free(s->line_offsets);
3529 s->line_offsets = NULL;
3531 got_ref_list_free(&s->refs);
3535 static const struct got_error *
3536 show_diff_view(struct tog_view *view)
3538 const struct got_error *err;
3539 struct tog_diff_view_state *s = &view->state.diff;
3540 char *id_str1 = NULL, *id_str2, *header;
3541 const char *label1, *label2;
3544 err = got_object_id_str(&id_str1, s->id1);
3547 label1 = s->label1 ? : id_str1;
3549 label1 = "/dev/null";
3551 err = got_object_id_str(&id_str2, s->id2);
3554 label2 = s->label2 ? : id_str2;
3556 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3557 err = got_error_from_errno("asprintf");
3565 return draw_file(view, header);
3568 static const struct got_error *
3569 set_selected_commit(struct tog_diff_view_state *s,
3570 struct commit_queue_entry *entry)
3572 const struct got_error *err;
3573 const struct got_object_id_queue *parent_ids;
3574 struct got_commit_object *selected_commit;
3575 struct got_object_qid *pid;
3578 s->id2 = got_object_id_dup(entry->id);
3580 return got_error_from_errno("got_object_id_dup");
3582 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3585 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3587 pid = SIMPLEQ_FIRST(parent_ids);
3588 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3589 got_object_commit_close(selected_commit);
3593 static const struct got_error *
3594 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3596 const struct got_error *err = NULL;
3597 struct tog_diff_view_state *s = &view->state.diff;
3598 struct tog_log_view_state *ls;
3599 struct commit_queue_entry *old_selected_entry;
3606 s->force_text_diff = !s->force_text_diff;
3608 s->ignore_whitespace = !s->ignore_whitespace;
3609 wclear(view->window);
3610 s->first_displayed_line = 1;
3611 s->last_displayed_line = view->nlines;
3612 diff_view_indicate_progress(view);
3613 err = create_diff(s);
3617 if (s->first_displayed_line > 1)
3618 s->first_displayed_line--;
3622 if (s->first_displayed_line == 1)
3625 while (i++ < view->nlines - 1 &&
3626 s->first_displayed_line > 1)
3627 s->first_displayed_line--;
3632 s->first_displayed_line++;
3640 while (!s->eof && i++ < view->nlines - 1) {
3642 line = parse_next_line(s->f, NULL);
3643 s->first_displayed_line++;
3649 if (s->diff_context > 0) {
3651 diff_view_indicate_progress(view);
3652 err = create_diff(s);
3653 if (s->first_displayed_line + view->nlines - 1 >
3655 s->first_displayed_line = 1;
3656 s->last_displayed_line = view->nlines;
3661 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3663 diff_view_indicate_progress(view);
3664 err = create_diff(s);
3669 if (s->log_view == NULL)
3671 ls = &s->log_view->state.log;
3672 old_selected_entry = ls->selected_entry;
3674 err = input_log_view(NULL, s->log_view, KEY_UP);
3678 if (old_selected_entry == ls->selected_entry)
3681 err = set_selected_commit(s, ls->selected_entry);
3685 s->first_displayed_line = 1;
3686 s->last_displayed_line = view->nlines;
3688 diff_view_indicate_progress(view);
3689 err = create_diff(s);
3693 if (s->log_view == NULL)
3695 ls = &s->log_view->state.log;
3696 old_selected_entry = ls->selected_entry;
3698 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3702 if (old_selected_entry == ls->selected_entry)
3705 err = set_selected_commit(s, ls->selected_entry);
3709 s->first_displayed_line = 1;
3710 s->last_displayed_line = view->nlines;
3712 diff_view_indicate_progress(view);
3713 err = create_diff(s);
3722 static const struct got_error *
3723 cmd_diff(int argc, char *argv[])
3725 const struct got_error *error = NULL;
3726 struct got_repository *repo = NULL;
3727 struct got_worktree *worktree = NULL;
3728 struct got_object_id *id1 = NULL, *id2 = NULL;
3729 char *repo_path = NULL, *cwd = NULL;
3730 char *id_str1 = NULL, *id_str2 = NULL;
3731 char *label1 = NULL, *label2 = NULL;
3732 int diff_context = 3, ignore_whitespace = 0;
3733 int ch, force_text_diff = 0;
3735 struct tog_view *view;
3737 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3740 force_text_diff = 1;
3743 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3746 err(1, "-C option %s", errstr);
3749 repo_path = realpath(optarg, NULL);
3750 if (repo_path == NULL)
3751 return got_error_from_errno2("realpath",
3753 got_path_strip_trailing_slashes(repo_path);
3756 ignore_whitespace = 1;
3768 usage_diff(); /* TODO show local worktree changes */
3769 } else if (argc == 2) {
3775 cwd = getcwd(NULL, 0);
3777 return got_error_from_errno("getcwd");
3779 error = got_worktree_open(&worktree, cwd);
3780 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3783 if (repo_path == NULL) {
3786 strdup(got_worktree_get_repo_path(worktree));
3788 repo_path = strdup(cwd);
3790 if (repo_path == NULL) {
3791 error = got_error_from_errno("strdup");
3795 error = got_repo_open(&repo, repo_path, NULL);
3801 error = apply_unveil(got_repo_get_path(repo), NULL);
3805 error = got_repo_match_object_id(&id1, &label1, id_str1,
3806 GOT_OBJ_TYPE_ANY, 1, repo);
3810 error = got_repo_match_object_id(&id2, &label2, id_str2,
3811 GOT_OBJ_TYPE_ANY, 1, repo);
3815 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3817 error = got_error_from_errno("view_open");
3820 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3821 ignore_whitespace, force_text_diff, NULL, repo);
3824 error = view_loop(view);
3831 got_repo_close(repo);
3833 got_worktree_close(worktree);
3841 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3846 struct tog_blame_line {
3848 struct got_object_id *id;
3851 static const struct got_error *
3852 draw_blame(struct tog_view *view)
3854 struct tog_blame_view_state *s = &view->state.blame;
3855 struct tog_blame *blame = &s->blame;
3856 regmatch_t *regmatch = &view->regmatch;
3857 const struct got_error *err;
3858 int lineno = 0, nprinted = 0;
3863 struct tog_blame_line *blame_line;
3864 struct got_object_id *prev_id = NULL;
3866 struct tog_color *tc;
3868 err = got_object_id_str(&id_str, s->blamed_commit->id);
3873 werase(view->window);
3875 if (asprintf(&line, "commit %s", id_str) == -1) {
3876 err = got_error_from_errno("asprintf");
3881 err = format_line(&wline, &width, line, view->ncols, 0);
3886 if (view_needs_focus_indication(view))
3887 wstandout(view->window);
3888 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3890 wattr_on(view->window,
3891 COLOR_PAIR(tc->colorpair), NULL);
3892 waddwstr(view->window, wline);
3894 wattr_off(view->window,
3895 COLOR_PAIR(tc->colorpair), NULL);
3896 if (view_needs_focus_indication(view))
3897 wstandend(view->window);
3900 if (width < view->ncols - 1)
3901 waddch(view->window, '\n');
3903 if (asprintf(&line, "[%d/%d] %s%s",
3904 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3905 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3907 return got_error_from_errno("asprintf");
3910 err = format_line(&wline, &width, line, view->ncols, 0);
3915 waddwstr(view->window, wline);
3918 if (width < view->ncols - 1)
3919 waddch(view->window, '\n');
3922 while (nprinted < view->nlines - 2) {
3923 line = parse_next_line(blame->f, &len);
3928 if (++lineno < s->first_displayed_line) {
3933 if (view->focussed && nprinted == s->selected_line - 1)
3934 wstandout(view->window);
3936 if (blame->nlines > 0) {
3937 blame_line = &blame->lines[lineno - 1];
3938 if (blame_line->annotated && prev_id &&
3939 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3941 nprinted == s->selected_line - 1)) {
3942 waddstr(view->window, " ");
3943 } else if (blame_line->annotated) {
3945 err = got_object_id_str(&id_str, blame_line->id);
3950 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3952 wattr_on(view->window,
3953 COLOR_PAIR(tc->colorpair), NULL);
3954 wprintw(view->window, "%.8s", id_str);
3956 wattr_off(view->window,
3957 COLOR_PAIR(tc->colorpair), NULL);
3959 prev_id = blame_line->id;
3961 waddstr(view->window, "........");
3965 waddstr(view->window, "........");
3969 if (view->focussed && nprinted == s->selected_line - 1)
3970 wstandend(view->window);
3971 waddstr(view->window, " ");
3973 if (view->ncols <= 9) {
3975 wline = wcsdup(L"");
3976 if (wline == NULL) {
3977 err = got_error_from_errno("wcsdup");
3981 } else if (s->first_displayed_line + nprinted ==
3983 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3984 err = add_matched_line(&width, line, view->ncols - 9, 9,
3985 view->window, regmatch);
3992 err = format_line(&wline, &width, line,
3993 view->ncols - 9, 9);
3994 waddwstr(view->window, wline);
4000 if (width <= view->ncols - 1)
4001 waddch(view->window, '\n');
4002 if (++nprinted == 1)
4003 s->first_displayed_line = lineno;
4006 s->last_displayed_line = lineno;
4013 static const struct got_error *
4014 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4016 const struct got_error *err = NULL;
4017 struct tog_blame_cb_args *a = arg;
4018 struct tog_blame_line *line;
4021 if (nlines != a->nlines ||
4022 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4023 return got_error(GOT_ERR_RANGE);
4025 errcode = pthread_mutex_lock(&tog_mutex);
4027 return got_error_set_errno(errcode, "pthread_mutex_lock");
4029 if (*a->quit) { /* user has quit the blame view */
4030 err = got_error(GOT_ERR_ITER_COMPLETED);
4035 goto done; /* no change in this commit */
4037 line = &a->lines[lineno - 1];
4038 if (line->annotated)
4041 line->id = got_object_id_dup(id);
4042 if (line->id == NULL) {
4043 err = got_error_from_errno("got_object_id_dup");
4046 line->annotated = 1;
4048 errcode = pthread_mutex_unlock(&tog_mutex);
4050 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4055 blame_thread(void *arg)
4057 const struct got_error *err;
4058 struct tog_blame_thread_args *ta = arg;
4059 struct tog_blame_cb_args *a = ta->cb_args;
4062 err = block_signals_used_by_main_thread();
4066 err = got_blame(ta->path, a->commit_id, ta->repo,
4067 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4068 if (err && err->code == GOT_ERR_CANCELLED)
4071 errcode = pthread_mutex_lock(&tog_mutex);
4073 return (void *)got_error_set_errno(errcode,
4074 "pthread_mutex_lock");
4076 got_repo_close(ta->repo);
4080 errcode = pthread_mutex_unlock(&tog_mutex);
4081 if (errcode && err == NULL)
4082 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4087 static struct got_object_id *
4088 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4089 int first_displayed_line, int selected_line)
4091 struct tog_blame_line *line;
4096 line = &lines[first_displayed_line - 1 + selected_line - 1];
4097 if (!line->annotated)
4103 static const struct got_error *
4104 stop_blame(struct tog_blame *blame)
4106 const struct got_error *err = NULL;
4109 if (blame->thread) {
4111 errcode = pthread_mutex_unlock(&tog_mutex);
4113 return got_error_set_errno(errcode,
4114 "pthread_mutex_unlock");
4115 errcode = pthread_join(blame->thread, (void **)&err);
4117 return got_error_set_errno(errcode, "pthread_join");
4118 errcode = pthread_mutex_lock(&tog_mutex);
4120 return got_error_set_errno(errcode,
4121 "pthread_mutex_lock");
4122 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4124 blame->thread = NULL;
4126 if (blame->thread_args.repo) {
4127 got_repo_close(blame->thread_args.repo);
4128 blame->thread_args.repo = NULL;
4131 if (fclose(blame->f) != 0 && err == NULL)
4132 err = got_error_from_errno("fclose");
4136 for (i = 0; i < blame->nlines; i++)
4137 free(blame->lines[i].id);
4139 blame->lines = NULL;
4141 free(blame->cb_args.commit_id);
4142 blame->cb_args.commit_id = NULL;
4147 static const struct got_error *
4148 cancel_blame_view(void *arg)
4150 const struct got_error *err = NULL;
4154 errcode = pthread_mutex_lock(&tog_mutex);
4156 return got_error_set_errno(errcode,
4157 "pthread_mutex_unlock");
4160 err = got_error(GOT_ERR_CANCELLED);
4162 errcode = pthread_mutex_unlock(&tog_mutex);
4164 return got_error_set_errno(errcode,
4165 "pthread_mutex_lock");
4170 static const struct got_error *
4171 run_blame(struct tog_view *view)
4173 struct tog_blame_view_state *s = &view->state.blame;
4174 struct tog_blame *blame = &s->blame;
4175 const struct got_error *err = NULL;
4176 struct got_blob_object *blob = NULL;
4177 struct got_repository *thread_repo = NULL;
4178 struct got_object_id *obj_id = NULL;
4181 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4186 err = got_object_get_type(&obj_type, s->repo, obj_id);
4190 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4191 err = got_error(GOT_ERR_OBJ_TYPE);
4195 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4198 blame->f = got_opentemp();
4199 if (blame->f == NULL) {
4200 err = got_error_from_errno("got_opentemp");
4203 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4204 &blame->line_offsets, blame->f, blob);
4205 if (err || blame->nlines == 0)
4208 /* Don't include \n at EOF in the blame line count. */
4209 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4212 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4213 if (blame->lines == NULL) {
4214 err = got_error_from_errno("calloc");
4218 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4222 blame->cb_args.view = view;
4223 blame->cb_args.lines = blame->lines;
4224 blame->cb_args.nlines = blame->nlines;
4225 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4226 if (blame->cb_args.commit_id == NULL) {
4227 err = got_error_from_errno("got_object_id_dup");
4230 blame->cb_args.quit = &s->done;
4232 blame->thread_args.path = s->path;
4233 blame->thread_args.repo = thread_repo;
4234 blame->thread_args.cb_args = &blame->cb_args;
4235 blame->thread_args.complete = &s->blame_complete;
4236 blame->thread_args.cancel_cb = cancel_blame_view;
4237 blame->thread_args.cancel_arg = &s->done;
4238 s->blame_complete = 0;
4242 got_object_blob_close(blob);
4249 static const struct got_error *
4250 open_blame_view(struct tog_view *view, char *path,
4251 struct got_object_id *commit_id, struct got_repository *repo)
4253 const struct got_error *err = NULL;
4254 struct tog_blame_view_state *s = &view->state.blame;
4256 SIMPLEQ_INIT(&s->blamed_commits);
4258 s->path = strdup(path);
4259 if (s->path == NULL)
4260 return got_error_from_errno("strdup");
4262 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4268 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4269 s->first_displayed_line = 1;
4270 s->last_displayed_line = view->nlines;
4271 s->selected_line = 1;
4272 s->blame_complete = 0;
4274 s->commit_id = commit_id;
4275 memset(&s->blame, 0, sizeof(s->blame));
4277 SIMPLEQ_INIT(&s->colors);
4278 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4279 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4280 get_color_value("TOG_COLOR_COMMIT"));
4285 view->show = show_blame_view;
4286 view->input = input_blame_view;
4287 view->close = close_blame_view;
4288 view->search_start = search_start_blame_view;
4289 view->search_next = search_next_blame_view;
4291 return run_blame(view);
4294 static const struct got_error *
4295 close_blame_view(struct tog_view *view)
4297 const struct got_error *err = NULL;
4298 struct tog_blame_view_state *s = &view->state.blame;
4300 if (s->blame.thread)
4301 err = stop_blame(&s->blame);
4303 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4304 struct got_object_qid *blamed_commit;
4305 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4306 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4307 got_object_qid_free(blamed_commit);
4311 free_colors(&s->colors);
4316 static const struct got_error *
4317 search_start_blame_view(struct tog_view *view)
4319 struct tog_blame_view_state *s = &view->state.blame;
4321 s->matched_line = 0;
4325 static const struct got_error *
4326 search_next_blame_view(struct tog_view *view)
4328 struct tog_blame_view_state *s = &view->state.blame;
4331 if (!view->searching) {
4332 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4336 if (s->matched_line) {
4337 if (view->searching == TOG_SEARCH_FORWARD)
4338 lineno = s->matched_line + 1;
4340 lineno = s->matched_line - 1;
4342 if (view->searching == TOG_SEARCH_FORWARD)
4345 lineno = s->blame.nlines;
4353 if (lineno <= 0 || lineno > s->blame.nlines) {
4354 if (s->matched_line == 0) {
4355 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4360 if (view->searching == TOG_SEARCH_FORWARD)
4363 lineno = s->blame.nlines;
4366 offset = s->blame.line_offsets[lineno - 1];
4367 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4369 return got_error_from_errno("fseeko");
4372 line = parse_next_line(s->blame.f, &len);
4374 match_line(line, &view->regex, 1, &view->regmatch)) {
4375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4376 s->matched_line = lineno;
4381 if (view->searching == TOG_SEARCH_FORWARD)
4387 if (s->matched_line) {
4388 s->first_displayed_line = s->matched_line;
4389 s->selected_line = 1;
4395 static const struct got_error *
4396 show_blame_view(struct tog_view *view)
4398 const struct got_error *err = NULL;
4399 struct tog_blame_view_state *s = &view->state.blame;
4402 if (s->blame.thread == NULL) {
4403 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4404 &s->blame.thread_args);
4406 return got_error_set_errno(errcode, "pthread_create");
4408 halfdelay(1); /* fast refresh while annotating */
4411 if (s->blame_complete)
4412 halfdelay(10); /* disable fast refresh */
4414 err = draw_blame(view);
4420 static const struct got_error *
4421 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4423 const struct got_error *err = NULL, *thread_err = NULL;
4424 struct tog_view *diff_view;
4425 struct tog_blame_view_state *s = &view->state.blame;
4434 if (s->selected_line > 1)
4436 else if (s->selected_line == 1 &&
4437 s->first_displayed_line > 1)
4438 s->first_displayed_line--;
4442 if (s->first_displayed_line == 1) {
4443 s->selected_line = 1;
4446 if (s->first_displayed_line > view->nlines - 2)
4447 s->first_displayed_line -=
4450 s->first_displayed_line = 1;
4454 if (s->selected_line < view->nlines - 2 &&
4455 s->first_displayed_line +
4456 s->selected_line <= s->blame.nlines)
4458 else if (s->last_displayed_line <
4460 s->first_displayed_line++;
4464 struct got_object_id *id = NULL;
4465 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4466 s->first_displayed_line, s->selected_line);
4470 struct got_commit_object *commit;
4471 struct got_object_qid *pid;
4472 struct got_object_id *blob_id = NULL;
4474 err = got_object_open_as_commit(&commit,
4478 pid = SIMPLEQ_FIRST(
4479 got_object_commit_get_parent_ids(commit));
4481 got_object_commit_close(commit);
4484 /* Check if path history ends here. */
4485 err = got_object_id_by_path(&blob_id, s->repo,
4488 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4490 got_object_commit_close(commit);
4493 err = got_object_get_type(&obj_type, s->repo,
4496 /* Can't blame non-blob type objects. */
4497 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4498 got_object_commit_close(commit);
4501 err = got_object_qid_alloc(&s->blamed_commit,
4503 got_object_commit_close(commit);
4505 if (got_object_id_cmp(id,
4506 s->blamed_commit->id) == 0)
4508 err = got_object_qid_alloc(&s->blamed_commit,
4514 thread_err = stop_blame(&s->blame);
4518 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4519 s->blamed_commit, entry);
4520 err = run_blame(view);
4526 struct got_object_qid *first;
4527 first = SIMPLEQ_FIRST(&s->blamed_commits);
4528 if (!got_object_id_cmp(first->id, s->commit_id))
4531 thread_err = stop_blame(&s->blame);
4535 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4536 got_object_qid_free(s->blamed_commit);
4538 SIMPLEQ_FIRST(&s->blamed_commits);
4539 err = run_blame(view);
4546 struct got_object_id *id = NULL;
4547 struct got_object_qid *pid;
4548 struct got_commit_object *commit = NULL;
4549 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4550 s->first_displayed_line, s->selected_line);
4553 err = got_object_open_as_commit(&commit, s->repo, id);
4556 pid = SIMPLEQ_FIRST(
4557 got_object_commit_get_parent_ids(commit));
4558 if (view_is_parent_view(view))
4559 begin_x = view_split_begin_x(view->begin_x);
4560 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4561 if (diff_view == NULL) {
4562 got_object_commit_close(commit);
4563 err = got_error_from_errno("view_open");
4566 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4567 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4568 got_object_commit_close(commit);
4570 view_close(diff_view);
4574 diff_view->focussed = 1;
4575 if (view_is_parent_view(view)) {
4576 err = view_close_child(view);
4579 view_set_child(view, diff_view);
4580 view->focus_child = 1;
4582 *new_view = diff_view;
4590 if (s->last_displayed_line >= s->blame.nlines &&
4591 s->selected_line >= MIN(s->blame.nlines,
4592 view->nlines - 2)) {
4595 if (s->last_displayed_line >= s->blame.nlines &&
4596 s->selected_line < view->nlines - 2) {
4597 s->selected_line = MIN(s->blame.nlines,
4601 if (s->last_displayed_line + view->nlines - 2
4603 s->first_displayed_line +=
4606 s->first_displayed_line =
4611 if (s->selected_line > view->nlines - 2) {
4612 s->selected_line = MIN(s->blame.nlines,
4619 return thread_err ? thread_err : err;
4622 static const struct got_error *
4623 cmd_blame(int argc, char *argv[])
4625 const struct got_error *error;
4626 struct got_repository *repo = NULL;
4627 struct got_worktree *worktree = NULL;
4628 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4629 char *link_target = NULL;
4630 struct got_object_id *commit_id = NULL;
4631 char *commit_id_str = NULL;
4633 struct tog_view *view;
4635 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4638 commit_id_str = optarg;
4641 repo_path = realpath(optarg, NULL);
4642 if (repo_path == NULL)
4643 return got_error_from_errno2("realpath",
4658 cwd = getcwd(NULL, 0);
4660 return got_error_from_errno("getcwd");
4662 error = got_worktree_open(&worktree, cwd);
4663 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4666 if (repo_path == NULL) {
4669 strdup(got_worktree_get_repo_path(worktree));
4671 repo_path = strdup(cwd);
4673 if (repo_path == NULL) {
4674 error = got_error_from_errno("strdup");
4678 error = got_repo_open(&repo, repo_path, NULL);
4682 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4689 error = apply_unveil(got_repo_get_path(repo), NULL);
4693 if (commit_id_str == NULL) {
4694 struct got_reference *head_ref;
4695 error = got_ref_open(&head_ref, repo, worktree ?
4696 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4699 error = got_ref_resolve(&commit_id, repo, head_ref);
4700 got_ref_close(head_ref);
4702 error = got_repo_match_object_id(&commit_id, NULL,
4703 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4708 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4710 error = got_error_from_errno("view_open");
4714 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4719 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4724 /* Release work tree lock. */
4725 got_worktree_close(worktree);
4728 error = view_loop(view);
4736 got_worktree_close(worktree);
4738 got_repo_close(repo);
4742 static const struct got_error *
4743 draw_tree_entries(struct tog_view *view, const char *parent_path)
4745 struct tog_tree_view_state *s = &view->state.tree;
4746 const struct got_error *err = NULL;
4747 struct got_tree_entry *te;
4749 struct tog_color *tc;
4750 int width, n, i, nentries;
4751 int limit = view->nlines;
4755 werase(view->window);
4760 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4763 if (view_needs_focus_indication(view))
4764 wstandout(view->window);
4765 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4767 wattr_on(view->window,
4768 COLOR_PAIR(tc->colorpair), NULL);
4769 waddwstr(view->window, wline);
4771 wattr_off(view->window,
4772 COLOR_PAIR(tc->colorpair), NULL);
4773 if (view_needs_focus_indication(view))
4774 wstandend(view->window);
4777 if (width < view->ncols - 1)
4778 waddch(view->window, '\n');
4781 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4784 waddwstr(view->window, wline);
4787 if (width < view->ncols - 1)
4788 waddch(view->window, '\n');
4791 waddch(view->window, '\n');
4795 if (s->first_displayed_entry == NULL) {
4796 te = got_object_tree_get_first_entry(s->tree);
4797 if (s->selected == 0) {
4799 wstandout(view->window);
4800 s->selected_entry = NULL;
4802 waddstr(view->window, " ..\n"); /* parent directory */
4803 if (s->selected == 0 && view->focussed)
4804 wstandend(view->window);
4811 te = s->first_displayed_entry;
4814 nentries = got_object_tree_get_nentries(s->tree);
4815 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4816 char *line = NULL, *id_str = NULL, *link_target = NULL;
4817 const char *modestr = "";
4820 te = got_object_tree_get_entry(s->tree, i);
4821 mode = got_tree_entry_get_mode(te);
4824 err = got_object_id_str(&id_str,
4825 got_tree_entry_get_id(te));
4827 return got_error_from_errno(
4828 "got_object_id_str");
4830 if (got_object_tree_entry_is_submodule(te))
4832 else if (S_ISLNK(mode)) {
4835 err = got_tree_entry_get_symlink_target(&link_target,
4841 for (i = 0; i < strlen(link_target); i++) {
4842 if (!isprint((unsigned char)link_target[i]))
4843 link_target[i] = '?';
4847 else if (S_ISDIR(mode))
4849 else if (mode & S_IXUSR)
4851 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4852 got_tree_entry_get_name(te), modestr,
4853 link_target ? " -> ": "",
4854 link_target ? link_target : "") == -1) {
4857 return got_error_from_errno("asprintf");
4861 err = format_line(&wline, &width, line, view->ncols, 0);
4866 if (n == s->selected) {
4868 wstandout(view->window);
4869 s->selected_entry = te;
4871 tc = match_color(&s->colors, line);
4873 wattr_on(view->window,
4874 COLOR_PAIR(tc->colorpair), NULL);
4875 waddwstr(view->window, wline);
4877 wattr_off(view->window,
4878 COLOR_PAIR(tc->colorpair), NULL);
4879 if (width < view->ncols - 1)
4880 waddch(view->window, '\n');
4881 if (n == s->selected && view->focussed)
4882 wstandend(view->window);
4888 s->last_displayed_entry = te;
4897 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4899 struct got_tree_entry *te;
4900 int isroot = s->tree == s->root;
4903 if (s->first_displayed_entry == NULL)
4906 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4907 while (i++ < maxscroll) {
4910 s->first_displayed_entry = NULL;
4913 s->first_displayed_entry = te;
4914 te = got_tree_entry_get_prev(s->tree, te);
4919 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4921 struct got_tree_entry *next, *last;
4924 if (s->first_displayed_entry)
4925 next = got_tree_entry_get_next(s->tree,
4926 s->first_displayed_entry);
4928 next = got_object_tree_get_first_entry(s->tree);
4930 last = s->last_displayed_entry;
4931 while (next && last && n++ < maxscroll) {
4932 last = got_tree_entry_get_next(s->tree, last);
4934 s->first_displayed_entry = next;
4935 next = got_tree_entry_get_next(s->tree, next);
4940 static const struct got_error *
4941 tree_entry_path(char **path, struct tog_parent_trees *parents,
4942 struct got_tree_entry *te)
4944 const struct got_error *err = NULL;
4945 struct tog_parent_tree *pt;
4946 size_t len = 2; /* for leading slash and NUL */
4948 TAILQ_FOREACH(pt, parents, entry)
4949 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4952 len += strlen(got_tree_entry_get_name(te));
4954 *path = calloc(1, len);
4956 return got_error_from_errno("calloc");
4959 pt = TAILQ_LAST(parents, tog_parent_trees);
4961 const char *name = got_tree_entry_get_name(pt->selected_entry);
4962 if (strlcat(*path, name, len) >= len) {
4963 err = got_error(GOT_ERR_NO_SPACE);
4966 if (strlcat(*path, "/", len) >= len) {
4967 err = got_error(GOT_ERR_NO_SPACE);
4970 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4973 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4974 err = got_error(GOT_ERR_NO_SPACE);
4986 static const struct got_error *
4987 blame_tree_entry(struct tog_view **new_view, int begin_x,
4988 struct got_tree_entry *te, struct tog_parent_trees *parents,
4989 struct got_object_id *commit_id, struct got_repository *repo)
4991 const struct got_error *err = NULL;
4993 struct tog_view *blame_view;
4997 err = tree_entry_path(&path, parents, te);
5001 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5002 if (blame_view == NULL) {
5003 err = got_error_from_errno("view_open");
5007 err = open_blame_view(blame_view, path, commit_id, repo);
5009 if (err->code == GOT_ERR_CANCELLED)
5011 view_close(blame_view);
5013 *new_view = blame_view;
5019 static const struct got_error *
5020 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5021 struct tog_tree_view_state *s)
5023 struct tog_view *log_view;
5024 const struct got_error *err = NULL;
5029 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5030 if (log_view == NULL)
5031 return got_error_from_errno("view_open");
5033 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5037 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5040 view_close(log_view);
5042 *new_view = log_view;
5047 static const struct got_error *
5048 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5049 struct got_object_id *commit_id, const char *head_ref_name,
5050 struct got_repository *repo)
5052 const struct got_error *err = NULL;
5053 char *commit_id_str = NULL;
5054 struct tog_tree_view_state *s = &view->state.tree;
5056 TAILQ_INIT(&s->parents);
5058 err = got_object_id_str(&commit_id_str, commit_id);
5062 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5063 err = got_error_from_errno("asprintf");
5067 s->root = s->tree = root;
5068 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5069 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5070 s->commit_id = got_object_id_dup(commit_id);
5071 if (s->commit_id == NULL) {
5072 err = got_error_from_errno("got_object_id_dup");
5075 s->head_ref_name = head_ref_name;
5078 SIMPLEQ_INIT(&s->colors);
5080 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5081 err = add_color(&s->colors, "\\$$",
5082 TOG_COLOR_TREE_SUBMODULE,
5083 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5086 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5087 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5089 free_colors(&s->colors);
5092 err = add_color(&s->colors, "/$",
5093 TOG_COLOR_TREE_DIRECTORY,
5094 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5096 free_colors(&s->colors);
5100 err = add_color(&s->colors, "\\*$",
5101 TOG_COLOR_TREE_EXECUTABLE,
5102 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5104 free_colors(&s->colors);
5108 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5109 get_color_value("TOG_COLOR_COMMIT"));
5111 free_colors(&s->colors);
5116 view->show = show_tree_view;
5117 view->input = input_tree_view;
5118 view->close = close_tree_view;
5119 view->search_start = search_start_tree_view;
5120 view->search_next = search_next_tree_view;
5122 free(commit_id_str);
5124 free(s->tree_label);
5125 s->tree_label = NULL;
5130 static const struct got_error *
5131 close_tree_view(struct tog_view *view)
5133 struct tog_tree_view_state *s = &view->state.tree;
5135 free_colors(&s->colors);
5136 free(s->tree_label);
5137 s->tree_label = NULL;
5139 s->commit_id = NULL;
5140 while (!TAILQ_EMPTY(&s->parents)) {
5141 struct tog_parent_tree *parent;
5142 parent = TAILQ_FIRST(&s->parents);
5143 TAILQ_REMOVE(&s->parents, parent, entry);
5147 if (s->tree != s->root)
5148 got_object_tree_close(s->tree);
5149 got_object_tree_close(s->root);
5153 static const struct got_error *
5154 search_start_tree_view(struct tog_view *view)
5156 struct tog_tree_view_state *s = &view->state.tree;
5158 s->matched_entry = NULL;
5163 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5165 regmatch_t regmatch;
5167 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5171 static const struct got_error *
5172 search_next_tree_view(struct tog_view *view)
5174 struct tog_tree_view_state *s = &view->state.tree;
5175 struct got_tree_entry *te = NULL;
5177 if (!view->searching) {
5178 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5182 if (s->matched_entry) {
5183 if (view->searching == TOG_SEARCH_FORWARD) {
5184 if (s->selected_entry)
5185 te = got_tree_entry_get_next(s->tree,
5188 te = got_object_tree_get_first_entry(s->tree);
5190 if (s->selected_entry == NULL)
5191 te = got_object_tree_get_last_entry(s->tree);
5193 te = got_tree_entry_get_prev(s->tree,
5197 if (view->searching == TOG_SEARCH_FORWARD)
5198 te = got_object_tree_get_first_entry(s->tree);
5200 te = got_object_tree_get_last_entry(s->tree);
5205 if (s->matched_entry == NULL) {
5206 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5209 if (view->searching == TOG_SEARCH_FORWARD)
5210 te = got_object_tree_get_first_entry(s->tree);
5212 te = got_object_tree_get_last_entry(s->tree);
5215 if (match_tree_entry(te, &view->regex)) {
5216 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5217 s->matched_entry = te;
5221 if (view->searching == TOG_SEARCH_FORWARD)
5222 te = got_tree_entry_get_next(s->tree, te);
5224 te = got_tree_entry_get_prev(s->tree, te);
5227 if (s->matched_entry) {
5228 s->first_displayed_entry = s->matched_entry;
5235 static const struct got_error *
5236 show_tree_view(struct tog_view *view)
5238 const struct got_error *err = NULL;
5239 struct tog_tree_view_state *s = &view->state.tree;
5242 err = tree_entry_path(&parent_path, &s->parents, NULL);
5246 err = draw_tree_entries(view, parent_path);
5253 static const struct got_error *
5254 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5256 const struct got_error *err = NULL;
5257 struct tog_tree_view_state *s = &view->state.tree;
5258 struct tog_view *log_view, *ref_view;
5263 s->show_ids = !s->show_ids;
5266 if (!s->selected_entry)
5268 if (view_is_parent_view(view))
5269 begin_x = view_split_begin_x(view->begin_x);
5270 err = log_selected_tree_entry(&log_view, begin_x, s);
5272 log_view->focussed = 1;
5273 if (view_is_parent_view(view)) {
5274 err = view_close_child(view);
5277 view_set_child(view, log_view);
5278 view->focus_child = 1;
5280 *new_view = log_view;
5283 if (view_is_parent_view(view))
5284 begin_x = view_split_begin_x(view->begin_x);
5285 ref_view = view_open(view->nlines, view->ncols,
5286 view->begin_y, begin_x, TOG_VIEW_REF);
5287 if (ref_view == NULL)
5288 return got_error_from_errno("view_open");
5289 err = open_ref_view(ref_view, s->repo);
5291 view_close(ref_view);
5295 ref_view->focussed = 1;
5296 if (view_is_parent_view(view)) {
5297 err = view_close_child(view);
5300 view_set_child(view, ref_view);
5301 view->focus_child = 1;
5303 *new_view = ref_view;
5307 if (s->selected > 0) {
5311 tree_scroll_up(s, 1);
5315 if (s->tree == s->root) {
5316 if (got_object_tree_get_first_entry(s->tree) ==
5317 s->first_displayed_entry)
5320 if (s->first_displayed_entry == NULL)
5323 tree_scroll_up(s, MAX(0, view->nlines - 3));
5327 if (s->selected < s->ndisplayed - 1) {
5331 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5333 /* can't scroll any further */
5335 tree_scroll_down(s, 1);
5339 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5341 /* can't scroll any further; move cursor down */
5342 if (s->selected < s->ndisplayed - 1)
5343 s->selected = s->ndisplayed - 1;
5346 tree_scroll_down(s, view->nlines - 3);
5351 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5352 struct tog_parent_tree *parent;
5353 /* user selected '..' */
5354 if (s->tree == s->root)
5356 parent = TAILQ_FIRST(&s->parents);
5357 TAILQ_REMOVE(&s->parents, parent,
5359 got_object_tree_close(s->tree);
5360 s->tree = parent->tree;
5361 s->first_displayed_entry =
5362 parent->first_displayed_entry;
5364 parent->selected_entry;
5365 s->selected = parent->selected;
5367 } else if (S_ISDIR(got_tree_entry_get_mode(
5368 s->selected_entry))) {
5369 struct got_tree_object *subtree;
5370 err = got_object_open_as_tree(&subtree, s->repo,
5371 got_tree_entry_get_id(s->selected_entry));
5374 err = tree_view_visit_subtree(s, subtree);
5376 got_object_tree_close(subtree);
5379 } else if (S_ISREG(got_tree_entry_get_mode(
5380 s->selected_entry))) {
5381 struct tog_view *blame_view;
5382 int begin_x = view_is_parent_view(view) ?
5383 view_split_begin_x(view->begin_x) : 0;
5385 err = blame_tree_entry(&blame_view, begin_x,
5386 s->selected_entry, &s->parents,
5387 s->commit_id, s->repo);
5391 blame_view->focussed = 1;
5392 if (view_is_parent_view(view)) {
5393 err = view_close_child(view);
5396 view_set_child(view, blame_view);
5397 view->focus_child = 1;
5399 *new_view = blame_view;
5403 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5404 s->selected = view->nlines - 4;
5417 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5422 static const struct got_error *
5423 cmd_tree(int argc, char *argv[])
5425 const struct got_error *error;
5426 struct got_repository *repo = NULL;
5427 struct got_worktree *worktree = NULL;
5428 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5429 struct got_object_id *commit_id = NULL;
5430 const char *commit_id_arg = NULL;
5432 struct got_commit_object *commit = NULL;
5433 struct got_tree_object *tree = NULL;
5434 struct got_reference *ref = NULL;
5435 const char *head_ref_name = NULL;
5437 struct tog_view *view;
5439 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5442 commit_id_arg = optarg;
5445 repo_path = realpath(optarg, NULL);
5446 if (repo_path == NULL)
5447 return got_error_from_errno2("realpath",
5462 cwd = getcwd(NULL, 0);
5464 return got_error_from_errno("getcwd");
5466 error = got_worktree_open(&worktree, cwd);
5467 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5470 if (repo_path == NULL) {
5473 strdup(got_worktree_get_repo_path(worktree));
5475 repo_path = strdup(cwd);
5477 if (repo_path == NULL) {
5478 error = got_error_from_errno("strdup");
5482 error = got_repo_open(&repo, repo_path, NULL);
5486 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5493 error = apply_unveil(got_repo_get_path(repo), NULL);
5497 if (commit_id_arg == NULL) {
5498 error = got_repo_match_object_id(&commit_id, &label,
5499 worktree ? got_worktree_get_head_ref_name(worktree) :
5500 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5503 head_ref_name = label;
5505 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5507 head_ref_name = got_ref_get_name(ref);
5508 else if (error->code != GOT_ERR_NOT_REF)
5510 error = got_repo_match_object_id(&commit_id, NULL,
5511 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5516 error = got_object_open_as_commit(&commit, repo, commit_id);
5520 error = got_object_open_as_tree(&tree, repo,
5521 got_object_commit_get_tree_id(commit));
5525 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5527 error = got_error_from_errno("view_open");
5530 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5533 if (!got_path_is_root_dir(in_repo_path)) {
5534 error = tree_view_walk_path(&view->state.tree, commit_id,
5541 /* Release work tree lock. */
5542 got_worktree_close(worktree);
5545 error = view_loop(view);
5554 got_object_commit_close(commit);
5556 got_object_tree_close(tree);
5558 got_repo_close(repo);
5562 static const struct got_error *
5563 ref_view_load_refs(struct tog_ref_view_state *s)
5565 const struct got_error *err;
5566 struct got_reflist_entry *sre;
5567 struct tog_reflist_entry *re;
5569 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5570 got_ref_cmp_by_name, NULL);
5575 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5576 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5579 re = malloc(sizeof(*re));
5581 return got_error_from_errno("malloc");
5584 re->idx = s->nrefs++;
5585 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5592 ref_view_free_refs(struct tog_ref_view_state *s)
5594 struct tog_reflist_entry *re;
5596 while (!TAILQ_EMPTY(&s->refs)) {
5597 re = TAILQ_FIRST(&s->refs);
5598 TAILQ_REMOVE(&s->refs, re, entry);
5601 got_ref_list_free(&s->simplerefs);
5604 static const struct got_error *
5605 open_ref_view(struct tog_view *view, struct got_repository *repo)
5607 const struct got_error *err = NULL;
5608 struct tog_ref_view_state *s = &view->state.ref;
5610 s->selected_entry = 0;
5613 SIMPLEQ_INIT(&s->simplerefs);
5614 TAILQ_INIT(&s->refs);
5615 SIMPLEQ_INIT(&s->colors);
5617 err = ref_view_load_refs(s);
5621 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5623 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5624 err = add_color(&s->colors, "^refs/heads/",
5625 TOG_COLOR_REFS_HEADS,
5626 get_color_value("TOG_COLOR_REFS_HEADS"));
5630 err = add_color(&s->colors, "^refs/tags/",
5631 TOG_COLOR_REFS_TAGS,
5632 get_color_value("TOG_COLOR_REFS_TAGS"));
5636 err = add_color(&s->colors, "^refs/remotes/",
5637 TOG_COLOR_REFS_REMOTES,
5638 get_color_value("TOG_COLOR_REFS_REMOTES"));
5643 view->show = show_ref_view;
5644 view->input = input_ref_view;
5645 view->close = close_ref_view;
5646 view->search_start = search_start_ref_view;
5647 view->search_next = search_next_ref_view;
5650 free_colors(&s->colors);
5654 static const struct got_error *
5655 close_ref_view(struct tog_view *view)
5657 struct tog_ref_view_state *s = &view->state.ref;
5659 ref_view_free_refs(s);
5660 free_colors(&s->colors);
5665 static const struct got_error *
5666 resolve_reflist_entry(struct got_object_id **commit_id,
5667 struct tog_reflist_entry *re, struct got_repository *repo)
5669 const struct got_error *err = NULL;
5670 struct got_object_id *obj_id;
5671 struct got_tag_object *tag = NULL;
5676 err = got_ref_resolve(&obj_id, repo, re->ref);
5680 err = got_object_get_type(&obj_type, repo, obj_id);
5685 case GOT_OBJ_TYPE_COMMIT:
5686 *commit_id = obj_id;
5688 case GOT_OBJ_TYPE_TAG:
5689 err = got_object_open_as_tag(&tag, repo, obj_id);
5693 err = got_object_get_type(&obj_type, repo,
5694 got_object_tag_get_object_id(tag));
5697 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5698 err = got_error(GOT_ERR_OBJ_TYPE);
5701 *commit_id = got_object_id_dup(
5702 got_object_tag_get_object_id(tag));
5703 if (*commit_id == NULL) {
5704 err = got_error_from_errno("got_object_id_dup");
5709 err = got_error(GOT_ERR_OBJ_TYPE);
5715 got_object_tag_close(tag);
5723 static const struct got_error *
5724 log_ref_entry(struct tog_view **new_view, int begin_x,
5725 struct tog_reflist_entry *re, struct got_repository *repo)
5727 struct tog_view *log_view;
5728 const struct got_error *err = NULL;
5729 struct got_object_id *commit_id = NULL;
5733 err = resolve_reflist_entry(&commit_id, re, repo);
5735 if (err->code != GOT_ERR_OBJ_TYPE)
5741 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5742 if (log_view == NULL) {
5743 err = got_error_from_errno("view_open");
5747 err = open_log_view(log_view, commit_id, repo,
5748 got_ref_get_name(re->ref), "", 0);
5751 view_close(log_view);
5753 *new_view = log_view;
5759 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5761 struct tog_reflist_entry *re;
5764 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5767 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5768 while (i++ < maxscroll) {
5771 s->first_displayed_entry = re;
5772 re = TAILQ_PREV(re, tog_reflist_head, entry);
5777 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5779 struct tog_reflist_entry *next, *last;
5782 if (s->first_displayed_entry)
5783 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5785 next = TAILQ_FIRST(&s->refs);
5787 last = s->last_displayed_entry;
5788 while (next && last && n++ < maxscroll) {
5789 last = TAILQ_NEXT(last, entry);
5791 s->first_displayed_entry = next;
5792 next = TAILQ_NEXT(next, entry);
5797 static const struct got_error *
5798 search_start_ref_view(struct tog_view *view)
5800 struct tog_ref_view_state *s = &view->state.ref;
5802 s->matched_entry = NULL;
5807 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5809 regmatch_t regmatch;
5811 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5815 static const struct got_error *
5816 search_next_ref_view(struct tog_view *view)
5818 struct tog_ref_view_state *s = &view->state.ref;
5819 struct tog_reflist_entry *re = NULL;
5821 if (!view->searching) {
5822 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5826 if (s->matched_entry) {
5827 if (view->searching == TOG_SEARCH_FORWARD) {
5828 if (s->selected_entry)
5829 re = TAILQ_NEXT(s->selected_entry, entry);
5831 re = TAILQ_PREV(s->selected_entry,
5832 tog_reflist_head, entry);
5834 if (s->selected_entry == NULL)
5835 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5837 re = TAILQ_PREV(s->selected_entry,
5838 tog_reflist_head, entry);
5841 if (view->searching == TOG_SEARCH_FORWARD)
5842 re = TAILQ_FIRST(&s->refs);
5844 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5849 if (s->matched_entry == NULL) {
5850 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5853 if (view->searching == TOG_SEARCH_FORWARD)
5854 re = TAILQ_FIRST(&s->refs);
5856 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5859 if (match_reflist_entry(re, &view->regex)) {
5860 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5861 s->matched_entry = re;
5865 if (view->searching == TOG_SEARCH_FORWARD)
5866 re = TAILQ_NEXT(re, entry);
5868 re = TAILQ_PREV(re, tog_reflist_head, entry);
5871 if (s->matched_entry) {
5872 s->first_displayed_entry = s->matched_entry;
5879 static const struct got_error *
5880 show_ref_view(struct tog_view *view)
5882 const struct got_error *err = NULL;
5883 struct tog_ref_view_state *s = &view->state.ref;
5884 struct tog_reflist_entry *re;
5887 struct tog_color *tc;
5889 int limit = view->nlines;
5891 werase(view->window);
5898 re = s->first_displayed_entry;
5900 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5902 return got_error_from_errno("asprintf");
5904 err = format_line(&wline, &width, line, view->ncols, 0);
5909 if (view_needs_focus_indication(view))
5910 wstandout(view->window);
5911 waddwstr(view->window, wline);
5912 if (view_needs_focus_indication(view))
5913 wstandend(view->window);
5918 if (width < view->ncols - 1)
5919 waddch(view->window, '\n');
5924 while (re && limit > 0) {
5927 if (got_ref_is_symbolic(re->ref)) {
5928 if (asprintf(&line, "%s -> %s",
5929 got_ref_get_name(re->ref),
5930 got_ref_get_symref_target(re->ref)) == -1)
5931 return got_error_from_errno("asprintf");
5932 } else if (s->show_ids) {
5933 struct got_object_id *id;
5935 err = got_ref_resolve(&id, s->repo, re->ref);
5938 err = got_object_id_str(&id_str, id);
5943 if (asprintf(&line, "%s: %s",
5944 got_ref_get_name(re->ref), id_str) == -1) {
5945 err = got_error_from_errno("asprintf");
5953 line = strdup(got_ref_get_name(re->ref));
5955 return got_error_from_errno("strdup");
5958 err = format_line(&wline, &width, line, view->ncols, 0);
5963 if (n == s->selected) {
5965 wstandout(view->window);
5966 s->selected_entry = re;
5968 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5970 wattr_on(view->window,
5971 COLOR_PAIR(tc->colorpair), NULL);
5972 waddwstr(view->window, wline);
5974 wattr_off(view->window,
5975 COLOR_PAIR(tc->colorpair), NULL);
5976 if (width < view->ncols - 1)
5977 waddch(view->window, '\n');
5978 if (n == s->selected && view->focussed)
5979 wstandend(view->window);
5985 s->last_displayed_entry = re;
5988 re = TAILQ_NEXT(re, entry);
5995 static const struct got_error *
5996 browse_ref_tree(struct tog_view **new_view, int begin_x,
5997 struct tog_reflist_entry *re, struct got_repository *repo)
5999 const struct got_error *err = NULL;
6000 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6001 struct got_tree_object *tree = NULL;
6002 struct tog_view *tree_view;
6006 err = resolve_reflist_entry(&commit_id, re, repo);
6008 if (err->code != GOT_ERR_OBJ_TYPE)
6014 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6018 err = got_object_open_as_tree(&tree, repo, tree_id);
6022 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6023 if (tree_view == NULL) {
6024 err = got_error_from_errno("view_open");
6028 err = open_tree_view(tree_view, tree, commit_id,
6029 got_ref_get_name(re->ref), repo);
6033 *new_view = tree_view;
6039 got_object_tree_close(tree);
6043 static const struct got_error *
6044 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6046 const struct got_error *err = NULL;
6047 struct tog_ref_view_state *s = &view->state.ref;
6048 struct tog_view *log_view, *tree_view;
6053 s->show_ids = !s->show_ids;
6057 if (!s->selected_entry)
6059 if (view_is_parent_view(view))
6060 begin_x = view_split_begin_x(view->begin_x);
6061 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6064 log_view->focussed = 1;
6065 if (view_is_parent_view(view)) {
6066 err = view_close_child(view);
6069 view_set_child(view, log_view);
6070 view->focus_child = 1;
6072 *new_view = log_view;
6075 if (!s->selected_entry)
6077 if (view_is_parent_view(view))
6078 begin_x = view_split_begin_x(view->begin_x);
6079 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6081 if (err || tree_view == NULL)
6084 tree_view->focussed = 1;
6085 if (view_is_parent_view(view)) {
6086 err = view_close_child(view);
6089 view_set_child(view, tree_view);
6090 view->focus_child = 1;
6092 *new_view = tree_view;
6096 if (s->selected > 0) {
6100 ref_scroll_up(s, 1);
6104 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6106 ref_scroll_up(s, MAX(0, view->nlines - 1));
6110 if (s->selected < s->ndisplayed - 1) {
6114 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6115 /* can't scroll any further */
6117 ref_scroll_down(s, 1);
6121 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6122 /* can't scroll any further; move cursor down */
6123 if (s->selected < s->ndisplayed - 1)
6124 s->selected = s->ndisplayed - 1;
6127 ref_scroll_down(s, view->nlines - 1);
6130 ref_view_free_refs(s);
6131 err = ref_view_load_refs(s);
6134 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6135 s->selected = view->nlines - 2;
6148 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6153 static const struct got_error *
6154 cmd_ref(int argc, char *argv[])
6156 const struct got_error *error;
6157 struct got_repository *repo = NULL;
6158 struct got_worktree *worktree = NULL;
6159 char *cwd = NULL, *repo_path = NULL;
6161 struct tog_view *view;
6163 while ((ch = getopt(argc, argv, "r:")) != -1) {
6166 repo_path = realpath(optarg, NULL);
6167 if (repo_path == NULL)
6168 return got_error_from_errno2("realpath",
6183 cwd = getcwd(NULL, 0);
6185 return got_error_from_errno("getcwd");
6187 error = got_worktree_open(&worktree, cwd);
6188 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6191 if (repo_path == NULL) {
6194 strdup(got_worktree_get_repo_path(worktree));
6196 repo_path = strdup(cwd);
6198 if (repo_path == NULL) {
6199 error = got_error_from_errno("strdup");
6203 error = got_repo_open(&repo, repo_path, NULL);
6209 error = apply_unveil(got_repo_get_path(repo), NULL);
6213 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6215 error = got_error_from_errno("view_open");
6219 error = open_ref_view(view, repo);
6224 /* Release work tree lock. */
6225 got_worktree_close(worktree);
6228 error = view_loop(view);
6233 got_repo_close(repo);
6238 list_commands(FILE *fp)
6242 fprintf(fp, "commands:");
6243 for (i = 0; i < nitems(tog_commands); i++) {
6244 struct tog_cmd *cmd = &tog_commands[i];
6245 fprintf(fp, " %s", cmd->name);
6251 usage(int hflag, int status)
6253 FILE *fp = (status == 0) ? stdout : stderr;
6255 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6258 fprintf(fp, "lazy usage: %s path\n", getprogname());
6265 make_argv(int argc, ...)
6273 argv = calloc(argc, sizeof(char *));
6276 for (i = 0; i < argc; i++) {
6277 argv[i] = strdup(va_arg(ap, char *));
6278 if (argv[i] == NULL)
6287 * Try to convert 'tog path' into a 'tog log path' command.
6288 * The user could simply have mistyped the command rather than knowingly
6289 * provided a path. So check whether argv[0] can in fact be resolved
6290 * to a path in the HEAD commit and print a special error if not.
6291 * This hack is for mpi@ <3
6293 static const struct got_error *
6294 tog_log_with_path(int argc, char *argv[])
6296 const struct got_error *error = NULL;
6297 struct tog_cmd *cmd = NULL;
6298 struct got_repository *repo = NULL;
6299 struct got_worktree *worktree = NULL;
6300 struct got_object_id *commit_id = NULL, *id = NULL;
6301 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6302 char *commit_id_str = NULL, **cmd_argv = NULL;
6304 cwd = getcwd(NULL, 0);
6306 return got_error_from_errno("getcwd");
6308 error = got_worktree_open(&worktree, cwd);
6309 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6313 repo_path = strdup(got_worktree_get_repo_path(worktree));
6315 repo_path = strdup(cwd);
6316 if (repo_path == NULL) {
6317 error = got_error_from_errno("strdup");
6321 error = got_repo_open(&repo, repo_path, NULL);
6325 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6330 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6331 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6332 GOT_OBJ_TYPE_COMMIT, 1, repo);
6337 got_worktree_close(worktree);
6341 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6343 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6345 fprintf(stderr, "%s: '%s' is no known command or path\n",
6346 getprogname(), argv[0]);
6351 got_repo_close(repo);
6354 error = got_object_id_str(&commit_id_str, commit_id);
6358 cmd = &tog_commands[0]; /* log */
6360 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6361 error = cmd->cmd_main(argc, cmd_argv);
6364 got_repo_close(repo);
6366 got_worktree_close(worktree);
6368 free(commit_id_str);
6375 for (i = 0; i < argc; i++)
6383 main(int argc, char *argv[])
6385 const struct got_error *error = NULL;
6386 struct tog_cmd *cmd = NULL;
6387 int ch, hflag = 0, Vflag = 0;
6388 char **cmd_argv = NULL;
6389 static struct option longopts[] = {
6390 { "version", no_argument, NULL, 'V' },
6394 setlocale(LC_CTYPE, "");
6396 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6416 got_version_print_str();
6421 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6429 /* Build an argument vector which runs a default command. */
6430 cmd = &tog_commands[0];
6432 cmd_argv = make_argv(argc, cmd->name);
6436 /* Did the user specify a command? */
6437 for (i = 0; i < nitems(tog_commands); i++) {
6438 if (strncmp(tog_commands[i].name, argv[0],
6439 strlen(argv[0])) == 0) {
6440 cmd = &tog_commands[i];
6449 /* No command specified; try log with a path */
6450 error = tog_log_with_path(argc, argv);
6455 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6462 for (i = 0; i < argc; i++)
6467 if (error && error->code != GOT_ERR_CANCELLED)
6468 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);