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 err = got_commit_graph_open(&s->thread_args.graph,
2514 s->in_repo_path, !s->log_branches);
2517 err = got_commit_graph_iter_start(s->thread_args.graph,
2518 s->start_id, s->repo, NULL, NULL);
2521 free_commits(&s->commits);
2522 s->first_displayed_entry = NULL;
2523 s->last_displayed_entry = NULL;
2524 s->selected_entry = NULL;
2526 s->thread_args.log_complete = 0;
2528 s->thread_args.commits_needed = view->nlines;
2531 if (view_is_parent_view(view))
2532 begin_x = view_split_begin_x(view->begin_x);
2533 ref_view = view_open(view->nlines, view->ncols,
2534 view->begin_y, begin_x, TOG_VIEW_REF);
2535 if (ref_view == NULL)
2536 return got_error_from_errno("view_open");
2537 err = open_ref_view(ref_view, s->repo);
2539 view_close(ref_view);
2543 ref_view->focussed = 1;
2544 if (view_is_parent_view(view)) {
2545 err = view_close_child(view);
2548 view_set_child(view, ref_view);
2549 view->focus_child = 1;
2551 *new_view = ref_view;
2560 static const struct got_error *
2561 apply_unveil(const char *repo_path, const char *worktree_path)
2563 const struct got_error *error;
2566 if (unveil("gmon.out", "rwc") != 0)
2567 return got_error_from_errno2("unveil", "gmon.out");
2569 if (repo_path && unveil(repo_path, "r") != 0)
2570 return got_error_from_errno2("unveil", repo_path);
2572 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2573 return got_error_from_errno2("unveil", worktree_path);
2575 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2576 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2578 error = got_privsep_unveil_exec_helpers();
2582 if (unveil(NULL, NULL) != 0)
2583 return got_error_from_errno("unveil");
2593 halfdelay(1); /* Do fast refresh while initial view is loading. */
2596 intrflush(stdscr, FALSE);
2597 keypad(stdscr, TRUE);
2599 if (getenv("TOG_COLORS") != NULL) {
2601 use_default_colors();
2603 signal(SIGWINCH, tog_sigwinch);
2604 signal(SIGPIPE, tog_sigpipe);
2605 signal(SIGCONT, tog_sigcont);
2608 static const struct got_error *
2609 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2610 struct got_repository *repo, struct got_worktree *worktree)
2612 const struct got_error *err = NULL;
2615 *in_repo_path = strdup("/");
2616 if (*in_repo_path == NULL)
2617 return got_error_from_errno("strdup");
2622 const char *prefix = got_worktree_get_path_prefix(worktree);
2625 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2628 if (asprintf(in_repo_path, "%s%s%s", prefix,
2629 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2631 err = got_error_from_errno("asprintf");
2632 *in_repo_path = NULL;
2636 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2641 static const struct got_error *
2642 cmd_log(int argc, char *argv[])
2644 const struct got_error *error;
2645 struct got_repository *repo = NULL;
2646 struct got_worktree *worktree = NULL;
2647 struct got_object_id *start_id = NULL;
2648 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2649 char *start_commit = NULL, *label = NULL;
2650 struct got_reference *ref = NULL;
2651 const char *head_ref_name = NULL;
2652 int ch, log_branches = 0;
2653 struct tog_view *view;
2655 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2661 start_commit = optarg;
2664 repo_path = realpath(optarg, NULL);
2665 if (repo_path == NULL)
2666 return got_error_from_errno2("realpath",
2681 cwd = getcwd(NULL, 0);
2683 return got_error_from_errno("getcwd");
2685 error = got_worktree_open(&worktree, cwd);
2686 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2689 if (repo_path == NULL) {
2692 strdup(got_worktree_get_repo_path(worktree));
2694 repo_path = strdup(cwd);
2696 if (repo_path == NULL) {
2697 error = got_error_from_errno("strdup");
2701 error = got_repo_open(&repo, repo_path, NULL);
2705 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2712 error = apply_unveil(got_repo_get_path(repo),
2713 worktree ? got_worktree_get_root_path(worktree) : NULL);
2717 if (start_commit == NULL) {
2718 error = got_repo_match_object_id(&start_id, &label,
2719 worktree ? got_worktree_get_head_ref_name(worktree) :
2720 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2723 head_ref_name = label;
2725 error = got_ref_open(&ref, repo, start_commit, 0);
2727 head_ref_name = got_ref_get_name(ref);
2728 else if (error->code != GOT_ERR_NOT_REF)
2730 error = got_repo_match_object_id(&start_id, NULL,
2731 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2736 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2738 error = got_error_from_errno("view_open");
2741 error = open_log_view(view, start_id, repo, head_ref_name,
2742 in_repo_path, log_branches);
2746 /* Release work tree lock. */
2747 got_worktree_close(worktree);
2750 error = view_loop(view);
2760 got_repo_close(repo);
2762 got_worktree_close(worktree);
2770 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2771 "[-w] object1 object2\n", getprogname());
2776 parse_next_line(FILE *f, size_t *len)
2781 const char delim[3] = { '\0', '\0', '\0'};
2783 line = fparseln(f, &linelen, &lineno, delim, 0);
2790 match_line(const char *line, regex_t *regex, size_t nmatch,
2791 regmatch_t *regmatch)
2793 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2797 match_color(struct tog_colors *colors, const char *line)
2799 struct tog_color *tc = NULL;
2801 SIMPLEQ_FOREACH(tc, colors, entry) {
2802 if (match_line(line, &tc->regex, 0, NULL))
2809 static const struct got_error *
2810 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2811 WINDOW *window, regmatch_t *regmatch)
2813 const struct got_error *err = NULL;
2820 s = strndup(line, regmatch->rm_so);
2822 return got_error_from_errno("strndup");
2824 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2829 waddwstr(window, wline);
2836 s = strndup(line + regmatch->rm_so,
2837 regmatch->rm_eo - regmatch->rm_so);
2839 err = got_error_from_errno("strndup");
2843 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2848 wattr_on(window, A_STANDOUT, NULL);
2849 waddwstr(window, wline);
2850 wattr_off(window, A_STANDOUT, NULL);
2857 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2858 err = format_line(&wline, &width,
2859 line + regmatch->rm_eo, wlimit, col_tab_align);
2862 waddwstr(window, wline);
2870 static const struct got_error *
2871 draw_file(struct tog_view *view, const char *header)
2873 struct tog_diff_view_state *s = &view->state.diff;
2874 regmatch_t *regmatch = &view->regmatch;
2875 const struct got_error *err;
2878 struct tog_color *tc;
2882 int max_lines = view->nlines;
2883 int nlines = s->nlines;
2886 line_offset = s->line_offsets[s->first_displayed_line - 1];
2887 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2888 return got_error_from_errno("fseek");
2890 werase(view->window);
2893 if (asprintf(&line, "[%d/%d] %s",
2894 s->first_displayed_line - 1 + s->selected_line, nlines,
2896 return got_error_from_errno("asprintf");
2897 err = format_line(&wline, &width, line, view->ncols, 0);
2902 if (view_needs_focus_indication(view))
2903 wstandout(view->window);
2904 waddwstr(view->window, wline);
2907 if (view_needs_focus_indication(view))
2908 wstandend(view->window);
2909 if (width <= view->ncols - 1)
2910 waddch(view->window, '\n');
2918 while (max_lines > 0 && nprinted < max_lines) {
2919 line = parse_next_line(s->f, &len);
2925 tc = match_color(&s->colors, line);
2927 wattr_on(view->window,
2928 COLOR_PAIR(tc->colorpair), NULL);
2929 if (s->first_displayed_line + nprinted == s->matched_line &&
2930 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2931 err = add_matched_line(&width, line, view->ncols, 0,
2932 view->window, regmatch);
2938 err = format_line(&wline, &width, line, view->ncols, 0);
2943 waddwstr(view->window, wline);
2948 wattr_off(view->window,
2949 COLOR_PAIR(tc->colorpair), NULL);
2950 if (width <= view->ncols - 1)
2951 waddch(view->window, '\n');
2956 s->last_displayed_line = s->first_displayed_line +
2959 s->last_displayed_line = s->first_displayed_line;
2964 while (nprinted < view->nlines) {
2965 waddch(view->window, '\n');
2969 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2974 wstandout(view->window);
2975 waddwstr(view->window, wline);
2978 wstandend(view->window);
2985 get_datestr(time_t *time, char *datebuf)
2987 struct tm mytm, *tm;
2990 tm = gmtime_r(time, &mytm);
2993 s = asctime_r(tm, datebuf);
2996 p = strchr(s, '\n');
3002 static const struct got_error *
3003 get_changed_paths(struct got_pathlist_head *paths,
3004 struct got_commit_object *commit, struct got_repository *repo)
3006 const struct got_error *err = NULL;
3007 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3008 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3009 struct got_object_qid *qid;
3011 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3013 struct got_commit_object *pcommit;
3014 err = got_object_open_as_commit(&pcommit, repo,
3019 tree_id1 = got_object_commit_get_tree_id(pcommit);
3020 got_object_commit_close(pcommit);
3025 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3030 tree_id2 = got_object_commit_get_tree_id(commit);
3031 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3035 err = got_diff_tree(tree1, tree2, "", "", repo,
3036 got_diff_tree_collect_changed_paths, paths, 0);
3039 got_object_tree_close(tree1);
3041 got_object_tree_close(tree2);
3045 static const struct got_error *
3046 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3050 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3052 return got_error_from_errno("reallocarray");
3054 (*line_offsets)[*nlines] = off;
3059 static const struct got_error *
3060 write_commit_info(off_t **line_offsets, size_t *nlines,
3061 struct got_object_id *commit_id, struct got_reflist_head *refs,
3062 struct got_repository *repo, FILE *outfile)
3064 const struct got_error *err = NULL;
3065 char datebuf[26], *datestr;
3066 struct got_commit_object *commit;
3067 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3068 time_t committer_time;
3069 const char *author, *committer;
3070 char *refs_str = NULL;
3071 struct got_pathlist_head changed_paths;
3072 struct got_pathlist_entry *pe;
3076 TAILQ_INIT(&changed_paths);
3079 err = build_refs_str(&refs_str, refs, commit_id, repo);
3084 err = got_object_open_as_commit(&commit, repo, commit_id);
3088 err = got_object_id_str(&id_str, commit_id);
3090 err = got_error_from_errno("got_object_id_str");
3094 err = add_line_offset(line_offsets, nlines, 0);
3098 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3099 refs_str ? refs_str : "", refs_str ? ")" : "");
3101 err = got_error_from_errno("fprintf");
3105 err = add_line_offset(line_offsets, nlines, outoff);
3109 n = fprintf(outfile, "from: %s\n",
3110 got_object_commit_get_author(commit));
3112 err = got_error_from_errno("fprintf");
3116 err = add_line_offset(line_offsets, nlines, outoff);
3120 committer_time = got_object_commit_get_committer_time(commit);
3121 datestr = get_datestr(&committer_time, datebuf);
3123 n = fprintf(outfile, "date: %s UTC\n", datestr);
3125 err = got_error_from_errno("fprintf");
3129 err = add_line_offset(line_offsets, nlines, outoff);
3133 author = got_object_commit_get_author(commit);
3134 committer = got_object_commit_get_committer(commit);
3135 if (strcmp(author, committer) != 0) {
3136 n = fprintf(outfile, "via: %s\n", committer);
3138 err = got_error_from_errno("fprintf");
3142 err = add_line_offset(line_offsets, nlines, outoff);
3146 err = got_object_commit_get_logmsg(&logmsg, commit);
3150 while ((line = strsep(&s, "\n")) != NULL) {
3151 n = fprintf(outfile, "%s\n", line);
3153 err = got_error_from_errno("fprintf");
3157 err = add_line_offset(line_offsets, nlines, outoff);
3162 err = get_changed_paths(&changed_paths, commit, repo);
3165 TAILQ_FOREACH(pe, &changed_paths, entry) {
3166 struct got_diff_changed_path *cp = pe->data;
3167 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3169 err = got_error_from_errno("fprintf");
3173 err = add_line_offset(line_offsets, nlines, outoff);
3176 free((char *)pe->path);
3180 fputc('\n', outfile);
3182 err = add_line_offset(line_offsets, nlines, outoff);
3184 got_pathlist_free(&changed_paths);
3188 got_object_commit_close(commit);
3190 free(*line_offsets);
3191 *line_offsets = NULL;
3197 static const struct got_error *
3198 create_diff(struct tog_diff_view_state *s)
3200 const struct got_error *err = NULL;
3204 free(s->line_offsets);
3205 s->line_offsets = malloc(sizeof(off_t));
3206 if (s->line_offsets == NULL)
3207 return got_error_from_errno("malloc");
3212 err = got_error_from_errno("got_opentemp");
3215 if (s->f && fclose(s->f) != 0) {
3216 err = got_error_from_errno("fclose");
3222 err = got_object_get_type(&obj_type, s->repo, s->id1);
3224 err = got_object_get_type(&obj_type, s->repo, s->id2);
3229 case GOT_OBJ_TYPE_BLOB:
3230 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3231 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3232 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3234 case GOT_OBJ_TYPE_TREE:
3235 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3236 s->id1, s->id2, "", "", s->diff_context,
3237 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3239 case GOT_OBJ_TYPE_COMMIT: {
3240 const struct got_object_id_queue *parent_ids;
3241 struct got_object_qid *pid;
3242 struct got_commit_object *commit2;
3244 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3247 /* Show commit info if we're diffing to a parent/root commit. */
3248 if (s->id1 == NULL) {
3249 err = write_commit_info(&s->line_offsets, &s->nlines,
3250 s->id2, &s->refs, s->repo, s->f);
3254 parent_ids = got_object_commit_get_parent_ids(commit2);
3255 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3256 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3257 err = write_commit_info(
3258 &s->line_offsets, &s->nlines,
3259 s->id2, &s->refs, s->repo, s->f);
3266 got_object_commit_close(commit2);
3268 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3269 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3270 s->force_text_diff, s->repo, s->f);
3274 err = got_error(GOT_ERR_OBJ_TYPE);
3280 if (s->f && fflush(s->f) != 0 && err == NULL)
3281 err = got_error_from_errno("fflush");
3286 diff_view_indicate_progress(struct tog_view *view)
3288 mvwaddstr(view->window, 0, 0, "diffing...");
3293 static const struct got_error *
3294 search_start_diff_view(struct tog_view *view)
3296 struct tog_diff_view_state *s = &view->state.diff;
3298 s->matched_line = 0;
3302 static const struct got_error *
3303 search_next_diff_view(struct tog_view *view)
3305 struct tog_diff_view_state *s = &view->state.diff;
3308 if (!view->searching) {
3309 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3313 if (s->matched_line) {
3314 if (view->searching == TOG_SEARCH_FORWARD)
3315 lineno = s->matched_line + 1;
3317 lineno = s->matched_line - 1;
3319 if (view->searching == TOG_SEARCH_FORWARD)
3330 if (lineno <= 0 || lineno > s->nlines) {
3331 if (s->matched_line == 0) {
3332 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3337 if (view->searching == TOG_SEARCH_FORWARD)
3343 offset = s->line_offsets[lineno - 1];
3344 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3346 return got_error_from_errno("fseeko");
3349 line = parse_next_line(s->f, &len);
3351 match_line(line, &view->regex, 1, &view->regmatch)) {
3352 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3353 s->matched_line = lineno;
3358 if (view->searching == TOG_SEARCH_FORWARD)
3364 if (s->matched_line) {
3365 s->first_displayed_line = s->matched_line;
3366 s->selected_line = 1;
3372 static const struct got_error *
3373 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3374 struct got_object_id *id2, const char *label1, const char *label2,
3375 int diff_context, int ignore_whitespace, int force_text_diff,
3376 struct tog_view *log_view, struct got_repository *repo)
3378 const struct got_error *err;
3379 struct tog_diff_view_state *s = &view->state.diff;
3381 SIMPLEQ_INIT(&s->refs);
3383 if (id1 != NULL && id2 != NULL) {
3385 err = got_object_get_type(&type1, repo, id1);
3388 err = got_object_get_type(&type2, repo, id2);
3393 return got_error(GOT_ERR_OBJ_TYPE);
3395 s->first_displayed_line = 1;
3396 s->last_displayed_line = view->nlines;
3397 s->selected_line = 1;
3405 s->id1 = got_object_id_dup(id1);
3407 return got_error_from_errno("got_object_id_dup");
3411 s->id2 = got_object_id_dup(id2);
3412 if (s->id2 == NULL) {
3415 return got_error_from_errno("got_object_id_dup");
3418 s->first_displayed_line = 1;
3419 s->last_displayed_line = view->nlines;
3420 s->diff_context = diff_context;
3421 s->ignore_whitespace = ignore_whitespace;
3422 s->force_text_diff = force_text_diff;
3423 s->log_view = log_view;
3426 SIMPLEQ_INIT(&s->colors);
3427 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3428 err = add_color(&s->colors,
3429 "^-", TOG_COLOR_DIFF_MINUS,
3430 get_color_value("TOG_COLOR_DIFF_MINUS"));
3433 err = add_color(&s->colors, "^\\+",
3434 TOG_COLOR_DIFF_PLUS,
3435 get_color_value("TOG_COLOR_DIFF_PLUS"));
3437 free_colors(&s->colors);
3440 err = add_color(&s->colors,
3441 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3442 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3444 free_colors(&s->colors);
3448 err = add_color(&s->colors,
3449 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3450 TOG_COLOR_DIFF_META,
3451 get_color_value("TOG_COLOR_DIFF_META"));
3453 free_colors(&s->colors);
3457 err = add_color(&s->colors,
3458 "^(from|via): ", TOG_COLOR_AUTHOR,
3459 get_color_value("TOG_COLOR_AUTHOR"));
3461 free_colors(&s->colors);
3465 err = add_color(&s->colors,
3466 "^date: ", TOG_COLOR_DATE,
3467 get_color_value("TOG_COLOR_DATE"));
3469 free_colors(&s->colors);
3474 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3480 free_colors(&s->colors);
3484 if (log_view && view_is_splitscreen(view))
3485 show_log_view(log_view); /* draw vborder */
3486 diff_view_indicate_progress(view);
3488 s->line_offsets = NULL;
3490 err = create_diff(s);
3496 free_colors(&s->colors);
3497 got_ref_list_free(&s->refs);
3501 view->show = show_diff_view;
3502 view->input = input_diff_view;
3503 view->close = close_diff_view;
3504 view->search_start = search_start_diff_view;
3505 view->search_next = search_next_diff_view;
3510 static const struct got_error *
3511 close_diff_view(struct tog_view *view)
3513 const struct got_error *err = NULL;
3514 struct tog_diff_view_state *s = &view->state.diff;
3520 if (s->f && fclose(s->f) == EOF)
3521 err = got_error_from_errno("fclose");
3522 free_colors(&s->colors);
3523 free(s->line_offsets);
3524 s->line_offsets = NULL;
3526 got_ref_list_free(&s->refs);
3530 static const struct got_error *
3531 show_diff_view(struct tog_view *view)
3533 const struct got_error *err;
3534 struct tog_diff_view_state *s = &view->state.diff;
3535 char *id_str1 = NULL, *id_str2, *header;
3536 const char *label1, *label2;
3539 err = got_object_id_str(&id_str1, s->id1);
3542 label1 = s->label1 ? : id_str1;
3544 label1 = "/dev/null";
3546 err = got_object_id_str(&id_str2, s->id2);
3549 label2 = s->label2 ? : id_str2;
3551 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3552 err = got_error_from_errno("asprintf");
3560 return draw_file(view, header);
3563 static const struct got_error *
3564 set_selected_commit(struct tog_diff_view_state *s,
3565 struct commit_queue_entry *entry)
3567 const struct got_error *err;
3568 const struct got_object_id_queue *parent_ids;
3569 struct got_commit_object *selected_commit;
3570 struct got_object_qid *pid;
3573 s->id2 = got_object_id_dup(entry->id);
3575 return got_error_from_errno("got_object_id_dup");
3577 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3580 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3582 pid = SIMPLEQ_FIRST(parent_ids);
3583 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3584 got_object_commit_close(selected_commit);
3588 static const struct got_error *
3589 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3591 const struct got_error *err = NULL;
3592 struct tog_diff_view_state *s = &view->state.diff;
3593 struct tog_log_view_state *ls;
3594 struct commit_queue_entry *old_selected_entry;
3601 s->force_text_diff = !s->force_text_diff;
3603 s->ignore_whitespace = !s->ignore_whitespace;
3604 wclear(view->window);
3605 s->first_displayed_line = 1;
3606 s->last_displayed_line = view->nlines;
3607 diff_view_indicate_progress(view);
3608 err = create_diff(s);
3612 if (s->first_displayed_line > 1)
3613 s->first_displayed_line--;
3617 if (s->first_displayed_line == 1)
3620 while (i++ < view->nlines - 1 &&
3621 s->first_displayed_line > 1)
3622 s->first_displayed_line--;
3627 s->first_displayed_line++;
3635 while (!s->eof && i++ < view->nlines - 1) {
3637 line = parse_next_line(s->f, NULL);
3638 s->first_displayed_line++;
3644 if (s->diff_context > 0) {
3646 diff_view_indicate_progress(view);
3647 err = create_diff(s);
3648 if (s->first_displayed_line + view->nlines - 1 >
3650 s->first_displayed_line = 1;
3651 s->last_displayed_line = view->nlines;
3656 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3658 diff_view_indicate_progress(view);
3659 err = create_diff(s);
3664 if (s->log_view == NULL)
3666 ls = &s->log_view->state.log;
3667 old_selected_entry = ls->selected_entry;
3669 err = input_log_view(NULL, s->log_view, KEY_UP);
3673 if (old_selected_entry == ls->selected_entry)
3676 err = set_selected_commit(s, ls->selected_entry);
3680 s->first_displayed_line = 1;
3681 s->last_displayed_line = view->nlines;
3683 diff_view_indicate_progress(view);
3684 err = create_diff(s);
3688 if (s->log_view == NULL)
3690 ls = &s->log_view->state.log;
3691 old_selected_entry = ls->selected_entry;
3693 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3697 if (old_selected_entry == ls->selected_entry)
3700 err = set_selected_commit(s, ls->selected_entry);
3704 s->first_displayed_line = 1;
3705 s->last_displayed_line = view->nlines;
3707 diff_view_indicate_progress(view);
3708 err = create_diff(s);
3717 static const struct got_error *
3718 cmd_diff(int argc, char *argv[])
3720 const struct got_error *error = NULL;
3721 struct got_repository *repo = NULL;
3722 struct got_worktree *worktree = NULL;
3723 struct got_object_id *id1 = NULL, *id2 = NULL;
3724 char *repo_path = NULL, *cwd = NULL;
3725 char *id_str1 = NULL, *id_str2 = NULL;
3726 char *label1 = NULL, *label2 = NULL;
3727 int diff_context = 3, ignore_whitespace = 0;
3728 int ch, force_text_diff = 0;
3730 struct tog_view *view;
3732 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3735 force_text_diff = 1;
3738 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3741 err(1, "-C option %s", errstr);
3744 repo_path = realpath(optarg, NULL);
3745 if (repo_path == NULL)
3746 return got_error_from_errno2("realpath",
3748 got_path_strip_trailing_slashes(repo_path);
3751 ignore_whitespace = 1;
3763 usage_diff(); /* TODO show local worktree changes */
3764 } else if (argc == 2) {
3770 cwd = getcwd(NULL, 0);
3772 return got_error_from_errno("getcwd");
3774 error = got_worktree_open(&worktree, cwd);
3775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3778 if (repo_path == NULL) {
3781 strdup(got_worktree_get_repo_path(worktree));
3783 repo_path = strdup(cwd);
3785 if (repo_path == NULL) {
3786 error = got_error_from_errno("strdup");
3790 error = got_repo_open(&repo, repo_path, NULL);
3796 error = apply_unveil(got_repo_get_path(repo), NULL);
3800 error = got_repo_match_object_id(&id1, &label1, id_str1,
3801 GOT_OBJ_TYPE_ANY, 1, repo);
3805 error = got_repo_match_object_id(&id2, &label2, id_str2,
3806 GOT_OBJ_TYPE_ANY, 1, repo);
3810 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3812 error = got_error_from_errno("view_open");
3815 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3816 ignore_whitespace, force_text_diff, NULL, repo);
3819 error = view_loop(view);
3826 got_repo_close(repo);
3828 got_worktree_close(worktree);
3836 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3841 struct tog_blame_line {
3843 struct got_object_id *id;
3846 static const struct got_error *
3847 draw_blame(struct tog_view *view)
3849 struct tog_blame_view_state *s = &view->state.blame;
3850 struct tog_blame *blame = &s->blame;
3851 regmatch_t *regmatch = &view->regmatch;
3852 const struct got_error *err;
3853 int lineno = 0, nprinted = 0;
3858 struct tog_blame_line *blame_line;
3859 struct got_object_id *prev_id = NULL;
3861 struct tog_color *tc;
3863 err = got_object_id_str(&id_str, s->blamed_commit->id);
3868 werase(view->window);
3870 if (asprintf(&line, "commit %s", id_str) == -1) {
3871 err = got_error_from_errno("asprintf");
3876 err = format_line(&wline, &width, line, view->ncols, 0);
3881 if (view_needs_focus_indication(view))
3882 wstandout(view->window);
3883 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3885 wattr_on(view->window,
3886 COLOR_PAIR(tc->colorpair), NULL);
3887 waddwstr(view->window, wline);
3889 wattr_off(view->window,
3890 COLOR_PAIR(tc->colorpair), NULL);
3891 if (view_needs_focus_indication(view))
3892 wstandend(view->window);
3895 if (width < view->ncols - 1)
3896 waddch(view->window, '\n');
3898 if (asprintf(&line, "[%d/%d] %s%s",
3899 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3900 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3902 return got_error_from_errno("asprintf");
3905 err = format_line(&wline, &width, line, view->ncols, 0);
3910 waddwstr(view->window, wline);
3913 if (width < view->ncols - 1)
3914 waddch(view->window, '\n');
3917 while (nprinted < view->nlines - 2) {
3918 line = parse_next_line(blame->f, &len);
3923 if (++lineno < s->first_displayed_line) {
3928 if (view->focussed && nprinted == s->selected_line - 1)
3929 wstandout(view->window);
3931 if (blame->nlines > 0) {
3932 blame_line = &blame->lines[lineno - 1];
3933 if (blame_line->annotated && prev_id &&
3934 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3936 nprinted == s->selected_line - 1)) {
3937 waddstr(view->window, " ");
3938 } else if (blame_line->annotated) {
3940 err = got_object_id_str(&id_str, blame_line->id);
3945 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3947 wattr_on(view->window,
3948 COLOR_PAIR(tc->colorpair), NULL);
3949 wprintw(view->window, "%.8s", id_str);
3951 wattr_off(view->window,
3952 COLOR_PAIR(tc->colorpair), NULL);
3954 prev_id = blame_line->id;
3956 waddstr(view->window, "........");
3960 waddstr(view->window, "........");
3964 if (view->focussed && nprinted == s->selected_line - 1)
3965 wstandend(view->window);
3966 waddstr(view->window, " ");
3968 if (view->ncols <= 9) {
3970 wline = wcsdup(L"");
3971 if (wline == NULL) {
3972 err = got_error_from_errno("wcsdup");
3976 } else if (s->first_displayed_line + nprinted ==
3978 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3979 err = add_matched_line(&width, line, view->ncols - 9, 9,
3980 view->window, regmatch);
3987 err = format_line(&wline, &width, line,
3988 view->ncols - 9, 9);
3989 waddwstr(view->window, wline);
3995 if (width <= view->ncols - 1)
3996 waddch(view->window, '\n');
3997 if (++nprinted == 1)
3998 s->first_displayed_line = lineno;
4001 s->last_displayed_line = lineno;
4008 static const struct got_error *
4009 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4011 const struct got_error *err = NULL;
4012 struct tog_blame_cb_args *a = arg;
4013 struct tog_blame_line *line;
4016 if (nlines != a->nlines ||
4017 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4018 return got_error(GOT_ERR_RANGE);
4020 errcode = pthread_mutex_lock(&tog_mutex);
4022 return got_error_set_errno(errcode, "pthread_mutex_lock");
4024 if (*a->quit) { /* user has quit the blame view */
4025 err = got_error(GOT_ERR_ITER_COMPLETED);
4030 goto done; /* no change in this commit */
4032 line = &a->lines[lineno - 1];
4033 if (line->annotated)
4036 line->id = got_object_id_dup(id);
4037 if (line->id == NULL) {
4038 err = got_error_from_errno("got_object_id_dup");
4041 line->annotated = 1;
4043 errcode = pthread_mutex_unlock(&tog_mutex);
4045 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4050 blame_thread(void *arg)
4052 const struct got_error *err;
4053 struct tog_blame_thread_args *ta = arg;
4054 struct tog_blame_cb_args *a = ta->cb_args;
4057 err = block_signals_used_by_main_thread();
4061 err = got_blame(ta->path, a->commit_id, ta->repo,
4062 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4063 if (err && err->code == GOT_ERR_CANCELLED)
4066 errcode = pthread_mutex_lock(&tog_mutex);
4068 return (void *)got_error_set_errno(errcode,
4069 "pthread_mutex_lock");
4071 got_repo_close(ta->repo);
4075 errcode = pthread_mutex_unlock(&tog_mutex);
4076 if (errcode && err == NULL)
4077 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4082 static struct got_object_id *
4083 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4084 int first_displayed_line, int selected_line)
4086 struct tog_blame_line *line;
4091 line = &lines[first_displayed_line - 1 + selected_line - 1];
4092 if (!line->annotated)
4098 static const struct got_error *
4099 stop_blame(struct tog_blame *blame)
4101 const struct got_error *err = NULL;
4104 if (blame->thread) {
4106 errcode = pthread_mutex_unlock(&tog_mutex);
4108 return got_error_set_errno(errcode,
4109 "pthread_mutex_unlock");
4110 errcode = pthread_join(blame->thread, (void **)&err);
4112 return got_error_set_errno(errcode, "pthread_join");
4113 errcode = pthread_mutex_lock(&tog_mutex);
4115 return got_error_set_errno(errcode,
4116 "pthread_mutex_lock");
4117 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4119 blame->thread = NULL;
4121 if (blame->thread_args.repo) {
4122 got_repo_close(blame->thread_args.repo);
4123 blame->thread_args.repo = NULL;
4126 if (fclose(blame->f) != 0 && err == NULL)
4127 err = got_error_from_errno("fclose");
4131 for (i = 0; i < blame->nlines; i++)
4132 free(blame->lines[i].id);
4134 blame->lines = NULL;
4136 free(blame->cb_args.commit_id);
4137 blame->cb_args.commit_id = NULL;
4142 static const struct got_error *
4143 cancel_blame_view(void *arg)
4145 const struct got_error *err = NULL;
4149 errcode = pthread_mutex_lock(&tog_mutex);
4151 return got_error_set_errno(errcode,
4152 "pthread_mutex_unlock");
4155 err = got_error(GOT_ERR_CANCELLED);
4157 errcode = pthread_mutex_unlock(&tog_mutex);
4159 return got_error_set_errno(errcode,
4160 "pthread_mutex_lock");
4165 static const struct got_error *
4166 run_blame(struct tog_view *view)
4168 struct tog_blame_view_state *s = &view->state.blame;
4169 struct tog_blame *blame = &s->blame;
4170 const struct got_error *err = NULL;
4171 struct got_blob_object *blob = NULL;
4172 struct got_repository *thread_repo = NULL;
4173 struct got_object_id *obj_id = NULL;
4176 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4181 err = got_object_get_type(&obj_type, s->repo, obj_id);
4185 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4186 err = got_error(GOT_ERR_OBJ_TYPE);
4190 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4193 blame->f = got_opentemp();
4194 if (blame->f == NULL) {
4195 err = got_error_from_errno("got_opentemp");
4198 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4199 &blame->line_offsets, blame->f, blob);
4200 if (err || blame->nlines == 0)
4203 /* Don't include \n at EOF in the blame line count. */
4204 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4207 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4208 if (blame->lines == NULL) {
4209 err = got_error_from_errno("calloc");
4213 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4217 blame->cb_args.view = view;
4218 blame->cb_args.lines = blame->lines;
4219 blame->cb_args.nlines = blame->nlines;
4220 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4221 if (blame->cb_args.commit_id == NULL) {
4222 err = got_error_from_errno("got_object_id_dup");
4225 blame->cb_args.quit = &s->done;
4227 blame->thread_args.path = s->path;
4228 blame->thread_args.repo = thread_repo;
4229 blame->thread_args.cb_args = &blame->cb_args;
4230 blame->thread_args.complete = &s->blame_complete;
4231 blame->thread_args.cancel_cb = cancel_blame_view;
4232 blame->thread_args.cancel_arg = &s->done;
4233 s->blame_complete = 0;
4237 got_object_blob_close(blob);
4244 static const struct got_error *
4245 open_blame_view(struct tog_view *view, char *path,
4246 struct got_object_id *commit_id, struct got_repository *repo)
4248 const struct got_error *err = NULL;
4249 struct tog_blame_view_state *s = &view->state.blame;
4251 SIMPLEQ_INIT(&s->blamed_commits);
4253 s->path = strdup(path);
4254 if (s->path == NULL)
4255 return got_error_from_errno("strdup");
4257 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4263 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4264 s->first_displayed_line = 1;
4265 s->last_displayed_line = view->nlines;
4266 s->selected_line = 1;
4267 s->blame_complete = 0;
4269 s->commit_id = commit_id;
4270 memset(&s->blame, 0, sizeof(s->blame));
4272 SIMPLEQ_INIT(&s->colors);
4273 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4274 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4275 get_color_value("TOG_COLOR_COMMIT"));
4280 view->show = show_blame_view;
4281 view->input = input_blame_view;
4282 view->close = close_blame_view;
4283 view->search_start = search_start_blame_view;
4284 view->search_next = search_next_blame_view;
4286 return run_blame(view);
4289 static const struct got_error *
4290 close_blame_view(struct tog_view *view)
4292 const struct got_error *err = NULL;
4293 struct tog_blame_view_state *s = &view->state.blame;
4295 if (s->blame.thread)
4296 err = stop_blame(&s->blame);
4298 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4299 struct got_object_qid *blamed_commit;
4300 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4301 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4302 got_object_qid_free(blamed_commit);
4306 free_colors(&s->colors);
4311 static const struct got_error *
4312 search_start_blame_view(struct tog_view *view)
4314 struct tog_blame_view_state *s = &view->state.blame;
4316 s->matched_line = 0;
4320 static const struct got_error *
4321 search_next_blame_view(struct tog_view *view)
4323 struct tog_blame_view_state *s = &view->state.blame;
4326 if (!view->searching) {
4327 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4331 if (s->matched_line) {
4332 if (view->searching == TOG_SEARCH_FORWARD)
4333 lineno = s->matched_line + 1;
4335 lineno = s->matched_line - 1;
4337 if (view->searching == TOG_SEARCH_FORWARD)
4340 lineno = s->blame.nlines;
4348 if (lineno <= 0 || lineno > s->blame.nlines) {
4349 if (s->matched_line == 0) {
4350 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4355 if (view->searching == TOG_SEARCH_FORWARD)
4358 lineno = s->blame.nlines;
4361 offset = s->blame.line_offsets[lineno - 1];
4362 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4364 return got_error_from_errno("fseeko");
4367 line = parse_next_line(s->blame.f, &len);
4369 match_line(line, &view->regex, 1, &view->regmatch)) {
4370 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4371 s->matched_line = lineno;
4376 if (view->searching == TOG_SEARCH_FORWARD)
4382 if (s->matched_line) {
4383 s->first_displayed_line = s->matched_line;
4384 s->selected_line = 1;
4390 static const struct got_error *
4391 show_blame_view(struct tog_view *view)
4393 const struct got_error *err = NULL;
4394 struct tog_blame_view_state *s = &view->state.blame;
4397 if (s->blame.thread == NULL) {
4398 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4399 &s->blame.thread_args);
4401 return got_error_set_errno(errcode, "pthread_create");
4403 halfdelay(1); /* fast refresh while annotating */
4406 if (s->blame_complete)
4407 halfdelay(10); /* disable fast refresh */
4409 err = draw_blame(view);
4415 static const struct got_error *
4416 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4418 const struct got_error *err = NULL, *thread_err = NULL;
4419 struct tog_view *diff_view;
4420 struct tog_blame_view_state *s = &view->state.blame;
4429 if (s->selected_line > 1)
4431 else if (s->selected_line == 1 &&
4432 s->first_displayed_line > 1)
4433 s->first_displayed_line--;
4437 if (s->first_displayed_line == 1) {
4438 s->selected_line = 1;
4441 if (s->first_displayed_line > view->nlines - 2)
4442 s->first_displayed_line -=
4445 s->first_displayed_line = 1;
4449 if (s->selected_line < view->nlines - 2 &&
4450 s->first_displayed_line +
4451 s->selected_line <= s->blame.nlines)
4453 else if (s->last_displayed_line <
4455 s->first_displayed_line++;
4459 struct got_object_id *id = NULL;
4460 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4461 s->first_displayed_line, s->selected_line);
4465 struct got_commit_object *commit;
4466 struct got_object_qid *pid;
4467 struct got_object_id *blob_id = NULL;
4469 err = got_object_open_as_commit(&commit,
4473 pid = SIMPLEQ_FIRST(
4474 got_object_commit_get_parent_ids(commit));
4476 got_object_commit_close(commit);
4479 /* Check if path history ends here. */
4480 err = got_object_id_by_path(&blob_id, s->repo,
4483 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4485 got_object_commit_close(commit);
4488 err = got_object_get_type(&obj_type, s->repo,
4491 /* Can't blame non-blob type objects. */
4492 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4493 got_object_commit_close(commit);
4496 err = got_object_qid_alloc(&s->blamed_commit,
4498 got_object_commit_close(commit);
4500 if (got_object_id_cmp(id,
4501 s->blamed_commit->id) == 0)
4503 err = got_object_qid_alloc(&s->blamed_commit,
4509 thread_err = stop_blame(&s->blame);
4513 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4514 s->blamed_commit, entry);
4515 err = run_blame(view);
4521 struct got_object_qid *first;
4522 first = SIMPLEQ_FIRST(&s->blamed_commits);
4523 if (!got_object_id_cmp(first->id, s->commit_id))
4526 thread_err = stop_blame(&s->blame);
4530 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4531 got_object_qid_free(s->blamed_commit);
4533 SIMPLEQ_FIRST(&s->blamed_commits);
4534 err = run_blame(view);
4541 struct got_object_id *id = NULL;
4542 struct got_object_qid *pid;
4543 struct got_commit_object *commit = NULL;
4544 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4545 s->first_displayed_line, s->selected_line);
4548 err = got_object_open_as_commit(&commit, s->repo, id);
4551 pid = SIMPLEQ_FIRST(
4552 got_object_commit_get_parent_ids(commit));
4553 if (view_is_parent_view(view))
4554 begin_x = view_split_begin_x(view->begin_x);
4555 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4556 if (diff_view == NULL) {
4557 got_object_commit_close(commit);
4558 err = got_error_from_errno("view_open");
4561 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4562 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4563 got_object_commit_close(commit);
4565 view_close(diff_view);
4569 diff_view->focussed = 1;
4570 if (view_is_parent_view(view)) {
4571 err = view_close_child(view);
4574 view_set_child(view, diff_view);
4575 view->focus_child = 1;
4577 *new_view = diff_view;
4585 if (s->last_displayed_line >= s->blame.nlines &&
4586 s->selected_line >= MIN(s->blame.nlines,
4587 view->nlines - 2)) {
4590 if (s->last_displayed_line >= s->blame.nlines &&
4591 s->selected_line < view->nlines - 2) {
4592 s->selected_line = MIN(s->blame.nlines,
4596 if (s->last_displayed_line + view->nlines - 2
4598 s->first_displayed_line +=
4601 s->first_displayed_line =
4606 if (s->selected_line > view->nlines - 2) {
4607 s->selected_line = MIN(s->blame.nlines,
4614 return thread_err ? thread_err : err;
4617 static const struct got_error *
4618 cmd_blame(int argc, char *argv[])
4620 const struct got_error *error;
4621 struct got_repository *repo = NULL;
4622 struct got_worktree *worktree = NULL;
4623 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4624 char *link_target = NULL;
4625 struct got_object_id *commit_id = NULL;
4626 char *commit_id_str = NULL;
4628 struct tog_view *view;
4630 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4633 commit_id_str = optarg;
4636 repo_path = realpath(optarg, NULL);
4637 if (repo_path == NULL)
4638 return got_error_from_errno2("realpath",
4653 cwd = getcwd(NULL, 0);
4655 return got_error_from_errno("getcwd");
4657 error = got_worktree_open(&worktree, cwd);
4658 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4661 if (repo_path == NULL) {
4664 strdup(got_worktree_get_repo_path(worktree));
4666 repo_path = strdup(cwd);
4668 if (repo_path == NULL) {
4669 error = got_error_from_errno("strdup");
4673 error = got_repo_open(&repo, repo_path, NULL);
4677 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4684 error = apply_unveil(got_repo_get_path(repo), NULL);
4688 if (commit_id_str == NULL) {
4689 struct got_reference *head_ref;
4690 error = got_ref_open(&head_ref, repo, worktree ?
4691 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4694 error = got_ref_resolve(&commit_id, repo, head_ref);
4695 got_ref_close(head_ref);
4697 error = got_repo_match_object_id(&commit_id, NULL,
4698 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4703 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4705 error = got_error_from_errno("view_open");
4709 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4714 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4719 /* Release work tree lock. */
4720 got_worktree_close(worktree);
4723 error = view_loop(view);
4731 got_worktree_close(worktree);
4733 got_repo_close(repo);
4737 static const struct got_error *
4738 draw_tree_entries(struct tog_view *view, const char *parent_path)
4740 struct tog_tree_view_state *s = &view->state.tree;
4741 const struct got_error *err = NULL;
4742 struct got_tree_entry *te;
4744 struct tog_color *tc;
4745 int width, n, i, nentries;
4746 int limit = view->nlines;
4750 werase(view->window);
4755 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4758 if (view_needs_focus_indication(view))
4759 wstandout(view->window);
4760 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4762 wattr_on(view->window,
4763 COLOR_PAIR(tc->colorpair), NULL);
4764 waddwstr(view->window, wline);
4766 wattr_off(view->window,
4767 COLOR_PAIR(tc->colorpair), NULL);
4768 if (view_needs_focus_indication(view))
4769 wstandend(view->window);
4772 if (width < view->ncols - 1)
4773 waddch(view->window, '\n');
4776 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4779 waddwstr(view->window, wline);
4782 if (width < view->ncols - 1)
4783 waddch(view->window, '\n');
4786 waddch(view->window, '\n');
4790 if (s->first_displayed_entry == NULL) {
4791 te = got_object_tree_get_first_entry(s->tree);
4792 if (s->selected == 0) {
4794 wstandout(view->window);
4795 s->selected_entry = NULL;
4797 waddstr(view->window, " ..\n"); /* parent directory */
4798 if (s->selected == 0 && view->focussed)
4799 wstandend(view->window);
4806 te = s->first_displayed_entry;
4809 nentries = got_object_tree_get_nentries(s->tree);
4810 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4811 char *line = NULL, *id_str = NULL, *link_target = NULL;
4812 const char *modestr = "";
4815 te = got_object_tree_get_entry(s->tree, i);
4816 mode = got_tree_entry_get_mode(te);
4819 err = got_object_id_str(&id_str,
4820 got_tree_entry_get_id(te));
4822 return got_error_from_errno(
4823 "got_object_id_str");
4825 if (got_object_tree_entry_is_submodule(te))
4827 else if (S_ISLNK(mode)) {
4830 err = got_tree_entry_get_symlink_target(&link_target,
4836 for (i = 0; i < strlen(link_target); i++) {
4837 if (!isprint((unsigned char)link_target[i]))
4838 link_target[i] = '?';
4842 else if (S_ISDIR(mode))
4844 else if (mode & S_IXUSR)
4846 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4847 got_tree_entry_get_name(te), modestr,
4848 link_target ? " -> ": "",
4849 link_target ? link_target : "") == -1) {
4852 return got_error_from_errno("asprintf");
4856 err = format_line(&wline, &width, line, view->ncols, 0);
4861 if (n == s->selected) {
4863 wstandout(view->window);
4864 s->selected_entry = te;
4866 tc = match_color(&s->colors, line);
4868 wattr_on(view->window,
4869 COLOR_PAIR(tc->colorpair), NULL);
4870 waddwstr(view->window, wline);
4872 wattr_off(view->window,
4873 COLOR_PAIR(tc->colorpair), NULL);
4874 if (width < view->ncols - 1)
4875 waddch(view->window, '\n');
4876 if (n == s->selected && view->focussed)
4877 wstandend(view->window);
4883 s->last_displayed_entry = te;
4892 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4894 struct got_tree_entry *te;
4895 int isroot = s->tree == s->root;
4898 if (s->first_displayed_entry == NULL)
4901 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4902 while (i++ < maxscroll) {
4905 s->first_displayed_entry = NULL;
4908 s->first_displayed_entry = te;
4909 te = got_tree_entry_get_prev(s->tree, te);
4914 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4916 struct got_tree_entry *next, *last;
4919 if (s->first_displayed_entry)
4920 next = got_tree_entry_get_next(s->tree,
4921 s->first_displayed_entry);
4923 next = got_object_tree_get_first_entry(s->tree);
4925 last = s->last_displayed_entry;
4926 while (next && last && n++ < maxscroll) {
4927 last = got_tree_entry_get_next(s->tree, last);
4929 s->first_displayed_entry = next;
4930 next = got_tree_entry_get_next(s->tree, next);
4935 static const struct got_error *
4936 tree_entry_path(char **path, struct tog_parent_trees *parents,
4937 struct got_tree_entry *te)
4939 const struct got_error *err = NULL;
4940 struct tog_parent_tree *pt;
4941 size_t len = 2; /* for leading slash and NUL */
4943 TAILQ_FOREACH(pt, parents, entry)
4944 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4947 len += strlen(got_tree_entry_get_name(te));
4949 *path = calloc(1, len);
4951 return got_error_from_errno("calloc");
4954 pt = TAILQ_LAST(parents, tog_parent_trees);
4956 const char *name = got_tree_entry_get_name(pt->selected_entry);
4957 if (strlcat(*path, name, len) >= len) {
4958 err = got_error(GOT_ERR_NO_SPACE);
4961 if (strlcat(*path, "/", len) >= len) {
4962 err = got_error(GOT_ERR_NO_SPACE);
4965 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4968 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4969 err = got_error(GOT_ERR_NO_SPACE);
4981 static const struct got_error *
4982 blame_tree_entry(struct tog_view **new_view, int begin_x,
4983 struct got_tree_entry *te, struct tog_parent_trees *parents,
4984 struct got_object_id *commit_id, struct got_repository *repo)
4986 const struct got_error *err = NULL;
4988 struct tog_view *blame_view;
4992 err = tree_entry_path(&path, parents, te);
4996 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4997 if (blame_view == NULL) {
4998 err = got_error_from_errno("view_open");
5002 err = open_blame_view(blame_view, path, commit_id, repo);
5004 if (err->code == GOT_ERR_CANCELLED)
5006 view_close(blame_view);
5008 *new_view = blame_view;
5014 static const struct got_error *
5015 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5016 struct tog_tree_view_state *s)
5018 struct tog_view *log_view;
5019 const struct got_error *err = NULL;
5024 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5025 if (log_view == NULL)
5026 return got_error_from_errno("view_open");
5028 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5032 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5035 view_close(log_view);
5037 *new_view = log_view;
5042 static const struct got_error *
5043 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5044 struct got_object_id *commit_id, const char *head_ref_name,
5045 struct got_repository *repo)
5047 const struct got_error *err = NULL;
5048 char *commit_id_str = NULL;
5049 struct tog_tree_view_state *s = &view->state.tree;
5051 TAILQ_INIT(&s->parents);
5053 err = got_object_id_str(&commit_id_str, commit_id);
5057 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5058 err = got_error_from_errno("asprintf");
5062 s->root = s->tree = root;
5063 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5064 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5065 s->commit_id = got_object_id_dup(commit_id);
5066 if (s->commit_id == NULL) {
5067 err = got_error_from_errno("got_object_id_dup");
5070 s->head_ref_name = head_ref_name;
5073 SIMPLEQ_INIT(&s->colors);
5075 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5076 err = add_color(&s->colors, "\\$$",
5077 TOG_COLOR_TREE_SUBMODULE,
5078 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5081 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5082 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5084 free_colors(&s->colors);
5087 err = add_color(&s->colors, "/$",
5088 TOG_COLOR_TREE_DIRECTORY,
5089 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5091 free_colors(&s->colors);
5095 err = add_color(&s->colors, "\\*$",
5096 TOG_COLOR_TREE_EXECUTABLE,
5097 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5099 free_colors(&s->colors);
5103 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5104 get_color_value("TOG_COLOR_COMMIT"));
5106 free_colors(&s->colors);
5111 view->show = show_tree_view;
5112 view->input = input_tree_view;
5113 view->close = close_tree_view;
5114 view->search_start = search_start_tree_view;
5115 view->search_next = search_next_tree_view;
5117 free(commit_id_str);
5119 free(s->tree_label);
5120 s->tree_label = NULL;
5125 static const struct got_error *
5126 close_tree_view(struct tog_view *view)
5128 struct tog_tree_view_state *s = &view->state.tree;
5130 free_colors(&s->colors);
5131 free(s->tree_label);
5132 s->tree_label = NULL;
5134 s->commit_id = NULL;
5135 while (!TAILQ_EMPTY(&s->parents)) {
5136 struct tog_parent_tree *parent;
5137 parent = TAILQ_FIRST(&s->parents);
5138 TAILQ_REMOVE(&s->parents, parent, entry);
5142 if (s->tree != s->root)
5143 got_object_tree_close(s->tree);
5144 got_object_tree_close(s->root);
5148 static const struct got_error *
5149 search_start_tree_view(struct tog_view *view)
5151 struct tog_tree_view_state *s = &view->state.tree;
5153 s->matched_entry = NULL;
5158 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5160 regmatch_t regmatch;
5162 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5166 static const struct got_error *
5167 search_next_tree_view(struct tog_view *view)
5169 struct tog_tree_view_state *s = &view->state.tree;
5170 struct got_tree_entry *te = NULL;
5172 if (!view->searching) {
5173 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5177 if (s->matched_entry) {
5178 if (view->searching == TOG_SEARCH_FORWARD) {
5179 if (s->selected_entry)
5180 te = got_tree_entry_get_next(s->tree,
5183 te = got_object_tree_get_first_entry(s->tree);
5185 if (s->selected_entry == NULL)
5186 te = got_object_tree_get_last_entry(s->tree);
5188 te = got_tree_entry_get_prev(s->tree,
5192 if (view->searching == TOG_SEARCH_FORWARD)
5193 te = got_object_tree_get_first_entry(s->tree);
5195 te = got_object_tree_get_last_entry(s->tree);
5200 if (s->matched_entry == NULL) {
5201 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5204 if (view->searching == TOG_SEARCH_FORWARD)
5205 te = got_object_tree_get_first_entry(s->tree);
5207 te = got_object_tree_get_last_entry(s->tree);
5210 if (match_tree_entry(te, &view->regex)) {
5211 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5212 s->matched_entry = te;
5216 if (view->searching == TOG_SEARCH_FORWARD)
5217 te = got_tree_entry_get_next(s->tree, te);
5219 te = got_tree_entry_get_prev(s->tree, te);
5222 if (s->matched_entry) {
5223 s->first_displayed_entry = s->matched_entry;
5230 static const struct got_error *
5231 show_tree_view(struct tog_view *view)
5233 const struct got_error *err = NULL;
5234 struct tog_tree_view_state *s = &view->state.tree;
5237 err = tree_entry_path(&parent_path, &s->parents, NULL);
5241 err = draw_tree_entries(view, parent_path);
5248 static const struct got_error *
5249 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5251 const struct got_error *err = NULL;
5252 struct tog_tree_view_state *s = &view->state.tree;
5253 struct tog_view *log_view, *ref_view;
5258 s->show_ids = !s->show_ids;
5261 if (!s->selected_entry)
5263 if (view_is_parent_view(view))
5264 begin_x = view_split_begin_x(view->begin_x);
5265 err = log_selected_tree_entry(&log_view, begin_x, s);
5267 log_view->focussed = 1;
5268 if (view_is_parent_view(view)) {
5269 err = view_close_child(view);
5272 view_set_child(view, log_view);
5273 view->focus_child = 1;
5275 *new_view = log_view;
5278 if (view_is_parent_view(view))
5279 begin_x = view_split_begin_x(view->begin_x);
5280 ref_view = view_open(view->nlines, view->ncols,
5281 view->begin_y, begin_x, TOG_VIEW_REF);
5282 if (ref_view == NULL)
5283 return got_error_from_errno("view_open");
5284 err = open_ref_view(ref_view, s->repo);
5286 view_close(ref_view);
5290 ref_view->focussed = 1;
5291 if (view_is_parent_view(view)) {
5292 err = view_close_child(view);
5295 view_set_child(view, ref_view);
5296 view->focus_child = 1;
5298 *new_view = ref_view;
5302 if (s->selected > 0) {
5306 tree_scroll_up(s, 1);
5310 if (s->tree == s->root) {
5311 if (got_object_tree_get_first_entry(s->tree) ==
5312 s->first_displayed_entry)
5315 if (s->first_displayed_entry == NULL)
5318 tree_scroll_up(s, MAX(0, view->nlines - 3));
5322 if (s->selected < s->ndisplayed - 1) {
5326 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5328 /* can't scroll any further */
5330 tree_scroll_down(s, 1);
5334 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5336 /* can't scroll any further; move cursor down */
5337 if (s->selected < s->ndisplayed - 1)
5338 s->selected = s->ndisplayed - 1;
5341 tree_scroll_down(s, view->nlines - 3);
5346 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5347 struct tog_parent_tree *parent;
5348 /* user selected '..' */
5349 if (s->tree == s->root)
5351 parent = TAILQ_FIRST(&s->parents);
5352 TAILQ_REMOVE(&s->parents, parent,
5354 got_object_tree_close(s->tree);
5355 s->tree = parent->tree;
5356 s->first_displayed_entry =
5357 parent->first_displayed_entry;
5359 parent->selected_entry;
5360 s->selected = parent->selected;
5362 } else if (S_ISDIR(got_tree_entry_get_mode(
5363 s->selected_entry))) {
5364 struct got_tree_object *subtree;
5365 err = got_object_open_as_tree(&subtree, s->repo,
5366 got_tree_entry_get_id(s->selected_entry));
5369 err = tree_view_visit_subtree(s, subtree);
5371 got_object_tree_close(subtree);
5374 } else if (S_ISREG(got_tree_entry_get_mode(
5375 s->selected_entry))) {
5376 struct tog_view *blame_view;
5377 int begin_x = view_is_parent_view(view) ?
5378 view_split_begin_x(view->begin_x) : 0;
5380 err = blame_tree_entry(&blame_view, begin_x,
5381 s->selected_entry, &s->parents,
5382 s->commit_id, s->repo);
5386 blame_view->focussed = 1;
5387 if (view_is_parent_view(view)) {
5388 err = view_close_child(view);
5391 view_set_child(view, blame_view);
5392 view->focus_child = 1;
5394 *new_view = blame_view;
5398 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5399 s->selected = view->nlines - 4;
5412 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5417 static const struct got_error *
5418 cmd_tree(int argc, char *argv[])
5420 const struct got_error *error;
5421 struct got_repository *repo = NULL;
5422 struct got_worktree *worktree = NULL;
5423 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5424 struct got_object_id *commit_id = NULL;
5425 const char *commit_id_arg = NULL;
5427 struct got_commit_object *commit = NULL;
5428 struct got_tree_object *tree = NULL;
5429 struct got_reference *ref = NULL;
5430 const char *head_ref_name = NULL;
5432 struct tog_view *view;
5434 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5437 commit_id_arg = optarg;
5440 repo_path = realpath(optarg, NULL);
5441 if (repo_path == NULL)
5442 return got_error_from_errno2("realpath",
5457 cwd = getcwd(NULL, 0);
5459 return got_error_from_errno("getcwd");
5461 error = got_worktree_open(&worktree, cwd);
5462 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5465 if (repo_path == NULL) {
5468 strdup(got_worktree_get_repo_path(worktree));
5470 repo_path = strdup(cwd);
5472 if (repo_path == NULL) {
5473 error = got_error_from_errno("strdup");
5477 error = got_repo_open(&repo, repo_path, NULL);
5481 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5488 error = apply_unveil(got_repo_get_path(repo), NULL);
5492 if (commit_id_arg == NULL) {
5493 error = got_repo_match_object_id(&commit_id, &label,
5494 worktree ? got_worktree_get_head_ref_name(worktree) :
5495 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5498 head_ref_name = label;
5500 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5502 head_ref_name = got_ref_get_name(ref);
5503 else if (error->code != GOT_ERR_NOT_REF)
5505 error = got_repo_match_object_id(&commit_id, NULL,
5506 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5511 error = got_object_open_as_commit(&commit, repo, commit_id);
5515 error = got_object_open_as_tree(&tree, repo,
5516 got_object_commit_get_tree_id(commit));
5520 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5522 error = got_error_from_errno("view_open");
5525 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5528 if (!got_path_is_root_dir(in_repo_path)) {
5529 error = tree_view_walk_path(&view->state.tree, commit_id,
5536 /* Release work tree lock. */
5537 got_worktree_close(worktree);
5540 error = view_loop(view);
5549 got_object_commit_close(commit);
5551 got_object_tree_close(tree);
5553 got_repo_close(repo);
5557 static const struct got_error *
5558 ref_view_load_refs(struct tog_ref_view_state *s)
5560 const struct got_error *err;
5561 struct got_reflist_entry *sre;
5562 struct tog_reflist_entry *re;
5564 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5565 got_ref_cmp_by_name, NULL);
5570 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5571 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5574 re = malloc(sizeof(*re));
5576 return got_error_from_errno("malloc");
5579 re->idx = s->nrefs++;
5580 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5587 ref_view_free_refs(struct tog_ref_view_state *s)
5589 struct tog_reflist_entry *re;
5591 while (!TAILQ_EMPTY(&s->refs)) {
5592 re = TAILQ_FIRST(&s->refs);
5593 TAILQ_REMOVE(&s->refs, re, entry);
5596 got_ref_list_free(&s->simplerefs);
5599 static const struct got_error *
5600 open_ref_view(struct tog_view *view, struct got_repository *repo)
5602 const struct got_error *err = NULL;
5603 struct tog_ref_view_state *s = &view->state.ref;
5605 s->selected_entry = 0;
5608 SIMPLEQ_INIT(&s->simplerefs);
5609 TAILQ_INIT(&s->refs);
5610 SIMPLEQ_INIT(&s->colors);
5612 err = ref_view_load_refs(s);
5616 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5618 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5619 err = add_color(&s->colors, "^refs/heads/",
5620 TOG_COLOR_REFS_HEADS,
5621 get_color_value("TOG_COLOR_REFS_HEADS"));
5625 err = add_color(&s->colors, "^refs/tags/",
5626 TOG_COLOR_REFS_TAGS,
5627 get_color_value("TOG_COLOR_REFS_TAGS"));
5631 err = add_color(&s->colors, "^refs/remotes/",
5632 TOG_COLOR_REFS_REMOTES,
5633 get_color_value("TOG_COLOR_REFS_REMOTES"));
5638 view->show = show_ref_view;
5639 view->input = input_ref_view;
5640 view->close = close_ref_view;
5641 view->search_start = search_start_ref_view;
5642 view->search_next = search_next_ref_view;
5645 free_colors(&s->colors);
5649 static const struct got_error *
5650 close_ref_view(struct tog_view *view)
5652 struct tog_ref_view_state *s = &view->state.ref;
5654 ref_view_free_refs(s);
5655 free_colors(&s->colors);
5660 static const struct got_error *
5661 resolve_reflist_entry(struct got_object_id **commit_id,
5662 struct tog_reflist_entry *re, struct got_repository *repo)
5664 const struct got_error *err = NULL;
5665 struct got_object_id *obj_id;
5666 struct got_tag_object *tag = NULL;
5671 err = got_ref_resolve(&obj_id, repo, re->ref);
5675 err = got_object_get_type(&obj_type, repo, obj_id);
5680 case GOT_OBJ_TYPE_COMMIT:
5681 *commit_id = obj_id;
5683 case GOT_OBJ_TYPE_TAG:
5684 err = got_object_open_as_tag(&tag, repo, obj_id);
5688 err = got_object_get_type(&obj_type, repo,
5689 got_object_tag_get_object_id(tag));
5692 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5693 err = got_error(GOT_ERR_OBJ_TYPE);
5696 *commit_id = got_object_id_dup(
5697 got_object_tag_get_object_id(tag));
5698 if (*commit_id == NULL) {
5699 err = got_error_from_errno("got_object_id_dup");
5704 err = got_error(GOT_ERR_OBJ_TYPE);
5710 got_object_tag_close(tag);
5718 static const struct got_error *
5719 log_ref_entry(struct tog_view **new_view, int begin_x,
5720 struct tog_reflist_entry *re, struct got_repository *repo)
5722 struct tog_view *log_view;
5723 const struct got_error *err = NULL;
5724 struct got_object_id *commit_id = NULL;
5728 err = resolve_reflist_entry(&commit_id, re, repo);
5730 if (err->code != GOT_ERR_OBJ_TYPE)
5736 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5737 if (log_view == NULL) {
5738 err = got_error_from_errno("view_open");
5742 err = open_log_view(log_view, commit_id, repo,
5743 got_ref_get_name(re->ref), "", 0);
5746 view_close(log_view);
5748 *new_view = log_view;
5754 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5756 struct tog_reflist_entry *re;
5759 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5762 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5763 while (i++ < maxscroll) {
5766 s->first_displayed_entry = re;
5767 re = TAILQ_PREV(re, tog_reflist_head, entry);
5772 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5774 struct tog_reflist_entry *next, *last;
5777 if (s->first_displayed_entry)
5778 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5780 next = TAILQ_FIRST(&s->refs);
5782 last = s->last_displayed_entry;
5783 while (next && last && n++ < maxscroll) {
5784 last = TAILQ_NEXT(last, entry);
5786 s->first_displayed_entry = next;
5787 next = TAILQ_NEXT(next, entry);
5792 static const struct got_error *
5793 search_start_ref_view(struct tog_view *view)
5795 struct tog_ref_view_state *s = &view->state.ref;
5797 s->matched_entry = NULL;
5802 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5804 regmatch_t regmatch;
5806 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5810 static const struct got_error *
5811 search_next_ref_view(struct tog_view *view)
5813 struct tog_ref_view_state *s = &view->state.ref;
5814 struct tog_reflist_entry *re = NULL;
5816 if (!view->searching) {
5817 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5821 if (s->matched_entry) {
5822 if (view->searching == TOG_SEARCH_FORWARD) {
5823 if (s->selected_entry)
5824 re = TAILQ_NEXT(s->selected_entry, entry);
5826 re = TAILQ_PREV(s->selected_entry,
5827 tog_reflist_head, entry);
5829 if (s->selected_entry == NULL)
5830 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5832 re = TAILQ_PREV(s->selected_entry,
5833 tog_reflist_head, entry);
5836 if (view->searching == TOG_SEARCH_FORWARD)
5837 re = TAILQ_FIRST(&s->refs);
5839 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5844 if (s->matched_entry == NULL) {
5845 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5848 if (view->searching == TOG_SEARCH_FORWARD)
5849 re = TAILQ_FIRST(&s->refs);
5851 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5854 if (match_reflist_entry(re, &view->regex)) {
5855 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5856 s->matched_entry = re;
5860 if (view->searching == TOG_SEARCH_FORWARD)
5861 re = TAILQ_NEXT(re, entry);
5863 re = TAILQ_PREV(re, tog_reflist_head, entry);
5866 if (s->matched_entry) {
5867 s->first_displayed_entry = s->matched_entry;
5874 static const struct got_error *
5875 show_ref_view(struct tog_view *view)
5877 const struct got_error *err = NULL;
5878 struct tog_ref_view_state *s = &view->state.ref;
5879 struct tog_reflist_entry *re;
5882 struct tog_color *tc;
5884 int limit = view->nlines;
5886 werase(view->window);
5893 re = s->first_displayed_entry;
5895 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5897 return got_error_from_errno("asprintf");
5899 err = format_line(&wline, &width, line, view->ncols, 0);
5904 if (view_needs_focus_indication(view))
5905 wstandout(view->window);
5906 waddwstr(view->window, wline);
5907 if (view_needs_focus_indication(view))
5908 wstandend(view->window);
5913 if (width < view->ncols - 1)
5914 waddch(view->window, '\n');
5919 while (re && limit > 0) {
5922 if (got_ref_is_symbolic(re->ref)) {
5923 if (asprintf(&line, "%s -> %s",
5924 got_ref_get_name(re->ref),
5925 got_ref_get_symref_target(re->ref)) == -1)
5926 return got_error_from_errno("asprintf");
5927 } else if (s->show_ids) {
5928 struct got_object_id *id;
5930 err = got_ref_resolve(&id, s->repo, re->ref);
5933 err = got_object_id_str(&id_str, id);
5938 if (asprintf(&line, "%s: %s",
5939 got_ref_get_name(re->ref), id_str) == -1) {
5940 err = got_error_from_errno("asprintf");
5948 line = strdup(got_ref_get_name(re->ref));
5950 return got_error_from_errno("strdup");
5953 err = format_line(&wline, &width, line, view->ncols, 0);
5958 if (n == s->selected) {
5960 wstandout(view->window);
5961 s->selected_entry = re;
5963 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5965 wattr_on(view->window,
5966 COLOR_PAIR(tc->colorpair), NULL);
5967 waddwstr(view->window, wline);
5969 wattr_off(view->window,
5970 COLOR_PAIR(tc->colorpair), NULL);
5971 if (width < view->ncols - 1)
5972 waddch(view->window, '\n');
5973 if (n == s->selected && view->focussed)
5974 wstandend(view->window);
5980 s->last_displayed_entry = re;
5983 re = TAILQ_NEXT(re, entry);
5990 static const struct got_error *
5991 browse_ref_tree(struct tog_view **new_view, int begin_x,
5992 struct tog_reflist_entry *re, struct got_repository *repo)
5994 const struct got_error *err = NULL;
5995 struct got_object_id *commit_id = NULL, *tree_id = NULL;
5996 struct got_tree_object *tree = NULL;
5997 struct tog_view *tree_view;
6001 err = resolve_reflist_entry(&commit_id, re, repo);
6003 if (err->code != GOT_ERR_OBJ_TYPE)
6009 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6013 err = got_object_open_as_tree(&tree, repo, tree_id);
6017 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6018 if (tree_view == NULL) {
6019 err = got_error_from_errno("view_open");
6023 err = open_tree_view(tree_view, tree, commit_id,
6024 got_ref_get_name(re->ref), repo);
6028 *new_view = tree_view;
6034 got_object_tree_close(tree);
6038 static const struct got_error *
6039 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6041 const struct got_error *err = NULL;
6042 struct tog_ref_view_state *s = &view->state.ref;
6043 struct tog_view *log_view, *tree_view;
6048 s->show_ids = !s->show_ids;
6052 if (!s->selected_entry)
6054 if (view_is_parent_view(view))
6055 begin_x = view_split_begin_x(view->begin_x);
6056 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6059 log_view->focussed = 1;
6060 if (view_is_parent_view(view)) {
6061 err = view_close_child(view);
6064 view_set_child(view, log_view);
6065 view->focus_child = 1;
6067 *new_view = log_view;
6070 if (!s->selected_entry)
6072 if (view_is_parent_view(view))
6073 begin_x = view_split_begin_x(view->begin_x);
6074 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6076 if (err || tree_view == NULL)
6079 tree_view->focussed = 1;
6080 if (view_is_parent_view(view)) {
6081 err = view_close_child(view);
6084 view_set_child(view, tree_view);
6085 view->focus_child = 1;
6087 *new_view = tree_view;
6091 if (s->selected > 0) {
6095 ref_scroll_up(s, 1);
6099 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6101 ref_scroll_up(s, MAX(0, view->nlines - 1));
6105 if (s->selected < s->ndisplayed - 1) {
6109 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6110 /* can't scroll any further */
6112 ref_scroll_down(s, 1);
6116 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6117 /* can't scroll any further; move cursor down */
6118 if (s->selected < s->ndisplayed - 1)
6119 s->selected = s->ndisplayed - 1;
6122 ref_scroll_down(s, view->nlines - 1);
6125 ref_view_free_refs(s);
6126 err = ref_view_load_refs(s);
6129 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6130 s->selected = view->nlines - 2;
6143 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6148 static const struct got_error *
6149 cmd_ref(int argc, char *argv[])
6151 const struct got_error *error;
6152 struct got_repository *repo = NULL;
6153 struct got_worktree *worktree = NULL;
6154 char *cwd = NULL, *repo_path = NULL;
6156 struct tog_view *view;
6158 while ((ch = getopt(argc, argv, "r:")) != -1) {
6161 repo_path = realpath(optarg, NULL);
6162 if (repo_path == NULL)
6163 return got_error_from_errno2("realpath",
6178 cwd = getcwd(NULL, 0);
6180 return got_error_from_errno("getcwd");
6182 error = got_worktree_open(&worktree, cwd);
6183 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6186 if (repo_path == NULL) {
6189 strdup(got_worktree_get_repo_path(worktree));
6191 repo_path = strdup(cwd);
6193 if (repo_path == NULL) {
6194 error = got_error_from_errno("strdup");
6198 error = got_repo_open(&repo, repo_path, NULL);
6204 error = apply_unveil(got_repo_get_path(repo), NULL);
6208 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6210 error = got_error_from_errno("view_open");
6214 error = open_ref_view(view, repo);
6219 /* Release work tree lock. */
6220 got_worktree_close(worktree);
6223 error = view_loop(view);
6228 got_repo_close(repo);
6233 list_commands(FILE *fp)
6237 fprintf(fp, "commands:");
6238 for (i = 0; i < nitems(tog_commands); i++) {
6239 struct tog_cmd *cmd = &tog_commands[i];
6240 fprintf(fp, " %s", cmd->name);
6246 usage(int hflag, int status)
6248 FILE *fp = (status == 0) ? stdout : stderr;
6250 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6253 fprintf(fp, "lazy usage: %s path\n", getprogname());
6260 make_argv(int argc, ...)
6268 argv = calloc(argc, sizeof(char *));
6271 for (i = 0; i < argc; i++) {
6272 argv[i] = strdup(va_arg(ap, char *));
6273 if (argv[i] == NULL)
6282 * Try to convert 'tog path' into a 'tog log path' command.
6283 * The user could simply have mistyped the command rather than knowingly
6284 * provided a path. So check whether argv[0] can in fact be resolved
6285 * to a path in the HEAD commit and print a special error if not.
6286 * This hack is for mpi@ <3
6288 static const struct got_error *
6289 tog_log_with_path(int argc, char *argv[])
6291 const struct got_error *error = NULL;
6292 struct tog_cmd *cmd = NULL;
6293 struct got_repository *repo = NULL;
6294 struct got_worktree *worktree = NULL;
6295 struct got_object_id *commit_id = NULL, *id = NULL;
6296 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6297 char *commit_id_str = NULL, **cmd_argv = NULL;
6299 cwd = getcwd(NULL, 0);
6301 return got_error_from_errno("getcwd");
6303 error = got_worktree_open(&worktree, cwd);
6304 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6308 repo_path = strdup(got_worktree_get_repo_path(worktree));
6310 repo_path = strdup(cwd);
6311 if (repo_path == NULL) {
6312 error = got_error_from_errno("strdup");
6316 error = got_repo_open(&repo, repo_path, NULL);
6320 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6325 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6326 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6327 GOT_OBJ_TYPE_COMMIT, 1, repo);
6332 got_worktree_close(worktree);
6336 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6338 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6340 fprintf(stderr, "%s: '%s' is no known command or path\n",
6341 getprogname(), argv[0]);
6346 got_repo_close(repo);
6349 error = got_object_id_str(&commit_id_str, commit_id);
6353 cmd = &tog_commands[0]; /* log */
6355 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6356 error = cmd->cmd_main(argc, cmd_argv);
6359 got_repo_close(repo);
6361 got_worktree_close(worktree);
6363 free(commit_id_str);
6370 for (i = 0; i < argc; i++)
6378 main(int argc, char *argv[])
6380 const struct got_error *error = NULL;
6381 struct tog_cmd *cmd = NULL;
6382 int ch, hflag = 0, Vflag = 0;
6383 char **cmd_argv = NULL;
6384 static struct option longopts[] = {
6385 { "version", no_argument, NULL, 'V' },
6389 setlocale(LC_CTYPE, "");
6391 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6411 got_version_print_str();
6416 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6424 /* Build an argument vector which runs a default command. */
6425 cmd = &tog_commands[0];
6427 cmd_argv = make_argv(argc, cmd->name);
6431 /* Did the user specify a command? */
6432 for (i = 0; i < nitems(tog_commands); i++) {
6433 if (strncmp(tog_commands[i].name, argv[0],
6434 strlen(argv[0])) == 0) {
6435 cmd = &tog_commands[i];
6444 /* No command specified; try log with a path */
6445 error = tog_log_with_path(argc, argv);
6450 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6457 for (i = 0; i < argc; i++)
6462 if (error && error->code != GOT_ERR_CANCELLED)
6463 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);