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, struct got_reflist_head *refs,
1281 const size_t date_display_cols, int author_display_cols,
1282 struct tog_colors *colors)
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(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(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(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, struct commit_queue_entry **last,
1525 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1526 struct commit_queue *commits, int selected_idx, int limit,
1527 struct got_reflist_head *refs, const char *path, int commits_needed,
1528 struct tog_colors *colors)
1530 const struct got_error *err = NULL;
1531 struct tog_log_view_state *s = &view->state.log;
1532 struct commit_queue_entry *entry;
1534 int ncommits, author_cols = 4;
1535 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1536 char *refs_str = NULL;
1538 struct tog_color *tc;
1539 static const size_t date_display_cols = 12;
1544 if (ncommits == selected_idx) {
1548 entry = TAILQ_NEXT(entry, entry);
1552 if (*selected && !(view->searching && view->search_next_done == 0)) {
1553 err = got_object_id_str(&id_str, (*selected)->id);
1557 err = build_refs_str(&refs_str, refs, (*selected)->id,
1564 if (commits_needed == 0)
1565 halfdelay(10); /* disable fast refresh */
1567 if (commits_needed > 0) {
1568 if (asprintf(&ncommits_str, " [%d/%d] %s",
1569 entry ? entry->idx + 1 : 0, commits->ncommits,
1570 (view->searching && !view->search_next_done) ?
1571 "searching..." : "loading...") == -1) {
1572 err = got_error_from_errno("asprintf");
1576 const char *search_str = NULL;
1578 if (view->searching) {
1579 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1580 search_str = "no more matches";
1581 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1582 search_str = "no matches found";
1583 else if (!view->search_next_done)
1584 search_str = "searching...";
1587 if (asprintf(&ncommits_str, " [%d/%d] %s",
1588 entry ? entry->idx + 1 : 0, commits->ncommits,
1589 search_str ? search_str :
1590 (refs_str ? refs_str : "")) == -1) {
1591 err = got_error_from_errno("asprintf");
1596 if (path && strcmp(path, "/") != 0) {
1597 if (asprintf(&header, "commit %s %s%s",
1598 id_str ? id_str : "........................................",
1599 path, ncommits_str) == -1) {
1600 err = got_error_from_errno("asprintf");
1604 } else if (asprintf(&header, "commit %s%s",
1605 id_str ? id_str : "........................................",
1606 ncommits_str) == -1) {
1607 err = got_error_from_errno("asprintf");
1611 err = format_line(&wline, &width, header, view->ncols, 0);
1615 werase(view->window);
1617 if (view_needs_focus_indication(view))
1618 wstandout(view->window);
1619 tc = get_color(colors, TOG_COLOR_COMMIT);
1621 wattr_on(view->window,
1622 COLOR_PAIR(tc->colorpair), NULL);
1623 waddwstr(view->window, wline);
1625 wattr_off(view->window,
1626 COLOR_PAIR(tc->colorpair), NULL);
1627 while (width < view->ncols) {
1628 waddch(view->window, ' ');
1631 if (view_needs_focus_indication(view))
1632 wstandend(view->window);
1637 /* Grow author column size if necessary. */
1644 if (ncommits >= limit - 1)
1646 author = strdup(got_object_commit_get_author(entry->commit));
1647 if (author == NULL) {
1648 err = got_error_from_errno("strdup");
1651 err = format_author(&wauthor, &width, author, COLS,
1653 if (author_cols < width)
1654 author_cols = width;
1658 entry = TAILQ_NEXT(entry, entry);
1665 if (ncommits >= limit - 1)
1667 if (ncommits == selected_idx)
1668 wstandout(view->window);
1669 err = draw_commit(view, entry->commit, entry->id, refs,
1670 date_display_cols, author_cols, colors);
1671 if (ncommits == selected_idx)
1672 wstandend(view->window);
1677 entry = TAILQ_NEXT(entry, entry);
1690 log_scroll_up(struct tog_view *view, int maxscroll)
1692 struct tog_log_view_state *s = &view->state.log;
1693 struct commit_queue_entry *entry;
1696 entry = TAILQ_FIRST(&s->commits.head);
1697 if (s->first_displayed_entry == entry)
1700 entry = s->first_displayed_entry;
1701 while (entry && nscrolled < maxscroll) {
1702 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1704 s->first_displayed_entry = entry;
1710 static const struct got_error *
1711 trigger_log_thread(struct tog_view *view, int wait)
1713 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1716 halfdelay(1); /* fast refresh while loading commits */
1718 while (ta->commits_needed > 0) {
1719 if (ta->log_complete)
1722 /* Wake the log thread. */
1723 errcode = pthread_cond_signal(&ta->need_commits);
1725 return got_error_set_errno(errcode,
1726 "pthread_cond_signal");
1729 * The mutex will be released while the view loop waits
1730 * in wgetch(), at which time the log thread will run.
1735 /* Display progress update in log view. */
1736 show_log_view(view);
1740 /* Wait right here while next commit is being loaded. */
1741 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1743 return got_error_set_errno(errcode,
1744 "pthread_cond_wait");
1746 /* Display progress update in log view. */
1747 show_log_view(view);
1755 static const struct got_error *
1756 log_scroll_down(struct tog_view *view, int maxscroll)
1758 struct tog_log_view_state *s = &view->state.log;
1759 const struct got_error *err = NULL;
1760 struct commit_queue_entry *pentry;
1761 int nscrolled = 0, ncommits_needed;
1763 if (s->last_displayed_entry == NULL)
1766 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1767 if (s->commits.ncommits < ncommits_needed &&
1768 !s->thread_args.log_complete) {
1770 * Ask the log thread for required amount of commits.
1772 s->thread_args.commits_needed += maxscroll;
1773 err = trigger_log_thread(view, 1);
1779 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1783 s->last_displayed_entry = pentry;
1785 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1788 s->first_displayed_entry = pentry;
1789 } while (++nscrolled < maxscroll);
1794 static const struct got_error *
1795 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1796 struct got_commit_object *commit, struct got_object_id *commit_id,
1797 struct tog_view *log_view, struct got_repository *repo)
1799 const struct got_error *err;
1800 struct got_object_qid *parent_id;
1801 struct tog_view *diff_view;
1803 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1804 if (diff_view == NULL)
1805 return got_error_from_errno("view_open");
1807 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1808 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1809 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1811 *new_view = diff_view;
1815 static const struct got_error *
1816 tree_view_visit_subtree(struct got_tree_object *subtree,
1817 struct tog_tree_view_state *s)
1819 struct tog_parent_tree *parent;
1821 parent = calloc(1, sizeof(*parent));
1823 return got_error_from_errno("calloc");
1825 parent->tree = s->tree;
1826 parent->first_displayed_entry = s->first_displayed_entry;
1827 parent->selected_entry = s->selected_entry;
1828 parent->selected = s->selected;
1829 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1832 s->first_displayed_entry = NULL;
1836 static const struct got_error *
1837 tree_view_walk_path(struct tog_tree_view_state *s,
1838 struct got_object_id *commit_id,
1839 const char *path, struct got_repository *repo)
1841 const struct got_error *err = NULL;
1842 struct got_tree_object *tree = NULL;
1844 char *slash, *subpath = NULL;
1846 /* Walk the path and open corresponding tree objects. */
1849 struct got_tree_entry *te;
1850 struct got_object_id *tree_id;
1856 /* Ensure the correct subtree entry is selected. */
1857 slash = strchr(p, '/');
1859 te_name = strdup(p);
1861 te_name = strndup(p, slash - p);
1862 if (te_name == NULL) {
1863 err = got_error_from_errno("strndup");
1866 te = got_object_tree_find_entry(s->tree, te_name);
1868 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1873 s->first_displayed_entry = s->selected_entry = te;
1875 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1876 break; /* jump to this file's entry */
1878 slash = strchr(p, '/');
1880 subpath = strndup(path, slash - path);
1882 subpath = strdup(path);
1883 if (subpath == NULL) {
1884 err = got_error_from_errno("strdup");
1888 err = got_object_id_by_path(&tree_id, repo, commit_id,
1893 err = got_object_open_as_tree(&tree, repo, tree_id);
1898 err = tree_view_visit_subtree(tree, s);
1900 got_object_tree_close(tree);
1914 static const struct got_error *
1915 browse_commit_tree(struct tog_view **new_view, int begin_x,
1916 struct commit_queue_entry *entry, const char *path,
1917 struct got_repository *repo)
1919 const struct got_error *err = NULL;
1920 struct got_tree_object *tree;
1921 struct tog_tree_view_state *s;
1922 struct tog_view *tree_view;
1924 err = got_object_open_as_tree(&tree, repo,
1925 got_object_commit_get_tree_id(entry->commit));
1929 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1930 if (tree_view == NULL)
1931 return got_error_from_errno("view_open");
1933 err = open_tree_view(tree_view, tree, entry->id, repo);
1935 got_object_tree_close(tree);
1938 s = &tree_view->state.tree;
1940 *new_view = tree_view;
1942 if (got_path_is_root_dir(path))
1945 return tree_view_walk_path(s, entry->id, path, repo);
1948 static const struct got_error *
1949 block_signals_used_by_main_thread(void)
1954 if (sigemptyset(&sigset) == -1)
1955 return got_error_from_errno("sigemptyset");
1957 /* tog handles SIGWINCH and SIGCONT */
1958 if (sigaddset(&sigset, SIGWINCH) == -1)
1959 return got_error_from_errno("sigaddset");
1960 if (sigaddset(&sigset, SIGCONT) == -1)
1961 return got_error_from_errno("sigaddset");
1963 /* ncurses handles SIGTSTP */
1964 if (sigaddset(&sigset, SIGTSTP) == -1)
1965 return got_error_from_errno("sigaddset");
1967 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1969 return got_error_set_errno(errcode, "pthread_sigmask");
1975 log_thread(void *arg)
1977 const struct got_error *err = NULL;
1979 struct tog_log_thread_args *a = arg;
1982 err = block_signals_used_by_main_thread();
1986 while (!done && !err && !tog_sigpipe_received) {
1987 err = queue_commits(a->graph, a->commits, 1, a->repo,
1988 a->in_repo_path, a->searching, a->search_next_done,
1991 if (err->code != GOT_ERR_ITER_COMPLETED)
1995 } else if (a->commits_needed > 0)
1996 a->commits_needed--;
1998 errcode = pthread_mutex_lock(&tog_mutex);
2000 err = got_error_set_errno(errcode,
2001 "pthread_mutex_lock");
2003 } else if (*a->quit)
2005 else if (*a->first_displayed_entry == NULL) {
2006 *a->first_displayed_entry =
2007 TAILQ_FIRST(&a->commits->head);
2008 *a->selected_entry = *a->first_displayed_entry;
2011 errcode = pthread_cond_signal(&a->commit_loaded);
2013 err = got_error_set_errno(errcode,
2014 "pthread_cond_signal");
2015 pthread_mutex_unlock(&tog_mutex);
2020 a->commits_needed = 0;
2022 if (a->commits_needed == 0) {
2023 errcode = pthread_cond_wait(&a->need_commits,
2026 err = got_error_set_errno(errcode,
2027 "pthread_cond_wait");
2031 errcode = pthread_mutex_unlock(&tog_mutex);
2032 if (errcode && err == NULL)
2033 err = got_error_set_errno(errcode,
2034 "pthread_mutex_unlock");
2036 a->log_complete = 1;
2040 static const struct got_error *
2041 stop_log_thread(struct tog_log_view_state *s)
2043 const struct got_error *err = NULL;
2048 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2050 return got_error_set_errno(errcode,
2051 "pthread_cond_signal");
2052 errcode = pthread_mutex_unlock(&tog_mutex);
2054 return got_error_set_errno(errcode,
2055 "pthread_mutex_unlock");
2056 errcode = pthread_join(s->thread, (void **)&err);
2058 return got_error_set_errno(errcode, "pthread_join");
2059 errcode = pthread_mutex_lock(&tog_mutex);
2061 return got_error_set_errno(errcode,
2062 "pthread_mutex_lock");
2066 if (s->thread_args.repo) {
2067 got_repo_close(s->thread_args.repo);
2068 s->thread_args.repo = NULL;
2071 if (s->thread_args.graph) {
2072 got_commit_graph_close(s->thread_args.graph);
2073 s->thread_args.graph = NULL;
2079 static const struct got_error *
2080 close_log_view(struct tog_view *view)
2082 const struct got_error *err = NULL;
2083 struct tog_log_view_state *s = &view->state.log;
2086 err = stop_log_thread(s);
2088 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2089 if (errcode && err == NULL)
2090 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2092 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2093 if (errcode && err == NULL)
2094 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2096 free_commits(&s->commits);
2097 free(s->in_repo_path);
2098 s->in_repo_path = NULL;
2101 got_ref_list_free(&s->refs);
2105 static const struct got_error *
2106 search_start_log_view(struct tog_view *view)
2108 struct tog_log_view_state *s = &view->state.log;
2110 s->matched_entry = NULL;
2111 s->search_entry = NULL;
2115 static const struct got_error *
2116 search_next_log_view(struct tog_view *view)
2118 const struct got_error *err = NULL;
2119 struct tog_log_view_state *s = &view->state.log;
2120 struct commit_queue_entry *entry;
2122 /* Display progress update in log view. */
2123 show_log_view(view);
2127 if (s->search_entry) {
2129 errcode = pthread_mutex_unlock(&tog_mutex);
2131 return got_error_set_errno(errcode,
2132 "pthread_mutex_unlock");
2133 ch = wgetch(view->window);
2134 errcode = pthread_mutex_lock(&tog_mutex);
2136 return got_error_set_errno(errcode,
2137 "pthread_mutex_lock");
2138 if (ch == KEY_BACKSPACE) {
2139 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2142 if (view->searching == TOG_SEARCH_FORWARD)
2143 entry = TAILQ_NEXT(s->search_entry, entry);
2145 entry = TAILQ_PREV(s->search_entry,
2146 commit_queue_head, entry);
2147 } else if (s->matched_entry) {
2148 if (view->searching == TOG_SEARCH_FORWARD)
2149 entry = TAILQ_NEXT(s->matched_entry, entry);
2151 entry = TAILQ_PREV(s->matched_entry,
2152 commit_queue_head, entry);
2154 if (view->searching == TOG_SEARCH_FORWARD)
2155 entry = TAILQ_FIRST(&s->commits.head);
2157 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2163 if (entry == NULL) {
2164 if (s->thread_args.log_complete ||
2165 view->searching == TOG_SEARCH_BACKWARD) {
2166 view->search_next_done =
2167 (s->matched_entry == NULL ?
2168 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2169 s->search_entry = NULL;
2173 * Poke the log thread for more commits and return,
2174 * allowing the main loop to make progress. Search
2175 * will resume at s->search_entry once we come back.
2177 s->thread_args.commits_needed++;
2178 return trigger_log_thread(view, 0);
2181 err = match_commit(&have_match, entry->id, entry->commit,
2186 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2187 s->matched_entry = entry;
2191 s->search_entry = entry;
2192 if (view->searching == TOG_SEARCH_FORWARD)
2193 entry = TAILQ_NEXT(entry, entry);
2195 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2198 if (s->matched_entry) {
2199 int cur = s->selected_entry->idx;
2200 while (cur < s->matched_entry->idx) {
2201 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2206 while (cur > s->matched_entry->idx) {
2207 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2214 s->search_entry = NULL;
2219 static const struct got_error *
2220 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2221 struct got_repository *repo, const char *head_ref_name,
2222 const char *in_repo_path, int log_branches)
2224 const struct got_error *err = NULL;
2225 struct tog_log_view_state *s = &view->state.log;
2226 struct got_repository *thread_repo = NULL;
2227 struct got_commit_graph *thread_graph = NULL;
2230 SIMPLEQ_INIT(&s->refs);
2232 if (in_repo_path != s->in_repo_path) {
2233 free(s->in_repo_path);
2234 s->in_repo_path = strdup(in_repo_path);
2235 if (s->in_repo_path == NULL)
2236 return got_error_from_errno("strdup");
2239 /* The commit queue only contains commits being displayed. */
2240 TAILQ_INIT(&s->commits.head);
2241 s->commits.ncommits = 0;
2243 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2248 s->head_ref_name = head_ref_name;
2249 s->start_id = got_object_id_dup(start_id);
2250 if (s->start_id == NULL) {
2251 err = got_error_from_errno("got_object_id_dup");
2254 s->log_branches = log_branches;
2256 SIMPLEQ_INIT(&s->colors);
2257 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2258 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2259 get_color_value("TOG_COLOR_COMMIT"));
2262 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2263 get_color_value("TOG_COLOR_AUTHOR"));
2265 free_colors(&s->colors);
2268 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2269 get_color_value("TOG_COLOR_DATE"));
2271 free_colors(&s->colors);
2276 view->show = show_log_view;
2277 view->input = input_log_view;
2278 view->close = close_log_view;
2279 view->search_start = search_start_log_view;
2280 view->search_next = search_next_log_view;
2282 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2285 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2289 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2290 s->repo, NULL, NULL);
2294 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2296 err = got_error_set_errno(errcode, "pthread_cond_init");
2299 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2301 err = got_error_set_errno(errcode, "pthread_cond_init");
2305 s->thread_args.commits_needed = view->nlines;
2306 s->thread_args.graph = thread_graph;
2307 s->thread_args.commits = &s->commits;
2308 s->thread_args.in_repo_path = s->in_repo_path;
2309 s->thread_args.start_id = s->start_id;
2310 s->thread_args.repo = thread_repo;
2311 s->thread_args.log_complete = 0;
2312 s->thread_args.quit = &s->quit;
2313 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2314 s->thread_args.selected_entry = &s->selected_entry;
2315 s->thread_args.searching = &view->searching;
2316 s->thread_args.search_next_done = &view->search_next_done;
2317 s->thread_args.regex = &view->regex;
2320 close_log_view(view);
2324 static const struct got_error *
2325 show_log_view(struct tog_view *view)
2327 const struct got_error *err;
2328 struct tog_log_view_state *s = &view->state.log;
2330 if (s->thread == NULL) {
2331 int errcode = pthread_create(&s->thread, NULL, log_thread,
2334 return got_error_set_errno(errcode, "pthread_create");
2335 if (s->thread_args.commits_needed > 0) {
2336 err = trigger_log_thread(view, 1);
2342 return draw_commits(view, &s->last_displayed_entry,
2343 &s->selected_entry, s->first_displayed_entry,
2344 &s->commits, s->selected, view->nlines, &s->refs,
2345 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2348 static const struct got_error *
2349 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2350 struct tog_view **focus_view, struct tog_view *view, int ch)
2352 const struct got_error *err = NULL;
2353 struct tog_log_view_state *s = &view->state.log;
2354 char *parent_path, *in_repo_path = NULL;
2355 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2356 struct tog_view *ref_view = NULL;
2358 struct got_object_id *start_id;
2368 if (s->first_displayed_entry == NULL)
2370 if (s->selected > 0)
2373 log_scroll_up(view, 1);
2377 if (s->first_displayed_entry == NULL)
2379 if (TAILQ_FIRST(&s->commits.head) ==
2380 s->first_displayed_entry) {
2384 log_scroll_up(view, view->nlines - 1);
2390 if (s->first_displayed_entry == NULL)
2392 if (s->selected < MIN(view->nlines - 2,
2393 s->commits.ncommits - 1)) {
2397 err = log_scroll_down(view, 1);
2401 struct commit_queue_entry *first;
2402 first = s->first_displayed_entry;
2405 err = log_scroll_down(view, view->nlines - 1);
2408 if (first == s->first_displayed_entry &&
2409 s->selected < MIN(view->nlines - 2,
2410 s->commits.ncommits - 1)) {
2411 /* can't scroll further down */
2412 s->selected = MIN(view->nlines - 2,
2413 s->commits.ncommits - 1);
2419 if (s->selected > view->nlines - 2)
2420 s->selected = view->nlines - 2;
2421 if (s->selected > s->commits.ncommits - 1)
2422 s->selected = s->commits.ncommits - 1;
2427 if (s->selected_entry == NULL)
2429 if (view_is_parent_view(view))
2430 begin_x = view_split_begin_x(view->begin_x);
2431 err = open_diff_view_for_commit(&diff_view, begin_x,
2432 s->selected_entry->commit, s->selected_entry->id,
2436 if (view_is_parent_view(view)) {
2437 err = view_close_child(view);
2440 err = view_set_child(view, diff_view);
2442 view_close(diff_view);
2445 *focus_view = diff_view;
2446 view->child_focussed = 1;
2448 *new_view = diff_view;
2451 if (s->selected_entry == NULL)
2453 if (view_is_parent_view(view))
2454 begin_x = view_split_begin_x(view->begin_x);
2455 err = browse_commit_tree(&tree_view, begin_x,
2456 s->selected_entry, s->in_repo_path, s->repo);
2459 if (view_is_parent_view(view)) {
2460 err = view_close_child(view);
2463 err = view_set_child(view, tree_view);
2465 view_close(tree_view);
2468 *focus_view = tree_view;
2469 view->child_focussed = 1;
2471 *new_view = tree_view;
2474 if (got_path_cmp(s->in_repo_path, "/",
2475 strlen(s->in_repo_path), 1) == 0)
2477 err = got_path_dirname(&parent_path, s->in_repo_path);
2480 err = stop_log_thread(s);
2485 lv = view_open(view->nlines, view->ncols,
2486 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2489 return got_error_from_errno("view_open");
2491 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2492 parent_path, s->log_branches);
2496 if (view_is_parent_view(view))
2499 view_set_child(view->parent, lv);
2504 err = stop_log_thread(s);
2507 lv = view_open(view->nlines, view->ncols,
2508 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2510 return got_error_from_errno("view_open");
2511 err = got_repo_match_object_id(&start_id, NULL,
2512 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2513 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2518 in_repo_path = strdup(s->in_repo_path);
2519 if (in_repo_path == NULL) {
2522 return got_error_from_errno("strdup");
2524 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2525 in_repo_path, s->log_branches);
2535 s->log_branches = !s->log_branches;
2536 err = stop_log_thread(s);
2539 lv = view_open(view->nlines, view->ncols,
2540 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2542 return got_error_from_errno("view_open");
2543 err = open_log_view(lv, s->start_id, s->repo,
2544 s->head_ref_name, s->in_repo_path, s->log_branches);
2553 if (view_is_parent_view(view))
2554 begin_x = view_split_begin_x(view->begin_x);
2555 ref_view = view_open(view->nlines, view->ncols,
2556 view->begin_y, begin_x, TOG_VIEW_REF);
2557 if (ref_view == NULL)
2558 return got_error_from_errno("view_open");
2559 err = open_ref_view(ref_view, s->repo);
2561 view_close(ref_view);
2564 if (view_is_parent_view(view)) {
2565 err = view_close_child(view);
2568 err = view_set_child(view, ref_view);
2570 view_close(ref_view);
2573 *focus_view = ref_view;
2574 view->child_focussed = 1;
2576 *new_view = ref_view;
2585 static const struct got_error *
2586 apply_unveil(const char *repo_path, const char *worktree_path)
2588 const struct got_error *error;
2591 if (unveil("gmon.out", "rwc") != 0)
2592 return got_error_from_errno2("unveil", "gmon.out");
2594 if (repo_path && unveil(repo_path, "r") != 0)
2595 return got_error_from_errno2("unveil", repo_path);
2597 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2598 return got_error_from_errno2("unveil", worktree_path);
2600 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2601 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2603 error = got_privsep_unveil_exec_helpers();
2607 if (unveil(NULL, NULL) != 0)
2608 return got_error_from_errno("unveil");
2618 halfdelay(1); /* Do fast refresh while initial view is loading. */
2621 intrflush(stdscr, FALSE);
2622 keypad(stdscr, TRUE);
2624 if (getenv("TOG_COLORS") != NULL) {
2626 use_default_colors();
2628 signal(SIGWINCH, tog_sigwinch);
2629 signal(SIGPIPE, tog_sigpipe);
2630 signal(SIGCONT, tog_sigcont);
2633 static const struct got_error *
2634 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2635 struct got_repository *repo, struct got_worktree *worktree)
2637 const struct got_error *err = NULL;
2640 *in_repo_path = strdup("/");
2641 if (*in_repo_path == NULL)
2642 return got_error_from_errno("strdup");
2647 const char *prefix = got_worktree_get_path_prefix(worktree);
2650 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2653 if (asprintf(in_repo_path, "%s%s%s", prefix,
2654 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2656 err = got_error_from_errno("asprintf");
2657 *in_repo_path = NULL;
2661 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2666 static const struct got_error *
2667 cmd_log(int argc, char *argv[])
2669 const struct got_error *error;
2670 struct got_repository *repo = NULL;
2671 struct got_worktree *worktree = NULL;
2672 struct got_object_id *start_id = NULL;
2673 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2674 char *start_commit = NULL, *head_ref_name = NULL;
2675 int ch, log_branches = 0;
2676 struct tog_view *view;
2679 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2684 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2690 start_commit = optarg;
2693 repo_path = realpath(optarg, NULL);
2694 if (repo_path == NULL)
2695 return got_error_from_errno2("realpath",
2710 cwd = getcwd(NULL, 0);
2712 return got_error_from_errno("getcwd");
2714 error = got_worktree_open(&worktree, cwd);
2715 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2718 if (repo_path == NULL) {
2721 strdup(got_worktree_get_repo_path(worktree));
2723 repo_path = strdup(cwd);
2725 if (repo_path == NULL) {
2726 error = got_error_from_errno("strdup");
2730 error = got_repo_open(&repo, repo_path, NULL);
2734 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2741 error = apply_unveil(got_repo_get_path(repo),
2742 worktree ? got_worktree_get_root_path(worktree) : NULL);
2746 if (start_commit == NULL)
2747 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2748 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2749 GOT_OBJ_TYPE_COMMIT, 1, repo);
2751 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2752 GOT_OBJ_TYPE_COMMIT, 1, repo);
2756 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2758 error = got_error_from_errno("view_open");
2762 head_ref_name = strdup(
2763 got_worktree_get_head_ref_name(worktree));
2764 if (head_ref_name == NULL) {
2765 error = got_error_from_errno("strdup");
2769 error = open_log_view(view, start_id, repo, head_ref_name,
2770 in_repo_path, log_branches);
2774 /* Release work tree lock. */
2775 got_worktree_close(worktree);
2778 error = view_loop(view);
2784 free(head_ref_name);
2786 got_repo_close(repo);
2788 got_worktree_close(worktree);
2796 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2797 "[-w] object1 object2\n", getprogname());
2802 parse_next_line(FILE *f, size_t *len)
2807 const char delim[3] = { '\0', '\0', '\0'};
2809 line = fparseln(f, &linelen, &lineno, delim, 0);
2816 match_line(const char *line, regex_t *regex, size_t nmatch,
2817 regmatch_t *regmatch)
2819 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2823 match_color(struct tog_colors *colors, const char *line)
2825 struct tog_color *tc = NULL;
2827 SIMPLEQ_FOREACH(tc, colors, entry) {
2828 if (match_line(line, &tc->regex, 0, NULL))
2835 static const struct got_error *
2836 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2837 WINDOW *window, regmatch_t *regmatch)
2839 const struct got_error *err = NULL;
2846 s = strndup(line, regmatch->rm_so);
2848 return got_error_from_errno("strndup");
2850 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2855 waddwstr(window, wline);
2862 s = strndup(line + regmatch->rm_so,
2863 regmatch->rm_eo - regmatch->rm_so);
2865 err = got_error_from_errno("strndup");
2869 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2874 wattr_on(window, A_STANDOUT, NULL);
2875 waddwstr(window, wline);
2876 wattr_off(window, A_STANDOUT, NULL);
2883 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2884 err = format_line(&wline, &width,
2885 line + regmatch->rm_eo, wlimit, col_tab_align);
2888 waddwstr(window, wline);
2896 static const struct got_error *
2897 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2898 off_t *line_offsets, int selected_line, int max_lines,
2899 int *last_displayed_line, int *eof, const char *header,
2900 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2902 const struct got_error *err;
2905 struct tog_color *tc;
2911 line_offset = line_offsets[first_displayed_line - 1];
2912 if (fseeko(f, line_offset, SEEK_SET) == -1)
2913 return got_error_from_errno("fseek");
2915 werase(view->window);
2918 if (asprintf(&line, "[%d/%d] %s",
2919 first_displayed_line - 1 + selected_line, nlines,
2921 return got_error_from_errno("asprintf");
2922 err = format_line(&wline, &width, line, view->ncols, 0);
2927 if (view_needs_focus_indication(view))
2928 wstandout(view->window);
2929 waddwstr(view->window, wline);
2932 if (view_needs_focus_indication(view))
2933 wstandend(view->window);
2934 if (width <= view->ncols - 1)
2935 waddch(view->window, '\n');
2943 while (max_lines > 0 && nprinted < max_lines) {
2944 line = parse_next_line(f, &len);
2950 tc = match_color(colors, line);
2952 wattr_on(view->window,
2953 COLOR_PAIR(tc->colorpair), NULL);
2954 if (first_displayed_line + nprinted == matched_line &&
2955 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2956 err = add_matched_line(&width, line, view->ncols, 0,
2957 view->window, regmatch);
2963 err = format_line(&wline, &width, line, view->ncols, 0);
2968 waddwstr(view->window, wline);
2973 wattr_off(view->window,
2974 COLOR_PAIR(tc->colorpair), NULL);
2975 if (width <= view->ncols - 1)
2976 waddch(view->window, '\n');
2981 *last_displayed_line = first_displayed_line + (nprinted - 1);
2983 *last_displayed_line = first_displayed_line;
2988 while (nprinted < view->nlines) {
2989 waddch(view->window, '\n');
2993 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2998 wstandout(view->window);
2999 waddwstr(view->window, wline);
3002 wstandend(view->window);
3009 get_datestr(time_t *time, char *datebuf)
3011 struct tm mytm, *tm;
3014 tm = gmtime_r(time, &mytm);
3017 s = asctime_r(tm, datebuf);
3020 p = strchr(s, '\n');
3026 static const struct got_error *
3027 get_changed_paths(struct got_pathlist_head *paths,
3028 struct got_commit_object *commit, struct got_repository *repo)
3030 const struct got_error *err = NULL;
3031 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3032 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3033 struct got_object_qid *qid;
3035 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3037 struct got_commit_object *pcommit;
3038 err = got_object_open_as_commit(&pcommit, repo,
3043 tree_id1 = got_object_commit_get_tree_id(pcommit);
3044 got_object_commit_close(pcommit);
3049 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3054 tree_id2 = got_object_commit_get_tree_id(commit);
3055 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3059 err = got_diff_tree(tree1, tree2, "", "", repo,
3060 got_diff_tree_collect_changed_paths, paths, 0);
3063 got_object_tree_close(tree1);
3065 got_object_tree_close(tree2);
3069 static const struct got_error *
3070 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3074 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3076 return got_error_from_errno("reallocarray");
3078 (*line_offsets)[*nlines] = off;
3083 static const struct got_error *
3084 write_commit_info(off_t **line_offsets, size_t *nlines,
3085 struct got_object_id *commit_id, struct got_reflist_head *refs,
3086 struct got_repository *repo, FILE *outfile)
3088 const struct got_error *err = NULL;
3089 char datebuf[26], *datestr;
3090 struct got_commit_object *commit;
3091 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3092 time_t committer_time;
3093 const char *author, *committer;
3094 char *refs_str = NULL;
3095 struct got_pathlist_head changed_paths;
3096 struct got_pathlist_entry *pe;
3100 TAILQ_INIT(&changed_paths);
3103 err = build_refs_str(&refs_str, refs, commit_id, repo);
3108 err = got_object_open_as_commit(&commit, repo, commit_id);
3112 err = got_object_id_str(&id_str, commit_id);
3114 err = got_error_from_errno("got_object_id_str");
3118 err = add_line_offset(line_offsets, nlines, 0);
3122 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3123 refs_str ? refs_str : "", refs_str ? ")" : "");
3125 err = got_error_from_errno("fprintf");
3129 err = add_line_offset(line_offsets, nlines, outoff);
3133 n = fprintf(outfile, "from: %s\n",
3134 got_object_commit_get_author(commit));
3136 err = got_error_from_errno("fprintf");
3140 err = add_line_offset(line_offsets, nlines, outoff);
3144 committer_time = got_object_commit_get_committer_time(commit);
3145 datestr = get_datestr(&committer_time, datebuf);
3147 n = fprintf(outfile, "date: %s UTC\n", datestr);
3149 err = got_error_from_errno("fprintf");
3153 err = add_line_offset(line_offsets, nlines, outoff);
3157 author = got_object_commit_get_author(commit);
3158 committer = got_object_commit_get_committer(commit);
3159 if (strcmp(author, committer) != 0) {
3160 n = fprintf(outfile, "via: %s\n", committer);
3162 err = got_error_from_errno("fprintf");
3166 err = add_line_offset(line_offsets, nlines, outoff);
3170 err = got_object_commit_get_logmsg(&logmsg, commit);
3174 while ((line = strsep(&s, "\n")) != NULL) {
3175 n = fprintf(outfile, "%s\n", line);
3177 err = got_error_from_errno("fprintf");
3181 err = add_line_offset(line_offsets, nlines, outoff);
3186 err = get_changed_paths(&changed_paths, commit, repo);
3189 TAILQ_FOREACH(pe, &changed_paths, entry) {
3190 struct got_diff_changed_path *cp = pe->data;
3191 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3193 err = got_error_from_errno("fprintf");
3197 err = add_line_offset(line_offsets, nlines, outoff);
3200 free((char *)pe->path);
3204 fputc('\n', outfile);
3206 err = add_line_offset(line_offsets, nlines, outoff);
3208 got_pathlist_free(&changed_paths);
3212 got_object_commit_close(commit);
3214 free(*line_offsets);
3215 *line_offsets = NULL;
3221 static const struct got_error *
3222 create_diff(struct tog_diff_view_state *s)
3224 const struct got_error *err = NULL;
3228 free(s->line_offsets);
3229 s->line_offsets = malloc(sizeof(off_t));
3230 if (s->line_offsets == NULL)
3231 return got_error_from_errno("malloc");
3236 err = got_error_from_errno("got_opentemp");
3239 if (s->f && fclose(s->f) != 0) {
3240 err = got_error_from_errno("fclose");
3246 err = got_object_get_type(&obj_type, s->repo, s->id1);
3248 err = got_object_get_type(&obj_type, s->repo, s->id2);
3253 case GOT_OBJ_TYPE_BLOB:
3254 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3255 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3256 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3258 case GOT_OBJ_TYPE_TREE:
3259 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3260 s->id1, s->id2, "", "", s->diff_context,
3261 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3263 case GOT_OBJ_TYPE_COMMIT: {
3264 const struct got_object_id_queue *parent_ids;
3265 struct got_object_qid *pid;
3266 struct got_commit_object *commit2;
3268 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3271 /* Show commit info if we're diffing to a parent/root commit. */
3272 if (s->id1 == NULL) {
3273 err = write_commit_info(&s->line_offsets, &s->nlines,
3274 s->id2, &s->refs, s->repo, s->f);
3278 parent_ids = got_object_commit_get_parent_ids(commit2);
3279 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3280 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3281 err = write_commit_info(
3282 &s->line_offsets, &s->nlines,
3283 s->id2, &s->refs, s->repo, s->f);
3290 got_object_commit_close(commit2);
3292 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3293 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3294 s->force_text_diff, s->repo, s->f);
3298 err = got_error(GOT_ERR_OBJ_TYPE);
3304 if (s->f && fflush(s->f) != 0 && err == NULL)
3305 err = got_error_from_errno("fflush");
3310 diff_view_indicate_progress(struct tog_view *view)
3312 mvwaddstr(view->window, 0, 0, "diffing...");
3317 static const struct got_error *
3318 search_start_diff_view(struct tog_view *view)
3320 struct tog_diff_view_state *s = &view->state.diff;
3322 s->matched_line = 0;
3326 static const struct got_error *
3327 search_next_diff_view(struct tog_view *view)
3329 struct tog_diff_view_state *s = &view->state.diff;
3332 if (!view->searching) {
3333 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3337 if (s->matched_line) {
3338 if (view->searching == TOG_SEARCH_FORWARD)
3339 lineno = s->matched_line + 1;
3341 lineno = s->matched_line - 1;
3343 if (view->searching == TOG_SEARCH_FORWARD)
3354 if (lineno <= 0 || lineno > s->nlines) {
3355 if (s->matched_line == 0) {
3356 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3361 if (view->searching == TOG_SEARCH_FORWARD)
3367 offset = s->line_offsets[lineno - 1];
3368 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3370 return got_error_from_errno("fseeko");
3373 line = parse_next_line(s->f, &len);
3375 match_line(line, &view->regex, 1, &view->regmatch)) {
3376 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3377 s->matched_line = lineno;
3382 if (view->searching == TOG_SEARCH_FORWARD)
3388 if (s->matched_line) {
3389 s->first_displayed_line = s->matched_line;
3390 s->selected_line = 1;
3396 static const struct got_error *
3397 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3398 struct got_object_id *id2, const char *label1, const char *label2,
3399 int diff_context, int ignore_whitespace, int force_text_diff,
3400 struct tog_view *log_view, struct got_repository *repo)
3402 const struct got_error *err;
3403 struct tog_diff_view_state *s = &view->state.diff;
3405 SIMPLEQ_INIT(&s->refs);
3407 if (id1 != NULL && id2 != NULL) {
3409 err = got_object_get_type(&type1, repo, id1);
3412 err = got_object_get_type(&type2, repo, id2);
3417 return got_error(GOT_ERR_OBJ_TYPE);
3419 s->first_displayed_line = 1;
3420 s->last_displayed_line = view->nlines;
3421 s->selected_line = 1;
3429 s->id1 = got_object_id_dup(id1);
3431 return got_error_from_errno("got_object_id_dup");
3435 s->id2 = got_object_id_dup(id2);
3436 if (s->id2 == NULL) {
3439 return got_error_from_errno("got_object_id_dup");
3442 s->first_displayed_line = 1;
3443 s->last_displayed_line = view->nlines;
3444 s->diff_context = diff_context;
3445 s->ignore_whitespace = ignore_whitespace;
3446 s->force_text_diff = force_text_diff;
3447 s->log_view = log_view;
3450 SIMPLEQ_INIT(&s->colors);
3451 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3452 err = add_color(&s->colors,
3453 "^-", TOG_COLOR_DIFF_MINUS,
3454 get_color_value("TOG_COLOR_DIFF_MINUS"));
3457 err = add_color(&s->colors, "^\\+",
3458 TOG_COLOR_DIFF_PLUS,
3459 get_color_value("TOG_COLOR_DIFF_PLUS"));
3461 free_colors(&s->colors);
3464 err = add_color(&s->colors,
3465 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3466 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3468 free_colors(&s->colors);
3472 err = add_color(&s->colors,
3473 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3474 TOG_COLOR_DIFF_META,
3475 get_color_value("TOG_COLOR_DIFF_META"));
3477 free_colors(&s->colors);
3481 err = add_color(&s->colors,
3482 "^(from|via): ", TOG_COLOR_AUTHOR,
3483 get_color_value("TOG_COLOR_AUTHOR"));
3485 free_colors(&s->colors);
3489 err = add_color(&s->colors,
3490 "^date: ", TOG_COLOR_DATE,
3491 get_color_value("TOG_COLOR_DATE"));
3493 free_colors(&s->colors);
3498 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3504 free_colors(&s->colors);
3508 if (log_view && view_is_splitscreen(view))
3509 show_log_view(log_view); /* draw vborder */
3510 diff_view_indicate_progress(view);
3512 s->line_offsets = NULL;
3514 err = create_diff(s);
3520 free_colors(&s->colors);
3521 got_ref_list_free(&s->refs);
3525 view->show = show_diff_view;
3526 view->input = input_diff_view;
3527 view->close = close_diff_view;
3528 view->search_start = search_start_diff_view;
3529 view->search_next = search_next_diff_view;
3534 static const struct got_error *
3535 close_diff_view(struct tog_view *view)
3537 const struct got_error *err = NULL;
3538 struct tog_diff_view_state *s = &view->state.diff;
3544 if (s->f && fclose(s->f) == EOF)
3545 err = got_error_from_errno("fclose");
3546 free_colors(&s->colors);
3547 free(s->line_offsets);
3548 s->line_offsets = NULL;
3550 got_ref_list_free(&s->refs);
3554 static const struct got_error *
3555 show_diff_view(struct tog_view *view)
3557 const struct got_error *err;
3558 struct tog_diff_view_state *s = &view->state.diff;
3559 char *id_str1 = NULL, *id_str2, *header;
3560 const char *label1, *label2;
3563 err = got_object_id_str(&id_str1, s->id1);
3566 label1 = s->label1 ? : id_str1;
3568 label1 = "/dev/null";
3570 err = got_object_id_str(&id_str2, s->id2);
3573 label2 = s->label2 ? : id_str2;
3575 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3576 err = got_error_from_errno("asprintf");
3584 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3585 s->line_offsets, s->selected_line, view->nlines,
3586 &s->last_displayed_line, &s->eof, header, &s->colors,
3587 s->matched_line, &view->regmatch);
3590 static const struct got_error *
3591 set_selected_commit(struct tog_diff_view_state *s,
3592 struct commit_queue_entry *entry)
3594 const struct got_error *err;
3595 const struct got_object_id_queue *parent_ids;
3596 struct got_commit_object *selected_commit;
3597 struct got_object_qid *pid;
3600 s->id2 = got_object_id_dup(entry->id);
3602 return got_error_from_errno("got_object_id_dup");
3604 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3607 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3609 pid = SIMPLEQ_FIRST(parent_ids);
3610 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3611 got_object_commit_close(selected_commit);
3615 static const struct got_error *
3616 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3617 struct tog_view **focus_view, struct tog_view *view, int ch)
3619 const struct got_error *err = NULL;
3620 struct tog_diff_view_state *s = &view->state.diff;
3621 struct tog_log_view_state *ls;
3622 struct commit_queue_entry *entry;
3629 s->force_text_diff = !s->force_text_diff;
3631 s->ignore_whitespace = !s->ignore_whitespace;
3632 wclear(view->window);
3633 s->first_displayed_line = 1;
3634 s->last_displayed_line = view->nlines;
3635 diff_view_indicate_progress(view);
3636 err = create_diff(s);
3640 if (s->first_displayed_line > 1)
3641 s->first_displayed_line--;
3645 if (s->first_displayed_line == 1)
3648 while (i++ < view->nlines - 1 &&
3649 s->first_displayed_line > 1)
3650 s->first_displayed_line--;
3655 s->first_displayed_line++;
3663 while (!s->eof && i++ < view->nlines - 1) {
3665 line = parse_next_line(s->f, NULL);
3666 s->first_displayed_line++;
3672 if (s->diff_context > 0) {
3674 diff_view_indicate_progress(view);
3675 err = create_diff(s);
3676 if (s->first_displayed_line + view->nlines - 1 >
3678 s->first_displayed_line = 1;
3679 s->last_displayed_line = view->nlines;
3684 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3686 diff_view_indicate_progress(view);
3687 err = create_diff(s);
3692 if (s->log_view == NULL)
3694 ls = &s->log_view->state.log;
3695 entry = TAILQ_PREV(ls->selected_entry,
3696 commit_queue_head, entry);
3700 err = input_log_view(NULL, NULL, NULL, s->log_view,
3705 err = set_selected_commit(s, entry);
3709 s->first_displayed_line = 1;
3710 s->last_displayed_line = view->nlines;
3712 diff_view_indicate_progress(view);
3713 err = create_diff(s);
3717 if (s->log_view == NULL)
3719 ls = &s->log_view->state.log;
3721 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3722 ls->thread_args.commits_needed++;
3723 err = trigger_log_thread(s->log_view, 1);
3727 err = input_log_view(NULL, NULL, NULL, s->log_view,
3732 entry = TAILQ_NEXT(ls->selected_entry, entry);
3736 err = set_selected_commit(s, entry);
3740 s->first_displayed_line = 1;
3741 s->last_displayed_line = view->nlines;
3743 diff_view_indicate_progress(view);
3744 err = create_diff(s);
3753 static const struct got_error *
3754 cmd_diff(int argc, char *argv[])
3756 const struct got_error *error = NULL;
3757 struct got_repository *repo = NULL;
3758 struct got_worktree *worktree = NULL;
3759 struct got_object_id *id1 = NULL, *id2 = NULL;
3760 char *repo_path = NULL, *cwd = NULL;
3761 char *id_str1 = NULL, *id_str2 = NULL;
3762 char *label1 = NULL, *label2 = NULL;
3763 int diff_context = 3, ignore_whitespace = 0;
3764 int ch, force_text_diff = 0;
3766 struct tog_view *view;
3769 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3773 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3776 force_text_diff = 1;
3779 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3782 err(1, "-C option %s", errstr);
3785 repo_path = realpath(optarg, NULL);
3786 if (repo_path == NULL)
3787 return got_error_from_errno2("realpath",
3789 got_path_strip_trailing_slashes(repo_path);
3792 ignore_whitespace = 1;
3804 usage_diff(); /* TODO show local worktree changes */
3805 } else if (argc == 2) {
3811 cwd = getcwd(NULL, 0);
3813 return got_error_from_errno("getcwd");
3815 error = got_worktree_open(&worktree, cwd);
3816 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3819 if (repo_path == NULL) {
3822 strdup(got_worktree_get_repo_path(worktree));
3824 repo_path = strdup(cwd);
3826 if (repo_path == NULL) {
3827 error = got_error_from_errno("strdup");
3831 error = got_repo_open(&repo, repo_path, NULL);
3837 error = apply_unveil(got_repo_get_path(repo), NULL);
3841 error = got_repo_match_object_id(&id1, &label1, id_str1,
3842 GOT_OBJ_TYPE_ANY, 1, repo);
3846 error = got_repo_match_object_id(&id2, &label2, id_str2,
3847 GOT_OBJ_TYPE_ANY, 1, repo);
3851 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3853 error = got_error_from_errno("view_open");
3856 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3857 ignore_whitespace, force_text_diff, NULL, repo);
3860 error = view_loop(view);
3867 got_repo_close(repo);
3869 got_worktree_close(worktree);
3877 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3882 struct tog_blame_line {
3884 struct got_object_id *id;
3887 static const struct got_error *
3888 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3889 const char *path, struct tog_blame_line *lines, int nlines,
3890 int blame_complete, int selected_line, int *first_displayed_line,
3891 int *last_displayed_line, int *eof, int max_lines,
3892 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3894 const struct got_error *err;
3895 int lineno = 0, nprinted = 0;
3900 struct tog_blame_line *blame_line;
3901 struct got_object_id *prev_id = NULL;
3903 struct tog_color *tc;
3905 err = got_object_id_str(&id_str, id);
3910 werase(view->window);
3912 if (asprintf(&line, "commit %s", id_str) == -1) {
3913 err = got_error_from_errno("asprintf");
3918 err = format_line(&wline, &width, line, view->ncols, 0);
3923 if (view_needs_focus_indication(view))
3924 wstandout(view->window);
3925 tc = get_color(colors, TOG_COLOR_COMMIT);
3927 wattr_on(view->window,
3928 COLOR_PAIR(tc->colorpair), NULL);
3929 waddwstr(view->window, wline);
3931 wattr_off(view->window,
3932 COLOR_PAIR(tc->colorpair), NULL);
3933 if (view_needs_focus_indication(view))
3934 wstandend(view->window);
3937 if (width < view->ncols - 1)
3938 waddch(view->window, '\n');
3940 if (asprintf(&line, "[%d/%d] %s%s",
3941 *first_displayed_line - 1 + selected_line, nlines,
3942 blame_complete ? "" : "annotating... ", path) == -1) {
3944 return got_error_from_errno("asprintf");
3947 err = format_line(&wline, &width, line, view->ncols, 0);
3952 waddwstr(view->window, wline);
3955 if (width < view->ncols - 1)
3956 waddch(view->window, '\n');
3959 while (nprinted < max_lines - 2) {
3960 line = parse_next_line(f, &len);
3965 if (++lineno < *first_displayed_line) {
3970 if (view->focussed && nprinted == selected_line - 1)
3971 wstandout(view->window);
3974 blame_line = &lines[lineno - 1];
3975 if (blame_line->annotated && prev_id &&
3976 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3978 nprinted == selected_line - 1)) {
3979 waddstr(view->window, " ");
3980 } else if (blame_line->annotated) {
3982 err = got_object_id_str(&id_str, blame_line->id);
3987 tc = get_color(colors, TOG_COLOR_COMMIT);
3989 wattr_on(view->window,
3990 COLOR_PAIR(tc->colorpair), NULL);
3991 wprintw(view->window, "%.8s", id_str);
3993 wattr_off(view->window,
3994 COLOR_PAIR(tc->colorpair), NULL);
3996 prev_id = blame_line->id;
3998 waddstr(view->window, "........");
4002 waddstr(view->window, "........");
4006 if (view->focussed && nprinted == selected_line - 1)
4007 wstandend(view->window);
4008 waddstr(view->window, " ");
4010 if (view->ncols <= 9) {
4012 wline = wcsdup(L"");
4013 if (wline == NULL) {
4014 err = got_error_from_errno("wcsdup");
4018 } else if (*first_displayed_line + nprinted == matched_line &&
4019 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4020 err = add_matched_line(&width, line, view->ncols - 9, 9,
4021 view->window, regmatch);
4028 err = format_line(&wline, &width, line,
4029 view->ncols - 9, 9);
4030 waddwstr(view->window, wline);
4036 if (width <= view->ncols - 1)
4037 waddch(view->window, '\n');
4038 if (++nprinted == 1)
4039 *first_displayed_line = lineno;
4042 *last_displayed_line = lineno;
4049 static const struct got_error *
4050 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4052 const struct got_error *err = NULL;
4053 struct tog_blame_cb_args *a = arg;
4054 struct tog_blame_line *line;
4057 if (nlines != a->nlines ||
4058 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4059 return got_error(GOT_ERR_RANGE);
4061 errcode = pthread_mutex_lock(&tog_mutex);
4063 return got_error_set_errno(errcode, "pthread_mutex_lock");
4065 if (*a->quit) { /* user has quit the blame view */
4066 err = got_error(GOT_ERR_ITER_COMPLETED);
4071 goto done; /* no change in this commit */
4073 line = &a->lines[lineno - 1];
4074 if (line->annotated)
4077 line->id = got_object_id_dup(id);
4078 if (line->id == NULL) {
4079 err = got_error_from_errno("got_object_id_dup");
4082 line->annotated = 1;
4084 errcode = pthread_mutex_unlock(&tog_mutex);
4086 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4091 blame_thread(void *arg)
4093 const struct got_error *err;
4094 struct tog_blame_thread_args *ta = arg;
4095 struct tog_blame_cb_args *a = ta->cb_args;
4098 err = block_signals_used_by_main_thread();
4102 err = got_blame(ta->path, a->commit_id, ta->repo,
4103 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4104 if (err && err->code == GOT_ERR_CANCELLED)
4107 errcode = pthread_mutex_lock(&tog_mutex);
4109 return (void *)got_error_set_errno(errcode,
4110 "pthread_mutex_lock");
4112 got_repo_close(ta->repo);
4116 errcode = pthread_mutex_unlock(&tog_mutex);
4117 if (errcode && err == NULL)
4118 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4123 static struct got_object_id *
4124 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4125 int first_displayed_line, int selected_line)
4127 struct tog_blame_line *line;
4132 line = &lines[first_displayed_line - 1 + selected_line - 1];
4133 if (!line->annotated)
4139 static const struct got_error *
4140 stop_blame(struct tog_blame *blame)
4142 const struct got_error *err = NULL;
4145 if (blame->thread) {
4147 errcode = pthread_mutex_unlock(&tog_mutex);
4149 return got_error_set_errno(errcode,
4150 "pthread_mutex_unlock");
4151 errcode = pthread_join(blame->thread, (void **)&err);
4153 return got_error_set_errno(errcode, "pthread_join");
4154 errcode = pthread_mutex_lock(&tog_mutex);
4156 return got_error_set_errno(errcode,
4157 "pthread_mutex_lock");
4158 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4160 blame->thread = NULL;
4162 if (blame->thread_args.repo) {
4163 got_repo_close(blame->thread_args.repo);
4164 blame->thread_args.repo = NULL;
4167 if (fclose(blame->f) != 0 && err == NULL)
4168 err = got_error_from_errno("fclose");
4172 for (i = 0; i < blame->nlines; i++)
4173 free(blame->lines[i].id);
4175 blame->lines = NULL;
4177 free(blame->cb_args.commit_id);
4178 blame->cb_args.commit_id = NULL;
4183 static const struct got_error *
4184 cancel_blame_view(void *arg)
4186 const struct got_error *err = NULL;
4190 errcode = pthread_mutex_lock(&tog_mutex);
4192 return got_error_set_errno(errcode,
4193 "pthread_mutex_unlock");
4196 err = got_error(GOT_ERR_CANCELLED);
4198 errcode = pthread_mutex_unlock(&tog_mutex);
4200 return got_error_set_errno(errcode,
4201 "pthread_mutex_lock");
4206 static const struct got_error *
4207 run_blame(struct tog_view *view)
4209 struct tog_blame_view_state *s = &view->state.blame;
4210 struct tog_blame *blame = &s->blame;
4211 const struct got_error *err = NULL;
4212 struct got_blob_object *blob = NULL;
4213 struct got_repository *thread_repo = NULL;
4214 struct got_object_id *obj_id = NULL;
4217 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4222 err = got_object_get_type(&obj_type, s->repo, obj_id);
4226 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4227 err = got_error(GOT_ERR_OBJ_TYPE);
4231 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4234 blame->f = got_opentemp();
4235 if (blame->f == NULL) {
4236 err = got_error_from_errno("got_opentemp");
4239 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4240 &blame->line_offsets, blame->f, blob);
4241 if (err || blame->nlines == 0)
4244 /* Don't include \n at EOF in the blame line count. */
4245 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4248 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4249 if (blame->lines == NULL) {
4250 err = got_error_from_errno("calloc");
4254 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4258 blame->cb_args.view = view;
4259 blame->cb_args.lines = blame->lines;
4260 blame->cb_args.nlines = blame->nlines;
4261 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4262 if (blame->cb_args.commit_id == NULL) {
4263 err = got_error_from_errno("got_object_id_dup");
4266 blame->cb_args.quit = &s->done;
4268 blame->thread_args.path = s->path;
4269 blame->thread_args.repo = thread_repo;
4270 blame->thread_args.cb_args = &blame->cb_args;
4271 blame->thread_args.complete = &s->blame_complete;
4272 blame->thread_args.cancel_cb = cancel_blame_view;
4273 blame->thread_args.cancel_arg = &s->done;
4274 s->blame_complete = 0;
4278 got_object_blob_close(blob);
4285 static const struct got_error *
4286 open_blame_view(struct tog_view *view, char *path,
4287 struct got_object_id *commit_id, struct got_repository *repo)
4289 const struct got_error *err = NULL;
4290 struct tog_blame_view_state *s = &view->state.blame;
4292 SIMPLEQ_INIT(&s->blamed_commits);
4294 s->path = strdup(path);
4295 if (s->path == NULL)
4296 return got_error_from_errno("strdup");
4298 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4304 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4305 s->first_displayed_line = 1;
4306 s->last_displayed_line = view->nlines;
4307 s->selected_line = 1;
4308 s->blame_complete = 0;
4310 s->commit_id = commit_id;
4311 memset(&s->blame, 0, sizeof(s->blame));
4313 SIMPLEQ_INIT(&s->colors);
4314 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4315 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4316 get_color_value("TOG_COLOR_COMMIT"));
4321 view->show = show_blame_view;
4322 view->input = input_blame_view;
4323 view->close = close_blame_view;
4324 view->search_start = search_start_blame_view;
4325 view->search_next = search_next_blame_view;
4327 return run_blame(view);
4330 static const struct got_error *
4331 close_blame_view(struct tog_view *view)
4333 const struct got_error *err = NULL;
4334 struct tog_blame_view_state *s = &view->state.blame;
4336 if (s->blame.thread)
4337 err = stop_blame(&s->blame);
4339 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4340 struct got_object_qid *blamed_commit;
4341 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4342 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4343 got_object_qid_free(blamed_commit);
4347 free_colors(&s->colors);
4352 static const struct got_error *
4353 search_start_blame_view(struct tog_view *view)
4355 struct tog_blame_view_state *s = &view->state.blame;
4357 s->matched_line = 0;
4361 static const struct got_error *
4362 search_next_blame_view(struct tog_view *view)
4364 struct tog_blame_view_state *s = &view->state.blame;
4367 if (!view->searching) {
4368 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4372 if (s->matched_line) {
4373 if (view->searching == TOG_SEARCH_FORWARD)
4374 lineno = s->matched_line + 1;
4376 lineno = s->matched_line - 1;
4378 if (view->searching == TOG_SEARCH_FORWARD)
4381 lineno = s->blame.nlines;
4389 if (lineno <= 0 || lineno > s->blame.nlines) {
4390 if (s->matched_line == 0) {
4391 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4396 if (view->searching == TOG_SEARCH_FORWARD)
4399 lineno = s->blame.nlines;
4402 offset = s->blame.line_offsets[lineno - 1];
4403 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4405 return got_error_from_errno("fseeko");
4408 line = parse_next_line(s->blame.f, &len);
4410 match_line(line, &view->regex, 1, &view->regmatch)) {
4411 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4412 s->matched_line = lineno;
4417 if (view->searching == TOG_SEARCH_FORWARD)
4423 if (s->matched_line) {
4424 s->first_displayed_line = s->matched_line;
4425 s->selected_line = 1;
4431 static const struct got_error *
4432 show_blame_view(struct tog_view *view)
4434 const struct got_error *err = NULL;
4435 struct tog_blame_view_state *s = &view->state.blame;
4438 if (s->blame.thread == NULL) {
4439 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4440 &s->blame.thread_args);
4442 return got_error_set_errno(errcode, "pthread_create");
4444 halfdelay(1); /* fast refresh while annotating */
4447 if (s->blame_complete)
4448 halfdelay(10); /* disable fast refresh */
4450 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4451 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4452 s->selected_line, &s->first_displayed_line,
4453 &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4454 s->matched_line, &view->regmatch);
4460 static const struct got_error *
4461 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4462 struct tog_view **focus_view, struct tog_view *view, int ch)
4464 const struct got_error *err = NULL, *thread_err = NULL;
4465 struct tog_view *diff_view;
4466 struct tog_blame_view_state *s = &view->state.blame;
4475 if (s->selected_line > 1)
4477 else if (s->selected_line == 1 &&
4478 s->first_displayed_line > 1)
4479 s->first_displayed_line--;
4483 if (s->first_displayed_line == 1) {
4484 s->selected_line = 1;
4487 if (s->first_displayed_line > view->nlines - 2)
4488 s->first_displayed_line -=
4491 s->first_displayed_line = 1;
4495 if (s->selected_line < view->nlines - 2 &&
4496 s->first_displayed_line +
4497 s->selected_line <= s->blame.nlines)
4499 else if (s->last_displayed_line <
4501 s->first_displayed_line++;
4505 struct got_object_id *id = NULL;
4506 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4507 s->first_displayed_line, s->selected_line);
4511 struct got_commit_object *commit;
4512 struct got_object_qid *pid;
4513 struct got_object_id *blob_id = NULL;
4515 err = got_object_open_as_commit(&commit,
4519 pid = SIMPLEQ_FIRST(
4520 got_object_commit_get_parent_ids(commit));
4522 got_object_commit_close(commit);
4525 /* Check if path history ends here. */
4526 err = got_object_id_by_path(&blob_id, s->repo,
4529 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4531 got_object_commit_close(commit);
4534 err = got_object_get_type(&obj_type, s->repo,
4537 /* Can't blame non-blob type objects. */
4538 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4539 got_object_commit_close(commit);
4542 err = got_object_qid_alloc(&s->blamed_commit,
4544 got_object_commit_close(commit);
4546 if (got_object_id_cmp(id,
4547 s->blamed_commit->id) == 0)
4549 err = got_object_qid_alloc(&s->blamed_commit,
4555 thread_err = stop_blame(&s->blame);
4559 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4560 s->blamed_commit, entry);
4561 err = run_blame(view);
4567 struct got_object_qid *first;
4568 first = SIMPLEQ_FIRST(&s->blamed_commits);
4569 if (!got_object_id_cmp(first->id, s->commit_id))
4572 thread_err = stop_blame(&s->blame);
4576 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4577 got_object_qid_free(s->blamed_commit);
4579 SIMPLEQ_FIRST(&s->blamed_commits);
4580 err = run_blame(view);
4587 struct got_object_id *id = NULL;
4588 struct got_object_qid *pid;
4589 struct got_commit_object *commit = NULL;
4590 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4591 s->first_displayed_line, s->selected_line);
4594 err = got_object_open_as_commit(&commit, s->repo, id);
4597 pid = SIMPLEQ_FIRST(
4598 got_object_commit_get_parent_ids(commit));
4599 if (view_is_parent_view(view))
4600 begin_x = view_split_begin_x(view->begin_x);
4601 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4602 if (diff_view == NULL) {
4603 got_object_commit_close(commit);
4604 err = got_error_from_errno("view_open");
4607 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4608 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4609 got_object_commit_close(commit);
4611 view_close(diff_view);
4614 if (view_is_parent_view(view)) {
4615 err = view_close_child(view);
4618 err = view_set_child(view, diff_view);
4620 view_close(diff_view);
4623 *focus_view = diff_view;
4624 view->child_focussed = 1;
4626 *new_view = diff_view;
4634 if (s->last_displayed_line >= s->blame.nlines &&
4635 s->selected_line >= MIN(s->blame.nlines,
4636 view->nlines - 2)) {
4639 if (s->last_displayed_line >= s->blame.nlines &&
4640 s->selected_line < view->nlines - 2) {
4641 s->selected_line = MIN(s->blame.nlines,
4645 if (s->last_displayed_line + view->nlines - 2
4647 s->first_displayed_line +=
4650 s->first_displayed_line =
4655 if (s->selected_line > view->nlines - 2) {
4656 s->selected_line = MIN(s->blame.nlines,
4663 return thread_err ? thread_err : err;
4666 static const struct got_error *
4667 cmd_blame(int argc, char *argv[])
4669 const struct got_error *error;
4670 struct got_repository *repo = NULL;
4671 struct got_worktree *worktree = NULL;
4672 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4673 char *link_target = NULL;
4674 struct got_object_id *commit_id = NULL;
4675 char *commit_id_str = NULL;
4677 struct tog_view *view;
4680 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4685 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4688 commit_id_str = optarg;
4691 repo_path = realpath(optarg, NULL);
4692 if (repo_path == NULL)
4693 return got_error_from_errno2("realpath",
4708 cwd = getcwd(NULL, 0);
4710 return got_error_from_errno("getcwd");
4712 error = got_worktree_open(&worktree, cwd);
4713 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4716 if (repo_path == NULL) {
4719 strdup(got_worktree_get_repo_path(worktree));
4721 repo_path = strdup(cwd);
4723 if (repo_path == NULL) {
4724 error = got_error_from_errno("strdup");
4728 error = got_repo_open(&repo, repo_path, NULL);
4732 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4739 error = apply_unveil(got_repo_get_path(repo), NULL);
4743 if (commit_id_str == NULL) {
4744 struct got_reference *head_ref;
4745 error = got_ref_open(&head_ref, repo, worktree ?
4746 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4749 error = got_ref_resolve(&commit_id, repo, head_ref);
4750 got_ref_close(head_ref);
4752 error = got_repo_match_object_id(&commit_id, NULL,
4753 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4758 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4760 error = got_error_from_errno("view_open");
4764 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4769 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4774 /* Release work tree lock. */
4775 got_worktree_close(worktree);
4778 error = view_loop(view);
4786 got_worktree_close(worktree);
4788 got_repo_close(repo);
4792 static const struct got_error *
4793 draw_tree_entries(struct tog_view *view,
4794 struct got_tree_entry **first_displayed_entry,
4795 struct got_tree_entry **last_displayed_entry,
4796 struct got_tree_entry **selected_entry, int *ndisplayed,
4797 const char *label, int show_ids, const char *parent_path,
4798 struct got_tree_object *tree, int selected, int limit,
4799 int isroot, struct tog_colors *colors, struct got_repository *repo)
4801 const struct got_error *err = NULL;
4802 struct got_tree_entry *te;
4804 struct tog_color *tc;
4805 int width, n, i, nentries;
4809 werase(view->window);
4814 err = format_line(&wline, &width, label, view->ncols, 0);
4817 if (view_needs_focus_indication(view))
4818 wstandout(view->window);
4819 tc = get_color(colors, TOG_COLOR_COMMIT);
4821 wattr_on(view->window,
4822 COLOR_PAIR(tc->colorpair), NULL);
4823 waddwstr(view->window, wline);
4825 wattr_off(view->window,
4826 COLOR_PAIR(tc->colorpair), NULL);
4827 if (view_needs_focus_indication(view))
4828 wstandend(view->window);
4831 if (width < view->ncols - 1)
4832 waddch(view->window, '\n');
4835 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4838 waddwstr(view->window, wline);
4841 if (width < view->ncols - 1)
4842 waddch(view->window, '\n');
4845 waddch(view->window, '\n');
4849 if (*first_displayed_entry == NULL) {
4850 te = got_object_tree_get_first_entry(tree);
4851 if (selected == 0) {
4853 wstandout(view->window);
4854 *selected_entry = NULL;
4856 waddstr(view->window, " ..\n"); /* parent directory */
4857 if (selected == 0 && view->focussed)
4858 wstandend(view->window);
4865 te = *first_displayed_entry;
4868 nentries = got_object_tree_get_nentries(tree);
4869 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4870 char *line = NULL, *id_str = NULL, *link_target = NULL;
4871 const char *modestr = "";
4874 te = got_object_tree_get_entry(tree, i);
4875 mode = got_tree_entry_get_mode(te);
4878 err = got_object_id_str(&id_str,
4879 got_tree_entry_get_id(te));
4881 return got_error_from_errno(
4882 "got_object_id_str");
4884 if (got_object_tree_entry_is_submodule(te))
4886 else if (S_ISLNK(mode)) {
4889 err = got_tree_entry_get_symlink_target(&link_target,
4895 for (i = 0; i < strlen(link_target); i++) {
4896 if (!isprint((unsigned char)link_target[i]))
4897 link_target[i] = '?';
4901 else if (S_ISDIR(mode))
4903 else if (mode & S_IXUSR)
4905 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4906 got_tree_entry_get_name(te), modestr,
4907 link_target ? " -> ": "",
4908 link_target ? link_target : "") == -1) {
4911 return got_error_from_errno("asprintf");
4915 err = format_line(&wline, &width, line, view->ncols, 0);
4920 if (n == selected) {
4922 wstandout(view->window);
4923 *selected_entry = te;
4925 tc = match_color(colors, line);
4927 wattr_on(view->window,
4928 COLOR_PAIR(tc->colorpair), NULL);
4929 waddwstr(view->window, wline);
4931 wattr_off(view->window,
4932 COLOR_PAIR(tc->colorpair), NULL);
4933 if (width < view->ncols - 1)
4934 waddch(view->window, '\n');
4935 if (n == selected && view->focussed)
4936 wstandend(view->window);
4942 *last_displayed_entry = te;
4951 tree_scroll_up(struct tog_view *view, int maxscroll)
4953 struct tog_tree_view_state *s = &view->state.tree;
4954 struct got_tree_entry *te;
4955 int isroot = s->tree == s->root;
4958 if (s->first_displayed_entry == NULL)
4961 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4962 while (i++ < maxscroll) {
4965 s->first_displayed_entry = NULL;
4968 s->first_displayed_entry = te;
4969 te = got_tree_entry_get_prev(s->tree, te);
4974 tree_scroll_down(struct tog_view *view, int maxscroll)
4976 struct tog_tree_view_state *s = &view->state.tree;
4977 struct got_tree_entry *next, *last;
4980 if (s->first_displayed_entry)
4981 next = got_tree_entry_get_next(s->tree,
4982 s->first_displayed_entry);
4984 next = got_object_tree_get_first_entry(s->tree);
4986 last = s->last_displayed_entry;
4987 while (next && last && n++ < maxscroll) {
4988 last = got_tree_entry_get_next(s->tree, last);
4990 s->first_displayed_entry = next;
4991 next = got_tree_entry_get_next(s->tree, next);
4996 static const struct got_error *
4997 tree_entry_path(char **path, struct tog_parent_trees *parents,
4998 struct got_tree_entry *te)
5000 const struct got_error *err = NULL;
5001 struct tog_parent_tree *pt;
5002 size_t len = 2; /* for leading slash and NUL */
5004 TAILQ_FOREACH(pt, parents, entry)
5005 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5008 len += strlen(got_tree_entry_get_name(te));
5010 *path = calloc(1, len);
5012 return got_error_from_errno("calloc");
5015 pt = TAILQ_LAST(parents, tog_parent_trees);
5017 const char *name = got_tree_entry_get_name(pt->selected_entry);
5018 if (strlcat(*path, name, len) >= len) {
5019 err = got_error(GOT_ERR_NO_SPACE);
5022 if (strlcat(*path, "/", len) >= len) {
5023 err = got_error(GOT_ERR_NO_SPACE);
5026 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5029 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5030 err = got_error(GOT_ERR_NO_SPACE);
5042 static const struct got_error *
5043 blame_tree_entry(struct tog_view **new_view, int begin_x,
5044 struct got_tree_entry *te, struct tog_parent_trees *parents,
5045 struct got_object_id *commit_id, struct got_repository *repo)
5047 const struct got_error *err = NULL;
5049 struct tog_view *blame_view;
5053 err = tree_entry_path(&path, parents, te);
5057 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5058 if (blame_view == NULL) {
5059 err = got_error_from_errno("view_open");
5063 err = open_blame_view(blame_view, path, commit_id, repo);
5065 if (err->code == GOT_ERR_CANCELLED)
5067 view_close(blame_view);
5069 *new_view = blame_view;
5075 static const struct got_error *
5076 log_tree_entry(struct tog_view **new_view, int begin_x,
5077 struct got_tree_entry *te, struct tog_parent_trees *parents,
5078 struct got_object_id *commit_id, struct got_repository *repo)
5080 struct tog_view *log_view;
5081 const struct got_error *err = NULL;
5086 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5087 if (log_view == NULL)
5088 return got_error_from_errno("view_open");
5090 err = tree_entry_path(&path, parents, te);
5094 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5096 view_close(log_view);
5098 *new_view = log_view;
5103 static const struct got_error *
5104 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5105 struct got_object_id *commit_id, struct got_repository *repo)
5107 const struct got_error *err = NULL;
5108 char *commit_id_str = NULL;
5109 struct tog_tree_view_state *s = &view->state.tree;
5111 TAILQ_INIT(&s->parents);
5113 err = got_object_id_str(&commit_id_str, commit_id);
5117 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5118 err = got_error_from_errno("asprintf");
5122 s->root = s->tree = root;
5123 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5124 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5125 s->commit_id = got_object_id_dup(commit_id);
5126 if (s->commit_id == NULL) {
5127 err = got_error_from_errno("got_object_id_dup");
5132 SIMPLEQ_INIT(&s->colors);
5134 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5135 err = add_color(&s->colors, "\\$$",
5136 TOG_COLOR_TREE_SUBMODULE,
5137 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5140 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5141 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5143 free_colors(&s->colors);
5146 err = add_color(&s->colors, "/$",
5147 TOG_COLOR_TREE_DIRECTORY,
5148 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5150 free_colors(&s->colors);
5154 err = add_color(&s->colors, "\\*$",
5155 TOG_COLOR_TREE_EXECUTABLE,
5156 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5158 free_colors(&s->colors);
5162 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5163 get_color_value("TOG_COLOR_COMMIT"));
5165 free_colors(&s->colors);
5170 view->show = show_tree_view;
5171 view->input = input_tree_view;
5172 view->close = close_tree_view;
5173 view->search_start = search_start_tree_view;
5174 view->search_next = search_next_tree_view;
5176 free(commit_id_str);
5178 free(s->tree_label);
5179 s->tree_label = NULL;
5184 static const struct got_error *
5185 close_tree_view(struct tog_view *view)
5187 struct tog_tree_view_state *s = &view->state.tree;
5189 free_colors(&s->colors);
5190 free(s->tree_label);
5191 s->tree_label = NULL;
5193 s->commit_id = NULL;
5194 while (!TAILQ_EMPTY(&s->parents)) {
5195 struct tog_parent_tree *parent;
5196 parent = TAILQ_FIRST(&s->parents);
5197 TAILQ_REMOVE(&s->parents, parent, entry);
5201 if (s->tree != s->root)
5202 got_object_tree_close(s->tree);
5203 got_object_tree_close(s->root);
5207 static const struct got_error *
5208 search_start_tree_view(struct tog_view *view)
5210 struct tog_tree_view_state *s = &view->state.tree;
5212 s->matched_entry = NULL;
5217 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5219 regmatch_t regmatch;
5221 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5225 static const struct got_error *
5226 search_next_tree_view(struct tog_view *view)
5228 struct tog_tree_view_state *s = &view->state.tree;
5229 struct got_tree_entry *te = NULL;
5231 if (!view->searching) {
5232 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5236 if (s->matched_entry) {
5237 if (view->searching == TOG_SEARCH_FORWARD) {
5238 if (s->selected_entry)
5239 te = got_tree_entry_get_next(s->tree,
5242 te = got_object_tree_get_first_entry(s->tree);
5244 if (s->selected_entry == NULL)
5245 te = got_object_tree_get_last_entry(s->tree);
5247 te = got_tree_entry_get_prev(s->tree,
5251 if (view->searching == TOG_SEARCH_FORWARD)
5252 te = got_object_tree_get_first_entry(s->tree);
5254 te = got_object_tree_get_last_entry(s->tree);
5259 if (s->matched_entry == NULL) {
5260 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5263 if (view->searching == TOG_SEARCH_FORWARD)
5264 te = got_object_tree_get_first_entry(s->tree);
5266 te = got_object_tree_get_last_entry(s->tree);
5269 if (match_tree_entry(te, &view->regex)) {
5270 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5271 s->matched_entry = te;
5275 if (view->searching == TOG_SEARCH_FORWARD)
5276 te = got_tree_entry_get_next(s->tree, te);
5278 te = got_tree_entry_get_prev(s->tree, te);
5281 if (s->matched_entry) {
5282 s->first_displayed_entry = s->matched_entry;
5289 static const struct got_error *
5290 show_tree_view(struct tog_view *view)
5292 const struct got_error *err = NULL;
5293 struct tog_tree_view_state *s = &view->state.tree;
5296 err = tree_entry_path(&parent_path, &s->parents, NULL);
5300 err = draw_tree_entries(view, &s->first_displayed_entry,
5301 &s->last_displayed_entry, &s->selected_entry,
5302 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5303 s->tree, s->selected, view->nlines, s->tree == s->root,
5304 &s->colors, s->repo);
5311 static const struct got_error *
5312 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5313 struct tog_view **focus_view, struct tog_view *view, int ch)
5315 const struct got_error *err = NULL;
5316 struct tog_tree_view_state *s = &view->state.tree;
5317 struct tog_view *log_view, *ref_view;
5322 s->show_ids = !s->show_ids;
5325 if (!s->selected_entry)
5327 if (view_is_parent_view(view))
5328 begin_x = view_split_begin_x(view->begin_x);
5329 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5330 &s->parents, s->commit_id, s->repo);
5331 if (view_is_parent_view(view)) {
5332 err = view_close_child(view);
5335 err = view_set_child(view, log_view);
5337 view_close(log_view);
5340 *focus_view = log_view;
5341 view->child_focussed = 1;
5343 *new_view = log_view;
5346 if (view_is_parent_view(view))
5347 begin_x = view_split_begin_x(view->begin_x);
5348 ref_view = view_open(view->nlines, view->ncols,
5349 view->begin_y, begin_x, TOG_VIEW_REF);
5350 if (ref_view == NULL)
5351 return got_error_from_errno("view_open");
5352 err = open_ref_view(ref_view, s->repo);
5354 view_close(ref_view);
5357 if (view_is_parent_view(view)) {
5358 err = view_close_child(view);
5361 err = view_set_child(view, ref_view);
5363 view_close(ref_view);
5366 *focus_view = ref_view;
5367 view->child_focussed = 1;
5369 *new_view = ref_view;
5373 if (s->selected > 0) {
5377 tree_scroll_up(view, 1);
5381 if (s->tree == s->root) {
5382 if (got_object_tree_get_first_entry(s->tree) ==
5383 s->first_displayed_entry)
5386 if (s->first_displayed_entry == NULL)
5389 tree_scroll_up(view, MAX(0, view->nlines - 3));
5393 if (s->selected < s->ndisplayed - 1) {
5397 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5399 /* can't scroll any further */
5401 tree_scroll_down(view, 1);
5405 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5407 /* can't scroll any further; move cursor down */
5408 if (s->selected < s->ndisplayed - 1)
5409 s->selected = s->ndisplayed - 1;
5412 tree_scroll_down(view, view->nlines - 3);
5417 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5418 struct tog_parent_tree *parent;
5419 /* user selected '..' */
5420 if (s->tree == s->root)
5422 parent = TAILQ_FIRST(&s->parents);
5423 TAILQ_REMOVE(&s->parents, parent,
5425 got_object_tree_close(s->tree);
5426 s->tree = parent->tree;
5427 s->first_displayed_entry =
5428 parent->first_displayed_entry;
5430 parent->selected_entry;
5431 s->selected = parent->selected;
5433 } else if (S_ISDIR(got_tree_entry_get_mode(
5434 s->selected_entry))) {
5435 struct got_tree_object *subtree;
5436 err = got_object_open_as_tree(&subtree, s->repo,
5437 got_tree_entry_get_id(s->selected_entry));
5440 err = tree_view_visit_subtree(subtree, s);
5442 got_object_tree_close(subtree);
5445 } else if (S_ISREG(got_tree_entry_get_mode(
5446 s->selected_entry))) {
5447 struct tog_view *blame_view;
5448 int begin_x = view_is_parent_view(view) ?
5449 view_split_begin_x(view->begin_x) : 0;
5451 err = blame_tree_entry(&blame_view, begin_x,
5452 s->selected_entry, &s->parents,
5453 s->commit_id, s->repo);
5456 if (view_is_parent_view(view)) {
5457 err = view_close_child(view);
5460 err = view_set_child(view, blame_view);
5462 view_close(blame_view);
5465 *focus_view = blame_view;
5466 view->child_focussed = 1;
5468 *new_view = blame_view;
5472 if (s->selected > view->nlines)
5473 s->selected = s->ndisplayed - 1;
5486 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5491 static const struct got_error *
5492 cmd_tree(int argc, char *argv[])
5494 const struct got_error *error;
5495 struct got_repository *repo = NULL;
5496 struct got_worktree *worktree = NULL;
5497 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5498 struct got_object_id *commit_id = NULL;
5499 char *commit_id_arg = NULL;
5500 struct got_commit_object *commit = NULL;
5501 struct got_tree_object *tree = NULL;
5503 struct tog_view *view;
5506 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5511 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5514 commit_id_arg = optarg;
5517 repo_path = realpath(optarg, NULL);
5518 if (repo_path == NULL)
5519 return got_error_from_errno2("realpath",
5534 cwd = getcwd(NULL, 0);
5536 return got_error_from_errno("getcwd");
5538 error = got_worktree_open(&worktree, cwd);
5539 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5542 if (repo_path == NULL) {
5545 strdup(got_worktree_get_repo_path(worktree));
5547 repo_path = strdup(cwd);
5549 if (repo_path == NULL) {
5550 error = got_error_from_errno("strdup");
5554 error = got_repo_open(&repo, repo_path, NULL);
5558 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5565 error = apply_unveil(got_repo_get_path(repo), NULL);
5569 error = got_repo_match_object_id(&commit_id, NULL,
5570 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5571 GOT_OBJ_TYPE_COMMIT, 1, repo);
5575 error = got_object_open_as_commit(&commit, repo, commit_id);
5579 error = got_object_open_as_tree(&tree, repo,
5580 got_object_commit_get_tree_id(commit));
5584 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5586 error = got_error_from_errno("view_open");
5589 error = open_tree_view(view, tree, commit_id, repo);
5592 if (!got_path_is_root_dir(in_repo_path)) {
5593 error = tree_view_walk_path(&view->state.tree, commit_id,
5594 in_repo_path, repo);
5600 /* Release work tree lock. */
5601 got_worktree_close(worktree);
5604 error = view_loop(view);
5610 got_object_commit_close(commit);
5612 got_object_tree_close(tree);
5614 got_repo_close(repo);
5618 static const struct got_error *
5619 ref_view_load_refs(struct tog_ref_view_state *s)
5621 const struct got_error *err;
5622 struct got_reflist_entry *sre;
5623 struct tog_reflist_entry *re;
5625 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5626 got_ref_cmp_by_name, NULL);
5631 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5632 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5635 re = malloc(sizeof(*re));
5637 return got_error_from_errno("malloc");
5640 re->idx = s->nrefs++;
5641 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5648 ref_view_free_refs(struct tog_ref_view_state *s)
5650 struct tog_reflist_entry *re;
5652 while (!TAILQ_EMPTY(&s->refs)) {
5653 re = TAILQ_FIRST(&s->refs);
5654 TAILQ_REMOVE(&s->refs, re, entry);
5657 got_ref_list_free(&s->simplerefs);
5660 static const struct got_error *
5661 open_ref_view(struct tog_view *view, struct got_repository *repo)
5663 const struct got_error *err = NULL;
5664 struct tog_ref_view_state *s = &view->state.ref;
5666 s->selected_entry = 0;
5669 SIMPLEQ_INIT(&s->simplerefs);
5670 TAILQ_INIT(&s->refs);
5671 SIMPLEQ_INIT(&s->colors);
5673 err = ref_view_load_refs(s);
5677 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5679 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5680 err = add_color(&s->colors, "^refs/heads/",
5681 TOG_COLOR_REFS_HEADS,
5682 get_color_value("TOG_COLOR_REFS_HEADS"));
5686 err = add_color(&s->colors, "^refs/tags/",
5687 TOG_COLOR_REFS_TAGS,
5688 get_color_value("TOG_COLOR_REFS_TAGS"));
5692 err = add_color(&s->colors, "^refs/remotes/",
5693 TOG_COLOR_REFS_REMOTES,
5694 get_color_value("TOG_COLOR_REFS_REMOTES"));
5699 view->show = show_ref_view;
5700 view->input = input_ref_view;
5701 view->close = close_ref_view;
5702 view->search_start = search_start_ref_view;
5703 view->search_next = search_next_ref_view;
5706 free_colors(&s->colors);
5710 static const struct got_error *
5711 close_ref_view(struct tog_view *view)
5713 struct tog_ref_view_state *s = &view->state.ref;
5715 ref_view_free_refs(s);
5716 free_colors(&s->colors);
5721 static const struct got_error *
5722 resolve_reflist_entry(struct got_object_id **commit_id,
5723 struct tog_reflist_entry *re, struct got_repository *repo)
5725 const struct got_error *err = NULL;
5726 struct got_object_id *obj_id;
5727 struct got_tag_object *tag = NULL;
5732 err = got_ref_resolve(&obj_id, repo, re->ref);
5736 err = got_object_get_type(&obj_type, repo, obj_id);
5741 case GOT_OBJ_TYPE_COMMIT:
5742 *commit_id = obj_id;
5744 case GOT_OBJ_TYPE_TAG:
5745 err = got_object_open_as_tag(&tag, repo, obj_id);
5749 err = got_object_get_type(&obj_type, repo,
5750 got_object_tag_get_object_id(tag));
5753 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5754 err = got_error(GOT_ERR_OBJ_TYPE);
5757 *commit_id = got_object_id_dup(
5758 got_object_tag_get_object_id(tag));
5759 if (*commit_id == NULL) {
5760 err = got_error_from_errno("got_object_id_dup");
5765 err = got_error(GOT_ERR_OBJ_TYPE);
5771 got_object_tag_close(tag);
5779 static const struct got_error *
5780 log_ref_entry(struct tog_view **new_view, int begin_x,
5781 struct tog_reflist_entry *re, struct got_repository *repo)
5783 struct tog_view *log_view;
5784 const struct got_error *err = NULL;
5785 struct got_object_id *commit_id = NULL;
5789 err = resolve_reflist_entry(&commit_id, re, repo);
5791 if (err->code != GOT_ERR_OBJ_TYPE)
5797 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5798 if (log_view == NULL) {
5799 err = got_error_from_errno("view_open");
5803 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5806 view_close(log_view);
5808 *new_view = log_view;
5814 ref_scroll_up(struct tog_view *view, int maxscroll)
5816 struct tog_ref_view_state *s = &view->state.ref;
5817 struct tog_reflist_entry *re;
5820 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5823 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5824 while (i++ < maxscroll) {
5827 s->first_displayed_entry = re;
5828 re = TAILQ_PREV(re, tog_reflist_head, entry);
5833 ref_scroll_down(struct tog_view *view, int maxscroll)
5835 struct tog_ref_view_state *s = &view->state.ref;
5836 struct tog_reflist_entry *next, *last;
5839 if (s->first_displayed_entry)
5840 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5842 next = TAILQ_FIRST(&s->refs);
5844 last = s->last_displayed_entry;
5845 while (next && last && n++ < maxscroll) {
5846 last = TAILQ_NEXT(last, entry);
5848 s->first_displayed_entry = next;
5849 next = TAILQ_NEXT(next, entry);
5854 static const struct got_error *
5855 search_start_ref_view(struct tog_view *view)
5857 struct tog_ref_view_state *s = &view->state.ref;
5859 s->matched_entry = NULL;
5864 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5866 regmatch_t regmatch;
5868 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5872 static const struct got_error *
5873 search_next_ref_view(struct tog_view *view)
5875 struct tog_ref_view_state *s = &view->state.ref;
5876 struct tog_reflist_entry *re = NULL;
5878 if (!view->searching) {
5879 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5883 if (s->matched_entry) {
5884 if (view->searching == TOG_SEARCH_FORWARD) {
5885 if (s->selected_entry)
5886 re = TAILQ_NEXT(s->selected_entry, entry);
5888 re = TAILQ_PREV(s->selected_entry,
5889 tog_reflist_head, entry);
5891 if (s->selected_entry == NULL)
5892 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5894 re = TAILQ_PREV(s->selected_entry,
5895 tog_reflist_head, entry);
5898 if (view->searching == TOG_SEARCH_FORWARD)
5899 re = TAILQ_FIRST(&s->refs);
5901 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5906 if (s->matched_entry == NULL) {
5907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5910 if (view->searching == TOG_SEARCH_FORWARD)
5911 re = TAILQ_FIRST(&s->refs);
5913 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5916 if (match_reflist_entry(re, &view->regex)) {
5917 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5918 s->matched_entry = re;
5922 if (view->searching == TOG_SEARCH_FORWARD)
5923 re = TAILQ_NEXT(re, entry);
5925 re = TAILQ_PREV(re, tog_reflist_head, entry);
5928 if (s->matched_entry) {
5929 s->first_displayed_entry = s->matched_entry;
5936 static const struct got_error *
5937 show_ref_view(struct tog_view *view)
5939 const struct got_error *err = NULL;
5940 struct tog_ref_view_state *s = &view->state.ref;
5941 struct tog_reflist_entry *re;
5944 struct tog_color *tc;
5946 int limit = view->nlines;
5948 werase(view->window);
5955 re = s->first_displayed_entry;
5957 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5959 return got_error_from_errno("asprintf");
5961 err = format_line(&wline, &width, line, view->ncols, 0);
5966 if (view_needs_focus_indication(view))
5967 wstandout(view->window);
5968 waddwstr(view->window, wline);
5969 if (view_needs_focus_indication(view))
5970 wstandend(view->window);
5975 if (width < view->ncols - 1)
5976 waddch(view->window, '\n');
5981 while (re && limit > 0) {
5984 if (got_ref_is_symbolic(re->ref)) {
5985 if (asprintf(&line, "%s -> %s",
5986 got_ref_get_name(re->ref),
5987 got_ref_get_symref_target(re->ref)) == -1)
5988 return got_error_from_errno("asprintf");
5989 } else if (s->show_ids) {
5990 struct got_object_id *id;
5992 err = got_ref_resolve(&id, s->repo, re->ref);
5995 err = got_object_id_str(&id_str, id);
6000 if (asprintf(&line, "%s: %s",
6001 got_ref_get_name(re->ref), id_str) == -1) {
6002 err = got_error_from_errno("asprintf");
6010 line = strdup(got_ref_get_name(re->ref));
6012 return got_error_from_errno("strdup");
6015 err = format_line(&wline, &width, line, view->ncols, 0);
6020 if (n == s->selected) {
6022 wstandout(view->window);
6023 s->selected_entry = re;
6025 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6027 wattr_on(view->window,
6028 COLOR_PAIR(tc->colorpair), NULL);
6029 waddwstr(view->window, wline);
6031 wattr_off(view->window,
6032 COLOR_PAIR(tc->colorpair), NULL);
6033 if (width < view->ncols - 1)
6034 waddch(view->window, '\n');
6035 if (n == s->selected && view->focussed)
6036 wstandend(view->window);
6042 s->last_displayed_entry = re;
6045 re = TAILQ_NEXT(re, entry);
6052 static const struct got_error *
6053 browse_ref_tree(struct tog_view **new_view, int begin_x,
6054 struct tog_reflist_entry *re, struct got_repository *repo)
6056 const struct got_error *err = NULL;
6057 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6058 struct got_tree_object *tree = NULL;
6059 struct tog_view *tree_view;
6063 err = resolve_reflist_entry(&commit_id, re, repo);
6065 if (err->code != GOT_ERR_OBJ_TYPE)
6071 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6075 err = got_object_open_as_tree(&tree, repo, tree_id);
6079 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6080 if (tree_view == NULL) {
6081 err = got_error_from_errno("view_open");
6085 err = open_tree_view(tree_view, tree, commit_id, repo);
6089 *new_view = tree_view;
6095 got_object_tree_close(tree);
6099 static const struct got_error *
6100 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6101 struct tog_view **focus_view, struct tog_view *view, int ch)
6103 const struct got_error *err = NULL;
6104 struct tog_ref_view_state *s = &view->state.ref;
6105 struct tog_view *log_view, *tree_view;
6110 s->show_ids = !s->show_ids;
6114 if (!s->selected_entry)
6116 if (view_is_parent_view(view))
6117 begin_x = view_split_begin_x(view->begin_x);
6118 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6120 if (view_is_parent_view(view)) {
6121 err = view_close_child(view);
6124 err = view_set_child(view, log_view);
6126 view_close(log_view);
6129 *focus_view = log_view;
6130 view->child_focussed = 1;
6132 *new_view = log_view;
6135 if (!s->selected_entry)
6137 if (view_is_parent_view(view))
6138 begin_x = view_split_begin_x(view->begin_x);
6139 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6141 if (err || tree_view == NULL)
6143 if (view_is_parent_view(view)) {
6144 err = view_close_child(view);
6147 err = view_set_child(view, tree_view);
6149 view_close(tree_view);
6152 *focus_view = tree_view;
6153 view->child_focussed = 1;
6155 *new_view = tree_view;
6159 if (s->selected > 0) {
6163 ref_scroll_up(view, 1);
6167 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6169 ref_scroll_up(view, MAX(0, view->nlines - 1));
6173 if (s->selected < s->ndisplayed - 1) {
6177 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6178 /* can't scroll any further */
6180 ref_scroll_down(view, 1);
6184 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6185 /* can't scroll any further; move cursor down */
6186 if (s->selected < s->ndisplayed - 1)
6187 s->selected = s->ndisplayed - 1;
6190 ref_scroll_down(view, view->nlines - 1);
6193 ref_view_free_refs(s);
6194 err = ref_view_load_refs(s);
6197 if (s->selected > view->nlines)
6198 s->selected = s->ndisplayed - 1;
6211 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6216 static const struct got_error *
6217 cmd_ref(int argc, char *argv[])
6219 const struct got_error *error;
6220 struct got_repository *repo = NULL;
6221 struct got_worktree *worktree = NULL;
6222 char *cwd = NULL, *repo_path = NULL;
6224 struct tog_view *view;
6227 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6232 while ((ch = getopt(argc, argv, "r:")) != -1) {
6235 repo_path = realpath(optarg, NULL);
6236 if (repo_path == NULL)
6237 return got_error_from_errno2("realpath",
6252 cwd = getcwd(NULL, 0);
6254 return got_error_from_errno("getcwd");
6256 error = got_worktree_open(&worktree, cwd);
6257 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6260 if (repo_path == NULL) {
6263 strdup(got_worktree_get_repo_path(worktree));
6265 repo_path = strdup(cwd);
6267 if (repo_path == NULL) {
6268 error = got_error_from_errno("strdup");
6272 error = got_repo_open(&repo, repo_path, NULL);
6278 error = apply_unveil(got_repo_get_path(repo), NULL);
6282 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6284 error = got_error_from_errno("view_open");
6288 error = open_ref_view(view, repo);
6293 /* Release work tree lock. */
6294 got_worktree_close(worktree);
6297 error = view_loop(view);
6302 got_repo_close(repo);
6307 list_commands(FILE *fp)
6311 fprintf(fp, "commands:");
6312 for (i = 0; i < nitems(tog_commands); i++) {
6313 struct tog_cmd *cmd = &tog_commands[i];
6314 fprintf(fp, " %s", cmd->name);
6320 usage(int hflag, int status)
6322 FILE *fp = (status == 0) ? stdout : stderr;
6324 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6327 fprintf(fp, "lazy usage: %s path\n", getprogname());
6334 make_argv(int argc, ...)
6342 argv = calloc(argc, sizeof(char *));
6345 for (i = 0; i < argc; i++) {
6346 argv[i] = strdup(va_arg(ap, char *));
6347 if (argv[i] == NULL)
6356 * Try to convert 'tog path' into a 'tog log path' command.
6357 * The user could simply have mistyped the command rather than knowingly
6358 * provided a path. So check whether argv[0] can in fact be resolved
6359 * to a path in the HEAD commit and print a special error if not.
6360 * This hack is for mpi@ <3
6362 static const struct got_error *
6363 tog_log_with_path(int argc, char *argv[])
6365 const struct got_error *error = NULL;
6366 struct tog_cmd *cmd = NULL;
6367 struct got_repository *repo = NULL;
6368 struct got_worktree *worktree = NULL;
6369 struct got_object_id *commit_id = NULL, *id = NULL;
6370 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6371 char *commit_id_str = NULL, **cmd_argv = NULL;
6373 cwd = getcwd(NULL, 0);
6375 return got_error_from_errno("getcwd");
6377 error = got_worktree_open(&worktree, cwd);
6378 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6382 repo_path = strdup(got_worktree_get_repo_path(worktree));
6384 repo_path = strdup(cwd);
6385 if (repo_path == NULL) {
6386 error = got_error_from_errno("strdup");
6390 error = got_repo_open(&repo, repo_path, NULL);
6394 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6399 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6400 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6401 GOT_OBJ_TYPE_COMMIT, 1, repo);
6406 got_worktree_close(worktree);
6410 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6412 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6414 fprintf(stderr, "%s: '%s' is no known command or path\n",
6415 getprogname(), argv[0]);
6420 got_repo_close(repo);
6423 error = got_object_id_str(&commit_id_str, commit_id);
6427 cmd = &tog_commands[0]; /* log */
6429 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6430 error = cmd->cmd_main(argc, cmd_argv);
6433 got_repo_close(repo);
6435 got_worktree_close(worktree);
6437 free(commit_id_str);
6444 for (i = 0; i < argc; i++)
6452 main(int argc, char *argv[])
6454 const struct got_error *error = NULL;
6455 struct tog_cmd *cmd = NULL;
6456 int ch, hflag = 0, Vflag = 0;
6457 char **cmd_argv = NULL;
6458 static struct option longopts[] = {
6459 { "version", no_argument, NULL, 'V' },
6463 setlocale(LC_CTYPE, "");
6465 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6485 got_version_print_str();
6492 /* Build an argument vector which runs a default command. */
6493 cmd = &tog_commands[0];
6495 cmd_argv = make_argv(argc, cmd->name);
6499 /* Did the user specify a command? */
6500 for (i = 0; i < nitems(tog_commands); i++) {
6501 if (strncmp(tog_commands[i].name, argv[0],
6502 strlen(argv[0])) == 0) {
6503 cmd = &tog_commands[i];
6512 /* No command specified; try log with a path */
6513 error = tog_log_with_path(argc, argv);
6518 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6525 for (i = 0; i < argc; i++)
6530 if (error && error->code != GOT_ERR_CANCELLED)
6531 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);