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 */
450 struct tog_view *parent;
451 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**, 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 **, 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 **, 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 **, 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 **, 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 **, 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);
717 static const struct got_error *
718 view_set_child(struct tog_view *view, struct tog_view *child)
720 const struct got_error *err = NULL;
723 child->parent = view;
728 view_is_splitscreen(struct tog_view *view)
730 return view->begin_x > 0;
739 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
740 cols = 80; /* Default */
746 resize_term(lines, cols);
749 static const struct got_error *
750 view_search_start(struct tog_view *view)
752 const struct got_error *err = NULL;
756 if (view->nlines < 1)
759 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
760 wclrtoeol(view->window);
764 ret = wgetnstr(view->window, pattern, sizeof(pattern));
770 if (view->searching) {
771 regfree(&view->regex);
775 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
776 err = view->search_start(view);
778 regfree(&view->regex);
781 view->searching = TOG_SEARCH_FORWARD;
782 view->search_next_done = 0;
783 view->search_next(view);
789 static const struct got_error *
790 view_input(struct tog_view **new, struct tog_view **dead,
791 struct tog_view **focus, int *done, struct tog_view *view,
792 struct tog_view_list_head *views)
794 const struct got_error *err = NULL;
802 /* Clear "no matches" indicator. */
803 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
804 view->search_next_done == TOG_SEARCH_HAVE_NONE)
805 view->search_next_done = TOG_SEARCH_HAVE_MORE;
807 if (view->searching && !view->search_next_done) {
808 errcode = pthread_mutex_unlock(&tog_mutex);
810 return got_error_set_errno(errcode,
811 "pthread_mutex_unlock");
813 errcode = pthread_mutex_lock(&tog_mutex);
815 return got_error_set_errno(errcode,
816 "pthread_mutex_lock");
817 view->search_next(view);
821 nodelay(stdscr, FALSE);
822 /* Allow threads to make progress while we are waiting for input. */
823 errcode = pthread_mutex_unlock(&tog_mutex);
825 return got_error_set_errno(errcode, "pthread_mutex_unlock");
826 ch = wgetch(view->window);
827 errcode = pthread_mutex_lock(&tog_mutex);
829 return got_error_set_errno(errcode, "pthread_mutex_lock");
830 nodelay(stdscr, TRUE);
832 if (tog_sigwinch_received || tog_sigcont_received) {
834 tog_sigwinch_received = 0;
835 tog_sigcont_received = 0;
836 TAILQ_FOREACH(v, views, entry) {
837 err = view_resize(v);
840 err = v->input(new, dead, focus, v, KEY_RESIZE);
851 *focus = view->child;
852 view->child_focussed = 1;
853 } else if (view->parent) {
854 *focus = view->parent;
855 view->parent->child_focussed = 0;
859 err = view->input(new, dead, focus, view, ch);
866 if (view_is_parent_view(view)) {
867 if (view->child == NULL)
869 if (view_is_splitscreen(view->child)) {
870 *focus = view->child;
871 view->child_focussed = 1;
872 err = view_fullscreen(view->child);
874 err = view_splitscreen(view->child);
877 err = view->child->input(new, dead, focus,
878 view->child, KEY_RESIZE);
880 if (view_is_splitscreen(view)) {
882 view->parent->child_focussed = 1;
883 err = view_fullscreen(view);
885 err = view_splitscreen(view);
889 err = view->input(new, dead, focus, view,
896 if (view->search_start)
897 view_search_start(view);
899 err = view->input(new, dead, focus, view, ch);
903 if (view->search_next) {
904 view->searching = (ch == 'n' ?
905 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
906 view->search_next_done = 0;
907 view->search_next(view);
909 err = view->input(new, dead, focus, view, ch);
912 err = view->input(new, dead, focus, view, ch);
920 view_vborder(struct tog_view *view)
923 struct tog_view *view_above;
926 return view_vborder(view->parent);
928 panel = panel_above(view->panel);
932 view_above = panel_userptr(panel);
933 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
934 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
938 view_needs_focus_indication(struct tog_view *view)
940 if (view_is_parent_view(view)) {
941 if (view->child == NULL || view->child_focussed)
943 if (!view_is_splitscreen(view->child))
945 } else if (!view_is_splitscreen(view))
948 return view->focussed;
951 static const struct got_error *
952 view_loop(struct tog_view *view)
954 const struct got_error *err = NULL;
955 struct tog_view_list_head views;
956 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
957 int fast_refresh = 10;
958 int done = 0, errcode;
960 errcode = pthread_mutex_lock(&tog_mutex);
962 return got_error_set_errno(errcode, "pthread_mutex_lock");
965 TAILQ_INSERT_HEAD(&views, view, entry);
969 err = view->show(view);
974 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
975 /* Refresh fast during initialization, then become slower. */
976 if (fast_refresh && fast_refresh-- == 0)
977 halfdelay(10); /* switch to once per second */
979 err = view_input(&new_view, &dead_view, &focus_view, &done,
984 struct tog_view *prev = NULL;
986 if (view_is_parent_view(dead_view))
987 prev = TAILQ_PREV(dead_view,
988 tog_view_list_head, entry);
989 else if (view->parent != dead_view)
992 if (dead_view->parent)
993 dead_view->parent->child = NULL;
995 TAILQ_REMOVE(&views, dead_view, entry);
997 err = view_close(dead_view);
998 if (err || (dead_view == main_view && new_view == NULL))
1001 if (view == dead_view) {
1006 else if (!TAILQ_EMPTY(&views))
1007 view = TAILQ_LAST(&views,
1008 tog_view_list_head);
1012 if (view->child && view->child_focussed)
1013 focus_view = view->child;
1020 struct tog_view *v, *t;
1021 /* Only allow one parent view per type. */
1022 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1023 if (v->type != new_view->type)
1025 TAILQ_REMOVE(&views, v, entry);
1026 err = view_close(v);
1031 TAILQ_INSERT_TAIL(&views, new_view, entry);
1033 if (focus_view == NULL)
1034 focus_view = new_view;
1037 show_panel(focus_view->panel);
1040 focus_view->focussed = 1;
1043 show_panel(new_view->panel);
1044 if (view->child && view_is_splitscreen(view->child))
1045 show_panel(view->child->panel);
1048 if (focus_view == NULL) {
1050 show_panel(view->panel);
1051 if (view->child && view_is_splitscreen(view->child))
1052 show_panel(view->child->panel);
1056 err = view->parent->show(view->parent);
1060 err = view->show(view);
1064 err = view->child->show(view->child);
1073 while (!TAILQ_EMPTY(&views)) {
1074 view = TAILQ_FIRST(&views);
1075 TAILQ_REMOVE(&views, view, entry);
1079 errcode = pthread_mutex_unlock(&tog_mutex);
1080 if (errcode && err == NULL)
1081 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1091 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1096 /* Create newly allocated wide-character string equivalent to a byte string. */
1097 static const struct got_error *
1098 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1101 const struct got_error *err = NULL;
1104 *wlen = mbstowcs(NULL, s, 0);
1105 if (*wlen == (size_t)-1) {
1107 if (errno != EILSEQ)
1108 return got_error_from_errno("mbstowcs");
1110 /* byte string invalid in current encoding; try to "fix" it */
1111 err = got_mbsavis(&vis, &vislen, s);
1114 *wlen = mbstowcs(NULL, vis, 0);
1115 if (*wlen == (size_t)-1) {
1116 err = got_error_from_errno("mbstowcs"); /* give up */
1121 *ws = calloc(*wlen + 1, sizeof(**ws));
1123 err = got_error_from_errno("calloc");
1127 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1128 err = got_error_from_errno("mbstowcs");
1139 /* Format a line for display, ensuring that it won't overflow a width limit. */
1140 static const struct got_error *
1141 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1144 const struct got_error *err = NULL;
1146 wchar_t *wline = NULL;
1153 err = mbs2ws(&wline, &wlen, line);
1159 int width = wcwidth(wline[i]);
1166 if (width == 1 || width == 2) {
1167 if (cols + width > wlimit)
1171 } else if (width == -1) {
1172 if (wline[i] == L'\t') {
1174 ((cols + col_tab_align) % TABSIZE);
1175 if (cols + width > wlimit)
1181 err = got_error_from_errno("wcwidth");
1196 static const struct got_error*
1197 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1198 struct got_object_id *id, struct got_repository *repo)
1200 static const struct got_error *err = NULL;
1201 struct got_reflist_entry *re;
1207 SIMPLEQ_FOREACH(re, refs, entry) {
1208 struct got_tag_object *tag = NULL;
1209 struct got_object_id *ref_id;
1212 name = got_ref_get_name(re->ref);
1213 if (strcmp(name, GOT_REF_HEAD) == 0)
1215 if (strncmp(name, "refs/", 5) == 0)
1217 if (strncmp(name, "got/", 4) == 0)
1219 if (strncmp(name, "heads/", 6) == 0)
1221 if (strncmp(name, "remotes/", 8) == 0) {
1223 s = strstr(name, "/" GOT_REF_HEAD);
1224 if (s != NULL && s[strlen(s)] == '\0')
1227 err = got_ref_resolve(&ref_id, repo, re->ref);
1230 if (strncmp(name, "tags/", 5) == 0) {
1231 err = got_object_open_as_tag(&tag, repo, ref_id);
1233 if (err->code != GOT_ERR_OBJ_TYPE) {
1237 /* Ref points at something other than a tag. */
1242 cmp = got_object_id_cmp(tag ?
1243 got_object_tag_get_object_id(tag) : ref_id, id);
1246 got_object_tag_close(tag);
1250 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1251 s ? ", " : "", name) == -1) {
1252 err = got_error_from_errno("asprintf");
1263 static const struct got_error *
1264 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1267 char *smallerthan, *at;
1269 smallerthan = strchr(author, '<');
1270 if (smallerthan && smallerthan[1] != '\0')
1271 author = smallerthan + 1;
1272 at = strchr(author, '@');
1275 return format_line(wauthor, author_width, author, limit, col_tab_align);
1278 static const struct got_error *
1279 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1280 struct got_object_id *id, const size_t date_display_cols,
1281 int author_display_cols)
1283 struct tog_log_view_state *s = &view->state.log;
1284 const struct got_error *err = NULL;
1285 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1286 char *logmsg0 = NULL, *logmsg = NULL;
1287 char *author = NULL;
1288 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1289 int author_width, logmsg_width;
1290 char *newline, *line = NULL;
1292 const int avail = view->ncols;
1294 time_t committer_time;
1295 struct tog_color *tc;
1297 committer_time = got_object_commit_get_committer_time(commit);
1298 if (localtime_r(&committer_time, &tm) == NULL)
1299 return got_error_from_errno("localtime_r");
1300 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1302 return got_error(GOT_ERR_NO_SPACE);
1304 if (avail <= date_display_cols)
1305 limit = MIN(sizeof(datebuf) - 1, avail);
1307 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1308 tc = get_color(&s->colors, TOG_COLOR_DATE);
1310 wattr_on(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1312 waddnstr(view->window, datebuf, limit);
1314 wattr_off(view->window,
1315 COLOR_PAIR(tc->colorpair), NULL);
1322 err = got_object_id_str(&id_str, id);
1325 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1327 wattr_on(view->window,
1328 COLOR_PAIR(tc->colorpair), NULL);
1329 wprintw(view->window, "%.8s ", id_str);
1331 wattr_off(view->window,
1332 COLOR_PAIR(tc->colorpair), NULL);
1339 author = strdup(got_object_commit_get_author(commit));
1340 if (author == NULL) {
1341 err = got_error_from_errno("strdup");
1344 err = format_author(&wauthor, &author_width, author, avail - col, col);
1347 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1349 wattr_on(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1351 waddwstr(view->window, wauthor);
1353 wattr_off(view->window,
1354 COLOR_PAIR(tc->colorpair), NULL);
1355 col += author_width;
1356 while (col < avail && author_width < author_display_cols + 2) {
1357 waddch(view->window, ' ');
1364 err = got_object_commit_get_logmsg(&logmsg0, commit);
1368 while (*logmsg == '\n')
1370 newline = strchr(logmsg, '\n');
1373 limit = avail - col;
1374 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1377 waddwstr(view->window, wlogmsg);
1378 col += logmsg_width;
1379 while (col < avail) {
1380 waddch(view->window, ' ');
1392 static struct commit_queue_entry *
1393 alloc_commit_queue_entry(struct got_commit_object *commit,
1394 struct got_object_id *id)
1396 struct commit_queue_entry *entry;
1398 entry = calloc(1, sizeof(*entry));
1403 entry->commit = commit;
1408 pop_commit(struct commit_queue *commits)
1410 struct commit_queue_entry *entry;
1412 entry = TAILQ_FIRST(&commits->head);
1413 TAILQ_REMOVE(&commits->head, entry, entry);
1414 got_object_commit_close(entry->commit);
1415 commits->ncommits--;
1416 /* Don't free entry->id! It is owned by the commit graph. */
1421 free_commits(struct commit_queue *commits)
1423 while (!TAILQ_EMPTY(&commits->head))
1424 pop_commit(commits);
1427 static const struct got_error *
1428 match_commit(int *have_match, struct got_object_id *id,
1429 struct got_commit_object *commit, regex_t *regex)
1431 const struct got_error *err = NULL;
1432 regmatch_t regmatch;
1433 char *id_str = NULL, *logmsg = NULL;
1437 err = got_object_id_str(&id_str, id);
1441 err = got_object_commit_get_logmsg(&logmsg, commit);
1445 if (regexec(regex, got_object_commit_get_author(commit), 1,
1446 ®match, 0) == 0 ||
1447 regexec(regex, got_object_commit_get_committer(commit), 1,
1448 ®match, 0) == 0 ||
1449 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1450 regexec(regex, logmsg, 1, ®match, 0) == 0)
1458 static const struct got_error *
1459 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1460 int minqueue, struct got_repository *repo, const char *path,
1461 int *searching, int *search_next_done, regex_t *regex)
1463 const struct got_error *err = NULL;
1467 * We keep all commits open throughout the lifetime of the log
1468 * view in order to avoid having to re-fetch commits from disk
1469 * while updating the display.
1471 while (nqueued < minqueue ||
1472 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1473 struct got_object_id *id;
1474 struct got_commit_object *commit;
1475 struct commit_queue_entry *entry;
1478 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1479 if (err || id == NULL)
1482 err = got_object_open_as_commit(&commit, repo, id);
1485 entry = alloc_commit_queue_entry(commit, id);
1486 if (entry == NULL) {
1487 err = got_error_from_errno("alloc_commit_queue_entry");
1491 errcode = pthread_mutex_lock(&tog_mutex);
1493 err = got_error_set_errno(errcode,
1494 "pthread_mutex_lock");
1498 entry->idx = commits->ncommits;
1499 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1501 commits->ncommits++;
1503 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1505 err = match_commit(&have_match, id, commit, regex);
1509 *search_next_done = TOG_SEARCH_HAVE_MORE;
1512 errcode = pthread_mutex_unlock(&tog_mutex);
1513 if (errcode && err == NULL)
1514 err = got_error_set_errno(errcode,
1515 "pthread_mutex_unlock");
1523 static const struct got_error *
1524 draw_commits(struct tog_view *view)
1526 const struct got_error *err = NULL;
1527 struct tog_log_view_state *s = &view->state.log;
1528 struct commit_queue_entry *entry;
1529 const int limit = view->nlines;
1531 int ncommits, author_cols = 4;
1532 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1533 char *refs_str = NULL;
1535 struct tog_color *tc;
1536 static const size_t date_display_cols = 12;
1538 entry = s->first_displayed_entry;
1541 if (ncommits == s->selected) {
1542 s->selected_entry = entry;
1545 entry = TAILQ_NEXT(entry, entry);
1549 if (s->selected_entry &&
1550 !(view->searching && view->search_next_done == 0)) {
1551 err = got_object_id_str(&id_str, s->selected_entry->id);
1554 err = build_refs_str(&refs_str, &s->refs,
1555 s->selected_entry->id, s->repo);
1560 if (s->thread_args.commits_needed == 0)
1561 halfdelay(10); /* disable fast refresh */
1563 if (s->thread_args.commits_needed > 0) {
1564 if (asprintf(&ncommits_str, " [%d/%d] %s",
1565 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1566 (view->searching && !view->search_next_done) ?
1567 "searching..." : "loading...") == -1) {
1568 err = got_error_from_errno("asprintf");
1572 const char *search_str = NULL;
1574 if (view->searching) {
1575 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1576 search_str = "no more matches";
1577 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1578 search_str = "no matches found";
1579 else if (!view->search_next_done)
1580 search_str = "searching...";
1583 if (asprintf(&ncommits_str, " [%d/%d] %s",
1584 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1585 search_str ? search_str :
1586 (refs_str ? refs_str : "")) == -1) {
1587 err = got_error_from_errno("asprintf");
1592 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1593 if (asprintf(&header, "commit %s %s%s",
1594 id_str ? id_str : "........................................",
1595 s->in_repo_path, ncommits_str) == -1) {
1596 err = got_error_from_errno("asprintf");
1600 } else if (asprintf(&header, "commit %s%s",
1601 id_str ? id_str : "........................................",
1602 ncommits_str) == -1) {
1603 err = got_error_from_errno("asprintf");
1607 err = format_line(&wline, &width, header, view->ncols, 0);
1611 werase(view->window);
1613 if (view_needs_focus_indication(view))
1614 wstandout(view->window);
1615 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1617 wattr_on(view->window,
1618 COLOR_PAIR(tc->colorpair), NULL);
1619 waddwstr(view->window, wline);
1621 wattr_off(view->window,
1622 COLOR_PAIR(tc->colorpair), NULL);
1623 while (width < view->ncols) {
1624 waddch(view->window, ' ');
1627 if (view_needs_focus_indication(view))
1628 wstandend(view->window);
1633 /* Grow author column size if necessary. */
1634 entry = s->first_displayed_entry;
1640 if (ncommits >= limit - 1)
1642 author = strdup(got_object_commit_get_author(entry->commit));
1643 if (author == NULL) {
1644 err = got_error_from_errno("strdup");
1647 err = format_author(&wauthor, &width, author, COLS,
1649 if (author_cols < width)
1650 author_cols = width;
1654 entry = TAILQ_NEXT(entry, entry);
1657 entry = s->first_displayed_entry;
1658 s->last_displayed_entry = s->first_displayed_entry;
1661 if (ncommits >= limit - 1)
1663 if (ncommits == s->selected)
1664 wstandout(view->window);
1665 err = draw_commit(view, entry->commit, entry->id,
1666 date_display_cols, author_cols);
1667 if (ncommits == s->selected)
1668 wstandend(view->window);
1672 s->last_displayed_entry = entry;
1673 entry = TAILQ_NEXT(entry, entry);
1686 log_scroll_up(struct tog_view *view, int maxscroll)
1688 struct tog_log_view_state *s = &view->state.log;
1689 struct commit_queue_entry *entry;
1692 entry = TAILQ_FIRST(&s->commits.head);
1693 if (s->first_displayed_entry == entry)
1696 entry = s->first_displayed_entry;
1697 while (entry && nscrolled < maxscroll) {
1698 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1700 s->first_displayed_entry = entry;
1706 static const struct got_error *
1707 trigger_log_thread(struct tog_view *view, int wait)
1709 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1712 halfdelay(1); /* fast refresh while loading commits */
1714 while (ta->commits_needed > 0) {
1715 if (ta->log_complete)
1718 /* Wake the log thread. */
1719 errcode = pthread_cond_signal(&ta->need_commits);
1721 return got_error_set_errno(errcode,
1722 "pthread_cond_signal");
1725 * The mutex will be released while the view loop waits
1726 * in wgetch(), at which time the log thread will run.
1731 /* Display progress update in log view. */
1732 show_log_view(view);
1736 /* Wait right here while next commit is being loaded. */
1737 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1739 return got_error_set_errno(errcode,
1740 "pthread_cond_wait");
1742 /* Display progress update in log view. */
1743 show_log_view(view);
1751 static const struct got_error *
1752 log_scroll_down(struct tog_view *view, int maxscroll)
1754 struct tog_log_view_state *s = &view->state.log;
1755 const struct got_error *err = NULL;
1756 struct commit_queue_entry *pentry;
1757 int nscrolled = 0, ncommits_needed;
1759 if (s->last_displayed_entry == NULL)
1762 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1763 if (s->commits.ncommits < ncommits_needed &&
1764 !s->thread_args.log_complete) {
1766 * Ask the log thread for required amount of commits.
1768 s->thread_args.commits_needed += maxscroll;
1769 err = trigger_log_thread(view, 1);
1775 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1779 s->last_displayed_entry = pentry;
1781 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1784 s->first_displayed_entry = pentry;
1785 } while (++nscrolled < maxscroll);
1790 static const struct got_error *
1791 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1792 struct got_commit_object *commit, struct got_object_id *commit_id,
1793 struct tog_view *log_view, struct got_repository *repo)
1795 const struct got_error *err;
1796 struct got_object_qid *parent_id;
1797 struct tog_view *diff_view;
1799 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1800 if (diff_view == NULL)
1801 return got_error_from_errno("view_open");
1803 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1804 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1805 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1807 *new_view = diff_view;
1811 static const struct got_error *
1812 tree_view_visit_subtree(struct got_tree_object *subtree,
1813 struct tog_tree_view_state *s)
1815 struct tog_parent_tree *parent;
1817 parent = calloc(1, sizeof(*parent));
1819 return got_error_from_errno("calloc");
1821 parent->tree = s->tree;
1822 parent->first_displayed_entry = s->first_displayed_entry;
1823 parent->selected_entry = s->selected_entry;
1824 parent->selected = s->selected;
1825 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1828 s->first_displayed_entry = NULL;
1832 static const struct got_error *
1833 tree_view_walk_path(struct tog_tree_view_state *s,
1834 struct got_object_id *commit_id,
1835 const char *path, struct got_repository *repo)
1837 const struct got_error *err = NULL;
1838 struct got_tree_object *tree = NULL;
1840 char *slash, *subpath = NULL;
1842 /* Walk the path and open corresponding tree objects. */
1845 struct got_tree_entry *te;
1846 struct got_object_id *tree_id;
1852 /* Ensure the correct subtree entry is selected. */
1853 slash = strchr(p, '/');
1855 te_name = strdup(p);
1857 te_name = strndup(p, slash - p);
1858 if (te_name == NULL) {
1859 err = got_error_from_errno("strndup");
1862 te = got_object_tree_find_entry(s->tree, te_name);
1864 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1869 s->first_displayed_entry = s->selected_entry = te;
1871 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1872 break; /* jump to this file's entry */
1874 slash = strchr(p, '/');
1876 subpath = strndup(path, slash - path);
1878 subpath = strdup(path);
1879 if (subpath == NULL) {
1880 err = got_error_from_errno("strdup");
1884 err = got_object_id_by_path(&tree_id, repo, commit_id,
1889 err = got_object_open_as_tree(&tree, repo, tree_id);
1894 err = tree_view_visit_subtree(tree, s);
1896 got_object_tree_close(tree);
1910 static const struct got_error *
1911 browse_commit_tree(struct tog_view **new_view, int begin_x,
1912 struct commit_queue_entry *entry, const char *path,
1913 struct got_repository *repo)
1915 const struct got_error *err = NULL;
1916 struct got_tree_object *tree;
1917 struct tog_tree_view_state *s;
1918 struct tog_view *tree_view;
1920 err = got_object_open_as_tree(&tree, repo,
1921 got_object_commit_get_tree_id(entry->commit));
1925 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1926 if (tree_view == NULL)
1927 return got_error_from_errno("view_open");
1929 err = open_tree_view(tree_view, tree, entry->id, repo);
1931 got_object_tree_close(tree);
1934 s = &tree_view->state.tree;
1936 *new_view = tree_view;
1938 if (got_path_is_root_dir(path))
1941 return tree_view_walk_path(s, entry->id, path, repo);
1944 static const struct got_error *
1945 block_signals_used_by_main_thread(void)
1950 if (sigemptyset(&sigset) == -1)
1951 return got_error_from_errno("sigemptyset");
1953 /* tog handles SIGWINCH and SIGCONT */
1954 if (sigaddset(&sigset, SIGWINCH) == -1)
1955 return got_error_from_errno("sigaddset");
1956 if (sigaddset(&sigset, SIGCONT) == -1)
1957 return got_error_from_errno("sigaddset");
1959 /* ncurses handles SIGTSTP */
1960 if (sigaddset(&sigset, SIGTSTP) == -1)
1961 return got_error_from_errno("sigaddset");
1963 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1965 return got_error_set_errno(errcode, "pthread_sigmask");
1971 log_thread(void *arg)
1973 const struct got_error *err = NULL;
1975 struct tog_log_thread_args *a = arg;
1978 err = block_signals_used_by_main_thread();
1982 while (!done && !err && !tog_sigpipe_received) {
1983 err = queue_commits(a->graph, a->commits, 1, a->repo,
1984 a->in_repo_path, a->searching, a->search_next_done,
1987 if (err->code != GOT_ERR_ITER_COMPLETED)
1991 } else if (a->commits_needed > 0)
1992 a->commits_needed--;
1994 errcode = pthread_mutex_lock(&tog_mutex);
1996 err = got_error_set_errno(errcode,
1997 "pthread_mutex_lock");
1999 } else if (*a->quit)
2001 else if (*a->first_displayed_entry == NULL) {
2002 *a->first_displayed_entry =
2003 TAILQ_FIRST(&a->commits->head);
2004 *a->selected_entry = *a->first_displayed_entry;
2007 errcode = pthread_cond_signal(&a->commit_loaded);
2009 err = got_error_set_errno(errcode,
2010 "pthread_cond_signal");
2011 pthread_mutex_unlock(&tog_mutex);
2016 a->commits_needed = 0;
2018 if (a->commits_needed == 0) {
2019 errcode = pthread_cond_wait(&a->need_commits,
2022 err = got_error_set_errno(errcode,
2023 "pthread_cond_wait");
2027 errcode = pthread_mutex_unlock(&tog_mutex);
2028 if (errcode && err == NULL)
2029 err = got_error_set_errno(errcode,
2030 "pthread_mutex_unlock");
2032 a->log_complete = 1;
2036 static const struct got_error *
2037 stop_log_thread(struct tog_log_view_state *s)
2039 const struct got_error *err = NULL;
2044 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2046 return got_error_set_errno(errcode,
2047 "pthread_cond_signal");
2048 errcode = pthread_mutex_unlock(&tog_mutex);
2050 return got_error_set_errno(errcode,
2051 "pthread_mutex_unlock");
2052 errcode = pthread_join(s->thread, (void **)&err);
2054 return got_error_set_errno(errcode, "pthread_join");
2055 errcode = pthread_mutex_lock(&tog_mutex);
2057 return got_error_set_errno(errcode,
2058 "pthread_mutex_lock");
2062 if (s->thread_args.repo) {
2063 got_repo_close(s->thread_args.repo);
2064 s->thread_args.repo = NULL;
2067 if (s->thread_args.graph) {
2068 got_commit_graph_close(s->thread_args.graph);
2069 s->thread_args.graph = NULL;
2075 static const struct got_error *
2076 close_log_view(struct tog_view *view)
2078 const struct got_error *err = NULL;
2079 struct tog_log_view_state *s = &view->state.log;
2082 err = stop_log_thread(s);
2084 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2085 if (errcode && err == NULL)
2086 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2088 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2089 if (errcode && err == NULL)
2090 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2092 free_commits(&s->commits);
2093 free(s->in_repo_path);
2094 s->in_repo_path = NULL;
2097 got_ref_list_free(&s->refs);
2101 static const struct got_error *
2102 search_start_log_view(struct tog_view *view)
2104 struct tog_log_view_state *s = &view->state.log;
2106 s->matched_entry = NULL;
2107 s->search_entry = NULL;
2111 static const struct got_error *
2112 search_next_log_view(struct tog_view *view)
2114 const struct got_error *err = NULL;
2115 struct tog_log_view_state *s = &view->state.log;
2116 struct commit_queue_entry *entry;
2118 /* Display progress update in log view. */
2119 show_log_view(view);
2123 if (s->search_entry) {
2125 errcode = pthread_mutex_unlock(&tog_mutex);
2127 return got_error_set_errno(errcode,
2128 "pthread_mutex_unlock");
2129 ch = wgetch(view->window);
2130 errcode = pthread_mutex_lock(&tog_mutex);
2132 return got_error_set_errno(errcode,
2133 "pthread_mutex_lock");
2134 if (ch == KEY_BACKSPACE) {
2135 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2138 if (view->searching == TOG_SEARCH_FORWARD)
2139 entry = TAILQ_NEXT(s->search_entry, entry);
2141 entry = TAILQ_PREV(s->search_entry,
2142 commit_queue_head, entry);
2143 } else if (s->matched_entry) {
2144 if (view->searching == TOG_SEARCH_FORWARD)
2145 entry = TAILQ_NEXT(s->matched_entry, entry);
2147 entry = TAILQ_PREV(s->matched_entry,
2148 commit_queue_head, entry);
2150 if (view->searching == TOG_SEARCH_FORWARD)
2151 entry = TAILQ_FIRST(&s->commits.head);
2153 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2159 if (entry == NULL) {
2160 if (s->thread_args.log_complete ||
2161 view->searching == TOG_SEARCH_BACKWARD) {
2162 view->search_next_done =
2163 (s->matched_entry == NULL ?
2164 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2165 s->search_entry = NULL;
2169 * Poke the log thread for more commits and return,
2170 * allowing the main loop to make progress. Search
2171 * will resume at s->search_entry once we come back.
2173 s->thread_args.commits_needed++;
2174 return trigger_log_thread(view, 0);
2177 err = match_commit(&have_match, entry->id, entry->commit,
2182 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2183 s->matched_entry = entry;
2187 s->search_entry = entry;
2188 if (view->searching == TOG_SEARCH_FORWARD)
2189 entry = TAILQ_NEXT(entry, entry);
2191 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2194 if (s->matched_entry) {
2195 int cur = s->selected_entry->idx;
2196 while (cur < s->matched_entry->idx) {
2197 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2202 while (cur > s->matched_entry->idx) {
2203 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2210 s->search_entry = NULL;
2215 static const struct got_error *
2216 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2217 struct got_repository *repo, const char *head_ref_name,
2218 const char *in_repo_path, int log_branches)
2220 const struct got_error *err = NULL;
2221 struct tog_log_view_state *s = &view->state.log;
2222 struct got_repository *thread_repo = NULL;
2223 struct got_commit_graph *thread_graph = NULL;
2226 SIMPLEQ_INIT(&s->refs);
2228 if (in_repo_path != s->in_repo_path) {
2229 free(s->in_repo_path);
2230 s->in_repo_path = strdup(in_repo_path);
2231 if (s->in_repo_path == NULL)
2232 return got_error_from_errno("strdup");
2235 /* The commit queue only contains commits being displayed. */
2236 TAILQ_INIT(&s->commits.head);
2237 s->commits.ncommits = 0;
2239 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2244 s->head_ref_name = head_ref_name;
2245 s->start_id = got_object_id_dup(start_id);
2246 if (s->start_id == NULL) {
2247 err = got_error_from_errno("got_object_id_dup");
2250 s->log_branches = log_branches;
2252 SIMPLEQ_INIT(&s->colors);
2253 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2254 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2255 get_color_value("TOG_COLOR_COMMIT"));
2258 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2259 get_color_value("TOG_COLOR_AUTHOR"));
2261 free_colors(&s->colors);
2264 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2265 get_color_value("TOG_COLOR_DATE"));
2267 free_colors(&s->colors);
2272 view->show = show_log_view;
2273 view->input = input_log_view;
2274 view->close = close_log_view;
2275 view->search_start = search_start_log_view;
2276 view->search_next = search_next_log_view;
2278 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2281 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2285 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2286 s->repo, NULL, NULL);
2290 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2292 err = got_error_set_errno(errcode, "pthread_cond_init");
2295 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2297 err = got_error_set_errno(errcode, "pthread_cond_init");
2301 s->thread_args.commits_needed = view->nlines;
2302 s->thread_args.graph = thread_graph;
2303 s->thread_args.commits = &s->commits;
2304 s->thread_args.in_repo_path = s->in_repo_path;
2305 s->thread_args.start_id = s->start_id;
2306 s->thread_args.repo = thread_repo;
2307 s->thread_args.log_complete = 0;
2308 s->thread_args.quit = &s->quit;
2309 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2310 s->thread_args.selected_entry = &s->selected_entry;
2311 s->thread_args.searching = &view->searching;
2312 s->thread_args.search_next_done = &view->search_next_done;
2313 s->thread_args.regex = &view->regex;
2316 close_log_view(view);
2320 static const struct got_error *
2321 show_log_view(struct tog_view *view)
2323 const struct got_error *err;
2324 struct tog_log_view_state *s = &view->state.log;
2326 if (s->thread == NULL) {
2327 int errcode = pthread_create(&s->thread, NULL, log_thread,
2330 return got_error_set_errno(errcode, "pthread_create");
2331 if (s->thread_args.commits_needed > 0) {
2332 err = trigger_log_thread(view, 1);
2338 return draw_commits(view);
2341 static const struct got_error *
2342 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2343 struct tog_view **focus_view, struct tog_view *view, int ch)
2345 const struct got_error *err = NULL;
2346 struct tog_log_view_state *s = &view->state.log;
2347 char *parent_path, *in_repo_path = NULL;
2348 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2349 struct tog_view *ref_view = NULL;
2351 struct got_object_id *start_id;
2361 if (s->first_displayed_entry == NULL)
2363 if (s->selected > 0)
2366 log_scroll_up(view, 1);
2370 if (s->first_displayed_entry == NULL)
2372 if (TAILQ_FIRST(&s->commits.head) ==
2373 s->first_displayed_entry) {
2377 log_scroll_up(view, view->nlines - 1);
2383 if (s->first_displayed_entry == NULL)
2385 if (s->selected < MIN(view->nlines - 2,
2386 s->commits.ncommits - 1)) {
2390 err = log_scroll_down(view, 1);
2394 struct commit_queue_entry *first;
2395 first = s->first_displayed_entry;
2398 err = log_scroll_down(view, view->nlines - 1);
2401 if (first == s->first_displayed_entry &&
2402 s->selected < MIN(view->nlines - 2,
2403 s->commits.ncommits - 1)) {
2404 /* can't scroll further down */
2405 s->selected = MIN(view->nlines - 2,
2406 s->commits.ncommits - 1);
2412 if (s->selected > view->nlines - 2)
2413 s->selected = view->nlines - 2;
2414 if (s->selected > s->commits.ncommits - 1)
2415 s->selected = s->commits.ncommits - 1;
2420 if (s->selected_entry == NULL)
2422 if (view_is_parent_view(view))
2423 begin_x = view_split_begin_x(view->begin_x);
2424 err = open_diff_view_for_commit(&diff_view, begin_x,
2425 s->selected_entry->commit, s->selected_entry->id,
2429 if (view_is_parent_view(view)) {
2430 err = view_close_child(view);
2433 err = view_set_child(view, diff_view);
2435 view_close(diff_view);
2438 *focus_view = diff_view;
2439 view->child_focussed = 1;
2441 *new_view = diff_view;
2444 if (s->selected_entry == NULL)
2446 if (view_is_parent_view(view))
2447 begin_x = view_split_begin_x(view->begin_x);
2448 err = browse_commit_tree(&tree_view, begin_x,
2449 s->selected_entry, s->in_repo_path, s->repo);
2452 if (view_is_parent_view(view)) {
2453 err = view_close_child(view);
2456 err = view_set_child(view, tree_view);
2458 view_close(tree_view);
2461 *focus_view = tree_view;
2462 view->child_focussed = 1;
2464 *new_view = tree_view;
2467 if (got_path_cmp(s->in_repo_path, "/",
2468 strlen(s->in_repo_path), 1) == 0)
2470 err = got_path_dirname(&parent_path, s->in_repo_path);
2473 err = stop_log_thread(s);
2478 lv = view_open(view->nlines, view->ncols,
2479 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2482 return got_error_from_errno("view_open");
2484 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2485 parent_path, s->log_branches);
2489 if (view_is_parent_view(view))
2492 view_set_child(view->parent, lv);
2497 err = stop_log_thread(s);
2500 lv = view_open(view->nlines, view->ncols,
2501 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2503 return got_error_from_errno("view_open");
2504 err = got_repo_match_object_id(&start_id, NULL,
2505 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2506 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2511 in_repo_path = strdup(s->in_repo_path);
2512 if (in_repo_path == NULL) {
2515 return got_error_from_errno("strdup");
2517 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2518 in_repo_path, s->log_branches);
2528 s->log_branches = !s->log_branches;
2529 err = stop_log_thread(s);
2532 lv = view_open(view->nlines, view->ncols,
2533 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2535 return got_error_from_errno("view_open");
2536 err = open_log_view(lv, s->start_id, s->repo,
2537 s->head_ref_name, s->in_repo_path, s->log_branches);
2546 if (view_is_parent_view(view))
2547 begin_x = view_split_begin_x(view->begin_x);
2548 ref_view = view_open(view->nlines, view->ncols,
2549 view->begin_y, begin_x, TOG_VIEW_REF);
2550 if (ref_view == NULL)
2551 return got_error_from_errno("view_open");
2552 err = open_ref_view(ref_view, s->repo);
2554 view_close(ref_view);
2557 if (view_is_parent_view(view)) {
2558 err = view_close_child(view);
2561 err = view_set_child(view, ref_view);
2563 view_close(ref_view);
2566 *focus_view = ref_view;
2567 view->child_focussed = 1;
2569 *new_view = ref_view;
2578 static const struct got_error *
2579 apply_unveil(const char *repo_path, const char *worktree_path)
2581 const struct got_error *error;
2584 if (unveil("gmon.out", "rwc") != 0)
2585 return got_error_from_errno2("unveil", "gmon.out");
2587 if (repo_path && unveil(repo_path, "r") != 0)
2588 return got_error_from_errno2("unveil", repo_path);
2590 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2591 return got_error_from_errno2("unveil", worktree_path);
2593 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2594 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2596 error = got_privsep_unveil_exec_helpers();
2600 if (unveil(NULL, NULL) != 0)
2601 return got_error_from_errno("unveil");
2611 halfdelay(1); /* Do fast refresh while initial view is loading. */
2614 intrflush(stdscr, FALSE);
2615 keypad(stdscr, TRUE);
2617 if (getenv("TOG_COLORS") != NULL) {
2619 use_default_colors();
2621 signal(SIGWINCH, tog_sigwinch);
2622 signal(SIGPIPE, tog_sigpipe);
2623 signal(SIGCONT, tog_sigcont);
2626 static const struct got_error *
2627 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2628 struct got_repository *repo, struct got_worktree *worktree)
2630 const struct got_error *err = NULL;
2633 *in_repo_path = strdup("/");
2634 if (*in_repo_path == NULL)
2635 return got_error_from_errno("strdup");
2640 const char *prefix = got_worktree_get_path_prefix(worktree);
2643 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2646 if (asprintf(in_repo_path, "%s%s%s", prefix,
2647 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2649 err = got_error_from_errno("asprintf");
2650 *in_repo_path = NULL;
2654 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2659 static const struct got_error *
2660 cmd_log(int argc, char *argv[])
2662 const struct got_error *error;
2663 struct got_repository *repo = NULL;
2664 struct got_worktree *worktree = NULL;
2665 struct got_object_id *start_id = NULL;
2666 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2667 char *start_commit = NULL, *head_ref_name = NULL;
2668 int ch, log_branches = 0;
2669 struct tog_view *view;
2672 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2677 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2683 start_commit = optarg;
2686 repo_path = realpath(optarg, NULL);
2687 if (repo_path == NULL)
2688 return got_error_from_errno2("realpath",
2703 cwd = getcwd(NULL, 0);
2705 return got_error_from_errno("getcwd");
2707 error = got_worktree_open(&worktree, cwd);
2708 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2711 if (repo_path == NULL) {
2714 strdup(got_worktree_get_repo_path(worktree));
2716 repo_path = strdup(cwd);
2718 if (repo_path == NULL) {
2719 error = got_error_from_errno("strdup");
2723 error = got_repo_open(&repo, repo_path, NULL);
2727 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2734 error = apply_unveil(got_repo_get_path(repo),
2735 worktree ? got_worktree_get_root_path(worktree) : NULL);
2739 if (start_commit == NULL)
2740 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2741 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2742 GOT_OBJ_TYPE_COMMIT, 1, repo);
2744 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2745 GOT_OBJ_TYPE_COMMIT, 1, repo);
2749 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2751 error = got_error_from_errno("view_open");
2755 head_ref_name = strdup(
2756 got_worktree_get_head_ref_name(worktree));
2757 if (head_ref_name == NULL) {
2758 error = got_error_from_errno("strdup");
2762 error = open_log_view(view, start_id, repo, head_ref_name,
2763 in_repo_path, log_branches);
2767 /* Release work tree lock. */
2768 got_worktree_close(worktree);
2771 error = view_loop(view);
2777 free(head_ref_name);
2779 got_repo_close(repo);
2781 got_worktree_close(worktree);
2789 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2790 "[-w] object1 object2\n", getprogname());
2795 parse_next_line(FILE *f, size_t *len)
2800 const char delim[3] = { '\0', '\0', '\0'};
2802 line = fparseln(f, &linelen, &lineno, delim, 0);
2809 match_line(const char *line, regex_t *regex, size_t nmatch,
2810 regmatch_t *regmatch)
2812 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2816 match_color(struct tog_colors *colors, const char *line)
2818 struct tog_color *tc = NULL;
2820 SIMPLEQ_FOREACH(tc, colors, entry) {
2821 if (match_line(line, &tc->regex, 0, NULL))
2828 static const struct got_error *
2829 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2830 WINDOW *window, regmatch_t *regmatch)
2832 const struct got_error *err = NULL;
2839 s = strndup(line, regmatch->rm_so);
2841 return got_error_from_errno("strndup");
2843 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2848 waddwstr(window, wline);
2855 s = strndup(line + regmatch->rm_so,
2856 regmatch->rm_eo - regmatch->rm_so);
2858 err = got_error_from_errno("strndup");
2862 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2867 wattr_on(window, A_STANDOUT, NULL);
2868 waddwstr(window, wline);
2869 wattr_off(window, A_STANDOUT, NULL);
2876 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2877 err = format_line(&wline, &width,
2878 line + regmatch->rm_eo, wlimit, col_tab_align);
2881 waddwstr(window, wline);
2889 static const struct got_error *
2890 draw_file(struct tog_view *view, const char *header)
2892 struct tog_diff_view_state *s = &view->state.diff;
2893 regmatch_t *regmatch = &view->regmatch;
2894 const struct got_error *err;
2897 struct tog_color *tc;
2901 int max_lines = view->nlines;
2902 int nlines = s->nlines;
2905 line_offset = s->line_offsets[s->first_displayed_line - 1];
2906 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2907 return got_error_from_errno("fseek");
2909 werase(view->window);
2912 if (asprintf(&line, "[%d/%d] %s",
2913 s->first_displayed_line - 1 + s->selected_line, nlines,
2915 return got_error_from_errno("asprintf");
2916 err = format_line(&wline, &width, line, view->ncols, 0);
2921 if (view_needs_focus_indication(view))
2922 wstandout(view->window);
2923 waddwstr(view->window, wline);
2926 if (view_needs_focus_indication(view))
2927 wstandend(view->window);
2928 if (width <= view->ncols - 1)
2929 waddch(view->window, '\n');
2937 while (max_lines > 0 && nprinted < max_lines) {
2938 line = parse_next_line(s->f, &len);
2944 tc = match_color(&s->colors, line);
2946 wattr_on(view->window,
2947 COLOR_PAIR(tc->colorpair), NULL);
2948 if (s->first_displayed_line + nprinted == s->matched_line &&
2949 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2950 err = add_matched_line(&width, line, view->ncols, 0,
2951 view->window, regmatch);
2957 err = format_line(&wline, &width, line, view->ncols, 0);
2962 waddwstr(view->window, wline);
2967 wattr_off(view->window,
2968 COLOR_PAIR(tc->colorpair), NULL);
2969 if (width <= view->ncols - 1)
2970 waddch(view->window, '\n');
2975 s->last_displayed_line = s->first_displayed_line +
2978 s->last_displayed_line = s->first_displayed_line;
2983 while (nprinted < view->nlines) {
2984 waddch(view->window, '\n');
2988 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2993 wstandout(view->window);
2994 waddwstr(view->window, wline);
2997 wstandend(view->window);
3004 get_datestr(time_t *time, char *datebuf)
3006 struct tm mytm, *tm;
3009 tm = gmtime_r(time, &mytm);
3012 s = asctime_r(tm, datebuf);
3015 p = strchr(s, '\n');
3021 static const struct got_error *
3022 get_changed_paths(struct got_pathlist_head *paths,
3023 struct got_commit_object *commit, struct got_repository *repo)
3025 const struct got_error *err = NULL;
3026 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3027 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3028 struct got_object_qid *qid;
3030 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3032 struct got_commit_object *pcommit;
3033 err = got_object_open_as_commit(&pcommit, repo,
3038 tree_id1 = got_object_commit_get_tree_id(pcommit);
3039 got_object_commit_close(pcommit);
3044 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3049 tree_id2 = got_object_commit_get_tree_id(commit);
3050 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3054 err = got_diff_tree(tree1, tree2, "", "", repo,
3055 got_diff_tree_collect_changed_paths, paths, 0);
3058 got_object_tree_close(tree1);
3060 got_object_tree_close(tree2);
3064 static const struct got_error *
3065 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3069 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3071 return got_error_from_errno("reallocarray");
3073 (*line_offsets)[*nlines] = off;
3078 static const struct got_error *
3079 write_commit_info(off_t **line_offsets, size_t *nlines,
3080 struct got_object_id *commit_id, struct got_reflist_head *refs,
3081 struct got_repository *repo, FILE *outfile)
3083 const struct got_error *err = NULL;
3084 char datebuf[26], *datestr;
3085 struct got_commit_object *commit;
3086 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3087 time_t committer_time;
3088 const char *author, *committer;
3089 char *refs_str = NULL;
3090 struct got_pathlist_head changed_paths;
3091 struct got_pathlist_entry *pe;
3095 TAILQ_INIT(&changed_paths);
3098 err = build_refs_str(&refs_str, refs, commit_id, repo);
3103 err = got_object_open_as_commit(&commit, repo, commit_id);
3107 err = got_object_id_str(&id_str, commit_id);
3109 err = got_error_from_errno("got_object_id_str");
3113 err = add_line_offset(line_offsets, nlines, 0);
3117 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3118 refs_str ? refs_str : "", refs_str ? ")" : "");
3120 err = got_error_from_errno("fprintf");
3124 err = add_line_offset(line_offsets, nlines, outoff);
3128 n = fprintf(outfile, "from: %s\n",
3129 got_object_commit_get_author(commit));
3131 err = got_error_from_errno("fprintf");
3135 err = add_line_offset(line_offsets, nlines, outoff);
3139 committer_time = got_object_commit_get_committer_time(commit);
3140 datestr = get_datestr(&committer_time, datebuf);
3142 n = fprintf(outfile, "date: %s UTC\n", datestr);
3144 err = got_error_from_errno("fprintf");
3148 err = add_line_offset(line_offsets, nlines, outoff);
3152 author = got_object_commit_get_author(commit);
3153 committer = got_object_commit_get_committer(commit);
3154 if (strcmp(author, committer) != 0) {
3155 n = fprintf(outfile, "via: %s\n", committer);
3157 err = got_error_from_errno("fprintf");
3161 err = add_line_offset(line_offsets, nlines, outoff);
3165 err = got_object_commit_get_logmsg(&logmsg, commit);
3169 while ((line = strsep(&s, "\n")) != NULL) {
3170 n = fprintf(outfile, "%s\n", line);
3172 err = got_error_from_errno("fprintf");
3176 err = add_line_offset(line_offsets, nlines, outoff);
3181 err = get_changed_paths(&changed_paths, commit, repo);
3184 TAILQ_FOREACH(pe, &changed_paths, entry) {
3185 struct got_diff_changed_path *cp = pe->data;
3186 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3188 err = got_error_from_errno("fprintf");
3192 err = add_line_offset(line_offsets, nlines, outoff);
3195 free((char *)pe->path);
3199 fputc('\n', outfile);
3201 err = add_line_offset(line_offsets, nlines, outoff);
3203 got_pathlist_free(&changed_paths);
3207 got_object_commit_close(commit);
3209 free(*line_offsets);
3210 *line_offsets = NULL;
3216 static const struct got_error *
3217 create_diff(struct tog_diff_view_state *s)
3219 const struct got_error *err = NULL;
3223 free(s->line_offsets);
3224 s->line_offsets = malloc(sizeof(off_t));
3225 if (s->line_offsets == NULL)
3226 return got_error_from_errno("malloc");
3231 err = got_error_from_errno("got_opentemp");
3234 if (s->f && fclose(s->f) != 0) {
3235 err = got_error_from_errno("fclose");
3241 err = got_object_get_type(&obj_type, s->repo, s->id1);
3243 err = got_object_get_type(&obj_type, s->repo, s->id2);
3248 case GOT_OBJ_TYPE_BLOB:
3249 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3250 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3251 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3253 case GOT_OBJ_TYPE_TREE:
3254 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3255 s->id1, s->id2, "", "", s->diff_context,
3256 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3258 case GOT_OBJ_TYPE_COMMIT: {
3259 const struct got_object_id_queue *parent_ids;
3260 struct got_object_qid *pid;
3261 struct got_commit_object *commit2;
3263 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3266 /* Show commit info if we're diffing to a parent/root commit. */
3267 if (s->id1 == NULL) {
3268 err = write_commit_info(&s->line_offsets, &s->nlines,
3269 s->id2, &s->refs, s->repo, s->f);
3273 parent_ids = got_object_commit_get_parent_ids(commit2);
3274 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3275 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3276 err = write_commit_info(
3277 &s->line_offsets, &s->nlines,
3278 s->id2, &s->refs, s->repo, s->f);
3285 got_object_commit_close(commit2);
3287 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3288 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3289 s->force_text_diff, s->repo, s->f);
3293 err = got_error(GOT_ERR_OBJ_TYPE);
3299 if (s->f && fflush(s->f) != 0 && err == NULL)
3300 err = got_error_from_errno("fflush");
3305 diff_view_indicate_progress(struct tog_view *view)
3307 mvwaddstr(view->window, 0, 0, "diffing...");
3312 static const struct got_error *
3313 search_start_diff_view(struct tog_view *view)
3315 struct tog_diff_view_state *s = &view->state.diff;
3317 s->matched_line = 0;
3321 static const struct got_error *
3322 search_next_diff_view(struct tog_view *view)
3324 struct tog_diff_view_state *s = &view->state.diff;
3327 if (!view->searching) {
3328 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3332 if (s->matched_line) {
3333 if (view->searching == TOG_SEARCH_FORWARD)
3334 lineno = s->matched_line + 1;
3336 lineno = s->matched_line - 1;
3338 if (view->searching == TOG_SEARCH_FORWARD)
3349 if (lineno <= 0 || lineno > s->nlines) {
3350 if (s->matched_line == 0) {
3351 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3356 if (view->searching == TOG_SEARCH_FORWARD)
3362 offset = s->line_offsets[lineno - 1];
3363 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3365 return got_error_from_errno("fseeko");
3368 line = parse_next_line(s->f, &len);
3370 match_line(line, &view->regex, 1, &view->regmatch)) {
3371 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3372 s->matched_line = lineno;
3377 if (view->searching == TOG_SEARCH_FORWARD)
3383 if (s->matched_line) {
3384 s->first_displayed_line = s->matched_line;
3385 s->selected_line = 1;
3391 static const struct got_error *
3392 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3393 struct got_object_id *id2, const char *label1, const char *label2,
3394 int diff_context, int ignore_whitespace, int force_text_diff,
3395 struct tog_view *log_view, struct got_repository *repo)
3397 const struct got_error *err;
3398 struct tog_diff_view_state *s = &view->state.diff;
3400 SIMPLEQ_INIT(&s->refs);
3402 if (id1 != NULL && id2 != NULL) {
3404 err = got_object_get_type(&type1, repo, id1);
3407 err = got_object_get_type(&type2, repo, id2);
3412 return got_error(GOT_ERR_OBJ_TYPE);
3414 s->first_displayed_line = 1;
3415 s->last_displayed_line = view->nlines;
3416 s->selected_line = 1;
3424 s->id1 = got_object_id_dup(id1);
3426 return got_error_from_errno("got_object_id_dup");
3430 s->id2 = got_object_id_dup(id2);
3431 if (s->id2 == NULL) {
3434 return got_error_from_errno("got_object_id_dup");
3437 s->first_displayed_line = 1;
3438 s->last_displayed_line = view->nlines;
3439 s->diff_context = diff_context;
3440 s->ignore_whitespace = ignore_whitespace;
3441 s->force_text_diff = force_text_diff;
3442 s->log_view = log_view;
3445 SIMPLEQ_INIT(&s->colors);
3446 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3447 err = add_color(&s->colors,
3448 "^-", TOG_COLOR_DIFF_MINUS,
3449 get_color_value("TOG_COLOR_DIFF_MINUS"));
3452 err = add_color(&s->colors, "^\\+",
3453 TOG_COLOR_DIFF_PLUS,
3454 get_color_value("TOG_COLOR_DIFF_PLUS"));
3456 free_colors(&s->colors);
3459 err = add_color(&s->colors,
3460 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3461 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3463 free_colors(&s->colors);
3467 err = add_color(&s->colors,
3468 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3469 TOG_COLOR_DIFF_META,
3470 get_color_value("TOG_COLOR_DIFF_META"));
3472 free_colors(&s->colors);
3476 err = add_color(&s->colors,
3477 "^(from|via): ", TOG_COLOR_AUTHOR,
3478 get_color_value("TOG_COLOR_AUTHOR"));
3480 free_colors(&s->colors);
3484 err = add_color(&s->colors,
3485 "^date: ", TOG_COLOR_DATE,
3486 get_color_value("TOG_COLOR_DATE"));
3488 free_colors(&s->colors);
3493 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3499 free_colors(&s->colors);
3503 if (log_view && view_is_splitscreen(view))
3504 show_log_view(log_view); /* draw vborder */
3505 diff_view_indicate_progress(view);
3507 s->line_offsets = NULL;
3509 err = create_diff(s);
3515 free_colors(&s->colors);
3516 got_ref_list_free(&s->refs);
3520 view->show = show_diff_view;
3521 view->input = input_diff_view;
3522 view->close = close_diff_view;
3523 view->search_start = search_start_diff_view;
3524 view->search_next = search_next_diff_view;
3529 static const struct got_error *
3530 close_diff_view(struct tog_view *view)
3532 const struct got_error *err = NULL;
3533 struct tog_diff_view_state *s = &view->state.diff;
3539 if (s->f && fclose(s->f) == EOF)
3540 err = got_error_from_errno("fclose");
3541 free_colors(&s->colors);
3542 free(s->line_offsets);
3543 s->line_offsets = NULL;
3545 got_ref_list_free(&s->refs);
3549 static const struct got_error *
3550 show_diff_view(struct tog_view *view)
3552 const struct got_error *err;
3553 struct tog_diff_view_state *s = &view->state.diff;
3554 char *id_str1 = NULL, *id_str2, *header;
3555 const char *label1, *label2;
3558 err = got_object_id_str(&id_str1, s->id1);
3561 label1 = s->label1 ? : id_str1;
3563 label1 = "/dev/null";
3565 err = got_object_id_str(&id_str2, s->id2);
3568 label2 = s->label2 ? : id_str2;
3570 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3571 err = got_error_from_errno("asprintf");
3579 return draw_file(view, header);
3582 static const struct got_error *
3583 set_selected_commit(struct tog_diff_view_state *s,
3584 struct commit_queue_entry *entry)
3586 const struct got_error *err;
3587 const struct got_object_id_queue *parent_ids;
3588 struct got_commit_object *selected_commit;
3589 struct got_object_qid *pid;
3592 s->id2 = got_object_id_dup(entry->id);
3594 return got_error_from_errno("got_object_id_dup");
3596 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3599 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3601 pid = SIMPLEQ_FIRST(parent_ids);
3602 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3603 got_object_commit_close(selected_commit);
3607 static const struct got_error *
3608 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3609 struct tog_view **focus_view, struct tog_view *view, int ch)
3611 const struct got_error *err = NULL;
3612 struct tog_diff_view_state *s = &view->state.diff;
3613 struct tog_log_view_state *ls;
3614 struct commit_queue_entry *entry;
3621 s->force_text_diff = !s->force_text_diff;
3623 s->ignore_whitespace = !s->ignore_whitespace;
3624 wclear(view->window);
3625 s->first_displayed_line = 1;
3626 s->last_displayed_line = view->nlines;
3627 diff_view_indicate_progress(view);
3628 err = create_diff(s);
3632 if (s->first_displayed_line > 1)
3633 s->first_displayed_line--;
3637 if (s->first_displayed_line == 1)
3640 while (i++ < view->nlines - 1 &&
3641 s->first_displayed_line > 1)
3642 s->first_displayed_line--;
3647 s->first_displayed_line++;
3655 while (!s->eof && i++ < view->nlines - 1) {
3657 line = parse_next_line(s->f, NULL);
3658 s->first_displayed_line++;
3664 if (s->diff_context > 0) {
3666 diff_view_indicate_progress(view);
3667 err = create_diff(s);
3668 if (s->first_displayed_line + view->nlines - 1 >
3670 s->first_displayed_line = 1;
3671 s->last_displayed_line = view->nlines;
3676 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3678 diff_view_indicate_progress(view);
3679 err = create_diff(s);
3684 if (s->log_view == NULL)
3686 ls = &s->log_view->state.log;
3687 entry = TAILQ_PREV(ls->selected_entry,
3688 commit_queue_head, entry);
3692 err = input_log_view(NULL, NULL, NULL, s->log_view,
3697 err = set_selected_commit(s, entry);
3701 s->first_displayed_line = 1;
3702 s->last_displayed_line = view->nlines;
3704 diff_view_indicate_progress(view);
3705 err = create_diff(s);
3709 if (s->log_view == NULL)
3711 ls = &s->log_view->state.log;
3713 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3714 ls->thread_args.commits_needed++;
3715 err = trigger_log_thread(s->log_view, 1);
3719 err = input_log_view(NULL, NULL, NULL, s->log_view,
3724 entry = TAILQ_NEXT(ls->selected_entry, entry);
3728 err = set_selected_commit(s, entry);
3732 s->first_displayed_line = 1;
3733 s->last_displayed_line = view->nlines;
3735 diff_view_indicate_progress(view);
3736 err = create_diff(s);
3745 static const struct got_error *
3746 cmd_diff(int argc, char *argv[])
3748 const struct got_error *error = NULL;
3749 struct got_repository *repo = NULL;
3750 struct got_worktree *worktree = NULL;
3751 struct got_object_id *id1 = NULL, *id2 = NULL;
3752 char *repo_path = NULL, *cwd = NULL;
3753 char *id_str1 = NULL, *id_str2 = NULL;
3754 char *label1 = NULL, *label2 = NULL;
3755 int diff_context = 3, ignore_whitespace = 0;
3756 int ch, force_text_diff = 0;
3758 struct tog_view *view;
3761 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3765 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3768 force_text_diff = 1;
3771 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3774 err(1, "-C option %s", errstr);
3777 repo_path = realpath(optarg, NULL);
3778 if (repo_path == NULL)
3779 return got_error_from_errno2("realpath",
3781 got_path_strip_trailing_slashes(repo_path);
3784 ignore_whitespace = 1;
3796 usage_diff(); /* TODO show local worktree changes */
3797 } else if (argc == 2) {
3803 cwd = getcwd(NULL, 0);
3805 return got_error_from_errno("getcwd");
3807 error = got_worktree_open(&worktree, cwd);
3808 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3811 if (repo_path == NULL) {
3814 strdup(got_worktree_get_repo_path(worktree));
3816 repo_path = strdup(cwd);
3818 if (repo_path == NULL) {
3819 error = got_error_from_errno("strdup");
3823 error = got_repo_open(&repo, repo_path, NULL);
3829 error = apply_unveil(got_repo_get_path(repo), NULL);
3833 error = got_repo_match_object_id(&id1, &label1, id_str1,
3834 GOT_OBJ_TYPE_ANY, 1, repo);
3838 error = got_repo_match_object_id(&id2, &label2, id_str2,
3839 GOT_OBJ_TYPE_ANY, 1, repo);
3843 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3845 error = got_error_from_errno("view_open");
3848 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3849 ignore_whitespace, force_text_diff, NULL, repo);
3852 error = view_loop(view);
3859 got_repo_close(repo);
3861 got_worktree_close(worktree);
3869 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3874 struct tog_blame_line {
3876 struct got_object_id *id;
3879 static const struct got_error *
3880 draw_blame(struct tog_view *view)
3882 struct tog_blame_view_state *s = &view->state.blame;
3883 struct tog_blame *blame = &s->blame;
3884 regmatch_t *regmatch = &view->regmatch;
3885 const struct got_error *err;
3886 int lineno = 0, nprinted = 0;
3891 struct tog_blame_line *blame_line;
3892 struct got_object_id *prev_id = NULL;
3894 struct tog_color *tc;
3896 err = got_object_id_str(&id_str, s->blamed_commit->id);
3901 werase(view->window);
3903 if (asprintf(&line, "commit %s", id_str) == -1) {
3904 err = got_error_from_errno("asprintf");
3909 err = format_line(&wline, &width, line, view->ncols, 0);
3914 if (view_needs_focus_indication(view))
3915 wstandout(view->window);
3916 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3918 wattr_on(view->window,
3919 COLOR_PAIR(tc->colorpair), NULL);
3920 waddwstr(view->window, wline);
3922 wattr_off(view->window,
3923 COLOR_PAIR(tc->colorpair), NULL);
3924 if (view_needs_focus_indication(view))
3925 wstandend(view->window);
3928 if (width < view->ncols - 1)
3929 waddch(view->window, '\n');
3931 if (asprintf(&line, "[%d/%d] %s%s",
3932 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3933 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3935 return got_error_from_errno("asprintf");
3938 err = format_line(&wline, &width, line, view->ncols, 0);
3943 waddwstr(view->window, wline);
3946 if (width < view->ncols - 1)
3947 waddch(view->window, '\n');
3950 while (nprinted < view->nlines - 2) {
3951 line = parse_next_line(blame->f, &len);
3956 if (++lineno < s->first_displayed_line) {
3961 if (view->focussed && nprinted == s->selected_line - 1)
3962 wstandout(view->window);
3964 if (blame->nlines > 0) {
3965 blame_line = &blame->lines[lineno - 1];
3966 if (blame_line->annotated && prev_id &&
3967 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3969 nprinted == s->selected_line - 1)) {
3970 waddstr(view->window, " ");
3971 } else if (blame_line->annotated) {
3973 err = got_object_id_str(&id_str, blame_line->id);
3978 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3980 wattr_on(view->window,
3981 COLOR_PAIR(tc->colorpair), NULL);
3982 wprintw(view->window, "%.8s", id_str);
3984 wattr_off(view->window,
3985 COLOR_PAIR(tc->colorpair), NULL);
3987 prev_id = blame_line->id;
3989 waddstr(view->window, "........");
3993 waddstr(view->window, "........");
3997 if (view->focussed && nprinted == s->selected_line - 1)
3998 wstandend(view->window);
3999 waddstr(view->window, " ");
4001 if (view->ncols <= 9) {
4003 wline = wcsdup(L"");
4004 if (wline == NULL) {
4005 err = got_error_from_errno("wcsdup");
4009 } else if (s->first_displayed_line + nprinted ==
4011 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4012 err = add_matched_line(&width, line, view->ncols - 9, 9,
4013 view->window, regmatch);
4020 err = format_line(&wline, &width, line,
4021 view->ncols - 9, 9);
4022 waddwstr(view->window, wline);
4028 if (width <= view->ncols - 1)
4029 waddch(view->window, '\n');
4030 if (++nprinted == 1)
4031 s->first_displayed_line = lineno;
4034 s->last_displayed_line = lineno;
4041 static const struct got_error *
4042 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4044 const struct got_error *err = NULL;
4045 struct tog_blame_cb_args *a = arg;
4046 struct tog_blame_line *line;
4049 if (nlines != a->nlines ||
4050 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4051 return got_error(GOT_ERR_RANGE);
4053 errcode = pthread_mutex_lock(&tog_mutex);
4055 return got_error_set_errno(errcode, "pthread_mutex_lock");
4057 if (*a->quit) { /* user has quit the blame view */
4058 err = got_error(GOT_ERR_ITER_COMPLETED);
4063 goto done; /* no change in this commit */
4065 line = &a->lines[lineno - 1];
4066 if (line->annotated)
4069 line->id = got_object_id_dup(id);
4070 if (line->id == NULL) {
4071 err = got_error_from_errno("got_object_id_dup");
4074 line->annotated = 1;
4076 errcode = pthread_mutex_unlock(&tog_mutex);
4078 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4083 blame_thread(void *arg)
4085 const struct got_error *err;
4086 struct tog_blame_thread_args *ta = arg;
4087 struct tog_blame_cb_args *a = ta->cb_args;
4090 err = block_signals_used_by_main_thread();
4094 err = got_blame(ta->path, a->commit_id, ta->repo,
4095 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4096 if (err && err->code == GOT_ERR_CANCELLED)
4099 errcode = pthread_mutex_lock(&tog_mutex);
4101 return (void *)got_error_set_errno(errcode,
4102 "pthread_mutex_lock");
4104 got_repo_close(ta->repo);
4108 errcode = pthread_mutex_unlock(&tog_mutex);
4109 if (errcode && err == NULL)
4110 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4115 static struct got_object_id *
4116 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4117 int first_displayed_line, int selected_line)
4119 struct tog_blame_line *line;
4124 line = &lines[first_displayed_line - 1 + selected_line - 1];
4125 if (!line->annotated)
4131 static const struct got_error *
4132 stop_blame(struct tog_blame *blame)
4134 const struct got_error *err = NULL;
4137 if (blame->thread) {
4139 errcode = pthread_mutex_unlock(&tog_mutex);
4141 return got_error_set_errno(errcode,
4142 "pthread_mutex_unlock");
4143 errcode = pthread_join(blame->thread, (void **)&err);
4145 return got_error_set_errno(errcode, "pthread_join");
4146 errcode = pthread_mutex_lock(&tog_mutex);
4148 return got_error_set_errno(errcode,
4149 "pthread_mutex_lock");
4150 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4152 blame->thread = NULL;
4154 if (blame->thread_args.repo) {
4155 got_repo_close(blame->thread_args.repo);
4156 blame->thread_args.repo = NULL;
4159 if (fclose(blame->f) != 0 && err == NULL)
4160 err = got_error_from_errno("fclose");
4164 for (i = 0; i < blame->nlines; i++)
4165 free(blame->lines[i].id);
4167 blame->lines = NULL;
4169 free(blame->cb_args.commit_id);
4170 blame->cb_args.commit_id = NULL;
4175 static const struct got_error *
4176 cancel_blame_view(void *arg)
4178 const struct got_error *err = NULL;
4182 errcode = pthread_mutex_lock(&tog_mutex);
4184 return got_error_set_errno(errcode,
4185 "pthread_mutex_unlock");
4188 err = got_error(GOT_ERR_CANCELLED);
4190 errcode = pthread_mutex_unlock(&tog_mutex);
4192 return got_error_set_errno(errcode,
4193 "pthread_mutex_lock");
4198 static const struct got_error *
4199 run_blame(struct tog_view *view)
4201 struct tog_blame_view_state *s = &view->state.blame;
4202 struct tog_blame *blame = &s->blame;
4203 const struct got_error *err = NULL;
4204 struct got_blob_object *blob = NULL;
4205 struct got_repository *thread_repo = NULL;
4206 struct got_object_id *obj_id = NULL;
4209 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4214 err = got_object_get_type(&obj_type, s->repo, obj_id);
4218 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4219 err = got_error(GOT_ERR_OBJ_TYPE);
4223 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4226 blame->f = got_opentemp();
4227 if (blame->f == NULL) {
4228 err = got_error_from_errno("got_opentemp");
4231 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4232 &blame->line_offsets, blame->f, blob);
4233 if (err || blame->nlines == 0)
4236 /* Don't include \n at EOF in the blame line count. */
4237 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4240 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4241 if (blame->lines == NULL) {
4242 err = got_error_from_errno("calloc");
4246 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4250 blame->cb_args.view = view;
4251 blame->cb_args.lines = blame->lines;
4252 blame->cb_args.nlines = blame->nlines;
4253 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4254 if (blame->cb_args.commit_id == NULL) {
4255 err = got_error_from_errno("got_object_id_dup");
4258 blame->cb_args.quit = &s->done;
4260 blame->thread_args.path = s->path;
4261 blame->thread_args.repo = thread_repo;
4262 blame->thread_args.cb_args = &blame->cb_args;
4263 blame->thread_args.complete = &s->blame_complete;
4264 blame->thread_args.cancel_cb = cancel_blame_view;
4265 blame->thread_args.cancel_arg = &s->done;
4266 s->blame_complete = 0;
4270 got_object_blob_close(blob);
4277 static const struct got_error *
4278 open_blame_view(struct tog_view *view, char *path,
4279 struct got_object_id *commit_id, struct got_repository *repo)
4281 const struct got_error *err = NULL;
4282 struct tog_blame_view_state *s = &view->state.blame;
4284 SIMPLEQ_INIT(&s->blamed_commits);
4286 s->path = strdup(path);
4287 if (s->path == NULL)
4288 return got_error_from_errno("strdup");
4290 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4296 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4297 s->first_displayed_line = 1;
4298 s->last_displayed_line = view->nlines;
4299 s->selected_line = 1;
4300 s->blame_complete = 0;
4302 s->commit_id = commit_id;
4303 memset(&s->blame, 0, sizeof(s->blame));
4305 SIMPLEQ_INIT(&s->colors);
4306 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4307 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4308 get_color_value("TOG_COLOR_COMMIT"));
4313 view->show = show_blame_view;
4314 view->input = input_blame_view;
4315 view->close = close_blame_view;
4316 view->search_start = search_start_blame_view;
4317 view->search_next = search_next_blame_view;
4319 return run_blame(view);
4322 static const struct got_error *
4323 close_blame_view(struct tog_view *view)
4325 const struct got_error *err = NULL;
4326 struct tog_blame_view_state *s = &view->state.blame;
4328 if (s->blame.thread)
4329 err = stop_blame(&s->blame);
4331 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4332 struct got_object_qid *blamed_commit;
4333 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4334 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4335 got_object_qid_free(blamed_commit);
4339 free_colors(&s->colors);
4344 static const struct got_error *
4345 search_start_blame_view(struct tog_view *view)
4347 struct tog_blame_view_state *s = &view->state.blame;
4349 s->matched_line = 0;
4353 static const struct got_error *
4354 search_next_blame_view(struct tog_view *view)
4356 struct tog_blame_view_state *s = &view->state.blame;
4359 if (!view->searching) {
4360 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4364 if (s->matched_line) {
4365 if (view->searching == TOG_SEARCH_FORWARD)
4366 lineno = s->matched_line + 1;
4368 lineno = s->matched_line - 1;
4370 if (view->searching == TOG_SEARCH_FORWARD)
4373 lineno = s->blame.nlines;
4381 if (lineno <= 0 || lineno > s->blame.nlines) {
4382 if (s->matched_line == 0) {
4383 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4388 if (view->searching == TOG_SEARCH_FORWARD)
4391 lineno = s->blame.nlines;
4394 offset = s->blame.line_offsets[lineno - 1];
4395 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4397 return got_error_from_errno("fseeko");
4400 line = parse_next_line(s->blame.f, &len);
4402 match_line(line, &view->regex, 1, &view->regmatch)) {
4403 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4404 s->matched_line = lineno;
4409 if (view->searching == TOG_SEARCH_FORWARD)
4415 if (s->matched_line) {
4416 s->first_displayed_line = s->matched_line;
4417 s->selected_line = 1;
4423 static const struct got_error *
4424 show_blame_view(struct tog_view *view)
4426 const struct got_error *err = NULL;
4427 struct tog_blame_view_state *s = &view->state.blame;
4430 if (s->blame.thread == NULL) {
4431 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4432 &s->blame.thread_args);
4434 return got_error_set_errno(errcode, "pthread_create");
4436 halfdelay(1); /* fast refresh while annotating */
4439 if (s->blame_complete)
4440 halfdelay(10); /* disable fast refresh */
4442 err = draw_blame(view);
4448 static const struct got_error *
4449 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4450 struct tog_view **focus_view, struct tog_view *view, int ch)
4452 const struct got_error *err = NULL, *thread_err = NULL;
4453 struct tog_view *diff_view;
4454 struct tog_blame_view_state *s = &view->state.blame;
4463 if (s->selected_line > 1)
4465 else if (s->selected_line == 1 &&
4466 s->first_displayed_line > 1)
4467 s->first_displayed_line--;
4471 if (s->first_displayed_line == 1) {
4472 s->selected_line = 1;
4475 if (s->first_displayed_line > view->nlines - 2)
4476 s->first_displayed_line -=
4479 s->first_displayed_line = 1;
4483 if (s->selected_line < view->nlines - 2 &&
4484 s->first_displayed_line +
4485 s->selected_line <= s->blame.nlines)
4487 else if (s->last_displayed_line <
4489 s->first_displayed_line++;
4493 struct got_object_id *id = NULL;
4494 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4495 s->first_displayed_line, s->selected_line);
4499 struct got_commit_object *commit;
4500 struct got_object_qid *pid;
4501 struct got_object_id *blob_id = NULL;
4503 err = got_object_open_as_commit(&commit,
4507 pid = SIMPLEQ_FIRST(
4508 got_object_commit_get_parent_ids(commit));
4510 got_object_commit_close(commit);
4513 /* Check if path history ends here. */
4514 err = got_object_id_by_path(&blob_id, s->repo,
4517 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4519 got_object_commit_close(commit);
4522 err = got_object_get_type(&obj_type, s->repo,
4525 /* Can't blame non-blob type objects. */
4526 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4527 got_object_commit_close(commit);
4530 err = got_object_qid_alloc(&s->blamed_commit,
4532 got_object_commit_close(commit);
4534 if (got_object_id_cmp(id,
4535 s->blamed_commit->id) == 0)
4537 err = got_object_qid_alloc(&s->blamed_commit,
4543 thread_err = stop_blame(&s->blame);
4547 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4548 s->blamed_commit, entry);
4549 err = run_blame(view);
4555 struct got_object_qid *first;
4556 first = SIMPLEQ_FIRST(&s->blamed_commits);
4557 if (!got_object_id_cmp(first->id, s->commit_id))
4560 thread_err = stop_blame(&s->blame);
4564 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4565 got_object_qid_free(s->blamed_commit);
4567 SIMPLEQ_FIRST(&s->blamed_commits);
4568 err = run_blame(view);
4575 struct got_object_id *id = NULL;
4576 struct got_object_qid *pid;
4577 struct got_commit_object *commit = NULL;
4578 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4579 s->first_displayed_line, s->selected_line);
4582 err = got_object_open_as_commit(&commit, s->repo, id);
4585 pid = SIMPLEQ_FIRST(
4586 got_object_commit_get_parent_ids(commit));
4587 if (view_is_parent_view(view))
4588 begin_x = view_split_begin_x(view->begin_x);
4589 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4590 if (diff_view == NULL) {
4591 got_object_commit_close(commit);
4592 err = got_error_from_errno("view_open");
4595 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4596 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4597 got_object_commit_close(commit);
4599 view_close(diff_view);
4602 if (view_is_parent_view(view)) {
4603 err = view_close_child(view);
4606 err = view_set_child(view, diff_view);
4608 view_close(diff_view);
4611 *focus_view = diff_view;
4612 view->child_focussed = 1;
4614 *new_view = diff_view;
4622 if (s->last_displayed_line >= s->blame.nlines &&
4623 s->selected_line >= MIN(s->blame.nlines,
4624 view->nlines - 2)) {
4627 if (s->last_displayed_line >= s->blame.nlines &&
4628 s->selected_line < view->nlines - 2) {
4629 s->selected_line = MIN(s->blame.nlines,
4633 if (s->last_displayed_line + view->nlines - 2
4635 s->first_displayed_line +=
4638 s->first_displayed_line =
4643 if (s->selected_line > view->nlines - 2) {
4644 s->selected_line = MIN(s->blame.nlines,
4651 return thread_err ? thread_err : err;
4654 static const struct got_error *
4655 cmd_blame(int argc, char *argv[])
4657 const struct got_error *error;
4658 struct got_repository *repo = NULL;
4659 struct got_worktree *worktree = NULL;
4660 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4661 char *link_target = NULL;
4662 struct got_object_id *commit_id = NULL;
4663 char *commit_id_str = NULL;
4665 struct tog_view *view;
4668 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4673 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4676 commit_id_str = optarg;
4679 repo_path = realpath(optarg, NULL);
4680 if (repo_path == NULL)
4681 return got_error_from_errno2("realpath",
4696 cwd = getcwd(NULL, 0);
4698 return got_error_from_errno("getcwd");
4700 error = got_worktree_open(&worktree, cwd);
4701 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4704 if (repo_path == NULL) {
4707 strdup(got_worktree_get_repo_path(worktree));
4709 repo_path = strdup(cwd);
4711 if (repo_path == NULL) {
4712 error = got_error_from_errno("strdup");
4716 error = got_repo_open(&repo, repo_path, NULL);
4720 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4727 error = apply_unveil(got_repo_get_path(repo), NULL);
4731 if (commit_id_str == NULL) {
4732 struct got_reference *head_ref;
4733 error = got_ref_open(&head_ref, repo, worktree ?
4734 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4737 error = got_ref_resolve(&commit_id, repo, head_ref);
4738 got_ref_close(head_ref);
4740 error = got_repo_match_object_id(&commit_id, NULL,
4741 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4746 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4748 error = got_error_from_errno("view_open");
4752 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4757 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4762 /* Release work tree lock. */
4763 got_worktree_close(worktree);
4766 error = view_loop(view);
4774 got_worktree_close(worktree);
4776 got_repo_close(repo);
4780 static const struct got_error *
4781 draw_tree_entries(struct tog_view *view, const char *parent_path)
4783 struct tog_tree_view_state *s = &view->state.tree;
4784 const struct got_error *err = NULL;
4785 struct got_tree_entry *te;
4787 struct tog_color *tc;
4788 int width, n, i, nentries;
4789 int limit = view->nlines;
4793 werase(view->window);
4798 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4801 if (view_needs_focus_indication(view))
4802 wstandout(view->window);
4803 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4805 wattr_on(view->window,
4806 COLOR_PAIR(tc->colorpair), NULL);
4807 waddwstr(view->window, wline);
4809 wattr_off(view->window,
4810 COLOR_PAIR(tc->colorpair), NULL);
4811 if (view_needs_focus_indication(view))
4812 wstandend(view->window);
4815 if (width < view->ncols - 1)
4816 waddch(view->window, '\n');
4819 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4822 waddwstr(view->window, wline);
4825 if (width < view->ncols - 1)
4826 waddch(view->window, '\n');
4829 waddch(view->window, '\n');
4833 if (s->first_displayed_entry == NULL) {
4834 te = got_object_tree_get_first_entry(s->tree);
4835 if (s->selected == 0) {
4837 wstandout(view->window);
4838 s->selected_entry = NULL;
4840 waddstr(view->window, " ..\n"); /* parent directory */
4841 if (s->selected == 0 && view->focussed)
4842 wstandend(view->window);
4849 te = s->first_displayed_entry;
4852 nentries = got_object_tree_get_nentries(s->tree);
4853 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4854 char *line = NULL, *id_str = NULL, *link_target = NULL;
4855 const char *modestr = "";
4858 te = got_object_tree_get_entry(s->tree, i);
4859 mode = got_tree_entry_get_mode(te);
4862 err = got_object_id_str(&id_str,
4863 got_tree_entry_get_id(te));
4865 return got_error_from_errno(
4866 "got_object_id_str");
4868 if (got_object_tree_entry_is_submodule(te))
4870 else if (S_ISLNK(mode)) {
4873 err = got_tree_entry_get_symlink_target(&link_target,
4879 for (i = 0; i < strlen(link_target); i++) {
4880 if (!isprint((unsigned char)link_target[i]))
4881 link_target[i] = '?';
4885 else if (S_ISDIR(mode))
4887 else if (mode & S_IXUSR)
4889 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4890 got_tree_entry_get_name(te), modestr,
4891 link_target ? " -> ": "",
4892 link_target ? link_target : "") == -1) {
4895 return got_error_from_errno("asprintf");
4899 err = format_line(&wline, &width, line, view->ncols, 0);
4904 if (n == s->selected) {
4906 wstandout(view->window);
4907 s->selected_entry = te;
4909 tc = match_color(&s->colors, line);
4911 wattr_on(view->window,
4912 COLOR_PAIR(tc->colorpair), NULL);
4913 waddwstr(view->window, wline);
4915 wattr_off(view->window,
4916 COLOR_PAIR(tc->colorpair), NULL);
4917 if (width < view->ncols - 1)
4918 waddch(view->window, '\n');
4919 if (n == s->selected && view->focussed)
4920 wstandend(view->window);
4926 s->last_displayed_entry = te;
4935 tree_scroll_up(struct tog_view *view, int maxscroll)
4937 struct tog_tree_view_state *s = &view->state.tree;
4938 struct got_tree_entry *te;
4939 int isroot = s->tree == s->root;
4942 if (s->first_displayed_entry == NULL)
4945 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4946 while (i++ < maxscroll) {
4949 s->first_displayed_entry = NULL;
4952 s->first_displayed_entry = te;
4953 te = got_tree_entry_get_prev(s->tree, te);
4958 tree_scroll_down(struct tog_view *view, int maxscroll)
4960 struct tog_tree_view_state *s = &view->state.tree;
4961 struct got_tree_entry *next, *last;
4964 if (s->first_displayed_entry)
4965 next = got_tree_entry_get_next(s->tree,
4966 s->first_displayed_entry);
4968 next = got_object_tree_get_first_entry(s->tree);
4970 last = s->last_displayed_entry;
4971 while (next && last && n++ < maxscroll) {
4972 last = got_tree_entry_get_next(s->tree, last);
4974 s->first_displayed_entry = next;
4975 next = got_tree_entry_get_next(s->tree, next);
4980 static const struct got_error *
4981 tree_entry_path(char **path, struct tog_parent_trees *parents,
4982 struct got_tree_entry *te)
4984 const struct got_error *err = NULL;
4985 struct tog_parent_tree *pt;
4986 size_t len = 2; /* for leading slash and NUL */
4988 TAILQ_FOREACH(pt, parents, entry)
4989 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4992 len += strlen(got_tree_entry_get_name(te));
4994 *path = calloc(1, len);
4996 return got_error_from_errno("calloc");
4999 pt = TAILQ_LAST(parents, tog_parent_trees);
5001 const char *name = got_tree_entry_get_name(pt->selected_entry);
5002 if (strlcat(*path, name, len) >= len) {
5003 err = got_error(GOT_ERR_NO_SPACE);
5006 if (strlcat(*path, "/", len) >= len) {
5007 err = got_error(GOT_ERR_NO_SPACE);
5010 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5013 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5014 err = got_error(GOT_ERR_NO_SPACE);
5026 static const struct got_error *
5027 blame_tree_entry(struct tog_view **new_view, int begin_x,
5028 struct got_tree_entry *te, struct tog_parent_trees *parents,
5029 struct got_object_id *commit_id, struct got_repository *repo)
5031 const struct got_error *err = NULL;
5033 struct tog_view *blame_view;
5037 err = tree_entry_path(&path, parents, te);
5041 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5042 if (blame_view == NULL) {
5043 err = got_error_from_errno("view_open");
5047 err = open_blame_view(blame_view, path, commit_id, repo);
5049 if (err->code == GOT_ERR_CANCELLED)
5051 view_close(blame_view);
5053 *new_view = blame_view;
5059 static const struct got_error *
5060 log_tree_entry(struct tog_view **new_view, int begin_x,
5061 struct got_tree_entry *te, struct tog_parent_trees *parents,
5062 struct got_object_id *commit_id, struct got_repository *repo)
5064 struct tog_view *log_view;
5065 const struct got_error *err = NULL;
5070 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5071 if (log_view == NULL)
5072 return got_error_from_errno("view_open");
5074 err = tree_entry_path(&path, parents, te);
5078 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5080 view_close(log_view);
5082 *new_view = log_view;
5087 static const struct got_error *
5088 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5089 struct got_object_id *commit_id, struct got_repository *repo)
5091 const struct got_error *err = NULL;
5092 char *commit_id_str = NULL;
5093 struct tog_tree_view_state *s = &view->state.tree;
5095 TAILQ_INIT(&s->parents);
5097 err = got_object_id_str(&commit_id_str, commit_id);
5101 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5102 err = got_error_from_errno("asprintf");
5106 s->root = s->tree = root;
5107 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5108 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5109 s->commit_id = got_object_id_dup(commit_id);
5110 if (s->commit_id == NULL) {
5111 err = got_error_from_errno("got_object_id_dup");
5116 SIMPLEQ_INIT(&s->colors);
5118 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5119 err = add_color(&s->colors, "\\$$",
5120 TOG_COLOR_TREE_SUBMODULE,
5121 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5124 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5125 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5127 free_colors(&s->colors);
5130 err = add_color(&s->colors, "/$",
5131 TOG_COLOR_TREE_DIRECTORY,
5132 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5134 free_colors(&s->colors);
5138 err = add_color(&s->colors, "\\*$",
5139 TOG_COLOR_TREE_EXECUTABLE,
5140 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5142 free_colors(&s->colors);
5146 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5147 get_color_value("TOG_COLOR_COMMIT"));
5149 free_colors(&s->colors);
5154 view->show = show_tree_view;
5155 view->input = input_tree_view;
5156 view->close = close_tree_view;
5157 view->search_start = search_start_tree_view;
5158 view->search_next = search_next_tree_view;
5160 free(commit_id_str);
5162 free(s->tree_label);
5163 s->tree_label = NULL;
5168 static const struct got_error *
5169 close_tree_view(struct tog_view *view)
5171 struct tog_tree_view_state *s = &view->state.tree;
5173 free_colors(&s->colors);
5174 free(s->tree_label);
5175 s->tree_label = NULL;
5177 s->commit_id = NULL;
5178 while (!TAILQ_EMPTY(&s->parents)) {
5179 struct tog_parent_tree *parent;
5180 parent = TAILQ_FIRST(&s->parents);
5181 TAILQ_REMOVE(&s->parents, parent, entry);
5185 if (s->tree != s->root)
5186 got_object_tree_close(s->tree);
5187 got_object_tree_close(s->root);
5191 static const struct got_error *
5192 search_start_tree_view(struct tog_view *view)
5194 struct tog_tree_view_state *s = &view->state.tree;
5196 s->matched_entry = NULL;
5201 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5203 regmatch_t regmatch;
5205 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5209 static const struct got_error *
5210 search_next_tree_view(struct tog_view *view)
5212 struct tog_tree_view_state *s = &view->state.tree;
5213 struct got_tree_entry *te = NULL;
5215 if (!view->searching) {
5216 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5220 if (s->matched_entry) {
5221 if (view->searching == TOG_SEARCH_FORWARD) {
5222 if (s->selected_entry)
5223 te = got_tree_entry_get_next(s->tree,
5226 te = got_object_tree_get_first_entry(s->tree);
5228 if (s->selected_entry == NULL)
5229 te = got_object_tree_get_last_entry(s->tree);
5231 te = got_tree_entry_get_prev(s->tree,
5235 if (view->searching == TOG_SEARCH_FORWARD)
5236 te = got_object_tree_get_first_entry(s->tree);
5238 te = got_object_tree_get_last_entry(s->tree);
5243 if (s->matched_entry == NULL) {
5244 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5247 if (view->searching == TOG_SEARCH_FORWARD)
5248 te = got_object_tree_get_first_entry(s->tree);
5250 te = got_object_tree_get_last_entry(s->tree);
5253 if (match_tree_entry(te, &view->regex)) {
5254 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5255 s->matched_entry = te;
5259 if (view->searching == TOG_SEARCH_FORWARD)
5260 te = got_tree_entry_get_next(s->tree, te);
5262 te = got_tree_entry_get_prev(s->tree, te);
5265 if (s->matched_entry) {
5266 s->first_displayed_entry = s->matched_entry;
5273 static const struct got_error *
5274 show_tree_view(struct tog_view *view)
5276 const struct got_error *err = NULL;
5277 struct tog_tree_view_state *s = &view->state.tree;
5280 err = tree_entry_path(&parent_path, &s->parents, NULL);
5284 err = draw_tree_entries(view, parent_path);
5291 static const struct got_error *
5292 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5293 struct tog_view **focus_view, struct tog_view *view, int ch)
5295 const struct got_error *err = NULL;
5296 struct tog_tree_view_state *s = &view->state.tree;
5297 struct tog_view *log_view, *ref_view;
5302 s->show_ids = !s->show_ids;
5305 if (!s->selected_entry)
5307 if (view_is_parent_view(view))
5308 begin_x = view_split_begin_x(view->begin_x);
5309 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5310 &s->parents, s->commit_id, s->repo);
5311 if (view_is_parent_view(view)) {
5312 err = view_close_child(view);
5315 err = view_set_child(view, log_view);
5317 view_close(log_view);
5320 *focus_view = log_view;
5321 view->child_focussed = 1;
5323 *new_view = log_view;
5326 if (view_is_parent_view(view))
5327 begin_x = view_split_begin_x(view->begin_x);
5328 ref_view = view_open(view->nlines, view->ncols,
5329 view->begin_y, begin_x, TOG_VIEW_REF);
5330 if (ref_view == NULL)
5331 return got_error_from_errno("view_open");
5332 err = open_ref_view(ref_view, s->repo);
5334 view_close(ref_view);
5337 if (view_is_parent_view(view)) {
5338 err = view_close_child(view);
5341 err = view_set_child(view, ref_view);
5343 view_close(ref_view);
5346 *focus_view = ref_view;
5347 view->child_focussed = 1;
5349 *new_view = ref_view;
5353 if (s->selected > 0) {
5357 tree_scroll_up(view, 1);
5361 if (s->tree == s->root) {
5362 if (got_object_tree_get_first_entry(s->tree) ==
5363 s->first_displayed_entry)
5366 if (s->first_displayed_entry == NULL)
5369 tree_scroll_up(view, MAX(0, view->nlines - 3));
5373 if (s->selected < s->ndisplayed - 1) {
5377 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5379 /* can't scroll any further */
5381 tree_scroll_down(view, 1);
5385 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5387 /* can't scroll any further; move cursor down */
5388 if (s->selected < s->ndisplayed - 1)
5389 s->selected = s->ndisplayed - 1;
5392 tree_scroll_down(view, view->nlines - 3);
5397 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5398 struct tog_parent_tree *parent;
5399 /* user selected '..' */
5400 if (s->tree == s->root)
5402 parent = TAILQ_FIRST(&s->parents);
5403 TAILQ_REMOVE(&s->parents, parent,
5405 got_object_tree_close(s->tree);
5406 s->tree = parent->tree;
5407 s->first_displayed_entry =
5408 parent->first_displayed_entry;
5410 parent->selected_entry;
5411 s->selected = parent->selected;
5413 } else if (S_ISDIR(got_tree_entry_get_mode(
5414 s->selected_entry))) {
5415 struct got_tree_object *subtree;
5416 err = got_object_open_as_tree(&subtree, s->repo,
5417 got_tree_entry_get_id(s->selected_entry));
5420 err = tree_view_visit_subtree(subtree, s);
5422 got_object_tree_close(subtree);
5425 } else if (S_ISREG(got_tree_entry_get_mode(
5426 s->selected_entry))) {
5427 struct tog_view *blame_view;
5428 int begin_x = view_is_parent_view(view) ?
5429 view_split_begin_x(view->begin_x) : 0;
5431 err = blame_tree_entry(&blame_view, begin_x,
5432 s->selected_entry, &s->parents,
5433 s->commit_id, s->repo);
5436 if (view_is_parent_view(view)) {
5437 err = view_close_child(view);
5440 err = view_set_child(view, blame_view);
5442 view_close(blame_view);
5445 *focus_view = blame_view;
5446 view->child_focussed = 1;
5448 *new_view = blame_view;
5452 if (s->selected > view->nlines)
5453 s->selected = s->ndisplayed - 1;
5466 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5471 static const struct got_error *
5472 cmd_tree(int argc, char *argv[])
5474 const struct got_error *error;
5475 struct got_repository *repo = NULL;
5476 struct got_worktree *worktree = NULL;
5477 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5478 struct got_object_id *commit_id = NULL;
5479 char *commit_id_arg = NULL;
5480 struct got_commit_object *commit = NULL;
5481 struct got_tree_object *tree = NULL;
5483 struct tog_view *view;
5486 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5491 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5494 commit_id_arg = optarg;
5497 repo_path = realpath(optarg, NULL);
5498 if (repo_path == NULL)
5499 return got_error_from_errno2("realpath",
5514 cwd = getcwd(NULL, 0);
5516 return got_error_from_errno("getcwd");
5518 error = got_worktree_open(&worktree, cwd);
5519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5522 if (repo_path == NULL) {
5525 strdup(got_worktree_get_repo_path(worktree));
5527 repo_path = strdup(cwd);
5529 if (repo_path == NULL) {
5530 error = got_error_from_errno("strdup");
5534 error = got_repo_open(&repo, repo_path, NULL);
5538 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5545 error = apply_unveil(got_repo_get_path(repo), NULL);
5549 error = got_repo_match_object_id(&commit_id, NULL,
5550 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5551 GOT_OBJ_TYPE_COMMIT, 1, repo);
5555 error = got_object_open_as_commit(&commit, repo, commit_id);
5559 error = got_object_open_as_tree(&tree, repo,
5560 got_object_commit_get_tree_id(commit));
5564 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5566 error = got_error_from_errno("view_open");
5569 error = open_tree_view(view, tree, commit_id, repo);
5572 if (!got_path_is_root_dir(in_repo_path)) {
5573 error = tree_view_walk_path(&view->state.tree, commit_id,
5574 in_repo_path, repo);
5580 /* Release work tree lock. */
5581 got_worktree_close(worktree);
5584 error = view_loop(view);
5590 got_object_commit_close(commit);
5592 got_object_tree_close(tree);
5594 got_repo_close(repo);
5598 static const struct got_error *
5599 ref_view_load_refs(struct tog_ref_view_state *s)
5601 const struct got_error *err;
5602 struct got_reflist_entry *sre;
5603 struct tog_reflist_entry *re;
5605 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5606 got_ref_cmp_by_name, NULL);
5611 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5612 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5615 re = malloc(sizeof(*re));
5617 return got_error_from_errno("malloc");
5620 re->idx = s->nrefs++;
5621 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5628 ref_view_free_refs(struct tog_ref_view_state *s)
5630 struct tog_reflist_entry *re;
5632 while (!TAILQ_EMPTY(&s->refs)) {
5633 re = TAILQ_FIRST(&s->refs);
5634 TAILQ_REMOVE(&s->refs, re, entry);
5637 got_ref_list_free(&s->simplerefs);
5640 static const struct got_error *
5641 open_ref_view(struct tog_view *view, struct got_repository *repo)
5643 const struct got_error *err = NULL;
5644 struct tog_ref_view_state *s = &view->state.ref;
5646 s->selected_entry = 0;
5649 SIMPLEQ_INIT(&s->simplerefs);
5650 TAILQ_INIT(&s->refs);
5651 SIMPLEQ_INIT(&s->colors);
5653 err = ref_view_load_refs(s);
5657 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5659 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5660 err = add_color(&s->colors, "^refs/heads/",
5661 TOG_COLOR_REFS_HEADS,
5662 get_color_value("TOG_COLOR_REFS_HEADS"));
5666 err = add_color(&s->colors, "^refs/tags/",
5667 TOG_COLOR_REFS_TAGS,
5668 get_color_value("TOG_COLOR_REFS_TAGS"));
5672 err = add_color(&s->colors, "^refs/remotes/",
5673 TOG_COLOR_REFS_REMOTES,
5674 get_color_value("TOG_COLOR_REFS_REMOTES"));
5679 view->show = show_ref_view;
5680 view->input = input_ref_view;
5681 view->close = close_ref_view;
5682 view->search_start = search_start_ref_view;
5683 view->search_next = search_next_ref_view;
5686 free_colors(&s->colors);
5690 static const struct got_error *
5691 close_ref_view(struct tog_view *view)
5693 struct tog_ref_view_state *s = &view->state.ref;
5695 ref_view_free_refs(s);
5696 free_colors(&s->colors);
5701 static const struct got_error *
5702 resolve_reflist_entry(struct got_object_id **commit_id,
5703 struct tog_reflist_entry *re, struct got_repository *repo)
5705 const struct got_error *err = NULL;
5706 struct got_object_id *obj_id;
5707 struct got_tag_object *tag = NULL;
5712 err = got_ref_resolve(&obj_id, repo, re->ref);
5716 err = got_object_get_type(&obj_type, repo, obj_id);
5721 case GOT_OBJ_TYPE_COMMIT:
5722 *commit_id = obj_id;
5724 case GOT_OBJ_TYPE_TAG:
5725 err = got_object_open_as_tag(&tag, repo, obj_id);
5729 err = got_object_get_type(&obj_type, repo,
5730 got_object_tag_get_object_id(tag));
5733 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5734 err = got_error(GOT_ERR_OBJ_TYPE);
5737 *commit_id = got_object_id_dup(
5738 got_object_tag_get_object_id(tag));
5739 if (*commit_id == NULL) {
5740 err = got_error_from_errno("got_object_id_dup");
5745 err = got_error(GOT_ERR_OBJ_TYPE);
5751 got_object_tag_close(tag);
5759 static const struct got_error *
5760 log_ref_entry(struct tog_view **new_view, int begin_x,
5761 struct tog_reflist_entry *re, struct got_repository *repo)
5763 struct tog_view *log_view;
5764 const struct got_error *err = NULL;
5765 struct got_object_id *commit_id = NULL;
5769 err = resolve_reflist_entry(&commit_id, re, repo);
5771 if (err->code != GOT_ERR_OBJ_TYPE)
5777 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5778 if (log_view == NULL) {
5779 err = got_error_from_errno("view_open");
5783 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5786 view_close(log_view);
5788 *new_view = log_view;
5794 ref_scroll_up(struct tog_view *view, int maxscroll)
5796 struct tog_ref_view_state *s = &view->state.ref;
5797 struct tog_reflist_entry *re;
5800 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5803 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5804 while (i++ < maxscroll) {
5807 s->first_displayed_entry = re;
5808 re = TAILQ_PREV(re, tog_reflist_head, entry);
5813 ref_scroll_down(struct tog_view *view, int maxscroll)
5815 struct tog_ref_view_state *s = &view->state.ref;
5816 struct tog_reflist_entry *next, *last;
5819 if (s->first_displayed_entry)
5820 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5822 next = TAILQ_FIRST(&s->refs);
5824 last = s->last_displayed_entry;
5825 while (next && last && n++ < maxscroll) {
5826 last = TAILQ_NEXT(last, entry);
5828 s->first_displayed_entry = next;
5829 next = TAILQ_NEXT(next, entry);
5834 static const struct got_error *
5835 search_start_ref_view(struct tog_view *view)
5837 struct tog_ref_view_state *s = &view->state.ref;
5839 s->matched_entry = NULL;
5844 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5846 regmatch_t regmatch;
5848 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5852 static const struct got_error *
5853 search_next_ref_view(struct tog_view *view)
5855 struct tog_ref_view_state *s = &view->state.ref;
5856 struct tog_reflist_entry *re = NULL;
5858 if (!view->searching) {
5859 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5863 if (s->matched_entry) {
5864 if (view->searching == TOG_SEARCH_FORWARD) {
5865 if (s->selected_entry)
5866 re = TAILQ_NEXT(s->selected_entry, entry);
5868 re = TAILQ_PREV(s->selected_entry,
5869 tog_reflist_head, entry);
5871 if (s->selected_entry == NULL)
5872 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5874 re = TAILQ_PREV(s->selected_entry,
5875 tog_reflist_head, entry);
5878 if (view->searching == TOG_SEARCH_FORWARD)
5879 re = TAILQ_FIRST(&s->refs);
5881 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5886 if (s->matched_entry == NULL) {
5887 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5890 if (view->searching == TOG_SEARCH_FORWARD)
5891 re = TAILQ_FIRST(&s->refs);
5893 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5896 if (match_reflist_entry(re, &view->regex)) {
5897 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5898 s->matched_entry = re;
5902 if (view->searching == TOG_SEARCH_FORWARD)
5903 re = TAILQ_NEXT(re, entry);
5905 re = TAILQ_PREV(re, tog_reflist_head, entry);
5908 if (s->matched_entry) {
5909 s->first_displayed_entry = s->matched_entry;
5916 static const struct got_error *
5917 show_ref_view(struct tog_view *view)
5919 const struct got_error *err = NULL;
5920 struct tog_ref_view_state *s = &view->state.ref;
5921 struct tog_reflist_entry *re;
5924 struct tog_color *tc;
5926 int limit = view->nlines;
5928 werase(view->window);
5935 re = s->first_displayed_entry;
5937 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5939 return got_error_from_errno("asprintf");
5941 err = format_line(&wline, &width, line, view->ncols, 0);
5946 if (view_needs_focus_indication(view))
5947 wstandout(view->window);
5948 waddwstr(view->window, wline);
5949 if (view_needs_focus_indication(view))
5950 wstandend(view->window);
5955 if (width < view->ncols - 1)
5956 waddch(view->window, '\n');
5961 while (re && limit > 0) {
5964 if (got_ref_is_symbolic(re->ref)) {
5965 if (asprintf(&line, "%s -> %s",
5966 got_ref_get_name(re->ref),
5967 got_ref_get_symref_target(re->ref)) == -1)
5968 return got_error_from_errno("asprintf");
5969 } else if (s->show_ids) {
5970 struct got_object_id *id;
5972 err = got_ref_resolve(&id, s->repo, re->ref);
5975 err = got_object_id_str(&id_str, id);
5980 if (asprintf(&line, "%s: %s",
5981 got_ref_get_name(re->ref), id_str) == -1) {
5982 err = got_error_from_errno("asprintf");
5990 line = strdup(got_ref_get_name(re->ref));
5992 return got_error_from_errno("strdup");
5995 err = format_line(&wline, &width, line, view->ncols, 0);
6000 if (n == s->selected) {
6002 wstandout(view->window);
6003 s->selected_entry = re;
6005 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6007 wattr_on(view->window,
6008 COLOR_PAIR(tc->colorpair), NULL);
6009 waddwstr(view->window, wline);
6011 wattr_off(view->window,
6012 COLOR_PAIR(tc->colorpair), NULL);
6013 if (width < view->ncols - 1)
6014 waddch(view->window, '\n');
6015 if (n == s->selected && view->focussed)
6016 wstandend(view->window);
6022 s->last_displayed_entry = re;
6025 re = TAILQ_NEXT(re, entry);
6032 static const struct got_error *
6033 browse_ref_tree(struct tog_view **new_view, int begin_x,
6034 struct tog_reflist_entry *re, struct got_repository *repo)
6036 const struct got_error *err = NULL;
6037 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6038 struct got_tree_object *tree = NULL;
6039 struct tog_view *tree_view;
6043 err = resolve_reflist_entry(&commit_id, re, repo);
6045 if (err->code != GOT_ERR_OBJ_TYPE)
6051 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6055 err = got_object_open_as_tree(&tree, repo, tree_id);
6059 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6060 if (tree_view == NULL) {
6061 err = got_error_from_errno("view_open");
6065 err = open_tree_view(tree_view, tree, commit_id, repo);
6069 *new_view = tree_view;
6075 got_object_tree_close(tree);
6079 static const struct got_error *
6080 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6081 struct tog_view **focus_view, struct tog_view *view, int ch)
6083 const struct got_error *err = NULL;
6084 struct tog_ref_view_state *s = &view->state.ref;
6085 struct tog_view *log_view, *tree_view;
6090 s->show_ids = !s->show_ids;
6094 if (!s->selected_entry)
6096 if (view_is_parent_view(view))
6097 begin_x = view_split_begin_x(view->begin_x);
6098 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6100 if (view_is_parent_view(view)) {
6101 err = view_close_child(view);
6104 err = view_set_child(view, log_view);
6106 view_close(log_view);
6109 *focus_view = log_view;
6110 view->child_focussed = 1;
6112 *new_view = log_view;
6115 if (!s->selected_entry)
6117 if (view_is_parent_view(view))
6118 begin_x = view_split_begin_x(view->begin_x);
6119 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6121 if (err || tree_view == NULL)
6123 if (view_is_parent_view(view)) {
6124 err = view_close_child(view);
6127 err = view_set_child(view, tree_view);
6129 view_close(tree_view);
6132 *focus_view = tree_view;
6133 view->child_focussed = 1;
6135 *new_view = tree_view;
6139 if (s->selected > 0) {
6143 ref_scroll_up(view, 1);
6147 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6149 ref_scroll_up(view, MAX(0, view->nlines - 1));
6153 if (s->selected < s->ndisplayed - 1) {
6157 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6158 /* can't scroll any further */
6160 ref_scroll_down(view, 1);
6164 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6165 /* can't scroll any further; move cursor down */
6166 if (s->selected < s->ndisplayed - 1)
6167 s->selected = s->ndisplayed - 1;
6170 ref_scroll_down(view, view->nlines - 1);
6173 ref_view_free_refs(s);
6174 err = ref_view_load_refs(s);
6177 if (s->selected > view->nlines)
6178 s->selected = s->ndisplayed - 1;
6191 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6196 static const struct got_error *
6197 cmd_ref(int argc, char *argv[])
6199 const struct got_error *error;
6200 struct got_repository *repo = NULL;
6201 struct got_worktree *worktree = NULL;
6202 char *cwd = NULL, *repo_path = NULL;
6204 struct tog_view *view;
6207 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6212 while ((ch = getopt(argc, argv, "r:")) != -1) {
6215 repo_path = realpath(optarg, NULL);
6216 if (repo_path == NULL)
6217 return got_error_from_errno2("realpath",
6232 cwd = getcwd(NULL, 0);
6234 return got_error_from_errno("getcwd");
6236 error = got_worktree_open(&worktree, cwd);
6237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6240 if (repo_path == NULL) {
6243 strdup(got_worktree_get_repo_path(worktree));
6245 repo_path = strdup(cwd);
6247 if (repo_path == NULL) {
6248 error = got_error_from_errno("strdup");
6252 error = got_repo_open(&repo, repo_path, NULL);
6258 error = apply_unveil(got_repo_get_path(repo), NULL);
6262 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6264 error = got_error_from_errno("view_open");
6268 error = open_ref_view(view, repo);
6273 /* Release work tree lock. */
6274 got_worktree_close(worktree);
6277 error = view_loop(view);
6282 got_repo_close(repo);
6287 list_commands(FILE *fp)
6291 fprintf(fp, "commands:");
6292 for (i = 0; i < nitems(tog_commands); i++) {
6293 struct tog_cmd *cmd = &tog_commands[i];
6294 fprintf(fp, " %s", cmd->name);
6300 usage(int hflag, int status)
6302 FILE *fp = (status == 0) ? stdout : stderr;
6304 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6307 fprintf(fp, "lazy usage: %s path\n", getprogname());
6314 make_argv(int argc, ...)
6322 argv = calloc(argc, sizeof(char *));
6325 for (i = 0; i < argc; i++) {
6326 argv[i] = strdup(va_arg(ap, char *));
6327 if (argv[i] == NULL)
6336 * Try to convert 'tog path' into a 'tog log path' command.
6337 * The user could simply have mistyped the command rather than knowingly
6338 * provided a path. So check whether argv[0] can in fact be resolved
6339 * to a path in the HEAD commit and print a special error if not.
6340 * This hack is for mpi@ <3
6342 static const struct got_error *
6343 tog_log_with_path(int argc, char *argv[])
6345 const struct got_error *error = NULL;
6346 struct tog_cmd *cmd = NULL;
6347 struct got_repository *repo = NULL;
6348 struct got_worktree *worktree = NULL;
6349 struct got_object_id *commit_id = NULL, *id = NULL;
6350 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6351 char *commit_id_str = NULL, **cmd_argv = NULL;
6353 cwd = getcwd(NULL, 0);
6355 return got_error_from_errno("getcwd");
6357 error = got_worktree_open(&worktree, cwd);
6358 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6362 repo_path = strdup(got_worktree_get_repo_path(worktree));
6364 repo_path = strdup(cwd);
6365 if (repo_path == NULL) {
6366 error = got_error_from_errno("strdup");
6370 error = got_repo_open(&repo, repo_path, NULL);
6374 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6379 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6380 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6381 GOT_OBJ_TYPE_COMMIT, 1, repo);
6386 got_worktree_close(worktree);
6390 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6392 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6394 fprintf(stderr, "%s: '%s' is no known command or path\n",
6395 getprogname(), argv[0]);
6400 got_repo_close(repo);
6403 error = got_object_id_str(&commit_id_str, commit_id);
6407 cmd = &tog_commands[0]; /* log */
6409 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6410 error = cmd->cmd_main(argc, cmd_argv);
6413 got_repo_close(repo);
6415 got_worktree_close(worktree);
6417 free(commit_id_str);
6424 for (i = 0; i < argc; i++)
6432 main(int argc, char *argv[])
6434 const struct got_error *error = NULL;
6435 struct tog_cmd *cmd = NULL;
6436 int ch, hflag = 0, Vflag = 0;
6437 char **cmd_argv = NULL;
6438 static struct option longopts[] = {
6439 { "version", no_argument, NULL, 'V' },
6443 setlocale(LC_CTYPE, "");
6445 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6465 got_version_print_str();
6472 /* Build an argument vector which runs a default command. */
6473 cmd = &tog_commands[0];
6475 cmd_argv = make_argv(argc, cmd->name);
6479 /* Did the user specify a command? */
6480 for (i = 0; i < nitems(tog_commands); i++) {
6481 if (strncmp(tog_commands[i].name, argv[0],
6482 strlen(argv[0])) == 0) {
6483 cmd = &tog_commands[i];
6492 /* No command specified; try log with a path */
6493 error = tog_log_with_path(argc, argv);
6498 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6505 for (i = 0; i < argc; i++)
6510 if (error && error->code != GOT_ERR_CANCELLED)
6511 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);