2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
123 SIMPLEQ_ENTRY(tog_color) entry;
127 SIMPLEQ_HEAD(tog_colors, tog_color);
129 static const struct got_error *
130 add_color(struct tog_colors *colors, const char *pattern,
131 int idx, short color)
133 const struct got_error *err = NULL;
134 struct tog_color *tc;
137 if (idx < 1 || idx > COLOR_PAIRS - 1)
140 init_pair(idx, color, -1);
142 tc = calloc(1, sizeof(*tc));
144 return got_error_from_errno("calloc");
145 regerr = regcomp(&tc->regex, pattern,
146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
148 static char regerr_msg[512];
149 static char err_msg[512];
150 regerror(regerr, &tc->regex, regerr_msg,
152 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
154 err = got_error_msg(GOT_ERR_REGEX, err_msg);
159 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
164 free_colors(struct tog_colors *colors)
166 struct tog_color *tc;
168 while (!SIMPLEQ_EMPTY(colors)) {
169 tc = SIMPLEQ_FIRST(colors);
170 SIMPLEQ_REMOVE_HEAD(colors, entry);
177 get_color(struct tog_colors *colors, int colorpair)
179 struct tog_color *tc = NULL;
181 SIMPLEQ_FOREACH(tc, colors, entry) {
182 if (tc->colorpair == colorpair)
190 default_color_value(const char *envvar)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 return COLOR_MAGENTA;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
196 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
198 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
200 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 return COLOR_MAGENTA;
202 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 return COLOR_MAGENTA;
204 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
206 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
208 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
210 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
212 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
214 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
216 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 return COLOR_MAGENTA;
218 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
225 get_color_value(const char *envvar)
227 const char *val = getenv(envvar);
230 return default_color_value(envvar);
232 if (strcasecmp(val, "black") == 0)
234 if (strcasecmp(val, "red") == 0)
236 if (strcasecmp(val, "green") == 0)
238 if (strcasecmp(val, "yellow") == 0)
240 if (strcasecmp(val, "blue") == 0)
242 if (strcasecmp(val, "magenta") == 0)
243 return COLOR_MAGENTA;
244 if (strcasecmp(val, "cyan") == 0)
246 if (strcasecmp(val, "white") == 0)
248 if (strcasecmp(val, "default") == 0)
251 return default_color_value(envvar);
255 struct tog_diff_view_state {
256 struct got_object_id *id1, *id2;
257 const char *label1, *label2;
259 int first_displayed_line;
260 int last_displayed_line;
263 int ignore_whitespace;
265 struct got_repository *repo;
266 struct got_reflist_head refs;
267 struct tog_colors colors;
273 /* passed from log view; may be NULL */
274 struct tog_view *log_view;
277 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 struct tog_log_thread_args {
280 pthread_cond_t need_commits;
281 pthread_cond_t commit_loaded;
283 struct got_commit_graph *graph;
284 struct commit_queue *commits;
285 const char *in_repo_path;
286 struct got_object_id *start_id;
287 struct got_repository *repo;
290 struct commit_queue_entry **first_displayed_entry;
291 struct commit_queue_entry **selected_entry;
293 int *search_next_done;
297 struct tog_log_view_state {
298 struct commit_queue commits;
299 struct commit_queue_entry *first_displayed_entry;
300 struct commit_queue_entry *last_displayed_entry;
301 struct commit_queue_entry *selected_entry;
304 const char *head_ref_name;
306 struct got_repository *repo;
307 struct got_reflist_head refs;
308 struct got_object_id *start_id;
311 struct tog_log_thread_args thread_args;
312 struct commit_queue_entry *matched_entry;
313 struct commit_queue_entry *search_entry;
314 struct tog_colors colors;
317 #define TOG_COLOR_DIFF_MINUS 1
318 #define TOG_COLOR_DIFF_PLUS 2
319 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 #define TOG_COLOR_DIFF_META 4
321 #define TOG_COLOR_TREE_SUBMODULE 5
322 #define TOG_COLOR_TREE_SYMLINK 6
323 #define TOG_COLOR_TREE_DIRECTORY 7
324 #define TOG_COLOR_TREE_EXECUTABLE 8
325 #define TOG_COLOR_COMMIT 9
326 #define TOG_COLOR_AUTHOR 10
327 #define TOG_COLOR_DATE 11
328 #define TOG_COLOR_REFS_HEADS 12
329 #define TOG_COLOR_REFS_TAGS 13
330 #define TOG_COLOR_REFS_REMOTES 14
332 struct tog_blame_cb_args {
333 struct tog_blame_line *lines; /* one per line */
336 struct tog_view *view;
337 struct got_object_id *commit_id;
341 struct tog_blame_thread_args {
343 struct got_repository *repo;
344 struct tog_blame_cb_args *cb_args;
346 got_cancel_cb cancel_cb;
353 struct tog_blame_line *lines;
357 struct tog_blame_thread_args thread_args;
358 struct tog_blame_cb_args cb_args;
362 struct tog_blame_view_state {
363 int first_displayed_line;
364 int last_displayed_line;
369 struct got_object_id_queue blamed_commits;
370 struct got_object_qid *blamed_commit;
372 struct got_repository *repo;
373 struct got_object_id *commit_id;
374 struct tog_blame blame;
376 struct tog_colors colors;
379 struct tog_parent_tree {
380 TAILQ_ENTRY(tog_parent_tree) entry;
381 struct got_tree_object *tree;
382 struct got_tree_entry *first_displayed_entry;
383 struct got_tree_entry *selected_entry;
387 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 struct tog_tree_view_state {
391 struct got_tree_object *root;
392 struct got_tree_object *tree;
393 struct got_tree_entry *first_displayed_entry;
394 struct got_tree_entry *last_displayed_entry;
395 struct got_tree_entry *selected_entry;
396 int ndisplayed, selected, show_ids;
397 struct tog_parent_trees parents;
398 struct got_object_id *commit_id;
399 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
441 TAILQ_HEAD(tog_view_list_head, tog_view);
444 TAILQ_ENTRY(tog_view) entry;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
451 struct tog_view *parent;
452 struct tog_view *child;
454 /* type-specific state */
455 enum tog_view_type type;
457 struct tog_diff_view_state diff;
458 struct tog_log_view_state log;
459 struct tog_blame_view_state blame;
460 struct tog_tree_view_state tree;
461 struct tog_ref_view_state ref;
464 const struct got_error *(*show)(struct tog_view *);
465 const struct got_error *(*input)(struct tog_view **,
466 struct tog_view **, struct tog_view *, int);
467 const struct got_error *(*close)(struct tog_view *);
469 const struct got_error *(*search_start)(struct tog_view *);
470 const struct got_error *(*search_next)(struct tog_view *);
472 #define TOG_SEARCH_FORWARD 1
473 #define TOG_SEARCH_BACKWARD 2
474 int search_next_done;
475 #define TOG_SEARCH_HAVE_MORE 1
476 #define TOG_SEARCH_NO_MORE 2
477 #define TOG_SEARCH_HAVE_NONE 3
482 static const struct got_error *open_diff_view(struct tog_view *,
483 struct got_object_id *, struct got_object_id *,
484 const char *, const char *, int, int, int, struct tog_view *,
485 struct got_repository *);
486 static const struct got_error *show_diff_view(struct tog_view *);
487 static const struct got_error *input_diff_view(struct tog_view **,
488 struct tog_view **, struct tog_view *, int);
489 static const struct got_error* close_diff_view(struct tog_view *);
490 static const struct got_error *search_start_diff_view(struct tog_view *);
491 static const struct got_error *search_next_diff_view(struct tog_view *);
493 static const struct got_error *open_log_view(struct tog_view *,
494 struct got_object_id *, struct got_repository *,
495 const char *, const char *, int);
496 static const struct got_error * show_log_view(struct tog_view *);
497 static const struct got_error *input_log_view(struct tog_view **,
498 struct tog_view **, struct tog_view *, int);
499 static const struct got_error *close_log_view(struct tog_view *);
500 static const struct got_error *search_start_log_view(struct tog_view *);
501 static const struct got_error *search_next_log_view(struct tog_view *);
503 static const struct got_error *open_blame_view(struct tog_view *, char *,
504 struct got_object_id *, struct got_repository *);
505 static const struct got_error *show_blame_view(struct tog_view *);
506 static const struct got_error *input_blame_view(struct tog_view **,
507 struct tog_view **, struct tog_view *, int);
508 static const struct got_error *close_blame_view(struct tog_view *);
509 static const struct got_error *search_start_blame_view(struct tog_view *);
510 static const struct got_error *search_next_blame_view(struct tog_view *);
512 static const struct got_error *open_tree_view(struct tog_view *,
513 struct got_tree_object *, struct got_object_id *, struct got_repository *);
514 static const struct got_error *show_tree_view(struct tog_view *);
515 static const struct got_error *input_tree_view(struct tog_view **,
516 struct tog_view **, struct tog_view *, int);
517 static const struct got_error *close_tree_view(struct tog_view *);
518 static const struct got_error *search_start_tree_view(struct tog_view *);
519 static const struct got_error *search_next_tree_view(struct tog_view *);
521 static const struct got_error *open_ref_view(struct tog_view *,
522 struct got_repository *);
523 static const struct got_error *show_ref_view(struct tog_view *);
524 static const struct got_error *input_ref_view(struct tog_view **,
525 struct tog_view **, struct tog_view *, int);
526 static const struct got_error *close_ref_view(struct tog_view *);
527 static const struct got_error *search_start_ref_view(struct tog_view *);
528 static const struct got_error *search_next_ref_view(struct tog_view *);
530 static volatile sig_atomic_t tog_sigwinch_received;
531 static volatile sig_atomic_t tog_sigpipe_received;
532 static volatile sig_atomic_t tog_sigcont_received;
535 tog_sigwinch(int signo)
537 tog_sigwinch_received = 1;
541 tog_sigpipe(int signo)
543 tog_sigpipe_received = 1;
547 tog_sigcont(int signo)
549 tog_sigcont_received = 1;
552 static const struct got_error *
553 view_close(struct tog_view *view)
555 const struct got_error *err = NULL;
558 view_close(view->child);
562 err = view->close(view);
564 del_panel(view->panel);
566 delwin(view->window);
571 static struct tog_view *
572 view_open(int nlines, int ncols, int begin_y, int begin_x,
573 enum tog_view_type type)
575 struct tog_view *view = calloc(1, sizeof(*view));
583 view->nlines = nlines ? nlines : LINES - begin_y;
584 view->ncols = ncols ? ncols : COLS - begin_x;
585 view->begin_y = begin_y;
586 view->begin_x = begin_x;
587 view->window = newwin(nlines, ncols, begin_y, begin_x);
588 if (view->window == NULL) {
592 view->panel = new_panel(view->window);
593 if (view->panel == NULL ||
594 set_panel_userptr(view->panel, view) != OK) {
599 keypad(view->window, TRUE);
604 view_split_begin_x(int begin_x)
606 if (begin_x > 0 || COLS < 120)
608 return (COLS - MAX(COLS / 2, 80));
611 static const struct got_error *view_resize(struct tog_view *);
613 static const struct got_error *
614 view_splitscreen(struct tog_view *view)
616 const struct got_error *err = NULL;
619 view->begin_x = view_split_begin_x(0);
620 view->nlines = LINES;
621 view->ncols = COLS - view->begin_x;
624 err = view_resize(view);
628 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
629 return got_error_from_errno("mvwin");
634 static const struct got_error *
635 view_fullscreen(struct tog_view *view)
637 const struct got_error *err = NULL;
641 view->nlines = LINES;
645 err = view_resize(view);
649 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
650 return got_error_from_errno("mvwin");
656 view_is_parent_view(struct tog_view *view)
658 return view->parent == NULL;
661 static const struct got_error *
662 view_resize(struct tog_view *view)
666 if (view->lines > LINES)
667 nlines = view->nlines - (view->lines - LINES);
669 nlines = view->nlines + (LINES - view->lines);
671 if (view->cols > COLS)
672 ncols = view->ncols - (view->cols - COLS);
674 ncols = view->ncols + (COLS - view->cols);
676 if (wresize(view->window, nlines, ncols) == ERR)
677 return got_error_from_errno("wresize");
678 if (replace_panel(view->panel, view->window) == ERR)
679 return got_error_from_errno("replace_panel");
680 wclear(view->window);
682 view->nlines = nlines;
688 view->child->begin_x = view_split_begin_x(view->begin_x);
689 if (view->child->begin_x == 0) {
690 view_fullscreen(view->child);
691 if (view->child->focussed)
692 show_panel(view->child->panel);
694 show_panel(view->panel);
696 view_splitscreen(view->child);
697 show_panel(view->child->panel);
704 static const struct got_error *
705 view_close_child(struct tog_view *view)
707 const struct got_error *err = NULL;
709 if (view->child == NULL)
712 err = view_close(view->child);
718 view_set_child(struct tog_view *view, struct tog_view *child)
721 child->parent = view;
725 view_is_splitscreen(struct tog_view *view)
727 return view->begin_x > 0;
736 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
737 cols = 80; /* Default */
743 resize_term(lines, cols);
746 static const struct got_error *
747 view_search_start(struct tog_view *view)
749 const struct got_error *err = NULL;
753 if (view->nlines < 1)
756 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
757 wclrtoeol(view->window);
761 ret = wgetnstr(view->window, pattern, sizeof(pattern));
767 if (view->searching) {
768 regfree(&view->regex);
772 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
773 err = view->search_start(view);
775 regfree(&view->regex);
778 view->searching = TOG_SEARCH_FORWARD;
779 view->search_next_done = 0;
780 view->search_next(view);
786 static const struct got_error *
787 view_input(struct tog_view **new, struct tog_view **focus, int *done,
788 struct tog_view *view, struct tog_view_list_head *views)
790 const struct got_error *err = NULL;
797 /* Clear "no matches" indicator. */
798 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
799 view->search_next_done == TOG_SEARCH_HAVE_NONE)
800 view->search_next_done = TOG_SEARCH_HAVE_MORE;
802 if (view->searching && !view->search_next_done) {
803 errcode = pthread_mutex_unlock(&tog_mutex);
805 return got_error_set_errno(errcode,
806 "pthread_mutex_unlock");
808 errcode = pthread_mutex_lock(&tog_mutex);
810 return got_error_set_errno(errcode,
811 "pthread_mutex_lock");
812 view->search_next(view);
816 nodelay(stdscr, FALSE);
817 /* Allow threads to make progress while we are waiting for input. */
818 errcode = pthread_mutex_unlock(&tog_mutex);
820 return got_error_set_errno(errcode, "pthread_mutex_unlock");
821 ch = wgetch(view->window);
822 errcode = pthread_mutex_lock(&tog_mutex);
824 return got_error_set_errno(errcode, "pthread_mutex_lock");
825 nodelay(stdscr, TRUE);
827 if (tog_sigwinch_received || tog_sigcont_received) {
829 tog_sigwinch_received = 0;
830 tog_sigcont_received = 0;
831 TAILQ_FOREACH(v, views, entry) {
832 err = view_resize(v);
835 err = v->input(new, focus, v, KEY_RESIZE);
846 *focus = view->child;
847 } else if (view->parent) {
848 *focus = view->parent;
852 err = view->input(new, focus, view, ch);
859 if (view_is_parent_view(view)) {
860 if (view->child == NULL)
862 if (view_is_splitscreen(view->child)) {
863 *focus = view->child;
864 err = view_fullscreen(view->child);
866 err = view_splitscreen(view->child);
869 err = view->child->input(new, focus, view->child,
872 if (view_is_splitscreen(view)) {
874 err = view_fullscreen(view);
876 err = view_splitscreen(view);
880 err = view->input(new, focus, view, KEY_RESIZE);
886 if (view->search_start)
887 view_search_start(view);
889 err = view->input(new, focus, view, ch);
893 if (view->search_next) {
894 view->searching = (ch == 'n' ?
895 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
896 view->search_next_done = 0;
897 view->search_next(view);
899 err = view->input(new, focus, view, ch);
902 err = view->input(new, focus, view, ch);
910 view_vborder(struct tog_view *view)
913 struct tog_view *view_above;
916 return view_vborder(view->parent);
918 panel = panel_above(view->panel);
922 view_above = panel_userptr(panel);
923 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
924 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
928 view_needs_focus_indication(struct tog_view *view)
930 if (view_is_parent_view(view)) {
931 if (view->child == NULL || view->child->focussed)
933 if (!view_is_splitscreen(view->child))
935 } else if (!view_is_splitscreen(view))
938 return view->focussed;
941 static const struct got_error *
942 view_loop(struct tog_view *view)
944 const struct got_error *err = NULL;
945 struct tog_view_list_head views;
946 struct tog_view *new_view, *focus_view, *main_view;
947 int fast_refresh = 10;
948 int done = 0, errcode;
950 errcode = pthread_mutex_lock(&tog_mutex);
952 return got_error_set_errno(errcode, "pthread_mutex_lock");
955 TAILQ_INSERT_HEAD(&views, view, entry);
959 err = view->show(view);
964 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
965 /* Refresh fast during initialization, then become slower. */
966 if (fast_refresh && fast_refresh-- == 0)
967 halfdelay(10); /* switch to once per second */
969 err = view_input(&new_view, &focus_view, &done, view, &views);
973 struct tog_view *prev = NULL;
975 if (view_is_parent_view(view))
976 prev = TAILQ_PREV(view, tog_view_list_head,
978 else if (view->parent != view)
982 view->parent->child = NULL;
984 TAILQ_REMOVE(&views, view, entry);
986 err = view_close(view);
987 if (err || (view == main_view && new_view == NULL))
994 else if (!TAILQ_EMPTY(&views))
995 view = TAILQ_LAST(&views,
1001 view->child->focussed)
1002 focus_view = view->child;
1008 struct tog_view *v, *t;
1009 /* Only allow one parent view per type. */
1010 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1011 if (v->type != new_view->type)
1013 TAILQ_REMOVE(&views, v, entry);
1014 err = view_close(v);
1019 TAILQ_INSERT_TAIL(&views, new_view, entry);
1021 if (focus_view == NULL)
1022 focus_view = new_view;
1025 show_panel(focus_view->panel);
1028 focus_view->focussed = 1;
1031 show_panel(new_view->panel);
1032 if (view->child && view_is_splitscreen(view->child))
1033 show_panel(view->child->panel);
1036 if (focus_view == NULL) {
1038 show_panel(view->panel);
1039 if (view->child && view_is_splitscreen(view->child))
1040 show_panel(view->child->panel);
1044 err = view->parent->show(view->parent);
1048 err = view->show(view);
1052 err = view->child->show(view->child);
1061 while (!TAILQ_EMPTY(&views)) {
1062 view = TAILQ_FIRST(&views);
1063 TAILQ_REMOVE(&views, view, entry);
1067 errcode = pthread_mutex_unlock(&tog_mutex);
1068 if (errcode && err == NULL)
1069 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1079 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1084 /* Create newly allocated wide-character string equivalent to a byte string. */
1085 static const struct got_error *
1086 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1089 const struct got_error *err = NULL;
1092 *wlen = mbstowcs(NULL, s, 0);
1093 if (*wlen == (size_t)-1) {
1095 if (errno != EILSEQ)
1096 return got_error_from_errno("mbstowcs");
1098 /* byte string invalid in current encoding; try to "fix" it */
1099 err = got_mbsavis(&vis, &vislen, s);
1102 *wlen = mbstowcs(NULL, vis, 0);
1103 if (*wlen == (size_t)-1) {
1104 err = got_error_from_errno("mbstowcs"); /* give up */
1109 *ws = calloc(*wlen + 1, sizeof(**ws));
1111 err = got_error_from_errno("calloc");
1115 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1116 err = got_error_from_errno("mbstowcs");
1127 /* Format a line for display, ensuring that it won't overflow a width limit. */
1128 static const struct got_error *
1129 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1132 const struct got_error *err = NULL;
1134 wchar_t *wline = NULL;
1141 err = mbs2ws(&wline, &wlen, line);
1147 int width = wcwidth(wline[i]);
1154 if (width == 1 || width == 2) {
1155 if (cols + width > wlimit)
1159 } else if (width == -1) {
1160 if (wline[i] == L'\t') {
1162 ((cols + col_tab_align) % TABSIZE);
1163 if (cols + width > wlimit)
1169 err = got_error_from_errno("wcwidth");
1184 static const struct got_error*
1185 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1186 struct got_object_id *id, struct got_repository *repo)
1188 static const struct got_error *err = NULL;
1189 struct got_reflist_entry *re;
1195 SIMPLEQ_FOREACH(re, refs, entry) {
1196 struct got_tag_object *tag = NULL;
1197 struct got_object_id *ref_id;
1200 name = got_ref_get_name(re->ref);
1201 if (strcmp(name, GOT_REF_HEAD) == 0)
1203 if (strncmp(name, "refs/", 5) == 0)
1205 if (strncmp(name, "got/", 4) == 0)
1207 if (strncmp(name, "heads/", 6) == 0)
1209 if (strncmp(name, "remotes/", 8) == 0) {
1211 s = strstr(name, "/" GOT_REF_HEAD);
1212 if (s != NULL && s[strlen(s)] == '\0')
1215 err = got_ref_resolve(&ref_id, repo, re->ref);
1218 if (strncmp(name, "tags/", 5) == 0) {
1219 err = got_object_open_as_tag(&tag, repo, ref_id);
1221 if (err->code != GOT_ERR_OBJ_TYPE) {
1225 /* Ref points at something other than a tag. */
1230 cmp = got_object_id_cmp(tag ?
1231 got_object_tag_get_object_id(tag) : ref_id, id);
1234 got_object_tag_close(tag);
1238 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1239 s ? ", " : "", name) == -1) {
1240 err = got_error_from_errno("asprintf");
1251 static const struct got_error *
1252 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1255 char *smallerthan, *at;
1257 smallerthan = strchr(author, '<');
1258 if (smallerthan && smallerthan[1] != '\0')
1259 author = smallerthan + 1;
1260 at = strchr(author, '@');
1263 return format_line(wauthor, author_width, author, limit, col_tab_align);
1266 static const struct got_error *
1267 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1268 struct got_object_id *id, const size_t date_display_cols,
1269 int author_display_cols)
1271 struct tog_log_view_state *s = &view->state.log;
1272 const struct got_error *err = NULL;
1273 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1274 char *logmsg0 = NULL, *logmsg = NULL;
1275 char *author = NULL;
1276 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1277 int author_width, logmsg_width;
1278 char *newline, *line = NULL;
1280 const int avail = view->ncols;
1282 time_t committer_time;
1283 struct tog_color *tc;
1285 committer_time = got_object_commit_get_committer_time(commit);
1286 if (localtime_r(&committer_time, &tm) == NULL)
1287 return got_error_from_errno("localtime_r");
1288 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1290 return got_error(GOT_ERR_NO_SPACE);
1292 if (avail <= date_display_cols)
1293 limit = MIN(sizeof(datebuf) - 1, avail);
1295 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1296 tc = get_color(&s->colors, TOG_COLOR_DATE);
1298 wattr_on(view->window,
1299 COLOR_PAIR(tc->colorpair), NULL);
1300 waddnstr(view->window, datebuf, limit);
1302 wattr_off(view->window,
1303 COLOR_PAIR(tc->colorpair), NULL);
1310 err = got_object_id_str(&id_str, id);
1313 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1315 wattr_on(view->window,
1316 COLOR_PAIR(tc->colorpair), NULL);
1317 wprintw(view->window, "%.8s ", id_str);
1319 wattr_off(view->window,
1320 COLOR_PAIR(tc->colorpair), NULL);
1327 author = strdup(got_object_commit_get_author(commit));
1328 if (author == NULL) {
1329 err = got_error_from_errno("strdup");
1332 err = format_author(&wauthor, &author_width, author, avail - col, col);
1335 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1337 wattr_on(view->window,
1338 COLOR_PAIR(tc->colorpair), NULL);
1339 waddwstr(view->window, wauthor);
1341 wattr_off(view->window,
1342 COLOR_PAIR(tc->colorpair), NULL);
1343 col += author_width;
1344 while (col < avail && author_width < author_display_cols + 2) {
1345 waddch(view->window, ' ');
1352 err = got_object_commit_get_logmsg(&logmsg0, commit);
1356 while (*logmsg == '\n')
1358 newline = strchr(logmsg, '\n');
1361 limit = avail - col;
1362 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1365 waddwstr(view->window, wlogmsg);
1366 col += logmsg_width;
1367 while (col < avail) {
1368 waddch(view->window, ' ');
1380 static struct commit_queue_entry *
1381 alloc_commit_queue_entry(struct got_commit_object *commit,
1382 struct got_object_id *id)
1384 struct commit_queue_entry *entry;
1386 entry = calloc(1, sizeof(*entry));
1391 entry->commit = commit;
1396 pop_commit(struct commit_queue *commits)
1398 struct commit_queue_entry *entry;
1400 entry = TAILQ_FIRST(&commits->head);
1401 TAILQ_REMOVE(&commits->head, entry, entry);
1402 got_object_commit_close(entry->commit);
1403 commits->ncommits--;
1404 /* Don't free entry->id! It is owned by the commit graph. */
1409 free_commits(struct commit_queue *commits)
1411 while (!TAILQ_EMPTY(&commits->head))
1412 pop_commit(commits);
1415 static const struct got_error *
1416 match_commit(int *have_match, struct got_object_id *id,
1417 struct got_commit_object *commit, regex_t *regex)
1419 const struct got_error *err = NULL;
1420 regmatch_t regmatch;
1421 char *id_str = NULL, *logmsg = NULL;
1425 err = got_object_id_str(&id_str, id);
1429 err = got_object_commit_get_logmsg(&logmsg, commit);
1433 if (regexec(regex, got_object_commit_get_author(commit), 1,
1434 ®match, 0) == 0 ||
1435 regexec(regex, got_object_commit_get_committer(commit), 1,
1436 ®match, 0) == 0 ||
1437 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1438 regexec(regex, logmsg, 1, ®match, 0) == 0)
1446 static const struct got_error *
1447 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1448 int minqueue, struct got_repository *repo, const char *path,
1449 int *searching, int *search_next_done, regex_t *regex)
1451 const struct got_error *err = NULL;
1455 * We keep all commits open throughout the lifetime of the log
1456 * view in order to avoid having to re-fetch commits from disk
1457 * while updating the display.
1459 while (nqueued < minqueue ||
1460 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1461 struct got_object_id *id;
1462 struct got_commit_object *commit;
1463 struct commit_queue_entry *entry;
1466 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1467 if (err || id == NULL)
1470 err = got_object_open_as_commit(&commit, repo, id);
1473 entry = alloc_commit_queue_entry(commit, id);
1474 if (entry == NULL) {
1475 err = got_error_from_errno("alloc_commit_queue_entry");
1479 errcode = pthread_mutex_lock(&tog_mutex);
1481 err = got_error_set_errno(errcode,
1482 "pthread_mutex_lock");
1486 entry->idx = commits->ncommits;
1487 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1489 commits->ncommits++;
1491 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1493 err = match_commit(&have_match, id, commit, regex);
1497 *search_next_done = TOG_SEARCH_HAVE_MORE;
1500 errcode = pthread_mutex_unlock(&tog_mutex);
1501 if (errcode && err == NULL)
1502 err = got_error_set_errno(errcode,
1503 "pthread_mutex_unlock");
1511 static const struct got_error *
1512 draw_commits(struct tog_view *view)
1514 const struct got_error *err = NULL;
1515 struct tog_log_view_state *s = &view->state.log;
1516 struct commit_queue_entry *entry;
1517 const int limit = view->nlines;
1519 int ncommits, author_cols = 4;
1520 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1521 char *refs_str = NULL;
1523 struct tog_color *tc;
1524 static const size_t date_display_cols = 12;
1526 entry = s->first_displayed_entry;
1529 if (ncommits == s->selected) {
1530 s->selected_entry = entry;
1533 entry = TAILQ_NEXT(entry, entry);
1537 if (s->selected_entry &&
1538 !(view->searching && view->search_next_done == 0)) {
1539 err = got_object_id_str(&id_str, s->selected_entry->id);
1542 err = build_refs_str(&refs_str, &s->refs,
1543 s->selected_entry->id, s->repo);
1548 if (s->thread_args.commits_needed == 0)
1549 halfdelay(10); /* disable fast refresh */
1551 if (s->thread_args.commits_needed > 0) {
1552 if (asprintf(&ncommits_str, " [%d/%d] %s",
1553 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1554 (view->searching && !view->search_next_done) ?
1555 "searching..." : "loading...") == -1) {
1556 err = got_error_from_errno("asprintf");
1560 const char *search_str = NULL;
1562 if (view->searching) {
1563 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1564 search_str = "no more matches";
1565 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1566 search_str = "no matches found";
1567 else if (!view->search_next_done)
1568 search_str = "searching...";
1571 if (asprintf(&ncommits_str, " [%d/%d] %s",
1572 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1573 search_str ? search_str :
1574 (refs_str ? refs_str : "")) == -1) {
1575 err = got_error_from_errno("asprintf");
1580 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1581 if (asprintf(&header, "commit %s %s%s",
1582 id_str ? id_str : "........................................",
1583 s->in_repo_path, ncommits_str) == -1) {
1584 err = got_error_from_errno("asprintf");
1588 } else if (asprintf(&header, "commit %s%s",
1589 id_str ? id_str : "........................................",
1590 ncommits_str) == -1) {
1591 err = got_error_from_errno("asprintf");
1595 err = format_line(&wline, &width, header, view->ncols, 0);
1599 werase(view->window);
1601 if (view_needs_focus_indication(view))
1602 wstandout(view->window);
1603 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1605 wattr_on(view->window,
1606 COLOR_PAIR(tc->colorpair), NULL);
1607 waddwstr(view->window, wline);
1609 wattr_off(view->window,
1610 COLOR_PAIR(tc->colorpair), NULL);
1611 while (width < view->ncols) {
1612 waddch(view->window, ' ');
1615 if (view_needs_focus_indication(view))
1616 wstandend(view->window);
1621 /* Grow author column size if necessary. */
1622 entry = s->first_displayed_entry;
1628 if (ncommits >= limit - 1)
1630 author = strdup(got_object_commit_get_author(entry->commit));
1631 if (author == NULL) {
1632 err = got_error_from_errno("strdup");
1635 err = format_author(&wauthor, &width, author, COLS,
1637 if (author_cols < width)
1638 author_cols = width;
1642 entry = TAILQ_NEXT(entry, entry);
1645 entry = s->first_displayed_entry;
1646 s->last_displayed_entry = s->first_displayed_entry;
1649 if (ncommits >= limit - 1)
1651 if (ncommits == s->selected)
1652 wstandout(view->window);
1653 err = draw_commit(view, entry->commit, entry->id,
1654 date_display_cols, author_cols);
1655 if (ncommits == s->selected)
1656 wstandend(view->window);
1660 s->last_displayed_entry = entry;
1661 entry = TAILQ_NEXT(entry, entry);
1674 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1676 struct commit_queue_entry *entry;
1679 entry = TAILQ_FIRST(&s->commits.head);
1680 if (s->first_displayed_entry == entry)
1683 entry = s->first_displayed_entry;
1684 while (entry && nscrolled < maxscroll) {
1685 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1687 s->first_displayed_entry = entry;
1693 static const struct got_error *
1694 trigger_log_thread(struct tog_view *view, int wait)
1696 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1699 halfdelay(1); /* fast refresh while loading commits */
1701 while (ta->commits_needed > 0) {
1702 if (ta->log_complete)
1705 /* Wake the log thread. */
1706 errcode = pthread_cond_signal(&ta->need_commits);
1708 return got_error_set_errno(errcode,
1709 "pthread_cond_signal");
1712 * The mutex will be released while the view loop waits
1713 * in wgetch(), at which time the log thread will run.
1718 /* Display progress update in log view. */
1719 show_log_view(view);
1723 /* Wait right here while next commit is being loaded. */
1724 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1726 return got_error_set_errno(errcode,
1727 "pthread_cond_wait");
1729 /* Display progress update in log view. */
1730 show_log_view(view);
1738 static const struct got_error *
1739 log_scroll_down(struct tog_view *view, int maxscroll)
1741 struct tog_log_view_state *s = &view->state.log;
1742 const struct got_error *err = NULL;
1743 struct commit_queue_entry *pentry;
1744 int nscrolled = 0, ncommits_needed;
1746 if (s->last_displayed_entry == NULL)
1749 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1750 if (s->commits.ncommits < ncommits_needed &&
1751 !s->thread_args.log_complete) {
1753 * Ask the log thread for required amount of commits.
1755 s->thread_args.commits_needed += maxscroll;
1756 err = trigger_log_thread(view, 1);
1762 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1766 s->last_displayed_entry = pentry;
1768 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1771 s->first_displayed_entry = pentry;
1772 } while (++nscrolled < maxscroll);
1777 static const struct got_error *
1778 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1779 struct got_commit_object *commit, struct got_object_id *commit_id,
1780 struct tog_view *log_view, struct got_repository *repo)
1782 const struct got_error *err;
1783 struct got_object_qid *parent_id;
1784 struct tog_view *diff_view;
1786 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1787 if (diff_view == NULL)
1788 return got_error_from_errno("view_open");
1790 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1791 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1792 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1794 *new_view = diff_view;
1798 static const struct got_error *
1799 tree_view_visit_subtree(struct tog_tree_view_state *s,
1800 struct got_tree_object *subtree)
1802 struct tog_parent_tree *parent;
1804 parent = calloc(1, sizeof(*parent));
1806 return got_error_from_errno("calloc");
1808 parent->tree = s->tree;
1809 parent->first_displayed_entry = s->first_displayed_entry;
1810 parent->selected_entry = s->selected_entry;
1811 parent->selected = s->selected;
1812 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1815 s->first_displayed_entry = NULL;
1819 static const struct got_error *
1820 tree_view_walk_path(struct tog_tree_view_state *s,
1821 struct got_object_id *commit_id, const char *path)
1823 const struct got_error *err = NULL;
1824 struct got_tree_object *tree = NULL;
1826 char *slash, *subpath = NULL;
1828 /* Walk the path and open corresponding tree objects. */
1831 struct got_tree_entry *te;
1832 struct got_object_id *tree_id;
1838 /* Ensure the correct subtree entry is selected. */
1839 slash = strchr(p, '/');
1841 te_name = strdup(p);
1843 te_name = strndup(p, slash - p);
1844 if (te_name == NULL) {
1845 err = got_error_from_errno("strndup");
1848 te = got_object_tree_find_entry(s->tree, te_name);
1850 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1855 s->first_displayed_entry = s->selected_entry = te;
1857 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1858 break; /* jump to this file's entry */
1860 slash = strchr(p, '/');
1862 subpath = strndup(path, slash - path);
1864 subpath = strdup(path);
1865 if (subpath == NULL) {
1866 err = got_error_from_errno("strdup");
1870 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1875 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1880 err = tree_view_visit_subtree(s, tree);
1882 got_object_tree_close(tree);
1896 static const struct got_error *
1897 browse_commit_tree(struct tog_view **new_view, int begin_x,
1898 struct commit_queue_entry *entry, const char *path,
1899 struct got_repository *repo)
1901 const struct got_error *err = NULL;
1902 struct got_tree_object *tree;
1903 struct tog_tree_view_state *s;
1904 struct tog_view *tree_view;
1906 err = got_object_open_as_tree(&tree, repo,
1907 got_object_commit_get_tree_id(entry->commit));
1911 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1912 if (tree_view == NULL)
1913 return got_error_from_errno("view_open");
1915 err = open_tree_view(tree_view, tree, entry->id, repo);
1917 got_object_tree_close(tree);
1920 s = &tree_view->state.tree;
1922 *new_view = tree_view;
1924 if (got_path_is_root_dir(path))
1927 return tree_view_walk_path(s, entry->id, path);
1930 static const struct got_error *
1931 block_signals_used_by_main_thread(void)
1936 if (sigemptyset(&sigset) == -1)
1937 return got_error_from_errno("sigemptyset");
1939 /* tog handles SIGWINCH and SIGCONT */
1940 if (sigaddset(&sigset, SIGWINCH) == -1)
1941 return got_error_from_errno("sigaddset");
1942 if (sigaddset(&sigset, SIGCONT) == -1)
1943 return got_error_from_errno("sigaddset");
1945 /* ncurses handles SIGTSTP */
1946 if (sigaddset(&sigset, SIGTSTP) == -1)
1947 return got_error_from_errno("sigaddset");
1949 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1951 return got_error_set_errno(errcode, "pthread_sigmask");
1957 log_thread(void *arg)
1959 const struct got_error *err = NULL;
1961 struct tog_log_thread_args *a = arg;
1964 err = block_signals_used_by_main_thread();
1968 while (!done && !err && !tog_sigpipe_received) {
1969 err = queue_commits(a->graph, a->commits, 1, a->repo,
1970 a->in_repo_path, a->searching, a->search_next_done,
1973 if (err->code != GOT_ERR_ITER_COMPLETED)
1977 } else if (a->commits_needed > 0)
1978 a->commits_needed--;
1980 errcode = pthread_mutex_lock(&tog_mutex);
1982 err = got_error_set_errno(errcode,
1983 "pthread_mutex_lock");
1985 } else if (*a->quit)
1987 else if (*a->first_displayed_entry == NULL) {
1988 *a->first_displayed_entry =
1989 TAILQ_FIRST(&a->commits->head);
1990 *a->selected_entry = *a->first_displayed_entry;
1993 errcode = pthread_cond_signal(&a->commit_loaded);
1995 err = got_error_set_errno(errcode,
1996 "pthread_cond_signal");
1997 pthread_mutex_unlock(&tog_mutex);
2002 a->commits_needed = 0;
2004 if (a->commits_needed == 0) {
2005 errcode = pthread_cond_wait(&a->need_commits,
2008 err = got_error_set_errno(errcode,
2009 "pthread_cond_wait");
2013 errcode = pthread_mutex_unlock(&tog_mutex);
2014 if (errcode && err == NULL)
2015 err = got_error_set_errno(errcode,
2016 "pthread_mutex_unlock");
2018 a->log_complete = 1;
2022 static const struct got_error *
2023 stop_log_thread(struct tog_log_view_state *s)
2025 const struct got_error *err = NULL;
2030 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2032 return got_error_set_errno(errcode,
2033 "pthread_cond_signal");
2034 errcode = pthread_mutex_unlock(&tog_mutex);
2036 return got_error_set_errno(errcode,
2037 "pthread_mutex_unlock");
2038 errcode = pthread_join(s->thread, (void **)&err);
2040 return got_error_set_errno(errcode, "pthread_join");
2041 errcode = pthread_mutex_lock(&tog_mutex);
2043 return got_error_set_errno(errcode,
2044 "pthread_mutex_lock");
2048 if (s->thread_args.repo) {
2049 got_repo_close(s->thread_args.repo);
2050 s->thread_args.repo = NULL;
2053 if (s->thread_args.graph) {
2054 got_commit_graph_close(s->thread_args.graph);
2055 s->thread_args.graph = NULL;
2061 static const struct got_error *
2062 close_log_view(struct tog_view *view)
2064 const struct got_error *err = NULL;
2065 struct tog_log_view_state *s = &view->state.log;
2068 err = stop_log_thread(s);
2070 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2071 if (errcode && err == NULL)
2072 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2074 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2075 if (errcode && err == NULL)
2076 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2078 free_commits(&s->commits);
2079 free(s->in_repo_path);
2080 s->in_repo_path = NULL;
2083 got_ref_list_free(&s->refs);
2087 static const struct got_error *
2088 search_start_log_view(struct tog_view *view)
2090 struct tog_log_view_state *s = &view->state.log;
2092 s->matched_entry = NULL;
2093 s->search_entry = NULL;
2097 static const struct got_error *
2098 search_next_log_view(struct tog_view *view)
2100 const struct got_error *err = NULL;
2101 struct tog_log_view_state *s = &view->state.log;
2102 struct commit_queue_entry *entry;
2104 /* Display progress update in log view. */
2105 show_log_view(view);
2109 if (s->search_entry) {
2111 errcode = pthread_mutex_unlock(&tog_mutex);
2113 return got_error_set_errno(errcode,
2114 "pthread_mutex_unlock");
2115 ch = wgetch(view->window);
2116 errcode = pthread_mutex_lock(&tog_mutex);
2118 return got_error_set_errno(errcode,
2119 "pthread_mutex_lock");
2120 if (ch == KEY_BACKSPACE) {
2121 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2124 if (view->searching == TOG_SEARCH_FORWARD)
2125 entry = TAILQ_NEXT(s->search_entry, entry);
2127 entry = TAILQ_PREV(s->search_entry,
2128 commit_queue_head, entry);
2129 } else if (s->matched_entry) {
2130 if (view->searching == TOG_SEARCH_FORWARD)
2131 entry = TAILQ_NEXT(s->matched_entry, entry);
2133 entry = TAILQ_PREV(s->matched_entry,
2134 commit_queue_head, entry);
2136 if (view->searching == TOG_SEARCH_FORWARD)
2137 entry = TAILQ_FIRST(&s->commits.head);
2139 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2145 if (entry == NULL) {
2146 if (s->thread_args.log_complete ||
2147 view->searching == TOG_SEARCH_BACKWARD) {
2148 view->search_next_done =
2149 (s->matched_entry == NULL ?
2150 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2151 s->search_entry = NULL;
2155 * Poke the log thread for more commits and return,
2156 * allowing the main loop to make progress. Search
2157 * will resume at s->search_entry once we come back.
2159 s->thread_args.commits_needed++;
2160 return trigger_log_thread(view, 0);
2163 err = match_commit(&have_match, entry->id, entry->commit,
2168 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2169 s->matched_entry = entry;
2173 s->search_entry = entry;
2174 if (view->searching == TOG_SEARCH_FORWARD)
2175 entry = TAILQ_NEXT(entry, entry);
2177 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2180 if (s->matched_entry) {
2181 int cur = s->selected_entry->idx;
2182 while (cur < s->matched_entry->idx) {
2183 err = input_log_view(NULL, NULL, view, KEY_DOWN);
2188 while (cur > s->matched_entry->idx) {
2189 err = input_log_view(NULL, NULL, view, KEY_UP);
2196 s->search_entry = NULL;
2201 static const struct got_error *
2202 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2203 struct got_repository *repo, const char *head_ref_name,
2204 const char *in_repo_path, int log_branches)
2206 const struct got_error *err = NULL;
2207 struct tog_log_view_state *s = &view->state.log;
2208 struct got_repository *thread_repo = NULL;
2209 struct got_commit_graph *thread_graph = NULL;
2212 SIMPLEQ_INIT(&s->refs);
2214 if (in_repo_path != s->in_repo_path) {
2215 free(s->in_repo_path);
2216 s->in_repo_path = strdup(in_repo_path);
2217 if (s->in_repo_path == NULL)
2218 return got_error_from_errno("strdup");
2221 /* The commit queue only contains commits being displayed. */
2222 TAILQ_INIT(&s->commits.head);
2223 s->commits.ncommits = 0;
2225 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2230 s->head_ref_name = head_ref_name;
2231 s->start_id = got_object_id_dup(start_id);
2232 if (s->start_id == NULL) {
2233 err = got_error_from_errno("got_object_id_dup");
2236 s->log_branches = log_branches;
2238 SIMPLEQ_INIT(&s->colors);
2239 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2240 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2241 get_color_value("TOG_COLOR_COMMIT"));
2244 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2245 get_color_value("TOG_COLOR_AUTHOR"));
2247 free_colors(&s->colors);
2250 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2251 get_color_value("TOG_COLOR_DATE"));
2253 free_colors(&s->colors);
2258 view->show = show_log_view;
2259 view->input = input_log_view;
2260 view->close = close_log_view;
2261 view->search_start = search_start_log_view;
2262 view->search_next = search_next_log_view;
2264 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2267 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2271 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2272 s->repo, NULL, NULL);
2276 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2278 err = got_error_set_errno(errcode, "pthread_cond_init");
2281 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2283 err = got_error_set_errno(errcode, "pthread_cond_init");
2287 s->thread_args.commits_needed = view->nlines;
2288 s->thread_args.graph = thread_graph;
2289 s->thread_args.commits = &s->commits;
2290 s->thread_args.in_repo_path = s->in_repo_path;
2291 s->thread_args.start_id = s->start_id;
2292 s->thread_args.repo = thread_repo;
2293 s->thread_args.log_complete = 0;
2294 s->thread_args.quit = &s->quit;
2295 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2296 s->thread_args.selected_entry = &s->selected_entry;
2297 s->thread_args.searching = &view->searching;
2298 s->thread_args.search_next_done = &view->search_next_done;
2299 s->thread_args.regex = &view->regex;
2302 close_log_view(view);
2306 static const struct got_error *
2307 show_log_view(struct tog_view *view)
2309 const struct got_error *err;
2310 struct tog_log_view_state *s = &view->state.log;
2312 if (s->thread == NULL) {
2313 int errcode = pthread_create(&s->thread, NULL, log_thread,
2316 return got_error_set_errno(errcode, "pthread_create");
2317 if (s->thread_args.commits_needed > 0) {
2318 err = trigger_log_thread(view, 1);
2324 return draw_commits(view);
2327 static const struct got_error *
2328 input_log_view(struct tog_view **new_view, struct tog_view **focus_view,
2329 struct tog_view *view, int ch)
2331 const struct got_error *err = NULL;
2332 struct tog_log_view_state *s = &view->state.log;
2333 char *parent_path, *in_repo_path = NULL;
2334 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2335 struct tog_view *ref_view = NULL;
2337 struct got_object_id *start_id;
2347 if (s->first_displayed_entry == NULL)
2349 if (s->selected > 0)
2352 log_scroll_up(s, 1);
2356 if (s->first_displayed_entry == NULL)
2358 if (TAILQ_FIRST(&s->commits.head) ==
2359 s->first_displayed_entry) {
2363 log_scroll_up(s, view->nlines - 1);
2369 if (s->first_displayed_entry == NULL)
2371 if (s->selected < MIN(view->nlines - 2,
2372 s->commits.ncommits - 1)) {
2376 err = log_scroll_down(view, 1);
2380 struct commit_queue_entry *first;
2381 first = s->first_displayed_entry;
2384 err = log_scroll_down(view, view->nlines - 1);
2387 if (first == s->first_displayed_entry &&
2388 s->selected < MIN(view->nlines - 2,
2389 s->commits.ncommits - 1)) {
2390 /* can't scroll further down */
2391 s->selected = MIN(view->nlines - 2,
2392 s->commits.ncommits - 1);
2398 if (s->selected > view->nlines - 2)
2399 s->selected = view->nlines - 2;
2400 if (s->selected > s->commits.ncommits - 1)
2401 s->selected = s->commits.ncommits - 1;
2402 if (s->commits.ncommits < view->nlines - 1 &&
2403 !s->thread_args.log_complete) {
2404 s->thread_args.commits_needed += (view->nlines - 1) -
2405 s->commits.ncommits;
2406 err = trigger_log_thread(view, 1);
2412 if (s->selected_entry == NULL)
2414 if (view_is_parent_view(view))
2415 begin_x = view_split_begin_x(view->begin_x);
2416 err = open_diff_view_for_commit(&diff_view, begin_x,
2417 s->selected_entry->commit, s->selected_entry->id,
2421 if (view_is_parent_view(view)) {
2422 err = view_close_child(view);
2425 view_set_child(view, diff_view);
2426 *focus_view = diff_view;
2428 *new_view = diff_view;
2431 if (s->selected_entry == NULL)
2433 if (view_is_parent_view(view))
2434 begin_x = view_split_begin_x(view->begin_x);
2435 err = browse_commit_tree(&tree_view, begin_x,
2436 s->selected_entry, s->in_repo_path, s->repo);
2439 if (view_is_parent_view(view)) {
2440 err = view_close_child(view);
2443 view_set_child(view, tree_view);
2444 *focus_view = tree_view;
2446 *new_view = tree_view;
2449 if (got_path_cmp(s->in_repo_path, "/",
2450 strlen(s->in_repo_path), 1) == 0)
2452 err = got_path_dirname(&parent_path, s->in_repo_path);
2455 err = stop_log_thread(s);
2460 lv = view_open(view->nlines, view->ncols,
2461 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2464 return got_error_from_errno("view_open");
2466 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2467 parent_path, s->log_branches);
2471 if (view_is_parent_view(view))
2474 view_set_child(view->parent, lv);
2479 err = stop_log_thread(s);
2482 lv = view_open(view->nlines, view->ncols,
2483 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2485 return got_error_from_errno("view_open");
2486 err = got_repo_match_object_id(&start_id, NULL,
2487 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2488 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2493 in_repo_path = strdup(s->in_repo_path);
2494 if (in_repo_path == NULL) {
2497 return got_error_from_errno("strdup");
2499 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2500 in_repo_path, s->log_branches);
2510 s->log_branches = !s->log_branches;
2511 err = stop_log_thread(s);
2514 lv = view_open(view->nlines, view->ncols,
2515 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2517 return got_error_from_errno("view_open");
2518 err = open_log_view(lv, s->start_id, s->repo,
2519 s->head_ref_name, s->in_repo_path, s->log_branches);
2528 if (view_is_parent_view(view))
2529 begin_x = view_split_begin_x(view->begin_x);
2530 ref_view = view_open(view->nlines, view->ncols,
2531 view->begin_y, begin_x, TOG_VIEW_REF);
2532 if (ref_view == NULL)
2533 return got_error_from_errno("view_open");
2534 err = open_ref_view(ref_view, s->repo);
2536 view_close(ref_view);
2539 if (view_is_parent_view(view)) {
2540 err = view_close_child(view);
2543 view_set_child(view, ref_view);
2544 *focus_view = ref_view;
2546 *new_view = ref_view;
2555 static const struct got_error *
2556 apply_unveil(const char *repo_path, const char *worktree_path)
2558 const struct got_error *error;
2561 if (unveil("gmon.out", "rwc") != 0)
2562 return got_error_from_errno2("unveil", "gmon.out");
2564 if (repo_path && unveil(repo_path, "r") != 0)
2565 return got_error_from_errno2("unveil", repo_path);
2567 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2568 return got_error_from_errno2("unveil", worktree_path);
2570 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2571 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2573 error = got_privsep_unveil_exec_helpers();
2577 if (unveil(NULL, NULL) != 0)
2578 return got_error_from_errno("unveil");
2588 halfdelay(1); /* Do fast refresh while initial view is loading. */
2591 intrflush(stdscr, FALSE);
2592 keypad(stdscr, TRUE);
2594 if (getenv("TOG_COLORS") != NULL) {
2596 use_default_colors();
2598 signal(SIGWINCH, tog_sigwinch);
2599 signal(SIGPIPE, tog_sigpipe);
2600 signal(SIGCONT, tog_sigcont);
2603 static const struct got_error *
2604 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2605 struct got_repository *repo, struct got_worktree *worktree)
2607 const struct got_error *err = NULL;
2610 *in_repo_path = strdup("/");
2611 if (*in_repo_path == NULL)
2612 return got_error_from_errno("strdup");
2617 const char *prefix = got_worktree_get_path_prefix(worktree);
2620 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2623 if (asprintf(in_repo_path, "%s%s%s", prefix,
2624 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2626 err = got_error_from_errno("asprintf");
2627 *in_repo_path = NULL;
2631 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2636 static const struct got_error *
2637 cmd_log(int argc, char *argv[])
2639 const struct got_error *error;
2640 struct got_repository *repo = NULL;
2641 struct got_worktree *worktree = NULL;
2642 struct got_object_id *start_id = NULL;
2643 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2644 char *start_commit = NULL, *head_ref_name = NULL;
2645 int ch, log_branches = 0;
2646 struct tog_view *view;
2649 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2654 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2660 start_commit = optarg;
2663 repo_path = realpath(optarg, NULL);
2664 if (repo_path == NULL)
2665 return got_error_from_errno2("realpath",
2680 cwd = getcwd(NULL, 0);
2682 return got_error_from_errno("getcwd");
2684 error = got_worktree_open(&worktree, cwd);
2685 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2688 if (repo_path == NULL) {
2691 strdup(got_worktree_get_repo_path(worktree));
2693 repo_path = strdup(cwd);
2695 if (repo_path == NULL) {
2696 error = got_error_from_errno("strdup");
2700 error = got_repo_open(&repo, repo_path, NULL);
2704 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2711 error = apply_unveil(got_repo_get_path(repo),
2712 worktree ? got_worktree_get_root_path(worktree) : NULL);
2716 if (start_commit == NULL)
2717 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2718 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2719 GOT_OBJ_TYPE_COMMIT, 1, repo);
2721 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2722 GOT_OBJ_TYPE_COMMIT, 1, repo);
2726 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2728 error = got_error_from_errno("view_open");
2732 head_ref_name = strdup(
2733 got_worktree_get_head_ref_name(worktree));
2734 if (head_ref_name == NULL) {
2735 error = got_error_from_errno("strdup");
2739 error = open_log_view(view, start_id, repo, head_ref_name,
2740 in_repo_path, log_branches);
2744 /* Release work tree lock. */
2745 got_worktree_close(worktree);
2748 error = view_loop(view);
2754 free(head_ref_name);
2756 got_repo_close(repo);
2758 got_worktree_close(worktree);
2766 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2767 "[-w] object1 object2\n", getprogname());
2772 parse_next_line(FILE *f, size_t *len)
2777 const char delim[3] = { '\0', '\0', '\0'};
2779 line = fparseln(f, &linelen, &lineno, delim, 0);
2786 match_line(const char *line, regex_t *regex, size_t nmatch,
2787 regmatch_t *regmatch)
2789 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2793 match_color(struct tog_colors *colors, const char *line)
2795 struct tog_color *tc = NULL;
2797 SIMPLEQ_FOREACH(tc, colors, entry) {
2798 if (match_line(line, &tc->regex, 0, NULL))
2805 static const struct got_error *
2806 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2807 WINDOW *window, regmatch_t *regmatch)
2809 const struct got_error *err = NULL;
2816 s = strndup(line, regmatch->rm_so);
2818 return got_error_from_errno("strndup");
2820 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2825 waddwstr(window, wline);
2832 s = strndup(line + regmatch->rm_so,
2833 regmatch->rm_eo - regmatch->rm_so);
2835 err = got_error_from_errno("strndup");
2839 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2844 wattr_on(window, A_STANDOUT, NULL);
2845 waddwstr(window, wline);
2846 wattr_off(window, A_STANDOUT, NULL);
2853 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2854 err = format_line(&wline, &width,
2855 line + regmatch->rm_eo, wlimit, col_tab_align);
2858 waddwstr(window, wline);
2866 static const struct got_error *
2867 draw_file(struct tog_view *view, const char *header)
2869 struct tog_diff_view_state *s = &view->state.diff;
2870 regmatch_t *regmatch = &view->regmatch;
2871 const struct got_error *err;
2874 struct tog_color *tc;
2878 int max_lines = view->nlines;
2879 int nlines = s->nlines;
2882 line_offset = s->line_offsets[s->first_displayed_line - 1];
2883 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2884 return got_error_from_errno("fseek");
2886 werase(view->window);
2889 if (asprintf(&line, "[%d/%d] %s",
2890 s->first_displayed_line - 1 + s->selected_line, nlines,
2892 return got_error_from_errno("asprintf");
2893 err = format_line(&wline, &width, line, view->ncols, 0);
2898 if (view_needs_focus_indication(view))
2899 wstandout(view->window);
2900 waddwstr(view->window, wline);
2903 if (view_needs_focus_indication(view))
2904 wstandend(view->window);
2905 if (width <= view->ncols - 1)
2906 waddch(view->window, '\n');
2914 while (max_lines > 0 && nprinted < max_lines) {
2915 line = parse_next_line(s->f, &len);
2921 tc = match_color(&s->colors, line);
2923 wattr_on(view->window,
2924 COLOR_PAIR(tc->colorpair), NULL);
2925 if (s->first_displayed_line + nprinted == s->matched_line &&
2926 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2927 err = add_matched_line(&width, line, view->ncols, 0,
2928 view->window, regmatch);
2934 err = format_line(&wline, &width, line, view->ncols, 0);
2939 waddwstr(view->window, wline);
2944 wattr_off(view->window,
2945 COLOR_PAIR(tc->colorpair), NULL);
2946 if (width <= view->ncols - 1)
2947 waddch(view->window, '\n');
2952 s->last_displayed_line = s->first_displayed_line +
2955 s->last_displayed_line = s->first_displayed_line;
2960 while (nprinted < view->nlines) {
2961 waddch(view->window, '\n');
2965 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2970 wstandout(view->window);
2971 waddwstr(view->window, wline);
2974 wstandend(view->window);
2981 get_datestr(time_t *time, char *datebuf)
2983 struct tm mytm, *tm;
2986 tm = gmtime_r(time, &mytm);
2989 s = asctime_r(tm, datebuf);
2992 p = strchr(s, '\n');
2998 static const struct got_error *
2999 get_changed_paths(struct got_pathlist_head *paths,
3000 struct got_commit_object *commit, struct got_repository *repo)
3002 const struct got_error *err = NULL;
3003 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3004 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3005 struct got_object_qid *qid;
3007 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3009 struct got_commit_object *pcommit;
3010 err = got_object_open_as_commit(&pcommit, repo,
3015 tree_id1 = got_object_commit_get_tree_id(pcommit);
3016 got_object_commit_close(pcommit);
3021 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3026 tree_id2 = got_object_commit_get_tree_id(commit);
3027 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3031 err = got_diff_tree(tree1, tree2, "", "", repo,
3032 got_diff_tree_collect_changed_paths, paths, 0);
3035 got_object_tree_close(tree1);
3037 got_object_tree_close(tree2);
3041 static const struct got_error *
3042 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3046 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3048 return got_error_from_errno("reallocarray");
3050 (*line_offsets)[*nlines] = off;
3055 static const struct got_error *
3056 write_commit_info(off_t **line_offsets, size_t *nlines,
3057 struct got_object_id *commit_id, struct got_reflist_head *refs,
3058 struct got_repository *repo, FILE *outfile)
3060 const struct got_error *err = NULL;
3061 char datebuf[26], *datestr;
3062 struct got_commit_object *commit;
3063 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3064 time_t committer_time;
3065 const char *author, *committer;
3066 char *refs_str = NULL;
3067 struct got_pathlist_head changed_paths;
3068 struct got_pathlist_entry *pe;
3072 TAILQ_INIT(&changed_paths);
3075 err = build_refs_str(&refs_str, refs, commit_id, repo);
3080 err = got_object_open_as_commit(&commit, repo, commit_id);
3084 err = got_object_id_str(&id_str, commit_id);
3086 err = got_error_from_errno("got_object_id_str");
3090 err = add_line_offset(line_offsets, nlines, 0);
3094 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3095 refs_str ? refs_str : "", refs_str ? ")" : "");
3097 err = got_error_from_errno("fprintf");
3101 err = add_line_offset(line_offsets, nlines, outoff);
3105 n = fprintf(outfile, "from: %s\n",
3106 got_object_commit_get_author(commit));
3108 err = got_error_from_errno("fprintf");
3112 err = add_line_offset(line_offsets, nlines, outoff);
3116 committer_time = got_object_commit_get_committer_time(commit);
3117 datestr = get_datestr(&committer_time, datebuf);
3119 n = fprintf(outfile, "date: %s UTC\n", datestr);
3121 err = got_error_from_errno("fprintf");
3125 err = add_line_offset(line_offsets, nlines, outoff);
3129 author = got_object_commit_get_author(commit);
3130 committer = got_object_commit_get_committer(commit);
3131 if (strcmp(author, committer) != 0) {
3132 n = fprintf(outfile, "via: %s\n", committer);
3134 err = got_error_from_errno("fprintf");
3138 err = add_line_offset(line_offsets, nlines, outoff);
3142 err = got_object_commit_get_logmsg(&logmsg, commit);
3146 while ((line = strsep(&s, "\n")) != NULL) {
3147 n = fprintf(outfile, "%s\n", line);
3149 err = got_error_from_errno("fprintf");
3153 err = add_line_offset(line_offsets, nlines, outoff);
3158 err = get_changed_paths(&changed_paths, commit, repo);
3161 TAILQ_FOREACH(pe, &changed_paths, entry) {
3162 struct got_diff_changed_path *cp = pe->data;
3163 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3165 err = got_error_from_errno("fprintf");
3169 err = add_line_offset(line_offsets, nlines, outoff);
3172 free((char *)pe->path);
3176 fputc('\n', outfile);
3178 err = add_line_offset(line_offsets, nlines, outoff);
3180 got_pathlist_free(&changed_paths);
3184 got_object_commit_close(commit);
3186 free(*line_offsets);
3187 *line_offsets = NULL;
3193 static const struct got_error *
3194 create_diff(struct tog_diff_view_state *s)
3196 const struct got_error *err = NULL;
3200 free(s->line_offsets);
3201 s->line_offsets = malloc(sizeof(off_t));
3202 if (s->line_offsets == NULL)
3203 return got_error_from_errno("malloc");
3208 err = got_error_from_errno("got_opentemp");
3211 if (s->f && fclose(s->f) != 0) {
3212 err = got_error_from_errno("fclose");
3218 err = got_object_get_type(&obj_type, s->repo, s->id1);
3220 err = got_object_get_type(&obj_type, s->repo, s->id2);
3225 case GOT_OBJ_TYPE_BLOB:
3226 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3227 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3228 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3230 case GOT_OBJ_TYPE_TREE:
3231 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3232 s->id1, s->id2, "", "", s->diff_context,
3233 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3235 case GOT_OBJ_TYPE_COMMIT: {
3236 const struct got_object_id_queue *parent_ids;
3237 struct got_object_qid *pid;
3238 struct got_commit_object *commit2;
3240 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3243 /* Show commit info if we're diffing to a parent/root commit. */
3244 if (s->id1 == NULL) {
3245 err = write_commit_info(&s->line_offsets, &s->nlines,
3246 s->id2, &s->refs, s->repo, s->f);
3250 parent_ids = got_object_commit_get_parent_ids(commit2);
3251 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3252 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3253 err = write_commit_info(
3254 &s->line_offsets, &s->nlines,
3255 s->id2, &s->refs, s->repo, s->f);
3262 got_object_commit_close(commit2);
3264 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3265 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3266 s->force_text_diff, s->repo, s->f);
3270 err = got_error(GOT_ERR_OBJ_TYPE);
3276 if (s->f && fflush(s->f) != 0 && err == NULL)
3277 err = got_error_from_errno("fflush");
3282 diff_view_indicate_progress(struct tog_view *view)
3284 mvwaddstr(view->window, 0, 0, "diffing...");
3289 static const struct got_error *
3290 search_start_diff_view(struct tog_view *view)
3292 struct tog_diff_view_state *s = &view->state.diff;
3294 s->matched_line = 0;
3298 static const struct got_error *
3299 search_next_diff_view(struct tog_view *view)
3301 struct tog_diff_view_state *s = &view->state.diff;
3304 if (!view->searching) {
3305 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3309 if (s->matched_line) {
3310 if (view->searching == TOG_SEARCH_FORWARD)
3311 lineno = s->matched_line + 1;
3313 lineno = s->matched_line - 1;
3315 if (view->searching == TOG_SEARCH_FORWARD)
3326 if (lineno <= 0 || lineno > s->nlines) {
3327 if (s->matched_line == 0) {
3328 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3333 if (view->searching == TOG_SEARCH_FORWARD)
3339 offset = s->line_offsets[lineno - 1];
3340 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3342 return got_error_from_errno("fseeko");
3345 line = parse_next_line(s->f, &len);
3347 match_line(line, &view->regex, 1, &view->regmatch)) {
3348 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3349 s->matched_line = lineno;
3354 if (view->searching == TOG_SEARCH_FORWARD)
3360 if (s->matched_line) {
3361 s->first_displayed_line = s->matched_line;
3362 s->selected_line = 1;
3368 static const struct got_error *
3369 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3370 struct got_object_id *id2, const char *label1, const char *label2,
3371 int diff_context, int ignore_whitespace, int force_text_diff,
3372 struct tog_view *log_view, struct got_repository *repo)
3374 const struct got_error *err;
3375 struct tog_diff_view_state *s = &view->state.diff;
3377 SIMPLEQ_INIT(&s->refs);
3379 if (id1 != NULL && id2 != NULL) {
3381 err = got_object_get_type(&type1, repo, id1);
3384 err = got_object_get_type(&type2, repo, id2);
3389 return got_error(GOT_ERR_OBJ_TYPE);
3391 s->first_displayed_line = 1;
3392 s->last_displayed_line = view->nlines;
3393 s->selected_line = 1;
3401 s->id1 = got_object_id_dup(id1);
3403 return got_error_from_errno("got_object_id_dup");
3407 s->id2 = got_object_id_dup(id2);
3408 if (s->id2 == NULL) {
3411 return got_error_from_errno("got_object_id_dup");
3414 s->first_displayed_line = 1;
3415 s->last_displayed_line = view->nlines;
3416 s->diff_context = diff_context;
3417 s->ignore_whitespace = ignore_whitespace;
3418 s->force_text_diff = force_text_diff;
3419 s->log_view = log_view;
3422 SIMPLEQ_INIT(&s->colors);
3423 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3424 err = add_color(&s->colors,
3425 "^-", TOG_COLOR_DIFF_MINUS,
3426 get_color_value("TOG_COLOR_DIFF_MINUS"));
3429 err = add_color(&s->colors, "^\\+",
3430 TOG_COLOR_DIFF_PLUS,
3431 get_color_value("TOG_COLOR_DIFF_PLUS"));
3433 free_colors(&s->colors);
3436 err = add_color(&s->colors,
3437 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3438 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3440 free_colors(&s->colors);
3444 err = add_color(&s->colors,
3445 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3446 TOG_COLOR_DIFF_META,
3447 get_color_value("TOG_COLOR_DIFF_META"));
3449 free_colors(&s->colors);
3453 err = add_color(&s->colors,
3454 "^(from|via): ", TOG_COLOR_AUTHOR,
3455 get_color_value("TOG_COLOR_AUTHOR"));
3457 free_colors(&s->colors);
3461 err = add_color(&s->colors,
3462 "^date: ", TOG_COLOR_DATE,
3463 get_color_value("TOG_COLOR_DATE"));
3465 free_colors(&s->colors);
3470 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3476 free_colors(&s->colors);
3480 if (log_view && view_is_splitscreen(view))
3481 show_log_view(log_view); /* draw vborder */
3482 diff_view_indicate_progress(view);
3484 s->line_offsets = NULL;
3486 err = create_diff(s);
3492 free_colors(&s->colors);
3493 got_ref_list_free(&s->refs);
3497 view->show = show_diff_view;
3498 view->input = input_diff_view;
3499 view->close = close_diff_view;
3500 view->search_start = search_start_diff_view;
3501 view->search_next = search_next_diff_view;
3506 static const struct got_error *
3507 close_diff_view(struct tog_view *view)
3509 const struct got_error *err = NULL;
3510 struct tog_diff_view_state *s = &view->state.diff;
3516 if (s->f && fclose(s->f) == EOF)
3517 err = got_error_from_errno("fclose");
3518 free_colors(&s->colors);
3519 free(s->line_offsets);
3520 s->line_offsets = NULL;
3522 got_ref_list_free(&s->refs);
3526 static const struct got_error *
3527 show_diff_view(struct tog_view *view)
3529 const struct got_error *err;
3530 struct tog_diff_view_state *s = &view->state.diff;
3531 char *id_str1 = NULL, *id_str2, *header;
3532 const char *label1, *label2;
3535 err = got_object_id_str(&id_str1, s->id1);
3538 label1 = s->label1 ? : id_str1;
3540 label1 = "/dev/null";
3542 err = got_object_id_str(&id_str2, s->id2);
3545 label2 = s->label2 ? : id_str2;
3547 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3548 err = got_error_from_errno("asprintf");
3556 return draw_file(view, header);
3559 static const struct got_error *
3560 set_selected_commit(struct tog_diff_view_state *s,
3561 struct commit_queue_entry *entry)
3563 const struct got_error *err;
3564 const struct got_object_id_queue *parent_ids;
3565 struct got_commit_object *selected_commit;
3566 struct got_object_qid *pid;
3569 s->id2 = got_object_id_dup(entry->id);
3571 return got_error_from_errno("got_object_id_dup");
3573 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3576 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3578 pid = SIMPLEQ_FIRST(parent_ids);
3579 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3580 got_object_commit_close(selected_commit);
3584 static const struct got_error *
3585 input_diff_view(struct tog_view **new_view, struct tog_view **focus_view,
3586 struct tog_view *view, int ch)
3588 const struct got_error *err = NULL;
3589 struct tog_diff_view_state *s = &view->state.diff;
3590 struct tog_log_view_state *ls;
3591 struct commit_queue_entry *entry;
3598 s->force_text_diff = !s->force_text_diff;
3600 s->ignore_whitespace = !s->ignore_whitespace;
3601 wclear(view->window);
3602 s->first_displayed_line = 1;
3603 s->last_displayed_line = view->nlines;
3604 diff_view_indicate_progress(view);
3605 err = create_diff(s);
3609 if (s->first_displayed_line > 1)
3610 s->first_displayed_line--;
3614 if (s->first_displayed_line == 1)
3617 while (i++ < view->nlines - 1 &&
3618 s->first_displayed_line > 1)
3619 s->first_displayed_line--;
3624 s->first_displayed_line++;
3632 while (!s->eof && i++ < view->nlines - 1) {
3634 line = parse_next_line(s->f, NULL);
3635 s->first_displayed_line++;
3641 if (s->diff_context > 0) {
3643 diff_view_indicate_progress(view);
3644 err = create_diff(s);
3645 if (s->first_displayed_line + view->nlines - 1 >
3647 s->first_displayed_line = 1;
3648 s->last_displayed_line = view->nlines;
3653 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3655 diff_view_indicate_progress(view);
3656 err = create_diff(s);
3661 if (s->log_view == NULL)
3663 ls = &s->log_view->state.log;
3664 entry = TAILQ_PREV(ls->selected_entry,
3665 commit_queue_head, entry);
3669 err = input_log_view(NULL, NULL, s->log_view, KEY_UP);
3673 err = set_selected_commit(s, entry);
3677 s->first_displayed_line = 1;
3678 s->last_displayed_line = view->nlines;
3680 diff_view_indicate_progress(view);
3681 err = create_diff(s);
3685 if (s->log_view == NULL)
3687 ls = &s->log_view->state.log;
3689 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3690 ls->thread_args.commits_needed++;
3691 err = trigger_log_thread(s->log_view, 1);
3695 err = input_log_view(NULL, NULL, s->log_view, KEY_DOWN);
3699 entry = TAILQ_NEXT(ls->selected_entry, entry);
3703 err = set_selected_commit(s, entry);
3707 s->first_displayed_line = 1;
3708 s->last_displayed_line = view->nlines;
3710 diff_view_indicate_progress(view);
3711 err = create_diff(s);
3720 static const struct got_error *
3721 cmd_diff(int argc, char *argv[])
3723 const struct got_error *error = NULL;
3724 struct got_repository *repo = NULL;
3725 struct got_worktree *worktree = NULL;
3726 struct got_object_id *id1 = NULL, *id2 = NULL;
3727 char *repo_path = NULL, *cwd = NULL;
3728 char *id_str1 = NULL, *id_str2 = NULL;
3729 char *label1 = NULL, *label2 = NULL;
3730 int diff_context = 3, ignore_whitespace = 0;
3731 int ch, force_text_diff = 0;
3733 struct tog_view *view;
3736 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3740 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3743 force_text_diff = 1;
3746 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3749 err(1, "-C option %s", errstr);
3752 repo_path = realpath(optarg, NULL);
3753 if (repo_path == NULL)
3754 return got_error_from_errno2("realpath",
3756 got_path_strip_trailing_slashes(repo_path);
3759 ignore_whitespace = 1;
3771 usage_diff(); /* TODO show local worktree changes */
3772 } else if (argc == 2) {
3778 cwd = getcwd(NULL, 0);
3780 return got_error_from_errno("getcwd");
3782 error = got_worktree_open(&worktree, cwd);
3783 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3786 if (repo_path == NULL) {
3789 strdup(got_worktree_get_repo_path(worktree));
3791 repo_path = strdup(cwd);
3793 if (repo_path == NULL) {
3794 error = got_error_from_errno("strdup");
3798 error = got_repo_open(&repo, repo_path, NULL);
3804 error = apply_unveil(got_repo_get_path(repo), NULL);
3808 error = got_repo_match_object_id(&id1, &label1, id_str1,
3809 GOT_OBJ_TYPE_ANY, 1, repo);
3813 error = got_repo_match_object_id(&id2, &label2, id_str2,
3814 GOT_OBJ_TYPE_ANY, 1, repo);
3818 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3820 error = got_error_from_errno("view_open");
3823 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3824 ignore_whitespace, force_text_diff, NULL, repo);
3827 error = view_loop(view);
3834 got_repo_close(repo);
3836 got_worktree_close(worktree);
3844 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3849 struct tog_blame_line {
3851 struct got_object_id *id;
3854 static const struct got_error *
3855 draw_blame(struct tog_view *view)
3857 struct tog_blame_view_state *s = &view->state.blame;
3858 struct tog_blame *blame = &s->blame;
3859 regmatch_t *regmatch = &view->regmatch;
3860 const struct got_error *err;
3861 int lineno = 0, nprinted = 0;
3866 struct tog_blame_line *blame_line;
3867 struct got_object_id *prev_id = NULL;
3869 struct tog_color *tc;
3871 err = got_object_id_str(&id_str, s->blamed_commit->id);
3876 werase(view->window);
3878 if (asprintf(&line, "commit %s", id_str) == -1) {
3879 err = got_error_from_errno("asprintf");
3884 err = format_line(&wline, &width, line, view->ncols, 0);
3889 if (view_needs_focus_indication(view))
3890 wstandout(view->window);
3891 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3893 wattr_on(view->window,
3894 COLOR_PAIR(tc->colorpair), NULL);
3895 waddwstr(view->window, wline);
3897 wattr_off(view->window,
3898 COLOR_PAIR(tc->colorpair), NULL);
3899 if (view_needs_focus_indication(view))
3900 wstandend(view->window);
3903 if (width < view->ncols - 1)
3904 waddch(view->window, '\n');
3906 if (asprintf(&line, "[%d/%d] %s%s",
3907 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3908 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3910 return got_error_from_errno("asprintf");
3913 err = format_line(&wline, &width, line, view->ncols, 0);
3918 waddwstr(view->window, wline);
3921 if (width < view->ncols - 1)
3922 waddch(view->window, '\n');
3925 while (nprinted < view->nlines - 2) {
3926 line = parse_next_line(blame->f, &len);
3931 if (++lineno < s->first_displayed_line) {
3936 if (view->focussed && nprinted == s->selected_line - 1)
3937 wstandout(view->window);
3939 if (blame->nlines > 0) {
3940 blame_line = &blame->lines[lineno - 1];
3941 if (blame_line->annotated && prev_id &&
3942 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3944 nprinted == s->selected_line - 1)) {
3945 waddstr(view->window, " ");
3946 } else if (blame_line->annotated) {
3948 err = got_object_id_str(&id_str, blame_line->id);
3953 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3955 wattr_on(view->window,
3956 COLOR_PAIR(tc->colorpair), NULL);
3957 wprintw(view->window, "%.8s", id_str);
3959 wattr_off(view->window,
3960 COLOR_PAIR(tc->colorpair), NULL);
3962 prev_id = blame_line->id;
3964 waddstr(view->window, "........");
3968 waddstr(view->window, "........");
3972 if (view->focussed && nprinted == s->selected_line - 1)
3973 wstandend(view->window);
3974 waddstr(view->window, " ");
3976 if (view->ncols <= 9) {
3978 wline = wcsdup(L"");
3979 if (wline == NULL) {
3980 err = got_error_from_errno("wcsdup");
3984 } else if (s->first_displayed_line + nprinted ==
3986 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3987 err = add_matched_line(&width, line, view->ncols - 9, 9,
3988 view->window, regmatch);
3995 err = format_line(&wline, &width, line,
3996 view->ncols - 9, 9);
3997 waddwstr(view->window, wline);
4003 if (width <= view->ncols - 1)
4004 waddch(view->window, '\n');
4005 if (++nprinted == 1)
4006 s->first_displayed_line = lineno;
4009 s->last_displayed_line = lineno;
4016 static const struct got_error *
4017 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4019 const struct got_error *err = NULL;
4020 struct tog_blame_cb_args *a = arg;
4021 struct tog_blame_line *line;
4024 if (nlines != a->nlines ||
4025 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4026 return got_error(GOT_ERR_RANGE);
4028 errcode = pthread_mutex_lock(&tog_mutex);
4030 return got_error_set_errno(errcode, "pthread_mutex_lock");
4032 if (*a->quit) { /* user has quit the blame view */
4033 err = got_error(GOT_ERR_ITER_COMPLETED);
4038 goto done; /* no change in this commit */
4040 line = &a->lines[lineno - 1];
4041 if (line->annotated)
4044 line->id = got_object_id_dup(id);
4045 if (line->id == NULL) {
4046 err = got_error_from_errno("got_object_id_dup");
4049 line->annotated = 1;
4051 errcode = pthread_mutex_unlock(&tog_mutex);
4053 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4058 blame_thread(void *arg)
4060 const struct got_error *err;
4061 struct tog_blame_thread_args *ta = arg;
4062 struct tog_blame_cb_args *a = ta->cb_args;
4065 err = block_signals_used_by_main_thread();
4069 err = got_blame(ta->path, a->commit_id, ta->repo,
4070 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4071 if (err && err->code == GOT_ERR_CANCELLED)
4074 errcode = pthread_mutex_lock(&tog_mutex);
4076 return (void *)got_error_set_errno(errcode,
4077 "pthread_mutex_lock");
4079 got_repo_close(ta->repo);
4083 errcode = pthread_mutex_unlock(&tog_mutex);
4084 if (errcode && err == NULL)
4085 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4090 static struct got_object_id *
4091 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4092 int first_displayed_line, int selected_line)
4094 struct tog_blame_line *line;
4099 line = &lines[first_displayed_line - 1 + selected_line - 1];
4100 if (!line->annotated)
4106 static const struct got_error *
4107 stop_blame(struct tog_blame *blame)
4109 const struct got_error *err = NULL;
4112 if (blame->thread) {
4114 errcode = pthread_mutex_unlock(&tog_mutex);
4116 return got_error_set_errno(errcode,
4117 "pthread_mutex_unlock");
4118 errcode = pthread_join(blame->thread, (void **)&err);
4120 return got_error_set_errno(errcode, "pthread_join");
4121 errcode = pthread_mutex_lock(&tog_mutex);
4123 return got_error_set_errno(errcode,
4124 "pthread_mutex_lock");
4125 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4127 blame->thread = NULL;
4129 if (blame->thread_args.repo) {
4130 got_repo_close(blame->thread_args.repo);
4131 blame->thread_args.repo = NULL;
4134 if (fclose(blame->f) != 0 && err == NULL)
4135 err = got_error_from_errno("fclose");
4139 for (i = 0; i < blame->nlines; i++)
4140 free(blame->lines[i].id);
4142 blame->lines = NULL;
4144 free(blame->cb_args.commit_id);
4145 blame->cb_args.commit_id = NULL;
4150 static const struct got_error *
4151 cancel_blame_view(void *arg)
4153 const struct got_error *err = NULL;
4157 errcode = pthread_mutex_lock(&tog_mutex);
4159 return got_error_set_errno(errcode,
4160 "pthread_mutex_unlock");
4163 err = got_error(GOT_ERR_CANCELLED);
4165 errcode = pthread_mutex_unlock(&tog_mutex);
4167 return got_error_set_errno(errcode,
4168 "pthread_mutex_lock");
4173 static const struct got_error *
4174 run_blame(struct tog_view *view)
4176 struct tog_blame_view_state *s = &view->state.blame;
4177 struct tog_blame *blame = &s->blame;
4178 const struct got_error *err = NULL;
4179 struct got_blob_object *blob = NULL;
4180 struct got_repository *thread_repo = NULL;
4181 struct got_object_id *obj_id = NULL;
4184 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4189 err = got_object_get_type(&obj_type, s->repo, obj_id);
4193 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4194 err = got_error(GOT_ERR_OBJ_TYPE);
4198 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4201 blame->f = got_opentemp();
4202 if (blame->f == NULL) {
4203 err = got_error_from_errno("got_opentemp");
4206 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4207 &blame->line_offsets, blame->f, blob);
4208 if (err || blame->nlines == 0)
4211 /* Don't include \n at EOF in the blame line count. */
4212 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4215 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4216 if (blame->lines == NULL) {
4217 err = got_error_from_errno("calloc");
4221 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4225 blame->cb_args.view = view;
4226 blame->cb_args.lines = blame->lines;
4227 blame->cb_args.nlines = blame->nlines;
4228 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4229 if (blame->cb_args.commit_id == NULL) {
4230 err = got_error_from_errno("got_object_id_dup");
4233 blame->cb_args.quit = &s->done;
4235 blame->thread_args.path = s->path;
4236 blame->thread_args.repo = thread_repo;
4237 blame->thread_args.cb_args = &blame->cb_args;
4238 blame->thread_args.complete = &s->blame_complete;
4239 blame->thread_args.cancel_cb = cancel_blame_view;
4240 blame->thread_args.cancel_arg = &s->done;
4241 s->blame_complete = 0;
4245 got_object_blob_close(blob);
4252 static const struct got_error *
4253 open_blame_view(struct tog_view *view, char *path,
4254 struct got_object_id *commit_id, struct got_repository *repo)
4256 const struct got_error *err = NULL;
4257 struct tog_blame_view_state *s = &view->state.blame;
4259 SIMPLEQ_INIT(&s->blamed_commits);
4261 s->path = strdup(path);
4262 if (s->path == NULL)
4263 return got_error_from_errno("strdup");
4265 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4271 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4272 s->first_displayed_line = 1;
4273 s->last_displayed_line = view->nlines;
4274 s->selected_line = 1;
4275 s->blame_complete = 0;
4277 s->commit_id = commit_id;
4278 memset(&s->blame, 0, sizeof(s->blame));
4280 SIMPLEQ_INIT(&s->colors);
4281 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4282 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4283 get_color_value("TOG_COLOR_COMMIT"));
4288 view->show = show_blame_view;
4289 view->input = input_blame_view;
4290 view->close = close_blame_view;
4291 view->search_start = search_start_blame_view;
4292 view->search_next = search_next_blame_view;
4294 return run_blame(view);
4297 static const struct got_error *
4298 close_blame_view(struct tog_view *view)
4300 const struct got_error *err = NULL;
4301 struct tog_blame_view_state *s = &view->state.blame;
4303 if (s->blame.thread)
4304 err = stop_blame(&s->blame);
4306 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4307 struct got_object_qid *blamed_commit;
4308 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4309 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4310 got_object_qid_free(blamed_commit);
4314 free_colors(&s->colors);
4319 static const struct got_error *
4320 search_start_blame_view(struct tog_view *view)
4322 struct tog_blame_view_state *s = &view->state.blame;
4324 s->matched_line = 0;
4328 static const struct got_error *
4329 search_next_blame_view(struct tog_view *view)
4331 struct tog_blame_view_state *s = &view->state.blame;
4334 if (!view->searching) {
4335 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4339 if (s->matched_line) {
4340 if (view->searching == TOG_SEARCH_FORWARD)
4341 lineno = s->matched_line + 1;
4343 lineno = s->matched_line - 1;
4345 if (view->searching == TOG_SEARCH_FORWARD)
4348 lineno = s->blame.nlines;
4356 if (lineno <= 0 || lineno > s->blame.nlines) {
4357 if (s->matched_line == 0) {
4358 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4363 if (view->searching == TOG_SEARCH_FORWARD)
4366 lineno = s->blame.nlines;
4369 offset = s->blame.line_offsets[lineno - 1];
4370 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4372 return got_error_from_errno("fseeko");
4375 line = parse_next_line(s->blame.f, &len);
4377 match_line(line, &view->regex, 1, &view->regmatch)) {
4378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4379 s->matched_line = lineno;
4384 if (view->searching == TOG_SEARCH_FORWARD)
4390 if (s->matched_line) {
4391 s->first_displayed_line = s->matched_line;
4392 s->selected_line = 1;
4398 static const struct got_error *
4399 show_blame_view(struct tog_view *view)
4401 const struct got_error *err = NULL;
4402 struct tog_blame_view_state *s = &view->state.blame;
4405 if (s->blame.thread == NULL) {
4406 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4407 &s->blame.thread_args);
4409 return got_error_set_errno(errcode, "pthread_create");
4411 halfdelay(1); /* fast refresh while annotating */
4414 if (s->blame_complete)
4415 halfdelay(10); /* disable fast refresh */
4417 err = draw_blame(view);
4423 static const struct got_error *
4424 input_blame_view(struct tog_view **new_view, struct tog_view **focus_view,
4425 struct tog_view *view, int ch)
4427 const struct got_error *err = NULL, *thread_err = NULL;
4428 struct tog_view *diff_view;
4429 struct tog_blame_view_state *s = &view->state.blame;
4438 if (s->selected_line > 1)
4440 else if (s->selected_line == 1 &&
4441 s->first_displayed_line > 1)
4442 s->first_displayed_line--;
4446 if (s->first_displayed_line == 1) {
4447 s->selected_line = 1;
4450 if (s->first_displayed_line > view->nlines - 2)
4451 s->first_displayed_line -=
4454 s->first_displayed_line = 1;
4458 if (s->selected_line < view->nlines - 2 &&
4459 s->first_displayed_line +
4460 s->selected_line <= s->blame.nlines)
4462 else if (s->last_displayed_line <
4464 s->first_displayed_line++;
4468 struct got_object_id *id = NULL;
4469 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4470 s->first_displayed_line, s->selected_line);
4474 struct got_commit_object *commit;
4475 struct got_object_qid *pid;
4476 struct got_object_id *blob_id = NULL;
4478 err = got_object_open_as_commit(&commit,
4482 pid = SIMPLEQ_FIRST(
4483 got_object_commit_get_parent_ids(commit));
4485 got_object_commit_close(commit);
4488 /* Check if path history ends here. */
4489 err = got_object_id_by_path(&blob_id, s->repo,
4492 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4494 got_object_commit_close(commit);
4497 err = got_object_get_type(&obj_type, s->repo,
4500 /* Can't blame non-blob type objects. */
4501 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4502 got_object_commit_close(commit);
4505 err = got_object_qid_alloc(&s->blamed_commit,
4507 got_object_commit_close(commit);
4509 if (got_object_id_cmp(id,
4510 s->blamed_commit->id) == 0)
4512 err = got_object_qid_alloc(&s->blamed_commit,
4518 thread_err = stop_blame(&s->blame);
4522 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4523 s->blamed_commit, entry);
4524 err = run_blame(view);
4530 struct got_object_qid *first;
4531 first = SIMPLEQ_FIRST(&s->blamed_commits);
4532 if (!got_object_id_cmp(first->id, s->commit_id))
4535 thread_err = stop_blame(&s->blame);
4539 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4540 got_object_qid_free(s->blamed_commit);
4542 SIMPLEQ_FIRST(&s->blamed_commits);
4543 err = run_blame(view);
4550 struct got_object_id *id = NULL;
4551 struct got_object_qid *pid;
4552 struct got_commit_object *commit = NULL;
4553 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4554 s->first_displayed_line, s->selected_line);
4557 err = got_object_open_as_commit(&commit, s->repo, id);
4560 pid = SIMPLEQ_FIRST(
4561 got_object_commit_get_parent_ids(commit));
4562 if (view_is_parent_view(view))
4563 begin_x = view_split_begin_x(view->begin_x);
4564 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4565 if (diff_view == NULL) {
4566 got_object_commit_close(commit);
4567 err = got_error_from_errno("view_open");
4570 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4571 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4572 got_object_commit_close(commit);
4574 view_close(diff_view);
4577 if (view_is_parent_view(view)) {
4578 err = view_close_child(view);
4581 view_set_child(view, diff_view);
4582 *focus_view = diff_view;
4584 *new_view = diff_view;
4592 if (s->last_displayed_line >= s->blame.nlines &&
4593 s->selected_line >= MIN(s->blame.nlines,
4594 view->nlines - 2)) {
4597 if (s->last_displayed_line >= s->blame.nlines &&
4598 s->selected_line < view->nlines - 2) {
4599 s->selected_line = MIN(s->blame.nlines,
4603 if (s->last_displayed_line + view->nlines - 2
4605 s->first_displayed_line +=
4608 s->first_displayed_line =
4613 if (s->selected_line > view->nlines - 2) {
4614 s->selected_line = MIN(s->blame.nlines,
4621 return thread_err ? thread_err : err;
4624 static const struct got_error *
4625 cmd_blame(int argc, char *argv[])
4627 const struct got_error *error;
4628 struct got_repository *repo = NULL;
4629 struct got_worktree *worktree = NULL;
4630 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4631 char *link_target = NULL;
4632 struct got_object_id *commit_id = NULL;
4633 char *commit_id_str = NULL;
4635 struct tog_view *view;
4638 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4643 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4646 commit_id_str = optarg;
4649 repo_path = realpath(optarg, NULL);
4650 if (repo_path == NULL)
4651 return got_error_from_errno2("realpath",
4666 cwd = getcwd(NULL, 0);
4668 return got_error_from_errno("getcwd");
4670 error = got_worktree_open(&worktree, cwd);
4671 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4674 if (repo_path == NULL) {
4677 strdup(got_worktree_get_repo_path(worktree));
4679 repo_path = strdup(cwd);
4681 if (repo_path == NULL) {
4682 error = got_error_from_errno("strdup");
4686 error = got_repo_open(&repo, repo_path, NULL);
4690 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4697 error = apply_unveil(got_repo_get_path(repo), NULL);
4701 if (commit_id_str == NULL) {
4702 struct got_reference *head_ref;
4703 error = got_ref_open(&head_ref, repo, worktree ?
4704 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4707 error = got_ref_resolve(&commit_id, repo, head_ref);
4708 got_ref_close(head_ref);
4710 error = got_repo_match_object_id(&commit_id, NULL,
4711 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4716 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4718 error = got_error_from_errno("view_open");
4722 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4727 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4732 /* Release work tree lock. */
4733 got_worktree_close(worktree);
4736 error = view_loop(view);
4744 got_worktree_close(worktree);
4746 got_repo_close(repo);
4750 static const struct got_error *
4751 draw_tree_entries(struct tog_view *view, const char *parent_path)
4753 struct tog_tree_view_state *s = &view->state.tree;
4754 const struct got_error *err = NULL;
4755 struct got_tree_entry *te;
4757 struct tog_color *tc;
4758 int width, n, i, nentries;
4759 int limit = view->nlines;
4763 werase(view->window);
4768 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4771 if (view_needs_focus_indication(view))
4772 wstandout(view->window);
4773 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4775 wattr_on(view->window,
4776 COLOR_PAIR(tc->colorpair), NULL);
4777 waddwstr(view->window, wline);
4779 wattr_off(view->window,
4780 COLOR_PAIR(tc->colorpair), NULL);
4781 if (view_needs_focus_indication(view))
4782 wstandend(view->window);
4785 if (width < view->ncols - 1)
4786 waddch(view->window, '\n');
4789 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4792 waddwstr(view->window, wline);
4795 if (width < view->ncols - 1)
4796 waddch(view->window, '\n');
4799 waddch(view->window, '\n');
4803 if (s->first_displayed_entry == NULL) {
4804 te = got_object_tree_get_first_entry(s->tree);
4805 if (s->selected == 0) {
4807 wstandout(view->window);
4808 s->selected_entry = NULL;
4810 waddstr(view->window, " ..\n"); /* parent directory */
4811 if (s->selected == 0 && view->focussed)
4812 wstandend(view->window);
4819 te = s->first_displayed_entry;
4822 nentries = got_object_tree_get_nentries(s->tree);
4823 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4824 char *line = NULL, *id_str = NULL, *link_target = NULL;
4825 const char *modestr = "";
4828 te = got_object_tree_get_entry(s->tree, i);
4829 mode = got_tree_entry_get_mode(te);
4832 err = got_object_id_str(&id_str,
4833 got_tree_entry_get_id(te));
4835 return got_error_from_errno(
4836 "got_object_id_str");
4838 if (got_object_tree_entry_is_submodule(te))
4840 else if (S_ISLNK(mode)) {
4843 err = got_tree_entry_get_symlink_target(&link_target,
4849 for (i = 0; i < strlen(link_target); i++) {
4850 if (!isprint((unsigned char)link_target[i]))
4851 link_target[i] = '?';
4855 else if (S_ISDIR(mode))
4857 else if (mode & S_IXUSR)
4859 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4860 got_tree_entry_get_name(te), modestr,
4861 link_target ? " -> ": "",
4862 link_target ? link_target : "") == -1) {
4865 return got_error_from_errno("asprintf");
4869 err = format_line(&wline, &width, line, view->ncols, 0);
4874 if (n == s->selected) {
4876 wstandout(view->window);
4877 s->selected_entry = te;
4879 tc = match_color(&s->colors, line);
4881 wattr_on(view->window,
4882 COLOR_PAIR(tc->colorpair), NULL);
4883 waddwstr(view->window, wline);
4885 wattr_off(view->window,
4886 COLOR_PAIR(tc->colorpair), NULL);
4887 if (width < view->ncols - 1)
4888 waddch(view->window, '\n');
4889 if (n == s->selected && view->focussed)
4890 wstandend(view->window);
4896 s->last_displayed_entry = te;
4905 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4907 struct got_tree_entry *te;
4908 int isroot = s->tree == s->root;
4911 if (s->first_displayed_entry == NULL)
4914 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4915 while (i++ < maxscroll) {
4918 s->first_displayed_entry = NULL;
4921 s->first_displayed_entry = te;
4922 te = got_tree_entry_get_prev(s->tree, te);
4927 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4929 struct got_tree_entry *next, *last;
4932 if (s->first_displayed_entry)
4933 next = got_tree_entry_get_next(s->tree,
4934 s->first_displayed_entry);
4936 next = got_object_tree_get_first_entry(s->tree);
4938 last = s->last_displayed_entry;
4939 while (next && last && n++ < maxscroll) {
4940 last = got_tree_entry_get_next(s->tree, last);
4942 s->first_displayed_entry = next;
4943 next = got_tree_entry_get_next(s->tree, next);
4948 static const struct got_error *
4949 tree_entry_path(char **path, struct tog_parent_trees *parents,
4950 struct got_tree_entry *te)
4952 const struct got_error *err = NULL;
4953 struct tog_parent_tree *pt;
4954 size_t len = 2; /* for leading slash and NUL */
4956 TAILQ_FOREACH(pt, parents, entry)
4957 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4960 len += strlen(got_tree_entry_get_name(te));
4962 *path = calloc(1, len);
4964 return got_error_from_errno("calloc");
4967 pt = TAILQ_LAST(parents, tog_parent_trees);
4969 const char *name = got_tree_entry_get_name(pt->selected_entry);
4970 if (strlcat(*path, name, len) >= len) {
4971 err = got_error(GOT_ERR_NO_SPACE);
4974 if (strlcat(*path, "/", len) >= len) {
4975 err = got_error(GOT_ERR_NO_SPACE);
4978 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4981 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4982 err = got_error(GOT_ERR_NO_SPACE);
4994 static const struct got_error *
4995 blame_tree_entry(struct tog_view **new_view, int begin_x,
4996 struct got_tree_entry *te, struct tog_parent_trees *parents,
4997 struct got_object_id *commit_id, struct got_repository *repo)
4999 const struct got_error *err = NULL;
5001 struct tog_view *blame_view;
5005 err = tree_entry_path(&path, parents, te);
5009 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5010 if (blame_view == NULL) {
5011 err = got_error_from_errno("view_open");
5015 err = open_blame_view(blame_view, path, commit_id, repo);
5017 if (err->code == GOT_ERR_CANCELLED)
5019 view_close(blame_view);
5021 *new_view = blame_view;
5027 static const struct got_error *
5028 log_tree_entry(struct tog_view **new_view, int begin_x,
5029 struct got_tree_entry *te, struct tog_parent_trees *parents,
5030 struct got_object_id *commit_id, struct got_repository *repo)
5032 struct tog_view *log_view;
5033 const struct got_error *err = NULL;
5038 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5039 if (log_view == NULL)
5040 return got_error_from_errno("view_open");
5042 err = tree_entry_path(&path, parents, te);
5046 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5048 view_close(log_view);
5050 *new_view = log_view;
5055 static const struct got_error *
5056 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5057 struct got_object_id *commit_id, struct got_repository *repo)
5059 const struct got_error *err = NULL;
5060 char *commit_id_str = NULL;
5061 struct tog_tree_view_state *s = &view->state.tree;
5063 TAILQ_INIT(&s->parents);
5065 err = got_object_id_str(&commit_id_str, commit_id);
5069 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5070 err = got_error_from_errno("asprintf");
5074 s->root = s->tree = root;
5075 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5076 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5077 s->commit_id = got_object_id_dup(commit_id);
5078 if (s->commit_id == NULL) {
5079 err = got_error_from_errno("got_object_id_dup");
5084 SIMPLEQ_INIT(&s->colors);
5086 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5087 err = add_color(&s->colors, "\\$$",
5088 TOG_COLOR_TREE_SUBMODULE,
5089 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5092 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5093 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5095 free_colors(&s->colors);
5098 err = add_color(&s->colors, "/$",
5099 TOG_COLOR_TREE_DIRECTORY,
5100 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5102 free_colors(&s->colors);
5106 err = add_color(&s->colors, "\\*$",
5107 TOG_COLOR_TREE_EXECUTABLE,
5108 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5110 free_colors(&s->colors);
5114 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5115 get_color_value("TOG_COLOR_COMMIT"));
5117 free_colors(&s->colors);
5122 view->show = show_tree_view;
5123 view->input = input_tree_view;
5124 view->close = close_tree_view;
5125 view->search_start = search_start_tree_view;
5126 view->search_next = search_next_tree_view;
5128 free(commit_id_str);
5130 free(s->tree_label);
5131 s->tree_label = NULL;
5136 static const struct got_error *
5137 close_tree_view(struct tog_view *view)
5139 struct tog_tree_view_state *s = &view->state.tree;
5141 free_colors(&s->colors);
5142 free(s->tree_label);
5143 s->tree_label = NULL;
5145 s->commit_id = NULL;
5146 while (!TAILQ_EMPTY(&s->parents)) {
5147 struct tog_parent_tree *parent;
5148 parent = TAILQ_FIRST(&s->parents);
5149 TAILQ_REMOVE(&s->parents, parent, entry);
5153 if (s->tree != s->root)
5154 got_object_tree_close(s->tree);
5155 got_object_tree_close(s->root);
5159 static const struct got_error *
5160 search_start_tree_view(struct tog_view *view)
5162 struct tog_tree_view_state *s = &view->state.tree;
5164 s->matched_entry = NULL;
5169 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5171 regmatch_t regmatch;
5173 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5177 static const struct got_error *
5178 search_next_tree_view(struct tog_view *view)
5180 struct tog_tree_view_state *s = &view->state.tree;
5181 struct got_tree_entry *te = NULL;
5183 if (!view->searching) {
5184 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5188 if (s->matched_entry) {
5189 if (view->searching == TOG_SEARCH_FORWARD) {
5190 if (s->selected_entry)
5191 te = got_tree_entry_get_next(s->tree,
5194 te = got_object_tree_get_first_entry(s->tree);
5196 if (s->selected_entry == NULL)
5197 te = got_object_tree_get_last_entry(s->tree);
5199 te = got_tree_entry_get_prev(s->tree,
5203 if (view->searching == TOG_SEARCH_FORWARD)
5204 te = got_object_tree_get_first_entry(s->tree);
5206 te = got_object_tree_get_last_entry(s->tree);
5211 if (s->matched_entry == NULL) {
5212 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5215 if (view->searching == TOG_SEARCH_FORWARD)
5216 te = got_object_tree_get_first_entry(s->tree);
5218 te = got_object_tree_get_last_entry(s->tree);
5221 if (match_tree_entry(te, &view->regex)) {
5222 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5223 s->matched_entry = te;
5227 if (view->searching == TOG_SEARCH_FORWARD)
5228 te = got_tree_entry_get_next(s->tree, te);
5230 te = got_tree_entry_get_prev(s->tree, te);
5233 if (s->matched_entry) {
5234 s->first_displayed_entry = s->matched_entry;
5241 static const struct got_error *
5242 show_tree_view(struct tog_view *view)
5244 const struct got_error *err = NULL;
5245 struct tog_tree_view_state *s = &view->state.tree;
5248 err = tree_entry_path(&parent_path, &s->parents, NULL);
5252 err = draw_tree_entries(view, parent_path);
5259 static const struct got_error *
5260 input_tree_view(struct tog_view **new_view, struct tog_view **focus_view,
5261 struct tog_view *view, int ch)
5263 const struct got_error *err = NULL;
5264 struct tog_tree_view_state *s = &view->state.tree;
5265 struct tog_view *log_view, *ref_view;
5270 s->show_ids = !s->show_ids;
5273 if (!s->selected_entry)
5275 if (view_is_parent_view(view))
5276 begin_x = view_split_begin_x(view->begin_x);
5277 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5278 &s->parents, s->commit_id, s->repo);
5279 if (view_is_parent_view(view)) {
5280 err = view_close_child(view);
5283 view_set_child(view, log_view);
5284 *focus_view = log_view;
5286 *new_view = log_view;
5289 if (view_is_parent_view(view))
5290 begin_x = view_split_begin_x(view->begin_x);
5291 ref_view = view_open(view->nlines, view->ncols,
5292 view->begin_y, begin_x, TOG_VIEW_REF);
5293 if (ref_view == NULL)
5294 return got_error_from_errno("view_open");
5295 err = open_ref_view(ref_view, s->repo);
5297 view_close(ref_view);
5300 if (view_is_parent_view(view)) {
5301 err = view_close_child(view);
5304 view_set_child(view, ref_view);
5305 *focus_view = ref_view;
5307 *new_view = ref_view;
5311 if (s->selected > 0) {
5315 tree_scroll_up(s, 1);
5319 if (s->tree == s->root) {
5320 if (got_object_tree_get_first_entry(s->tree) ==
5321 s->first_displayed_entry)
5324 if (s->first_displayed_entry == NULL)
5327 tree_scroll_up(s, MAX(0, view->nlines - 3));
5331 if (s->selected < s->ndisplayed - 1) {
5335 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5337 /* can't scroll any further */
5339 tree_scroll_down(s, 1);
5343 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5345 /* can't scroll any further; move cursor down */
5346 if (s->selected < s->ndisplayed - 1)
5347 s->selected = s->ndisplayed - 1;
5350 tree_scroll_down(s, view->nlines - 3);
5355 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5356 struct tog_parent_tree *parent;
5357 /* user selected '..' */
5358 if (s->tree == s->root)
5360 parent = TAILQ_FIRST(&s->parents);
5361 TAILQ_REMOVE(&s->parents, parent,
5363 got_object_tree_close(s->tree);
5364 s->tree = parent->tree;
5365 s->first_displayed_entry =
5366 parent->first_displayed_entry;
5368 parent->selected_entry;
5369 s->selected = parent->selected;
5371 } else if (S_ISDIR(got_tree_entry_get_mode(
5372 s->selected_entry))) {
5373 struct got_tree_object *subtree;
5374 err = got_object_open_as_tree(&subtree, s->repo,
5375 got_tree_entry_get_id(s->selected_entry));
5378 err = tree_view_visit_subtree(s, subtree);
5380 got_object_tree_close(subtree);
5383 } else if (S_ISREG(got_tree_entry_get_mode(
5384 s->selected_entry))) {
5385 struct tog_view *blame_view;
5386 int begin_x = view_is_parent_view(view) ?
5387 view_split_begin_x(view->begin_x) : 0;
5389 err = blame_tree_entry(&blame_view, begin_x,
5390 s->selected_entry, &s->parents,
5391 s->commit_id, s->repo);
5394 if (view_is_parent_view(view)) {
5395 err = view_close_child(view);
5398 view_set_child(view, blame_view);
5399 *focus_view = blame_view;
5401 *new_view = blame_view;
5405 if (s->selected > view->nlines)
5406 s->selected = s->ndisplayed - 1;
5419 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5424 static const struct got_error *
5425 cmd_tree(int argc, char *argv[])
5427 const struct got_error *error;
5428 struct got_repository *repo = NULL;
5429 struct got_worktree *worktree = NULL;
5430 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5431 struct got_object_id *commit_id = NULL;
5432 char *commit_id_arg = NULL;
5433 struct got_commit_object *commit = NULL;
5434 struct got_tree_object *tree = NULL;
5436 struct tog_view *view;
5439 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5444 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5447 commit_id_arg = optarg;
5450 repo_path = realpath(optarg, NULL);
5451 if (repo_path == NULL)
5452 return got_error_from_errno2("realpath",
5467 cwd = getcwd(NULL, 0);
5469 return got_error_from_errno("getcwd");
5471 error = got_worktree_open(&worktree, cwd);
5472 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5475 if (repo_path == NULL) {
5478 strdup(got_worktree_get_repo_path(worktree));
5480 repo_path = strdup(cwd);
5482 if (repo_path == NULL) {
5483 error = got_error_from_errno("strdup");
5487 error = got_repo_open(&repo, repo_path, NULL);
5491 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5498 error = apply_unveil(got_repo_get_path(repo), NULL);
5502 error = got_repo_match_object_id(&commit_id, NULL,
5503 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5504 GOT_OBJ_TYPE_COMMIT, 1, repo);
5508 error = got_object_open_as_commit(&commit, repo, commit_id);
5512 error = got_object_open_as_tree(&tree, repo,
5513 got_object_commit_get_tree_id(commit));
5517 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5519 error = got_error_from_errno("view_open");
5522 error = open_tree_view(view, tree, commit_id, repo);
5525 if (!got_path_is_root_dir(in_repo_path)) {
5526 error = tree_view_walk_path(&view->state.tree, commit_id,
5533 /* Release work tree lock. */
5534 got_worktree_close(worktree);
5537 error = view_loop(view);
5543 got_object_commit_close(commit);
5545 got_object_tree_close(tree);
5547 got_repo_close(repo);
5551 static const struct got_error *
5552 ref_view_load_refs(struct tog_ref_view_state *s)
5554 const struct got_error *err;
5555 struct got_reflist_entry *sre;
5556 struct tog_reflist_entry *re;
5558 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5559 got_ref_cmp_by_name, NULL);
5564 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5565 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5568 re = malloc(sizeof(*re));
5570 return got_error_from_errno("malloc");
5573 re->idx = s->nrefs++;
5574 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5581 ref_view_free_refs(struct tog_ref_view_state *s)
5583 struct tog_reflist_entry *re;
5585 while (!TAILQ_EMPTY(&s->refs)) {
5586 re = TAILQ_FIRST(&s->refs);
5587 TAILQ_REMOVE(&s->refs, re, entry);
5590 got_ref_list_free(&s->simplerefs);
5593 static const struct got_error *
5594 open_ref_view(struct tog_view *view, struct got_repository *repo)
5596 const struct got_error *err = NULL;
5597 struct tog_ref_view_state *s = &view->state.ref;
5599 s->selected_entry = 0;
5602 SIMPLEQ_INIT(&s->simplerefs);
5603 TAILQ_INIT(&s->refs);
5604 SIMPLEQ_INIT(&s->colors);
5606 err = ref_view_load_refs(s);
5610 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5612 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5613 err = add_color(&s->colors, "^refs/heads/",
5614 TOG_COLOR_REFS_HEADS,
5615 get_color_value("TOG_COLOR_REFS_HEADS"));
5619 err = add_color(&s->colors, "^refs/tags/",
5620 TOG_COLOR_REFS_TAGS,
5621 get_color_value("TOG_COLOR_REFS_TAGS"));
5625 err = add_color(&s->colors, "^refs/remotes/",
5626 TOG_COLOR_REFS_REMOTES,
5627 get_color_value("TOG_COLOR_REFS_REMOTES"));
5632 view->show = show_ref_view;
5633 view->input = input_ref_view;
5634 view->close = close_ref_view;
5635 view->search_start = search_start_ref_view;
5636 view->search_next = search_next_ref_view;
5639 free_colors(&s->colors);
5643 static const struct got_error *
5644 close_ref_view(struct tog_view *view)
5646 struct tog_ref_view_state *s = &view->state.ref;
5648 ref_view_free_refs(s);
5649 free_colors(&s->colors);
5654 static const struct got_error *
5655 resolve_reflist_entry(struct got_object_id **commit_id,
5656 struct tog_reflist_entry *re, struct got_repository *repo)
5658 const struct got_error *err = NULL;
5659 struct got_object_id *obj_id;
5660 struct got_tag_object *tag = NULL;
5665 err = got_ref_resolve(&obj_id, repo, re->ref);
5669 err = got_object_get_type(&obj_type, repo, obj_id);
5674 case GOT_OBJ_TYPE_COMMIT:
5675 *commit_id = obj_id;
5677 case GOT_OBJ_TYPE_TAG:
5678 err = got_object_open_as_tag(&tag, repo, obj_id);
5682 err = got_object_get_type(&obj_type, repo,
5683 got_object_tag_get_object_id(tag));
5686 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5687 err = got_error(GOT_ERR_OBJ_TYPE);
5690 *commit_id = got_object_id_dup(
5691 got_object_tag_get_object_id(tag));
5692 if (*commit_id == NULL) {
5693 err = got_error_from_errno("got_object_id_dup");
5698 err = got_error(GOT_ERR_OBJ_TYPE);
5704 got_object_tag_close(tag);
5712 static const struct got_error *
5713 log_ref_entry(struct tog_view **new_view, int begin_x,
5714 struct tog_reflist_entry *re, struct got_repository *repo)
5716 struct tog_view *log_view;
5717 const struct got_error *err = NULL;
5718 struct got_object_id *commit_id = NULL;
5722 err = resolve_reflist_entry(&commit_id, re, repo);
5724 if (err->code != GOT_ERR_OBJ_TYPE)
5730 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5731 if (log_view == NULL) {
5732 err = got_error_from_errno("view_open");
5736 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5739 view_close(log_view);
5741 *new_view = log_view;
5747 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5749 struct tog_reflist_entry *re;
5752 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5755 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5756 while (i++ < maxscroll) {
5759 s->first_displayed_entry = re;
5760 re = TAILQ_PREV(re, tog_reflist_head, entry);
5765 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5767 struct tog_reflist_entry *next, *last;
5770 if (s->first_displayed_entry)
5771 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5773 next = TAILQ_FIRST(&s->refs);
5775 last = s->last_displayed_entry;
5776 while (next && last && n++ < maxscroll) {
5777 last = TAILQ_NEXT(last, entry);
5779 s->first_displayed_entry = next;
5780 next = TAILQ_NEXT(next, entry);
5785 static const struct got_error *
5786 search_start_ref_view(struct tog_view *view)
5788 struct tog_ref_view_state *s = &view->state.ref;
5790 s->matched_entry = NULL;
5795 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5797 regmatch_t regmatch;
5799 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5803 static const struct got_error *
5804 search_next_ref_view(struct tog_view *view)
5806 struct tog_ref_view_state *s = &view->state.ref;
5807 struct tog_reflist_entry *re = NULL;
5809 if (!view->searching) {
5810 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5814 if (s->matched_entry) {
5815 if (view->searching == TOG_SEARCH_FORWARD) {
5816 if (s->selected_entry)
5817 re = TAILQ_NEXT(s->selected_entry, entry);
5819 re = TAILQ_PREV(s->selected_entry,
5820 tog_reflist_head, entry);
5822 if (s->selected_entry == NULL)
5823 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5825 re = TAILQ_PREV(s->selected_entry,
5826 tog_reflist_head, entry);
5829 if (view->searching == TOG_SEARCH_FORWARD)
5830 re = TAILQ_FIRST(&s->refs);
5832 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5837 if (s->matched_entry == NULL) {
5838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5841 if (view->searching == TOG_SEARCH_FORWARD)
5842 re = TAILQ_FIRST(&s->refs);
5844 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5847 if (match_reflist_entry(re, &view->regex)) {
5848 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5849 s->matched_entry = re;
5853 if (view->searching == TOG_SEARCH_FORWARD)
5854 re = TAILQ_NEXT(re, entry);
5856 re = TAILQ_PREV(re, tog_reflist_head, entry);
5859 if (s->matched_entry) {
5860 s->first_displayed_entry = s->matched_entry;
5867 static const struct got_error *
5868 show_ref_view(struct tog_view *view)
5870 const struct got_error *err = NULL;
5871 struct tog_ref_view_state *s = &view->state.ref;
5872 struct tog_reflist_entry *re;
5875 struct tog_color *tc;
5877 int limit = view->nlines;
5879 werase(view->window);
5886 re = s->first_displayed_entry;
5888 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5890 return got_error_from_errno("asprintf");
5892 err = format_line(&wline, &width, line, view->ncols, 0);
5897 if (view_needs_focus_indication(view))
5898 wstandout(view->window);
5899 waddwstr(view->window, wline);
5900 if (view_needs_focus_indication(view))
5901 wstandend(view->window);
5906 if (width < view->ncols - 1)
5907 waddch(view->window, '\n');
5912 while (re && limit > 0) {
5915 if (got_ref_is_symbolic(re->ref)) {
5916 if (asprintf(&line, "%s -> %s",
5917 got_ref_get_name(re->ref),
5918 got_ref_get_symref_target(re->ref)) == -1)
5919 return got_error_from_errno("asprintf");
5920 } else if (s->show_ids) {
5921 struct got_object_id *id;
5923 err = got_ref_resolve(&id, s->repo, re->ref);
5926 err = got_object_id_str(&id_str, id);
5931 if (asprintf(&line, "%s: %s",
5932 got_ref_get_name(re->ref), id_str) == -1) {
5933 err = got_error_from_errno("asprintf");
5941 line = strdup(got_ref_get_name(re->ref));
5943 return got_error_from_errno("strdup");
5946 err = format_line(&wline, &width, line, view->ncols, 0);
5951 if (n == s->selected) {
5953 wstandout(view->window);
5954 s->selected_entry = re;
5956 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5958 wattr_on(view->window,
5959 COLOR_PAIR(tc->colorpair), NULL);
5960 waddwstr(view->window, wline);
5962 wattr_off(view->window,
5963 COLOR_PAIR(tc->colorpair), NULL);
5964 if (width < view->ncols - 1)
5965 waddch(view->window, '\n');
5966 if (n == s->selected && view->focussed)
5967 wstandend(view->window);
5973 s->last_displayed_entry = re;
5976 re = TAILQ_NEXT(re, entry);
5983 static const struct got_error *
5984 browse_ref_tree(struct tog_view **new_view, int begin_x,
5985 struct tog_reflist_entry *re, struct got_repository *repo)
5987 const struct got_error *err = NULL;
5988 struct got_object_id *commit_id = NULL, *tree_id = NULL;
5989 struct got_tree_object *tree = NULL;
5990 struct tog_view *tree_view;
5994 err = resolve_reflist_entry(&commit_id, re, repo);
5996 if (err->code != GOT_ERR_OBJ_TYPE)
6002 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6006 err = got_object_open_as_tree(&tree, repo, tree_id);
6010 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6011 if (tree_view == NULL) {
6012 err = got_error_from_errno("view_open");
6016 err = open_tree_view(tree_view, tree, commit_id, repo);
6020 *new_view = tree_view;
6026 got_object_tree_close(tree);
6030 static const struct got_error *
6031 input_ref_view(struct tog_view **new_view, struct tog_view **focus_view,
6032 struct tog_view *view, int ch)
6034 const struct got_error *err = NULL;
6035 struct tog_ref_view_state *s = &view->state.ref;
6036 struct tog_view *log_view, *tree_view;
6041 s->show_ids = !s->show_ids;
6045 if (!s->selected_entry)
6047 if (view_is_parent_view(view))
6048 begin_x = view_split_begin_x(view->begin_x);
6049 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6051 if (view_is_parent_view(view)) {
6052 err = view_close_child(view);
6055 view_set_child(view, log_view);
6056 *focus_view = log_view;
6058 *new_view = log_view;
6061 if (!s->selected_entry)
6063 if (view_is_parent_view(view))
6064 begin_x = view_split_begin_x(view->begin_x);
6065 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6067 if (err || tree_view == NULL)
6069 if (view_is_parent_view(view)) {
6070 err = view_close_child(view);
6073 view_set_child(view, tree_view);
6074 *focus_view = tree_view;
6076 *new_view = tree_view;
6080 if (s->selected > 0) {
6084 ref_scroll_up(s, 1);
6088 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6090 ref_scroll_up(s, MAX(0, view->nlines - 1));
6094 if (s->selected < s->ndisplayed - 1) {
6098 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6099 /* can't scroll any further */
6101 ref_scroll_down(s, 1);
6105 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6106 /* can't scroll any further; move cursor down */
6107 if (s->selected < s->ndisplayed - 1)
6108 s->selected = s->ndisplayed - 1;
6111 ref_scroll_down(s, view->nlines - 1);
6114 ref_view_free_refs(s);
6115 err = ref_view_load_refs(s);
6118 if (s->selected > view->nlines)
6119 s->selected = s->ndisplayed - 1;
6132 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6137 static const struct got_error *
6138 cmd_ref(int argc, char *argv[])
6140 const struct got_error *error;
6141 struct got_repository *repo = NULL;
6142 struct got_worktree *worktree = NULL;
6143 char *cwd = NULL, *repo_path = NULL;
6145 struct tog_view *view;
6148 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6153 while ((ch = getopt(argc, argv, "r:")) != -1) {
6156 repo_path = realpath(optarg, NULL);
6157 if (repo_path == NULL)
6158 return got_error_from_errno2("realpath",
6173 cwd = getcwd(NULL, 0);
6175 return got_error_from_errno("getcwd");
6177 error = got_worktree_open(&worktree, cwd);
6178 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6181 if (repo_path == NULL) {
6184 strdup(got_worktree_get_repo_path(worktree));
6186 repo_path = strdup(cwd);
6188 if (repo_path == NULL) {
6189 error = got_error_from_errno("strdup");
6193 error = got_repo_open(&repo, repo_path, NULL);
6199 error = apply_unveil(got_repo_get_path(repo), NULL);
6203 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6205 error = got_error_from_errno("view_open");
6209 error = open_ref_view(view, repo);
6214 /* Release work tree lock. */
6215 got_worktree_close(worktree);
6218 error = view_loop(view);
6223 got_repo_close(repo);
6228 list_commands(FILE *fp)
6232 fprintf(fp, "commands:");
6233 for (i = 0; i < nitems(tog_commands); i++) {
6234 struct tog_cmd *cmd = &tog_commands[i];
6235 fprintf(fp, " %s", cmd->name);
6241 usage(int hflag, int status)
6243 FILE *fp = (status == 0) ? stdout : stderr;
6245 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6248 fprintf(fp, "lazy usage: %s path\n", getprogname());
6255 make_argv(int argc, ...)
6263 argv = calloc(argc, sizeof(char *));
6266 for (i = 0; i < argc; i++) {
6267 argv[i] = strdup(va_arg(ap, char *));
6268 if (argv[i] == NULL)
6277 * Try to convert 'tog path' into a 'tog log path' command.
6278 * The user could simply have mistyped the command rather than knowingly
6279 * provided a path. So check whether argv[0] can in fact be resolved
6280 * to a path in the HEAD commit and print a special error if not.
6281 * This hack is for mpi@ <3
6283 static const struct got_error *
6284 tog_log_with_path(int argc, char *argv[])
6286 const struct got_error *error = NULL;
6287 struct tog_cmd *cmd = NULL;
6288 struct got_repository *repo = NULL;
6289 struct got_worktree *worktree = NULL;
6290 struct got_object_id *commit_id = NULL, *id = NULL;
6291 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6292 char *commit_id_str = NULL, **cmd_argv = NULL;
6294 cwd = getcwd(NULL, 0);
6296 return got_error_from_errno("getcwd");
6298 error = got_worktree_open(&worktree, cwd);
6299 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6303 repo_path = strdup(got_worktree_get_repo_path(worktree));
6305 repo_path = strdup(cwd);
6306 if (repo_path == NULL) {
6307 error = got_error_from_errno("strdup");
6311 error = got_repo_open(&repo, repo_path, NULL);
6315 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6320 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6321 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6322 GOT_OBJ_TYPE_COMMIT, 1, repo);
6327 got_worktree_close(worktree);
6331 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6333 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6335 fprintf(stderr, "%s: '%s' is no known command or path\n",
6336 getprogname(), argv[0]);
6341 got_repo_close(repo);
6344 error = got_object_id_str(&commit_id_str, commit_id);
6348 cmd = &tog_commands[0]; /* log */
6350 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6351 error = cmd->cmd_main(argc, cmd_argv);
6354 got_repo_close(repo);
6356 got_worktree_close(worktree);
6358 free(commit_id_str);
6365 for (i = 0; i < argc; i++)
6373 main(int argc, char *argv[])
6375 const struct got_error *error = NULL;
6376 struct tog_cmd *cmd = NULL;
6377 int ch, hflag = 0, Vflag = 0;
6378 char **cmd_argv = NULL;
6379 static struct option longopts[] = {
6380 { "version", no_argument, NULL, 'V' },
6384 setlocale(LC_CTYPE, "");
6386 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6406 got_version_print_str();
6413 /* Build an argument vector which runs a default command. */
6414 cmd = &tog_commands[0];
6416 cmd_argv = make_argv(argc, cmd->name);
6420 /* Did the user specify a command? */
6421 for (i = 0; i < nitems(tog_commands); i++) {
6422 if (strncmp(tog_commands[i].name, argv[0],
6423 strlen(argv[0])) == 0) {
6424 cmd = &tog_commands[i];
6433 /* No command specified; try log with a path */
6434 error = tog_log_with_path(argc, argv);
6439 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6446 for (i = 0; i < argc; i++)
6451 if (error && error->code != GOT_ERR_CANCELLED)
6452 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);