2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
123 SIMPLEQ_ENTRY(tog_color) entry;
127 SIMPLEQ_HEAD(tog_colors, tog_color);
129 static const struct got_error *
130 add_color(struct tog_colors *colors, const char *pattern,
131 int idx, short color)
133 const struct got_error *err = NULL;
134 struct tog_color *tc;
137 if (idx < 1 || idx > COLOR_PAIRS - 1)
140 init_pair(idx, color, -1);
142 tc = calloc(1, sizeof(*tc));
144 return got_error_from_errno("calloc");
145 regerr = regcomp(&tc->regex, pattern,
146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
148 static char regerr_msg[512];
149 static char err_msg[512];
150 regerror(regerr, &tc->regex, regerr_msg,
152 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
154 err = got_error_msg(GOT_ERR_REGEX, err_msg);
159 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
164 free_colors(struct tog_colors *colors)
166 struct tog_color *tc;
168 while (!SIMPLEQ_EMPTY(colors)) {
169 tc = SIMPLEQ_FIRST(colors);
170 SIMPLEQ_REMOVE_HEAD(colors, entry);
177 get_color(struct tog_colors *colors, int colorpair)
179 struct tog_color *tc = NULL;
181 SIMPLEQ_FOREACH(tc, colors, entry) {
182 if (tc->colorpair == colorpair)
190 default_color_value(const char *envvar)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 return COLOR_MAGENTA;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
196 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
198 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
200 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 return COLOR_MAGENTA;
202 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 return COLOR_MAGENTA;
204 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
206 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
208 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
210 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
212 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
214 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
216 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 return COLOR_MAGENTA;
218 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
225 get_color_value(const char *envvar)
227 const char *val = getenv(envvar);
230 return default_color_value(envvar);
232 if (strcasecmp(val, "black") == 0)
234 if (strcasecmp(val, "red") == 0)
236 if (strcasecmp(val, "green") == 0)
238 if (strcasecmp(val, "yellow") == 0)
240 if (strcasecmp(val, "blue") == 0)
242 if (strcasecmp(val, "magenta") == 0)
243 return COLOR_MAGENTA;
244 if (strcasecmp(val, "cyan") == 0)
246 if (strcasecmp(val, "white") == 0)
248 if (strcasecmp(val, "default") == 0)
251 return default_color_value(envvar);
255 struct tog_diff_view_state {
256 struct got_object_id *id1, *id2;
257 const char *label1, *label2;
259 int first_displayed_line;
260 int last_displayed_line;
263 int ignore_whitespace;
265 struct got_repository *repo;
266 struct got_reflist_head refs;
267 struct tog_colors colors;
273 /* passed from log view; may be NULL */
274 struct tog_view *log_view;
277 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 struct tog_log_thread_args {
280 pthread_cond_t need_commits;
281 pthread_cond_t commit_loaded;
283 struct got_commit_graph *graph;
284 struct commit_queue *commits;
285 const char *in_repo_path;
286 struct got_object_id *start_id;
287 struct got_repository *repo;
290 struct commit_queue_entry **first_displayed_entry;
291 struct commit_queue_entry **selected_entry;
293 int *search_next_done;
297 struct tog_log_view_state {
298 struct commit_queue commits;
299 struct commit_queue_entry *first_displayed_entry;
300 struct commit_queue_entry *last_displayed_entry;
301 struct commit_queue_entry *selected_entry;
304 const char *head_ref_name;
306 struct got_repository *repo;
307 struct got_reflist_head refs;
308 struct got_object_id *start_id;
311 struct tog_log_thread_args thread_args;
312 struct commit_queue_entry *matched_entry;
313 struct commit_queue_entry *search_entry;
314 struct tog_colors colors;
317 #define TOG_COLOR_DIFF_MINUS 1
318 #define TOG_COLOR_DIFF_PLUS 2
319 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 #define TOG_COLOR_DIFF_META 4
321 #define TOG_COLOR_TREE_SUBMODULE 5
322 #define TOG_COLOR_TREE_SYMLINK 6
323 #define TOG_COLOR_TREE_DIRECTORY 7
324 #define TOG_COLOR_TREE_EXECUTABLE 8
325 #define TOG_COLOR_COMMIT 9
326 #define TOG_COLOR_AUTHOR 10
327 #define TOG_COLOR_DATE 11
328 #define TOG_COLOR_REFS_HEADS 12
329 #define TOG_COLOR_REFS_TAGS 13
330 #define TOG_COLOR_REFS_REMOTES 14
332 struct tog_blame_cb_args {
333 struct tog_blame_line *lines; /* one per line */
336 struct tog_view *view;
337 struct got_object_id *commit_id;
341 struct tog_blame_thread_args {
343 struct got_repository *repo;
344 struct tog_blame_cb_args *cb_args;
346 got_cancel_cb cancel_cb;
353 struct tog_blame_line *lines;
357 struct tog_blame_thread_args thread_args;
358 struct tog_blame_cb_args cb_args;
362 struct tog_blame_view_state {
363 int first_displayed_line;
364 int last_displayed_line;
369 struct got_object_id_queue blamed_commits;
370 struct got_object_qid *blamed_commit;
372 struct got_repository *repo;
373 struct got_object_id *commit_id;
374 struct tog_blame blame;
376 struct tog_colors colors;
379 struct tog_parent_tree {
380 TAILQ_ENTRY(tog_parent_tree) entry;
381 struct got_tree_object *tree;
382 struct got_tree_entry *first_displayed_entry;
383 struct got_tree_entry *selected_entry;
387 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 struct tog_tree_view_state {
391 struct got_tree_object *root;
392 struct got_tree_object *tree;
393 struct got_tree_entry *first_displayed_entry;
394 struct got_tree_entry *last_displayed_entry;
395 struct got_tree_entry *selected_entry;
396 int ndisplayed, selected, show_ids;
397 struct tog_parent_trees parents;
398 struct got_object_id *commit_id;
399 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches focus between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
441 TAILQ_HEAD(tog_view_list_head, tog_view);
444 TAILQ_ENTRY(tog_view) entry;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
449 int focussed; /* Only set on one parent or child view at a time. */
451 struct tog_view *parent;
452 struct tog_view *child;
455 * This flag is initially set on parent views when a new child view
456 * is created. It gets toggled when the 'Tab' key switches focus
457 * between parent and child.
458 * The flag indicates whether focus should be passed on to our child
459 * view if this parent view gets picked for focus after another parent
460 * view was closed. This prevents child views from losing focus in such
465 /* type-specific state */
466 enum tog_view_type type;
468 struct tog_diff_view_state diff;
469 struct tog_log_view_state log;
470 struct tog_blame_view_state blame;
471 struct tog_tree_view_state tree;
472 struct tog_ref_view_state ref;
475 const struct got_error *(*show)(struct tog_view *);
476 const struct got_error *(*input)(struct tog_view **,
477 struct tog_view *, int);
478 const struct got_error *(*close)(struct tog_view *);
480 const struct got_error *(*search_start)(struct tog_view *);
481 const struct got_error *(*search_next)(struct tog_view *);
483 #define TOG_SEARCH_FORWARD 1
484 #define TOG_SEARCH_BACKWARD 2
485 int search_next_done;
486 #define TOG_SEARCH_HAVE_MORE 1
487 #define TOG_SEARCH_NO_MORE 2
488 #define TOG_SEARCH_HAVE_NONE 3
493 static const struct got_error *open_diff_view(struct tog_view *,
494 struct got_object_id *, struct got_object_id *,
495 const char *, const char *, int, int, int, struct tog_view *,
496 struct got_repository *);
497 static const struct got_error *show_diff_view(struct tog_view *);
498 static const struct got_error *input_diff_view(struct tog_view **,
499 struct tog_view *, int);
500 static const struct got_error* close_diff_view(struct tog_view *);
501 static const struct got_error *search_start_diff_view(struct tog_view *);
502 static const struct got_error *search_next_diff_view(struct tog_view *);
504 static const struct got_error *open_log_view(struct tog_view *,
505 struct got_object_id *, struct got_repository *,
506 const char *, const char *, int);
507 static const struct got_error * show_log_view(struct tog_view *);
508 static const struct got_error *input_log_view(struct tog_view **,
509 struct tog_view *, int);
510 static const struct got_error *close_log_view(struct tog_view *);
511 static const struct got_error *search_start_log_view(struct tog_view *);
512 static const struct got_error *search_next_log_view(struct tog_view *);
514 static const struct got_error *open_blame_view(struct tog_view *, char *,
515 struct got_object_id *, struct got_repository *);
516 static const struct got_error *show_blame_view(struct tog_view *);
517 static const struct got_error *input_blame_view(struct tog_view **,
518 struct tog_view *, int);
519 static const struct got_error *close_blame_view(struct tog_view *);
520 static const struct got_error *search_start_blame_view(struct tog_view *);
521 static const struct got_error *search_next_blame_view(struct tog_view *);
523 static const struct got_error *open_tree_view(struct tog_view *,
524 struct got_tree_object *, struct got_object_id *, struct got_repository *);
525 static const struct got_error *show_tree_view(struct tog_view *);
526 static const struct got_error *input_tree_view(struct tog_view **,
527 struct tog_view *, int);
528 static const struct got_error *close_tree_view(struct tog_view *);
529 static const struct got_error *search_start_tree_view(struct tog_view *);
530 static const struct got_error *search_next_tree_view(struct tog_view *);
532 static const struct got_error *open_ref_view(struct tog_view *,
533 struct got_repository *);
534 static const struct got_error *show_ref_view(struct tog_view *);
535 static const struct got_error *input_ref_view(struct tog_view **,
536 struct tog_view *, int);
537 static const struct got_error *close_ref_view(struct tog_view *);
538 static const struct got_error *search_start_ref_view(struct tog_view *);
539 static const struct got_error *search_next_ref_view(struct tog_view *);
541 static volatile sig_atomic_t tog_sigwinch_received;
542 static volatile sig_atomic_t tog_sigpipe_received;
543 static volatile sig_atomic_t tog_sigcont_received;
546 tog_sigwinch(int signo)
548 tog_sigwinch_received = 1;
552 tog_sigpipe(int signo)
554 tog_sigpipe_received = 1;
558 tog_sigcont(int signo)
560 tog_sigcont_received = 1;
563 static const struct got_error *
564 view_close(struct tog_view *view)
566 const struct got_error *err = NULL;
569 view_close(view->child);
573 err = view->close(view);
575 del_panel(view->panel);
577 delwin(view->window);
582 static struct tog_view *
583 view_open(int nlines, int ncols, int begin_y, int begin_x,
584 enum tog_view_type type)
586 struct tog_view *view = calloc(1, sizeof(*view));
594 view->nlines = nlines ? nlines : LINES - begin_y;
595 view->ncols = ncols ? ncols : COLS - begin_x;
596 view->begin_y = begin_y;
597 view->begin_x = begin_x;
598 view->window = newwin(nlines, ncols, begin_y, begin_x);
599 if (view->window == NULL) {
603 view->panel = new_panel(view->window);
604 if (view->panel == NULL ||
605 set_panel_userptr(view->panel, view) != OK) {
610 keypad(view->window, TRUE);
615 view_split_begin_x(int begin_x)
617 if (begin_x > 0 || COLS < 120)
619 return (COLS - MAX(COLS / 2, 80));
622 static const struct got_error *view_resize(struct tog_view *);
624 static const struct got_error *
625 view_splitscreen(struct tog_view *view)
627 const struct got_error *err = NULL;
630 view->begin_x = view_split_begin_x(0);
631 view->nlines = LINES;
632 view->ncols = COLS - view->begin_x;
635 err = view_resize(view);
639 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
640 return got_error_from_errno("mvwin");
645 static const struct got_error *
646 view_fullscreen(struct tog_view *view)
648 const struct got_error *err = NULL;
652 view->nlines = LINES;
656 err = view_resize(view);
660 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
661 return got_error_from_errno("mvwin");
667 view_is_parent_view(struct tog_view *view)
669 return view->parent == NULL;
672 static const struct got_error *
673 view_resize(struct tog_view *view)
677 if (view->lines > LINES)
678 nlines = view->nlines - (view->lines - LINES);
680 nlines = view->nlines + (LINES - view->lines);
682 if (view->cols > COLS)
683 ncols = view->ncols - (view->cols - COLS);
685 ncols = view->ncols + (COLS - view->cols);
687 if (wresize(view->window, nlines, ncols) == ERR)
688 return got_error_from_errno("wresize");
689 if (replace_panel(view->panel, view->window) == ERR)
690 return got_error_from_errno("replace_panel");
691 wclear(view->window);
693 view->nlines = nlines;
699 view->child->begin_x = view_split_begin_x(view->begin_x);
700 if (view->child->begin_x == 0) {
701 view_fullscreen(view->child);
702 if (view->child->focussed)
703 show_panel(view->child->panel);
705 show_panel(view->panel);
707 view_splitscreen(view->child);
708 show_panel(view->child->panel);
715 static const struct got_error *
716 view_close_child(struct tog_view *view)
718 const struct got_error *err = NULL;
720 if (view->child == NULL)
723 err = view_close(view->child);
729 view_set_child(struct tog_view *view, struct tog_view *child)
732 child->parent = view;
736 view_is_splitscreen(struct tog_view *view)
738 return view->begin_x > 0;
747 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
748 cols = 80; /* Default */
754 resize_term(lines, cols);
757 static const struct got_error *
758 view_search_start(struct tog_view *view)
760 const struct got_error *err = NULL;
764 if (view->nlines < 1)
767 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
768 wclrtoeol(view->window);
772 ret = wgetnstr(view->window, pattern, sizeof(pattern));
778 if (view->searching) {
779 regfree(&view->regex);
783 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
784 err = view->search_start(view);
786 regfree(&view->regex);
789 view->searching = TOG_SEARCH_FORWARD;
790 view->search_next_done = 0;
791 view->search_next(view);
797 static const struct got_error *
798 view_input(struct tog_view **new, int *done, struct tog_view *view,
799 struct tog_view_list_head *views)
801 const struct got_error *err = NULL;
807 /* Clear "no matches" indicator. */
808 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
809 view->search_next_done == TOG_SEARCH_HAVE_NONE)
810 view->search_next_done = TOG_SEARCH_HAVE_MORE;
812 if (view->searching && !view->search_next_done) {
813 errcode = pthread_mutex_unlock(&tog_mutex);
815 return got_error_set_errno(errcode,
816 "pthread_mutex_unlock");
818 errcode = pthread_mutex_lock(&tog_mutex);
820 return got_error_set_errno(errcode,
821 "pthread_mutex_lock");
822 view->search_next(view);
826 nodelay(stdscr, FALSE);
827 /* Allow threads to make progress while we are waiting for input. */
828 errcode = pthread_mutex_unlock(&tog_mutex);
830 return got_error_set_errno(errcode, "pthread_mutex_unlock");
831 ch = wgetch(view->window);
832 errcode = pthread_mutex_lock(&tog_mutex);
834 return got_error_set_errno(errcode, "pthread_mutex_lock");
835 nodelay(stdscr, TRUE);
837 if (tog_sigwinch_received || tog_sigcont_received) {
839 tog_sigwinch_received = 0;
840 tog_sigcont_received = 0;
841 TAILQ_FOREACH(v, views, entry) {
842 err = view_resize(v);
845 err = v->input(new, v, KEY_RESIZE);
857 view->child->focussed = 1;
858 view->focus_child = 1;
859 } else if (view->parent) {
861 view->parent->focussed = 1;
862 view->parent->focus_child = 0;
866 err = view->input(new, view, ch);
873 if (view_is_parent_view(view)) {
874 if (view->child == NULL)
876 if (view_is_splitscreen(view->child)) {
878 view->child->focussed = 1;
879 err = view_fullscreen(view->child);
881 err = view_splitscreen(view->child);
884 err = view->child->input(new, view->child,
887 if (view_is_splitscreen(view)) {
888 view->parent->focussed = 0;
890 err = view_fullscreen(view);
892 err = view_splitscreen(view);
896 err = view->input(new, view, KEY_RESIZE);
902 if (view->search_start)
903 view_search_start(view);
905 err = view->input(new, view, ch);
909 if (view->search_next) {
910 view->searching = (ch == 'n' ?
911 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
912 view->search_next_done = 0;
913 view->search_next(view);
915 err = view->input(new, view, ch);
918 err = view->input(new, view, ch);
926 view_vborder(struct tog_view *view)
929 struct tog_view *view_above;
932 return view_vborder(view->parent);
934 panel = panel_above(view->panel);
938 view_above = panel_userptr(panel);
939 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
940 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
944 view_needs_focus_indication(struct tog_view *view)
946 if (view_is_parent_view(view)) {
947 if (view->child == NULL || view->child->focussed)
949 if (!view_is_splitscreen(view->child))
951 } else if (!view_is_splitscreen(view))
954 return view->focussed;
957 static const struct got_error *
958 view_loop(struct tog_view *view)
960 const struct got_error *err = NULL;
961 struct tog_view_list_head views;
962 struct tog_view *new_view;
963 int fast_refresh = 10;
964 int done = 0, errcode;
966 errcode = pthread_mutex_lock(&tog_mutex);
968 return got_error_set_errno(errcode, "pthread_mutex_lock");
971 TAILQ_INSERT_HEAD(&views, view, entry);
974 err = view->show(view);
979 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
980 /* Refresh fast during initialization, then become slower. */
981 if (fast_refresh && fast_refresh-- == 0)
982 halfdelay(10); /* switch to once per second */
984 err = view_input(&new_view, &done, view, &views);
988 struct tog_view *v, *prev = NULL;
990 if (view_is_parent_view(view))
991 prev = TAILQ_PREV(view, tog_view_list_head,
993 else if (view->parent)
997 view->parent->child = NULL;
998 view->parent->focus_child = 0;
1000 TAILQ_REMOVE(&views, view, entry);
1002 err = view_close(view);
1007 TAILQ_FOREACH(v, &views, entry) {
1011 if (view == NULL && new_view == NULL) {
1012 /* No view has focus. Try to pick one. */
1015 else if (!TAILQ_EMPTY(&views)) {
1016 view = TAILQ_LAST(&views,
1017 tog_view_list_head);
1020 if (view->focus_child) {
1021 view->child->focussed = 1;
1029 struct tog_view *v, *t;
1030 /* Only allow one parent view per type. */
1031 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1032 if (v->type != new_view->type)
1034 TAILQ_REMOVE(&views, v, entry);
1035 err = view_close(v);
1040 TAILQ_INSERT_TAIL(&views, new_view, entry);
1044 if (view_is_parent_view(view)) {
1045 if (view->child && view->child->focussed)
1048 if (view->parent && view->parent->focussed)
1049 view = view->parent;
1051 show_panel(view->panel);
1052 if (view->child && view_is_splitscreen(view->child))
1053 show_panel(view->child->panel);
1054 if (view->parent && view_is_splitscreen(view)) {
1055 err = view->parent->show(view->parent);
1059 err = view->show(view);
1063 err = view->child->show(view->child);
1072 while (!TAILQ_EMPTY(&views)) {
1073 view = TAILQ_FIRST(&views);
1074 TAILQ_REMOVE(&views, view, entry);
1078 errcode = pthread_mutex_unlock(&tog_mutex);
1079 if (errcode && err == NULL)
1080 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1090 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1095 /* Create newly allocated wide-character string equivalent to a byte string. */
1096 static const struct got_error *
1097 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1100 const struct got_error *err = NULL;
1103 *wlen = mbstowcs(NULL, s, 0);
1104 if (*wlen == (size_t)-1) {
1106 if (errno != EILSEQ)
1107 return got_error_from_errno("mbstowcs");
1109 /* byte string invalid in current encoding; try to "fix" it */
1110 err = got_mbsavis(&vis, &vislen, s);
1113 *wlen = mbstowcs(NULL, vis, 0);
1114 if (*wlen == (size_t)-1) {
1115 err = got_error_from_errno("mbstowcs"); /* give up */
1120 *ws = calloc(*wlen + 1, sizeof(**ws));
1122 err = got_error_from_errno("calloc");
1126 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1127 err = got_error_from_errno("mbstowcs");
1138 /* Format a line for display, ensuring that it won't overflow a width limit. */
1139 static const struct got_error *
1140 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1143 const struct got_error *err = NULL;
1145 wchar_t *wline = NULL;
1152 err = mbs2ws(&wline, &wlen, line);
1158 int width = wcwidth(wline[i]);
1165 if (width == 1 || width == 2) {
1166 if (cols + width > wlimit)
1170 } else if (width == -1) {
1171 if (wline[i] == L'\t') {
1173 ((cols + col_tab_align) % TABSIZE);
1174 if (cols + width > wlimit)
1180 err = got_error_from_errno("wcwidth");
1195 static const struct got_error*
1196 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1197 struct got_object_id *id, struct got_repository *repo)
1199 static const struct got_error *err = NULL;
1200 struct got_reflist_entry *re;
1206 SIMPLEQ_FOREACH(re, refs, entry) {
1207 struct got_tag_object *tag = NULL;
1208 struct got_object_id *ref_id;
1211 name = got_ref_get_name(re->ref);
1212 if (strcmp(name, GOT_REF_HEAD) == 0)
1214 if (strncmp(name, "refs/", 5) == 0)
1216 if (strncmp(name, "got/", 4) == 0)
1218 if (strncmp(name, "heads/", 6) == 0)
1220 if (strncmp(name, "remotes/", 8) == 0) {
1222 s = strstr(name, "/" GOT_REF_HEAD);
1223 if (s != NULL && s[strlen(s)] == '\0')
1226 err = got_ref_resolve(&ref_id, repo, re->ref);
1229 if (strncmp(name, "tags/", 5) == 0) {
1230 err = got_object_open_as_tag(&tag, repo, ref_id);
1232 if (err->code != GOT_ERR_OBJ_TYPE) {
1236 /* Ref points at something other than a tag. */
1241 cmp = got_object_id_cmp(tag ?
1242 got_object_tag_get_object_id(tag) : ref_id, id);
1245 got_object_tag_close(tag);
1249 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1250 s ? ", " : "", name) == -1) {
1251 err = got_error_from_errno("asprintf");
1262 static const struct got_error *
1263 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1266 char *smallerthan, *at;
1268 smallerthan = strchr(author, '<');
1269 if (smallerthan && smallerthan[1] != '\0')
1270 author = smallerthan + 1;
1271 at = strchr(author, '@');
1274 return format_line(wauthor, author_width, author, limit, col_tab_align);
1277 static const struct got_error *
1278 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1279 struct got_object_id *id, const size_t date_display_cols,
1280 int author_display_cols)
1282 struct tog_log_view_state *s = &view->state.log;
1283 const struct got_error *err = NULL;
1284 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1285 char *logmsg0 = NULL, *logmsg = NULL;
1286 char *author = NULL;
1287 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1288 int author_width, logmsg_width;
1289 char *newline, *line = NULL;
1291 const int avail = view->ncols;
1293 time_t committer_time;
1294 struct tog_color *tc;
1296 committer_time = got_object_commit_get_committer_time(commit);
1297 if (localtime_r(&committer_time, &tm) == NULL)
1298 return got_error_from_errno("localtime_r");
1299 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1301 return got_error(GOT_ERR_NO_SPACE);
1303 if (avail <= date_display_cols)
1304 limit = MIN(sizeof(datebuf) - 1, avail);
1306 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1307 tc = get_color(&s->colors, TOG_COLOR_DATE);
1309 wattr_on(view->window,
1310 COLOR_PAIR(tc->colorpair), NULL);
1311 waddnstr(view->window, datebuf, limit);
1313 wattr_off(view->window,
1314 COLOR_PAIR(tc->colorpair), NULL);
1321 err = got_object_id_str(&id_str, id);
1324 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1326 wattr_on(view->window,
1327 COLOR_PAIR(tc->colorpair), NULL);
1328 wprintw(view->window, "%.8s ", id_str);
1330 wattr_off(view->window,
1331 COLOR_PAIR(tc->colorpair), NULL);
1338 author = strdup(got_object_commit_get_author(commit));
1339 if (author == NULL) {
1340 err = got_error_from_errno("strdup");
1343 err = format_author(&wauthor, &author_width, author, avail - col, col);
1346 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1348 wattr_on(view->window,
1349 COLOR_PAIR(tc->colorpair), NULL);
1350 waddwstr(view->window, wauthor);
1352 wattr_off(view->window,
1353 COLOR_PAIR(tc->colorpair), NULL);
1354 col += author_width;
1355 while (col < avail && author_width < author_display_cols + 2) {
1356 waddch(view->window, ' ');
1363 err = got_object_commit_get_logmsg(&logmsg0, commit);
1367 while (*logmsg == '\n')
1369 newline = strchr(logmsg, '\n');
1372 limit = avail - col;
1373 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1376 waddwstr(view->window, wlogmsg);
1377 col += logmsg_width;
1378 while (col < avail) {
1379 waddch(view->window, ' ');
1391 static struct commit_queue_entry *
1392 alloc_commit_queue_entry(struct got_commit_object *commit,
1393 struct got_object_id *id)
1395 struct commit_queue_entry *entry;
1397 entry = calloc(1, sizeof(*entry));
1402 entry->commit = commit;
1407 pop_commit(struct commit_queue *commits)
1409 struct commit_queue_entry *entry;
1411 entry = TAILQ_FIRST(&commits->head);
1412 TAILQ_REMOVE(&commits->head, entry, entry);
1413 got_object_commit_close(entry->commit);
1414 commits->ncommits--;
1415 /* Don't free entry->id! It is owned by the commit graph. */
1420 free_commits(struct commit_queue *commits)
1422 while (!TAILQ_EMPTY(&commits->head))
1423 pop_commit(commits);
1426 static const struct got_error *
1427 match_commit(int *have_match, struct got_object_id *id,
1428 struct got_commit_object *commit, regex_t *regex)
1430 const struct got_error *err = NULL;
1431 regmatch_t regmatch;
1432 char *id_str = NULL, *logmsg = NULL;
1436 err = got_object_id_str(&id_str, id);
1440 err = got_object_commit_get_logmsg(&logmsg, commit);
1444 if (regexec(regex, got_object_commit_get_author(commit), 1,
1445 ®match, 0) == 0 ||
1446 regexec(regex, got_object_commit_get_committer(commit), 1,
1447 ®match, 0) == 0 ||
1448 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1449 regexec(regex, logmsg, 1, ®match, 0) == 0)
1457 static const struct got_error *
1458 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1459 int minqueue, struct got_repository *repo, const char *path,
1460 int *searching, int *search_next_done, regex_t *regex)
1462 const struct got_error *err = NULL;
1466 * We keep all commits open throughout the lifetime of the log
1467 * view in order to avoid having to re-fetch commits from disk
1468 * while updating the display.
1470 while (nqueued < minqueue ||
1471 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1472 struct got_object_id *id;
1473 struct got_commit_object *commit;
1474 struct commit_queue_entry *entry;
1477 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1478 if (err || id == NULL)
1481 err = got_object_open_as_commit(&commit, repo, id);
1484 entry = alloc_commit_queue_entry(commit, id);
1485 if (entry == NULL) {
1486 err = got_error_from_errno("alloc_commit_queue_entry");
1490 errcode = pthread_mutex_lock(&tog_mutex);
1492 err = got_error_set_errno(errcode,
1493 "pthread_mutex_lock");
1497 entry->idx = commits->ncommits;
1498 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1500 commits->ncommits++;
1502 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1504 err = match_commit(&have_match, id, commit, regex);
1508 *search_next_done = TOG_SEARCH_HAVE_MORE;
1511 errcode = pthread_mutex_unlock(&tog_mutex);
1512 if (errcode && err == NULL)
1513 err = got_error_set_errno(errcode,
1514 "pthread_mutex_unlock");
1523 select_commit(struct tog_log_view_state *s)
1525 struct commit_queue_entry *entry;
1528 entry = s->first_displayed_entry;
1530 if (ncommits == s->selected) {
1531 s->selected_entry = entry;
1534 entry = TAILQ_NEXT(entry, entry);
1539 static const struct got_error *
1540 draw_commits(struct tog_view *view)
1542 const struct got_error *err = NULL;
1543 struct tog_log_view_state *s = &view->state.log;
1544 struct commit_queue_entry *entry = s->selected_entry;
1545 const int limit = view->nlines;
1547 int ncommits, author_cols = 4;
1548 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1549 char *refs_str = NULL;
1551 struct tog_color *tc;
1552 static const size_t date_display_cols = 12;
1554 if (s->selected_entry &&
1555 !(view->searching && view->search_next_done == 0)) {
1556 err = got_object_id_str(&id_str, s->selected_entry->id);
1559 err = build_refs_str(&refs_str, &s->refs,
1560 s->selected_entry->id, s->repo);
1565 if (s->thread_args.commits_needed == 0)
1566 halfdelay(10); /* disable fast refresh */
1568 if (s->thread_args.commits_needed > 0) {
1569 if (asprintf(&ncommits_str, " [%d/%d] %s",
1570 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1571 (view->searching && !view->search_next_done) ?
1572 "searching..." : "loading...") == -1) {
1573 err = got_error_from_errno("asprintf");
1577 const char *search_str = NULL;
1579 if (view->searching) {
1580 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1581 search_str = "no more matches";
1582 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1583 search_str = "no matches found";
1584 else if (!view->search_next_done)
1585 search_str = "searching...";
1588 if (asprintf(&ncommits_str, " [%d/%d] %s",
1589 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1590 search_str ? search_str :
1591 (refs_str ? refs_str : "")) == -1) {
1592 err = got_error_from_errno("asprintf");
1597 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1598 if (asprintf(&header, "commit %s %s%s",
1599 id_str ? id_str : "........................................",
1600 s->in_repo_path, ncommits_str) == -1) {
1601 err = got_error_from_errno("asprintf");
1605 } else if (asprintf(&header, "commit %s%s",
1606 id_str ? id_str : "........................................",
1607 ncommits_str) == -1) {
1608 err = got_error_from_errno("asprintf");
1612 err = format_line(&wline, &width, header, view->ncols, 0);
1616 werase(view->window);
1618 if (view_needs_focus_indication(view))
1619 wstandout(view->window);
1620 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1622 wattr_on(view->window,
1623 COLOR_PAIR(tc->colorpair), NULL);
1624 waddwstr(view->window, wline);
1626 wattr_off(view->window,
1627 COLOR_PAIR(tc->colorpair), NULL);
1628 while (width < view->ncols) {
1629 waddch(view->window, ' ');
1632 if (view_needs_focus_indication(view))
1633 wstandend(view->window);
1638 /* Grow author column size if necessary. */
1639 entry = s->first_displayed_entry;
1645 if (ncommits >= limit - 1)
1647 author = strdup(got_object_commit_get_author(entry->commit));
1648 if (author == NULL) {
1649 err = got_error_from_errno("strdup");
1652 err = format_author(&wauthor, &width, author, COLS,
1654 if (author_cols < width)
1655 author_cols = width;
1659 entry = TAILQ_NEXT(entry, entry);
1662 entry = s->first_displayed_entry;
1663 s->last_displayed_entry = s->first_displayed_entry;
1666 if (ncommits >= limit - 1)
1668 if (ncommits == s->selected)
1669 wstandout(view->window);
1670 err = draw_commit(view, entry->commit, entry->id,
1671 date_display_cols, author_cols);
1672 if (ncommits == s->selected)
1673 wstandend(view->window);
1677 s->last_displayed_entry = entry;
1678 entry = TAILQ_NEXT(entry, entry);
1691 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1693 struct commit_queue_entry *entry;
1696 entry = TAILQ_FIRST(&s->commits.head);
1697 if (s->first_displayed_entry == entry)
1700 entry = s->first_displayed_entry;
1701 while (entry && nscrolled < maxscroll) {
1702 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1704 s->first_displayed_entry = entry;
1710 static const struct got_error *
1711 trigger_log_thread(struct tog_view *view, int wait)
1713 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1716 halfdelay(1); /* fast refresh while loading commits */
1718 while (ta->commits_needed > 0) {
1719 if (ta->log_complete)
1722 /* Wake the log thread. */
1723 errcode = pthread_cond_signal(&ta->need_commits);
1725 return got_error_set_errno(errcode,
1726 "pthread_cond_signal");
1729 * The mutex will be released while the view loop waits
1730 * in wgetch(), at which time the log thread will run.
1735 /* Display progress update in log view. */
1736 show_log_view(view);
1740 /* Wait right here while next commit is being loaded. */
1741 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1743 return got_error_set_errno(errcode,
1744 "pthread_cond_wait");
1746 /* Display progress update in log view. */
1747 show_log_view(view);
1755 static const struct got_error *
1756 log_scroll_down(struct tog_view *view, int maxscroll)
1758 struct tog_log_view_state *s = &view->state.log;
1759 const struct got_error *err = NULL;
1760 struct commit_queue_entry *pentry;
1761 int nscrolled = 0, ncommits_needed;
1763 if (s->last_displayed_entry == NULL)
1766 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1767 if (s->commits.ncommits < ncommits_needed &&
1768 !s->thread_args.log_complete) {
1770 * Ask the log thread for required amount of commits.
1772 s->thread_args.commits_needed += maxscroll;
1773 err = trigger_log_thread(view, 1);
1779 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1783 s->last_displayed_entry = pentry;
1785 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1788 s->first_displayed_entry = pentry;
1789 } while (++nscrolled < maxscroll);
1794 static const struct got_error *
1795 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1796 struct got_commit_object *commit, struct got_object_id *commit_id,
1797 struct tog_view *log_view, struct got_repository *repo)
1799 const struct got_error *err;
1800 struct got_object_qid *parent_id;
1801 struct tog_view *diff_view;
1803 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1804 if (diff_view == NULL)
1805 return got_error_from_errno("view_open");
1807 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1808 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1809 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1811 *new_view = diff_view;
1815 static const struct got_error *
1816 tree_view_visit_subtree(struct tog_tree_view_state *s,
1817 struct got_tree_object *subtree)
1819 struct tog_parent_tree *parent;
1821 parent = calloc(1, sizeof(*parent));
1823 return got_error_from_errno("calloc");
1825 parent->tree = s->tree;
1826 parent->first_displayed_entry = s->first_displayed_entry;
1827 parent->selected_entry = s->selected_entry;
1828 parent->selected = s->selected;
1829 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1832 s->first_displayed_entry = NULL;
1836 static const struct got_error *
1837 tree_view_walk_path(struct tog_tree_view_state *s,
1838 struct got_object_id *commit_id, const char *path)
1840 const struct got_error *err = NULL;
1841 struct got_tree_object *tree = NULL;
1843 char *slash, *subpath = NULL;
1845 /* Walk the path and open corresponding tree objects. */
1848 struct got_tree_entry *te;
1849 struct got_object_id *tree_id;
1855 /* Ensure the correct subtree entry is selected. */
1856 slash = strchr(p, '/');
1858 te_name = strdup(p);
1860 te_name = strndup(p, slash - p);
1861 if (te_name == NULL) {
1862 err = got_error_from_errno("strndup");
1865 te = got_object_tree_find_entry(s->tree, te_name);
1867 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1872 s->first_displayed_entry = s->selected_entry = te;
1874 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1875 break; /* jump to this file's entry */
1877 slash = strchr(p, '/');
1879 subpath = strndup(path, slash - path);
1881 subpath = strdup(path);
1882 if (subpath == NULL) {
1883 err = got_error_from_errno("strdup");
1887 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1892 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1897 err = tree_view_visit_subtree(s, tree);
1899 got_object_tree_close(tree);
1913 static const struct got_error *
1914 browse_commit_tree(struct tog_view **new_view, int begin_x,
1915 struct commit_queue_entry *entry, const char *path,
1916 struct got_repository *repo)
1918 const struct got_error *err = NULL;
1919 struct got_tree_object *tree;
1920 struct tog_tree_view_state *s;
1921 struct tog_view *tree_view;
1923 err = got_object_open_as_tree(&tree, repo,
1924 got_object_commit_get_tree_id(entry->commit));
1928 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1929 if (tree_view == NULL)
1930 return got_error_from_errno("view_open");
1932 err = open_tree_view(tree_view, tree, entry->id, repo);
1934 got_object_tree_close(tree);
1937 s = &tree_view->state.tree;
1939 *new_view = tree_view;
1941 if (got_path_is_root_dir(path))
1944 return tree_view_walk_path(s, entry->id, path);
1947 static const struct got_error *
1948 block_signals_used_by_main_thread(void)
1953 if (sigemptyset(&sigset) == -1)
1954 return got_error_from_errno("sigemptyset");
1956 /* tog handles SIGWINCH and SIGCONT */
1957 if (sigaddset(&sigset, SIGWINCH) == -1)
1958 return got_error_from_errno("sigaddset");
1959 if (sigaddset(&sigset, SIGCONT) == -1)
1960 return got_error_from_errno("sigaddset");
1962 /* ncurses handles SIGTSTP */
1963 if (sigaddset(&sigset, SIGTSTP) == -1)
1964 return got_error_from_errno("sigaddset");
1966 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1968 return got_error_set_errno(errcode, "pthread_sigmask");
1974 log_thread(void *arg)
1976 const struct got_error *err = NULL;
1978 struct tog_log_thread_args *a = arg;
1981 err = block_signals_used_by_main_thread();
1985 while (!done && !err && !tog_sigpipe_received) {
1986 err = queue_commits(a->graph, a->commits, 1, a->repo,
1987 a->in_repo_path, a->searching, a->search_next_done,
1990 if (err->code != GOT_ERR_ITER_COMPLETED)
1994 } else if (a->commits_needed > 0)
1995 a->commits_needed--;
1997 errcode = pthread_mutex_lock(&tog_mutex);
1999 err = got_error_set_errno(errcode,
2000 "pthread_mutex_lock");
2002 } else if (*a->quit)
2004 else if (*a->first_displayed_entry == NULL) {
2005 *a->first_displayed_entry =
2006 TAILQ_FIRST(&a->commits->head);
2007 *a->selected_entry = *a->first_displayed_entry;
2010 errcode = pthread_cond_signal(&a->commit_loaded);
2012 err = got_error_set_errno(errcode,
2013 "pthread_cond_signal");
2014 pthread_mutex_unlock(&tog_mutex);
2019 a->commits_needed = 0;
2021 if (a->commits_needed == 0) {
2022 errcode = pthread_cond_wait(&a->need_commits,
2025 err = got_error_set_errno(errcode,
2026 "pthread_cond_wait");
2030 errcode = pthread_mutex_unlock(&tog_mutex);
2031 if (errcode && err == NULL)
2032 err = got_error_set_errno(errcode,
2033 "pthread_mutex_unlock");
2035 a->log_complete = 1;
2039 static const struct got_error *
2040 stop_log_thread(struct tog_log_view_state *s)
2042 const struct got_error *err = NULL;
2047 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2049 return got_error_set_errno(errcode,
2050 "pthread_cond_signal");
2051 errcode = pthread_mutex_unlock(&tog_mutex);
2053 return got_error_set_errno(errcode,
2054 "pthread_mutex_unlock");
2055 errcode = pthread_join(s->thread, (void **)&err);
2057 return got_error_set_errno(errcode, "pthread_join");
2058 errcode = pthread_mutex_lock(&tog_mutex);
2060 return got_error_set_errno(errcode,
2061 "pthread_mutex_lock");
2065 if (s->thread_args.repo) {
2066 got_repo_close(s->thread_args.repo);
2067 s->thread_args.repo = NULL;
2070 if (s->thread_args.graph) {
2071 got_commit_graph_close(s->thread_args.graph);
2072 s->thread_args.graph = NULL;
2078 static const struct got_error *
2079 close_log_view(struct tog_view *view)
2081 const struct got_error *err = NULL;
2082 struct tog_log_view_state *s = &view->state.log;
2085 err = stop_log_thread(s);
2087 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2088 if (errcode && err == NULL)
2089 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2091 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2092 if (errcode && err == NULL)
2093 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2095 free_commits(&s->commits);
2096 free(s->in_repo_path);
2097 s->in_repo_path = NULL;
2100 got_ref_list_free(&s->refs);
2104 static const struct got_error *
2105 search_start_log_view(struct tog_view *view)
2107 struct tog_log_view_state *s = &view->state.log;
2109 s->matched_entry = NULL;
2110 s->search_entry = NULL;
2114 static const struct got_error *
2115 search_next_log_view(struct tog_view *view)
2117 const struct got_error *err = NULL;
2118 struct tog_log_view_state *s = &view->state.log;
2119 struct commit_queue_entry *entry;
2121 /* Display progress update in log view. */
2122 show_log_view(view);
2126 if (s->search_entry) {
2128 errcode = pthread_mutex_unlock(&tog_mutex);
2130 return got_error_set_errno(errcode,
2131 "pthread_mutex_unlock");
2132 ch = wgetch(view->window);
2133 errcode = pthread_mutex_lock(&tog_mutex);
2135 return got_error_set_errno(errcode,
2136 "pthread_mutex_lock");
2137 if (ch == KEY_BACKSPACE) {
2138 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2141 if (view->searching == TOG_SEARCH_FORWARD)
2142 entry = TAILQ_NEXT(s->search_entry, entry);
2144 entry = TAILQ_PREV(s->search_entry,
2145 commit_queue_head, entry);
2146 } else if (s->matched_entry) {
2147 if (view->searching == TOG_SEARCH_FORWARD)
2148 entry = TAILQ_NEXT(s->matched_entry, entry);
2150 entry = TAILQ_PREV(s->matched_entry,
2151 commit_queue_head, entry);
2153 if (view->searching == TOG_SEARCH_FORWARD)
2154 entry = TAILQ_FIRST(&s->commits.head);
2156 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2162 if (entry == NULL) {
2163 if (s->thread_args.log_complete ||
2164 view->searching == TOG_SEARCH_BACKWARD) {
2165 view->search_next_done =
2166 (s->matched_entry == NULL ?
2167 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2168 s->search_entry = NULL;
2172 * Poke the log thread for more commits and return,
2173 * allowing the main loop to make progress. Search
2174 * will resume at s->search_entry once we come back.
2176 s->thread_args.commits_needed++;
2177 return trigger_log_thread(view, 0);
2180 err = match_commit(&have_match, entry->id, entry->commit,
2185 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2186 s->matched_entry = entry;
2190 s->search_entry = entry;
2191 if (view->searching == TOG_SEARCH_FORWARD)
2192 entry = TAILQ_NEXT(entry, entry);
2194 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2197 if (s->matched_entry) {
2198 int cur = s->selected_entry->idx;
2199 while (cur < s->matched_entry->idx) {
2200 err = input_log_view(NULL, view, KEY_DOWN);
2205 while (cur > s->matched_entry->idx) {
2206 err = input_log_view(NULL, view, KEY_UP);
2213 s->search_entry = NULL;
2218 static const struct got_error *
2219 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2220 struct got_repository *repo, const char *head_ref_name,
2221 const char *in_repo_path, int log_branches)
2223 const struct got_error *err = NULL;
2224 struct tog_log_view_state *s = &view->state.log;
2225 struct got_repository *thread_repo = NULL;
2226 struct got_commit_graph *thread_graph = NULL;
2229 SIMPLEQ_INIT(&s->refs);
2231 if (in_repo_path != s->in_repo_path) {
2232 free(s->in_repo_path);
2233 s->in_repo_path = strdup(in_repo_path);
2234 if (s->in_repo_path == NULL)
2235 return got_error_from_errno("strdup");
2238 /* The commit queue only contains commits being displayed. */
2239 TAILQ_INIT(&s->commits.head);
2240 s->commits.ncommits = 0;
2242 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2247 s->head_ref_name = head_ref_name;
2248 s->start_id = got_object_id_dup(start_id);
2249 if (s->start_id == NULL) {
2250 err = got_error_from_errno("got_object_id_dup");
2253 s->log_branches = log_branches;
2255 SIMPLEQ_INIT(&s->colors);
2256 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2257 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2258 get_color_value("TOG_COLOR_COMMIT"));
2261 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2262 get_color_value("TOG_COLOR_AUTHOR"));
2264 free_colors(&s->colors);
2267 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2268 get_color_value("TOG_COLOR_DATE"));
2270 free_colors(&s->colors);
2275 view->show = show_log_view;
2276 view->input = input_log_view;
2277 view->close = close_log_view;
2278 view->search_start = search_start_log_view;
2279 view->search_next = search_next_log_view;
2281 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2284 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2288 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2289 s->repo, NULL, NULL);
2293 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2295 err = got_error_set_errno(errcode, "pthread_cond_init");
2298 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2300 err = got_error_set_errno(errcode, "pthread_cond_init");
2304 s->thread_args.commits_needed = view->nlines;
2305 s->thread_args.graph = thread_graph;
2306 s->thread_args.commits = &s->commits;
2307 s->thread_args.in_repo_path = s->in_repo_path;
2308 s->thread_args.start_id = s->start_id;
2309 s->thread_args.repo = thread_repo;
2310 s->thread_args.log_complete = 0;
2311 s->thread_args.quit = &s->quit;
2312 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2313 s->thread_args.selected_entry = &s->selected_entry;
2314 s->thread_args.searching = &view->searching;
2315 s->thread_args.search_next_done = &view->search_next_done;
2316 s->thread_args.regex = &view->regex;
2319 close_log_view(view);
2323 static const struct got_error *
2324 show_log_view(struct tog_view *view)
2326 const struct got_error *err;
2327 struct tog_log_view_state *s = &view->state.log;
2329 if (s->thread == NULL) {
2330 int errcode = pthread_create(&s->thread, NULL, log_thread,
2333 return got_error_set_errno(errcode, "pthread_create");
2334 if (s->thread_args.commits_needed > 0) {
2335 err = trigger_log_thread(view, 1);
2341 return draw_commits(view);
2344 static const struct got_error *
2345 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2347 const struct got_error *err = NULL;
2348 struct tog_log_view_state *s = &view->state.log;
2349 char *parent_path, *in_repo_path = NULL;
2350 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2351 struct tog_view *ref_view = NULL;
2353 struct got_object_id *start_id;
2363 if (s->first_displayed_entry == NULL)
2365 if (s->selected > 0)
2368 log_scroll_up(s, 1);
2373 if (s->first_displayed_entry == NULL)
2375 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2378 log_scroll_up(s, view->nlines - 1);
2385 if (s->first_displayed_entry == NULL)
2387 if (s->selected < MIN(view->nlines - 2,
2388 s->commits.ncommits - 1))
2391 err = log_scroll_down(view, 1);
2399 struct commit_queue_entry *first;
2400 first = s->first_displayed_entry;
2403 err = log_scroll_down(view, view->nlines - 1);
2406 if (first == s->first_displayed_entry &&
2407 s->selected < MIN(view->nlines - 2,
2408 s->commits.ncommits - 1)) {
2409 /* can't scroll further down */
2410 s->selected = MIN(view->nlines - 2,
2411 s->commits.ncommits - 1);
2417 if (s->selected > view->nlines - 2)
2418 s->selected = view->nlines - 2;
2419 if (s->selected > s->commits.ncommits - 1)
2420 s->selected = s->commits.ncommits - 1;
2422 if (s->commits.ncommits < view->nlines - 1 &&
2423 !s->thread_args.log_complete) {
2424 s->thread_args.commits_needed += (view->nlines - 1) -
2425 s->commits.ncommits;
2426 err = trigger_log_thread(view, 1);
2432 if (s->selected_entry == NULL)
2434 if (view_is_parent_view(view))
2435 begin_x = view_split_begin_x(view->begin_x);
2436 err = open_diff_view_for_commit(&diff_view, begin_x,
2437 s->selected_entry->commit, s->selected_entry->id,
2442 diff_view->focussed = 1;
2443 if (view_is_parent_view(view)) {
2444 err = view_close_child(view);
2447 view_set_child(view, diff_view);
2448 view->focus_child = 1;
2450 *new_view = diff_view;
2453 if (s->selected_entry == NULL)
2455 if (view_is_parent_view(view))
2456 begin_x = view_split_begin_x(view->begin_x);
2457 err = browse_commit_tree(&tree_view, begin_x,
2458 s->selected_entry, s->in_repo_path, s->repo);
2462 tree_view->focussed = 1;
2463 if (view_is_parent_view(view)) {
2464 err = view_close_child(view);
2467 view_set_child(view, tree_view);
2468 view->focus_child = 1;
2470 *new_view = tree_view;
2473 if (got_path_cmp(s->in_repo_path, "/",
2474 strlen(s->in_repo_path), 1) == 0)
2476 err = got_path_dirname(&parent_path, s->in_repo_path);
2479 err = stop_log_thread(s);
2484 lv = view_open(view->nlines, view->ncols,
2485 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2488 return got_error_from_errno("view_open");
2490 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2491 parent_path, s->log_branches);
2497 if (view_is_parent_view(view))
2500 view_set_child(view->parent, lv);
2503 err = stop_log_thread(s);
2506 lv = view_open(view->nlines, view->ncols,
2507 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2509 return got_error_from_errno("view_open");
2510 err = got_repo_match_object_id(&start_id, NULL,
2511 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2512 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2517 in_repo_path = strdup(s->in_repo_path);
2518 if (in_repo_path == NULL) {
2521 return got_error_from_errno("strdup");
2523 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2524 in_repo_path, s->log_branches);
2534 s->log_branches = !s->log_branches;
2535 err = stop_log_thread(s);
2538 lv = view_open(view->nlines, view->ncols,
2539 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2541 return got_error_from_errno("view_open");
2542 err = open_log_view(lv, s->start_id, s->repo,
2543 s->head_ref_name, s->in_repo_path, s->log_branches);
2552 if (view_is_parent_view(view))
2553 begin_x = view_split_begin_x(view->begin_x);
2554 ref_view = view_open(view->nlines, view->ncols,
2555 view->begin_y, begin_x, TOG_VIEW_REF);
2556 if (ref_view == NULL)
2557 return got_error_from_errno("view_open");
2558 err = open_ref_view(ref_view, s->repo);
2560 view_close(ref_view);
2564 ref_view->focussed = 1;
2565 if (view_is_parent_view(view)) {
2566 err = view_close_child(view);
2569 view_set_child(view, ref_view);
2570 view->focus_child = 1;
2572 *new_view = ref_view;
2581 static const struct got_error *
2582 apply_unveil(const char *repo_path, const char *worktree_path)
2584 const struct got_error *error;
2587 if (unveil("gmon.out", "rwc") != 0)
2588 return got_error_from_errno2("unveil", "gmon.out");
2590 if (repo_path && unveil(repo_path, "r") != 0)
2591 return got_error_from_errno2("unveil", repo_path);
2593 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2594 return got_error_from_errno2("unveil", worktree_path);
2596 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2597 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2599 error = got_privsep_unveil_exec_helpers();
2603 if (unveil(NULL, NULL) != 0)
2604 return got_error_from_errno("unveil");
2614 halfdelay(1); /* Do fast refresh while initial view is loading. */
2617 intrflush(stdscr, FALSE);
2618 keypad(stdscr, TRUE);
2620 if (getenv("TOG_COLORS") != NULL) {
2622 use_default_colors();
2624 signal(SIGWINCH, tog_sigwinch);
2625 signal(SIGPIPE, tog_sigpipe);
2626 signal(SIGCONT, tog_sigcont);
2629 static const struct got_error *
2630 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2631 struct got_repository *repo, struct got_worktree *worktree)
2633 const struct got_error *err = NULL;
2636 *in_repo_path = strdup("/");
2637 if (*in_repo_path == NULL)
2638 return got_error_from_errno("strdup");
2643 const char *prefix = got_worktree_get_path_prefix(worktree);
2646 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2649 if (asprintf(in_repo_path, "%s%s%s", prefix,
2650 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2652 err = got_error_from_errno("asprintf");
2653 *in_repo_path = NULL;
2657 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2662 static const struct got_error *
2663 cmd_log(int argc, char *argv[])
2665 const struct got_error *error;
2666 struct got_repository *repo = NULL;
2667 struct got_worktree *worktree = NULL;
2668 struct got_object_id *start_id = NULL;
2669 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2670 char *start_commit = NULL, *label = NULL;
2671 struct got_reference *ref = NULL;
2672 const char *head_ref_name = NULL;
2673 int ch, log_branches = 0;
2674 struct tog_view *view;
2676 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2682 start_commit = optarg;
2685 repo_path = realpath(optarg, NULL);
2686 if (repo_path == NULL)
2687 return got_error_from_errno2("realpath",
2702 cwd = getcwd(NULL, 0);
2704 return got_error_from_errno("getcwd");
2706 error = got_worktree_open(&worktree, cwd);
2707 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2710 if (repo_path == NULL) {
2713 strdup(got_worktree_get_repo_path(worktree));
2715 repo_path = strdup(cwd);
2717 if (repo_path == NULL) {
2718 error = got_error_from_errno("strdup");
2722 error = got_repo_open(&repo, repo_path, NULL);
2726 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2733 error = apply_unveil(got_repo_get_path(repo),
2734 worktree ? got_worktree_get_root_path(worktree) : NULL);
2738 if (start_commit == NULL) {
2739 error = got_repo_match_object_id(&start_id, &label,
2740 worktree ? got_worktree_get_head_ref_name(worktree) :
2741 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2744 head_ref_name = label;
2746 error = got_ref_open(&ref, repo, start_commit, 0);
2748 head_ref_name = got_ref_get_name(ref);
2749 else if (error->code != GOT_ERR_NOT_REF)
2751 error = got_repo_match_object_id(&start_id, NULL,
2752 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2757 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2759 error = got_error_from_errno("view_open");
2762 error = open_log_view(view, start_id, repo, head_ref_name,
2763 in_repo_path, log_branches);
2767 /* Release work tree lock. */
2768 got_worktree_close(worktree);
2771 error = view_loop(view);
2781 got_repo_close(repo);
2783 got_worktree_close(worktree);
2791 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2792 "[-w] object1 object2\n", getprogname());
2797 parse_next_line(FILE *f, size_t *len)
2802 const char delim[3] = { '\0', '\0', '\0'};
2804 line = fparseln(f, &linelen, &lineno, delim, 0);
2811 match_line(const char *line, regex_t *regex, size_t nmatch,
2812 regmatch_t *regmatch)
2814 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2818 match_color(struct tog_colors *colors, const char *line)
2820 struct tog_color *tc = NULL;
2822 SIMPLEQ_FOREACH(tc, colors, entry) {
2823 if (match_line(line, &tc->regex, 0, NULL))
2830 static const struct got_error *
2831 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2832 WINDOW *window, regmatch_t *regmatch)
2834 const struct got_error *err = NULL;
2841 s = strndup(line, regmatch->rm_so);
2843 return got_error_from_errno("strndup");
2845 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2850 waddwstr(window, wline);
2857 s = strndup(line + regmatch->rm_so,
2858 regmatch->rm_eo - regmatch->rm_so);
2860 err = got_error_from_errno("strndup");
2864 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2869 wattr_on(window, A_STANDOUT, NULL);
2870 waddwstr(window, wline);
2871 wattr_off(window, A_STANDOUT, NULL);
2878 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2879 err = format_line(&wline, &width,
2880 line + regmatch->rm_eo, wlimit, col_tab_align);
2883 waddwstr(window, wline);
2891 static const struct got_error *
2892 draw_file(struct tog_view *view, const char *header)
2894 struct tog_diff_view_state *s = &view->state.diff;
2895 regmatch_t *regmatch = &view->regmatch;
2896 const struct got_error *err;
2899 struct tog_color *tc;
2903 int max_lines = view->nlines;
2904 int nlines = s->nlines;
2907 line_offset = s->line_offsets[s->first_displayed_line - 1];
2908 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2909 return got_error_from_errno("fseek");
2911 werase(view->window);
2914 if (asprintf(&line, "[%d/%d] %s",
2915 s->first_displayed_line - 1 + s->selected_line, nlines,
2917 return got_error_from_errno("asprintf");
2918 err = format_line(&wline, &width, line, view->ncols, 0);
2923 if (view_needs_focus_indication(view))
2924 wstandout(view->window);
2925 waddwstr(view->window, wline);
2928 if (view_needs_focus_indication(view))
2929 wstandend(view->window);
2930 if (width <= view->ncols - 1)
2931 waddch(view->window, '\n');
2939 while (max_lines > 0 && nprinted < max_lines) {
2940 line = parse_next_line(s->f, &len);
2946 tc = match_color(&s->colors, line);
2948 wattr_on(view->window,
2949 COLOR_PAIR(tc->colorpair), NULL);
2950 if (s->first_displayed_line + nprinted == s->matched_line &&
2951 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2952 err = add_matched_line(&width, line, view->ncols, 0,
2953 view->window, regmatch);
2959 err = format_line(&wline, &width, line, view->ncols, 0);
2964 waddwstr(view->window, wline);
2969 wattr_off(view->window,
2970 COLOR_PAIR(tc->colorpair), NULL);
2971 if (width <= view->ncols - 1)
2972 waddch(view->window, '\n');
2977 s->last_displayed_line = s->first_displayed_line +
2980 s->last_displayed_line = s->first_displayed_line;
2985 while (nprinted < view->nlines) {
2986 waddch(view->window, '\n');
2990 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2995 wstandout(view->window);
2996 waddwstr(view->window, wline);
2999 wstandend(view->window);
3006 get_datestr(time_t *time, char *datebuf)
3008 struct tm mytm, *tm;
3011 tm = gmtime_r(time, &mytm);
3014 s = asctime_r(tm, datebuf);
3017 p = strchr(s, '\n');
3023 static const struct got_error *
3024 get_changed_paths(struct got_pathlist_head *paths,
3025 struct got_commit_object *commit, struct got_repository *repo)
3027 const struct got_error *err = NULL;
3028 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3029 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3030 struct got_object_qid *qid;
3032 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3034 struct got_commit_object *pcommit;
3035 err = got_object_open_as_commit(&pcommit, repo,
3040 tree_id1 = got_object_commit_get_tree_id(pcommit);
3041 got_object_commit_close(pcommit);
3046 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3051 tree_id2 = got_object_commit_get_tree_id(commit);
3052 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3056 err = got_diff_tree(tree1, tree2, "", "", repo,
3057 got_diff_tree_collect_changed_paths, paths, 0);
3060 got_object_tree_close(tree1);
3062 got_object_tree_close(tree2);
3066 static const struct got_error *
3067 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3071 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3073 return got_error_from_errno("reallocarray");
3075 (*line_offsets)[*nlines] = off;
3080 static const struct got_error *
3081 write_commit_info(off_t **line_offsets, size_t *nlines,
3082 struct got_object_id *commit_id, struct got_reflist_head *refs,
3083 struct got_repository *repo, FILE *outfile)
3085 const struct got_error *err = NULL;
3086 char datebuf[26], *datestr;
3087 struct got_commit_object *commit;
3088 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3089 time_t committer_time;
3090 const char *author, *committer;
3091 char *refs_str = NULL;
3092 struct got_pathlist_head changed_paths;
3093 struct got_pathlist_entry *pe;
3097 TAILQ_INIT(&changed_paths);
3100 err = build_refs_str(&refs_str, refs, commit_id, repo);
3105 err = got_object_open_as_commit(&commit, repo, commit_id);
3109 err = got_object_id_str(&id_str, commit_id);
3111 err = got_error_from_errno("got_object_id_str");
3115 err = add_line_offset(line_offsets, nlines, 0);
3119 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3120 refs_str ? refs_str : "", refs_str ? ")" : "");
3122 err = got_error_from_errno("fprintf");
3126 err = add_line_offset(line_offsets, nlines, outoff);
3130 n = fprintf(outfile, "from: %s\n",
3131 got_object_commit_get_author(commit));
3133 err = got_error_from_errno("fprintf");
3137 err = add_line_offset(line_offsets, nlines, outoff);
3141 committer_time = got_object_commit_get_committer_time(commit);
3142 datestr = get_datestr(&committer_time, datebuf);
3144 n = fprintf(outfile, "date: %s UTC\n", datestr);
3146 err = got_error_from_errno("fprintf");
3150 err = add_line_offset(line_offsets, nlines, outoff);
3154 author = got_object_commit_get_author(commit);
3155 committer = got_object_commit_get_committer(commit);
3156 if (strcmp(author, committer) != 0) {
3157 n = fprintf(outfile, "via: %s\n", committer);
3159 err = got_error_from_errno("fprintf");
3163 err = add_line_offset(line_offsets, nlines, outoff);
3167 err = got_object_commit_get_logmsg(&logmsg, commit);
3171 while ((line = strsep(&s, "\n")) != NULL) {
3172 n = fprintf(outfile, "%s\n", line);
3174 err = got_error_from_errno("fprintf");
3178 err = add_line_offset(line_offsets, nlines, outoff);
3183 err = get_changed_paths(&changed_paths, commit, repo);
3186 TAILQ_FOREACH(pe, &changed_paths, entry) {
3187 struct got_diff_changed_path *cp = pe->data;
3188 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3190 err = got_error_from_errno("fprintf");
3194 err = add_line_offset(line_offsets, nlines, outoff);
3197 free((char *)pe->path);
3201 fputc('\n', outfile);
3203 err = add_line_offset(line_offsets, nlines, outoff);
3205 got_pathlist_free(&changed_paths);
3209 got_object_commit_close(commit);
3211 free(*line_offsets);
3212 *line_offsets = NULL;
3218 static const struct got_error *
3219 create_diff(struct tog_diff_view_state *s)
3221 const struct got_error *err = NULL;
3225 free(s->line_offsets);
3226 s->line_offsets = malloc(sizeof(off_t));
3227 if (s->line_offsets == NULL)
3228 return got_error_from_errno("malloc");
3233 err = got_error_from_errno("got_opentemp");
3236 if (s->f && fclose(s->f) != 0) {
3237 err = got_error_from_errno("fclose");
3243 err = got_object_get_type(&obj_type, s->repo, s->id1);
3245 err = got_object_get_type(&obj_type, s->repo, s->id2);
3250 case GOT_OBJ_TYPE_BLOB:
3251 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3252 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3253 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3255 case GOT_OBJ_TYPE_TREE:
3256 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3257 s->id1, s->id2, "", "", s->diff_context,
3258 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3260 case GOT_OBJ_TYPE_COMMIT: {
3261 const struct got_object_id_queue *parent_ids;
3262 struct got_object_qid *pid;
3263 struct got_commit_object *commit2;
3265 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3268 /* Show commit info if we're diffing to a parent/root commit. */
3269 if (s->id1 == NULL) {
3270 err = write_commit_info(&s->line_offsets, &s->nlines,
3271 s->id2, &s->refs, s->repo, s->f);
3275 parent_ids = got_object_commit_get_parent_ids(commit2);
3276 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3277 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3278 err = write_commit_info(
3279 &s->line_offsets, &s->nlines,
3280 s->id2, &s->refs, s->repo, s->f);
3287 got_object_commit_close(commit2);
3289 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3290 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3291 s->force_text_diff, s->repo, s->f);
3295 err = got_error(GOT_ERR_OBJ_TYPE);
3301 if (s->f && fflush(s->f) != 0 && err == NULL)
3302 err = got_error_from_errno("fflush");
3307 diff_view_indicate_progress(struct tog_view *view)
3309 mvwaddstr(view->window, 0, 0, "diffing...");
3314 static const struct got_error *
3315 search_start_diff_view(struct tog_view *view)
3317 struct tog_diff_view_state *s = &view->state.diff;
3319 s->matched_line = 0;
3323 static const struct got_error *
3324 search_next_diff_view(struct tog_view *view)
3326 struct tog_diff_view_state *s = &view->state.diff;
3329 if (!view->searching) {
3330 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3334 if (s->matched_line) {
3335 if (view->searching == TOG_SEARCH_FORWARD)
3336 lineno = s->matched_line + 1;
3338 lineno = s->matched_line - 1;
3340 if (view->searching == TOG_SEARCH_FORWARD)
3351 if (lineno <= 0 || lineno > s->nlines) {
3352 if (s->matched_line == 0) {
3353 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3358 if (view->searching == TOG_SEARCH_FORWARD)
3364 offset = s->line_offsets[lineno - 1];
3365 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3367 return got_error_from_errno("fseeko");
3370 line = parse_next_line(s->f, &len);
3372 match_line(line, &view->regex, 1, &view->regmatch)) {
3373 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3374 s->matched_line = lineno;
3379 if (view->searching == TOG_SEARCH_FORWARD)
3385 if (s->matched_line) {
3386 s->first_displayed_line = s->matched_line;
3387 s->selected_line = 1;
3393 static const struct got_error *
3394 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3395 struct got_object_id *id2, const char *label1, const char *label2,
3396 int diff_context, int ignore_whitespace, int force_text_diff,
3397 struct tog_view *log_view, struct got_repository *repo)
3399 const struct got_error *err;
3400 struct tog_diff_view_state *s = &view->state.diff;
3402 SIMPLEQ_INIT(&s->refs);
3404 if (id1 != NULL && id2 != NULL) {
3406 err = got_object_get_type(&type1, repo, id1);
3409 err = got_object_get_type(&type2, repo, id2);
3414 return got_error(GOT_ERR_OBJ_TYPE);
3416 s->first_displayed_line = 1;
3417 s->last_displayed_line = view->nlines;
3418 s->selected_line = 1;
3426 s->id1 = got_object_id_dup(id1);
3428 return got_error_from_errno("got_object_id_dup");
3432 s->id2 = got_object_id_dup(id2);
3433 if (s->id2 == NULL) {
3436 return got_error_from_errno("got_object_id_dup");
3439 s->first_displayed_line = 1;
3440 s->last_displayed_line = view->nlines;
3441 s->diff_context = diff_context;
3442 s->ignore_whitespace = ignore_whitespace;
3443 s->force_text_diff = force_text_diff;
3444 s->log_view = log_view;
3447 SIMPLEQ_INIT(&s->colors);
3448 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3449 err = add_color(&s->colors,
3450 "^-", TOG_COLOR_DIFF_MINUS,
3451 get_color_value("TOG_COLOR_DIFF_MINUS"));
3454 err = add_color(&s->colors, "^\\+",
3455 TOG_COLOR_DIFF_PLUS,
3456 get_color_value("TOG_COLOR_DIFF_PLUS"));
3458 free_colors(&s->colors);
3461 err = add_color(&s->colors,
3462 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3463 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3465 free_colors(&s->colors);
3469 err = add_color(&s->colors,
3470 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3471 TOG_COLOR_DIFF_META,
3472 get_color_value("TOG_COLOR_DIFF_META"));
3474 free_colors(&s->colors);
3478 err = add_color(&s->colors,
3479 "^(from|via): ", TOG_COLOR_AUTHOR,
3480 get_color_value("TOG_COLOR_AUTHOR"));
3482 free_colors(&s->colors);
3486 err = add_color(&s->colors,
3487 "^date: ", TOG_COLOR_DATE,
3488 get_color_value("TOG_COLOR_DATE"));
3490 free_colors(&s->colors);
3495 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3501 free_colors(&s->colors);
3505 if (log_view && view_is_splitscreen(view))
3506 show_log_view(log_view); /* draw vborder */
3507 diff_view_indicate_progress(view);
3509 s->line_offsets = NULL;
3511 err = create_diff(s);
3517 free_colors(&s->colors);
3518 got_ref_list_free(&s->refs);
3522 view->show = show_diff_view;
3523 view->input = input_diff_view;
3524 view->close = close_diff_view;
3525 view->search_start = search_start_diff_view;
3526 view->search_next = search_next_diff_view;
3531 static const struct got_error *
3532 close_diff_view(struct tog_view *view)
3534 const struct got_error *err = NULL;
3535 struct tog_diff_view_state *s = &view->state.diff;
3541 if (s->f && fclose(s->f) == EOF)
3542 err = got_error_from_errno("fclose");
3543 free_colors(&s->colors);
3544 free(s->line_offsets);
3545 s->line_offsets = NULL;
3547 got_ref_list_free(&s->refs);
3551 static const struct got_error *
3552 show_diff_view(struct tog_view *view)
3554 const struct got_error *err;
3555 struct tog_diff_view_state *s = &view->state.diff;
3556 char *id_str1 = NULL, *id_str2, *header;
3557 const char *label1, *label2;
3560 err = got_object_id_str(&id_str1, s->id1);
3563 label1 = s->label1 ? : id_str1;
3565 label1 = "/dev/null";
3567 err = got_object_id_str(&id_str2, s->id2);
3570 label2 = s->label2 ? : id_str2;
3572 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3573 err = got_error_from_errno("asprintf");
3581 return draw_file(view, header);
3584 static const struct got_error *
3585 set_selected_commit(struct tog_diff_view_state *s,
3586 struct commit_queue_entry *entry)
3588 const struct got_error *err;
3589 const struct got_object_id_queue *parent_ids;
3590 struct got_commit_object *selected_commit;
3591 struct got_object_qid *pid;
3594 s->id2 = got_object_id_dup(entry->id);
3596 return got_error_from_errno("got_object_id_dup");
3598 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3601 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3603 pid = SIMPLEQ_FIRST(parent_ids);
3604 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3605 got_object_commit_close(selected_commit);
3609 static const struct got_error *
3610 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3612 const struct got_error *err = NULL;
3613 struct tog_diff_view_state *s = &view->state.diff;
3614 struct tog_log_view_state *ls;
3615 struct commit_queue_entry *old_selected_entry;
3622 s->force_text_diff = !s->force_text_diff;
3624 s->ignore_whitespace = !s->ignore_whitespace;
3625 wclear(view->window);
3626 s->first_displayed_line = 1;
3627 s->last_displayed_line = view->nlines;
3628 diff_view_indicate_progress(view);
3629 err = create_diff(s);
3633 if (s->first_displayed_line > 1)
3634 s->first_displayed_line--;
3638 if (s->first_displayed_line == 1)
3641 while (i++ < view->nlines - 1 &&
3642 s->first_displayed_line > 1)
3643 s->first_displayed_line--;
3648 s->first_displayed_line++;
3656 while (!s->eof && i++ < view->nlines - 1) {
3658 line = parse_next_line(s->f, NULL);
3659 s->first_displayed_line++;
3665 if (s->diff_context > 0) {
3667 diff_view_indicate_progress(view);
3668 err = create_diff(s);
3669 if (s->first_displayed_line + view->nlines - 1 >
3671 s->first_displayed_line = 1;
3672 s->last_displayed_line = view->nlines;
3677 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3679 diff_view_indicate_progress(view);
3680 err = create_diff(s);
3685 if (s->log_view == NULL)
3687 ls = &s->log_view->state.log;
3688 old_selected_entry = ls->selected_entry;
3690 err = input_log_view(NULL, s->log_view, KEY_UP);
3694 if (old_selected_entry == ls->selected_entry)
3697 err = set_selected_commit(s, ls->selected_entry);
3701 s->first_displayed_line = 1;
3702 s->last_displayed_line = view->nlines;
3704 diff_view_indicate_progress(view);
3705 err = create_diff(s);
3709 if (s->log_view == NULL)
3711 ls = &s->log_view->state.log;
3712 old_selected_entry = ls->selected_entry;
3714 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3718 if (old_selected_entry == ls->selected_entry)
3721 err = set_selected_commit(s, ls->selected_entry);
3725 s->first_displayed_line = 1;
3726 s->last_displayed_line = view->nlines;
3728 diff_view_indicate_progress(view);
3729 err = create_diff(s);
3738 static const struct got_error *
3739 cmd_diff(int argc, char *argv[])
3741 const struct got_error *error = NULL;
3742 struct got_repository *repo = NULL;
3743 struct got_worktree *worktree = NULL;
3744 struct got_object_id *id1 = NULL, *id2 = NULL;
3745 char *repo_path = NULL, *cwd = NULL;
3746 char *id_str1 = NULL, *id_str2 = NULL;
3747 char *label1 = NULL, *label2 = NULL;
3748 int diff_context = 3, ignore_whitespace = 0;
3749 int ch, force_text_diff = 0;
3751 struct tog_view *view;
3753 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3756 force_text_diff = 1;
3759 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3762 err(1, "-C option %s", errstr);
3765 repo_path = realpath(optarg, NULL);
3766 if (repo_path == NULL)
3767 return got_error_from_errno2("realpath",
3769 got_path_strip_trailing_slashes(repo_path);
3772 ignore_whitespace = 1;
3784 usage_diff(); /* TODO show local worktree changes */
3785 } else if (argc == 2) {
3791 cwd = getcwd(NULL, 0);
3793 return got_error_from_errno("getcwd");
3795 error = got_worktree_open(&worktree, cwd);
3796 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3799 if (repo_path == NULL) {
3802 strdup(got_worktree_get_repo_path(worktree));
3804 repo_path = strdup(cwd);
3806 if (repo_path == NULL) {
3807 error = got_error_from_errno("strdup");
3811 error = got_repo_open(&repo, repo_path, NULL);
3817 error = apply_unveil(got_repo_get_path(repo), NULL);
3821 error = got_repo_match_object_id(&id1, &label1, id_str1,
3822 GOT_OBJ_TYPE_ANY, 1, repo);
3826 error = got_repo_match_object_id(&id2, &label2, id_str2,
3827 GOT_OBJ_TYPE_ANY, 1, repo);
3831 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3833 error = got_error_from_errno("view_open");
3836 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3837 ignore_whitespace, force_text_diff, NULL, repo);
3840 error = view_loop(view);
3847 got_repo_close(repo);
3849 got_worktree_close(worktree);
3857 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3862 struct tog_blame_line {
3864 struct got_object_id *id;
3867 static const struct got_error *
3868 draw_blame(struct tog_view *view)
3870 struct tog_blame_view_state *s = &view->state.blame;
3871 struct tog_blame *blame = &s->blame;
3872 regmatch_t *regmatch = &view->regmatch;
3873 const struct got_error *err;
3874 int lineno = 0, nprinted = 0;
3879 struct tog_blame_line *blame_line;
3880 struct got_object_id *prev_id = NULL;
3882 struct tog_color *tc;
3884 err = got_object_id_str(&id_str, s->blamed_commit->id);
3889 werase(view->window);
3891 if (asprintf(&line, "commit %s", id_str) == -1) {
3892 err = got_error_from_errno("asprintf");
3897 err = format_line(&wline, &width, line, view->ncols, 0);
3902 if (view_needs_focus_indication(view))
3903 wstandout(view->window);
3904 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3906 wattr_on(view->window,
3907 COLOR_PAIR(tc->colorpair), NULL);
3908 waddwstr(view->window, wline);
3910 wattr_off(view->window,
3911 COLOR_PAIR(tc->colorpair), NULL);
3912 if (view_needs_focus_indication(view))
3913 wstandend(view->window);
3916 if (width < view->ncols - 1)
3917 waddch(view->window, '\n');
3919 if (asprintf(&line, "[%d/%d] %s%s",
3920 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3921 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3923 return got_error_from_errno("asprintf");
3926 err = format_line(&wline, &width, line, view->ncols, 0);
3931 waddwstr(view->window, wline);
3934 if (width < view->ncols - 1)
3935 waddch(view->window, '\n');
3938 while (nprinted < view->nlines - 2) {
3939 line = parse_next_line(blame->f, &len);
3944 if (++lineno < s->first_displayed_line) {
3949 if (view->focussed && nprinted == s->selected_line - 1)
3950 wstandout(view->window);
3952 if (blame->nlines > 0) {
3953 blame_line = &blame->lines[lineno - 1];
3954 if (blame_line->annotated && prev_id &&
3955 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3957 nprinted == s->selected_line - 1)) {
3958 waddstr(view->window, " ");
3959 } else if (blame_line->annotated) {
3961 err = got_object_id_str(&id_str, blame_line->id);
3966 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3968 wattr_on(view->window,
3969 COLOR_PAIR(tc->colorpair), NULL);
3970 wprintw(view->window, "%.8s", id_str);
3972 wattr_off(view->window,
3973 COLOR_PAIR(tc->colorpair), NULL);
3975 prev_id = blame_line->id;
3977 waddstr(view->window, "........");
3981 waddstr(view->window, "........");
3985 if (view->focussed && nprinted == s->selected_line - 1)
3986 wstandend(view->window);
3987 waddstr(view->window, " ");
3989 if (view->ncols <= 9) {
3991 wline = wcsdup(L"");
3992 if (wline == NULL) {
3993 err = got_error_from_errno("wcsdup");
3997 } else if (s->first_displayed_line + nprinted ==
3999 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4000 err = add_matched_line(&width, line, view->ncols - 9, 9,
4001 view->window, regmatch);
4008 err = format_line(&wline, &width, line,
4009 view->ncols - 9, 9);
4010 waddwstr(view->window, wline);
4016 if (width <= view->ncols - 1)
4017 waddch(view->window, '\n');
4018 if (++nprinted == 1)
4019 s->first_displayed_line = lineno;
4022 s->last_displayed_line = lineno;
4029 static const struct got_error *
4030 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4032 const struct got_error *err = NULL;
4033 struct tog_blame_cb_args *a = arg;
4034 struct tog_blame_line *line;
4037 if (nlines != a->nlines ||
4038 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4039 return got_error(GOT_ERR_RANGE);
4041 errcode = pthread_mutex_lock(&tog_mutex);
4043 return got_error_set_errno(errcode, "pthread_mutex_lock");
4045 if (*a->quit) { /* user has quit the blame view */
4046 err = got_error(GOT_ERR_ITER_COMPLETED);
4051 goto done; /* no change in this commit */
4053 line = &a->lines[lineno - 1];
4054 if (line->annotated)
4057 line->id = got_object_id_dup(id);
4058 if (line->id == NULL) {
4059 err = got_error_from_errno("got_object_id_dup");
4062 line->annotated = 1;
4064 errcode = pthread_mutex_unlock(&tog_mutex);
4066 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4071 blame_thread(void *arg)
4073 const struct got_error *err;
4074 struct tog_blame_thread_args *ta = arg;
4075 struct tog_blame_cb_args *a = ta->cb_args;
4078 err = block_signals_used_by_main_thread();
4082 err = got_blame(ta->path, a->commit_id, ta->repo,
4083 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4084 if (err && err->code == GOT_ERR_CANCELLED)
4087 errcode = pthread_mutex_lock(&tog_mutex);
4089 return (void *)got_error_set_errno(errcode,
4090 "pthread_mutex_lock");
4092 got_repo_close(ta->repo);
4096 errcode = pthread_mutex_unlock(&tog_mutex);
4097 if (errcode && err == NULL)
4098 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4103 static struct got_object_id *
4104 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4105 int first_displayed_line, int selected_line)
4107 struct tog_blame_line *line;
4112 line = &lines[first_displayed_line - 1 + selected_line - 1];
4113 if (!line->annotated)
4119 static const struct got_error *
4120 stop_blame(struct tog_blame *blame)
4122 const struct got_error *err = NULL;
4125 if (blame->thread) {
4127 errcode = pthread_mutex_unlock(&tog_mutex);
4129 return got_error_set_errno(errcode,
4130 "pthread_mutex_unlock");
4131 errcode = pthread_join(blame->thread, (void **)&err);
4133 return got_error_set_errno(errcode, "pthread_join");
4134 errcode = pthread_mutex_lock(&tog_mutex);
4136 return got_error_set_errno(errcode,
4137 "pthread_mutex_lock");
4138 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4140 blame->thread = NULL;
4142 if (blame->thread_args.repo) {
4143 got_repo_close(blame->thread_args.repo);
4144 blame->thread_args.repo = NULL;
4147 if (fclose(blame->f) != 0 && err == NULL)
4148 err = got_error_from_errno("fclose");
4152 for (i = 0; i < blame->nlines; i++)
4153 free(blame->lines[i].id);
4155 blame->lines = NULL;
4157 free(blame->cb_args.commit_id);
4158 blame->cb_args.commit_id = NULL;
4163 static const struct got_error *
4164 cancel_blame_view(void *arg)
4166 const struct got_error *err = NULL;
4170 errcode = pthread_mutex_lock(&tog_mutex);
4172 return got_error_set_errno(errcode,
4173 "pthread_mutex_unlock");
4176 err = got_error(GOT_ERR_CANCELLED);
4178 errcode = pthread_mutex_unlock(&tog_mutex);
4180 return got_error_set_errno(errcode,
4181 "pthread_mutex_lock");
4186 static const struct got_error *
4187 run_blame(struct tog_view *view)
4189 struct tog_blame_view_state *s = &view->state.blame;
4190 struct tog_blame *blame = &s->blame;
4191 const struct got_error *err = NULL;
4192 struct got_blob_object *blob = NULL;
4193 struct got_repository *thread_repo = NULL;
4194 struct got_object_id *obj_id = NULL;
4197 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4202 err = got_object_get_type(&obj_type, s->repo, obj_id);
4206 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4207 err = got_error(GOT_ERR_OBJ_TYPE);
4211 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4214 blame->f = got_opentemp();
4215 if (blame->f == NULL) {
4216 err = got_error_from_errno("got_opentemp");
4219 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4220 &blame->line_offsets, blame->f, blob);
4221 if (err || blame->nlines == 0)
4224 /* Don't include \n at EOF in the blame line count. */
4225 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4228 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4229 if (blame->lines == NULL) {
4230 err = got_error_from_errno("calloc");
4234 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4238 blame->cb_args.view = view;
4239 blame->cb_args.lines = blame->lines;
4240 blame->cb_args.nlines = blame->nlines;
4241 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4242 if (blame->cb_args.commit_id == NULL) {
4243 err = got_error_from_errno("got_object_id_dup");
4246 blame->cb_args.quit = &s->done;
4248 blame->thread_args.path = s->path;
4249 blame->thread_args.repo = thread_repo;
4250 blame->thread_args.cb_args = &blame->cb_args;
4251 blame->thread_args.complete = &s->blame_complete;
4252 blame->thread_args.cancel_cb = cancel_blame_view;
4253 blame->thread_args.cancel_arg = &s->done;
4254 s->blame_complete = 0;
4258 got_object_blob_close(blob);
4265 static const struct got_error *
4266 open_blame_view(struct tog_view *view, char *path,
4267 struct got_object_id *commit_id, struct got_repository *repo)
4269 const struct got_error *err = NULL;
4270 struct tog_blame_view_state *s = &view->state.blame;
4272 SIMPLEQ_INIT(&s->blamed_commits);
4274 s->path = strdup(path);
4275 if (s->path == NULL)
4276 return got_error_from_errno("strdup");
4278 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4284 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4285 s->first_displayed_line = 1;
4286 s->last_displayed_line = view->nlines;
4287 s->selected_line = 1;
4288 s->blame_complete = 0;
4290 s->commit_id = commit_id;
4291 memset(&s->blame, 0, sizeof(s->blame));
4293 SIMPLEQ_INIT(&s->colors);
4294 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4295 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4296 get_color_value("TOG_COLOR_COMMIT"));
4301 view->show = show_blame_view;
4302 view->input = input_blame_view;
4303 view->close = close_blame_view;
4304 view->search_start = search_start_blame_view;
4305 view->search_next = search_next_blame_view;
4307 return run_blame(view);
4310 static const struct got_error *
4311 close_blame_view(struct tog_view *view)
4313 const struct got_error *err = NULL;
4314 struct tog_blame_view_state *s = &view->state.blame;
4316 if (s->blame.thread)
4317 err = stop_blame(&s->blame);
4319 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4320 struct got_object_qid *blamed_commit;
4321 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4322 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4323 got_object_qid_free(blamed_commit);
4327 free_colors(&s->colors);
4332 static const struct got_error *
4333 search_start_blame_view(struct tog_view *view)
4335 struct tog_blame_view_state *s = &view->state.blame;
4337 s->matched_line = 0;
4341 static const struct got_error *
4342 search_next_blame_view(struct tog_view *view)
4344 struct tog_blame_view_state *s = &view->state.blame;
4347 if (!view->searching) {
4348 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4352 if (s->matched_line) {
4353 if (view->searching == TOG_SEARCH_FORWARD)
4354 lineno = s->matched_line + 1;
4356 lineno = s->matched_line - 1;
4358 if (view->searching == TOG_SEARCH_FORWARD)
4361 lineno = s->blame.nlines;
4369 if (lineno <= 0 || lineno > s->blame.nlines) {
4370 if (s->matched_line == 0) {
4371 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4376 if (view->searching == TOG_SEARCH_FORWARD)
4379 lineno = s->blame.nlines;
4382 offset = s->blame.line_offsets[lineno - 1];
4383 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4385 return got_error_from_errno("fseeko");
4388 line = parse_next_line(s->blame.f, &len);
4390 match_line(line, &view->regex, 1, &view->regmatch)) {
4391 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4392 s->matched_line = lineno;
4397 if (view->searching == TOG_SEARCH_FORWARD)
4403 if (s->matched_line) {
4404 s->first_displayed_line = s->matched_line;
4405 s->selected_line = 1;
4411 static const struct got_error *
4412 show_blame_view(struct tog_view *view)
4414 const struct got_error *err = NULL;
4415 struct tog_blame_view_state *s = &view->state.blame;
4418 if (s->blame.thread == NULL) {
4419 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4420 &s->blame.thread_args);
4422 return got_error_set_errno(errcode, "pthread_create");
4424 halfdelay(1); /* fast refresh while annotating */
4427 if (s->blame_complete)
4428 halfdelay(10); /* disable fast refresh */
4430 err = draw_blame(view);
4436 static const struct got_error *
4437 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4439 const struct got_error *err = NULL, *thread_err = NULL;
4440 struct tog_view *diff_view;
4441 struct tog_blame_view_state *s = &view->state.blame;
4450 if (s->selected_line > 1)
4452 else if (s->selected_line == 1 &&
4453 s->first_displayed_line > 1)
4454 s->first_displayed_line--;
4458 if (s->first_displayed_line == 1) {
4459 s->selected_line = 1;
4462 if (s->first_displayed_line > view->nlines - 2)
4463 s->first_displayed_line -=
4466 s->first_displayed_line = 1;
4470 if (s->selected_line < view->nlines - 2 &&
4471 s->first_displayed_line +
4472 s->selected_line <= s->blame.nlines)
4474 else if (s->last_displayed_line <
4476 s->first_displayed_line++;
4480 struct got_object_id *id = NULL;
4481 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4482 s->first_displayed_line, s->selected_line);
4486 struct got_commit_object *commit;
4487 struct got_object_qid *pid;
4488 struct got_object_id *blob_id = NULL;
4490 err = got_object_open_as_commit(&commit,
4494 pid = SIMPLEQ_FIRST(
4495 got_object_commit_get_parent_ids(commit));
4497 got_object_commit_close(commit);
4500 /* Check if path history ends here. */
4501 err = got_object_id_by_path(&blob_id, s->repo,
4504 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4506 got_object_commit_close(commit);
4509 err = got_object_get_type(&obj_type, s->repo,
4512 /* Can't blame non-blob type objects. */
4513 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4514 got_object_commit_close(commit);
4517 err = got_object_qid_alloc(&s->blamed_commit,
4519 got_object_commit_close(commit);
4521 if (got_object_id_cmp(id,
4522 s->blamed_commit->id) == 0)
4524 err = got_object_qid_alloc(&s->blamed_commit,
4530 thread_err = stop_blame(&s->blame);
4534 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4535 s->blamed_commit, entry);
4536 err = run_blame(view);
4542 struct got_object_qid *first;
4543 first = SIMPLEQ_FIRST(&s->blamed_commits);
4544 if (!got_object_id_cmp(first->id, s->commit_id))
4547 thread_err = stop_blame(&s->blame);
4551 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4552 got_object_qid_free(s->blamed_commit);
4554 SIMPLEQ_FIRST(&s->blamed_commits);
4555 err = run_blame(view);
4562 struct got_object_id *id = NULL;
4563 struct got_object_qid *pid;
4564 struct got_commit_object *commit = NULL;
4565 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4566 s->first_displayed_line, s->selected_line);
4569 err = got_object_open_as_commit(&commit, s->repo, id);
4572 pid = SIMPLEQ_FIRST(
4573 got_object_commit_get_parent_ids(commit));
4574 if (view_is_parent_view(view))
4575 begin_x = view_split_begin_x(view->begin_x);
4576 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4577 if (diff_view == NULL) {
4578 got_object_commit_close(commit);
4579 err = got_error_from_errno("view_open");
4582 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4583 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4584 got_object_commit_close(commit);
4586 view_close(diff_view);
4590 diff_view->focussed = 1;
4591 if (view_is_parent_view(view)) {
4592 err = view_close_child(view);
4595 view_set_child(view, diff_view);
4596 view->focus_child = 1;
4598 *new_view = diff_view;
4606 if (s->last_displayed_line >= s->blame.nlines &&
4607 s->selected_line >= MIN(s->blame.nlines,
4608 view->nlines - 2)) {
4611 if (s->last_displayed_line >= s->blame.nlines &&
4612 s->selected_line < view->nlines - 2) {
4613 s->selected_line = MIN(s->blame.nlines,
4617 if (s->last_displayed_line + view->nlines - 2
4619 s->first_displayed_line +=
4622 s->first_displayed_line =
4627 if (s->selected_line > view->nlines - 2) {
4628 s->selected_line = MIN(s->blame.nlines,
4635 return thread_err ? thread_err : err;
4638 static const struct got_error *
4639 cmd_blame(int argc, char *argv[])
4641 const struct got_error *error;
4642 struct got_repository *repo = NULL;
4643 struct got_worktree *worktree = NULL;
4644 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4645 char *link_target = NULL;
4646 struct got_object_id *commit_id = NULL;
4647 char *commit_id_str = NULL;
4649 struct tog_view *view;
4651 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4654 commit_id_str = optarg;
4657 repo_path = realpath(optarg, NULL);
4658 if (repo_path == NULL)
4659 return got_error_from_errno2("realpath",
4674 cwd = getcwd(NULL, 0);
4676 return got_error_from_errno("getcwd");
4678 error = got_worktree_open(&worktree, cwd);
4679 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4682 if (repo_path == NULL) {
4685 strdup(got_worktree_get_repo_path(worktree));
4687 repo_path = strdup(cwd);
4689 if (repo_path == NULL) {
4690 error = got_error_from_errno("strdup");
4694 error = got_repo_open(&repo, repo_path, NULL);
4698 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4705 error = apply_unveil(got_repo_get_path(repo), NULL);
4709 if (commit_id_str == NULL) {
4710 struct got_reference *head_ref;
4711 error = got_ref_open(&head_ref, repo, worktree ?
4712 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4715 error = got_ref_resolve(&commit_id, repo, head_ref);
4716 got_ref_close(head_ref);
4718 error = got_repo_match_object_id(&commit_id, NULL,
4719 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4724 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4726 error = got_error_from_errno("view_open");
4730 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4735 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4740 /* Release work tree lock. */
4741 got_worktree_close(worktree);
4744 error = view_loop(view);
4752 got_worktree_close(worktree);
4754 got_repo_close(repo);
4758 static const struct got_error *
4759 draw_tree_entries(struct tog_view *view, const char *parent_path)
4761 struct tog_tree_view_state *s = &view->state.tree;
4762 const struct got_error *err = NULL;
4763 struct got_tree_entry *te;
4765 struct tog_color *tc;
4766 int width, n, i, nentries;
4767 int limit = view->nlines;
4771 werase(view->window);
4776 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4779 if (view_needs_focus_indication(view))
4780 wstandout(view->window);
4781 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4783 wattr_on(view->window,
4784 COLOR_PAIR(tc->colorpair), NULL);
4785 waddwstr(view->window, wline);
4787 wattr_off(view->window,
4788 COLOR_PAIR(tc->colorpair), NULL);
4789 if (view_needs_focus_indication(view))
4790 wstandend(view->window);
4793 if (width < view->ncols - 1)
4794 waddch(view->window, '\n');
4797 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4800 waddwstr(view->window, wline);
4803 if (width < view->ncols - 1)
4804 waddch(view->window, '\n');
4807 waddch(view->window, '\n');
4811 if (s->first_displayed_entry == NULL) {
4812 te = got_object_tree_get_first_entry(s->tree);
4813 if (s->selected == 0) {
4815 wstandout(view->window);
4816 s->selected_entry = NULL;
4818 waddstr(view->window, " ..\n"); /* parent directory */
4819 if (s->selected == 0 && view->focussed)
4820 wstandend(view->window);
4827 te = s->first_displayed_entry;
4830 nentries = got_object_tree_get_nentries(s->tree);
4831 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4832 char *line = NULL, *id_str = NULL, *link_target = NULL;
4833 const char *modestr = "";
4836 te = got_object_tree_get_entry(s->tree, i);
4837 mode = got_tree_entry_get_mode(te);
4840 err = got_object_id_str(&id_str,
4841 got_tree_entry_get_id(te));
4843 return got_error_from_errno(
4844 "got_object_id_str");
4846 if (got_object_tree_entry_is_submodule(te))
4848 else if (S_ISLNK(mode)) {
4851 err = got_tree_entry_get_symlink_target(&link_target,
4857 for (i = 0; i < strlen(link_target); i++) {
4858 if (!isprint((unsigned char)link_target[i]))
4859 link_target[i] = '?';
4863 else if (S_ISDIR(mode))
4865 else if (mode & S_IXUSR)
4867 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4868 got_tree_entry_get_name(te), modestr,
4869 link_target ? " -> ": "",
4870 link_target ? link_target : "") == -1) {
4873 return got_error_from_errno("asprintf");
4877 err = format_line(&wline, &width, line, view->ncols, 0);
4882 if (n == s->selected) {
4884 wstandout(view->window);
4885 s->selected_entry = te;
4887 tc = match_color(&s->colors, line);
4889 wattr_on(view->window,
4890 COLOR_PAIR(tc->colorpair), NULL);
4891 waddwstr(view->window, wline);
4893 wattr_off(view->window,
4894 COLOR_PAIR(tc->colorpair), NULL);
4895 if (width < view->ncols - 1)
4896 waddch(view->window, '\n');
4897 if (n == s->selected && view->focussed)
4898 wstandend(view->window);
4904 s->last_displayed_entry = te;
4913 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4915 struct got_tree_entry *te;
4916 int isroot = s->tree == s->root;
4919 if (s->first_displayed_entry == NULL)
4922 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4923 while (i++ < maxscroll) {
4926 s->first_displayed_entry = NULL;
4929 s->first_displayed_entry = te;
4930 te = got_tree_entry_get_prev(s->tree, te);
4935 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4937 struct got_tree_entry *next, *last;
4940 if (s->first_displayed_entry)
4941 next = got_tree_entry_get_next(s->tree,
4942 s->first_displayed_entry);
4944 next = got_object_tree_get_first_entry(s->tree);
4946 last = s->last_displayed_entry;
4947 while (next && last && n++ < maxscroll) {
4948 last = got_tree_entry_get_next(s->tree, last);
4950 s->first_displayed_entry = next;
4951 next = got_tree_entry_get_next(s->tree, next);
4956 static const struct got_error *
4957 tree_entry_path(char **path, struct tog_parent_trees *parents,
4958 struct got_tree_entry *te)
4960 const struct got_error *err = NULL;
4961 struct tog_parent_tree *pt;
4962 size_t len = 2; /* for leading slash and NUL */
4964 TAILQ_FOREACH(pt, parents, entry)
4965 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4968 len += strlen(got_tree_entry_get_name(te));
4970 *path = calloc(1, len);
4972 return got_error_from_errno("calloc");
4975 pt = TAILQ_LAST(parents, tog_parent_trees);
4977 const char *name = got_tree_entry_get_name(pt->selected_entry);
4978 if (strlcat(*path, name, len) >= len) {
4979 err = got_error(GOT_ERR_NO_SPACE);
4982 if (strlcat(*path, "/", len) >= len) {
4983 err = got_error(GOT_ERR_NO_SPACE);
4986 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4989 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4990 err = got_error(GOT_ERR_NO_SPACE);
5002 static const struct got_error *
5003 blame_tree_entry(struct tog_view **new_view, int begin_x,
5004 struct got_tree_entry *te, struct tog_parent_trees *parents,
5005 struct got_object_id *commit_id, struct got_repository *repo)
5007 const struct got_error *err = NULL;
5009 struct tog_view *blame_view;
5013 err = tree_entry_path(&path, parents, te);
5017 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5018 if (blame_view == NULL) {
5019 err = got_error_from_errno("view_open");
5023 err = open_blame_view(blame_view, path, commit_id, repo);
5025 if (err->code == GOT_ERR_CANCELLED)
5027 view_close(blame_view);
5029 *new_view = blame_view;
5035 static const struct got_error *
5036 log_tree_entry(struct tog_view **new_view, int begin_x,
5037 struct got_tree_entry *te, struct tog_parent_trees *parents,
5038 struct got_object_id *commit_id, struct got_repository *repo)
5040 struct tog_view *log_view;
5041 const struct got_error *err = NULL;
5046 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5047 if (log_view == NULL)
5048 return got_error_from_errno("view_open");
5050 err = tree_entry_path(&path, parents, te);
5054 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5056 view_close(log_view);
5058 *new_view = log_view;
5063 static const struct got_error *
5064 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5065 struct got_object_id *commit_id, struct got_repository *repo)
5067 const struct got_error *err = NULL;
5068 char *commit_id_str = NULL;
5069 struct tog_tree_view_state *s = &view->state.tree;
5071 TAILQ_INIT(&s->parents);
5073 err = got_object_id_str(&commit_id_str, commit_id);
5077 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5078 err = got_error_from_errno("asprintf");
5082 s->root = s->tree = root;
5083 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5084 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5085 s->commit_id = got_object_id_dup(commit_id);
5086 if (s->commit_id == NULL) {
5087 err = got_error_from_errno("got_object_id_dup");
5092 SIMPLEQ_INIT(&s->colors);
5094 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5095 err = add_color(&s->colors, "\\$$",
5096 TOG_COLOR_TREE_SUBMODULE,
5097 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5100 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5101 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5103 free_colors(&s->colors);
5106 err = add_color(&s->colors, "/$",
5107 TOG_COLOR_TREE_DIRECTORY,
5108 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5110 free_colors(&s->colors);
5114 err = add_color(&s->colors, "\\*$",
5115 TOG_COLOR_TREE_EXECUTABLE,
5116 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5118 free_colors(&s->colors);
5122 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5123 get_color_value("TOG_COLOR_COMMIT"));
5125 free_colors(&s->colors);
5130 view->show = show_tree_view;
5131 view->input = input_tree_view;
5132 view->close = close_tree_view;
5133 view->search_start = search_start_tree_view;
5134 view->search_next = search_next_tree_view;
5136 free(commit_id_str);
5138 free(s->tree_label);
5139 s->tree_label = NULL;
5144 static const struct got_error *
5145 close_tree_view(struct tog_view *view)
5147 struct tog_tree_view_state *s = &view->state.tree;
5149 free_colors(&s->colors);
5150 free(s->tree_label);
5151 s->tree_label = NULL;
5153 s->commit_id = NULL;
5154 while (!TAILQ_EMPTY(&s->parents)) {
5155 struct tog_parent_tree *parent;
5156 parent = TAILQ_FIRST(&s->parents);
5157 TAILQ_REMOVE(&s->parents, parent, entry);
5161 if (s->tree != s->root)
5162 got_object_tree_close(s->tree);
5163 got_object_tree_close(s->root);
5167 static const struct got_error *
5168 search_start_tree_view(struct tog_view *view)
5170 struct tog_tree_view_state *s = &view->state.tree;
5172 s->matched_entry = NULL;
5177 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5179 regmatch_t regmatch;
5181 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5185 static const struct got_error *
5186 search_next_tree_view(struct tog_view *view)
5188 struct tog_tree_view_state *s = &view->state.tree;
5189 struct got_tree_entry *te = NULL;
5191 if (!view->searching) {
5192 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5196 if (s->matched_entry) {
5197 if (view->searching == TOG_SEARCH_FORWARD) {
5198 if (s->selected_entry)
5199 te = got_tree_entry_get_next(s->tree,
5202 te = got_object_tree_get_first_entry(s->tree);
5204 if (s->selected_entry == NULL)
5205 te = got_object_tree_get_last_entry(s->tree);
5207 te = got_tree_entry_get_prev(s->tree,
5211 if (view->searching == TOG_SEARCH_FORWARD)
5212 te = got_object_tree_get_first_entry(s->tree);
5214 te = got_object_tree_get_last_entry(s->tree);
5219 if (s->matched_entry == NULL) {
5220 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5223 if (view->searching == TOG_SEARCH_FORWARD)
5224 te = got_object_tree_get_first_entry(s->tree);
5226 te = got_object_tree_get_last_entry(s->tree);
5229 if (match_tree_entry(te, &view->regex)) {
5230 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5231 s->matched_entry = te;
5235 if (view->searching == TOG_SEARCH_FORWARD)
5236 te = got_tree_entry_get_next(s->tree, te);
5238 te = got_tree_entry_get_prev(s->tree, te);
5241 if (s->matched_entry) {
5242 s->first_displayed_entry = s->matched_entry;
5249 static const struct got_error *
5250 show_tree_view(struct tog_view *view)
5252 const struct got_error *err = NULL;
5253 struct tog_tree_view_state *s = &view->state.tree;
5256 err = tree_entry_path(&parent_path, &s->parents, NULL);
5260 err = draw_tree_entries(view, parent_path);
5267 static const struct got_error *
5268 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5270 const struct got_error *err = NULL;
5271 struct tog_tree_view_state *s = &view->state.tree;
5272 struct tog_view *log_view, *ref_view;
5277 s->show_ids = !s->show_ids;
5280 if (!s->selected_entry)
5282 if (view_is_parent_view(view))
5283 begin_x = view_split_begin_x(view->begin_x);
5284 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5285 &s->parents, s->commit_id, s->repo);
5287 log_view->focussed = 1;
5288 if (view_is_parent_view(view)) {
5289 err = view_close_child(view);
5292 view_set_child(view, log_view);
5293 view->focus_child = 1;
5295 *new_view = log_view;
5298 if (view_is_parent_view(view))
5299 begin_x = view_split_begin_x(view->begin_x);
5300 ref_view = view_open(view->nlines, view->ncols,
5301 view->begin_y, begin_x, TOG_VIEW_REF);
5302 if (ref_view == NULL)
5303 return got_error_from_errno("view_open");
5304 err = open_ref_view(ref_view, s->repo);
5306 view_close(ref_view);
5310 ref_view->focussed = 1;
5311 if (view_is_parent_view(view)) {
5312 err = view_close_child(view);
5315 view_set_child(view, ref_view);
5316 view->focus_child = 1;
5318 *new_view = ref_view;
5322 if (s->selected > 0) {
5326 tree_scroll_up(s, 1);
5330 if (s->tree == s->root) {
5331 if (got_object_tree_get_first_entry(s->tree) ==
5332 s->first_displayed_entry)
5335 if (s->first_displayed_entry == NULL)
5338 tree_scroll_up(s, MAX(0, view->nlines - 3));
5342 if (s->selected < s->ndisplayed - 1) {
5346 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5348 /* can't scroll any further */
5350 tree_scroll_down(s, 1);
5354 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5356 /* can't scroll any further; move cursor down */
5357 if (s->selected < s->ndisplayed - 1)
5358 s->selected = s->ndisplayed - 1;
5361 tree_scroll_down(s, view->nlines - 3);
5366 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5367 struct tog_parent_tree *parent;
5368 /* user selected '..' */
5369 if (s->tree == s->root)
5371 parent = TAILQ_FIRST(&s->parents);
5372 TAILQ_REMOVE(&s->parents, parent,
5374 got_object_tree_close(s->tree);
5375 s->tree = parent->tree;
5376 s->first_displayed_entry =
5377 parent->first_displayed_entry;
5379 parent->selected_entry;
5380 s->selected = parent->selected;
5382 } else if (S_ISDIR(got_tree_entry_get_mode(
5383 s->selected_entry))) {
5384 struct got_tree_object *subtree;
5385 err = got_object_open_as_tree(&subtree, s->repo,
5386 got_tree_entry_get_id(s->selected_entry));
5389 err = tree_view_visit_subtree(s, subtree);
5391 got_object_tree_close(subtree);
5394 } else if (S_ISREG(got_tree_entry_get_mode(
5395 s->selected_entry))) {
5396 struct tog_view *blame_view;
5397 int begin_x = view_is_parent_view(view) ?
5398 view_split_begin_x(view->begin_x) : 0;
5400 err = blame_tree_entry(&blame_view, begin_x,
5401 s->selected_entry, &s->parents,
5402 s->commit_id, s->repo);
5406 blame_view->focussed = 1;
5407 if (view_is_parent_view(view)) {
5408 err = view_close_child(view);
5411 view_set_child(view, blame_view);
5412 view->focus_child = 1;
5414 *new_view = blame_view;
5418 if (s->selected > view->nlines)
5419 s->selected = s->ndisplayed - 1;
5432 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5437 static const struct got_error *
5438 cmd_tree(int argc, char *argv[])
5440 const struct got_error *error;
5441 struct got_repository *repo = NULL;
5442 struct got_worktree *worktree = NULL;
5443 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5444 struct got_object_id *commit_id = NULL;
5445 char *commit_id_arg = NULL;
5446 struct got_commit_object *commit = NULL;
5447 struct got_tree_object *tree = NULL;
5449 struct tog_view *view;
5451 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5454 commit_id_arg = optarg;
5457 repo_path = realpath(optarg, NULL);
5458 if (repo_path == NULL)
5459 return got_error_from_errno2("realpath",
5474 cwd = getcwd(NULL, 0);
5476 return got_error_from_errno("getcwd");
5478 error = got_worktree_open(&worktree, cwd);
5479 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5482 if (repo_path == NULL) {
5485 strdup(got_worktree_get_repo_path(worktree));
5487 repo_path = strdup(cwd);
5489 if (repo_path == NULL) {
5490 error = got_error_from_errno("strdup");
5494 error = got_repo_open(&repo, repo_path, NULL);
5498 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5505 error = apply_unveil(got_repo_get_path(repo), NULL);
5509 error = got_repo_match_object_id(&commit_id, NULL,
5510 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5511 GOT_OBJ_TYPE_COMMIT, 1, repo);
5515 error = got_object_open_as_commit(&commit, repo, commit_id);
5519 error = got_object_open_as_tree(&tree, repo,
5520 got_object_commit_get_tree_id(commit));
5524 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5526 error = got_error_from_errno("view_open");
5529 error = open_tree_view(view, tree, commit_id, repo);
5532 if (!got_path_is_root_dir(in_repo_path)) {
5533 error = tree_view_walk_path(&view->state.tree, commit_id,
5540 /* Release work tree lock. */
5541 got_worktree_close(worktree);
5544 error = view_loop(view);
5550 got_object_commit_close(commit);
5552 got_object_tree_close(tree);
5554 got_repo_close(repo);
5558 static const struct got_error *
5559 ref_view_load_refs(struct tog_ref_view_state *s)
5561 const struct got_error *err;
5562 struct got_reflist_entry *sre;
5563 struct tog_reflist_entry *re;
5565 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5566 got_ref_cmp_by_name, NULL);
5571 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5572 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5575 re = malloc(sizeof(*re));
5577 return got_error_from_errno("malloc");
5580 re->idx = s->nrefs++;
5581 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5588 ref_view_free_refs(struct tog_ref_view_state *s)
5590 struct tog_reflist_entry *re;
5592 while (!TAILQ_EMPTY(&s->refs)) {
5593 re = TAILQ_FIRST(&s->refs);
5594 TAILQ_REMOVE(&s->refs, re, entry);
5597 got_ref_list_free(&s->simplerefs);
5600 static const struct got_error *
5601 open_ref_view(struct tog_view *view, struct got_repository *repo)
5603 const struct got_error *err = NULL;
5604 struct tog_ref_view_state *s = &view->state.ref;
5606 s->selected_entry = 0;
5609 SIMPLEQ_INIT(&s->simplerefs);
5610 TAILQ_INIT(&s->refs);
5611 SIMPLEQ_INIT(&s->colors);
5613 err = ref_view_load_refs(s);
5617 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5619 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5620 err = add_color(&s->colors, "^refs/heads/",
5621 TOG_COLOR_REFS_HEADS,
5622 get_color_value("TOG_COLOR_REFS_HEADS"));
5626 err = add_color(&s->colors, "^refs/tags/",
5627 TOG_COLOR_REFS_TAGS,
5628 get_color_value("TOG_COLOR_REFS_TAGS"));
5632 err = add_color(&s->colors, "^refs/remotes/",
5633 TOG_COLOR_REFS_REMOTES,
5634 get_color_value("TOG_COLOR_REFS_REMOTES"));
5639 view->show = show_ref_view;
5640 view->input = input_ref_view;
5641 view->close = close_ref_view;
5642 view->search_start = search_start_ref_view;
5643 view->search_next = search_next_ref_view;
5646 free_colors(&s->colors);
5650 static const struct got_error *
5651 close_ref_view(struct tog_view *view)
5653 struct tog_ref_view_state *s = &view->state.ref;
5655 ref_view_free_refs(s);
5656 free_colors(&s->colors);
5661 static const struct got_error *
5662 resolve_reflist_entry(struct got_object_id **commit_id,
5663 struct tog_reflist_entry *re, struct got_repository *repo)
5665 const struct got_error *err = NULL;
5666 struct got_object_id *obj_id;
5667 struct got_tag_object *tag = NULL;
5672 err = got_ref_resolve(&obj_id, repo, re->ref);
5676 err = got_object_get_type(&obj_type, repo, obj_id);
5681 case GOT_OBJ_TYPE_COMMIT:
5682 *commit_id = obj_id;
5684 case GOT_OBJ_TYPE_TAG:
5685 err = got_object_open_as_tag(&tag, repo, obj_id);
5689 err = got_object_get_type(&obj_type, repo,
5690 got_object_tag_get_object_id(tag));
5693 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5694 err = got_error(GOT_ERR_OBJ_TYPE);
5697 *commit_id = got_object_id_dup(
5698 got_object_tag_get_object_id(tag));
5699 if (*commit_id == NULL) {
5700 err = got_error_from_errno("got_object_id_dup");
5705 err = got_error(GOT_ERR_OBJ_TYPE);
5711 got_object_tag_close(tag);
5719 static const struct got_error *
5720 log_ref_entry(struct tog_view **new_view, int begin_x,
5721 struct tog_reflist_entry *re, struct got_repository *repo)
5723 struct tog_view *log_view;
5724 const struct got_error *err = NULL;
5725 struct got_object_id *commit_id = NULL;
5729 err = resolve_reflist_entry(&commit_id, re, repo);
5731 if (err->code != GOT_ERR_OBJ_TYPE)
5737 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5738 if (log_view == NULL) {
5739 err = got_error_from_errno("view_open");
5743 err = open_log_view(log_view, commit_id, repo,
5744 got_ref_get_name(re->ref), "", 0);
5747 view_close(log_view);
5749 *new_view = log_view;
5755 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5757 struct tog_reflist_entry *re;
5760 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5763 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5764 while (i++ < maxscroll) {
5767 s->first_displayed_entry = re;
5768 re = TAILQ_PREV(re, tog_reflist_head, entry);
5773 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5775 struct tog_reflist_entry *next, *last;
5778 if (s->first_displayed_entry)
5779 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5781 next = TAILQ_FIRST(&s->refs);
5783 last = s->last_displayed_entry;
5784 while (next && last && n++ < maxscroll) {
5785 last = TAILQ_NEXT(last, entry);
5787 s->first_displayed_entry = next;
5788 next = TAILQ_NEXT(next, entry);
5793 static const struct got_error *
5794 search_start_ref_view(struct tog_view *view)
5796 struct tog_ref_view_state *s = &view->state.ref;
5798 s->matched_entry = NULL;
5803 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5805 regmatch_t regmatch;
5807 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5811 static const struct got_error *
5812 search_next_ref_view(struct tog_view *view)
5814 struct tog_ref_view_state *s = &view->state.ref;
5815 struct tog_reflist_entry *re = NULL;
5817 if (!view->searching) {
5818 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5822 if (s->matched_entry) {
5823 if (view->searching == TOG_SEARCH_FORWARD) {
5824 if (s->selected_entry)
5825 re = TAILQ_NEXT(s->selected_entry, entry);
5827 re = TAILQ_PREV(s->selected_entry,
5828 tog_reflist_head, entry);
5830 if (s->selected_entry == NULL)
5831 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5833 re = TAILQ_PREV(s->selected_entry,
5834 tog_reflist_head, entry);
5837 if (view->searching == TOG_SEARCH_FORWARD)
5838 re = TAILQ_FIRST(&s->refs);
5840 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5845 if (s->matched_entry == NULL) {
5846 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5849 if (view->searching == TOG_SEARCH_FORWARD)
5850 re = TAILQ_FIRST(&s->refs);
5852 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5855 if (match_reflist_entry(re, &view->regex)) {
5856 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5857 s->matched_entry = re;
5861 if (view->searching == TOG_SEARCH_FORWARD)
5862 re = TAILQ_NEXT(re, entry);
5864 re = TAILQ_PREV(re, tog_reflist_head, entry);
5867 if (s->matched_entry) {
5868 s->first_displayed_entry = s->matched_entry;
5875 static const struct got_error *
5876 show_ref_view(struct tog_view *view)
5878 const struct got_error *err = NULL;
5879 struct tog_ref_view_state *s = &view->state.ref;
5880 struct tog_reflist_entry *re;
5883 struct tog_color *tc;
5885 int limit = view->nlines;
5887 werase(view->window);
5894 re = s->first_displayed_entry;
5896 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5898 return got_error_from_errno("asprintf");
5900 err = format_line(&wline, &width, line, view->ncols, 0);
5905 if (view_needs_focus_indication(view))
5906 wstandout(view->window);
5907 waddwstr(view->window, wline);
5908 if (view_needs_focus_indication(view))
5909 wstandend(view->window);
5914 if (width < view->ncols - 1)
5915 waddch(view->window, '\n');
5920 while (re && limit > 0) {
5923 if (got_ref_is_symbolic(re->ref)) {
5924 if (asprintf(&line, "%s -> %s",
5925 got_ref_get_name(re->ref),
5926 got_ref_get_symref_target(re->ref)) == -1)
5927 return got_error_from_errno("asprintf");
5928 } else if (s->show_ids) {
5929 struct got_object_id *id;
5931 err = got_ref_resolve(&id, s->repo, re->ref);
5934 err = got_object_id_str(&id_str, id);
5939 if (asprintf(&line, "%s: %s",
5940 got_ref_get_name(re->ref), id_str) == -1) {
5941 err = got_error_from_errno("asprintf");
5949 line = strdup(got_ref_get_name(re->ref));
5951 return got_error_from_errno("strdup");
5954 err = format_line(&wline, &width, line, view->ncols, 0);
5959 if (n == s->selected) {
5961 wstandout(view->window);
5962 s->selected_entry = re;
5964 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5966 wattr_on(view->window,
5967 COLOR_PAIR(tc->colorpair), NULL);
5968 waddwstr(view->window, wline);
5970 wattr_off(view->window,
5971 COLOR_PAIR(tc->colorpair), NULL);
5972 if (width < view->ncols - 1)
5973 waddch(view->window, '\n');
5974 if (n == s->selected && view->focussed)
5975 wstandend(view->window);
5981 s->last_displayed_entry = re;
5984 re = TAILQ_NEXT(re, entry);
5991 static const struct got_error *
5992 browse_ref_tree(struct tog_view **new_view, int begin_x,
5993 struct tog_reflist_entry *re, struct got_repository *repo)
5995 const struct got_error *err = NULL;
5996 struct got_object_id *commit_id = NULL, *tree_id = NULL;
5997 struct got_tree_object *tree = NULL;
5998 struct tog_view *tree_view;
6002 err = resolve_reflist_entry(&commit_id, re, repo);
6004 if (err->code != GOT_ERR_OBJ_TYPE)
6010 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6014 err = got_object_open_as_tree(&tree, repo, tree_id);
6018 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6019 if (tree_view == NULL) {
6020 err = got_error_from_errno("view_open");
6024 err = open_tree_view(tree_view, tree, commit_id, 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 (s->selected > view->nlines)
6130 s->selected = s->ndisplayed - 1;
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);