2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
123 SIMPLEQ_ENTRY(tog_color) entry;
127 SIMPLEQ_HEAD(tog_colors, tog_color);
129 static const struct got_error *
130 add_color(struct tog_colors *colors, const char *pattern,
131 int idx, short color)
133 const struct got_error *err = NULL;
134 struct tog_color *tc;
137 if (idx < 1 || idx > COLOR_PAIRS - 1)
140 init_pair(idx, color, -1);
142 tc = calloc(1, sizeof(*tc));
144 return got_error_from_errno("calloc");
145 regerr = regcomp(&tc->regex, pattern,
146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
148 static char regerr_msg[512];
149 static char err_msg[512];
150 regerror(regerr, &tc->regex, regerr_msg,
152 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
154 err = got_error_msg(GOT_ERR_REGEX, err_msg);
159 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
164 free_colors(struct tog_colors *colors)
166 struct tog_color *tc;
168 while (!SIMPLEQ_EMPTY(colors)) {
169 tc = SIMPLEQ_FIRST(colors);
170 SIMPLEQ_REMOVE_HEAD(colors, entry);
177 get_color(struct tog_colors *colors, int colorpair)
179 struct tog_color *tc = NULL;
181 SIMPLEQ_FOREACH(tc, colors, entry) {
182 if (tc->colorpair == colorpair)
190 default_color_value(const char *envvar)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 return COLOR_MAGENTA;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
196 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
198 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
200 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 return COLOR_MAGENTA;
202 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 return COLOR_MAGENTA;
204 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
206 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
208 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
210 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
212 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
214 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
216 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 return COLOR_MAGENTA;
218 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
225 get_color_value(const char *envvar)
227 const char *val = getenv(envvar);
230 return default_color_value(envvar);
232 if (strcasecmp(val, "black") == 0)
234 if (strcasecmp(val, "red") == 0)
236 if (strcasecmp(val, "green") == 0)
238 if (strcasecmp(val, "yellow") == 0)
240 if (strcasecmp(val, "blue") == 0)
242 if (strcasecmp(val, "magenta") == 0)
243 return COLOR_MAGENTA;
244 if (strcasecmp(val, "cyan") == 0)
246 if (strcasecmp(val, "white") == 0)
248 if (strcasecmp(val, "default") == 0)
251 return default_color_value(envvar);
255 struct tog_diff_view_state {
256 struct got_object_id *id1, *id2;
257 const char *label1, *label2;
259 int first_displayed_line;
260 int last_displayed_line;
263 int ignore_whitespace;
265 struct got_repository *repo;
266 struct got_reflist_head refs;
267 struct tog_colors colors;
273 /* passed from log view; may be NULL */
274 struct tog_view *log_view;
277 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 struct tog_log_thread_args {
280 pthread_cond_t need_commits;
281 pthread_cond_t commit_loaded;
283 struct got_commit_graph *graph;
284 struct commit_queue *commits;
285 const char *in_repo_path;
286 struct got_object_id *start_id;
287 struct got_repository *repo;
290 struct commit_queue_entry **first_displayed_entry;
291 struct commit_queue_entry **selected_entry;
293 int *search_next_done;
297 struct tog_log_view_state {
298 struct commit_queue commits;
299 struct commit_queue_entry *first_displayed_entry;
300 struct commit_queue_entry *last_displayed_entry;
301 struct commit_queue_entry *selected_entry;
304 const char *head_ref_name;
306 struct got_repository *repo;
307 struct got_reflist_head refs;
308 struct got_object_id *start_id;
311 struct tog_log_thread_args thread_args;
312 struct commit_queue_entry *matched_entry;
313 struct commit_queue_entry *search_entry;
314 struct tog_colors colors;
317 #define TOG_COLOR_DIFF_MINUS 1
318 #define TOG_COLOR_DIFF_PLUS 2
319 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 #define TOG_COLOR_DIFF_META 4
321 #define TOG_COLOR_TREE_SUBMODULE 5
322 #define TOG_COLOR_TREE_SYMLINK 6
323 #define TOG_COLOR_TREE_DIRECTORY 7
324 #define TOG_COLOR_TREE_EXECUTABLE 8
325 #define TOG_COLOR_COMMIT 9
326 #define TOG_COLOR_AUTHOR 10
327 #define TOG_COLOR_DATE 11
328 #define TOG_COLOR_REFS_HEADS 12
329 #define TOG_COLOR_REFS_TAGS 13
330 #define TOG_COLOR_REFS_REMOTES 14
332 struct tog_blame_cb_args {
333 struct tog_blame_line *lines; /* one per line */
336 struct tog_view *view;
337 struct got_object_id *commit_id;
341 struct tog_blame_thread_args {
343 struct got_repository *repo;
344 struct tog_blame_cb_args *cb_args;
346 got_cancel_cb cancel_cb;
353 struct tog_blame_line *lines;
357 struct tog_blame_thread_args thread_args;
358 struct tog_blame_cb_args cb_args;
362 struct tog_blame_view_state {
363 int first_displayed_line;
364 int last_displayed_line;
369 struct got_object_id_queue blamed_commits;
370 struct got_object_qid *blamed_commit;
372 struct got_repository *repo;
373 struct got_object_id *commit_id;
374 struct tog_blame blame;
376 struct tog_colors colors;
379 struct tog_parent_tree {
380 TAILQ_ENTRY(tog_parent_tree) entry;
381 struct got_tree_object *tree;
382 struct got_tree_entry *first_displayed_entry;
383 struct got_tree_entry *selected_entry;
387 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 struct tog_tree_view_state {
391 struct got_tree_object *root;
392 struct got_tree_object *tree;
393 struct got_tree_entry *first_displayed_entry;
394 struct got_tree_entry *last_displayed_entry;
395 struct got_tree_entry *selected_entry;
396 int ndisplayed, selected, show_ids;
397 struct tog_parent_trees parents;
398 struct got_object_id *commit_id;
399 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
441 TAILQ_HEAD(tog_view_list_head, tog_view);
444 TAILQ_ENTRY(tog_view) entry;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
450 struct tog_view *parent;
451 struct tog_view *child;
454 /* type-specific state */
455 enum tog_view_type type;
457 struct tog_diff_view_state diff;
458 struct tog_log_view_state log;
459 struct tog_blame_view_state blame;
460 struct tog_tree_view_state tree;
461 struct tog_ref_view_state ref;
464 const struct got_error *(*show)(struct tog_view *);
465 const struct got_error *(*input)(struct tog_view **,
466 struct tog_view **, struct tog_view**, struct tog_view *, int);
467 const struct got_error *(*close)(struct tog_view *);
469 const struct got_error *(*search_start)(struct tog_view *);
470 const struct got_error *(*search_next)(struct tog_view *);
472 #define TOG_SEARCH_FORWARD 1
473 #define TOG_SEARCH_BACKWARD 2
474 int search_next_done;
475 #define TOG_SEARCH_HAVE_MORE 1
476 #define TOG_SEARCH_NO_MORE 2
477 #define TOG_SEARCH_HAVE_NONE 3
482 static const struct got_error *open_diff_view(struct tog_view *,
483 struct got_object_id *, struct got_object_id *,
484 const char *, const char *, int, int, int, struct tog_view *,
485 struct got_repository *);
486 static const struct got_error *show_diff_view(struct tog_view *);
487 static const struct got_error *input_diff_view(struct tog_view **,
488 struct tog_view **, struct tog_view **, struct tog_view *, int);
489 static const struct got_error* close_diff_view(struct tog_view *);
490 static const struct got_error *search_start_diff_view(struct tog_view *);
491 static const struct got_error *search_next_diff_view(struct tog_view *);
493 static const struct got_error *open_log_view(struct tog_view *,
494 struct got_object_id *, struct got_repository *,
495 const char *, const char *, int);
496 static const struct got_error * show_log_view(struct tog_view *);
497 static const struct got_error *input_log_view(struct tog_view **,
498 struct tog_view **, struct tog_view **, struct tog_view *, int);
499 static const struct got_error *close_log_view(struct tog_view *);
500 static const struct got_error *search_start_log_view(struct tog_view *);
501 static const struct got_error *search_next_log_view(struct tog_view *);
503 static const struct got_error *open_blame_view(struct tog_view *, char *,
504 struct got_object_id *, struct got_repository *);
505 static const struct got_error *show_blame_view(struct tog_view *);
506 static const struct got_error *input_blame_view(struct tog_view **,
507 struct tog_view **, struct tog_view **, struct tog_view *, int);
508 static const struct got_error *close_blame_view(struct tog_view *);
509 static const struct got_error *search_start_blame_view(struct tog_view *);
510 static const struct got_error *search_next_blame_view(struct tog_view *);
512 static const struct got_error *open_tree_view(struct tog_view *,
513 struct got_tree_object *, struct got_object_id *, struct got_repository *);
514 static const struct got_error *show_tree_view(struct tog_view *);
515 static const struct got_error *input_tree_view(struct tog_view **,
516 struct tog_view **, struct tog_view **, struct tog_view *, int);
517 static const struct got_error *close_tree_view(struct tog_view *);
518 static const struct got_error *search_start_tree_view(struct tog_view *);
519 static const struct got_error *search_next_tree_view(struct tog_view *);
521 static const struct got_error *open_ref_view(struct tog_view *,
522 struct got_repository *);
523 static const struct got_error *show_ref_view(struct tog_view *);
524 static const struct got_error *input_ref_view(struct tog_view **,
525 struct tog_view **, struct tog_view **, struct tog_view *, int);
526 static const struct got_error *close_ref_view(struct tog_view *);
527 static const struct got_error *search_start_ref_view(struct tog_view *);
528 static const struct got_error *search_next_ref_view(struct tog_view *);
530 static volatile sig_atomic_t tog_sigwinch_received;
531 static volatile sig_atomic_t tog_sigpipe_received;
532 static volatile sig_atomic_t tog_sigcont_received;
535 tog_sigwinch(int signo)
537 tog_sigwinch_received = 1;
541 tog_sigpipe(int signo)
543 tog_sigpipe_received = 1;
547 tog_sigcont(int signo)
549 tog_sigcont_received = 1;
552 static const struct got_error *
553 view_close(struct tog_view *view)
555 const struct got_error *err = NULL;
558 view_close(view->child);
562 err = view->close(view);
564 del_panel(view->panel);
566 delwin(view->window);
571 static struct tog_view *
572 view_open(int nlines, int ncols, int begin_y, int begin_x,
573 enum tog_view_type type)
575 struct tog_view *view = calloc(1, sizeof(*view));
583 view->nlines = nlines ? nlines : LINES - begin_y;
584 view->ncols = ncols ? ncols : COLS - begin_x;
585 view->begin_y = begin_y;
586 view->begin_x = begin_x;
587 view->window = newwin(nlines, ncols, begin_y, begin_x);
588 if (view->window == NULL) {
592 view->panel = new_panel(view->window);
593 if (view->panel == NULL ||
594 set_panel_userptr(view->panel, view) != OK) {
599 keypad(view->window, TRUE);
604 view_split_begin_x(int begin_x)
606 if (begin_x > 0 || COLS < 120)
608 return (COLS - MAX(COLS / 2, 80));
611 static const struct got_error *view_resize(struct tog_view *);
613 static const struct got_error *
614 view_splitscreen(struct tog_view *view)
616 const struct got_error *err = NULL;
619 view->begin_x = view_split_begin_x(0);
620 view->nlines = LINES;
621 view->ncols = COLS - view->begin_x;
624 err = view_resize(view);
628 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
629 return got_error_from_errno("mvwin");
634 static const struct got_error *
635 view_fullscreen(struct tog_view *view)
637 const struct got_error *err = NULL;
641 view->nlines = LINES;
645 err = view_resize(view);
649 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
650 return got_error_from_errno("mvwin");
656 view_is_parent_view(struct tog_view *view)
658 return view->parent == NULL;
661 static const struct got_error *
662 view_resize(struct tog_view *view)
666 if (view->lines > LINES)
667 nlines = view->nlines - (view->lines - LINES);
669 nlines = view->nlines + (LINES - view->lines);
671 if (view->cols > COLS)
672 ncols = view->ncols - (view->cols - COLS);
674 ncols = view->ncols + (COLS - view->cols);
676 if (wresize(view->window, nlines, ncols) == ERR)
677 return got_error_from_errno("wresize");
678 if (replace_panel(view->panel, view->window) == ERR)
679 return got_error_from_errno("replace_panel");
680 wclear(view->window);
682 view->nlines = nlines;
688 view->child->begin_x = view_split_begin_x(view->begin_x);
689 if (view->child->begin_x == 0) {
690 view_fullscreen(view->child);
691 if (view->child->focussed)
692 show_panel(view->child->panel);
694 show_panel(view->panel);
696 view_splitscreen(view->child);
697 show_panel(view->child->panel);
704 static const struct got_error *
705 view_close_child(struct tog_view *view)
707 const struct got_error *err = NULL;
709 if (view->child == NULL)
712 err = view_close(view->child);
717 static const struct got_error *
718 view_set_child(struct tog_view *view, struct tog_view *child)
720 const struct got_error *err = NULL;
723 child->parent = view;
728 view_is_splitscreen(struct tog_view *view)
730 return view->begin_x > 0;
739 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
740 cols = 80; /* Default */
746 resize_term(lines, cols);
749 static const struct got_error *
750 view_search_start(struct tog_view *view)
752 const struct got_error *err = NULL;
756 if (view->nlines < 1)
759 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
760 wclrtoeol(view->window);
764 ret = wgetnstr(view->window, pattern, sizeof(pattern));
770 if (view->searching) {
771 regfree(&view->regex);
775 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
776 err = view->search_start(view);
778 regfree(&view->regex);
781 view->searching = TOG_SEARCH_FORWARD;
782 view->search_next_done = 0;
783 view->search_next(view);
789 static const struct got_error *
790 view_input(struct tog_view **new, struct tog_view **dead,
791 struct tog_view **focus, int *done, struct tog_view *view,
792 struct tog_view_list_head *views)
794 const struct got_error *err = NULL;
802 /* Clear "no matches" indicator. */
803 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
804 view->search_next_done == TOG_SEARCH_HAVE_NONE)
805 view->search_next_done = TOG_SEARCH_HAVE_MORE;
807 if (view->searching && !view->search_next_done) {
808 errcode = pthread_mutex_unlock(&tog_mutex);
810 return got_error_set_errno(errcode,
811 "pthread_mutex_unlock");
813 errcode = pthread_mutex_lock(&tog_mutex);
815 return got_error_set_errno(errcode,
816 "pthread_mutex_lock");
817 view->search_next(view);
821 nodelay(stdscr, FALSE);
822 /* Allow threads to make progress while we are waiting for input. */
823 errcode = pthread_mutex_unlock(&tog_mutex);
825 return got_error_set_errno(errcode, "pthread_mutex_unlock");
826 ch = wgetch(view->window);
827 errcode = pthread_mutex_lock(&tog_mutex);
829 return got_error_set_errno(errcode, "pthread_mutex_lock");
830 nodelay(stdscr, TRUE);
832 if (tog_sigwinch_received || tog_sigcont_received) {
834 tog_sigwinch_received = 0;
835 tog_sigcont_received = 0;
836 TAILQ_FOREACH(v, views, entry) {
837 err = view_resize(v);
840 err = v->input(new, dead, focus, v, KEY_RESIZE);
851 *focus = view->child;
852 view->child_focussed = 1;
853 } else if (view->parent) {
854 *focus = view->parent;
855 view->parent->child_focussed = 0;
859 err = view->input(new, dead, focus, view, ch);
866 if (view_is_parent_view(view)) {
867 if (view->child == NULL)
869 if (view_is_splitscreen(view->child)) {
870 *focus = view->child;
871 view->child_focussed = 1;
872 err = view_fullscreen(view->child);
874 err = view_splitscreen(view->child);
877 err = view->child->input(new, dead, focus,
878 view->child, KEY_RESIZE);
880 if (view_is_splitscreen(view)) {
882 view->parent->child_focussed = 1;
883 err = view_fullscreen(view);
885 err = view_splitscreen(view);
889 err = view->input(new, dead, focus, view,
896 if (view->search_start)
897 view_search_start(view);
899 err = view->input(new, dead, focus, view, ch);
903 if (view->search_next) {
904 view->searching = (ch == 'n' ?
905 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
906 view->search_next_done = 0;
907 view->search_next(view);
909 err = view->input(new, dead, focus, view, ch);
912 err = view->input(new, dead, focus, view, ch);
920 view_vborder(struct tog_view *view)
923 struct tog_view *view_above;
926 return view_vborder(view->parent);
928 panel = panel_above(view->panel);
932 view_above = panel_userptr(panel);
933 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
934 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
938 view_needs_focus_indication(struct tog_view *view)
940 if (view_is_parent_view(view)) {
941 if (view->child == NULL || view->child_focussed)
943 if (!view_is_splitscreen(view->child))
945 } else if (!view_is_splitscreen(view))
948 return view->focussed;
951 static const struct got_error *
952 view_loop(struct tog_view *view)
954 const struct got_error *err = NULL;
955 struct tog_view_list_head views;
956 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
957 int fast_refresh = 10;
958 int done = 0, errcode;
960 errcode = pthread_mutex_lock(&tog_mutex);
962 return got_error_set_errno(errcode, "pthread_mutex_lock");
965 TAILQ_INSERT_HEAD(&views, view, entry);
969 err = view->show(view);
974 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
975 /* Refresh fast during initialization, then become slower. */
976 if (fast_refresh && fast_refresh-- == 0)
977 halfdelay(10); /* switch to once per second */
979 err = view_input(&new_view, &dead_view, &focus_view, &done,
984 struct tog_view *prev = NULL;
986 if (view_is_parent_view(dead_view))
987 prev = TAILQ_PREV(dead_view,
988 tog_view_list_head, entry);
989 else if (view->parent != dead_view)
992 if (dead_view->parent)
993 dead_view->parent->child = NULL;
995 TAILQ_REMOVE(&views, dead_view, entry);
997 err = view_close(dead_view);
998 if (err || (dead_view == main_view && new_view == NULL))
1001 if (view == dead_view) {
1006 else if (!TAILQ_EMPTY(&views))
1007 view = TAILQ_LAST(&views,
1008 tog_view_list_head);
1012 if (view->child && view->child_focussed)
1013 focus_view = view->child;
1020 struct tog_view *v, *t;
1021 /* Only allow one parent view per type. */
1022 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1023 if (v->type != new_view->type)
1025 TAILQ_REMOVE(&views, v, entry);
1026 err = view_close(v);
1031 TAILQ_INSERT_TAIL(&views, new_view, entry);
1033 if (focus_view == NULL)
1034 focus_view = new_view;
1037 show_panel(focus_view->panel);
1040 focus_view->focussed = 1;
1043 show_panel(new_view->panel);
1044 if (view->child && view_is_splitscreen(view->child))
1045 show_panel(view->child->panel);
1048 if (focus_view == NULL) {
1050 show_panel(view->panel);
1051 if (view->child && view_is_splitscreen(view->child))
1052 show_panel(view->child->panel);
1056 err = view->parent->show(view->parent);
1060 err = view->show(view);
1064 err = view->child->show(view->child);
1073 while (!TAILQ_EMPTY(&views)) {
1074 view = TAILQ_FIRST(&views);
1075 TAILQ_REMOVE(&views, view, entry);
1079 errcode = pthread_mutex_unlock(&tog_mutex);
1080 if (errcode && err == NULL)
1081 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1091 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1096 /* Create newly allocated wide-character string equivalent to a byte string. */
1097 static const struct got_error *
1098 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1101 const struct got_error *err = NULL;
1104 *wlen = mbstowcs(NULL, s, 0);
1105 if (*wlen == (size_t)-1) {
1107 if (errno != EILSEQ)
1108 return got_error_from_errno("mbstowcs");
1110 /* byte string invalid in current encoding; try to "fix" it */
1111 err = got_mbsavis(&vis, &vislen, s);
1114 *wlen = mbstowcs(NULL, vis, 0);
1115 if (*wlen == (size_t)-1) {
1116 err = got_error_from_errno("mbstowcs"); /* give up */
1121 *ws = calloc(*wlen + 1, sizeof(**ws));
1123 err = got_error_from_errno("calloc");
1127 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1128 err = got_error_from_errno("mbstowcs");
1139 /* Format a line for display, ensuring that it won't overflow a width limit. */
1140 static const struct got_error *
1141 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1144 const struct got_error *err = NULL;
1146 wchar_t *wline = NULL;
1153 err = mbs2ws(&wline, &wlen, line);
1159 int width = wcwidth(wline[i]);
1166 if (width == 1 || width == 2) {
1167 if (cols + width > wlimit)
1171 } else if (width == -1) {
1172 if (wline[i] == L'\t') {
1174 ((cols + col_tab_align) % TABSIZE);
1175 if (cols + width > wlimit)
1181 err = got_error_from_errno("wcwidth");
1196 static const struct got_error*
1197 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1198 struct got_object_id *id, struct got_repository *repo)
1200 static const struct got_error *err = NULL;
1201 struct got_reflist_entry *re;
1207 SIMPLEQ_FOREACH(re, refs, entry) {
1208 struct got_tag_object *tag = NULL;
1209 struct got_object_id *ref_id;
1212 name = got_ref_get_name(re->ref);
1213 if (strcmp(name, GOT_REF_HEAD) == 0)
1215 if (strncmp(name, "refs/", 5) == 0)
1217 if (strncmp(name, "got/", 4) == 0)
1219 if (strncmp(name, "heads/", 6) == 0)
1221 if (strncmp(name, "remotes/", 8) == 0) {
1223 s = strstr(name, "/" GOT_REF_HEAD);
1224 if (s != NULL && s[strlen(s)] == '\0')
1227 err = got_ref_resolve(&ref_id, repo, re->ref);
1230 if (strncmp(name, "tags/", 5) == 0) {
1231 err = got_object_open_as_tag(&tag, repo, ref_id);
1233 if (err->code != GOT_ERR_OBJ_TYPE) {
1237 /* Ref points at something other than a tag. */
1242 cmp = got_object_id_cmp(tag ?
1243 got_object_tag_get_object_id(tag) : ref_id, id);
1246 got_object_tag_close(tag);
1250 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1251 s ? ", " : "", name) == -1) {
1252 err = got_error_from_errno("asprintf");
1263 static const struct got_error *
1264 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1267 char *smallerthan, *at;
1269 smallerthan = strchr(author, '<');
1270 if (smallerthan && smallerthan[1] != '\0')
1271 author = smallerthan + 1;
1272 at = strchr(author, '@');
1275 return format_line(wauthor, author_width, author, limit, col_tab_align);
1278 static const struct got_error *
1279 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1280 struct got_object_id *id, const size_t date_display_cols,
1281 int author_display_cols)
1283 struct tog_log_view_state *s = &view->state.log;
1284 const struct got_error *err = NULL;
1285 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1286 char *logmsg0 = NULL, *logmsg = NULL;
1287 char *author = NULL;
1288 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1289 int author_width, logmsg_width;
1290 char *newline, *line = NULL;
1292 const int avail = view->ncols;
1294 time_t committer_time;
1295 struct tog_color *tc;
1297 committer_time = got_object_commit_get_committer_time(commit);
1298 if (localtime_r(&committer_time, &tm) == NULL)
1299 return got_error_from_errno("localtime_r");
1300 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1302 return got_error(GOT_ERR_NO_SPACE);
1304 if (avail <= date_display_cols)
1305 limit = MIN(sizeof(datebuf) - 1, avail);
1307 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1308 tc = get_color(&s->colors, TOG_COLOR_DATE);
1310 wattr_on(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1312 waddnstr(view->window, datebuf, limit);
1314 wattr_off(view->window,
1315 COLOR_PAIR(tc->colorpair), NULL);
1322 err = got_object_id_str(&id_str, id);
1325 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1327 wattr_on(view->window,
1328 COLOR_PAIR(tc->colorpair), NULL);
1329 wprintw(view->window, "%.8s ", id_str);
1331 wattr_off(view->window,
1332 COLOR_PAIR(tc->colorpair), NULL);
1339 author = strdup(got_object_commit_get_author(commit));
1340 if (author == NULL) {
1341 err = got_error_from_errno("strdup");
1344 err = format_author(&wauthor, &author_width, author, avail - col, col);
1347 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1349 wattr_on(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1351 waddwstr(view->window, wauthor);
1353 wattr_off(view->window,
1354 COLOR_PAIR(tc->colorpair), NULL);
1355 col += author_width;
1356 while (col < avail && author_width < author_display_cols + 2) {
1357 waddch(view->window, ' ');
1364 err = got_object_commit_get_logmsg(&logmsg0, commit);
1368 while (*logmsg == '\n')
1370 newline = strchr(logmsg, '\n');
1373 limit = avail - col;
1374 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1377 waddwstr(view->window, wlogmsg);
1378 col += logmsg_width;
1379 while (col < avail) {
1380 waddch(view->window, ' ');
1392 static struct commit_queue_entry *
1393 alloc_commit_queue_entry(struct got_commit_object *commit,
1394 struct got_object_id *id)
1396 struct commit_queue_entry *entry;
1398 entry = calloc(1, sizeof(*entry));
1403 entry->commit = commit;
1408 pop_commit(struct commit_queue *commits)
1410 struct commit_queue_entry *entry;
1412 entry = TAILQ_FIRST(&commits->head);
1413 TAILQ_REMOVE(&commits->head, entry, entry);
1414 got_object_commit_close(entry->commit);
1415 commits->ncommits--;
1416 /* Don't free entry->id! It is owned by the commit graph. */
1421 free_commits(struct commit_queue *commits)
1423 while (!TAILQ_EMPTY(&commits->head))
1424 pop_commit(commits);
1427 static const struct got_error *
1428 match_commit(int *have_match, struct got_object_id *id,
1429 struct got_commit_object *commit, regex_t *regex)
1431 const struct got_error *err = NULL;
1432 regmatch_t regmatch;
1433 char *id_str = NULL, *logmsg = NULL;
1437 err = got_object_id_str(&id_str, id);
1441 err = got_object_commit_get_logmsg(&logmsg, commit);
1445 if (regexec(regex, got_object_commit_get_author(commit), 1,
1446 ®match, 0) == 0 ||
1447 regexec(regex, got_object_commit_get_committer(commit), 1,
1448 ®match, 0) == 0 ||
1449 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1450 regexec(regex, logmsg, 1, ®match, 0) == 0)
1458 static const struct got_error *
1459 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1460 int minqueue, struct got_repository *repo, const char *path,
1461 int *searching, int *search_next_done, regex_t *regex)
1463 const struct got_error *err = NULL;
1467 * We keep all commits open throughout the lifetime of the log
1468 * view in order to avoid having to re-fetch commits from disk
1469 * while updating the display.
1471 while (nqueued < minqueue ||
1472 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1473 struct got_object_id *id;
1474 struct got_commit_object *commit;
1475 struct commit_queue_entry *entry;
1478 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1479 if (err || id == NULL)
1482 err = got_object_open_as_commit(&commit, repo, id);
1485 entry = alloc_commit_queue_entry(commit, id);
1486 if (entry == NULL) {
1487 err = got_error_from_errno("alloc_commit_queue_entry");
1491 errcode = pthread_mutex_lock(&tog_mutex);
1493 err = got_error_set_errno(errcode,
1494 "pthread_mutex_lock");
1498 entry->idx = commits->ncommits;
1499 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1501 commits->ncommits++;
1503 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1505 err = match_commit(&have_match, id, commit, regex);
1509 *search_next_done = TOG_SEARCH_HAVE_MORE;
1512 errcode = pthread_mutex_unlock(&tog_mutex);
1513 if (errcode && err == NULL)
1514 err = got_error_set_errno(errcode,
1515 "pthread_mutex_unlock");
1523 static const struct got_error *
1524 draw_commits(struct tog_view *view)
1526 const struct got_error *err = NULL;
1527 struct tog_log_view_state *s = &view->state.log;
1528 struct commit_queue_entry *entry;
1529 const int limit = view->nlines;
1531 int ncommits, author_cols = 4;
1532 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1533 char *refs_str = NULL;
1535 struct tog_color *tc;
1536 static const size_t date_display_cols = 12;
1538 entry = s->first_displayed_entry;
1541 if (ncommits == s->selected) {
1542 s->selected_entry = entry;
1545 entry = TAILQ_NEXT(entry, entry);
1549 if (s->selected_entry &&
1550 !(view->searching && view->search_next_done == 0)) {
1551 err = got_object_id_str(&id_str, s->selected_entry->id);
1554 err = build_refs_str(&refs_str, &s->refs,
1555 s->selected_entry->id, s->repo);
1560 if (s->thread_args.commits_needed == 0)
1561 halfdelay(10); /* disable fast refresh */
1563 if (s->thread_args.commits_needed > 0) {
1564 if (asprintf(&ncommits_str, " [%d/%d] %s",
1565 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1566 (view->searching && !view->search_next_done) ?
1567 "searching..." : "loading...") == -1) {
1568 err = got_error_from_errno("asprintf");
1572 const char *search_str = NULL;
1574 if (view->searching) {
1575 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1576 search_str = "no more matches";
1577 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1578 search_str = "no matches found";
1579 else if (!view->search_next_done)
1580 search_str = "searching...";
1583 if (asprintf(&ncommits_str, " [%d/%d] %s",
1584 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1585 search_str ? search_str :
1586 (refs_str ? refs_str : "")) == -1) {
1587 err = got_error_from_errno("asprintf");
1592 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1593 if (asprintf(&header, "commit %s %s%s",
1594 id_str ? id_str : "........................................",
1595 s->in_repo_path, ncommits_str) == -1) {
1596 err = got_error_from_errno("asprintf");
1600 } else if (asprintf(&header, "commit %s%s",
1601 id_str ? id_str : "........................................",
1602 ncommits_str) == -1) {
1603 err = got_error_from_errno("asprintf");
1607 err = format_line(&wline, &width, header, view->ncols, 0);
1611 werase(view->window);
1613 if (view_needs_focus_indication(view))
1614 wstandout(view->window);
1615 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1617 wattr_on(view->window,
1618 COLOR_PAIR(tc->colorpair), NULL);
1619 waddwstr(view->window, wline);
1621 wattr_off(view->window,
1622 COLOR_PAIR(tc->colorpair), NULL);
1623 while (width < view->ncols) {
1624 waddch(view->window, ' ');
1627 if (view_needs_focus_indication(view))
1628 wstandend(view->window);
1633 /* Grow author column size if necessary. */
1634 entry = s->first_displayed_entry;
1640 if (ncommits >= limit - 1)
1642 author = strdup(got_object_commit_get_author(entry->commit));
1643 if (author == NULL) {
1644 err = got_error_from_errno("strdup");
1647 err = format_author(&wauthor, &width, author, COLS,
1649 if (author_cols < width)
1650 author_cols = width;
1654 entry = TAILQ_NEXT(entry, entry);
1657 entry = s->first_displayed_entry;
1658 s->last_displayed_entry = s->first_displayed_entry;
1661 if (ncommits >= limit - 1)
1663 if (ncommits == s->selected)
1664 wstandout(view->window);
1665 err = draw_commit(view, entry->commit, entry->id,
1666 date_display_cols, author_cols);
1667 if (ncommits == s->selected)
1668 wstandend(view->window);
1672 s->last_displayed_entry = entry;
1673 entry = TAILQ_NEXT(entry, entry);
1686 log_scroll_up(struct tog_view *view, int maxscroll)
1688 struct tog_log_view_state *s = &view->state.log;
1689 struct commit_queue_entry *entry;
1692 entry = TAILQ_FIRST(&s->commits.head);
1693 if (s->first_displayed_entry == entry)
1696 entry = s->first_displayed_entry;
1697 while (entry && nscrolled < maxscroll) {
1698 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1700 s->first_displayed_entry = entry;
1706 static const struct got_error *
1707 trigger_log_thread(struct tog_view *view, int wait)
1709 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1712 halfdelay(1); /* fast refresh while loading commits */
1714 while (ta->commits_needed > 0) {
1715 if (ta->log_complete)
1718 /* Wake the log thread. */
1719 errcode = pthread_cond_signal(&ta->need_commits);
1721 return got_error_set_errno(errcode,
1722 "pthread_cond_signal");
1725 * The mutex will be released while the view loop waits
1726 * in wgetch(), at which time the log thread will run.
1731 /* Display progress update in log view. */
1732 show_log_view(view);
1736 /* Wait right here while next commit is being loaded. */
1737 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1739 return got_error_set_errno(errcode,
1740 "pthread_cond_wait");
1742 /* Display progress update in log view. */
1743 show_log_view(view);
1751 static const struct got_error *
1752 log_scroll_down(struct tog_view *view, int maxscroll)
1754 struct tog_log_view_state *s = &view->state.log;
1755 const struct got_error *err = NULL;
1756 struct commit_queue_entry *pentry;
1757 int nscrolled = 0, ncommits_needed;
1759 if (s->last_displayed_entry == NULL)
1762 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1763 if (s->commits.ncommits < ncommits_needed &&
1764 !s->thread_args.log_complete) {
1766 * Ask the log thread for required amount of commits.
1768 s->thread_args.commits_needed += maxscroll;
1769 err = trigger_log_thread(view, 1);
1775 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1779 s->last_displayed_entry = pentry;
1781 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1784 s->first_displayed_entry = pentry;
1785 } while (++nscrolled < maxscroll);
1790 static const struct got_error *
1791 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1792 struct got_commit_object *commit, struct got_object_id *commit_id,
1793 struct tog_view *log_view, struct got_repository *repo)
1795 const struct got_error *err;
1796 struct got_object_qid *parent_id;
1797 struct tog_view *diff_view;
1799 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1800 if (diff_view == NULL)
1801 return got_error_from_errno("view_open");
1803 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1804 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1805 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1807 *new_view = diff_view;
1811 static const struct got_error *
1812 tree_view_visit_subtree(struct got_tree_object *subtree,
1813 struct tog_tree_view_state *s)
1815 struct tog_parent_tree *parent;
1817 parent = calloc(1, sizeof(*parent));
1819 return got_error_from_errno("calloc");
1821 parent->tree = s->tree;
1822 parent->first_displayed_entry = s->first_displayed_entry;
1823 parent->selected_entry = s->selected_entry;
1824 parent->selected = s->selected;
1825 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1828 s->first_displayed_entry = NULL;
1832 static const struct got_error *
1833 tree_view_walk_path(struct tog_tree_view_state *s,
1834 struct got_object_id *commit_id, const char *path)
1836 const struct got_error *err = NULL;
1837 struct got_tree_object *tree = NULL;
1839 char *slash, *subpath = NULL;
1841 /* Walk the path and open corresponding tree objects. */
1844 struct got_tree_entry *te;
1845 struct got_object_id *tree_id;
1851 /* Ensure the correct subtree entry is selected. */
1852 slash = strchr(p, '/');
1854 te_name = strdup(p);
1856 te_name = strndup(p, slash - p);
1857 if (te_name == NULL) {
1858 err = got_error_from_errno("strndup");
1861 te = got_object_tree_find_entry(s->tree, te_name);
1863 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1868 s->first_displayed_entry = s->selected_entry = te;
1870 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1871 break; /* jump to this file's entry */
1873 slash = strchr(p, '/');
1875 subpath = strndup(path, slash - path);
1877 subpath = strdup(path);
1878 if (subpath == NULL) {
1879 err = got_error_from_errno("strdup");
1883 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1888 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1893 err = tree_view_visit_subtree(tree, s);
1895 got_object_tree_close(tree);
1909 static const struct got_error *
1910 browse_commit_tree(struct tog_view **new_view, int begin_x,
1911 struct commit_queue_entry *entry, const char *path,
1912 struct got_repository *repo)
1914 const struct got_error *err = NULL;
1915 struct got_tree_object *tree;
1916 struct tog_tree_view_state *s;
1917 struct tog_view *tree_view;
1919 err = got_object_open_as_tree(&tree, repo,
1920 got_object_commit_get_tree_id(entry->commit));
1924 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1925 if (tree_view == NULL)
1926 return got_error_from_errno("view_open");
1928 err = open_tree_view(tree_view, tree, entry->id, repo);
1930 got_object_tree_close(tree);
1933 s = &tree_view->state.tree;
1935 *new_view = tree_view;
1937 if (got_path_is_root_dir(path))
1940 return tree_view_walk_path(s, entry->id, path);
1943 static const struct got_error *
1944 block_signals_used_by_main_thread(void)
1949 if (sigemptyset(&sigset) == -1)
1950 return got_error_from_errno("sigemptyset");
1952 /* tog handles SIGWINCH and SIGCONT */
1953 if (sigaddset(&sigset, SIGWINCH) == -1)
1954 return got_error_from_errno("sigaddset");
1955 if (sigaddset(&sigset, SIGCONT) == -1)
1956 return got_error_from_errno("sigaddset");
1958 /* ncurses handles SIGTSTP */
1959 if (sigaddset(&sigset, SIGTSTP) == -1)
1960 return got_error_from_errno("sigaddset");
1962 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1964 return got_error_set_errno(errcode, "pthread_sigmask");
1970 log_thread(void *arg)
1972 const struct got_error *err = NULL;
1974 struct tog_log_thread_args *a = arg;
1977 err = block_signals_used_by_main_thread();
1981 while (!done && !err && !tog_sigpipe_received) {
1982 err = queue_commits(a->graph, a->commits, 1, a->repo,
1983 a->in_repo_path, a->searching, a->search_next_done,
1986 if (err->code != GOT_ERR_ITER_COMPLETED)
1990 } else if (a->commits_needed > 0)
1991 a->commits_needed--;
1993 errcode = pthread_mutex_lock(&tog_mutex);
1995 err = got_error_set_errno(errcode,
1996 "pthread_mutex_lock");
1998 } else if (*a->quit)
2000 else if (*a->first_displayed_entry == NULL) {
2001 *a->first_displayed_entry =
2002 TAILQ_FIRST(&a->commits->head);
2003 *a->selected_entry = *a->first_displayed_entry;
2006 errcode = pthread_cond_signal(&a->commit_loaded);
2008 err = got_error_set_errno(errcode,
2009 "pthread_cond_signal");
2010 pthread_mutex_unlock(&tog_mutex);
2015 a->commits_needed = 0;
2017 if (a->commits_needed == 0) {
2018 errcode = pthread_cond_wait(&a->need_commits,
2021 err = got_error_set_errno(errcode,
2022 "pthread_cond_wait");
2026 errcode = pthread_mutex_unlock(&tog_mutex);
2027 if (errcode && err == NULL)
2028 err = got_error_set_errno(errcode,
2029 "pthread_mutex_unlock");
2031 a->log_complete = 1;
2035 static const struct got_error *
2036 stop_log_thread(struct tog_log_view_state *s)
2038 const struct got_error *err = NULL;
2043 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2045 return got_error_set_errno(errcode,
2046 "pthread_cond_signal");
2047 errcode = pthread_mutex_unlock(&tog_mutex);
2049 return got_error_set_errno(errcode,
2050 "pthread_mutex_unlock");
2051 errcode = pthread_join(s->thread, (void **)&err);
2053 return got_error_set_errno(errcode, "pthread_join");
2054 errcode = pthread_mutex_lock(&tog_mutex);
2056 return got_error_set_errno(errcode,
2057 "pthread_mutex_lock");
2061 if (s->thread_args.repo) {
2062 got_repo_close(s->thread_args.repo);
2063 s->thread_args.repo = NULL;
2066 if (s->thread_args.graph) {
2067 got_commit_graph_close(s->thread_args.graph);
2068 s->thread_args.graph = NULL;
2074 static const struct got_error *
2075 close_log_view(struct tog_view *view)
2077 const struct got_error *err = NULL;
2078 struct tog_log_view_state *s = &view->state.log;
2081 err = stop_log_thread(s);
2083 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2084 if (errcode && err == NULL)
2085 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2087 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2088 if (errcode && err == NULL)
2089 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2091 free_commits(&s->commits);
2092 free(s->in_repo_path);
2093 s->in_repo_path = NULL;
2096 got_ref_list_free(&s->refs);
2100 static const struct got_error *
2101 search_start_log_view(struct tog_view *view)
2103 struct tog_log_view_state *s = &view->state.log;
2105 s->matched_entry = NULL;
2106 s->search_entry = NULL;
2110 static const struct got_error *
2111 search_next_log_view(struct tog_view *view)
2113 const struct got_error *err = NULL;
2114 struct tog_log_view_state *s = &view->state.log;
2115 struct commit_queue_entry *entry;
2117 /* Display progress update in log view. */
2118 show_log_view(view);
2122 if (s->search_entry) {
2124 errcode = pthread_mutex_unlock(&tog_mutex);
2126 return got_error_set_errno(errcode,
2127 "pthread_mutex_unlock");
2128 ch = wgetch(view->window);
2129 errcode = pthread_mutex_lock(&tog_mutex);
2131 return got_error_set_errno(errcode,
2132 "pthread_mutex_lock");
2133 if (ch == KEY_BACKSPACE) {
2134 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2137 if (view->searching == TOG_SEARCH_FORWARD)
2138 entry = TAILQ_NEXT(s->search_entry, entry);
2140 entry = TAILQ_PREV(s->search_entry,
2141 commit_queue_head, entry);
2142 } else if (s->matched_entry) {
2143 if (view->searching == TOG_SEARCH_FORWARD)
2144 entry = TAILQ_NEXT(s->matched_entry, entry);
2146 entry = TAILQ_PREV(s->matched_entry,
2147 commit_queue_head, entry);
2149 if (view->searching == TOG_SEARCH_FORWARD)
2150 entry = TAILQ_FIRST(&s->commits.head);
2152 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2158 if (entry == NULL) {
2159 if (s->thread_args.log_complete ||
2160 view->searching == TOG_SEARCH_BACKWARD) {
2161 view->search_next_done =
2162 (s->matched_entry == NULL ?
2163 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2164 s->search_entry = NULL;
2168 * Poke the log thread for more commits and return,
2169 * allowing the main loop to make progress. Search
2170 * will resume at s->search_entry once we come back.
2172 s->thread_args.commits_needed++;
2173 return trigger_log_thread(view, 0);
2176 err = match_commit(&have_match, entry->id, entry->commit,
2181 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2182 s->matched_entry = entry;
2186 s->search_entry = entry;
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(entry, entry);
2190 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2193 if (s->matched_entry) {
2194 int cur = s->selected_entry->idx;
2195 while (cur < s->matched_entry->idx) {
2196 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2201 while (cur > s->matched_entry->idx) {
2202 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2209 s->search_entry = NULL;
2214 static const struct got_error *
2215 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2216 struct got_repository *repo, const char *head_ref_name,
2217 const char *in_repo_path, int log_branches)
2219 const struct got_error *err = NULL;
2220 struct tog_log_view_state *s = &view->state.log;
2221 struct got_repository *thread_repo = NULL;
2222 struct got_commit_graph *thread_graph = NULL;
2225 SIMPLEQ_INIT(&s->refs);
2227 if (in_repo_path != s->in_repo_path) {
2228 free(s->in_repo_path);
2229 s->in_repo_path = strdup(in_repo_path);
2230 if (s->in_repo_path == NULL)
2231 return got_error_from_errno("strdup");
2234 /* The commit queue only contains commits being displayed. */
2235 TAILQ_INIT(&s->commits.head);
2236 s->commits.ncommits = 0;
2238 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2243 s->head_ref_name = head_ref_name;
2244 s->start_id = got_object_id_dup(start_id);
2245 if (s->start_id == NULL) {
2246 err = got_error_from_errno("got_object_id_dup");
2249 s->log_branches = log_branches;
2251 SIMPLEQ_INIT(&s->colors);
2252 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2253 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2254 get_color_value("TOG_COLOR_COMMIT"));
2257 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2258 get_color_value("TOG_COLOR_AUTHOR"));
2260 free_colors(&s->colors);
2263 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2264 get_color_value("TOG_COLOR_DATE"));
2266 free_colors(&s->colors);
2271 view->show = show_log_view;
2272 view->input = input_log_view;
2273 view->close = close_log_view;
2274 view->search_start = search_start_log_view;
2275 view->search_next = search_next_log_view;
2277 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2280 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2284 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2285 s->repo, NULL, NULL);
2289 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2291 err = got_error_set_errno(errcode, "pthread_cond_init");
2294 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2296 err = got_error_set_errno(errcode, "pthread_cond_init");
2300 s->thread_args.commits_needed = view->nlines;
2301 s->thread_args.graph = thread_graph;
2302 s->thread_args.commits = &s->commits;
2303 s->thread_args.in_repo_path = s->in_repo_path;
2304 s->thread_args.start_id = s->start_id;
2305 s->thread_args.repo = thread_repo;
2306 s->thread_args.log_complete = 0;
2307 s->thread_args.quit = &s->quit;
2308 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2309 s->thread_args.selected_entry = &s->selected_entry;
2310 s->thread_args.searching = &view->searching;
2311 s->thread_args.search_next_done = &view->search_next_done;
2312 s->thread_args.regex = &view->regex;
2315 close_log_view(view);
2319 static const struct got_error *
2320 show_log_view(struct tog_view *view)
2322 const struct got_error *err;
2323 struct tog_log_view_state *s = &view->state.log;
2325 if (s->thread == NULL) {
2326 int errcode = pthread_create(&s->thread, NULL, log_thread,
2329 return got_error_set_errno(errcode, "pthread_create");
2330 if (s->thread_args.commits_needed > 0) {
2331 err = trigger_log_thread(view, 1);
2337 return draw_commits(view);
2340 static const struct got_error *
2341 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2342 struct tog_view **focus_view, struct tog_view *view, int ch)
2344 const struct got_error *err = NULL;
2345 struct tog_log_view_state *s = &view->state.log;
2346 char *parent_path, *in_repo_path = NULL;
2347 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2348 struct tog_view *ref_view = NULL;
2350 struct got_object_id *start_id;
2360 if (s->first_displayed_entry == NULL)
2362 if (s->selected > 0)
2365 log_scroll_up(view, 1);
2369 if (s->first_displayed_entry == NULL)
2371 if (TAILQ_FIRST(&s->commits.head) ==
2372 s->first_displayed_entry) {
2376 log_scroll_up(view, view->nlines - 1);
2382 if (s->first_displayed_entry == NULL)
2384 if (s->selected < MIN(view->nlines - 2,
2385 s->commits.ncommits - 1)) {
2389 err = log_scroll_down(view, 1);
2393 struct commit_queue_entry *first;
2394 first = s->first_displayed_entry;
2397 err = log_scroll_down(view, view->nlines - 1);
2400 if (first == s->first_displayed_entry &&
2401 s->selected < MIN(view->nlines - 2,
2402 s->commits.ncommits - 1)) {
2403 /* can't scroll further down */
2404 s->selected = MIN(view->nlines - 2,
2405 s->commits.ncommits - 1);
2411 if (s->selected > view->nlines - 2)
2412 s->selected = view->nlines - 2;
2413 if (s->selected > s->commits.ncommits - 1)
2414 s->selected = s->commits.ncommits - 1;
2419 if (s->selected_entry == NULL)
2421 if (view_is_parent_view(view))
2422 begin_x = view_split_begin_x(view->begin_x);
2423 err = open_diff_view_for_commit(&diff_view, begin_x,
2424 s->selected_entry->commit, s->selected_entry->id,
2428 if (view_is_parent_view(view)) {
2429 err = view_close_child(view);
2432 err = view_set_child(view, diff_view);
2434 view_close(diff_view);
2437 *focus_view = diff_view;
2438 view->child_focussed = 1;
2440 *new_view = diff_view;
2443 if (s->selected_entry == NULL)
2445 if (view_is_parent_view(view))
2446 begin_x = view_split_begin_x(view->begin_x);
2447 err = browse_commit_tree(&tree_view, begin_x,
2448 s->selected_entry, s->in_repo_path, s->repo);
2451 if (view_is_parent_view(view)) {
2452 err = view_close_child(view);
2455 err = view_set_child(view, tree_view);
2457 view_close(tree_view);
2460 *focus_view = tree_view;
2461 view->child_focussed = 1;
2463 *new_view = tree_view;
2466 if (got_path_cmp(s->in_repo_path, "/",
2467 strlen(s->in_repo_path), 1) == 0)
2469 err = got_path_dirname(&parent_path, s->in_repo_path);
2472 err = stop_log_thread(s);
2477 lv = view_open(view->nlines, view->ncols,
2478 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2481 return got_error_from_errno("view_open");
2483 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2484 parent_path, s->log_branches);
2488 if (view_is_parent_view(view))
2491 view_set_child(view->parent, lv);
2496 err = stop_log_thread(s);
2499 lv = view_open(view->nlines, view->ncols,
2500 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2502 return got_error_from_errno("view_open");
2503 err = got_repo_match_object_id(&start_id, NULL,
2504 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2505 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2510 in_repo_path = strdup(s->in_repo_path);
2511 if (in_repo_path == NULL) {
2514 return got_error_from_errno("strdup");
2516 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2517 in_repo_path, s->log_branches);
2527 s->log_branches = !s->log_branches;
2528 err = stop_log_thread(s);
2531 lv = view_open(view->nlines, view->ncols,
2532 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2534 return got_error_from_errno("view_open");
2535 err = open_log_view(lv, s->start_id, s->repo,
2536 s->head_ref_name, s->in_repo_path, s->log_branches);
2545 if (view_is_parent_view(view))
2546 begin_x = view_split_begin_x(view->begin_x);
2547 ref_view = view_open(view->nlines, view->ncols,
2548 view->begin_y, begin_x, TOG_VIEW_REF);
2549 if (ref_view == NULL)
2550 return got_error_from_errno("view_open");
2551 err = open_ref_view(ref_view, s->repo);
2553 view_close(ref_view);
2556 if (view_is_parent_view(view)) {
2557 err = view_close_child(view);
2560 err = view_set_child(view, ref_view);
2562 view_close(ref_view);
2565 *focus_view = ref_view;
2566 view->child_focussed = 1;
2568 *new_view = ref_view;
2577 static const struct got_error *
2578 apply_unveil(const char *repo_path, const char *worktree_path)
2580 const struct got_error *error;
2583 if (unveil("gmon.out", "rwc") != 0)
2584 return got_error_from_errno2("unveil", "gmon.out");
2586 if (repo_path && unveil(repo_path, "r") != 0)
2587 return got_error_from_errno2("unveil", repo_path);
2589 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2590 return got_error_from_errno2("unveil", worktree_path);
2592 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2593 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2595 error = got_privsep_unveil_exec_helpers();
2599 if (unveil(NULL, NULL) != 0)
2600 return got_error_from_errno("unveil");
2610 halfdelay(1); /* Do fast refresh while initial view is loading. */
2613 intrflush(stdscr, FALSE);
2614 keypad(stdscr, TRUE);
2616 if (getenv("TOG_COLORS") != NULL) {
2618 use_default_colors();
2620 signal(SIGWINCH, tog_sigwinch);
2621 signal(SIGPIPE, tog_sigpipe);
2622 signal(SIGCONT, tog_sigcont);
2625 static const struct got_error *
2626 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2627 struct got_repository *repo, struct got_worktree *worktree)
2629 const struct got_error *err = NULL;
2632 *in_repo_path = strdup("/");
2633 if (*in_repo_path == NULL)
2634 return got_error_from_errno("strdup");
2639 const char *prefix = got_worktree_get_path_prefix(worktree);
2642 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2645 if (asprintf(in_repo_path, "%s%s%s", prefix,
2646 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2648 err = got_error_from_errno("asprintf");
2649 *in_repo_path = NULL;
2653 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2658 static const struct got_error *
2659 cmd_log(int argc, char *argv[])
2661 const struct got_error *error;
2662 struct got_repository *repo = NULL;
2663 struct got_worktree *worktree = NULL;
2664 struct got_object_id *start_id = NULL;
2665 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2666 char *start_commit = NULL, *head_ref_name = NULL;
2667 int ch, log_branches = 0;
2668 struct tog_view *view;
2671 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2676 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2682 start_commit = optarg;
2685 repo_path = realpath(optarg, NULL);
2686 if (repo_path == NULL)
2687 return got_error_from_errno2("realpath",
2702 cwd = getcwd(NULL, 0);
2704 return got_error_from_errno("getcwd");
2706 error = got_worktree_open(&worktree, cwd);
2707 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2710 if (repo_path == NULL) {
2713 strdup(got_worktree_get_repo_path(worktree));
2715 repo_path = strdup(cwd);
2717 if (repo_path == NULL) {
2718 error = got_error_from_errno("strdup");
2722 error = got_repo_open(&repo, repo_path, NULL);
2726 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2733 error = apply_unveil(got_repo_get_path(repo),
2734 worktree ? got_worktree_get_root_path(worktree) : NULL);
2738 if (start_commit == NULL)
2739 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2740 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2741 GOT_OBJ_TYPE_COMMIT, 1, repo);
2743 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2744 GOT_OBJ_TYPE_COMMIT, 1, repo);
2748 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2750 error = got_error_from_errno("view_open");
2754 head_ref_name = strdup(
2755 got_worktree_get_head_ref_name(worktree));
2756 if (head_ref_name == NULL) {
2757 error = got_error_from_errno("strdup");
2761 error = open_log_view(view, start_id, repo, head_ref_name,
2762 in_repo_path, log_branches);
2766 /* Release work tree lock. */
2767 got_worktree_close(worktree);
2770 error = view_loop(view);
2776 free(head_ref_name);
2778 got_repo_close(repo);
2780 got_worktree_close(worktree);
2788 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2789 "[-w] object1 object2\n", getprogname());
2794 parse_next_line(FILE *f, size_t *len)
2799 const char delim[3] = { '\0', '\0', '\0'};
2801 line = fparseln(f, &linelen, &lineno, delim, 0);
2808 match_line(const char *line, regex_t *regex, size_t nmatch,
2809 regmatch_t *regmatch)
2811 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2815 match_color(struct tog_colors *colors, const char *line)
2817 struct tog_color *tc = NULL;
2819 SIMPLEQ_FOREACH(tc, colors, entry) {
2820 if (match_line(line, &tc->regex, 0, NULL))
2827 static const struct got_error *
2828 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2829 WINDOW *window, regmatch_t *regmatch)
2831 const struct got_error *err = NULL;
2838 s = strndup(line, regmatch->rm_so);
2840 return got_error_from_errno("strndup");
2842 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2847 waddwstr(window, wline);
2854 s = strndup(line + regmatch->rm_so,
2855 regmatch->rm_eo - regmatch->rm_so);
2857 err = got_error_from_errno("strndup");
2861 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2866 wattr_on(window, A_STANDOUT, NULL);
2867 waddwstr(window, wline);
2868 wattr_off(window, A_STANDOUT, NULL);
2875 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2876 err = format_line(&wline, &width,
2877 line + regmatch->rm_eo, wlimit, col_tab_align);
2880 waddwstr(window, wline);
2888 static const struct got_error *
2889 draw_file(struct tog_view *view, const char *header)
2891 struct tog_diff_view_state *s = &view->state.diff;
2892 regmatch_t *regmatch = &view->regmatch;
2893 const struct got_error *err;
2896 struct tog_color *tc;
2900 int max_lines = view->nlines;
2901 int nlines = s->nlines;
2904 line_offset = s->line_offsets[s->first_displayed_line - 1];
2905 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2906 return got_error_from_errno("fseek");
2908 werase(view->window);
2911 if (asprintf(&line, "[%d/%d] %s",
2912 s->first_displayed_line - 1 + s->selected_line, nlines,
2914 return got_error_from_errno("asprintf");
2915 err = format_line(&wline, &width, line, view->ncols, 0);
2920 if (view_needs_focus_indication(view))
2921 wstandout(view->window);
2922 waddwstr(view->window, wline);
2925 if (view_needs_focus_indication(view))
2926 wstandend(view->window);
2927 if (width <= view->ncols - 1)
2928 waddch(view->window, '\n');
2936 while (max_lines > 0 && nprinted < max_lines) {
2937 line = parse_next_line(s->f, &len);
2943 tc = match_color(&s->colors, line);
2945 wattr_on(view->window,
2946 COLOR_PAIR(tc->colorpair), NULL);
2947 if (s->first_displayed_line + nprinted == s->matched_line &&
2948 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2949 err = add_matched_line(&width, line, view->ncols, 0,
2950 view->window, regmatch);
2956 err = format_line(&wline, &width, line, view->ncols, 0);
2961 waddwstr(view->window, wline);
2966 wattr_off(view->window,
2967 COLOR_PAIR(tc->colorpair), NULL);
2968 if (width <= view->ncols - 1)
2969 waddch(view->window, '\n');
2974 s->last_displayed_line = s->first_displayed_line +
2977 s->last_displayed_line = s->first_displayed_line;
2982 while (nprinted < view->nlines) {
2983 waddch(view->window, '\n');
2987 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2992 wstandout(view->window);
2993 waddwstr(view->window, wline);
2996 wstandend(view->window);
3003 get_datestr(time_t *time, char *datebuf)
3005 struct tm mytm, *tm;
3008 tm = gmtime_r(time, &mytm);
3011 s = asctime_r(tm, datebuf);
3014 p = strchr(s, '\n');
3020 static const struct got_error *
3021 get_changed_paths(struct got_pathlist_head *paths,
3022 struct got_commit_object *commit, struct got_repository *repo)
3024 const struct got_error *err = NULL;
3025 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3026 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3027 struct got_object_qid *qid;
3029 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3031 struct got_commit_object *pcommit;
3032 err = got_object_open_as_commit(&pcommit, repo,
3037 tree_id1 = got_object_commit_get_tree_id(pcommit);
3038 got_object_commit_close(pcommit);
3043 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3048 tree_id2 = got_object_commit_get_tree_id(commit);
3049 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3053 err = got_diff_tree(tree1, tree2, "", "", repo,
3054 got_diff_tree_collect_changed_paths, paths, 0);
3057 got_object_tree_close(tree1);
3059 got_object_tree_close(tree2);
3063 static const struct got_error *
3064 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3068 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3070 return got_error_from_errno("reallocarray");
3072 (*line_offsets)[*nlines] = off;
3077 static const struct got_error *
3078 write_commit_info(off_t **line_offsets, size_t *nlines,
3079 struct got_object_id *commit_id, struct got_reflist_head *refs,
3080 struct got_repository *repo, FILE *outfile)
3082 const struct got_error *err = NULL;
3083 char datebuf[26], *datestr;
3084 struct got_commit_object *commit;
3085 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3086 time_t committer_time;
3087 const char *author, *committer;
3088 char *refs_str = NULL;
3089 struct got_pathlist_head changed_paths;
3090 struct got_pathlist_entry *pe;
3094 TAILQ_INIT(&changed_paths);
3097 err = build_refs_str(&refs_str, refs, commit_id, repo);
3102 err = got_object_open_as_commit(&commit, repo, commit_id);
3106 err = got_object_id_str(&id_str, commit_id);
3108 err = got_error_from_errno("got_object_id_str");
3112 err = add_line_offset(line_offsets, nlines, 0);
3116 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3117 refs_str ? refs_str : "", refs_str ? ")" : "");
3119 err = got_error_from_errno("fprintf");
3123 err = add_line_offset(line_offsets, nlines, outoff);
3127 n = fprintf(outfile, "from: %s\n",
3128 got_object_commit_get_author(commit));
3130 err = got_error_from_errno("fprintf");
3134 err = add_line_offset(line_offsets, nlines, outoff);
3138 committer_time = got_object_commit_get_committer_time(commit);
3139 datestr = get_datestr(&committer_time, datebuf);
3141 n = fprintf(outfile, "date: %s UTC\n", datestr);
3143 err = got_error_from_errno("fprintf");
3147 err = add_line_offset(line_offsets, nlines, outoff);
3151 author = got_object_commit_get_author(commit);
3152 committer = got_object_commit_get_committer(commit);
3153 if (strcmp(author, committer) != 0) {
3154 n = fprintf(outfile, "via: %s\n", committer);
3156 err = got_error_from_errno("fprintf");
3160 err = add_line_offset(line_offsets, nlines, outoff);
3164 err = got_object_commit_get_logmsg(&logmsg, commit);
3168 while ((line = strsep(&s, "\n")) != NULL) {
3169 n = fprintf(outfile, "%s\n", line);
3171 err = got_error_from_errno("fprintf");
3175 err = add_line_offset(line_offsets, nlines, outoff);
3180 err = get_changed_paths(&changed_paths, commit, repo);
3183 TAILQ_FOREACH(pe, &changed_paths, entry) {
3184 struct got_diff_changed_path *cp = pe->data;
3185 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3187 err = got_error_from_errno("fprintf");
3191 err = add_line_offset(line_offsets, nlines, outoff);
3194 free((char *)pe->path);
3198 fputc('\n', outfile);
3200 err = add_line_offset(line_offsets, nlines, outoff);
3202 got_pathlist_free(&changed_paths);
3206 got_object_commit_close(commit);
3208 free(*line_offsets);
3209 *line_offsets = NULL;
3215 static const struct got_error *
3216 create_diff(struct tog_diff_view_state *s)
3218 const struct got_error *err = NULL;
3222 free(s->line_offsets);
3223 s->line_offsets = malloc(sizeof(off_t));
3224 if (s->line_offsets == NULL)
3225 return got_error_from_errno("malloc");
3230 err = got_error_from_errno("got_opentemp");
3233 if (s->f && fclose(s->f) != 0) {
3234 err = got_error_from_errno("fclose");
3240 err = got_object_get_type(&obj_type, s->repo, s->id1);
3242 err = got_object_get_type(&obj_type, s->repo, s->id2);
3247 case GOT_OBJ_TYPE_BLOB:
3248 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3249 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3250 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3252 case GOT_OBJ_TYPE_TREE:
3253 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3254 s->id1, s->id2, "", "", s->diff_context,
3255 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3257 case GOT_OBJ_TYPE_COMMIT: {
3258 const struct got_object_id_queue *parent_ids;
3259 struct got_object_qid *pid;
3260 struct got_commit_object *commit2;
3262 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3265 /* Show commit info if we're diffing to a parent/root commit. */
3266 if (s->id1 == NULL) {
3267 err = write_commit_info(&s->line_offsets, &s->nlines,
3268 s->id2, &s->refs, s->repo, s->f);
3272 parent_ids = got_object_commit_get_parent_ids(commit2);
3273 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3274 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3275 err = write_commit_info(
3276 &s->line_offsets, &s->nlines,
3277 s->id2, &s->refs, s->repo, s->f);
3284 got_object_commit_close(commit2);
3286 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3287 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3288 s->force_text_diff, s->repo, s->f);
3292 err = got_error(GOT_ERR_OBJ_TYPE);
3298 if (s->f && fflush(s->f) != 0 && err == NULL)
3299 err = got_error_from_errno("fflush");
3304 diff_view_indicate_progress(struct tog_view *view)
3306 mvwaddstr(view->window, 0, 0, "diffing...");
3311 static const struct got_error *
3312 search_start_diff_view(struct tog_view *view)
3314 struct tog_diff_view_state *s = &view->state.diff;
3316 s->matched_line = 0;
3320 static const struct got_error *
3321 search_next_diff_view(struct tog_view *view)
3323 struct tog_diff_view_state *s = &view->state.diff;
3326 if (!view->searching) {
3327 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3331 if (s->matched_line) {
3332 if (view->searching == TOG_SEARCH_FORWARD)
3333 lineno = s->matched_line + 1;
3335 lineno = s->matched_line - 1;
3337 if (view->searching == TOG_SEARCH_FORWARD)
3348 if (lineno <= 0 || lineno > s->nlines) {
3349 if (s->matched_line == 0) {
3350 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3355 if (view->searching == TOG_SEARCH_FORWARD)
3361 offset = s->line_offsets[lineno - 1];
3362 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3364 return got_error_from_errno("fseeko");
3367 line = parse_next_line(s->f, &len);
3369 match_line(line, &view->regex, 1, &view->regmatch)) {
3370 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3371 s->matched_line = lineno;
3376 if (view->searching == TOG_SEARCH_FORWARD)
3382 if (s->matched_line) {
3383 s->first_displayed_line = s->matched_line;
3384 s->selected_line = 1;
3390 static const struct got_error *
3391 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3392 struct got_object_id *id2, const char *label1, const char *label2,
3393 int diff_context, int ignore_whitespace, int force_text_diff,
3394 struct tog_view *log_view, struct got_repository *repo)
3396 const struct got_error *err;
3397 struct tog_diff_view_state *s = &view->state.diff;
3399 SIMPLEQ_INIT(&s->refs);
3401 if (id1 != NULL && id2 != NULL) {
3403 err = got_object_get_type(&type1, repo, id1);
3406 err = got_object_get_type(&type2, repo, id2);
3411 return got_error(GOT_ERR_OBJ_TYPE);
3413 s->first_displayed_line = 1;
3414 s->last_displayed_line = view->nlines;
3415 s->selected_line = 1;
3423 s->id1 = got_object_id_dup(id1);
3425 return got_error_from_errno("got_object_id_dup");
3429 s->id2 = got_object_id_dup(id2);
3430 if (s->id2 == NULL) {
3433 return got_error_from_errno("got_object_id_dup");
3436 s->first_displayed_line = 1;
3437 s->last_displayed_line = view->nlines;
3438 s->diff_context = diff_context;
3439 s->ignore_whitespace = ignore_whitespace;
3440 s->force_text_diff = force_text_diff;
3441 s->log_view = log_view;
3444 SIMPLEQ_INIT(&s->colors);
3445 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3446 err = add_color(&s->colors,
3447 "^-", TOG_COLOR_DIFF_MINUS,
3448 get_color_value("TOG_COLOR_DIFF_MINUS"));
3451 err = add_color(&s->colors, "^\\+",
3452 TOG_COLOR_DIFF_PLUS,
3453 get_color_value("TOG_COLOR_DIFF_PLUS"));
3455 free_colors(&s->colors);
3458 err = add_color(&s->colors,
3459 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3460 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3462 free_colors(&s->colors);
3466 err = add_color(&s->colors,
3467 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3468 TOG_COLOR_DIFF_META,
3469 get_color_value("TOG_COLOR_DIFF_META"));
3471 free_colors(&s->colors);
3475 err = add_color(&s->colors,
3476 "^(from|via): ", TOG_COLOR_AUTHOR,
3477 get_color_value("TOG_COLOR_AUTHOR"));
3479 free_colors(&s->colors);
3483 err = add_color(&s->colors,
3484 "^date: ", TOG_COLOR_DATE,
3485 get_color_value("TOG_COLOR_DATE"));
3487 free_colors(&s->colors);
3492 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3498 free_colors(&s->colors);
3502 if (log_view && view_is_splitscreen(view))
3503 show_log_view(log_view); /* draw vborder */
3504 diff_view_indicate_progress(view);
3506 s->line_offsets = NULL;
3508 err = create_diff(s);
3514 free_colors(&s->colors);
3515 got_ref_list_free(&s->refs);
3519 view->show = show_diff_view;
3520 view->input = input_diff_view;
3521 view->close = close_diff_view;
3522 view->search_start = search_start_diff_view;
3523 view->search_next = search_next_diff_view;
3528 static const struct got_error *
3529 close_diff_view(struct tog_view *view)
3531 const struct got_error *err = NULL;
3532 struct tog_diff_view_state *s = &view->state.diff;
3538 if (s->f && fclose(s->f) == EOF)
3539 err = got_error_from_errno("fclose");
3540 free_colors(&s->colors);
3541 free(s->line_offsets);
3542 s->line_offsets = NULL;
3544 got_ref_list_free(&s->refs);
3548 static const struct got_error *
3549 show_diff_view(struct tog_view *view)
3551 const struct got_error *err;
3552 struct tog_diff_view_state *s = &view->state.diff;
3553 char *id_str1 = NULL, *id_str2, *header;
3554 const char *label1, *label2;
3557 err = got_object_id_str(&id_str1, s->id1);
3560 label1 = s->label1 ? : id_str1;
3562 label1 = "/dev/null";
3564 err = got_object_id_str(&id_str2, s->id2);
3567 label2 = s->label2 ? : id_str2;
3569 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3570 err = got_error_from_errno("asprintf");
3578 return draw_file(view, header);
3581 static const struct got_error *
3582 set_selected_commit(struct tog_diff_view_state *s,
3583 struct commit_queue_entry *entry)
3585 const struct got_error *err;
3586 const struct got_object_id_queue *parent_ids;
3587 struct got_commit_object *selected_commit;
3588 struct got_object_qid *pid;
3591 s->id2 = got_object_id_dup(entry->id);
3593 return got_error_from_errno("got_object_id_dup");
3595 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3598 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3600 pid = SIMPLEQ_FIRST(parent_ids);
3601 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3602 got_object_commit_close(selected_commit);
3606 static const struct got_error *
3607 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3608 struct tog_view **focus_view, struct tog_view *view, int ch)
3610 const struct got_error *err = NULL;
3611 struct tog_diff_view_state *s = &view->state.diff;
3612 struct tog_log_view_state *ls;
3613 struct commit_queue_entry *entry;
3620 s->force_text_diff = !s->force_text_diff;
3622 s->ignore_whitespace = !s->ignore_whitespace;
3623 wclear(view->window);
3624 s->first_displayed_line = 1;
3625 s->last_displayed_line = view->nlines;
3626 diff_view_indicate_progress(view);
3627 err = create_diff(s);
3631 if (s->first_displayed_line > 1)
3632 s->first_displayed_line--;
3636 if (s->first_displayed_line == 1)
3639 while (i++ < view->nlines - 1 &&
3640 s->first_displayed_line > 1)
3641 s->first_displayed_line--;
3646 s->first_displayed_line++;
3654 while (!s->eof && i++ < view->nlines - 1) {
3656 line = parse_next_line(s->f, NULL);
3657 s->first_displayed_line++;
3663 if (s->diff_context > 0) {
3665 diff_view_indicate_progress(view);
3666 err = create_diff(s);
3667 if (s->first_displayed_line + view->nlines - 1 >
3669 s->first_displayed_line = 1;
3670 s->last_displayed_line = view->nlines;
3675 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3677 diff_view_indicate_progress(view);
3678 err = create_diff(s);
3683 if (s->log_view == NULL)
3685 ls = &s->log_view->state.log;
3686 entry = TAILQ_PREV(ls->selected_entry,
3687 commit_queue_head, entry);
3691 err = input_log_view(NULL, NULL, NULL, s->log_view,
3696 err = set_selected_commit(s, entry);
3700 s->first_displayed_line = 1;
3701 s->last_displayed_line = view->nlines;
3703 diff_view_indicate_progress(view);
3704 err = create_diff(s);
3708 if (s->log_view == NULL)
3710 ls = &s->log_view->state.log;
3712 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3713 ls->thread_args.commits_needed++;
3714 err = trigger_log_thread(s->log_view, 1);
3718 err = input_log_view(NULL, NULL, NULL, s->log_view,
3723 entry = TAILQ_NEXT(ls->selected_entry, entry);
3727 err = set_selected_commit(s, entry);
3731 s->first_displayed_line = 1;
3732 s->last_displayed_line = view->nlines;
3734 diff_view_indicate_progress(view);
3735 err = create_diff(s);
3744 static const struct got_error *
3745 cmd_diff(int argc, char *argv[])
3747 const struct got_error *error = NULL;
3748 struct got_repository *repo = NULL;
3749 struct got_worktree *worktree = NULL;
3750 struct got_object_id *id1 = NULL, *id2 = NULL;
3751 char *repo_path = NULL, *cwd = NULL;
3752 char *id_str1 = NULL, *id_str2 = NULL;
3753 char *label1 = NULL, *label2 = NULL;
3754 int diff_context = 3, ignore_whitespace = 0;
3755 int ch, force_text_diff = 0;
3757 struct tog_view *view;
3760 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3764 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3767 force_text_diff = 1;
3770 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3773 err(1, "-C option %s", errstr);
3776 repo_path = realpath(optarg, NULL);
3777 if (repo_path == NULL)
3778 return got_error_from_errno2("realpath",
3780 got_path_strip_trailing_slashes(repo_path);
3783 ignore_whitespace = 1;
3795 usage_diff(); /* TODO show local worktree changes */
3796 } else if (argc == 2) {
3802 cwd = getcwd(NULL, 0);
3804 return got_error_from_errno("getcwd");
3806 error = got_worktree_open(&worktree, cwd);
3807 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3810 if (repo_path == NULL) {
3813 strdup(got_worktree_get_repo_path(worktree));
3815 repo_path = strdup(cwd);
3817 if (repo_path == NULL) {
3818 error = got_error_from_errno("strdup");
3822 error = got_repo_open(&repo, repo_path, NULL);
3828 error = apply_unveil(got_repo_get_path(repo), NULL);
3832 error = got_repo_match_object_id(&id1, &label1, id_str1,
3833 GOT_OBJ_TYPE_ANY, 1, repo);
3837 error = got_repo_match_object_id(&id2, &label2, id_str2,
3838 GOT_OBJ_TYPE_ANY, 1, repo);
3842 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3844 error = got_error_from_errno("view_open");
3847 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3848 ignore_whitespace, force_text_diff, NULL, repo);
3851 error = view_loop(view);
3858 got_repo_close(repo);
3860 got_worktree_close(worktree);
3868 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3873 struct tog_blame_line {
3875 struct got_object_id *id;
3878 static const struct got_error *
3879 draw_blame(struct tog_view *view)
3881 struct tog_blame_view_state *s = &view->state.blame;
3882 struct tog_blame *blame = &s->blame;
3883 regmatch_t *regmatch = &view->regmatch;
3884 const struct got_error *err;
3885 int lineno = 0, nprinted = 0;
3890 struct tog_blame_line *blame_line;
3891 struct got_object_id *prev_id = NULL;
3893 struct tog_color *tc;
3895 err = got_object_id_str(&id_str, s->blamed_commit->id);
3900 werase(view->window);
3902 if (asprintf(&line, "commit %s", id_str) == -1) {
3903 err = got_error_from_errno("asprintf");
3908 err = format_line(&wline, &width, line, view->ncols, 0);
3913 if (view_needs_focus_indication(view))
3914 wstandout(view->window);
3915 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3917 wattr_on(view->window,
3918 COLOR_PAIR(tc->colorpair), NULL);
3919 waddwstr(view->window, wline);
3921 wattr_off(view->window,
3922 COLOR_PAIR(tc->colorpair), NULL);
3923 if (view_needs_focus_indication(view))
3924 wstandend(view->window);
3927 if (width < view->ncols - 1)
3928 waddch(view->window, '\n');
3930 if (asprintf(&line, "[%d/%d] %s%s",
3931 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3932 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3934 return got_error_from_errno("asprintf");
3937 err = format_line(&wline, &width, line, view->ncols, 0);
3942 waddwstr(view->window, wline);
3945 if (width < view->ncols - 1)
3946 waddch(view->window, '\n');
3949 while (nprinted < view->nlines - 2) {
3950 line = parse_next_line(blame->f, &len);
3955 if (++lineno < s->first_displayed_line) {
3960 if (view->focussed && nprinted == s->selected_line - 1)
3961 wstandout(view->window);
3963 if (blame->nlines > 0) {
3964 blame_line = &blame->lines[lineno - 1];
3965 if (blame_line->annotated && prev_id &&
3966 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3968 nprinted == s->selected_line - 1)) {
3969 waddstr(view->window, " ");
3970 } else if (blame_line->annotated) {
3972 err = got_object_id_str(&id_str, blame_line->id);
3977 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3979 wattr_on(view->window,
3980 COLOR_PAIR(tc->colorpair), NULL);
3981 wprintw(view->window, "%.8s", id_str);
3983 wattr_off(view->window,
3984 COLOR_PAIR(tc->colorpair), NULL);
3986 prev_id = blame_line->id;
3988 waddstr(view->window, "........");
3992 waddstr(view->window, "........");
3996 if (view->focussed && nprinted == s->selected_line - 1)
3997 wstandend(view->window);
3998 waddstr(view->window, " ");
4000 if (view->ncols <= 9) {
4002 wline = wcsdup(L"");
4003 if (wline == NULL) {
4004 err = got_error_from_errno("wcsdup");
4008 } else if (s->first_displayed_line + nprinted ==
4010 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4011 err = add_matched_line(&width, line, view->ncols - 9, 9,
4012 view->window, regmatch);
4019 err = format_line(&wline, &width, line,
4020 view->ncols - 9, 9);
4021 waddwstr(view->window, wline);
4027 if (width <= view->ncols - 1)
4028 waddch(view->window, '\n');
4029 if (++nprinted == 1)
4030 s->first_displayed_line = lineno;
4033 s->last_displayed_line = lineno;
4040 static const struct got_error *
4041 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4043 const struct got_error *err = NULL;
4044 struct tog_blame_cb_args *a = arg;
4045 struct tog_blame_line *line;
4048 if (nlines != a->nlines ||
4049 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4050 return got_error(GOT_ERR_RANGE);
4052 errcode = pthread_mutex_lock(&tog_mutex);
4054 return got_error_set_errno(errcode, "pthread_mutex_lock");
4056 if (*a->quit) { /* user has quit the blame view */
4057 err = got_error(GOT_ERR_ITER_COMPLETED);
4062 goto done; /* no change in this commit */
4064 line = &a->lines[lineno - 1];
4065 if (line->annotated)
4068 line->id = got_object_id_dup(id);
4069 if (line->id == NULL) {
4070 err = got_error_from_errno("got_object_id_dup");
4073 line->annotated = 1;
4075 errcode = pthread_mutex_unlock(&tog_mutex);
4077 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4082 blame_thread(void *arg)
4084 const struct got_error *err;
4085 struct tog_blame_thread_args *ta = arg;
4086 struct tog_blame_cb_args *a = ta->cb_args;
4089 err = block_signals_used_by_main_thread();
4093 err = got_blame(ta->path, a->commit_id, ta->repo,
4094 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4095 if (err && err->code == GOT_ERR_CANCELLED)
4098 errcode = pthread_mutex_lock(&tog_mutex);
4100 return (void *)got_error_set_errno(errcode,
4101 "pthread_mutex_lock");
4103 got_repo_close(ta->repo);
4107 errcode = pthread_mutex_unlock(&tog_mutex);
4108 if (errcode && err == NULL)
4109 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4114 static struct got_object_id *
4115 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4116 int first_displayed_line, int selected_line)
4118 struct tog_blame_line *line;
4123 line = &lines[first_displayed_line - 1 + selected_line - 1];
4124 if (!line->annotated)
4130 static const struct got_error *
4131 stop_blame(struct tog_blame *blame)
4133 const struct got_error *err = NULL;
4136 if (blame->thread) {
4138 errcode = pthread_mutex_unlock(&tog_mutex);
4140 return got_error_set_errno(errcode,
4141 "pthread_mutex_unlock");
4142 errcode = pthread_join(blame->thread, (void **)&err);
4144 return got_error_set_errno(errcode, "pthread_join");
4145 errcode = pthread_mutex_lock(&tog_mutex);
4147 return got_error_set_errno(errcode,
4148 "pthread_mutex_lock");
4149 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4151 blame->thread = NULL;
4153 if (blame->thread_args.repo) {
4154 got_repo_close(blame->thread_args.repo);
4155 blame->thread_args.repo = NULL;
4158 if (fclose(blame->f) != 0 && err == NULL)
4159 err = got_error_from_errno("fclose");
4163 for (i = 0; i < blame->nlines; i++)
4164 free(blame->lines[i].id);
4166 blame->lines = NULL;
4168 free(blame->cb_args.commit_id);
4169 blame->cb_args.commit_id = NULL;
4174 static const struct got_error *
4175 cancel_blame_view(void *arg)
4177 const struct got_error *err = NULL;
4181 errcode = pthread_mutex_lock(&tog_mutex);
4183 return got_error_set_errno(errcode,
4184 "pthread_mutex_unlock");
4187 err = got_error(GOT_ERR_CANCELLED);
4189 errcode = pthread_mutex_unlock(&tog_mutex);
4191 return got_error_set_errno(errcode,
4192 "pthread_mutex_lock");
4197 static const struct got_error *
4198 run_blame(struct tog_view *view)
4200 struct tog_blame_view_state *s = &view->state.blame;
4201 struct tog_blame *blame = &s->blame;
4202 const struct got_error *err = NULL;
4203 struct got_blob_object *blob = NULL;
4204 struct got_repository *thread_repo = NULL;
4205 struct got_object_id *obj_id = NULL;
4208 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4213 err = got_object_get_type(&obj_type, s->repo, obj_id);
4217 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4218 err = got_error(GOT_ERR_OBJ_TYPE);
4222 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4225 blame->f = got_opentemp();
4226 if (blame->f == NULL) {
4227 err = got_error_from_errno("got_opentemp");
4230 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4231 &blame->line_offsets, blame->f, blob);
4232 if (err || blame->nlines == 0)
4235 /* Don't include \n at EOF in the blame line count. */
4236 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4239 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4240 if (blame->lines == NULL) {
4241 err = got_error_from_errno("calloc");
4245 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4249 blame->cb_args.view = view;
4250 blame->cb_args.lines = blame->lines;
4251 blame->cb_args.nlines = blame->nlines;
4252 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4253 if (blame->cb_args.commit_id == NULL) {
4254 err = got_error_from_errno("got_object_id_dup");
4257 blame->cb_args.quit = &s->done;
4259 blame->thread_args.path = s->path;
4260 blame->thread_args.repo = thread_repo;
4261 blame->thread_args.cb_args = &blame->cb_args;
4262 blame->thread_args.complete = &s->blame_complete;
4263 blame->thread_args.cancel_cb = cancel_blame_view;
4264 blame->thread_args.cancel_arg = &s->done;
4265 s->blame_complete = 0;
4269 got_object_blob_close(blob);
4276 static const struct got_error *
4277 open_blame_view(struct tog_view *view, char *path,
4278 struct got_object_id *commit_id, struct got_repository *repo)
4280 const struct got_error *err = NULL;
4281 struct tog_blame_view_state *s = &view->state.blame;
4283 SIMPLEQ_INIT(&s->blamed_commits);
4285 s->path = strdup(path);
4286 if (s->path == NULL)
4287 return got_error_from_errno("strdup");
4289 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4295 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4296 s->first_displayed_line = 1;
4297 s->last_displayed_line = view->nlines;
4298 s->selected_line = 1;
4299 s->blame_complete = 0;
4301 s->commit_id = commit_id;
4302 memset(&s->blame, 0, sizeof(s->blame));
4304 SIMPLEQ_INIT(&s->colors);
4305 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4306 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4307 get_color_value("TOG_COLOR_COMMIT"));
4312 view->show = show_blame_view;
4313 view->input = input_blame_view;
4314 view->close = close_blame_view;
4315 view->search_start = search_start_blame_view;
4316 view->search_next = search_next_blame_view;
4318 return run_blame(view);
4321 static const struct got_error *
4322 close_blame_view(struct tog_view *view)
4324 const struct got_error *err = NULL;
4325 struct tog_blame_view_state *s = &view->state.blame;
4327 if (s->blame.thread)
4328 err = stop_blame(&s->blame);
4330 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4331 struct got_object_qid *blamed_commit;
4332 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4333 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4334 got_object_qid_free(blamed_commit);
4338 free_colors(&s->colors);
4343 static const struct got_error *
4344 search_start_blame_view(struct tog_view *view)
4346 struct tog_blame_view_state *s = &view->state.blame;
4348 s->matched_line = 0;
4352 static const struct got_error *
4353 search_next_blame_view(struct tog_view *view)
4355 struct tog_blame_view_state *s = &view->state.blame;
4358 if (!view->searching) {
4359 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4363 if (s->matched_line) {
4364 if (view->searching == TOG_SEARCH_FORWARD)
4365 lineno = s->matched_line + 1;
4367 lineno = s->matched_line - 1;
4369 if (view->searching == TOG_SEARCH_FORWARD)
4372 lineno = s->blame.nlines;
4380 if (lineno <= 0 || lineno > s->blame.nlines) {
4381 if (s->matched_line == 0) {
4382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4387 if (view->searching == TOG_SEARCH_FORWARD)
4390 lineno = s->blame.nlines;
4393 offset = s->blame.line_offsets[lineno - 1];
4394 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4396 return got_error_from_errno("fseeko");
4399 line = parse_next_line(s->blame.f, &len);
4401 match_line(line, &view->regex, 1, &view->regmatch)) {
4402 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4403 s->matched_line = lineno;
4408 if (view->searching == TOG_SEARCH_FORWARD)
4414 if (s->matched_line) {
4415 s->first_displayed_line = s->matched_line;
4416 s->selected_line = 1;
4422 static const struct got_error *
4423 show_blame_view(struct tog_view *view)
4425 const struct got_error *err = NULL;
4426 struct tog_blame_view_state *s = &view->state.blame;
4429 if (s->blame.thread == NULL) {
4430 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4431 &s->blame.thread_args);
4433 return got_error_set_errno(errcode, "pthread_create");
4435 halfdelay(1); /* fast refresh while annotating */
4438 if (s->blame_complete)
4439 halfdelay(10); /* disable fast refresh */
4441 err = draw_blame(view);
4447 static const struct got_error *
4448 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4449 struct tog_view **focus_view, struct tog_view *view, int ch)
4451 const struct got_error *err = NULL, *thread_err = NULL;
4452 struct tog_view *diff_view;
4453 struct tog_blame_view_state *s = &view->state.blame;
4462 if (s->selected_line > 1)
4464 else if (s->selected_line == 1 &&
4465 s->first_displayed_line > 1)
4466 s->first_displayed_line--;
4470 if (s->first_displayed_line == 1) {
4471 s->selected_line = 1;
4474 if (s->first_displayed_line > view->nlines - 2)
4475 s->first_displayed_line -=
4478 s->first_displayed_line = 1;
4482 if (s->selected_line < view->nlines - 2 &&
4483 s->first_displayed_line +
4484 s->selected_line <= s->blame.nlines)
4486 else if (s->last_displayed_line <
4488 s->first_displayed_line++;
4492 struct got_object_id *id = NULL;
4493 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4494 s->first_displayed_line, s->selected_line);
4498 struct got_commit_object *commit;
4499 struct got_object_qid *pid;
4500 struct got_object_id *blob_id = NULL;
4502 err = got_object_open_as_commit(&commit,
4506 pid = SIMPLEQ_FIRST(
4507 got_object_commit_get_parent_ids(commit));
4509 got_object_commit_close(commit);
4512 /* Check if path history ends here. */
4513 err = got_object_id_by_path(&blob_id, s->repo,
4516 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4518 got_object_commit_close(commit);
4521 err = got_object_get_type(&obj_type, s->repo,
4524 /* Can't blame non-blob type objects. */
4525 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4526 got_object_commit_close(commit);
4529 err = got_object_qid_alloc(&s->blamed_commit,
4531 got_object_commit_close(commit);
4533 if (got_object_id_cmp(id,
4534 s->blamed_commit->id) == 0)
4536 err = got_object_qid_alloc(&s->blamed_commit,
4542 thread_err = stop_blame(&s->blame);
4546 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4547 s->blamed_commit, entry);
4548 err = run_blame(view);
4554 struct got_object_qid *first;
4555 first = SIMPLEQ_FIRST(&s->blamed_commits);
4556 if (!got_object_id_cmp(first->id, s->commit_id))
4559 thread_err = stop_blame(&s->blame);
4563 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4564 got_object_qid_free(s->blamed_commit);
4566 SIMPLEQ_FIRST(&s->blamed_commits);
4567 err = run_blame(view);
4574 struct got_object_id *id = NULL;
4575 struct got_object_qid *pid;
4576 struct got_commit_object *commit = NULL;
4577 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4578 s->first_displayed_line, s->selected_line);
4581 err = got_object_open_as_commit(&commit, s->repo, id);
4584 pid = SIMPLEQ_FIRST(
4585 got_object_commit_get_parent_ids(commit));
4586 if (view_is_parent_view(view))
4587 begin_x = view_split_begin_x(view->begin_x);
4588 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4589 if (diff_view == NULL) {
4590 got_object_commit_close(commit);
4591 err = got_error_from_errno("view_open");
4594 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4595 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4596 got_object_commit_close(commit);
4598 view_close(diff_view);
4601 if (view_is_parent_view(view)) {
4602 err = view_close_child(view);
4605 err = view_set_child(view, diff_view);
4607 view_close(diff_view);
4610 *focus_view = diff_view;
4611 view->child_focussed = 1;
4613 *new_view = diff_view;
4621 if (s->last_displayed_line >= s->blame.nlines &&
4622 s->selected_line >= MIN(s->blame.nlines,
4623 view->nlines - 2)) {
4626 if (s->last_displayed_line >= s->blame.nlines &&
4627 s->selected_line < view->nlines - 2) {
4628 s->selected_line = MIN(s->blame.nlines,
4632 if (s->last_displayed_line + view->nlines - 2
4634 s->first_displayed_line +=
4637 s->first_displayed_line =
4642 if (s->selected_line > view->nlines - 2) {
4643 s->selected_line = MIN(s->blame.nlines,
4650 return thread_err ? thread_err : err;
4653 static const struct got_error *
4654 cmd_blame(int argc, char *argv[])
4656 const struct got_error *error;
4657 struct got_repository *repo = NULL;
4658 struct got_worktree *worktree = NULL;
4659 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4660 char *link_target = NULL;
4661 struct got_object_id *commit_id = NULL;
4662 char *commit_id_str = NULL;
4664 struct tog_view *view;
4667 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4672 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4675 commit_id_str = optarg;
4678 repo_path = realpath(optarg, NULL);
4679 if (repo_path == NULL)
4680 return got_error_from_errno2("realpath",
4695 cwd = getcwd(NULL, 0);
4697 return got_error_from_errno("getcwd");
4699 error = got_worktree_open(&worktree, cwd);
4700 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4703 if (repo_path == NULL) {
4706 strdup(got_worktree_get_repo_path(worktree));
4708 repo_path = strdup(cwd);
4710 if (repo_path == NULL) {
4711 error = got_error_from_errno("strdup");
4715 error = got_repo_open(&repo, repo_path, NULL);
4719 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4726 error = apply_unveil(got_repo_get_path(repo), NULL);
4730 if (commit_id_str == NULL) {
4731 struct got_reference *head_ref;
4732 error = got_ref_open(&head_ref, repo, worktree ?
4733 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4736 error = got_ref_resolve(&commit_id, repo, head_ref);
4737 got_ref_close(head_ref);
4739 error = got_repo_match_object_id(&commit_id, NULL,
4740 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4745 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4747 error = got_error_from_errno("view_open");
4751 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4756 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4761 /* Release work tree lock. */
4762 got_worktree_close(worktree);
4765 error = view_loop(view);
4773 got_worktree_close(worktree);
4775 got_repo_close(repo);
4779 static const struct got_error *
4780 draw_tree_entries(struct tog_view *view, const char *parent_path)
4782 struct tog_tree_view_state *s = &view->state.tree;
4783 const struct got_error *err = NULL;
4784 struct got_tree_entry *te;
4786 struct tog_color *tc;
4787 int width, n, i, nentries;
4788 int limit = view->nlines;
4792 werase(view->window);
4797 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4800 if (view_needs_focus_indication(view))
4801 wstandout(view->window);
4802 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4804 wattr_on(view->window,
4805 COLOR_PAIR(tc->colorpair), NULL);
4806 waddwstr(view->window, wline);
4808 wattr_off(view->window,
4809 COLOR_PAIR(tc->colorpair), NULL);
4810 if (view_needs_focus_indication(view))
4811 wstandend(view->window);
4814 if (width < view->ncols - 1)
4815 waddch(view->window, '\n');
4818 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4821 waddwstr(view->window, wline);
4824 if (width < view->ncols - 1)
4825 waddch(view->window, '\n');
4828 waddch(view->window, '\n');
4832 if (s->first_displayed_entry == NULL) {
4833 te = got_object_tree_get_first_entry(s->tree);
4834 if (s->selected == 0) {
4836 wstandout(view->window);
4837 s->selected_entry = NULL;
4839 waddstr(view->window, " ..\n"); /* parent directory */
4840 if (s->selected == 0 && view->focussed)
4841 wstandend(view->window);
4848 te = s->first_displayed_entry;
4851 nentries = got_object_tree_get_nentries(s->tree);
4852 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4853 char *line = NULL, *id_str = NULL, *link_target = NULL;
4854 const char *modestr = "";
4857 te = got_object_tree_get_entry(s->tree, i);
4858 mode = got_tree_entry_get_mode(te);
4861 err = got_object_id_str(&id_str,
4862 got_tree_entry_get_id(te));
4864 return got_error_from_errno(
4865 "got_object_id_str");
4867 if (got_object_tree_entry_is_submodule(te))
4869 else if (S_ISLNK(mode)) {
4872 err = got_tree_entry_get_symlink_target(&link_target,
4878 for (i = 0; i < strlen(link_target); i++) {
4879 if (!isprint((unsigned char)link_target[i]))
4880 link_target[i] = '?';
4884 else if (S_ISDIR(mode))
4886 else if (mode & S_IXUSR)
4888 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4889 got_tree_entry_get_name(te), modestr,
4890 link_target ? " -> ": "",
4891 link_target ? link_target : "") == -1) {
4894 return got_error_from_errno("asprintf");
4898 err = format_line(&wline, &width, line, view->ncols, 0);
4903 if (n == s->selected) {
4905 wstandout(view->window);
4906 s->selected_entry = te;
4908 tc = match_color(&s->colors, line);
4910 wattr_on(view->window,
4911 COLOR_PAIR(tc->colorpair), NULL);
4912 waddwstr(view->window, wline);
4914 wattr_off(view->window,
4915 COLOR_PAIR(tc->colorpair), NULL);
4916 if (width < view->ncols - 1)
4917 waddch(view->window, '\n');
4918 if (n == s->selected && view->focussed)
4919 wstandend(view->window);
4925 s->last_displayed_entry = te;
4934 tree_scroll_up(struct tog_view *view, int maxscroll)
4936 struct tog_tree_view_state *s = &view->state.tree;
4937 struct got_tree_entry *te;
4938 int isroot = s->tree == s->root;
4941 if (s->first_displayed_entry == NULL)
4944 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4945 while (i++ < maxscroll) {
4948 s->first_displayed_entry = NULL;
4951 s->first_displayed_entry = te;
4952 te = got_tree_entry_get_prev(s->tree, te);
4957 tree_scroll_down(struct tog_view *view, int maxscroll)
4959 struct tog_tree_view_state *s = &view->state.tree;
4960 struct got_tree_entry *next, *last;
4963 if (s->first_displayed_entry)
4964 next = got_tree_entry_get_next(s->tree,
4965 s->first_displayed_entry);
4967 next = got_object_tree_get_first_entry(s->tree);
4969 last = s->last_displayed_entry;
4970 while (next && last && n++ < maxscroll) {
4971 last = got_tree_entry_get_next(s->tree, last);
4973 s->first_displayed_entry = next;
4974 next = got_tree_entry_get_next(s->tree, next);
4979 static const struct got_error *
4980 tree_entry_path(char **path, struct tog_parent_trees *parents,
4981 struct got_tree_entry *te)
4983 const struct got_error *err = NULL;
4984 struct tog_parent_tree *pt;
4985 size_t len = 2; /* for leading slash and NUL */
4987 TAILQ_FOREACH(pt, parents, entry)
4988 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4991 len += strlen(got_tree_entry_get_name(te));
4993 *path = calloc(1, len);
4995 return got_error_from_errno("calloc");
4998 pt = TAILQ_LAST(parents, tog_parent_trees);
5000 const char *name = got_tree_entry_get_name(pt->selected_entry);
5001 if (strlcat(*path, name, len) >= len) {
5002 err = got_error(GOT_ERR_NO_SPACE);
5005 if (strlcat(*path, "/", len) >= len) {
5006 err = got_error(GOT_ERR_NO_SPACE);
5009 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5012 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5013 err = got_error(GOT_ERR_NO_SPACE);
5025 static const struct got_error *
5026 blame_tree_entry(struct tog_view **new_view, int begin_x,
5027 struct got_tree_entry *te, struct tog_parent_trees *parents,
5028 struct got_object_id *commit_id, struct got_repository *repo)
5030 const struct got_error *err = NULL;
5032 struct tog_view *blame_view;
5036 err = tree_entry_path(&path, parents, te);
5040 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5041 if (blame_view == NULL) {
5042 err = got_error_from_errno("view_open");
5046 err = open_blame_view(blame_view, path, commit_id, repo);
5048 if (err->code == GOT_ERR_CANCELLED)
5050 view_close(blame_view);
5052 *new_view = blame_view;
5058 static const struct got_error *
5059 log_tree_entry(struct tog_view **new_view, int begin_x,
5060 struct got_tree_entry *te, struct tog_parent_trees *parents,
5061 struct got_object_id *commit_id, struct got_repository *repo)
5063 struct tog_view *log_view;
5064 const struct got_error *err = NULL;
5069 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5070 if (log_view == NULL)
5071 return got_error_from_errno("view_open");
5073 err = tree_entry_path(&path, parents, te);
5077 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5079 view_close(log_view);
5081 *new_view = log_view;
5086 static const struct got_error *
5087 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5088 struct got_object_id *commit_id, struct got_repository *repo)
5090 const struct got_error *err = NULL;
5091 char *commit_id_str = NULL;
5092 struct tog_tree_view_state *s = &view->state.tree;
5094 TAILQ_INIT(&s->parents);
5096 err = got_object_id_str(&commit_id_str, commit_id);
5100 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5101 err = got_error_from_errno("asprintf");
5105 s->root = s->tree = root;
5106 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5107 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5108 s->commit_id = got_object_id_dup(commit_id);
5109 if (s->commit_id == NULL) {
5110 err = got_error_from_errno("got_object_id_dup");
5115 SIMPLEQ_INIT(&s->colors);
5117 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5118 err = add_color(&s->colors, "\\$$",
5119 TOG_COLOR_TREE_SUBMODULE,
5120 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5123 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5124 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5126 free_colors(&s->colors);
5129 err = add_color(&s->colors, "/$",
5130 TOG_COLOR_TREE_DIRECTORY,
5131 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5133 free_colors(&s->colors);
5137 err = add_color(&s->colors, "\\*$",
5138 TOG_COLOR_TREE_EXECUTABLE,
5139 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5141 free_colors(&s->colors);
5145 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5146 get_color_value("TOG_COLOR_COMMIT"));
5148 free_colors(&s->colors);
5153 view->show = show_tree_view;
5154 view->input = input_tree_view;
5155 view->close = close_tree_view;
5156 view->search_start = search_start_tree_view;
5157 view->search_next = search_next_tree_view;
5159 free(commit_id_str);
5161 free(s->tree_label);
5162 s->tree_label = NULL;
5167 static const struct got_error *
5168 close_tree_view(struct tog_view *view)
5170 struct tog_tree_view_state *s = &view->state.tree;
5172 free_colors(&s->colors);
5173 free(s->tree_label);
5174 s->tree_label = NULL;
5176 s->commit_id = NULL;
5177 while (!TAILQ_EMPTY(&s->parents)) {
5178 struct tog_parent_tree *parent;
5179 parent = TAILQ_FIRST(&s->parents);
5180 TAILQ_REMOVE(&s->parents, parent, entry);
5184 if (s->tree != s->root)
5185 got_object_tree_close(s->tree);
5186 got_object_tree_close(s->root);
5190 static const struct got_error *
5191 search_start_tree_view(struct tog_view *view)
5193 struct tog_tree_view_state *s = &view->state.tree;
5195 s->matched_entry = NULL;
5200 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5202 regmatch_t regmatch;
5204 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5208 static const struct got_error *
5209 search_next_tree_view(struct tog_view *view)
5211 struct tog_tree_view_state *s = &view->state.tree;
5212 struct got_tree_entry *te = NULL;
5214 if (!view->searching) {
5215 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5219 if (s->matched_entry) {
5220 if (view->searching == TOG_SEARCH_FORWARD) {
5221 if (s->selected_entry)
5222 te = got_tree_entry_get_next(s->tree,
5225 te = got_object_tree_get_first_entry(s->tree);
5227 if (s->selected_entry == NULL)
5228 te = got_object_tree_get_last_entry(s->tree);
5230 te = got_tree_entry_get_prev(s->tree,
5234 if (view->searching == TOG_SEARCH_FORWARD)
5235 te = got_object_tree_get_first_entry(s->tree);
5237 te = got_object_tree_get_last_entry(s->tree);
5242 if (s->matched_entry == NULL) {
5243 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5246 if (view->searching == TOG_SEARCH_FORWARD)
5247 te = got_object_tree_get_first_entry(s->tree);
5249 te = got_object_tree_get_last_entry(s->tree);
5252 if (match_tree_entry(te, &view->regex)) {
5253 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5254 s->matched_entry = te;
5258 if (view->searching == TOG_SEARCH_FORWARD)
5259 te = got_tree_entry_get_next(s->tree, te);
5261 te = got_tree_entry_get_prev(s->tree, te);
5264 if (s->matched_entry) {
5265 s->first_displayed_entry = s->matched_entry;
5272 static const struct got_error *
5273 show_tree_view(struct tog_view *view)
5275 const struct got_error *err = NULL;
5276 struct tog_tree_view_state *s = &view->state.tree;
5279 err = tree_entry_path(&parent_path, &s->parents, NULL);
5283 err = draw_tree_entries(view, parent_path);
5290 static const struct got_error *
5291 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5292 struct tog_view **focus_view, struct tog_view *view, int ch)
5294 const struct got_error *err = NULL;
5295 struct tog_tree_view_state *s = &view->state.tree;
5296 struct tog_view *log_view, *ref_view;
5301 s->show_ids = !s->show_ids;
5304 if (!s->selected_entry)
5306 if (view_is_parent_view(view))
5307 begin_x = view_split_begin_x(view->begin_x);
5308 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5309 &s->parents, s->commit_id, s->repo);
5310 if (view_is_parent_view(view)) {
5311 err = view_close_child(view);
5314 err = view_set_child(view, log_view);
5316 view_close(log_view);
5319 *focus_view = log_view;
5320 view->child_focussed = 1;
5322 *new_view = log_view;
5325 if (view_is_parent_view(view))
5326 begin_x = view_split_begin_x(view->begin_x);
5327 ref_view = view_open(view->nlines, view->ncols,
5328 view->begin_y, begin_x, TOG_VIEW_REF);
5329 if (ref_view == NULL)
5330 return got_error_from_errno("view_open");
5331 err = open_ref_view(ref_view, s->repo);
5333 view_close(ref_view);
5336 if (view_is_parent_view(view)) {
5337 err = view_close_child(view);
5340 err = view_set_child(view, ref_view);
5342 view_close(ref_view);
5345 *focus_view = ref_view;
5346 view->child_focussed = 1;
5348 *new_view = ref_view;
5352 if (s->selected > 0) {
5356 tree_scroll_up(view, 1);
5360 if (s->tree == s->root) {
5361 if (got_object_tree_get_first_entry(s->tree) ==
5362 s->first_displayed_entry)
5365 if (s->first_displayed_entry == NULL)
5368 tree_scroll_up(view, MAX(0, view->nlines - 3));
5372 if (s->selected < s->ndisplayed - 1) {
5376 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5378 /* can't scroll any further */
5380 tree_scroll_down(view, 1);
5384 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5386 /* can't scroll any further; move cursor down */
5387 if (s->selected < s->ndisplayed - 1)
5388 s->selected = s->ndisplayed - 1;
5391 tree_scroll_down(view, view->nlines - 3);
5396 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5397 struct tog_parent_tree *parent;
5398 /* user selected '..' */
5399 if (s->tree == s->root)
5401 parent = TAILQ_FIRST(&s->parents);
5402 TAILQ_REMOVE(&s->parents, parent,
5404 got_object_tree_close(s->tree);
5405 s->tree = parent->tree;
5406 s->first_displayed_entry =
5407 parent->first_displayed_entry;
5409 parent->selected_entry;
5410 s->selected = parent->selected;
5412 } else if (S_ISDIR(got_tree_entry_get_mode(
5413 s->selected_entry))) {
5414 struct got_tree_object *subtree;
5415 err = got_object_open_as_tree(&subtree, s->repo,
5416 got_tree_entry_get_id(s->selected_entry));
5419 err = tree_view_visit_subtree(subtree, s);
5421 got_object_tree_close(subtree);
5424 } else if (S_ISREG(got_tree_entry_get_mode(
5425 s->selected_entry))) {
5426 struct tog_view *blame_view;
5427 int begin_x = view_is_parent_view(view) ?
5428 view_split_begin_x(view->begin_x) : 0;
5430 err = blame_tree_entry(&blame_view, begin_x,
5431 s->selected_entry, &s->parents,
5432 s->commit_id, s->repo);
5435 if (view_is_parent_view(view)) {
5436 err = view_close_child(view);
5439 err = view_set_child(view, blame_view);
5441 view_close(blame_view);
5444 *focus_view = blame_view;
5445 view->child_focussed = 1;
5447 *new_view = blame_view;
5451 if (s->selected > view->nlines)
5452 s->selected = s->ndisplayed - 1;
5465 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5470 static const struct got_error *
5471 cmd_tree(int argc, char *argv[])
5473 const struct got_error *error;
5474 struct got_repository *repo = NULL;
5475 struct got_worktree *worktree = NULL;
5476 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5477 struct got_object_id *commit_id = NULL;
5478 char *commit_id_arg = NULL;
5479 struct got_commit_object *commit = NULL;
5480 struct got_tree_object *tree = NULL;
5482 struct tog_view *view;
5485 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5490 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5493 commit_id_arg = optarg;
5496 repo_path = realpath(optarg, NULL);
5497 if (repo_path == NULL)
5498 return got_error_from_errno2("realpath",
5513 cwd = getcwd(NULL, 0);
5515 return got_error_from_errno("getcwd");
5517 error = got_worktree_open(&worktree, cwd);
5518 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5521 if (repo_path == NULL) {
5524 strdup(got_worktree_get_repo_path(worktree));
5526 repo_path = strdup(cwd);
5528 if (repo_path == NULL) {
5529 error = got_error_from_errno("strdup");
5533 error = got_repo_open(&repo, repo_path, NULL);
5537 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5544 error = apply_unveil(got_repo_get_path(repo), NULL);
5548 error = got_repo_match_object_id(&commit_id, NULL,
5549 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5550 GOT_OBJ_TYPE_COMMIT, 1, repo);
5554 error = got_object_open_as_commit(&commit, repo, commit_id);
5558 error = got_object_open_as_tree(&tree, repo,
5559 got_object_commit_get_tree_id(commit));
5563 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5565 error = got_error_from_errno("view_open");
5568 error = open_tree_view(view, tree, commit_id, repo);
5571 if (!got_path_is_root_dir(in_repo_path)) {
5572 error = tree_view_walk_path(&view->state.tree, commit_id,
5579 /* Release work tree lock. */
5580 got_worktree_close(worktree);
5583 error = view_loop(view);
5589 got_object_commit_close(commit);
5591 got_object_tree_close(tree);
5593 got_repo_close(repo);
5597 static const struct got_error *
5598 ref_view_load_refs(struct tog_ref_view_state *s)
5600 const struct got_error *err;
5601 struct got_reflist_entry *sre;
5602 struct tog_reflist_entry *re;
5604 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5605 got_ref_cmp_by_name, NULL);
5610 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5611 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5614 re = malloc(sizeof(*re));
5616 return got_error_from_errno("malloc");
5619 re->idx = s->nrefs++;
5620 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5627 ref_view_free_refs(struct tog_ref_view_state *s)
5629 struct tog_reflist_entry *re;
5631 while (!TAILQ_EMPTY(&s->refs)) {
5632 re = TAILQ_FIRST(&s->refs);
5633 TAILQ_REMOVE(&s->refs, re, entry);
5636 got_ref_list_free(&s->simplerefs);
5639 static const struct got_error *
5640 open_ref_view(struct tog_view *view, struct got_repository *repo)
5642 const struct got_error *err = NULL;
5643 struct tog_ref_view_state *s = &view->state.ref;
5645 s->selected_entry = 0;
5648 SIMPLEQ_INIT(&s->simplerefs);
5649 TAILQ_INIT(&s->refs);
5650 SIMPLEQ_INIT(&s->colors);
5652 err = ref_view_load_refs(s);
5656 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5658 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5659 err = add_color(&s->colors, "^refs/heads/",
5660 TOG_COLOR_REFS_HEADS,
5661 get_color_value("TOG_COLOR_REFS_HEADS"));
5665 err = add_color(&s->colors, "^refs/tags/",
5666 TOG_COLOR_REFS_TAGS,
5667 get_color_value("TOG_COLOR_REFS_TAGS"));
5671 err = add_color(&s->colors, "^refs/remotes/",
5672 TOG_COLOR_REFS_REMOTES,
5673 get_color_value("TOG_COLOR_REFS_REMOTES"));
5678 view->show = show_ref_view;
5679 view->input = input_ref_view;
5680 view->close = close_ref_view;
5681 view->search_start = search_start_ref_view;
5682 view->search_next = search_next_ref_view;
5685 free_colors(&s->colors);
5689 static const struct got_error *
5690 close_ref_view(struct tog_view *view)
5692 struct tog_ref_view_state *s = &view->state.ref;
5694 ref_view_free_refs(s);
5695 free_colors(&s->colors);
5700 static const struct got_error *
5701 resolve_reflist_entry(struct got_object_id **commit_id,
5702 struct tog_reflist_entry *re, struct got_repository *repo)
5704 const struct got_error *err = NULL;
5705 struct got_object_id *obj_id;
5706 struct got_tag_object *tag = NULL;
5711 err = got_ref_resolve(&obj_id, repo, re->ref);
5715 err = got_object_get_type(&obj_type, repo, obj_id);
5720 case GOT_OBJ_TYPE_COMMIT:
5721 *commit_id = obj_id;
5723 case GOT_OBJ_TYPE_TAG:
5724 err = got_object_open_as_tag(&tag, repo, obj_id);
5728 err = got_object_get_type(&obj_type, repo,
5729 got_object_tag_get_object_id(tag));
5732 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5733 err = got_error(GOT_ERR_OBJ_TYPE);
5736 *commit_id = got_object_id_dup(
5737 got_object_tag_get_object_id(tag));
5738 if (*commit_id == NULL) {
5739 err = got_error_from_errno("got_object_id_dup");
5744 err = got_error(GOT_ERR_OBJ_TYPE);
5750 got_object_tag_close(tag);
5758 static const struct got_error *
5759 log_ref_entry(struct tog_view **new_view, int begin_x,
5760 struct tog_reflist_entry *re, struct got_repository *repo)
5762 struct tog_view *log_view;
5763 const struct got_error *err = NULL;
5764 struct got_object_id *commit_id = NULL;
5768 err = resolve_reflist_entry(&commit_id, re, repo);
5770 if (err->code != GOT_ERR_OBJ_TYPE)
5776 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5777 if (log_view == NULL) {
5778 err = got_error_from_errno("view_open");
5782 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5785 view_close(log_view);
5787 *new_view = log_view;
5793 ref_scroll_up(struct tog_view *view, int maxscroll)
5795 struct tog_ref_view_state *s = &view->state.ref;
5796 struct tog_reflist_entry *re;
5799 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5802 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5803 while (i++ < maxscroll) {
5806 s->first_displayed_entry = re;
5807 re = TAILQ_PREV(re, tog_reflist_head, entry);
5812 ref_scroll_down(struct tog_view *view, int maxscroll)
5814 struct tog_ref_view_state *s = &view->state.ref;
5815 struct tog_reflist_entry *next, *last;
5818 if (s->first_displayed_entry)
5819 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5821 next = TAILQ_FIRST(&s->refs);
5823 last = s->last_displayed_entry;
5824 while (next && last && n++ < maxscroll) {
5825 last = TAILQ_NEXT(last, entry);
5827 s->first_displayed_entry = next;
5828 next = TAILQ_NEXT(next, entry);
5833 static const struct got_error *
5834 search_start_ref_view(struct tog_view *view)
5836 struct tog_ref_view_state *s = &view->state.ref;
5838 s->matched_entry = NULL;
5843 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5845 regmatch_t regmatch;
5847 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5851 static const struct got_error *
5852 search_next_ref_view(struct tog_view *view)
5854 struct tog_ref_view_state *s = &view->state.ref;
5855 struct tog_reflist_entry *re = NULL;
5857 if (!view->searching) {
5858 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5862 if (s->matched_entry) {
5863 if (view->searching == TOG_SEARCH_FORWARD) {
5864 if (s->selected_entry)
5865 re = TAILQ_NEXT(s->selected_entry, entry);
5867 re = TAILQ_PREV(s->selected_entry,
5868 tog_reflist_head, entry);
5870 if (s->selected_entry == NULL)
5871 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5873 re = TAILQ_PREV(s->selected_entry,
5874 tog_reflist_head, entry);
5877 if (view->searching == TOG_SEARCH_FORWARD)
5878 re = TAILQ_FIRST(&s->refs);
5880 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5885 if (s->matched_entry == NULL) {
5886 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5889 if (view->searching == TOG_SEARCH_FORWARD)
5890 re = TAILQ_FIRST(&s->refs);
5892 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5895 if (match_reflist_entry(re, &view->regex)) {
5896 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5897 s->matched_entry = re;
5901 if (view->searching == TOG_SEARCH_FORWARD)
5902 re = TAILQ_NEXT(re, entry);
5904 re = TAILQ_PREV(re, tog_reflist_head, entry);
5907 if (s->matched_entry) {
5908 s->first_displayed_entry = s->matched_entry;
5915 static const struct got_error *
5916 show_ref_view(struct tog_view *view)
5918 const struct got_error *err = NULL;
5919 struct tog_ref_view_state *s = &view->state.ref;
5920 struct tog_reflist_entry *re;
5923 struct tog_color *tc;
5925 int limit = view->nlines;
5927 werase(view->window);
5934 re = s->first_displayed_entry;
5936 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5938 return got_error_from_errno("asprintf");
5940 err = format_line(&wline, &width, line, view->ncols, 0);
5945 if (view_needs_focus_indication(view))
5946 wstandout(view->window);
5947 waddwstr(view->window, wline);
5948 if (view_needs_focus_indication(view))
5949 wstandend(view->window);
5954 if (width < view->ncols - 1)
5955 waddch(view->window, '\n');
5960 while (re && limit > 0) {
5963 if (got_ref_is_symbolic(re->ref)) {
5964 if (asprintf(&line, "%s -> %s",
5965 got_ref_get_name(re->ref),
5966 got_ref_get_symref_target(re->ref)) == -1)
5967 return got_error_from_errno("asprintf");
5968 } else if (s->show_ids) {
5969 struct got_object_id *id;
5971 err = got_ref_resolve(&id, s->repo, re->ref);
5974 err = got_object_id_str(&id_str, id);
5979 if (asprintf(&line, "%s: %s",
5980 got_ref_get_name(re->ref), id_str) == -1) {
5981 err = got_error_from_errno("asprintf");
5989 line = strdup(got_ref_get_name(re->ref));
5991 return got_error_from_errno("strdup");
5994 err = format_line(&wline, &width, line, view->ncols, 0);
5999 if (n == s->selected) {
6001 wstandout(view->window);
6002 s->selected_entry = re;
6004 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6006 wattr_on(view->window,
6007 COLOR_PAIR(tc->colorpair), NULL);
6008 waddwstr(view->window, wline);
6010 wattr_off(view->window,
6011 COLOR_PAIR(tc->colorpair), NULL);
6012 if (width < view->ncols - 1)
6013 waddch(view->window, '\n');
6014 if (n == s->selected && view->focussed)
6015 wstandend(view->window);
6021 s->last_displayed_entry = re;
6024 re = TAILQ_NEXT(re, entry);
6031 static const struct got_error *
6032 browse_ref_tree(struct tog_view **new_view, int begin_x,
6033 struct tog_reflist_entry *re, struct got_repository *repo)
6035 const struct got_error *err = NULL;
6036 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6037 struct got_tree_object *tree = NULL;
6038 struct tog_view *tree_view;
6042 err = resolve_reflist_entry(&commit_id, re, repo);
6044 if (err->code != GOT_ERR_OBJ_TYPE)
6050 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6054 err = got_object_open_as_tree(&tree, repo, tree_id);
6058 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6059 if (tree_view == NULL) {
6060 err = got_error_from_errno("view_open");
6064 err = open_tree_view(tree_view, tree, commit_id, repo);
6068 *new_view = tree_view;
6074 got_object_tree_close(tree);
6078 static const struct got_error *
6079 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6080 struct tog_view **focus_view, struct tog_view *view, int ch)
6082 const struct got_error *err = NULL;
6083 struct tog_ref_view_state *s = &view->state.ref;
6084 struct tog_view *log_view, *tree_view;
6089 s->show_ids = !s->show_ids;
6093 if (!s->selected_entry)
6095 if (view_is_parent_view(view))
6096 begin_x = view_split_begin_x(view->begin_x);
6097 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6099 if (view_is_parent_view(view)) {
6100 err = view_close_child(view);
6103 err = view_set_child(view, log_view);
6105 view_close(log_view);
6108 *focus_view = log_view;
6109 view->child_focussed = 1;
6111 *new_view = log_view;
6114 if (!s->selected_entry)
6116 if (view_is_parent_view(view))
6117 begin_x = view_split_begin_x(view->begin_x);
6118 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6120 if (err || tree_view == NULL)
6122 if (view_is_parent_view(view)) {
6123 err = view_close_child(view);
6126 err = view_set_child(view, tree_view);
6128 view_close(tree_view);
6131 *focus_view = tree_view;
6132 view->child_focussed = 1;
6134 *new_view = tree_view;
6138 if (s->selected > 0) {
6142 ref_scroll_up(view, 1);
6146 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6148 ref_scroll_up(view, MAX(0, view->nlines - 1));
6152 if (s->selected < s->ndisplayed - 1) {
6156 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6157 /* can't scroll any further */
6159 ref_scroll_down(view, 1);
6163 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6164 /* can't scroll any further; move cursor down */
6165 if (s->selected < s->ndisplayed - 1)
6166 s->selected = s->ndisplayed - 1;
6169 ref_scroll_down(view, view->nlines - 1);
6172 ref_view_free_refs(s);
6173 err = ref_view_load_refs(s);
6176 if (s->selected > view->nlines)
6177 s->selected = s->ndisplayed - 1;
6190 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6195 static const struct got_error *
6196 cmd_ref(int argc, char *argv[])
6198 const struct got_error *error;
6199 struct got_repository *repo = NULL;
6200 struct got_worktree *worktree = NULL;
6201 char *cwd = NULL, *repo_path = NULL;
6203 struct tog_view *view;
6206 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6211 while ((ch = getopt(argc, argv, "r:")) != -1) {
6214 repo_path = realpath(optarg, NULL);
6215 if (repo_path == NULL)
6216 return got_error_from_errno2("realpath",
6231 cwd = getcwd(NULL, 0);
6233 return got_error_from_errno("getcwd");
6235 error = got_worktree_open(&worktree, cwd);
6236 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6239 if (repo_path == NULL) {
6242 strdup(got_worktree_get_repo_path(worktree));
6244 repo_path = strdup(cwd);
6246 if (repo_path == NULL) {
6247 error = got_error_from_errno("strdup");
6251 error = got_repo_open(&repo, repo_path, NULL);
6257 error = apply_unveil(got_repo_get_path(repo), NULL);
6261 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6263 error = got_error_from_errno("view_open");
6267 error = open_ref_view(view, repo);
6272 /* Release work tree lock. */
6273 got_worktree_close(worktree);
6276 error = view_loop(view);
6281 got_repo_close(repo);
6286 list_commands(FILE *fp)
6290 fprintf(fp, "commands:");
6291 for (i = 0; i < nitems(tog_commands); i++) {
6292 struct tog_cmd *cmd = &tog_commands[i];
6293 fprintf(fp, " %s", cmd->name);
6299 usage(int hflag, int status)
6301 FILE *fp = (status == 0) ? stdout : stderr;
6303 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6306 fprintf(fp, "lazy usage: %s path\n", getprogname());
6313 make_argv(int argc, ...)
6321 argv = calloc(argc, sizeof(char *));
6324 for (i = 0; i < argc; i++) {
6325 argv[i] = strdup(va_arg(ap, char *));
6326 if (argv[i] == NULL)
6335 * Try to convert 'tog path' into a 'tog log path' command.
6336 * The user could simply have mistyped the command rather than knowingly
6337 * provided a path. So check whether argv[0] can in fact be resolved
6338 * to a path in the HEAD commit and print a special error if not.
6339 * This hack is for mpi@ <3
6341 static const struct got_error *
6342 tog_log_with_path(int argc, char *argv[])
6344 const struct got_error *error = NULL;
6345 struct tog_cmd *cmd = NULL;
6346 struct got_repository *repo = NULL;
6347 struct got_worktree *worktree = NULL;
6348 struct got_object_id *commit_id = NULL, *id = NULL;
6349 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6350 char *commit_id_str = NULL, **cmd_argv = NULL;
6352 cwd = getcwd(NULL, 0);
6354 return got_error_from_errno("getcwd");
6356 error = got_worktree_open(&worktree, cwd);
6357 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6361 repo_path = strdup(got_worktree_get_repo_path(worktree));
6363 repo_path = strdup(cwd);
6364 if (repo_path == NULL) {
6365 error = got_error_from_errno("strdup");
6369 error = got_repo_open(&repo, repo_path, NULL);
6373 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6378 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6379 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6380 GOT_OBJ_TYPE_COMMIT, 1, repo);
6385 got_worktree_close(worktree);
6389 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6391 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6393 fprintf(stderr, "%s: '%s' is no known command or path\n",
6394 getprogname(), argv[0]);
6399 got_repo_close(repo);
6402 error = got_object_id_str(&commit_id_str, commit_id);
6406 cmd = &tog_commands[0]; /* log */
6408 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6409 error = cmd->cmd_main(argc, cmd_argv);
6412 got_repo_close(repo);
6414 got_worktree_close(worktree);
6416 free(commit_id_str);
6423 for (i = 0; i < argc; i++)
6431 main(int argc, char *argv[])
6433 const struct got_error *error = NULL;
6434 struct tog_cmd *cmd = NULL;
6435 int ch, hflag = 0, Vflag = 0;
6436 char **cmd_argv = NULL;
6437 static struct option longopts[] = {
6438 { "version", no_argument, NULL, 'V' },
6442 setlocale(LC_CTYPE, "");
6444 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6464 got_version_print_str();
6471 /* Build an argument vector which runs a default command. */
6472 cmd = &tog_commands[0];
6474 cmd_argv = make_argv(argc, cmd->name);
6478 /* Did the user specify a command? */
6479 for (i = 0; i < nitems(tog_commands); i++) {
6480 if (strncmp(tog_commands[i].name, argv[0],
6481 strlen(argv[0])) == 0) {
6482 cmd = &tog_commands[i];
6491 /* No command specified; try log with a path */
6492 error = tog_log_with_path(argc, argv);
6497 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6504 for (i = 0; i < argc; i++)
6509 if (error && error->code != GOT_ERR_CANCELLED)
6510 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);