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_log_view_state *s, int maxscroll)
1688 struct commit_queue_entry *entry;
1691 entry = TAILQ_FIRST(&s->commits.head);
1692 if (s->first_displayed_entry == entry)
1695 entry = s->first_displayed_entry;
1696 while (entry && nscrolled < maxscroll) {
1697 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1699 s->first_displayed_entry = entry;
1705 static const struct got_error *
1706 trigger_log_thread(struct tog_view *view, int wait)
1708 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1711 halfdelay(1); /* fast refresh while loading commits */
1713 while (ta->commits_needed > 0) {
1714 if (ta->log_complete)
1717 /* Wake the log thread. */
1718 errcode = pthread_cond_signal(&ta->need_commits);
1720 return got_error_set_errno(errcode,
1721 "pthread_cond_signal");
1724 * The mutex will be released while the view loop waits
1725 * in wgetch(), at which time the log thread will run.
1730 /* Display progress update in log view. */
1731 show_log_view(view);
1735 /* Wait right here while next commit is being loaded. */
1736 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1738 return got_error_set_errno(errcode,
1739 "pthread_cond_wait");
1741 /* Display progress update in log view. */
1742 show_log_view(view);
1750 static const struct got_error *
1751 log_scroll_down(struct tog_view *view, int maxscroll)
1753 struct tog_log_view_state *s = &view->state.log;
1754 const struct got_error *err = NULL;
1755 struct commit_queue_entry *pentry;
1756 int nscrolled = 0, ncommits_needed;
1758 if (s->last_displayed_entry == NULL)
1761 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1762 if (s->commits.ncommits < ncommits_needed &&
1763 !s->thread_args.log_complete) {
1765 * Ask the log thread for required amount of commits.
1767 s->thread_args.commits_needed += maxscroll;
1768 err = trigger_log_thread(view, 1);
1774 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1778 s->last_displayed_entry = pentry;
1780 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1783 s->first_displayed_entry = pentry;
1784 } while (++nscrolled < maxscroll);
1789 static const struct got_error *
1790 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1791 struct got_commit_object *commit, struct got_object_id *commit_id,
1792 struct tog_view *log_view, struct got_repository *repo)
1794 const struct got_error *err;
1795 struct got_object_qid *parent_id;
1796 struct tog_view *diff_view;
1798 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1799 if (diff_view == NULL)
1800 return got_error_from_errno("view_open");
1802 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1803 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1804 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1806 *new_view = diff_view;
1810 static const struct got_error *
1811 tree_view_visit_subtree(struct tog_tree_view_state *s,
1812 struct got_tree_object *subtree)
1814 struct tog_parent_tree *parent;
1816 parent = calloc(1, sizeof(*parent));
1818 return got_error_from_errno("calloc");
1820 parent->tree = s->tree;
1821 parent->first_displayed_entry = s->first_displayed_entry;
1822 parent->selected_entry = s->selected_entry;
1823 parent->selected = s->selected;
1824 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1827 s->first_displayed_entry = NULL;
1831 static const struct got_error *
1832 tree_view_walk_path(struct tog_tree_view_state *s,
1833 struct got_object_id *commit_id, const char *path)
1835 const struct got_error *err = NULL;
1836 struct got_tree_object *tree = NULL;
1838 char *slash, *subpath = NULL;
1840 /* Walk the path and open corresponding tree objects. */
1843 struct got_tree_entry *te;
1844 struct got_object_id *tree_id;
1850 /* Ensure the correct subtree entry is selected. */
1851 slash = strchr(p, '/');
1853 te_name = strdup(p);
1855 te_name = strndup(p, slash - p);
1856 if (te_name == NULL) {
1857 err = got_error_from_errno("strndup");
1860 te = got_object_tree_find_entry(s->tree, te_name);
1862 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1867 s->first_displayed_entry = s->selected_entry = te;
1869 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1870 break; /* jump to this file's entry */
1872 slash = strchr(p, '/');
1874 subpath = strndup(path, slash - path);
1876 subpath = strdup(path);
1877 if (subpath == NULL) {
1878 err = got_error_from_errno("strdup");
1882 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1887 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1892 err = tree_view_visit_subtree(s, tree);
1894 got_object_tree_close(tree);
1908 static const struct got_error *
1909 browse_commit_tree(struct tog_view **new_view, int begin_x,
1910 struct commit_queue_entry *entry, const char *path,
1911 struct got_repository *repo)
1913 const struct got_error *err = NULL;
1914 struct got_tree_object *tree;
1915 struct tog_tree_view_state *s;
1916 struct tog_view *tree_view;
1918 err = got_object_open_as_tree(&tree, repo,
1919 got_object_commit_get_tree_id(entry->commit));
1923 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1924 if (tree_view == NULL)
1925 return got_error_from_errno("view_open");
1927 err = open_tree_view(tree_view, tree, entry->id, repo);
1929 got_object_tree_close(tree);
1932 s = &tree_view->state.tree;
1934 *new_view = tree_view;
1936 if (got_path_is_root_dir(path))
1939 return tree_view_walk_path(s, entry->id, path);
1942 static const struct got_error *
1943 block_signals_used_by_main_thread(void)
1948 if (sigemptyset(&sigset) == -1)
1949 return got_error_from_errno("sigemptyset");
1951 /* tog handles SIGWINCH and SIGCONT */
1952 if (sigaddset(&sigset, SIGWINCH) == -1)
1953 return got_error_from_errno("sigaddset");
1954 if (sigaddset(&sigset, SIGCONT) == -1)
1955 return got_error_from_errno("sigaddset");
1957 /* ncurses handles SIGTSTP */
1958 if (sigaddset(&sigset, SIGTSTP) == -1)
1959 return got_error_from_errno("sigaddset");
1961 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1963 return got_error_set_errno(errcode, "pthread_sigmask");
1969 log_thread(void *arg)
1971 const struct got_error *err = NULL;
1973 struct tog_log_thread_args *a = arg;
1976 err = block_signals_used_by_main_thread();
1980 while (!done && !err && !tog_sigpipe_received) {
1981 err = queue_commits(a->graph, a->commits, 1, a->repo,
1982 a->in_repo_path, a->searching, a->search_next_done,
1985 if (err->code != GOT_ERR_ITER_COMPLETED)
1989 } else if (a->commits_needed > 0)
1990 a->commits_needed--;
1992 errcode = pthread_mutex_lock(&tog_mutex);
1994 err = got_error_set_errno(errcode,
1995 "pthread_mutex_lock");
1997 } else if (*a->quit)
1999 else if (*a->first_displayed_entry == NULL) {
2000 *a->first_displayed_entry =
2001 TAILQ_FIRST(&a->commits->head);
2002 *a->selected_entry = *a->first_displayed_entry;
2005 errcode = pthread_cond_signal(&a->commit_loaded);
2007 err = got_error_set_errno(errcode,
2008 "pthread_cond_signal");
2009 pthread_mutex_unlock(&tog_mutex);
2014 a->commits_needed = 0;
2016 if (a->commits_needed == 0) {
2017 errcode = pthread_cond_wait(&a->need_commits,
2020 err = got_error_set_errno(errcode,
2021 "pthread_cond_wait");
2025 errcode = pthread_mutex_unlock(&tog_mutex);
2026 if (errcode && err == NULL)
2027 err = got_error_set_errno(errcode,
2028 "pthread_mutex_unlock");
2030 a->log_complete = 1;
2034 static const struct got_error *
2035 stop_log_thread(struct tog_log_view_state *s)
2037 const struct got_error *err = NULL;
2042 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2044 return got_error_set_errno(errcode,
2045 "pthread_cond_signal");
2046 errcode = pthread_mutex_unlock(&tog_mutex);
2048 return got_error_set_errno(errcode,
2049 "pthread_mutex_unlock");
2050 errcode = pthread_join(s->thread, (void **)&err);
2052 return got_error_set_errno(errcode, "pthread_join");
2053 errcode = pthread_mutex_lock(&tog_mutex);
2055 return got_error_set_errno(errcode,
2056 "pthread_mutex_lock");
2060 if (s->thread_args.repo) {
2061 got_repo_close(s->thread_args.repo);
2062 s->thread_args.repo = NULL;
2065 if (s->thread_args.graph) {
2066 got_commit_graph_close(s->thread_args.graph);
2067 s->thread_args.graph = NULL;
2073 static const struct got_error *
2074 close_log_view(struct tog_view *view)
2076 const struct got_error *err = NULL;
2077 struct tog_log_view_state *s = &view->state.log;
2080 err = stop_log_thread(s);
2082 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2083 if (errcode && err == NULL)
2084 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2086 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2087 if (errcode && err == NULL)
2088 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2090 free_commits(&s->commits);
2091 free(s->in_repo_path);
2092 s->in_repo_path = NULL;
2095 got_ref_list_free(&s->refs);
2099 static const struct got_error *
2100 search_start_log_view(struct tog_view *view)
2102 struct tog_log_view_state *s = &view->state.log;
2104 s->matched_entry = NULL;
2105 s->search_entry = NULL;
2109 static const struct got_error *
2110 search_next_log_view(struct tog_view *view)
2112 const struct got_error *err = NULL;
2113 struct tog_log_view_state *s = &view->state.log;
2114 struct commit_queue_entry *entry;
2116 /* Display progress update in log view. */
2117 show_log_view(view);
2121 if (s->search_entry) {
2123 errcode = pthread_mutex_unlock(&tog_mutex);
2125 return got_error_set_errno(errcode,
2126 "pthread_mutex_unlock");
2127 ch = wgetch(view->window);
2128 errcode = pthread_mutex_lock(&tog_mutex);
2130 return got_error_set_errno(errcode,
2131 "pthread_mutex_lock");
2132 if (ch == KEY_BACKSPACE) {
2133 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2136 if (view->searching == TOG_SEARCH_FORWARD)
2137 entry = TAILQ_NEXT(s->search_entry, entry);
2139 entry = TAILQ_PREV(s->search_entry,
2140 commit_queue_head, entry);
2141 } else if (s->matched_entry) {
2142 if (view->searching == TOG_SEARCH_FORWARD)
2143 entry = TAILQ_NEXT(s->matched_entry, entry);
2145 entry = TAILQ_PREV(s->matched_entry,
2146 commit_queue_head, entry);
2148 if (view->searching == TOG_SEARCH_FORWARD)
2149 entry = TAILQ_FIRST(&s->commits.head);
2151 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2157 if (entry == NULL) {
2158 if (s->thread_args.log_complete ||
2159 view->searching == TOG_SEARCH_BACKWARD) {
2160 view->search_next_done =
2161 (s->matched_entry == NULL ?
2162 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2163 s->search_entry = NULL;
2167 * Poke the log thread for more commits and return,
2168 * allowing the main loop to make progress. Search
2169 * will resume at s->search_entry once we come back.
2171 s->thread_args.commits_needed++;
2172 return trigger_log_thread(view, 0);
2175 err = match_commit(&have_match, entry->id, entry->commit,
2180 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2181 s->matched_entry = entry;
2185 s->search_entry = entry;
2186 if (view->searching == TOG_SEARCH_FORWARD)
2187 entry = TAILQ_NEXT(entry, entry);
2189 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2192 if (s->matched_entry) {
2193 int cur = s->selected_entry->idx;
2194 while (cur < s->matched_entry->idx) {
2195 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2200 while (cur > s->matched_entry->idx) {
2201 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2208 s->search_entry = NULL;
2213 static const struct got_error *
2214 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2215 struct got_repository *repo, const char *head_ref_name,
2216 const char *in_repo_path, int log_branches)
2218 const struct got_error *err = NULL;
2219 struct tog_log_view_state *s = &view->state.log;
2220 struct got_repository *thread_repo = NULL;
2221 struct got_commit_graph *thread_graph = NULL;
2224 SIMPLEQ_INIT(&s->refs);
2226 if (in_repo_path != s->in_repo_path) {
2227 free(s->in_repo_path);
2228 s->in_repo_path = strdup(in_repo_path);
2229 if (s->in_repo_path == NULL)
2230 return got_error_from_errno("strdup");
2233 /* The commit queue only contains commits being displayed. */
2234 TAILQ_INIT(&s->commits.head);
2235 s->commits.ncommits = 0;
2237 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2242 s->head_ref_name = head_ref_name;
2243 s->start_id = got_object_id_dup(start_id);
2244 if (s->start_id == NULL) {
2245 err = got_error_from_errno("got_object_id_dup");
2248 s->log_branches = log_branches;
2250 SIMPLEQ_INIT(&s->colors);
2251 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2252 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2253 get_color_value("TOG_COLOR_COMMIT"));
2256 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2257 get_color_value("TOG_COLOR_AUTHOR"));
2259 free_colors(&s->colors);
2262 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2263 get_color_value("TOG_COLOR_DATE"));
2265 free_colors(&s->colors);
2270 view->show = show_log_view;
2271 view->input = input_log_view;
2272 view->close = close_log_view;
2273 view->search_start = search_start_log_view;
2274 view->search_next = search_next_log_view;
2276 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2279 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2283 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2284 s->repo, NULL, NULL);
2288 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2290 err = got_error_set_errno(errcode, "pthread_cond_init");
2293 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2295 err = got_error_set_errno(errcode, "pthread_cond_init");
2299 s->thread_args.commits_needed = view->nlines;
2300 s->thread_args.graph = thread_graph;
2301 s->thread_args.commits = &s->commits;
2302 s->thread_args.in_repo_path = s->in_repo_path;
2303 s->thread_args.start_id = s->start_id;
2304 s->thread_args.repo = thread_repo;
2305 s->thread_args.log_complete = 0;
2306 s->thread_args.quit = &s->quit;
2307 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2308 s->thread_args.selected_entry = &s->selected_entry;
2309 s->thread_args.searching = &view->searching;
2310 s->thread_args.search_next_done = &view->search_next_done;
2311 s->thread_args.regex = &view->regex;
2314 close_log_view(view);
2318 static const struct got_error *
2319 show_log_view(struct tog_view *view)
2321 const struct got_error *err;
2322 struct tog_log_view_state *s = &view->state.log;
2324 if (s->thread == NULL) {
2325 int errcode = pthread_create(&s->thread, NULL, log_thread,
2328 return got_error_set_errno(errcode, "pthread_create");
2329 if (s->thread_args.commits_needed > 0) {
2330 err = trigger_log_thread(view, 1);
2336 return draw_commits(view);
2339 static const struct got_error *
2340 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2341 struct tog_view **focus_view, struct tog_view *view, int ch)
2343 const struct got_error *err = NULL;
2344 struct tog_log_view_state *s = &view->state.log;
2345 char *parent_path, *in_repo_path = NULL;
2346 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2347 struct tog_view *ref_view = NULL;
2349 struct got_object_id *start_id;
2359 if (s->first_displayed_entry == NULL)
2361 if (s->selected > 0)
2364 log_scroll_up(s, 1);
2368 if (s->first_displayed_entry == NULL)
2370 if (TAILQ_FIRST(&s->commits.head) ==
2371 s->first_displayed_entry) {
2375 log_scroll_up(s, view->nlines - 1);
2381 if (s->first_displayed_entry == NULL)
2383 if (s->selected < MIN(view->nlines - 2,
2384 s->commits.ncommits - 1)) {
2388 err = log_scroll_down(view, 1);
2392 struct commit_queue_entry *first;
2393 first = s->first_displayed_entry;
2396 err = log_scroll_down(view, view->nlines - 1);
2399 if (first == s->first_displayed_entry &&
2400 s->selected < MIN(view->nlines - 2,
2401 s->commits.ncommits - 1)) {
2402 /* can't scroll further down */
2403 s->selected = MIN(view->nlines - 2,
2404 s->commits.ncommits - 1);
2410 if (s->selected > view->nlines - 2)
2411 s->selected = view->nlines - 2;
2412 if (s->selected > s->commits.ncommits - 1)
2413 s->selected = s->commits.ncommits - 1;
2414 if (s->commits.ncommits < view->nlines - 1 &&
2415 !s->thread_args.log_complete) {
2416 s->thread_args.commits_needed += (view->nlines - 1) -
2417 s->commits.ncommits;
2418 err = trigger_log_thread(view, 1);
2424 if (s->selected_entry == NULL)
2426 if (view_is_parent_view(view))
2427 begin_x = view_split_begin_x(view->begin_x);
2428 err = open_diff_view_for_commit(&diff_view, begin_x,
2429 s->selected_entry->commit, s->selected_entry->id,
2433 if (view_is_parent_view(view)) {
2434 err = view_close_child(view);
2437 err = view_set_child(view, diff_view);
2439 view_close(diff_view);
2442 *focus_view = diff_view;
2443 view->child_focussed = 1;
2445 *new_view = diff_view;
2448 if (s->selected_entry == NULL)
2450 if (view_is_parent_view(view))
2451 begin_x = view_split_begin_x(view->begin_x);
2452 err = browse_commit_tree(&tree_view, begin_x,
2453 s->selected_entry, s->in_repo_path, s->repo);
2456 if (view_is_parent_view(view)) {
2457 err = view_close_child(view);
2460 err = view_set_child(view, tree_view);
2462 view_close(tree_view);
2465 *focus_view = tree_view;
2466 view->child_focussed = 1;
2468 *new_view = tree_view;
2471 if (got_path_cmp(s->in_repo_path, "/",
2472 strlen(s->in_repo_path), 1) == 0)
2474 err = got_path_dirname(&parent_path, s->in_repo_path);
2477 err = stop_log_thread(s);
2482 lv = view_open(view->nlines, view->ncols,
2483 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2486 return got_error_from_errno("view_open");
2488 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2489 parent_path, s->log_branches);
2493 if (view_is_parent_view(view))
2496 view_set_child(view->parent, lv);
2501 err = stop_log_thread(s);
2504 lv = view_open(view->nlines, view->ncols,
2505 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2507 return got_error_from_errno("view_open");
2508 err = got_repo_match_object_id(&start_id, NULL,
2509 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2510 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2515 in_repo_path = strdup(s->in_repo_path);
2516 if (in_repo_path == NULL) {
2519 return got_error_from_errno("strdup");
2521 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2522 in_repo_path, s->log_branches);
2532 s->log_branches = !s->log_branches;
2533 err = stop_log_thread(s);
2536 lv = view_open(view->nlines, view->ncols,
2537 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2539 return got_error_from_errno("view_open");
2540 err = open_log_view(lv, s->start_id, s->repo,
2541 s->head_ref_name, s->in_repo_path, s->log_branches);
2550 if (view_is_parent_view(view))
2551 begin_x = view_split_begin_x(view->begin_x);
2552 ref_view = view_open(view->nlines, view->ncols,
2553 view->begin_y, begin_x, TOG_VIEW_REF);
2554 if (ref_view == NULL)
2555 return got_error_from_errno("view_open");
2556 err = open_ref_view(ref_view, s->repo);
2558 view_close(ref_view);
2561 if (view_is_parent_view(view)) {
2562 err = view_close_child(view);
2565 err = view_set_child(view, ref_view);
2567 view_close(ref_view);
2570 *focus_view = ref_view;
2571 view->child_focussed = 1;
2573 *new_view = ref_view;
2582 static const struct got_error *
2583 apply_unveil(const char *repo_path, const char *worktree_path)
2585 const struct got_error *error;
2588 if (unveil("gmon.out", "rwc") != 0)
2589 return got_error_from_errno2("unveil", "gmon.out");
2591 if (repo_path && unveil(repo_path, "r") != 0)
2592 return got_error_from_errno2("unveil", repo_path);
2594 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2595 return got_error_from_errno2("unveil", worktree_path);
2597 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2598 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2600 error = got_privsep_unveil_exec_helpers();
2604 if (unveil(NULL, NULL) != 0)
2605 return got_error_from_errno("unveil");
2615 halfdelay(1); /* Do fast refresh while initial view is loading. */
2618 intrflush(stdscr, FALSE);
2619 keypad(stdscr, TRUE);
2621 if (getenv("TOG_COLORS") != NULL) {
2623 use_default_colors();
2625 signal(SIGWINCH, tog_sigwinch);
2626 signal(SIGPIPE, tog_sigpipe);
2627 signal(SIGCONT, tog_sigcont);
2630 static const struct got_error *
2631 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2632 struct got_repository *repo, struct got_worktree *worktree)
2634 const struct got_error *err = NULL;
2637 *in_repo_path = strdup("/");
2638 if (*in_repo_path == NULL)
2639 return got_error_from_errno("strdup");
2644 const char *prefix = got_worktree_get_path_prefix(worktree);
2647 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2650 if (asprintf(in_repo_path, "%s%s%s", prefix,
2651 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2653 err = got_error_from_errno("asprintf");
2654 *in_repo_path = NULL;
2658 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2663 static const struct got_error *
2664 cmd_log(int argc, char *argv[])
2666 const struct got_error *error;
2667 struct got_repository *repo = NULL;
2668 struct got_worktree *worktree = NULL;
2669 struct got_object_id *start_id = NULL;
2670 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2671 char *start_commit = NULL, *head_ref_name = NULL;
2672 int ch, log_branches = 0;
2673 struct tog_view *view;
2676 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2681 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2687 start_commit = optarg;
2690 repo_path = realpath(optarg, NULL);
2691 if (repo_path == NULL)
2692 return got_error_from_errno2("realpath",
2707 cwd = getcwd(NULL, 0);
2709 return got_error_from_errno("getcwd");
2711 error = got_worktree_open(&worktree, cwd);
2712 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2715 if (repo_path == NULL) {
2718 strdup(got_worktree_get_repo_path(worktree));
2720 repo_path = strdup(cwd);
2722 if (repo_path == NULL) {
2723 error = got_error_from_errno("strdup");
2727 error = got_repo_open(&repo, repo_path, NULL);
2731 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2738 error = apply_unveil(got_repo_get_path(repo),
2739 worktree ? got_worktree_get_root_path(worktree) : NULL);
2743 if (start_commit == NULL)
2744 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2745 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2746 GOT_OBJ_TYPE_COMMIT, 1, repo);
2748 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2749 GOT_OBJ_TYPE_COMMIT, 1, repo);
2753 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2755 error = got_error_from_errno("view_open");
2759 head_ref_name = strdup(
2760 got_worktree_get_head_ref_name(worktree));
2761 if (head_ref_name == NULL) {
2762 error = got_error_from_errno("strdup");
2766 error = open_log_view(view, start_id, repo, head_ref_name,
2767 in_repo_path, log_branches);
2771 /* Release work tree lock. */
2772 got_worktree_close(worktree);
2775 error = view_loop(view);
2781 free(head_ref_name);
2783 got_repo_close(repo);
2785 got_worktree_close(worktree);
2793 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2794 "[-w] object1 object2\n", getprogname());
2799 parse_next_line(FILE *f, size_t *len)
2804 const char delim[3] = { '\0', '\0', '\0'};
2806 line = fparseln(f, &linelen, &lineno, delim, 0);
2813 match_line(const char *line, regex_t *regex, size_t nmatch,
2814 regmatch_t *regmatch)
2816 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2820 match_color(struct tog_colors *colors, const char *line)
2822 struct tog_color *tc = NULL;
2824 SIMPLEQ_FOREACH(tc, colors, entry) {
2825 if (match_line(line, &tc->regex, 0, NULL))
2832 static const struct got_error *
2833 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2834 WINDOW *window, regmatch_t *regmatch)
2836 const struct got_error *err = NULL;
2843 s = strndup(line, regmatch->rm_so);
2845 return got_error_from_errno("strndup");
2847 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2852 waddwstr(window, wline);
2859 s = strndup(line + regmatch->rm_so,
2860 regmatch->rm_eo - regmatch->rm_so);
2862 err = got_error_from_errno("strndup");
2866 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2871 wattr_on(window, A_STANDOUT, NULL);
2872 waddwstr(window, wline);
2873 wattr_off(window, A_STANDOUT, NULL);
2880 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2881 err = format_line(&wline, &width,
2882 line + regmatch->rm_eo, wlimit, col_tab_align);
2885 waddwstr(window, wline);
2893 static const struct got_error *
2894 draw_file(struct tog_view *view, const char *header)
2896 struct tog_diff_view_state *s = &view->state.diff;
2897 regmatch_t *regmatch = &view->regmatch;
2898 const struct got_error *err;
2901 struct tog_color *tc;
2905 int max_lines = view->nlines;
2906 int nlines = s->nlines;
2909 line_offset = s->line_offsets[s->first_displayed_line - 1];
2910 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2911 return got_error_from_errno("fseek");
2913 werase(view->window);
2916 if (asprintf(&line, "[%d/%d] %s",
2917 s->first_displayed_line - 1 + s->selected_line, nlines,
2919 return got_error_from_errno("asprintf");
2920 err = format_line(&wline, &width, line, view->ncols, 0);
2925 if (view_needs_focus_indication(view))
2926 wstandout(view->window);
2927 waddwstr(view->window, wline);
2930 if (view_needs_focus_indication(view))
2931 wstandend(view->window);
2932 if (width <= view->ncols - 1)
2933 waddch(view->window, '\n');
2941 while (max_lines > 0 && nprinted < max_lines) {
2942 line = parse_next_line(s->f, &len);
2948 tc = match_color(&s->colors, line);
2950 wattr_on(view->window,
2951 COLOR_PAIR(tc->colorpair), NULL);
2952 if (s->first_displayed_line + nprinted == s->matched_line &&
2953 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2954 err = add_matched_line(&width, line, view->ncols, 0,
2955 view->window, regmatch);
2961 err = format_line(&wline, &width, line, view->ncols, 0);
2966 waddwstr(view->window, wline);
2971 wattr_off(view->window,
2972 COLOR_PAIR(tc->colorpair), NULL);
2973 if (width <= view->ncols - 1)
2974 waddch(view->window, '\n');
2979 s->last_displayed_line = s->first_displayed_line +
2982 s->last_displayed_line = s->first_displayed_line;
2987 while (nprinted < view->nlines) {
2988 waddch(view->window, '\n');
2992 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2997 wstandout(view->window);
2998 waddwstr(view->window, wline);
3001 wstandend(view->window);
3008 get_datestr(time_t *time, char *datebuf)
3010 struct tm mytm, *tm;
3013 tm = gmtime_r(time, &mytm);
3016 s = asctime_r(tm, datebuf);
3019 p = strchr(s, '\n');
3025 static const struct got_error *
3026 get_changed_paths(struct got_pathlist_head *paths,
3027 struct got_commit_object *commit, struct got_repository *repo)
3029 const struct got_error *err = NULL;
3030 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3031 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3032 struct got_object_qid *qid;
3034 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3036 struct got_commit_object *pcommit;
3037 err = got_object_open_as_commit(&pcommit, repo,
3042 tree_id1 = got_object_commit_get_tree_id(pcommit);
3043 got_object_commit_close(pcommit);
3048 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3053 tree_id2 = got_object_commit_get_tree_id(commit);
3054 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3058 err = got_diff_tree(tree1, tree2, "", "", repo,
3059 got_diff_tree_collect_changed_paths, paths, 0);
3062 got_object_tree_close(tree1);
3064 got_object_tree_close(tree2);
3068 static const struct got_error *
3069 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3073 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3075 return got_error_from_errno("reallocarray");
3077 (*line_offsets)[*nlines] = off;
3082 static const struct got_error *
3083 write_commit_info(off_t **line_offsets, size_t *nlines,
3084 struct got_object_id *commit_id, struct got_reflist_head *refs,
3085 struct got_repository *repo, FILE *outfile)
3087 const struct got_error *err = NULL;
3088 char datebuf[26], *datestr;
3089 struct got_commit_object *commit;
3090 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3091 time_t committer_time;
3092 const char *author, *committer;
3093 char *refs_str = NULL;
3094 struct got_pathlist_head changed_paths;
3095 struct got_pathlist_entry *pe;
3099 TAILQ_INIT(&changed_paths);
3102 err = build_refs_str(&refs_str, refs, commit_id, repo);
3107 err = got_object_open_as_commit(&commit, repo, commit_id);
3111 err = got_object_id_str(&id_str, commit_id);
3113 err = got_error_from_errno("got_object_id_str");
3117 err = add_line_offset(line_offsets, nlines, 0);
3121 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3122 refs_str ? refs_str : "", refs_str ? ")" : "");
3124 err = got_error_from_errno("fprintf");
3128 err = add_line_offset(line_offsets, nlines, outoff);
3132 n = fprintf(outfile, "from: %s\n",
3133 got_object_commit_get_author(commit));
3135 err = got_error_from_errno("fprintf");
3139 err = add_line_offset(line_offsets, nlines, outoff);
3143 committer_time = got_object_commit_get_committer_time(commit);
3144 datestr = get_datestr(&committer_time, datebuf);
3146 n = fprintf(outfile, "date: %s UTC\n", datestr);
3148 err = got_error_from_errno("fprintf");
3152 err = add_line_offset(line_offsets, nlines, outoff);
3156 author = got_object_commit_get_author(commit);
3157 committer = got_object_commit_get_committer(commit);
3158 if (strcmp(author, committer) != 0) {
3159 n = fprintf(outfile, "via: %s\n", committer);
3161 err = got_error_from_errno("fprintf");
3165 err = add_line_offset(line_offsets, nlines, outoff);
3169 err = got_object_commit_get_logmsg(&logmsg, commit);
3173 while ((line = strsep(&s, "\n")) != NULL) {
3174 n = fprintf(outfile, "%s\n", line);
3176 err = got_error_from_errno("fprintf");
3180 err = add_line_offset(line_offsets, nlines, outoff);
3185 err = get_changed_paths(&changed_paths, commit, repo);
3188 TAILQ_FOREACH(pe, &changed_paths, entry) {
3189 struct got_diff_changed_path *cp = pe->data;
3190 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3192 err = got_error_from_errno("fprintf");
3196 err = add_line_offset(line_offsets, nlines, outoff);
3199 free((char *)pe->path);
3203 fputc('\n', outfile);
3205 err = add_line_offset(line_offsets, nlines, outoff);
3207 got_pathlist_free(&changed_paths);
3211 got_object_commit_close(commit);
3213 free(*line_offsets);
3214 *line_offsets = NULL;
3220 static const struct got_error *
3221 create_diff(struct tog_diff_view_state *s)
3223 const struct got_error *err = NULL;
3227 free(s->line_offsets);
3228 s->line_offsets = malloc(sizeof(off_t));
3229 if (s->line_offsets == NULL)
3230 return got_error_from_errno("malloc");
3235 err = got_error_from_errno("got_opentemp");
3238 if (s->f && fclose(s->f) != 0) {
3239 err = got_error_from_errno("fclose");
3245 err = got_object_get_type(&obj_type, s->repo, s->id1);
3247 err = got_object_get_type(&obj_type, s->repo, s->id2);
3252 case GOT_OBJ_TYPE_BLOB:
3253 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3254 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3255 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3257 case GOT_OBJ_TYPE_TREE:
3258 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3259 s->id1, s->id2, "", "", s->diff_context,
3260 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3262 case GOT_OBJ_TYPE_COMMIT: {
3263 const struct got_object_id_queue *parent_ids;
3264 struct got_object_qid *pid;
3265 struct got_commit_object *commit2;
3267 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3270 /* Show commit info if we're diffing to a parent/root commit. */
3271 if (s->id1 == NULL) {
3272 err = write_commit_info(&s->line_offsets, &s->nlines,
3273 s->id2, &s->refs, s->repo, s->f);
3277 parent_ids = got_object_commit_get_parent_ids(commit2);
3278 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3279 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3280 err = write_commit_info(
3281 &s->line_offsets, &s->nlines,
3282 s->id2, &s->refs, s->repo, s->f);
3289 got_object_commit_close(commit2);
3291 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3292 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3293 s->force_text_diff, s->repo, s->f);
3297 err = got_error(GOT_ERR_OBJ_TYPE);
3303 if (s->f && fflush(s->f) != 0 && err == NULL)
3304 err = got_error_from_errno("fflush");
3309 diff_view_indicate_progress(struct tog_view *view)
3311 mvwaddstr(view->window, 0, 0, "diffing...");
3316 static const struct got_error *
3317 search_start_diff_view(struct tog_view *view)
3319 struct tog_diff_view_state *s = &view->state.diff;
3321 s->matched_line = 0;
3325 static const struct got_error *
3326 search_next_diff_view(struct tog_view *view)
3328 struct tog_diff_view_state *s = &view->state.diff;
3331 if (!view->searching) {
3332 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3336 if (s->matched_line) {
3337 if (view->searching == TOG_SEARCH_FORWARD)
3338 lineno = s->matched_line + 1;
3340 lineno = s->matched_line - 1;
3342 if (view->searching == TOG_SEARCH_FORWARD)
3353 if (lineno <= 0 || lineno > s->nlines) {
3354 if (s->matched_line == 0) {
3355 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3360 if (view->searching == TOG_SEARCH_FORWARD)
3366 offset = s->line_offsets[lineno - 1];
3367 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3369 return got_error_from_errno("fseeko");
3372 line = parse_next_line(s->f, &len);
3374 match_line(line, &view->regex, 1, &view->regmatch)) {
3375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3376 s->matched_line = lineno;
3381 if (view->searching == TOG_SEARCH_FORWARD)
3387 if (s->matched_line) {
3388 s->first_displayed_line = s->matched_line;
3389 s->selected_line = 1;
3395 static const struct got_error *
3396 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3397 struct got_object_id *id2, const char *label1, const char *label2,
3398 int diff_context, int ignore_whitespace, int force_text_diff,
3399 struct tog_view *log_view, struct got_repository *repo)
3401 const struct got_error *err;
3402 struct tog_diff_view_state *s = &view->state.diff;
3404 SIMPLEQ_INIT(&s->refs);
3406 if (id1 != NULL && id2 != NULL) {
3408 err = got_object_get_type(&type1, repo, id1);
3411 err = got_object_get_type(&type2, repo, id2);
3416 return got_error(GOT_ERR_OBJ_TYPE);
3418 s->first_displayed_line = 1;
3419 s->last_displayed_line = view->nlines;
3420 s->selected_line = 1;
3428 s->id1 = got_object_id_dup(id1);
3430 return got_error_from_errno("got_object_id_dup");
3434 s->id2 = got_object_id_dup(id2);
3435 if (s->id2 == NULL) {
3438 return got_error_from_errno("got_object_id_dup");
3441 s->first_displayed_line = 1;
3442 s->last_displayed_line = view->nlines;
3443 s->diff_context = diff_context;
3444 s->ignore_whitespace = ignore_whitespace;
3445 s->force_text_diff = force_text_diff;
3446 s->log_view = log_view;
3449 SIMPLEQ_INIT(&s->colors);
3450 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3451 err = add_color(&s->colors,
3452 "^-", TOG_COLOR_DIFF_MINUS,
3453 get_color_value("TOG_COLOR_DIFF_MINUS"));
3456 err = add_color(&s->colors, "^\\+",
3457 TOG_COLOR_DIFF_PLUS,
3458 get_color_value("TOG_COLOR_DIFF_PLUS"));
3460 free_colors(&s->colors);
3463 err = add_color(&s->colors,
3464 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3465 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3467 free_colors(&s->colors);
3471 err = add_color(&s->colors,
3472 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3473 TOG_COLOR_DIFF_META,
3474 get_color_value("TOG_COLOR_DIFF_META"));
3476 free_colors(&s->colors);
3480 err = add_color(&s->colors,
3481 "^(from|via): ", TOG_COLOR_AUTHOR,
3482 get_color_value("TOG_COLOR_AUTHOR"));
3484 free_colors(&s->colors);
3488 err = add_color(&s->colors,
3489 "^date: ", TOG_COLOR_DATE,
3490 get_color_value("TOG_COLOR_DATE"));
3492 free_colors(&s->colors);
3497 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3503 free_colors(&s->colors);
3507 if (log_view && view_is_splitscreen(view))
3508 show_log_view(log_view); /* draw vborder */
3509 diff_view_indicate_progress(view);
3511 s->line_offsets = NULL;
3513 err = create_diff(s);
3519 free_colors(&s->colors);
3520 got_ref_list_free(&s->refs);
3524 view->show = show_diff_view;
3525 view->input = input_diff_view;
3526 view->close = close_diff_view;
3527 view->search_start = search_start_diff_view;
3528 view->search_next = search_next_diff_view;
3533 static const struct got_error *
3534 close_diff_view(struct tog_view *view)
3536 const struct got_error *err = NULL;
3537 struct tog_diff_view_state *s = &view->state.diff;
3543 if (s->f && fclose(s->f) == EOF)
3544 err = got_error_from_errno("fclose");
3545 free_colors(&s->colors);
3546 free(s->line_offsets);
3547 s->line_offsets = NULL;
3549 got_ref_list_free(&s->refs);
3553 static const struct got_error *
3554 show_diff_view(struct tog_view *view)
3556 const struct got_error *err;
3557 struct tog_diff_view_state *s = &view->state.diff;
3558 char *id_str1 = NULL, *id_str2, *header;
3559 const char *label1, *label2;
3562 err = got_object_id_str(&id_str1, s->id1);
3565 label1 = s->label1 ? : id_str1;
3567 label1 = "/dev/null";
3569 err = got_object_id_str(&id_str2, s->id2);
3572 label2 = s->label2 ? : id_str2;
3574 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3575 err = got_error_from_errno("asprintf");
3583 return draw_file(view, header);
3586 static const struct got_error *
3587 set_selected_commit(struct tog_diff_view_state *s,
3588 struct commit_queue_entry *entry)
3590 const struct got_error *err;
3591 const struct got_object_id_queue *parent_ids;
3592 struct got_commit_object *selected_commit;
3593 struct got_object_qid *pid;
3596 s->id2 = got_object_id_dup(entry->id);
3598 return got_error_from_errno("got_object_id_dup");
3600 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3603 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3605 pid = SIMPLEQ_FIRST(parent_ids);
3606 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3607 got_object_commit_close(selected_commit);
3611 static const struct got_error *
3612 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3613 struct tog_view **focus_view, struct tog_view *view, int ch)
3615 const struct got_error *err = NULL;
3616 struct tog_diff_view_state *s = &view->state.diff;
3617 struct tog_log_view_state *ls;
3618 struct commit_queue_entry *entry;
3625 s->force_text_diff = !s->force_text_diff;
3627 s->ignore_whitespace = !s->ignore_whitespace;
3628 wclear(view->window);
3629 s->first_displayed_line = 1;
3630 s->last_displayed_line = view->nlines;
3631 diff_view_indicate_progress(view);
3632 err = create_diff(s);
3636 if (s->first_displayed_line > 1)
3637 s->first_displayed_line--;
3641 if (s->first_displayed_line == 1)
3644 while (i++ < view->nlines - 1 &&
3645 s->first_displayed_line > 1)
3646 s->first_displayed_line--;
3651 s->first_displayed_line++;
3659 while (!s->eof && i++ < view->nlines - 1) {
3661 line = parse_next_line(s->f, NULL);
3662 s->first_displayed_line++;
3668 if (s->diff_context > 0) {
3670 diff_view_indicate_progress(view);
3671 err = create_diff(s);
3672 if (s->first_displayed_line + view->nlines - 1 >
3674 s->first_displayed_line = 1;
3675 s->last_displayed_line = view->nlines;
3680 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3682 diff_view_indicate_progress(view);
3683 err = create_diff(s);
3688 if (s->log_view == NULL)
3690 ls = &s->log_view->state.log;
3691 entry = TAILQ_PREV(ls->selected_entry,
3692 commit_queue_head, entry);
3696 err = input_log_view(NULL, NULL, NULL, s->log_view,
3701 err = set_selected_commit(s, entry);
3705 s->first_displayed_line = 1;
3706 s->last_displayed_line = view->nlines;
3708 diff_view_indicate_progress(view);
3709 err = create_diff(s);
3713 if (s->log_view == NULL)
3715 ls = &s->log_view->state.log;
3717 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3718 ls->thread_args.commits_needed++;
3719 err = trigger_log_thread(s->log_view, 1);
3723 err = input_log_view(NULL, NULL, NULL, s->log_view,
3728 entry = TAILQ_NEXT(ls->selected_entry, entry);
3732 err = set_selected_commit(s, entry);
3736 s->first_displayed_line = 1;
3737 s->last_displayed_line = view->nlines;
3739 diff_view_indicate_progress(view);
3740 err = create_diff(s);
3749 static const struct got_error *
3750 cmd_diff(int argc, char *argv[])
3752 const struct got_error *error = NULL;
3753 struct got_repository *repo = NULL;
3754 struct got_worktree *worktree = NULL;
3755 struct got_object_id *id1 = NULL, *id2 = NULL;
3756 char *repo_path = NULL, *cwd = NULL;
3757 char *id_str1 = NULL, *id_str2 = NULL;
3758 char *label1 = NULL, *label2 = NULL;
3759 int diff_context = 3, ignore_whitespace = 0;
3760 int ch, force_text_diff = 0;
3762 struct tog_view *view;
3765 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3769 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3772 force_text_diff = 1;
3775 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3778 err(1, "-C option %s", errstr);
3781 repo_path = realpath(optarg, NULL);
3782 if (repo_path == NULL)
3783 return got_error_from_errno2("realpath",
3785 got_path_strip_trailing_slashes(repo_path);
3788 ignore_whitespace = 1;
3800 usage_diff(); /* TODO show local worktree changes */
3801 } else if (argc == 2) {
3807 cwd = getcwd(NULL, 0);
3809 return got_error_from_errno("getcwd");
3811 error = got_worktree_open(&worktree, cwd);
3812 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3815 if (repo_path == NULL) {
3818 strdup(got_worktree_get_repo_path(worktree));
3820 repo_path = strdup(cwd);
3822 if (repo_path == NULL) {
3823 error = got_error_from_errno("strdup");
3827 error = got_repo_open(&repo, repo_path, NULL);
3833 error = apply_unveil(got_repo_get_path(repo), NULL);
3837 error = got_repo_match_object_id(&id1, &label1, id_str1,
3838 GOT_OBJ_TYPE_ANY, 1, repo);
3842 error = got_repo_match_object_id(&id2, &label2, id_str2,
3843 GOT_OBJ_TYPE_ANY, 1, repo);
3847 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3849 error = got_error_from_errno("view_open");
3852 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3853 ignore_whitespace, force_text_diff, NULL, repo);
3856 error = view_loop(view);
3863 got_repo_close(repo);
3865 got_worktree_close(worktree);
3873 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3878 struct tog_blame_line {
3880 struct got_object_id *id;
3883 static const struct got_error *
3884 draw_blame(struct tog_view *view)
3886 struct tog_blame_view_state *s = &view->state.blame;
3887 struct tog_blame *blame = &s->blame;
3888 regmatch_t *regmatch = &view->regmatch;
3889 const struct got_error *err;
3890 int lineno = 0, nprinted = 0;
3895 struct tog_blame_line *blame_line;
3896 struct got_object_id *prev_id = NULL;
3898 struct tog_color *tc;
3900 err = got_object_id_str(&id_str, s->blamed_commit->id);
3905 werase(view->window);
3907 if (asprintf(&line, "commit %s", id_str) == -1) {
3908 err = got_error_from_errno("asprintf");
3913 err = format_line(&wline, &width, line, view->ncols, 0);
3918 if (view_needs_focus_indication(view))
3919 wstandout(view->window);
3920 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3922 wattr_on(view->window,
3923 COLOR_PAIR(tc->colorpair), NULL);
3924 waddwstr(view->window, wline);
3926 wattr_off(view->window,
3927 COLOR_PAIR(tc->colorpair), NULL);
3928 if (view_needs_focus_indication(view))
3929 wstandend(view->window);
3932 if (width < view->ncols - 1)
3933 waddch(view->window, '\n');
3935 if (asprintf(&line, "[%d/%d] %s%s",
3936 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3937 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3939 return got_error_from_errno("asprintf");
3942 err = format_line(&wline, &width, line, view->ncols, 0);
3947 waddwstr(view->window, wline);
3950 if (width < view->ncols - 1)
3951 waddch(view->window, '\n');
3954 while (nprinted < view->nlines - 2) {
3955 line = parse_next_line(blame->f, &len);
3960 if (++lineno < s->first_displayed_line) {
3965 if (view->focussed && nprinted == s->selected_line - 1)
3966 wstandout(view->window);
3968 if (blame->nlines > 0) {
3969 blame_line = &blame->lines[lineno - 1];
3970 if (blame_line->annotated && prev_id &&
3971 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3973 nprinted == s->selected_line - 1)) {
3974 waddstr(view->window, " ");
3975 } else if (blame_line->annotated) {
3977 err = got_object_id_str(&id_str, blame_line->id);
3982 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3984 wattr_on(view->window,
3985 COLOR_PAIR(tc->colorpair), NULL);
3986 wprintw(view->window, "%.8s", id_str);
3988 wattr_off(view->window,
3989 COLOR_PAIR(tc->colorpair), NULL);
3991 prev_id = blame_line->id;
3993 waddstr(view->window, "........");
3997 waddstr(view->window, "........");
4001 if (view->focussed && nprinted == s->selected_line - 1)
4002 wstandend(view->window);
4003 waddstr(view->window, " ");
4005 if (view->ncols <= 9) {
4007 wline = wcsdup(L"");
4008 if (wline == NULL) {
4009 err = got_error_from_errno("wcsdup");
4013 } else if (s->first_displayed_line + nprinted ==
4015 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4016 err = add_matched_line(&width, line, view->ncols - 9, 9,
4017 view->window, regmatch);
4024 err = format_line(&wline, &width, line,
4025 view->ncols - 9, 9);
4026 waddwstr(view->window, wline);
4032 if (width <= view->ncols - 1)
4033 waddch(view->window, '\n');
4034 if (++nprinted == 1)
4035 s->first_displayed_line = lineno;
4038 s->last_displayed_line = lineno;
4045 static const struct got_error *
4046 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4048 const struct got_error *err = NULL;
4049 struct tog_blame_cb_args *a = arg;
4050 struct tog_blame_line *line;
4053 if (nlines != a->nlines ||
4054 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4055 return got_error(GOT_ERR_RANGE);
4057 errcode = pthread_mutex_lock(&tog_mutex);
4059 return got_error_set_errno(errcode, "pthread_mutex_lock");
4061 if (*a->quit) { /* user has quit the blame view */
4062 err = got_error(GOT_ERR_ITER_COMPLETED);
4067 goto done; /* no change in this commit */
4069 line = &a->lines[lineno - 1];
4070 if (line->annotated)
4073 line->id = got_object_id_dup(id);
4074 if (line->id == NULL) {
4075 err = got_error_from_errno("got_object_id_dup");
4078 line->annotated = 1;
4080 errcode = pthread_mutex_unlock(&tog_mutex);
4082 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4087 blame_thread(void *arg)
4089 const struct got_error *err;
4090 struct tog_blame_thread_args *ta = arg;
4091 struct tog_blame_cb_args *a = ta->cb_args;
4094 err = block_signals_used_by_main_thread();
4098 err = got_blame(ta->path, a->commit_id, ta->repo,
4099 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4100 if (err && err->code == GOT_ERR_CANCELLED)
4103 errcode = pthread_mutex_lock(&tog_mutex);
4105 return (void *)got_error_set_errno(errcode,
4106 "pthread_mutex_lock");
4108 got_repo_close(ta->repo);
4112 errcode = pthread_mutex_unlock(&tog_mutex);
4113 if (errcode && err == NULL)
4114 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4119 static struct got_object_id *
4120 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4121 int first_displayed_line, int selected_line)
4123 struct tog_blame_line *line;
4128 line = &lines[first_displayed_line - 1 + selected_line - 1];
4129 if (!line->annotated)
4135 static const struct got_error *
4136 stop_blame(struct tog_blame *blame)
4138 const struct got_error *err = NULL;
4141 if (blame->thread) {
4143 errcode = pthread_mutex_unlock(&tog_mutex);
4145 return got_error_set_errno(errcode,
4146 "pthread_mutex_unlock");
4147 errcode = pthread_join(blame->thread, (void **)&err);
4149 return got_error_set_errno(errcode, "pthread_join");
4150 errcode = pthread_mutex_lock(&tog_mutex);
4152 return got_error_set_errno(errcode,
4153 "pthread_mutex_lock");
4154 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4156 blame->thread = NULL;
4158 if (blame->thread_args.repo) {
4159 got_repo_close(blame->thread_args.repo);
4160 blame->thread_args.repo = NULL;
4163 if (fclose(blame->f) != 0 && err == NULL)
4164 err = got_error_from_errno("fclose");
4168 for (i = 0; i < blame->nlines; i++)
4169 free(blame->lines[i].id);
4171 blame->lines = NULL;
4173 free(blame->cb_args.commit_id);
4174 blame->cb_args.commit_id = NULL;
4179 static const struct got_error *
4180 cancel_blame_view(void *arg)
4182 const struct got_error *err = NULL;
4186 errcode = pthread_mutex_lock(&tog_mutex);
4188 return got_error_set_errno(errcode,
4189 "pthread_mutex_unlock");
4192 err = got_error(GOT_ERR_CANCELLED);
4194 errcode = pthread_mutex_unlock(&tog_mutex);
4196 return got_error_set_errno(errcode,
4197 "pthread_mutex_lock");
4202 static const struct got_error *
4203 run_blame(struct tog_view *view)
4205 struct tog_blame_view_state *s = &view->state.blame;
4206 struct tog_blame *blame = &s->blame;
4207 const struct got_error *err = NULL;
4208 struct got_blob_object *blob = NULL;
4209 struct got_repository *thread_repo = NULL;
4210 struct got_object_id *obj_id = NULL;
4213 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4218 err = got_object_get_type(&obj_type, s->repo, obj_id);
4222 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4223 err = got_error(GOT_ERR_OBJ_TYPE);
4227 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4230 blame->f = got_opentemp();
4231 if (blame->f == NULL) {
4232 err = got_error_from_errno("got_opentemp");
4235 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4236 &blame->line_offsets, blame->f, blob);
4237 if (err || blame->nlines == 0)
4240 /* Don't include \n at EOF in the blame line count. */
4241 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4244 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4245 if (blame->lines == NULL) {
4246 err = got_error_from_errno("calloc");
4250 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4254 blame->cb_args.view = view;
4255 blame->cb_args.lines = blame->lines;
4256 blame->cb_args.nlines = blame->nlines;
4257 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4258 if (blame->cb_args.commit_id == NULL) {
4259 err = got_error_from_errno("got_object_id_dup");
4262 blame->cb_args.quit = &s->done;
4264 blame->thread_args.path = s->path;
4265 blame->thread_args.repo = thread_repo;
4266 blame->thread_args.cb_args = &blame->cb_args;
4267 blame->thread_args.complete = &s->blame_complete;
4268 blame->thread_args.cancel_cb = cancel_blame_view;
4269 blame->thread_args.cancel_arg = &s->done;
4270 s->blame_complete = 0;
4274 got_object_blob_close(blob);
4281 static const struct got_error *
4282 open_blame_view(struct tog_view *view, char *path,
4283 struct got_object_id *commit_id, struct got_repository *repo)
4285 const struct got_error *err = NULL;
4286 struct tog_blame_view_state *s = &view->state.blame;
4288 SIMPLEQ_INIT(&s->blamed_commits);
4290 s->path = strdup(path);
4291 if (s->path == NULL)
4292 return got_error_from_errno("strdup");
4294 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4300 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4301 s->first_displayed_line = 1;
4302 s->last_displayed_line = view->nlines;
4303 s->selected_line = 1;
4304 s->blame_complete = 0;
4306 s->commit_id = commit_id;
4307 memset(&s->blame, 0, sizeof(s->blame));
4309 SIMPLEQ_INIT(&s->colors);
4310 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4311 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4312 get_color_value("TOG_COLOR_COMMIT"));
4317 view->show = show_blame_view;
4318 view->input = input_blame_view;
4319 view->close = close_blame_view;
4320 view->search_start = search_start_blame_view;
4321 view->search_next = search_next_blame_view;
4323 return run_blame(view);
4326 static const struct got_error *
4327 close_blame_view(struct tog_view *view)
4329 const struct got_error *err = NULL;
4330 struct tog_blame_view_state *s = &view->state.blame;
4332 if (s->blame.thread)
4333 err = stop_blame(&s->blame);
4335 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4336 struct got_object_qid *blamed_commit;
4337 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4338 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4339 got_object_qid_free(blamed_commit);
4343 free_colors(&s->colors);
4348 static const struct got_error *
4349 search_start_blame_view(struct tog_view *view)
4351 struct tog_blame_view_state *s = &view->state.blame;
4353 s->matched_line = 0;
4357 static const struct got_error *
4358 search_next_blame_view(struct tog_view *view)
4360 struct tog_blame_view_state *s = &view->state.blame;
4363 if (!view->searching) {
4364 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4368 if (s->matched_line) {
4369 if (view->searching == TOG_SEARCH_FORWARD)
4370 lineno = s->matched_line + 1;
4372 lineno = s->matched_line - 1;
4374 if (view->searching == TOG_SEARCH_FORWARD)
4377 lineno = s->blame.nlines;
4385 if (lineno <= 0 || lineno > s->blame.nlines) {
4386 if (s->matched_line == 0) {
4387 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4392 if (view->searching == TOG_SEARCH_FORWARD)
4395 lineno = s->blame.nlines;
4398 offset = s->blame.line_offsets[lineno - 1];
4399 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4401 return got_error_from_errno("fseeko");
4404 line = parse_next_line(s->blame.f, &len);
4406 match_line(line, &view->regex, 1, &view->regmatch)) {
4407 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4408 s->matched_line = lineno;
4413 if (view->searching == TOG_SEARCH_FORWARD)
4419 if (s->matched_line) {
4420 s->first_displayed_line = s->matched_line;
4421 s->selected_line = 1;
4427 static const struct got_error *
4428 show_blame_view(struct tog_view *view)
4430 const struct got_error *err = NULL;
4431 struct tog_blame_view_state *s = &view->state.blame;
4434 if (s->blame.thread == NULL) {
4435 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4436 &s->blame.thread_args);
4438 return got_error_set_errno(errcode, "pthread_create");
4440 halfdelay(1); /* fast refresh while annotating */
4443 if (s->blame_complete)
4444 halfdelay(10); /* disable fast refresh */
4446 err = draw_blame(view);
4452 static const struct got_error *
4453 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4454 struct tog_view **focus_view, struct tog_view *view, int ch)
4456 const struct got_error *err = NULL, *thread_err = NULL;
4457 struct tog_view *diff_view;
4458 struct tog_blame_view_state *s = &view->state.blame;
4467 if (s->selected_line > 1)
4469 else if (s->selected_line == 1 &&
4470 s->first_displayed_line > 1)
4471 s->first_displayed_line--;
4475 if (s->first_displayed_line == 1) {
4476 s->selected_line = 1;
4479 if (s->first_displayed_line > view->nlines - 2)
4480 s->first_displayed_line -=
4483 s->first_displayed_line = 1;
4487 if (s->selected_line < view->nlines - 2 &&
4488 s->first_displayed_line +
4489 s->selected_line <= s->blame.nlines)
4491 else if (s->last_displayed_line <
4493 s->first_displayed_line++;
4497 struct got_object_id *id = NULL;
4498 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4499 s->first_displayed_line, s->selected_line);
4503 struct got_commit_object *commit;
4504 struct got_object_qid *pid;
4505 struct got_object_id *blob_id = NULL;
4507 err = got_object_open_as_commit(&commit,
4511 pid = SIMPLEQ_FIRST(
4512 got_object_commit_get_parent_ids(commit));
4514 got_object_commit_close(commit);
4517 /* Check if path history ends here. */
4518 err = got_object_id_by_path(&blob_id, s->repo,
4521 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4523 got_object_commit_close(commit);
4526 err = got_object_get_type(&obj_type, s->repo,
4529 /* Can't blame non-blob type objects. */
4530 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4531 got_object_commit_close(commit);
4534 err = got_object_qid_alloc(&s->blamed_commit,
4536 got_object_commit_close(commit);
4538 if (got_object_id_cmp(id,
4539 s->blamed_commit->id) == 0)
4541 err = got_object_qid_alloc(&s->blamed_commit,
4547 thread_err = stop_blame(&s->blame);
4551 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4552 s->blamed_commit, entry);
4553 err = run_blame(view);
4559 struct got_object_qid *first;
4560 first = SIMPLEQ_FIRST(&s->blamed_commits);
4561 if (!got_object_id_cmp(first->id, s->commit_id))
4564 thread_err = stop_blame(&s->blame);
4568 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4569 got_object_qid_free(s->blamed_commit);
4571 SIMPLEQ_FIRST(&s->blamed_commits);
4572 err = run_blame(view);
4579 struct got_object_id *id = NULL;
4580 struct got_object_qid *pid;
4581 struct got_commit_object *commit = NULL;
4582 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4583 s->first_displayed_line, s->selected_line);
4586 err = got_object_open_as_commit(&commit, s->repo, id);
4589 pid = SIMPLEQ_FIRST(
4590 got_object_commit_get_parent_ids(commit));
4591 if (view_is_parent_view(view))
4592 begin_x = view_split_begin_x(view->begin_x);
4593 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4594 if (diff_view == NULL) {
4595 got_object_commit_close(commit);
4596 err = got_error_from_errno("view_open");
4599 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4600 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4601 got_object_commit_close(commit);
4603 view_close(diff_view);
4606 if (view_is_parent_view(view)) {
4607 err = view_close_child(view);
4610 err = view_set_child(view, diff_view);
4612 view_close(diff_view);
4615 *focus_view = diff_view;
4616 view->child_focussed = 1;
4618 *new_view = diff_view;
4626 if (s->last_displayed_line >= s->blame.nlines &&
4627 s->selected_line >= MIN(s->blame.nlines,
4628 view->nlines - 2)) {
4631 if (s->last_displayed_line >= s->blame.nlines &&
4632 s->selected_line < view->nlines - 2) {
4633 s->selected_line = MIN(s->blame.nlines,
4637 if (s->last_displayed_line + view->nlines - 2
4639 s->first_displayed_line +=
4642 s->first_displayed_line =
4647 if (s->selected_line > view->nlines - 2) {
4648 s->selected_line = MIN(s->blame.nlines,
4655 return thread_err ? thread_err : err;
4658 static const struct got_error *
4659 cmd_blame(int argc, char *argv[])
4661 const struct got_error *error;
4662 struct got_repository *repo = NULL;
4663 struct got_worktree *worktree = NULL;
4664 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4665 char *link_target = NULL;
4666 struct got_object_id *commit_id = NULL;
4667 char *commit_id_str = NULL;
4669 struct tog_view *view;
4672 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4677 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4680 commit_id_str = optarg;
4683 repo_path = realpath(optarg, NULL);
4684 if (repo_path == NULL)
4685 return got_error_from_errno2("realpath",
4700 cwd = getcwd(NULL, 0);
4702 return got_error_from_errno("getcwd");
4704 error = got_worktree_open(&worktree, cwd);
4705 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4708 if (repo_path == NULL) {
4711 strdup(got_worktree_get_repo_path(worktree));
4713 repo_path = strdup(cwd);
4715 if (repo_path == NULL) {
4716 error = got_error_from_errno("strdup");
4720 error = got_repo_open(&repo, repo_path, NULL);
4724 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4731 error = apply_unveil(got_repo_get_path(repo), NULL);
4735 if (commit_id_str == NULL) {
4736 struct got_reference *head_ref;
4737 error = got_ref_open(&head_ref, repo, worktree ?
4738 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4741 error = got_ref_resolve(&commit_id, repo, head_ref);
4742 got_ref_close(head_ref);
4744 error = got_repo_match_object_id(&commit_id, NULL,
4745 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4750 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4752 error = got_error_from_errno("view_open");
4756 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4761 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4766 /* Release work tree lock. */
4767 got_worktree_close(worktree);
4770 error = view_loop(view);
4778 got_worktree_close(worktree);
4780 got_repo_close(repo);
4784 static const struct got_error *
4785 draw_tree_entries(struct tog_view *view, const char *parent_path)
4787 struct tog_tree_view_state *s = &view->state.tree;
4788 const struct got_error *err = NULL;
4789 struct got_tree_entry *te;
4791 struct tog_color *tc;
4792 int width, n, i, nentries;
4793 int limit = view->nlines;
4797 werase(view->window);
4802 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4805 if (view_needs_focus_indication(view))
4806 wstandout(view->window);
4807 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4809 wattr_on(view->window,
4810 COLOR_PAIR(tc->colorpair), NULL);
4811 waddwstr(view->window, wline);
4813 wattr_off(view->window,
4814 COLOR_PAIR(tc->colorpair), NULL);
4815 if (view_needs_focus_indication(view))
4816 wstandend(view->window);
4819 if (width < view->ncols - 1)
4820 waddch(view->window, '\n');
4823 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4826 waddwstr(view->window, wline);
4829 if (width < view->ncols - 1)
4830 waddch(view->window, '\n');
4833 waddch(view->window, '\n');
4837 if (s->first_displayed_entry == NULL) {
4838 te = got_object_tree_get_first_entry(s->tree);
4839 if (s->selected == 0) {
4841 wstandout(view->window);
4842 s->selected_entry = NULL;
4844 waddstr(view->window, " ..\n"); /* parent directory */
4845 if (s->selected == 0 && view->focussed)
4846 wstandend(view->window);
4853 te = s->first_displayed_entry;
4856 nentries = got_object_tree_get_nentries(s->tree);
4857 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4858 char *line = NULL, *id_str = NULL, *link_target = NULL;
4859 const char *modestr = "";
4862 te = got_object_tree_get_entry(s->tree, i);
4863 mode = got_tree_entry_get_mode(te);
4866 err = got_object_id_str(&id_str,
4867 got_tree_entry_get_id(te));
4869 return got_error_from_errno(
4870 "got_object_id_str");
4872 if (got_object_tree_entry_is_submodule(te))
4874 else if (S_ISLNK(mode)) {
4877 err = got_tree_entry_get_symlink_target(&link_target,
4883 for (i = 0; i < strlen(link_target); i++) {
4884 if (!isprint((unsigned char)link_target[i]))
4885 link_target[i] = '?';
4889 else if (S_ISDIR(mode))
4891 else if (mode & S_IXUSR)
4893 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4894 got_tree_entry_get_name(te), modestr,
4895 link_target ? " -> ": "",
4896 link_target ? link_target : "") == -1) {
4899 return got_error_from_errno("asprintf");
4903 err = format_line(&wline, &width, line, view->ncols, 0);
4908 if (n == s->selected) {
4910 wstandout(view->window);
4911 s->selected_entry = te;
4913 tc = match_color(&s->colors, line);
4915 wattr_on(view->window,
4916 COLOR_PAIR(tc->colorpair), NULL);
4917 waddwstr(view->window, wline);
4919 wattr_off(view->window,
4920 COLOR_PAIR(tc->colorpair), NULL);
4921 if (width < view->ncols - 1)
4922 waddch(view->window, '\n');
4923 if (n == s->selected && view->focussed)
4924 wstandend(view->window);
4930 s->last_displayed_entry = te;
4939 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4941 struct got_tree_entry *te;
4942 int isroot = s->tree == s->root;
4945 if (s->first_displayed_entry == NULL)
4948 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4949 while (i++ < maxscroll) {
4952 s->first_displayed_entry = NULL;
4955 s->first_displayed_entry = te;
4956 te = got_tree_entry_get_prev(s->tree, te);
4961 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4963 struct got_tree_entry *next, *last;
4966 if (s->first_displayed_entry)
4967 next = got_tree_entry_get_next(s->tree,
4968 s->first_displayed_entry);
4970 next = got_object_tree_get_first_entry(s->tree);
4972 last = s->last_displayed_entry;
4973 while (next && last && n++ < maxscroll) {
4974 last = got_tree_entry_get_next(s->tree, last);
4976 s->first_displayed_entry = next;
4977 next = got_tree_entry_get_next(s->tree, next);
4982 static const struct got_error *
4983 tree_entry_path(char **path, struct tog_parent_trees *parents,
4984 struct got_tree_entry *te)
4986 const struct got_error *err = NULL;
4987 struct tog_parent_tree *pt;
4988 size_t len = 2; /* for leading slash and NUL */
4990 TAILQ_FOREACH(pt, parents, entry)
4991 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4994 len += strlen(got_tree_entry_get_name(te));
4996 *path = calloc(1, len);
4998 return got_error_from_errno("calloc");
5001 pt = TAILQ_LAST(parents, tog_parent_trees);
5003 const char *name = got_tree_entry_get_name(pt->selected_entry);
5004 if (strlcat(*path, name, len) >= len) {
5005 err = got_error(GOT_ERR_NO_SPACE);
5008 if (strlcat(*path, "/", len) >= len) {
5009 err = got_error(GOT_ERR_NO_SPACE);
5012 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5015 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5016 err = got_error(GOT_ERR_NO_SPACE);
5028 static const struct got_error *
5029 blame_tree_entry(struct tog_view **new_view, int begin_x,
5030 struct got_tree_entry *te, struct tog_parent_trees *parents,
5031 struct got_object_id *commit_id, struct got_repository *repo)
5033 const struct got_error *err = NULL;
5035 struct tog_view *blame_view;
5039 err = tree_entry_path(&path, parents, te);
5043 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5044 if (blame_view == NULL) {
5045 err = got_error_from_errno("view_open");
5049 err = open_blame_view(blame_view, path, commit_id, repo);
5051 if (err->code == GOT_ERR_CANCELLED)
5053 view_close(blame_view);
5055 *new_view = blame_view;
5061 static const struct got_error *
5062 log_tree_entry(struct tog_view **new_view, int begin_x,
5063 struct got_tree_entry *te, struct tog_parent_trees *parents,
5064 struct got_object_id *commit_id, struct got_repository *repo)
5066 struct tog_view *log_view;
5067 const struct got_error *err = NULL;
5072 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5073 if (log_view == NULL)
5074 return got_error_from_errno("view_open");
5076 err = tree_entry_path(&path, parents, te);
5080 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5082 view_close(log_view);
5084 *new_view = log_view;
5089 static const struct got_error *
5090 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5091 struct got_object_id *commit_id, struct got_repository *repo)
5093 const struct got_error *err = NULL;
5094 char *commit_id_str = NULL;
5095 struct tog_tree_view_state *s = &view->state.tree;
5097 TAILQ_INIT(&s->parents);
5099 err = got_object_id_str(&commit_id_str, commit_id);
5103 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5104 err = got_error_from_errno("asprintf");
5108 s->root = s->tree = root;
5109 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5110 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5111 s->commit_id = got_object_id_dup(commit_id);
5112 if (s->commit_id == NULL) {
5113 err = got_error_from_errno("got_object_id_dup");
5118 SIMPLEQ_INIT(&s->colors);
5120 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5121 err = add_color(&s->colors, "\\$$",
5122 TOG_COLOR_TREE_SUBMODULE,
5123 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5126 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5127 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5129 free_colors(&s->colors);
5132 err = add_color(&s->colors, "/$",
5133 TOG_COLOR_TREE_DIRECTORY,
5134 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5136 free_colors(&s->colors);
5140 err = add_color(&s->colors, "\\*$",
5141 TOG_COLOR_TREE_EXECUTABLE,
5142 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5144 free_colors(&s->colors);
5148 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5149 get_color_value("TOG_COLOR_COMMIT"));
5151 free_colors(&s->colors);
5156 view->show = show_tree_view;
5157 view->input = input_tree_view;
5158 view->close = close_tree_view;
5159 view->search_start = search_start_tree_view;
5160 view->search_next = search_next_tree_view;
5162 free(commit_id_str);
5164 free(s->tree_label);
5165 s->tree_label = NULL;
5170 static const struct got_error *
5171 close_tree_view(struct tog_view *view)
5173 struct tog_tree_view_state *s = &view->state.tree;
5175 free_colors(&s->colors);
5176 free(s->tree_label);
5177 s->tree_label = NULL;
5179 s->commit_id = NULL;
5180 while (!TAILQ_EMPTY(&s->parents)) {
5181 struct tog_parent_tree *parent;
5182 parent = TAILQ_FIRST(&s->parents);
5183 TAILQ_REMOVE(&s->parents, parent, entry);
5187 if (s->tree != s->root)
5188 got_object_tree_close(s->tree);
5189 got_object_tree_close(s->root);
5193 static const struct got_error *
5194 search_start_tree_view(struct tog_view *view)
5196 struct tog_tree_view_state *s = &view->state.tree;
5198 s->matched_entry = NULL;
5203 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5205 regmatch_t regmatch;
5207 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5211 static const struct got_error *
5212 search_next_tree_view(struct tog_view *view)
5214 struct tog_tree_view_state *s = &view->state.tree;
5215 struct got_tree_entry *te = NULL;
5217 if (!view->searching) {
5218 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5222 if (s->matched_entry) {
5223 if (view->searching == TOG_SEARCH_FORWARD) {
5224 if (s->selected_entry)
5225 te = got_tree_entry_get_next(s->tree,
5228 te = got_object_tree_get_first_entry(s->tree);
5230 if (s->selected_entry == NULL)
5231 te = got_object_tree_get_last_entry(s->tree);
5233 te = got_tree_entry_get_prev(s->tree,
5237 if (view->searching == TOG_SEARCH_FORWARD)
5238 te = got_object_tree_get_first_entry(s->tree);
5240 te = got_object_tree_get_last_entry(s->tree);
5245 if (s->matched_entry == NULL) {
5246 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5249 if (view->searching == TOG_SEARCH_FORWARD)
5250 te = got_object_tree_get_first_entry(s->tree);
5252 te = got_object_tree_get_last_entry(s->tree);
5255 if (match_tree_entry(te, &view->regex)) {
5256 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5257 s->matched_entry = te;
5261 if (view->searching == TOG_SEARCH_FORWARD)
5262 te = got_tree_entry_get_next(s->tree, te);
5264 te = got_tree_entry_get_prev(s->tree, te);
5267 if (s->matched_entry) {
5268 s->first_displayed_entry = s->matched_entry;
5275 static const struct got_error *
5276 show_tree_view(struct tog_view *view)
5278 const struct got_error *err = NULL;
5279 struct tog_tree_view_state *s = &view->state.tree;
5282 err = tree_entry_path(&parent_path, &s->parents, NULL);
5286 err = draw_tree_entries(view, parent_path);
5293 static const struct got_error *
5294 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5295 struct tog_view **focus_view, struct tog_view *view, int ch)
5297 const struct got_error *err = NULL;
5298 struct tog_tree_view_state *s = &view->state.tree;
5299 struct tog_view *log_view, *ref_view;
5304 s->show_ids = !s->show_ids;
5307 if (!s->selected_entry)
5309 if (view_is_parent_view(view))
5310 begin_x = view_split_begin_x(view->begin_x);
5311 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5312 &s->parents, s->commit_id, s->repo);
5313 if (view_is_parent_view(view)) {
5314 err = view_close_child(view);
5317 err = view_set_child(view, log_view);
5319 view_close(log_view);
5322 *focus_view = log_view;
5323 view->child_focussed = 1;
5325 *new_view = log_view;
5328 if (view_is_parent_view(view))
5329 begin_x = view_split_begin_x(view->begin_x);
5330 ref_view = view_open(view->nlines, view->ncols,
5331 view->begin_y, begin_x, TOG_VIEW_REF);
5332 if (ref_view == NULL)
5333 return got_error_from_errno("view_open");
5334 err = open_ref_view(ref_view, s->repo);
5336 view_close(ref_view);
5339 if (view_is_parent_view(view)) {
5340 err = view_close_child(view);
5343 err = view_set_child(view, ref_view);
5345 view_close(ref_view);
5348 *focus_view = ref_view;
5349 view->child_focussed = 1;
5351 *new_view = ref_view;
5355 if (s->selected > 0) {
5359 tree_scroll_up(s, 1);
5363 if (s->tree == s->root) {
5364 if (got_object_tree_get_first_entry(s->tree) ==
5365 s->first_displayed_entry)
5368 if (s->first_displayed_entry == NULL)
5371 tree_scroll_up(s, MAX(0, view->nlines - 3));
5375 if (s->selected < s->ndisplayed - 1) {
5379 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5381 /* can't scroll any further */
5383 tree_scroll_down(s, 1);
5387 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5389 /* can't scroll any further; move cursor down */
5390 if (s->selected < s->ndisplayed - 1)
5391 s->selected = s->ndisplayed - 1;
5394 tree_scroll_down(s, view->nlines - 3);
5399 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5400 struct tog_parent_tree *parent;
5401 /* user selected '..' */
5402 if (s->tree == s->root)
5404 parent = TAILQ_FIRST(&s->parents);
5405 TAILQ_REMOVE(&s->parents, parent,
5407 got_object_tree_close(s->tree);
5408 s->tree = parent->tree;
5409 s->first_displayed_entry =
5410 parent->first_displayed_entry;
5412 parent->selected_entry;
5413 s->selected = parent->selected;
5415 } else if (S_ISDIR(got_tree_entry_get_mode(
5416 s->selected_entry))) {
5417 struct got_tree_object *subtree;
5418 err = got_object_open_as_tree(&subtree, s->repo,
5419 got_tree_entry_get_id(s->selected_entry));
5422 err = tree_view_visit_subtree(s, subtree);
5424 got_object_tree_close(subtree);
5427 } else if (S_ISREG(got_tree_entry_get_mode(
5428 s->selected_entry))) {
5429 struct tog_view *blame_view;
5430 int begin_x = view_is_parent_view(view) ?
5431 view_split_begin_x(view->begin_x) : 0;
5433 err = blame_tree_entry(&blame_view, begin_x,
5434 s->selected_entry, &s->parents,
5435 s->commit_id, s->repo);
5438 if (view_is_parent_view(view)) {
5439 err = view_close_child(view);
5442 err = view_set_child(view, blame_view);
5444 view_close(blame_view);
5447 *focus_view = blame_view;
5448 view->child_focussed = 1;
5450 *new_view = blame_view;
5454 if (s->selected > view->nlines)
5455 s->selected = s->ndisplayed - 1;
5468 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5473 static const struct got_error *
5474 cmd_tree(int argc, char *argv[])
5476 const struct got_error *error;
5477 struct got_repository *repo = NULL;
5478 struct got_worktree *worktree = NULL;
5479 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5480 struct got_object_id *commit_id = NULL;
5481 char *commit_id_arg = NULL;
5482 struct got_commit_object *commit = NULL;
5483 struct got_tree_object *tree = NULL;
5485 struct tog_view *view;
5488 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5493 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5496 commit_id_arg = optarg;
5499 repo_path = realpath(optarg, NULL);
5500 if (repo_path == NULL)
5501 return got_error_from_errno2("realpath",
5516 cwd = getcwd(NULL, 0);
5518 return got_error_from_errno("getcwd");
5520 error = got_worktree_open(&worktree, cwd);
5521 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5524 if (repo_path == NULL) {
5527 strdup(got_worktree_get_repo_path(worktree));
5529 repo_path = strdup(cwd);
5531 if (repo_path == NULL) {
5532 error = got_error_from_errno("strdup");
5536 error = got_repo_open(&repo, repo_path, NULL);
5540 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5547 error = apply_unveil(got_repo_get_path(repo), NULL);
5551 error = got_repo_match_object_id(&commit_id, NULL,
5552 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5553 GOT_OBJ_TYPE_COMMIT, 1, repo);
5557 error = got_object_open_as_commit(&commit, repo, commit_id);
5561 error = got_object_open_as_tree(&tree, repo,
5562 got_object_commit_get_tree_id(commit));
5566 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5568 error = got_error_from_errno("view_open");
5571 error = open_tree_view(view, tree, commit_id, repo);
5574 if (!got_path_is_root_dir(in_repo_path)) {
5575 error = tree_view_walk_path(&view->state.tree, commit_id,
5582 /* Release work tree lock. */
5583 got_worktree_close(worktree);
5586 error = view_loop(view);
5592 got_object_commit_close(commit);
5594 got_object_tree_close(tree);
5596 got_repo_close(repo);
5600 static const struct got_error *
5601 ref_view_load_refs(struct tog_ref_view_state *s)
5603 const struct got_error *err;
5604 struct got_reflist_entry *sre;
5605 struct tog_reflist_entry *re;
5607 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5608 got_ref_cmp_by_name, NULL);
5613 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5614 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5617 re = malloc(sizeof(*re));
5619 return got_error_from_errno("malloc");
5622 re->idx = s->nrefs++;
5623 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5630 ref_view_free_refs(struct tog_ref_view_state *s)
5632 struct tog_reflist_entry *re;
5634 while (!TAILQ_EMPTY(&s->refs)) {
5635 re = TAILQ_FIRST(&s->refs);
5636 TAILQ_REMOVE(&s->refs, re, entry);
5639 got_ref_list_free(&s->simplerefs);
5642 static const struct got_error *
5643 open_ref_view(struct tog_view *view, struct got_repository *repo)
5645 const struct got_error *err = NULL;
5646 struct tog_ref_view_state *s = &view->state.ref;
5648 s->selected_entry = 0;
5651 SIMPLEQ_INIT(&s->simplerefs);
5652 TAILQ_INIT(&s->refs);
5653 SIMPLEQ_INIT(&s->colors);
5655 err = ref_view_load_refs(s);
5659 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5661 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5662 err = add_color(&s->colors, "^refs/heads/",
5663 TOG_COLOR_REFS_HEADS,
5664 get_color_value("TOG_COLOR_REFS_HEADS"));
5668 err = add_color(&s->colors, "^refs/tags/",
5669 TOG_COLOR_REFS_TAGS,
5670 get_color_value("TOG_COLOR_REFS_TAGS"));
5674 err = add_color(&s->colors, "^refs/remotes/",
5675 TOG_COLOR_REFS_REMOTES,
5676 get_color_value("TOG_COLOR_REFS_REMOTES"));
5681 view->show = show_ref_view;
5682 view->input = input_ref_view;
5683 view->close = close_ref_view;
5684 view->search_start = search_start_ref_view;
5685 view->search_next = search_next_ref_view;
5688 free_colors(&s->colors);
5692 static const struct got_error *
5693 close_ref_view(struct tog_view *view)
5695 struct tog_ref_view_state *s = &view->state.ref;
5697 ref_view_free_refs(s);
5698 free_colors(&s->colors);
5703 static const struct got_error *
5704 resolve_reflist_entry(struct got_object_id **commit_id,
5705 struct tog_reflist_entry *re, struct got_repository *repo)
5707 const struct got_error *err = NULL;
5708 struct got_object_id *obj_id;
5709 struct got_tag_object *tag = NULL;
5714 err = got_ref_resolve(&obj_id, repo, re->ref);
5718 err = got_object_get_type(&obj_type, repo, obj_id);
5723 case GOT_OBJ_TYPE_COMMIT:
5724 *commit_id = obj_id;
5726 case GOT_OBJ_TYPE_TAG:
5727 err = got_object_open_as_tag(&tag, repo, obj_id);
5731 err = got_object_get_type(&obj_type, repo,
5732 got_object_tag_get_object_id(tag));
5735 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5736 err = got_error(GOT_ERR_OBJ_TYPE);
5739 *commit_id = got_object_id_dup(
5740 got_object_tag_get_object_id(tag));
5741 if (*commit_id == NULL) {
5742 err = got_error_from_errno("got_object_id_dup");
5747 err = got_error(GOT_ERR_OBJ_TYPE);
5753 got_object_tag_close(tag);
5761 static const struct got_error *
5762 log_ref_entry(struct tog_view **new_view, int begin_x,
5763 struct tog_reflist_entry *re, struct got_repository *repo)
5765 struct tog_view *log_view;
5766 const struct got_error *err = NULL;
5767 struct got_object_id *commit_id = NULL;
5771 err = resolve_reflist_entry(&commit_id, re, repo);
5773 if (err->code != GOT_ERR_OBJ_TYPE)
5779 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5780 if (log_view == NULL) {
5781 err = got_error_from_errno("view_open");
5785 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5788 view_close(log_view);
5790 *new_view = log_view;
5796 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5798 struct tog_reflist_entry *re;
5801 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5804 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5805 while (i++ < maxscroll) {
5808 s->first_displayed_entry = re;
5809 re = TAILQ_PREV(re, tog_reflist_head, entry);
5814 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
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(s, 1);
6147 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6149 ref_scroll_up(s, 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(s, 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(s, 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);