2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
123 SIMPLEQ_ENTRY(tog_color) entry;
127 SIMPLEQ_HEAD(tog_colors, tog_color);
129 static const struct got_error *
130 add_color(struct tog_colors *colors, const char *pattern,
131 int idx, short color)
133 const struct got_error *err = NULL;
134 struct tog_color *tc;
137 if (idx < 1 || idx > COLOR_PAIRS - 1)
140 init_pair(idx, color, -1);
142 tc = calloc(1, sizeof(*tc));
144 return got_error_from_errno("calloc");
145 regerr = regcomp(&tc->regex, pattern,
146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
148 static char regerr_msg[512];
149 static char err_msg[512];
150 regerror(regerr, &tc->regex, regerr_msg,
152 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
154 err = got_error_msg(GOT_ERR_REGEX, err_msg);
159 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
164 free_colors(struct tog_colors *colors)
166 struct tog_color *tc;
168 while (!SIMPLEQ_EMPTY(colors)) {
169 tc = SIMPLEQ_FIRST(colors);
170 SIMPLEQ_REMOVE_HEAD(colors, entry);
177 get_color(struct tog_colors *colors, int colorpair)
179 struct tog_color *tc = NULL;
181 SIMPLEQ_FOREACH(tc, colors, entry) {
182 if (tc->colorpair == colorpair)
190 default_color_value(const char *envvar)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 return COLOR_MAGENTA;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
196 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
198 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
200 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 return COLOR_MAGENTA;
202 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 return COLOR_MAGENTA;
204 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
206 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
208 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
210 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
212 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
214 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
216 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 return COLOR_MAGENTA;
218 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
225 get_color_value(const char *envvar)
227 const char *val = getenv(envvar);
230 return default_color_value(envvar);
232 if (strcasecmp(val, "black") == 0)
234 if (strcasecmp(val, "red") == 0)
236 if (strcasecmp(val, "green") == 0)
238 if (strcasecmp(val, "yellow") == 0)
240 if (strcasecmp(val, "blue") == 0)
242 if (strcasecmp(val, "magenta") == 0)
243 return COLOR_MAGENTA;
244 if (strcasecmp(val, "cyan") == 0)
246 if (strcasecmp(val, "white") == 0)
248 if (strcasecmp(val, "default") == 0)
251 return default_color_value(envvar);
255 struct tog_diff_view_state {
256 struct got_object_id *id1, *id2;
257 const char *label1, *label2;
259 int first_displayed_line;
260 int last_displayed_line;
263 int ignore_whitespace;
265 struct got_repository *repo;
266 struct got_reflist_head refs;
267 struct tog_colors colors;
273 /* passed from log view; may be NULL */
274 struct tog_view *log_view;
277 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 struct tog_log_thread_args {
280 pthread_cond_t need_commits;
281 pthread_cond_t commit_loaded;
283 struct got_commit_graph *graph;
284 struct commit_queue *commits;
285 const char *in_repo_path;
286 struct got_object_id *start_id;
287 struct got_repository *repo;
290 struct commit_queue_entry **first_displayed_entry;
291 struct commit_queue_entry **selected_entry;
293 int *search_next_done;
297 struct tog_log_view_state {
298 struct commit_queue commits;
299 struct commit_queue_entry *first_displayed_entry;
300 struct commit_queue_entry *last_displayed_entry;
301 struct commit_queue_entry *selected_entry;
304 const char *head_ref_name;
306 struct got_repository *repo;
307 struct got_reflist_head refs;
308 struct got_object_id *start_id;
311 struct tog_log_thread_args thread_args;
312 struct commit_queue_entry *matched_entry;
313 struct commit_queue_entry *search_entry;
314 struct tog_colors colors;
317 #define TOG_COLOR_DIFF_MINUS 1
318 #define TOG_COLOR_DIFF_PLUS 2
319 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 #define TOG_COLOR_DIFF_META 4
321 #define TOG_COLOR_TREE_SUBMODULE 5
322 #define TOG_COLOR_TREE_SYMLINK 6
323 #define TOG_COLOR_TREE_DIRECTORY 7
324 #define TOG_COLOR_TREE_EXECUTABLE 8
325 #define TOG_COLOR_COMMIT 9
326 #define TOG_COLOR_AUTHOR 10
327 #define TOG_COLOR_DATE 11
328 #define TOG_COLOR_REFS_HEADS 12
329 #define TOG_COLOR_REFS_TAGS 13
330 #define TOG_COLOR_REFS_REMOTES 14
332 struct tog_blame_cb_args {
333 struct tog_blame_line *lines; /* one per line */
336 struct tog_view *view;
337 struct got_object_id *commit_id;
341 struct tog_blame_thread_args {
343 struct got_repository *repo;
344 struct tog_blame_cb_args *cb_args;
346 got_cancel_cb cancel_cb;
353 struct tog_blame_line *lines;
357 struct tog_blame_thread_args thread_args;
358 struct tog_blame_cb_args cb_args;
362 struct tog_blame_view_state {
363 int first_displayed_line;
364 int last_displayed_line;
369 struct got_object_id_queue blamed_commits;
370 struct got_object_qid *blamed_commit;
372 struct got_repository *repo;
373 struct got_object_id *commit_id;
374 struct tog_blame blame;
376 struct tog_colors colors;
379 struct tog_parent_tree {
380 TAILQ_ENTRY(tog_parent_tree) entry;
381 struct got_tree_object *tree;
382 struct got_tree_entry *first_displayed_entry;
383 struct got_tree_entry *selected_entry;
387 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 struct tog_tree_view_state {
391 struct got_tree_object *root;
392 struct got_tree_object *tree;
393 struct got_tree_entry *first_displayed_entry;
394 struct got_tree_entry *last_displayed_entry;
395 struct got_tree_entry *selected_entry;
396 int ndisplayed, selected, show_ids;
397 struct tog_parent_trees parents;
398 struct got_object_id *commit_id;
399 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
441 TAILQ_HEAD(tog_view_list_head, tog_view);
444 TAILQ_ENTRY(tog_view) entry;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
450 struct tog_view *parent;
451 struct tog_view *child;
454 /* type-specific state */
455 enum tog_view_type type;
457 struct tog_diff_view_state diff;
458 struct tog_log_view_state log;
459 struct tog_blame_view_state blame;
460 struct tog_tree_view_state tree;
461 struct tog_ref_view_state ref;
464 const struct got_error *(*show)(struct tog_view *);
465 const struct got_error *(*input)(struct tog_view **,
466 struct tog_view **, struct tog_view**, struct tog_view *, int);
467 const struct got_error *(*close)(struct tog_view *);
469 const struct got_error *(*search_start)(struct tog_view *);
470 const struct got_error *(*search_next)(struct tog_view *);
472 #define TOG_SEARCH_FORWARD 1
473 #define TOG_SEARCH_BACKWARD 2
474 int search_next_done;
475 #define TOG_SEARCH_HAVE_MORE 1
476 #define TOG_SEARCH_NO_MORE 2
477 #define TOG_SEARCH_HAVE_NONE 3
482 static const struct got_error *open_diff_view(struct tog_view *,
483 struct got_object_id *, struct got_object_id *,
484 const char *, const char *, int, int, int, struct tog_view *,
485 struct got_repository *);
486 static const struct got_error *show_diff_view(struct tog_view *);
487 static const struct got_error *input_diff_view(struct tog_view **,
488 struct tog_view **, struct tog_view **, struct tog_view *, int);
489 static const struct got_error* close_diff_view(struct tog_view *);
490 static const struct got_error *search_start_diff_view(struct tog_view *);
491 static const struct got_error *search_next_diff_view(struct tog_view *);
493 static const struct got_error *open_log_view(struct tog_view *,
494 struct got_object_id *, struct got_repository *,
495 const char *, const char *, int);
496 static const struct got_error * show_log_view(struct tog_view *);
497 static const struct got_error *input_log_view(struct tog_view **,
498 struct tog_view **, struct tog_view **, struct tog_view *, int);
499 static const struct got_error *close_log_view(struct tog_view *);
500 static const struct got_error *search_start_log_view(struct tog_view *);
501 static const struct got_error *search_next_log_view(struct tog_view *);
503 static const struct got_error *open_blame_view(struct tog_view *, char *,
504 struct got_object_id *, struct got_repository *);
505 static const struct got_error *show_blame_view(struct tog_view *);
506 static const struct got_error *input_blame_view(struct tog_view **,
507 struct tog_view **, struct tog_view **, struct tog_view *, int);
508 static const struct got_error *close_blame_view(struct tog_view *);
509 static const struct got_error *search_start_blame_view(struct tog_view *);
510 static const struct got_error *search_next_blame_view(struct tog_view *);
512 static const struct got_error *open_tree_view(struct tog_view *,
513 struct got_tree_object *, struct got_object_id *, struct got_repository *);
514 static const struct got_error *show_tree_view(struct tog_view *);
515 static const struct got_error *input_tree_view(struct tog_view **,
516 struct tog_view **, struct tog_view **, struct tog_view *, int);
517 static const struct got_error *close_tree_view(struct tog_view *);
518 static const struct got_error *search_start_tree_view(struct tog_view *);
519 static const struct got_error *search_next_tree_view(struct tog_view *);
521 static const struct got_error *open_ref_view(struct tog_view *,
522 struct got_repository *);
523 static const struct got_error *show_ref_view(struct tog_view *);
524 static const struct got_error *input_ref_view(struct tog_view **,
525 struct tog_view **, struct tog_view **, struct tog_view *, int);
526 static const struct got_error *close_ref_view(struct tog_view *);
527 static const struct got_error *search_start_ref_view(struct tog_view *);
528 static const struct got_error *search_next_ref_view(struct tog_view *);
530 static volatile sig_atomic_t tog_sigwinch_received;
531 static volatile sig_atomic_t tog_sigpipe_received;
532 static volatile sig_atomic_t tog_sigcont_received;
535 tog_sigwinch(int signo)
537 tog_sigwinch_received = 1;
541 tog_sigpipe(int signo)
543 tog_sigpipe_received = 1;
547 tog_sigcont(int signo)
549 tog_sigcont_received = 1;
552 static const struct got_error *
553 view_close(struct tog_view *view)
555 const struct got_error *err = NULL;
558 view_close(view->child);
562 err = view->close(view);
564 del_panel(view->panel);
566 delwin(view->window);
571 static struct tog_view *
572 view_open(int nlines, int ncols, int begin_y, int begin_x,
573 enum tog_view_type type)
575 struct tog_view *view = calloc(1, sizeof(*view));
583 view->nlines = nlines ? nlines : LINES - begin_y;
584 view->ncols = ncols ? ncols : COLS - begin_x;
585 view->begin_y = begin_y;
586 view->begin_x = begin_x;
587 view->window = newwin(nlines, ncols, begin_y, begin_x);
588 if (view->window == NULL) {
592 view->panel = new_panel(view->window);
593 if (view->panel == NULL ||
594 set_panel_userptr(view->panel, view) != OK) {
599 keypad(view->window, TRUE);
604 view_split_begin_x(int begin_x)
606 if (begin_x > 0 || COLS < 120)
608 return (COLS - MAX(COLS / 2, 80));
611 static const struct got_error *view_resize(struct tog_view *);
613 static const struct got_error *
614 view_splitscreen(struct tog_view *view)
616 const struct got_error *err = NULL;
619 view->begin_x = view_split_begin_x(0);
620 view->nlines = LINES;
621 view->ncols = COLS - view->begin_x;
624 err = view_resize(view);
628 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
629 return got_error_from_errno("mvwin");
634 static const struct got_error *
635 view_fullscreen(struct tog_view *view)
637 const struct got_error *err = NULL;
641 view->nlines = LINES;
645 err = view_resize(view);
649 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
650 return got_error_from_errno("mvwin");
656 view_is_parent_view(struct tog_view *view)
658 return view->parent == NULL;
661 static const struct got_error *
662 view_resize(struct tog_view *view)
666 if (view->lines > LINES)
667 nlines = view->nlines - (view->lines - LINES);
669 nlines = view->nlines + (LINES - view->lines);
671 if (view->cols > COLS)
672 ncols = view->ncols - (view->cols - COLS);
674 ncols = view->ncols + (COLS - view->cols);
676 if (wresize(view->window, nlines, ncols) == ERR)
677 return got_error_from_errno("wresize");
678 if (replace_panel(view->panel, view->window) == ERR)
679 return got_error_from_errno("replace_panel");
680 wclear(view->window);
682 view->nlines = nlines;
688 view->child->begin_x = view_split_begin_x(view->begin_x);
689 if (view->child->begin_x == 0) {
690 view_fullscreen(view->child);
691 if (view->child->focussed)
692 show_panel(view->child->panel);
694 show_panel(view->panel);
696 view_splitscreen(view->child);
697 show_panel(view->child->panel);
704 static const struct got_error *
705 view_close_child(struct tog_view *view)
707 const struct got_error *err = NULL;
709 if (view->child == NULL)
712 err = view_close(view->child);
717 static const struct got_error *
718 view_set_child(struct tog_view *view, struct tog_view *child)
720 const struct got_error *err = NULL;
723 child->parent = view;
728 view_is_splitscreen(struct tog_view *view)
730 return view->begin_x > 0;
739 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
740 cols = 80; /* Default */
746 resize_term(lines, cols);
749 static const struct got_error *
750 view_search_start(struct tog_view *view)
752 const struct got_error *err = NULL;
756 if (view->nlines < 1)
759 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
760 wclrtoeol(view->window);
764 ret = wgetnstr(view->window, pattern, sizeof(pattern));
770 if (view->searching) {
771 regfree(&view->regex);
775 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
776 err = view->search_start(view);
778 regfree(&view->regex);
781 view->searching = TOG_SEARCH_FORWARD;
782 view->search_next_done = 0;
783 view->search_next(view);
789 static const struct got_error *
790 view_input(struct tog_view **new, struct tog_view **dead,
791 struct tog_view **focus, int *done, struct tog_view *view,
792 struct tog_view_list_head *views)
794 const struct got_error *err = NULL;
802 /* Clear "no matches" indicator. */
803 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
804 view->search_next_done == TOG_SEARCH_HAVE_NONE)
805 view->search_next_done = TOG_SEARCH_HAVE_MORE;
807 if (view->searching && !view->search_next_done) {
808 errcode = pthread_mutex_unlock(&tog_mutex);
810 return got_error_set_errno(errcode,
811 "pthread_mutex_unlock");
813 errcode = pthread_mutex_lock(&tog_mutex);
815 return got_error_set_errno(errcode,
816 "pthread_mutex_lock");
817 view->search_next(view);
821 nodelay(stdscr, FALSE);
822 /* Allow threads to make progress while we are waiting for input. */
823 errcode = pthread_mutex_unlock(&tog_mutex);
825 return got_error_set_errno(errcode, "pthread_mutex_unlock");
826 ch = wgetch(view->window);
827 errcode = pthread_mutex_lock(&tog_mutex);
829 return got_error_set_errno(errcode, "pthread_mutex_lock");
830 nodelay(stdscr, TRUE);
832 if (tog_sigwinch_received || tog_sigcont_received) {
834 tog_sigwinch_received = 0;
835 tog_sigcont_received = 0;
836 TAILQ_FOREACH(v, views, entry) {
837 err = view_resize(v);
840 err = v->input(new, dead, focus, v, KEY_RESIZE);
851 *focus = view->child;
852 view->child_focussed = 1;
853 } else if (view->parent) {
854 *focus = view->parent;
855 view->parent->child_focussed = 0;
859 err = view->input(new, dead, focus, view, ch);
866 if (view_is_parent_view(view)) {
867 if (view->child == NULL)
869 if (view_is_splitscreen(view->child)) {
870 *focus = view->child;
871 view->child_focussed = 1;
872 err = view_fullscreen(view->child);
874 err = view_splitscreen(view->child);
877 err = view->child->input(new, dead, focus,
878 view->child, KEY_RESIZE);
880 if (view_is_splitscreen(view)) {
882 view->parent->child_focussed = 1;
883 err = view_fullscreen(view);
885 err = view_splitscreen(view);
889 err = view->input(new, dead, focus, view,
896 if (view->search_start)
897 view_search_start(view);
899 err = view->input(new, dead, focus, view, ch);
903 if (view->search_next) {
904 view->searching = (ch == 'n' ?
905 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
906 view->search_next_done = 0;
907 view->search_next(view);
909 err = view->input(new, dead, focus, view, ch);
912 err = view->input(new, dead, focus, view, ch);
920 view_vborder(struct tog_view *view)
923 struct tog_view *view_above;
926 return view_vborder(view->parent);
928 panel = panel_above(view->panel);
932 view_above = panel_userptr(panel);
933 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
934 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
938 view_needs_focus_indication(struct tog_view *view)
940 if (view_is_parent_view(view)) {
941 if (view->child == NULL || view->child_focussed)
943 if (!view_is_splitscreen(view->child))
945 } else if (!view_is_splitscreen(view))
948 return view->focussed;
951 static const struct got_error *
952 view_loop(struct tog_view *view)
954 const struct got_error *err = NULL;
955 struct tog_view_list_head views;
956 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
957 int fast_refresh = 10;
958 int done = 0, errcode;
960 errcode = pthread_mutex_lock(&tog_mutex);
962 return got_error_set_errno(errcode, "pthread_mutex_lock");
965 TAILQ_INSERT_HEAD(&views, view, entry);
969 err = view->show(view);
974 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
975 /* Refresh fast during initialization, then become slower. */
976 if (fast_refresh && fast_refresh-- == 0)
977 halfdelay(10); /* switch to once per second */
979 err = view_input(&new_view, &dead_view, &focus_view, &done,
984 struct tog_view *prev = NULL;
986 if (view_is_parent_view(dead_view))
987 prev = TAILQ_PREV(dead_view,
988 tog_view_list_head, entry);
989 else if (view->parent != dead_view)
992 if (dead_view->parent)
993 dead_view->parent->child = NULL;
995 TAILQ_REMOVE(&views, dead_view, entry);
997 err = view_close(dead_view);
998 if (err || (dead_view == main_view && new_view == NULL))
1001 if (view == dead_view) {
1006 else if (!TAILQ_EMPTY(&views))
1007 view = TAILQ_LAST(&views,
1008 tog_view_list_head);
1012 if (view->child && view->child_focussed)
1013 focus_view = view->child;
1020 struct tog_view *v, *t;
1021 /* Only allow one parent view per type. */
1022 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1023 if (v->type != new_view->type)
1025 TAILQ_REMOVE(&views, v, entry);
1026 err = view_close(v);
1031 TAILQ_INSERT_TAIL(&views, new_view, entry);
1033 if (focus_view == NULL)
1034 focus_view = new_view;
1037 show_panel(focus_view->panel);
1040 focus_view->focussed = 1;
1043 show_panel(new_view->panel);
1044 if (view->child && view_is_splitscreen(view->child))
1045 show_panel(view->child->panel);
1048 if (focus_view == NULL) {
1050 show_panel(view->panel);
1051 if (view->child && view_is_splitscreen(view->child))
1052 show_panel(view->child->panel);
1056 err = view->parent->show(view->parent);
1060 err = view->show(view);
1064 err = view->child->show(view->child);
1073 while (!TAILQ_EMPTY(&views)) {
1074 view = TAILQ_FIRST(&views);
1075 TAILQ_REMOVE(&views, view, entry);
1079 errcode = pthread_mutex_unlock(&tog_mutex);
1080 if (errcode && err == NULL)
1081 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1091 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1096 /* Create newly allocated wide-character string equivalent to a byte string. */
1097 static const struct got_error *
1098 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1101 const struct got_error *err = NULL;
1104 *wlen = mbstowcs(NULL, s, 0);
1105 if (*wlen == (size_t)-1) {
1107 if (errno != EILSEQ)
1108 return got_error_from_errno("mbstowcs");
1110 /* byte string invalid in current encoding; try to "fix" it */
1111 err = got_mbsavis(&vis, &vislen, s);
1114 *wlen = mbstowcs(NULL, vis, 0);
1115 if (*wlen == (size_t)-1) {
1116 err = got_error_from_errno("mbstowcs"); /* give up */
1121 *ws = calloc(*wlen + 1, sizeof(**ws));
1123 err = got_error_from_errno("calloc");
1127 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1128 err = got_error_from_errno("mbstowcs");
1139 /* Format a line for display, ensuring that it won't overflow a width limit. */
1140 static const struct got_error *
1141 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1144 const struct got_error *err = NULL;
1146 wchar_t *wline = NULL;
1153 err = mbs2ws(&wline, &wlen, line);
1159 int width = wcwidth(wline[i]);
1166 if (width == 1 || width == 2) {
1167 if (cols + width > wlimit)
1171 } else if (width == -1) {
1172 if (wline[i] == L'\t') {
1174 ((cols + col_tab_align) % TABSIZE);
1175 if (cols + width > wlimit)
1181 err = got_error_from_errno("wcwidth");
1196 static const struct got_error*
1197 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1198 struct got_object_id *id, struct got_repository *repo)
1200 static const struct got_error *err = NULL;
1201 struct got_reflist_entry *re;
1207 SIMPLEQ_FOREACH(re, refs, entry) {
1208 struct got_tag_object *tag = NULL;
1209 struct got_object_id *ref_id;
1212 name = got_ref_get_name(re->ref);
1213 if (strcmp(name, GOT_REF_HEAD) == 0)
1215 if (strncmp(name, "refs/", 5) == 0)
1217 if (strncmp(name, "got/", 4) == 0)
1219 if (strncmp(name, "heads/", 6) == 0)
1221 if (strncmp(name, "remotes/", 8) == 0) {
1223 s = strstr(name, "/" GOT_REF_HEAD);
1224 if (s != NULL && s[strlen(s)] == '\0')
1227 err = got_ref_resolve(&ref_id, repo, re->ref);
1230 if (strncmp(name, "tags/", 5) == 0) {
1231 err = got_object_open_as_tag(&tag, repo, ref_id);
1233 if (err->code != GOT_ERR_OBJ_TYPE) {
1237 /* Ref points at something other than a tag. */
1242 cmp = got_object_id_cmp(tag ?
1243 got_object_tag_get_object_id(tag) : ref_id, id);
1246 got_object_tag_close(tag);
1250 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1251 s ? ", " : "", name) == -1) {
1252 err = got_error_from_errno("asprintf");
1263 static const struct got_error *
1264 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1267 char *smallerthan, *at;
1269 smallerthan = strchr(author, '<');
1270 if (smallerthan && smallerthan[1] != '\0')
1271 author = smallerthan + 1;
1272 at = strchr(author, '@');
1275 return format_line(wauthor, author_width, author, limit, col_tab_align);
1278 static const struct got_error *
1279 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1280 struct got_object_id *id, const size_t date_display_cols,
1281 int author_display_cols)
1283 struct tog_log_view_state *s = &view->state.log;
1284 const struct got_error *err = NULL;
1285 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1286 char *logmsg0 = NULL, *logmsg = NULL;
1287 char *author = NULL;
1288 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1289 int author_width, logmsg_width;
1290 char *newline, *line = NULL;
1292 const int avail = view->ncols;
1294 time_t committer_time;
1295 struct tog_color *tc;
1297 committer_time = got_object_commit_get_committer_time(commit);
1298 if (localtime_r(&committer_time, &tm) == NULL)
1299 return got_error_from_errno("localtime_r");
1300 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1302 return got_error(GOT_ERR_NO_SPACE);
1304 if (avail <= date_display_cols)
1305 limit = MIN(sizeof(datebuf) - 1, avail);
1307 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1308 tc = get_color(&s->colors, TOG_COLOR_DATE);
1310 wattr_on(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1312 waddnstr(view->window, datebuf, limit);
1314 wattr_off(view->window,
1315 COLOR_PAIR(tc->colorpair), NULL);
1322 err = got_object_id_str(&id_str, id);
1325 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1327 wattr_on(view->window,
1328 COLOR_PAIR(tc->colorpair), NULL);
1329 wprintw(view->window, "%.8s ", id_str);
1331 wattr_off(view->window,
1332 COLOR_PAIR(tc->colorpair), NULL);
1339 author = strdup(got_object_commit_get_author(commit));
1340 if (author == NULL) {
1341 err = got_error_from_errno("strdup");
1344 err = format_author(&wauthor, &author_width, author, avail - col, col);
1347 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1349 wattr_on(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1351 waddwstr(view->window, wauthor);
1353 wattr_off(view->window,
1354 COLOR_PAIR(tc->colorpair), NULL);
1355 col += author_width;
1356 while (col < avail && author_width < author_display_cols + 2) {
1357 waddch(view->window, ' ');
1364 err = got_object_commit_get_logmsg(&logmsg0, commit);
1368 while (*logmsg == '\n')
1370 newline = strchr(logmsg, '\n');
1373 limit = avail - col;
1374 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1377 waddwstr(view->window, wlogmsg);
1378 col += logmsg_width;
1379 while (col < avail) {
1380 waddch(view->window, ' ');
1392 static struct commit_queue_entry *
1393 alloc_commit_queue_entry(struct got_commit_object *commit,
1394 struct got_object_id *id)
1396 struct commit_queue_entry *entry;
1398 entry = calloc(1, sizeof(*entry));
1403 entry->commit = commit;
1408 pop_commit(struct commit_queue *commits)
1410 struct commit_queue_entry *entry;
1412 entry = TAILQ_FIRST(&commits->head);
1413 TAILQ_REMOVE(&commits->head, entry, entry);
1414 got_object_commit_close(entry->commit);
1415 commits->ncommits--;
1416 /* Don't free entry->id! It is owned by the commit graph. */
1421 free_commits(struct commit_queue *commits)
1423 while (!TAILQ_EMPTY(&commits->head))
1424 pop_commit(commits);
1427 static const struct got_error *
1428 match_commit(int *have_match, struct got_object_id *id,
1429 struct got_commit_object *commit, regex_t *regex)
1431 const struct got_error *err = NULL;
1432 regmatch_t regmatch;
1433 char *id_str = NULL, *logmsg = NULL;
1437 err = got_object_id_str(&id_str, id);
1441 err = got_object_commit_get_logmsg(&logmsg, commit);
1445 if (regexec(regex, got_object_commit_get_author(commit), 1,
1446 ®match, 0) == 0 ||
1447 regexec(regex, got_object_commit_get_committer(commit), 1,
1448 ®match, 0) == 0 ||
1449 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1450 regexec(regex, logmsg, 1, ®match, 0) == 0)
1458 static const struct got_error *
1459 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1460 int minqueue, struct got_repository *repo, const char *path,
1461 int *searching, int *search_next_done, regex_t *regex)
1463 const struct got_error *err = NULL;
1467 * We keep all commits open throughout the lifetime of the log
1468 * view in order to avoid having to re-fetch commits from disk
1469 * while updating the display.
1471 while (nqueued < minqueue ||
1472 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1473 struct got_object_id *id;
1474 struct got_commit_object *commit;
1475 struct commit_queue_entry *entry;
1478 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1479 if (err || id == NULL)
1482 err = got_object_open_as_commit(&commit, repo, id);
1485 entry = alloc_commit_queue_entry(commit, id);
1486 if (entry == NULL) {
1487 err = got_error_from_errno("alloc_commit_queue_entry");
1491 errcode = pthread_mutex_lock(&tog_mutex);
1493 err = got_error_set_errno(errcode,
1494 "pthread_mutex_lock");
1498 entry->idx = commits->ncommits;
1499 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1501 commits->ncommits++;
1503 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1505 err = match_commit(&have_match, id, commit, regex);
1509 *search_next_done = TOG_SEARCH_HAVE_MORE;
1512 errcode = pthread_mutex_unlock(&tog_mutex);
1513 if (errcode && err == NULL)
1514 err = got_error_set_errno(errcode,
1515 "pthread_mutex_unlock");
1523 static const struct got_error *
1524 draw_commits(struct tog_view *view)
1526 const struct got_error *err = NULL;
1527 struct tog_log_view_state *s = &view->state.log;
1528 struct commit_queue_entry *entry;
1529 const int limit = view->nlines;
1531 int ncommits, author_cols = 4;
1532 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1533 char *refs_str = NULL;
1535 struct tog_color *tc;
1536 static const size_t date_display_cols = 12;
1538 entry = s->first_displayed_entry;
1541 if (ncommits == s->selected) {
1542 s->selected_entry = entry;
1545 entry = TAILQ_NEXT(entry, entry);
1549 if (s->selected_entry &&
1550 !(view->searching && view->search_next_done == 0)) {
1551 err = got_object_id_str(&id_str, s->selected_entry->id);
1554 err = build_refs_str(&refs_str, &s->refs,
1555 s->selected_entry->id, s->repo);
1560 if (s->thread_args.commits_needed == 0)
1561 halfdelay(10); /* disable fast refresh */
1563 if (s->thread_args.commits_needed > 0) {
1564 if (asprintf(&ncommits_str, " [%d/%d] %s",
1565 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1566 (view->searching && !view->search_next_done) ?
1567 "searching..." : "loading...") == -1) {
1568 err = got_error_from_errno("asprintf");
1572 const char *search_str = NULL;
1574 if (view->searching) {
1575 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1576 search_str = "no more matches";
1577 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1578 search_str = "no matches found";
1579 else if (!view->search_next_done)
1580 search_str = "searching...";
1583 if (asprintf(&ncommits_str, " [%d/%d] %s",
1584 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1585 search_str ? search_str :
1586 (refs_str ? refs_str : "")) == -1) {
1587 err = got_error_from_errno("asprintf");
1592 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1593 if (asprintf(&header, "commit %s %s%s",
1594 id_str ? id_str : "........................................",
1595 s->in_repo_path, ncommits_str) == -1) {
1596 err = got_error_from_errno("asprintf");
1600 } else if (asprintf(&header, "commit %s%s",
1601 id_str ? id_str : "........................................",
1602 ncommits_str) == -1) {
1603 err = got_error_from_errno("asprintf");
1607 err = format_line(&wline, &width, header, view->ncols, 0);
1611 werase(view->window);
1613 if (view_needs_focus_indication(view))
1614 wstandout(view->window);
1615 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1617 wattr_on(view->window,
1618 COLOR_PAIR(tc->colorpair), NULL);
1619 waddwstr(view->window, wline);
1621 wattr_off(view->window,
1622 COLOR_PAIR(tc->colorpair), NULL);
1623 while (width < view->ncols) {
1624 waddch(view->window, ' ');
1627 if (view_needs_focus_indication(view))
1628 wstandend(view->window);
1633 /* Grow author column size if necessary. */
1634 entry = s->first_displayed_entry;
1640 if (ncommits >= limit - 1)
1642 author = strdup(got_object_commit_get_author(entry->commit));
1643 if (author == NULL) {
1644 err = got_error_from_errno("strdup");
1647 err = format_author(&wauthor, &width, author, COLS,
1649 if (author_cols < width)
1650 author_cols = width;
1654 entry = TAILQ_NEXT(entry, entry);
1657 entry = s->first_displayed_entry;
1658 s->last_displayed_entry = s->first_displayed_entry;
1661 if (ncommits >= limit - 1)
1663 if (ncommits == s->selected)
1664 wstandout(view->window);
1665 err = draw_commit(view, entry->commit, entry->id,
1666 date_display_cols, author_cols);
1667 if (ncommits == s->selected)
1668 wstandend(view->window);
1672 s->last_displayed_entry = entry;
1673 entry = TAILQ_NEXT(entry, entry);
1686 log_scroll_up(struct tog_view *view, int maxscroll)
1688 struct tog_log_view_state *s = &view->state.log;
1689 struct commit_queue_entry *entry;
1692 entry = TAILQ_FIRST(&s->commits.head);
1693 if (s->first_displayed_entry == entry)
1696 entry = s->first_displayed_entry;
1697 while (entry && nscrolled < maxscroll) {
1698 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1700 s->first_displayed_entry = entry;
1706 static const struct got_error *
1707 trigger_log_thread(struct tog_view *view, int wait)
1709 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1712 halfdelay(1); /* fast refresh while loading commits */
1714 while (ta->commits_needed > 0) {
1715 if (ta->log_complete)
1718 /* Wake the log thread. */
1719 errcode = pthread_cond_signal(&ta->need_commits);
1721 return got_error_set_errno(errcode,
1722 "pthread_cond_signal");
1725 * The mutex will be released while the view loop waits
1726 * in wgetch(), at which time the log thread will run.
1731 /* Display progress update in log view. */
1732 show_log_view(view);
1736 /* Wait right here while next commit is being loaded. */
1737 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1739 return got_error_set_errno(errcode,
1740 "pthread_cond_wait");
1742 /* Display progress update in log view. */
1743 show_log_view(view);
1751 static const struct got_error *
1752 log_scroll_down(struct tog_view *view, int maxscroll)
1754 struct tog_log_view_state *s = &view->state.log;
1755 const struct got_error *err = NULL;
1756 struct commit_queue_entry *pentry;
1757 int nscrolled = 0, ncommits_needed;
1759 if (s->last_displayed_entry == NULL)
1762 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1763 if (s->commits.ncommits < ncommits_needed &&
1764 !s->thread_args.log_complete) {
1766 * Ask the log thread for required amount of commits.
1768 s->thread_args.commits_needed += maxscroll;
1769 err = trigger_log_thread(view, 1);
1775 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1779 s->last_displayed_entry = pentry;
1781 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1784 s->first_displayed_entry = pentry;
1785 } while (++nscrolled < maxscroll);
1790 static const struct got_error *
1791 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1792 struct got_commit_object *commit, struct got_object_id *commit_id,
1793 struct tog_view *log_view, struct got_repository *repo)
1795 const struct got_error *err;
1796 struct got_object_qid *parent_id;
1797 struct tog_view *diff_view;
1799 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1800 if (diff_view == NULL)
1801 return got_error_from_errno("view_open");
1803 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1804 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1805 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1807 *new_view = diff_view;
1811 static const struct got_error *
1812 tree_view_visit_subtree(struct got_tree_object *subtree,
1813 struct tog_tree_view_state *s)
1815 struct tog_parent_tree *parent;
1817 parent = calloc(1, sizeof(*parent));
1819 return got_error_from_errno("calloc");
1821 parent->tree = s->tree;
1822 parent->first_displayed_entry = s->first_displayed_entry;
1823 parent->selected_entry = s->selected_entry;
1824 parent->selected = s->selected;
1825 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1828 s->first_displayed_entry = NULL;
1832 static const struct got_error *
1833 tree_view_walk_path(struct tog_tree_view_state *s,
1834 struct got_object_id *commit_id,
1835 const char *path, struct got_repository *repo)
1837 const struct got_error *err = NULL;
1838 struct got_tree_object *tree = NULL;
1840 char *slash, *subpath = NULL;
1842 /* Walk the path and open corresponding tree objects. */
1845 struct got_tree_entry *te;
1846 struct got_object_id *tree_id;
1852 /* Ensure the correct subtree entry is selected. */
1853 slash = strchr(p, '/');
1855 te_name = strdup(p);
1857 te_name = strndup(p, slash - p);
1858 if (te_name == NULL) {
1859 err = got_error_from_errno("strndup");
1862 te = got_object_tree_find_entry(s->tree, te_name);
1864 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1869 s->first_displayed_entry = s->selected_entry = te;
1871 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1872 break; /* jump to this file's entry */
1874 slash = strchr(p, '/');
1876 subpath = strndup(path, slash - path);
1878 subpath = strdup(path);
1879 if (subpath == NULL) {
1880 err = got_error_from_errno("strdup");
1884 err = got_object_id_by_path(&tree_id, repo, commit_id,
1889 err = got_object_open_as_tree(&tree, repo, tree_id);
1894 err = tree_view_visit_subtree(tree, s);
1896 got_object_tree_close(tree);
1910 static const struct got_error *
1911 browse_commit_tree(struct tog_view **new_view, int begin_x,
1912 struct commit_queue_entry *entry, const char *path,
1913 struct got_repository *repo)
1915 const struct got_error *err = NULL;
1916 struct got_tree_object *tree;
1917 struct tog_tree_view_state *s;
1918 struct tog_view *tree_view;
1920 err = got_object_open_as_tree(&tree, repo,
1921 got_object_commit_get_tree_id(entry->commit));
1925 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1926 if (tree_view == NULL)
1927 return got_error_from_errno("view_open");
1929 err = open_tree_view(tree_view, tree, entry->id, repo);
1931 got_object_tree_close(tree);
1934 s = &tree_view->state.tree;
1936 *new_view = tree_view;
1938 if (got_path_is_root_dir(path))
1941 return tree_view_walk_path(s, entry->id, path, repo);
1944 static const struct got_error *
1945 block_signals_used_by_main_thread(void)
1950 if (sigemptyset(&sigset) == -1)
1951 return got_error_from_errno("sigemptyset");
1953 /* tog handles SIGWINCH and SIGCONT */
1954 if (sigaddset(&sigset, SIGWINCH) == -1)
1955 return got_error_from_errno("sigaddset");
1956 if (sigaddset(&sigset, SIGCONT) == -1)
1957 return got_error_from_errno("sigaddset");
1959 /* ncurses handles SIGTSTP */
1960 if (sigaddset(&sigset, SIGTSTP) == -1)
1961 return got_error_from_errno("sigaddset");
1963 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1965 return got_error_set_errno(errcode, "pthread_sigmask");
1971 log_thread(void *arg)
1973 const struct got_error *err = NULL;
1975 struct tog_log_thread_args *a = arg;
1978 err = block_signals_used_by_main_thread();
1982 while (!done && !err && !tog_sigpipe_received) {
1983 err = queue_commits(a->graph, a->commits, 1, a->repo,
1984 a->in_repo_path, a->searching, a->search_next_done,
1987 if (err->code != GOT_ERR_ITER_COMPLETED)
1991 } else if (a->commits_needed > 0)
1992 a->commits_needed--;
1994 errcode = pthread_mutex_lock(&tog_mutex);
1996 err = got_error_set_errno(errcode,
1997 "pthread_mutex_lock");
1999 } else if (*a->quit)
2001 else if (*a->first_displayed_entry == NULL) {
2002 *a->first_displayed_entry =
2003 TAILQ_FIRST(&a->commits->head);
2004 *a->selected_entry = *a->first_displayed_entry;
2007 errcode = pthread_cond_signal(&a->commit_loaded);
2009 err = got_error_set_errno(errcode,
2010 "pthread_cond_signal");
2011 pthread_mutex_unlock(&tog_mutex);
2016 a->commits_needed = 0;
2018 if (a->commits_needed == 0) {
2019 errcode = pthread_cond_wait(&a->need_commits,
2022 err = got_error_set_errno(errcode,
2023 "pthread_cond_wait");
2027 errcode = pthread_mutex_unlock(&tog_mutex);
2028 if (errcode && err == NULL)
2029 err = got_error_set_errno(errcode,
2030 "pthread_mutex_unlock");
2032 a->log_complete = 1;
2036 static const struct got_error *
2037 stop_log_thread(struct tog_log_view_state *s)
2039 const struct got_error *err = NULL;
2044 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2046 return got_error_set_errno(errcode,
2047 "pthread_cond_signal");
2048 errcode = pthread_mutex_unlock(&tog_mutex);
2050 return got_error_set_errno(errcode,
2051 "pthread_mutex_unlock");
2052 errcode = pthread_join(s->thread, (void **)&err);
2054 return got_error_set_errno(errcode, "pthread_join");
2055 errcode = pthread_mutex_lock(&tog_mutex);
2057 return got_error_set_errno(errcode,
2058 "pthread_mutex_lock");
2062 if (s->thread_args.repo) {
2063 got_repo_close(s->thread_args.repo);
2064 s->thread_args.repo = NULL;
2067 if (s->thread_args.graph) {
2068 got_commit_graph_close(s->thread_args.graph);
2069 s->thread_args.graph = NULL;
2075 static const struct got_error *
2076 close_log_view(struct tog_view *view)
2078 const struct got_error *err = NULL;
2079 struct tog_log_view_state *s = &view->state.log;
2082 err = stop_log_thread(s);
2084 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2085 if (errcode && err == NULL)
2086 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2088 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2089 if (errcode && err == NULL)
2090 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2092 free_commits(&s->commits);
2093 free(s->in_repo_path);
2094 s->in_repo_path = NULL;
2097 got_ref_list_free(&s->refs);
2101 static const struct got_error *
2102 search_start_log_view(struct tog_view *view)
2104 struct tog_log_view_state *s = &view->state.log;
2106 s->matched_entry = NULL;
2107 s->search_entry = NULL;
2111 static const struct got_error *
2112 search_next_log_view(struct tog_view *view)
2114 const struct got_error *err = NULL;
2115 struct tog_log_view_state *s = &view->state.log;
2116 struct commit_queue_entry *entry;
2118 /* Display progress update in log view. */
2119 show_log_view(view);
2123 if (s->search_entry) {
2125 errcode = pthread_mutex_unlock(&tog_mutex);
2127 return got_error_set_errno(errcode,
2128 "pthread_mutex_unlock");
2129 ch = wgetch(view->window);
2130 errcode = pthread_mutex_lock(&tog_mutex);
2132 return got_error_set_errno(errcode,
2133 "pthread_mutex_lock");
2134 if (ch == KEY_BACKSPACE) {
2135 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2138 if (view->searching == TOG_SEARCH_FORWARD)
2139 entry = TAILQ_NEXT(s->search_entry, entry);
2141 entry = TAILQ_PREV(s->search_entry,
2142 commit_queue_head, entry);
2143 } else if (s->matched_entry) {
2144 if (view->searching == TOG_SEARCH_FORWARD)
2145 entry = TAILQ_NEXT(s->matched_entry, entry);
2147 entry = TAILQ_PREV(s->matched_entry,
2148 commit_queue_head, entry);
2150 if (view->searching == TOG_SEARCH_FORWARD)
2151 entry = TAILQ_FIRST(&s->commits.head);
2153 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2159 if (entry == NULL) {
2160 if (s->thread_args.log_complete ||
2161 view->searching == TOG_SEARCH_BACKWARD) {
2162 view->search_next_done =
2163 (s->matched_entry == NULL ?
2164 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2165 s->search_entry = NULL;
2169 * Poke the log thread for more commits and return,
2170 * allowing the main loop to make progress. Search
2171 * will resume at s->search_entry once we come back.
2173 s->thread_args.commits_needed++;
2174 return trigger_log_thread(view, 0);
2177 err = match_commit(&have_match, entry->id, entry->commit,
2182 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2183 s->matched_entry = entry;
2187 s->search_entry = entry;
2188 if (view->searching == TOG_SEARCH_FORWARD)
2189 entry = TAILQ_NEXT(entry, entry);
2191 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2194 if (s->matched_entry) {
2195 int cur = s->selected_entry->idx;
2196 while (cur < s->matched_entry->idx) {
2197 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2202 while (cur > s->matched_entry->idx) {
2203 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2210 s->search_entry = NULL;
2215 static const struct got_error *
2216 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2217 struct got_repository *repo, const char *head_ref_name,
2218 const char *in_repo_path, int log_branches)
2220 const struct got_error *err = NULL;
2221 struct tog_log_view_state *s = &view->state.log;
2222 struct got_repository *thread_repo = NULL;
2223 struct got_commit_graph *thread_graph = NULL;
2226 SIMPLEQ_INIT(&s->refs);
2228 if (in_repo_path != s->in_repo_path) {
2229 free(s->in_repo_path);
2230 s->in_repo_path = strdup(in_repo_path);
2231 if (s->in_repo_path == NULL)
2232 return got_error_from_errno("strdup");
2235 /* The commit queue only contains commits being displayed. */
2236 TAILQ_INIT(&s->commits.head);
2237 s->commits.ncommits = 0;
2239 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2244 s->head_ref_name = head_ref_name;
2245 s->start_id = got_object_id_dup(start_id);
2246 if (s->start_id == NULL) {
2247 err = got_error_from_errno("got_object_id_dup");
2250 s->log_branches = log_branches;
2252 SIMPLEQ_INIT(&s->colors);
2253 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2254 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2255 get_color_value("TOG_COLOR_COMMIT"));
2258 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2259 get_color_value("TOG_COLOR_AUTHOR"));
2261 free_colors(&s->colors);
2264 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2265 get_color_value("TOG_COLOR_DATE"));
2267 free_colors(&s->colors);
2272 view->show = show_log_view;
2273 view->input = input_log_view;
2274 view->close = close_log_view;
2275 view->search_start = search_start_log_view;
2276 view->search_next = search_next_log_view;
2278 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2281 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2285 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2286 s->repo, NULL, NULL);
2290 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2292 err = got_error_set_errno(errcode, "pthread_cond_init");
2295 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2297 err = got_error_set_errno(errcode, "pthread_cond_init");
2301 s->thread_args.commits_needed = view->nlines;
2302 s->thread_args.graph = thread_graph;
2303 s->thread_args.commits = &s->commits;
2304 s->thread_args.in_repo_path = s->in_repo_path;
2305 s->thread_args.start_id = s->start_id;
2306 s->thread_args.repo = thread_repo;
2307 s->thread_args.log_complete = 0;
2308 s->thread_args.quit = &s->quit;
2309 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2310 s->thread_args.selected_entry = &s->selected_entry;
2311 s->thread_args.searching = &view->searching;
2312 s->thread_args.search_next_done = &view->search_next_done;
2313 s->thread_args.regex = &view->regex;
2316 close_log_view(view);
2320 static const struct got_error *
2321 show_log_view(struct tog_view *view)
2323 const struct got_error *err;
2324 struct tog_log_view_state *s = &view->state.log;
2326 if (s->thread == NULL) {
2327 int errcode = pthread_create(&s->thread, NULL, log_thread,
2330 return got_error_set_errno(errcode, "pthread_create");
2331 if (s->thread_args.commits_needed > 0) {
2332 err = trigger_log_thread(view, 1);
2338 return draw_commits(view);
2341 static const struct got_error *
2342 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2343 struct tog_view **focus_view, struct tog_view *view, int ch)
2345 const struct got_error *err = NULL;
2346 struct tog_log_view_state *s = &view->state.log;
2347 char *parent_path, *in_repo_path = NULL;
2348 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2349 struct tog_view *ref_view = NULL;
2351 struct got_object_id *start_id;
2361 if (s->first_displayed_entry == NULL)
2363 if (s->selected > 0)
2366 log_scroll_up(view, 1);
2370 if (s->first_displayed_entry == NULL)
2372 if (TAILQ_FIRST(&s->commits.head) ==
2373 s->first_displayed_entry) {
2377 log_scroll_up(view, view->nlines - 1);
2383 if (s->first_displayed_entry == NULL)
2385 if (s->selected < MIN(view->nlines - 2,
2386 s->commits.ncommits - 1)) {
2390 err = log_scroll_down(view, 1);
2394 struct commit_queue_entry *first;
2395 first = s->first_displayed_entry;
2398 err = log_scroll_down(view, view->nlines - 1);
2401 if (first == s->first_displayed_entry &&
2402 s->selected < MIN(view->nlines - 2,
2403 s->commits.ncommits - 1)) {
2404 /* can't scroll further down */
2405 s->selected = MIN(view->nlines - 2,
2406 s->commits.ncommits - 1);
2412 if (s->selected > view->nlines - 2)
2413 s->selected = view->nlines - 2;
2414 if (s->selected > s->commits.ncommits - 1)
2415 s->selected = s->commits.ncommits - 1;
2420 if (s->selected_entry == NULL)
2422 if (view_is_parent_view(view))
2423 begin_x = view_split_begin_x(view->begin_x);
2424 err = open_diff_view_for_commit(&diff_view, begin_x,
2425 s->selected_entry->commit, s->selected_entry->id,
2429 if (view_is_parent_view(view)) {
2430 err = view_close_child(view);
2433 err = view_set_child(view, diff_view);
2435 view_close(diff_view);
2438 *focus_view = diff_view;
2439 view->child_focussed = 1;
2441 *new_view = diff_view;
2444 if (s->selected_entry == NULL)
2446 if (view_is_parent_view(view))
2447 begin_x = view_split_begin_x(view->begin_x);
2448 err = browse_commit_tree(&tree_view, begin_x,
2449 s->selected_entry, s->in_repo_path, s->repo);
2452 if (view_is_parent_view(view)) {
2453 err = view_close_child(view);
2456 err = view_set_child(view, tree_view);
2458 view_close(tree_view);
2461 *focus_view = tree_view;
2462 view->child_focussed = 1;
2464 *new_view = tree_view;
2467 if (got_path_cmp(s->in_repo_path, "/",
2468 strlen(s->in_repo_path), 1) == 0)
2470 err = got_path_dirname(&parent_path, s->in_repo_path);
2473 err = stop_log_thread(s);
2478 lv = view_open(view->nlines, view->ncols,
2479 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2482 return got_error_from_errno("view_open");
2484 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2485 parent_path, s->log_branches);
2489 if (view_is_parent_view(view))
2492 view_set_child(view->parent, lv);
2497 err = stop_log_thread(s);
2500 lv = view_open(view->nlines, view->ncols,
2501 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2503 return got_error_from_errno("view_open");
2504 err = got_repo_match_object_id(&start_id, NULL,
2505 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2506 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2511 in_repo_path = strdup(s->in_repo_path);
2512 if (in_repo_path == NULL) {
2515 return got_error_from_errno("strdup");
2517 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2518 in_repo_path, s->log_branches);
2528 s->log_branches = !s->log_branches;
2529 err = stop_log_thread(s);
2532 lv = view_open(view->nlines, view->ncols,
2533 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2535 return got_error_from_errno("view_open");
2536 err = open_log_view(lv, s->start_id, s->repo,
2537 s->head_ref_name, s->in_repo_path, s->log_branches);
2546 if (view_is_parent_view(view))
2547 begin_x = view_split_begin_x(view->begin_x);
2548 ref_view = view_open(view->nlines, view->ncols,
2549 view->begin_y, begin_x, TOG_VIEW_REF);
2550 if (ref_view == NULL)
2551 return got_error_from_errno("view_open");
2552 err = open_ref_view(ref_view, s->repo);
2554 view_close(ref_view);
2557 if (view_is_parent_view(view)) {
2558 err = view_close_child(view);
2561 err = view_set_child(view, ref_view);
2563 view_close(ref_view);
2566 *focus_view = ref_view;
2567 view->child_focussed = 1;
2569 *new_view = ref_view;
2578 static const struct got_error *
2579 apply_unveil(const char *repo_path, const char *worktree_path)
2581 const struct got_error *error;
2584 if (unveil("gmon.out", "rwc") != 0)
2585 return got_error_from_errno2("unveil", "gmon.out");
2587 if (repo_path && unveil(repo_path, "r") != 0)
2588 return got_error_from_errno2("unveil", repo_path);
2590 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2591 return got_error_from_errno2("unveil", worktree_path);
2593 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2594 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2596 error = got_privsep_unveil_exec_helpers();
2600 if (unveil(NULL, NULL) != 0)
2601 return got_error_from_errno("unveil");
2611 halfdelay(1); /* Do fast refresh while initial view is loading. */
2614 intrflush(stdscr, FALSE);
2615 keypad(stdscr, TRUE);
2617 if (getenv("TOG_COLORS") != NULL) {
2619 use_default_colors();
2621 signal(SIGWINCH, tog_sigwinch);
2622 signal(SIGPIPE, tog_sigpipe);
2623 signal(SIGCONT, tog_sigcont);
2626 static const struct got_error *
2627 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2628 struct got_repository *repo, struct got_worktree *worktree)
2630 const struct got_error *err = NULL;
2633 *in_repo_path = strdup("/");
2634 if (*in_repo_path == NULL)
2635 return got_error_from_errno("strdup");
2640 const char *prefix = got_worktree_get_path_prefix(worktree);
2643 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2646 if (asprintf(in_repo_path, "%s%s%s", prefix,
2647 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2649 err = got_error_from_errno("asprintf");
2650 *in_repo_path = NULL;
2654 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2659 static const struct got_error *
2660 cmd_log(int argc, char *argv[])
2662 const struct got_error *error;
2663 struct got_repository *repo = NULL;
2664 struct got_worktree *worktree = NULL;
2665 struct got_object_id *start_id = NULL;
2666 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2667 char *start_commit = NULL, *head_ref_name = NULL;
2668 int ch, log_branches = 0;
2669 struct tog_view *view;
2672 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2677 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2683 start_commit = optarg;
2686 repo_path = realpath(optarg, NULL);
2687 if (repo_path == NULL)
2688 return got_error_from_errno2("realpath",
2703 cwd = getcwd(NULL, 0);
2705 return got_error_from_errno("getcwd");
2707 error = got_worktree_open(&worktree, cwd);
2708 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2711 if (repo_path == NULL) {
2714 strdup(got_worktree_get_repo_path(worktree));
2716 repo_path = strdup(cwd);
2718 if (repo_path == NULL) {
2719 error = got_error_from_errno("strdup");
2723 error = got_repo_open(&repo, repo_path, NULL);
2727 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2734 error = apply_unveil(got_repo_get_path(repo),
2735 worktree ? got_worktree_get_root_path(worktree) : NULL);
2739 if (start_commit == NULL)
2740 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2741 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2742 GOT_OBJ_TYPE_COMMIT, 1, repo);
2744 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2745 GOT_OBJ_TYPE_COMMIT, 1, repo);
2749 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2751 error = got_error_from_errno("view_open");
2755 head_ref_name = strdup(
2756 got_worktree_get_head_ref_name(worktree));
2757 if (head_ref_name == NULL) {
2758 error = got_error_from_errno("strdup");
2762 error = open_log_view(view, start_id, repo, head_ref_name,
2763 in_repo_path, log_branches);
2767 /* Release work tree lock. */
2768 got_worktree_close(worktree);
2771 error = view_loop(view);
2777 free(head_ref_name);
2779 got_repo_close(repo);
2781 got_worktree_close(worktree);
2789 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2790 "[-w] object1 object2\n", getprogname());
2795 parse_next_line(FILE *f, size_t *len)
2800 const char delim[3] = { '\0', '\0', '\0'};
2802 line = fparseln(f, &linelen, &lineno, delim, 0);
2809 match_line(const char *line, regex_t *regex, size_t nmatch,
2810 regmatch_t *regmatch)
2812 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2816 match_color(struct tog_colors *colors, const char *line)
2818 struct tog_color *tc = NULL;
2820 SIMPLEQ_FOREACH(tc, colors, entry) {
2821 if (match_line(line, &tc->regex, 0, NULL))
2828 static const struct got_error *
2829 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2830 WINDOW *window, regmatch_t *regmatch)
2832 const struct got_error *err = NULL;
2839 s = strndup(line, regmatch->rm_so);
2841 return got_error_from_errno("strndup");
2843 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2848 waddwstr(window, wline);
2855 s = strndup(line + regmatch->rm_so,
2856 regmatch->rm_eo - regmatch->rm_so);
2858 err = got_error_from_errno("strndup");
2862 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2867 wattr_on(window, A_STANDOUT, NULL);
2868 waddwstr(window, wline);
2869 wattr_off(window, A_STANDOUT, NULL);
2876 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2877 err = format_line(&wline, &width,
2878 line + regmatch->rm_eo, wlimit, col_tab_align);
2881 waddwstr(window, wline);
2889 static const struct got_error *
2890 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2891 off_t *line_offsets, int selected_line, int max_lines,
2892 int *last_displayed_line, int *eof, const char *header,
2893 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2895 const struct got_error *err;
2898 struct tog_color *tc;
2904 line_offset = line_offsets[first_displayed_line - 1];
2905 if (fseeko(f, line_offset, SEEK_SET) == -1)
2906 return got_error_from_errno("fseek");
2908 werase(view->window);
2911 if (asprintf(&line, "[%d/%d] %s",
2912 first_displayed_line - 1 + selected_line, nlines,
2914 return got_error_from_errno("asprintf");
2915 err = format_line(&wline, &width, line, view->ncols, 0);
2920 if (view_needs_focus_indication(view))
2921 wstandout(view->window);
2922 waddwstr(view->window, wline);
2925 if (view_needs_focus_indication(view))
2926 wstandend(view->window);
2927 if (width <= view->ncols - 1)
2928 waddch(view->window, '\n');
2936 while (max_lines > 0 && nprinted < max_lines) {
2937 line = parse_next_line(f, &len);
2943 tc = match_color(colors, line);
2945 wattr_on(view->window,
2946 COLOR_PAIR(tc->colorpair), NULL);
2947 if (first_displayed_line + nprinted == matched_line &&
2948 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2949 err = add_matched_line(&width, line, view->ncols, 0,
2950 view->window, regmatch);
2956 err = format_line(&wline, &width, line, view->ncols, 0);
2961 waddwstr(view->window, wline);
2966 wattr_off(view->window,
2967 COLOR_PAIR(tc->colorpair), NULL);
2968 if (width <= view->ncols - 1)
2969 waddch(view->window, '\n');
2974 *last_displayed_line = first_displayed_line + (nprinted - 1);
2976 *last_displayed_line = first_displayed_line;
2981 while (nprinted < view->nlines) {
2982 waddch(view->window, '\n');
2986 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2991 wstandout(view->window);
2992 waddwstr(view->window, wline);
2995 wstandend(view->window);
3002 get_datestr(time_t *time, char *datebuf)
3004 struct tm mytm, *tm;
3007 tm = gmtime_r(time, &mytm);
3010 s = asctime_r(tm, datebuf);
3013 p = strchr(s, '\n');
3019 static const struct got_error *
3020 get_changed_paths(struct got_pathlist_head *paths,
3021 struct got_commit_object *commit, struct got_repository *repo)
3023 const struct got_error *err = NULL;
3024 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3025 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3026 struct got_object_qid *qid;
3028 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3030 struct got_commit_object *pcommit;
3031 err = got_object_open_as_commit(&pcommit, repo,
3036 tree_id1 = got_object_commit_get_tree_id(pcommit);
3037 got_object_commit_close(pcommit);
3042 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3047 tree_id2 = got_object_commit_get_tree_id(commit);
3048 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3052 err = got_diff_tree(tree1, tree2, "", "", repo,
3053 got_diff_tree_collect_changed_paths, paths, 0);
3056 got_object_tree_close(tree1);
3058 got_object_tree_close(tree2);
3062 static const struct got_error *
3063 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3067 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3069 return got_error_from_errno("reallocarray");
3071 (*line_offsets)[*nlines] = off;
3076 static const struct got_error *
3077 write_commit_info(off_t **line_offsets, size_t *nlines,
3078 struct got_object_id *commit_id, struct got_reflist_head *refs,
3079 struct got_repository *repo, FILE *outfile)
3081 const struct got_error *err = NULL;
3082 char datebuf[26], *datestr;
3083 struct got_commit_object *commit;
3084 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3085 time_t committer_time;
3086 const char *author, *committer;
3087 char *refs_str = NULL;
3088 struct got_pathlist_head changed_paths;
3089 struct got_pathlist_entry *pe;
3093 TAILQ_INIT(&changed_paths);
3096 err = build_refs_str(&refs_str, refs, commit_id, repo);
3101 err = got_object_open_as_commit(&commit, repo, commit_id);
3105 err = got_object_id_str(&id_str, commit_id);
3107 err = got_error_from_errno("got_object_id_str");
3111 err = add_line_offset(line_offsets, nlines, 0);
3115 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3116 refs_str ? refs_str : "", refs_str ? ")" : "");
3118 err = got_error_from_errno("fprintf");
3122 err = add_line_offset(line_offsets, nlines, outoff);
3126 n = fprintf(outfile, "from: %s\n",
3127 got_object_commit_get_author(commit));
3129 err = got_error_from_errno("fprintf");
3133 err = add_line_offset(line_offsets, nlines, outoff);
3137 committer_time = got_object_commit_get_committer_time(commit);
3138 datestr = get_datestr(&committer_time, datebuf);
3140 n = fprintf(outfile, "date: %s UTC\n", datestr);
3142 err = got_error_from_errno("fprintf");
3146 err = add_line_offset(line_offsets, nlines, outoff);
3150 author = got_object_commit_get_author(commit);
3151 committer = got_object_commit_get_committer(commit);
3152 if (strcmp(author, committer) != 0) {
3153 n = fprintf(outfile, "via: %s\n", committer);
3155 err = got_error_from_errno("fprintf");
3159 err = add_line_offset(line_offsets, nlines, outoff);
3163 err = got_object_commit_get_logmsg(&logmsg, commit);
3167 while ((line = strsep(&s, "\n")) != NULL) {
3168 n = fprintf(outfile, "%s\n", line);
3170 err = got_error_from_errno("fprintf");
3174 err = add_line_offset(line_offsets, nlines, outoff);
3179 err = get_changed_paths(&changed_paths, commit, repo);
3182 TAILQ_FOREACH(pe, &changed_paths, entry) {
3183 struct got_diff_changed_path *cp = pe->data;
3184 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3186 err = got_error_from_errno("fprintf");
3190 err = add_line_offset(line_offsets, nlines, outoff);
3193 free((char *)pe->path);
3197 fputc('\n', outfile);
3199 err = add_line_offset(line_offsets, nlines, outoff);
3201 got_pathlist_free(&changed_paths);
3205 got_object_commit_close(commit);
3207 free(*line_offsets);
3208 *line_offsets = NULL;
3214 static const struct got_error *
3215 create_diff(struct tog_diff_view_state *s)
3217 const struct got_error *err = NULL;
3221 free(s->line_offsets);
3222 s->line_offsets = malloc(sizeof(off_t));
3223 if (s->line_offsets == NULL)
3224 return got_error_from_errno("malloc");
3229 err = got_error_from_errno("got_opentemp");
3232 if (s->f && fclose(s->f) != 0) {
3233 err = got_error_from_errno("fclose");
3239 err = got_object_get_type(&obj_type, s->repo, s->id1);
3241 err = got_object_get_type(&obj_type, s->repo, s->id2);
3246 case GOT_OBJ_TYPE_BLOB:
3247 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3248 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3249 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3251 case GOT_OBJ_TYPE_TREE:
3252 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3253 s->id1, s->id2, "", "", s->diff_context,
3254 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3256 case GOT_OBJ_TYPE_COMMIT: {
3257 const struct got_object_id_queue *parent_ids;
3258 struct got_object_qid *pid;
3259 struct got_commit_object *commit2;
3261 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3264 /* Show commit info if we're diffing to a parent/root commit. */
3265 if (s->id1 == NULL) {
3266 err = write_commit_info(&s->line_offsets, &s->nlines,
3267 s->id2, &s->refs, s->repo, s->f);
3271 parent_ids = got_object_commit_get_parent_ids(commit2);
3272 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3273 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3274 err = write_commit_info(
3275 &s->line_offsets, &s->nlines,
3276 s->id2, &s->refs, s->repo, s->f);
3283 got_object_commit_close(commit2);
3285 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3286 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3287 s->force_text_diff, s->repo, s->f);
3291 err = got_error(GOT_ERR_OBJ_TYPE);
3297 if (s->f && fflush(s->f) != 0 && err == NULL)
3298 err = got_error_from_errno("fflush");
3303 diff_view_indicate_progress(struct tog_view *view)
3305 mvwaddstr(view->window, 0, 0, "diffing...");
3310 static const struct got_error *
3311 search_start_diff_view(struct tog_view *view)
3313 struct tog_diff_view_state *s = &view->state.diff;
3315 s->matched_line = 0;
3319 static const struct got_error *
3320 search_next_diff_view(struct tog_view *view)
3322 struct tog_diff_view_state *s = &view->state.diff;
3325 if (!view->searching) {
3326 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3330 if (s->matched_line) {
3331 if (view->searching == TOG_SEARCH_FORWARD)
3332 lineno = s->matched_line + 1;
3334 lineno = s->matched_line - 1;
3336 if (view->searching == TOG_SEARCH_FORWARD)
3347 if (lineno <= 0 || lineno > s->nlines) {
3348 if (s->matched_line == 0) {
3349 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3354 if (view->searching == TOG_SEARCH_FORWARD)
3360 offset = s->line_offsets[lineno - 1];
3361 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3363 return got_error_from_errno("fseeko");
3366 line = parse_next_line(s->f, &len);
3368 match_line(line, &view->regex, 1, &view->regmatch)) {
3369 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3370 s->matched_line = lineno;
3375 if (view->searching == TOG_SEARCH_FORWARD)
3381 if (s->matched_line) {
3382 s->first_displayed_line = s->matched_line;
3383 s->selected_line = 1;
3389 static const struct got_error *
3390 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3391 struct got_object_id *id2, const char *label1, const char *label2,
3392 int diff_context, int ignore_whitespace, int force_text_diff,
3393 struct tog_view *log_view, struct got_repository *repo)
3395 const struct got_error *err;
3396 struct tog_diff_view_state *s = &view->state.diff;
3398 SIMPLEQ_INIT(&s->refs);
3400 if (id1 != NULL && id2 != NULL) {
3402 err = got_object_get_type(&type1, repo, id1);
3405 err = got_object_get_type(&type2, repo, id2);
3410 return got_error(GOT_ERR_OBJ_TYPE);
3412 s->first_displayed_line = 1;
3413 s->last_displayed_line = view->nlines;
3414 s->selected_line = 1;
3422 s->id1 = got_object_id_dup(id1);
3424 return got_error_from_errno("got_object_id_dup");
3428 s->id2 = got_object_id_dup(id2);
3429 if (s->id2 == NULL) {
3432 return got_error_from_errno("got_object_id_dup");
3435 s->first_displayed_line = 1;
3436 s->last_displayed_line = view->nlines;
3437 s->diff_context = diff_context;
3438 s->ignore_whitespace = ignore_whitespace;
3439 s->force_text_diff = force_text_diff;
3440 s->log_view = log_view;
3443 SIMPLEQ_INIT(&s->colors);
3444 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3445 err = add_color(&s->colors,
3446 "^-", TOG_COLOR_DIFF_MINUS,
3447 get_color_value("TOG_COLOR_DIFF_MINUS"));
3450 err = add_color(&s->colors, "^\\+",
3451 TOG_COLOR_DIFF_PLUS,
3452 get_color_value("TOG_COLOR_DIFF_PLUS"));
3454 free_colors(&s->colors);
3457 err = add_color(&s->colors,
3458 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3459 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3461 free_colors(&s->colors);
3465 err = add_color(&s->colors,
3466 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3467 TOG_COLOR_DIFF_META,
3468 get_color_value("TOG_COLOR_DIFF_META"));
3470 free_colors(&s->colors);
3474 err = add_color(&s->colors,
3475 "^(from|via): ", TOG_COLOR_AUTHOR,
3476 get_color_value("TOG_COLOR_AUTHOR"));
3478 free_colors(&s->colors);
3482 err = add_color(&s->colors,
3483 "^date: ", TOG_COLOR_DATE,
3484 get_color_value("TOG_COLOR_DATE"));
3486 free_colors(&s->colors);
3491 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3497 free_colors(&s->colors);
3501 if (log_view && view_is_splitscreen(view))
3502 show_log_view(log_view); /* draw vborder */
3503 diff_view_indicate_progress(view);
3505 s->line_offsets = NULL;
3507 err = create_diff(s);
3513 free_colors(&s->colors);
3514 got_ref_list_free(&s->refs);
3518 view->show = show_diff_view;
3519 view->input = input_diff_view;
3520 view->close = close_diff_view;
3521 view->search_start = search_start_diff_view;
3522 view->search_next = search_next_diff_view;
3527 static const struct got_error *
3528 close_diff_view(struct tog_view *view)
3530 const struct got_error *err = NULL;
3531 struct tog_diff_view_state *s = &view->state.diff;
3537 if (s->f && fclose(s->f) == EOF)
3538 err = got_error_from_errno("fclose");
3539 free_colors(&s->colors);
3540 free(s->line_offsets);
3541 s->line_offsets = NULL;
3543 got_ref_list_free(&s->refs);
3547 static const struct got_error *
3548 show_diff_view(struct tog_view *view)
3550 const struct got_error *err;
3551 struct tog_diff_view_state *s = &view->state.diff;
3552 char *id_str1 = NULL, *id_str2, *header;
3553 const char *label1, *label2;
3556 err = got_object_id_str(&id_str1, s->id1);
3559 label1 = s->label1 ? : id_str1;
3561 label1 = "/dev/null";
3563 err = got_object_id_str(&id_str2, s->id2);
3566 label2 = s->label2 ? : id_str2;
3568 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3569 err = got_error_from_errno("asprintf");
3577 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3578 s->line_offsets, s->selected_line, view->nlines,
3579 &s->last_displayed_line, &s->eof, header, &s->colors,
3580 s->matched_line, &view->regmatch);
3583 static const struct got_error *
3584 set_selected_commit(struct tog_diff_view_state *s,
3585 struct commit_queue_entry *entry)
3587 const struct got_error *err;
3588 const struct got_object_id_queue *parent_ids;
3589 struct got_commit_object *selected_commit;
3590 struct got_object_qid *pid;
3593 s->id2 = got_object_id_dup(entry->id);
3595 return got_error_from_errno("got_object_id_dup");
3597 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3600 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3602 pid = SIMPLEQ_FIRST(parent_ids);
3603 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3604 got_object_commit_close(selected_commit);
3608 static const struct got_error *
3609 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3610 struct tog_view **focus_view, struct tog_view *view, int ch)
3612 const struct got_error *err = NULL;
3613 struct tog_diff_view_state *s = &view->state.diff;
3614 struct tog_log_view_state *ls;
3615 struct commit_queue_entry *entry;
3622 s->force_text_diff = !s->force_text_diff;
3624 s->ignore_whitespace = !s->ignore_whitespace;
3625 wclear(view->window);
3626 s->first_displayed_line = 1;
3627 s->last_displayed_line = view->nlines;
3628 diff_view_indicate_progress(view);
3629 err = create_diff(s);
3633 if (s->first_displayed_line > 1)
3634 s->first_displayed_line--;
3638 if (s->first_displayed_line == 1)
3641 while (i++ < view->nlines - 1 &&
3642 s->first_displayed_line > 1)
3643 s->first_displayed_line--;
3648 s->first_displayed_line++;
3656 while (!s->eof && i++ < view->nlines - 1) {
3658 line = parse_next_line(s->f, NULL);
3659 s->first_displayed_line++;
3665 if (s->diff_context > 0) {
3667 diff_view_indicate_progress(view);
3668 err = create_diff(s);
3669 if (s->first_displayed_line + view->nlines - 1 >
3671 s->first_displayed_line = 1;
3672 s->last_displayed_line = view->nlines;
3677 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3679 diff_view_indicate_progress(view);
3680 err = create_diff(s);
3685 if (s->log_view == NULL)
3687 ls = &s->log_view->state.log;
3688 entry = TAILQ_PREV(ls->selected_entry,
3689 commit_queue_head, entry);
3693 err = input_log_view(NULL, NULL, NULL, s->log_view,
3698 err = set_selected_commit(s, entry);
3702 s->first_displayed_line = 1;
3703 s->last_displayed_line = view->nlines;
3705 diff_view_indicate_progress(view);
3706 err = create_diff(s);
3710 if (s->log_view == NULL)
3712 ls = &s->log_view->state.log;
3714 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3715 ls->thread_args.commits_needed++;
3716 err = trigger_log_thread(s->log_view, 1);
3720 err = input_log_view(NULL, NULL, NULL, s->log_view,
3725 entry = TAILQ_NEXT(ls->selected_entry, entry);
3729 err = set_selected_commit(s, entry);
3733 s->first_displayed_line = 1;
3734 s->last_displayed_line = view->nlines;
3736 diff_view_indicate_progress(view);
3737 err = create_diff(s);
3746 static const struct got_error *
3747 cmd_diff(int argc, char *argv[])
3749 const struct got_error *error = NULL;
3750 struct got_repository *repo = NULL;
3751 struct got_worktree *worktree = NULL;
3752 struct got_object_id *id1 = NULL, *id2 = NULL;
3753 char *repo_path = NULL, *cwd = NULL;
3754 char *id_str1 = NULL, *id_str2 = NULL;
3755 char *label1 = NULL, *label2 = NULL;
3756 int diff_context = 3, ignore_whitespace = 0;
3757 int ch, force_text_diff = 0;
3759 struct tog_view *view;
3762 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3766 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3769 force_text_diff = 1;
3772 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3775 err(1, "-C option %s", errstr);
3778 repo_path = realpath(optarg, NULL);
3779 if (repo_path == NULL)
3780 return got_error_from_errno2("realpath",
3782 got_path_strip_trailing_slashes(repo_path);
3785 ignore_whitespace = 1;
3797 usage_diff(); /* TODO show local worktree changes */
3798 } else if (argc == 2) {
3804 cwd = getcwd(NULL, 0);
3806 return got_error_from_errno("getcwd");
3808 error = got_worktree_open(&worktree, cwd);
3809 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3812 if (repo_path == NULL) {
3815 strdup(got_worktree_get_repo_path(worktree));
3817 repo_path = strdup(cwd);
3819 if (repo_path == NULL) {
3820 error = got_error_from_errno("strdup");
3824 error = got_repo_open(&repo, repo_path, NULL);
3830 error = apply_unveil(got_repo_get_path(repo), NULL);
3834 error = got_repo_match_object_id(&id1, &label1, id_str1,
3835 GOT_OBJ_TYPE_ANY, 1, repo);
3839 error = got_repo_match_object_id(&id2, &label2, id_str2,
3840 GOT_OBJ_TYPE_ANY, 1, repo);
3844 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3846 error = got_error_from_errno("view_open");
3849 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3850 ignore_whitespace, force_text_diff, NULL, repo);
3853 error = view_loop(view);
3860 got_repo_close(repo);
3862 got_worktree_close(worktree);
3870 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3875 struct tog_blame_line {
3877 struct got_object_id *id;
3880 static const struct got_error *
3881 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3882 const char *path, struct tog_blame_line *lines, int nlines,
3883 int blame_complete, int selected_line, int *first_displayed_line,
3884 int *last_displayed_line, int *eof, int max_lines,
3885 struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3887 const struct got_error *err;
3888 int lineno = 0, nprinted = 0;
3893 struct tog_blame_line *blame_line;
3894 struct got_object_id *prev_id = NULL;
3896 struct tog_color *tc;
3898 err = got_object_id_str(&id_str, id);
3903 werase(view->window);
3905 if (asprintf(&line, "commit %s", id_str) == -1) {
3906 err = got_error_from_errno("asprintf");
3911 err = format_line(&wline, &width, line, view->ncols, 0);
3916 if (view_needs_focus_indication(view))
3917 wstandout(view->window);
3918 tc = get_color(colors, TOG_COLOR_COMMIT);
3920 wattr_on(view->window,
3921 COLOR_PAIR(tc->colorpair), NULL);
3922 waddwstr(view->window, wline);
3924 wattr_off(view->window,
3925 COLOR_PAIR(tc->colorpair), NULL);
3926 if (view_needs_focus_indication(view))
3927 wstandend(view->window);
3930 if (width < view->ncols - 1)
3931 waddch(view->window, '\n');
3933 if (asprintf(&line, "[%d/%d] %s%s",
3934 *first_displayed_line - 1 + selected_line, nlines,
3935 blame_complete ? "" : "annotating... ", path) == -1) {
3937 return got_error_from_errno("asprintf");
3940 err = format_line(&wline, &width, line, view->ncols, 0);
3945 waddwstr(view->window, wline);
3948 if (width < view->ncols - 1)
3949 waddch(view->window, '\n');
3952 while (nprinted < max_lines - 2) {
3953 line = parse_next_line(f, &len);
3958 if (++lineno < *first_displayed_line) {
3963 if (view->focussed && nprinted == selected_line - 1)
3964 wstandout(view->window);
3967 blame_line = &lines[lineno - 1];
3968 if (blame_line->annotated && prev_id &&
3969 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3971 nprinted == selected_line - 1)) {
3972 waddstr(view->window, " ");
3973 } else if (blame_line->annotated) {
3975 err = got_object_id_str(&id_str, blame_line->id);
3980 tc = get_color(colors, TOG_COLOR_COMMIT);
3982 wattr_on(view->window,
3983 COLOR_PAIR(tc->colorpair), NULL);
3984 wprintw(view->window, "%.8s", id_str);
3986 wattr_off(view->window,
3987 COLOR_PAIR(tc->colorpair), NULL);
3989 prev_id = blame_line->id;
3991 waddstr(view->window, "........");
3995 waddstr(view->window, "........");
3999 if (view->focussed && nprinted == selected_line - 1)
4000 wstandend(view->window);
4001 waddstr(view->window, " ");
4003 if (view->ncols <= 9) {
4005 wline = wcsdup(L"");
4006 if (wline == NULL) {
4007 err = got_error_from_errno("wcsdup");
4011 } else if (*first_displayed_line + nprinted == matched_line &&
4012 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4013 err = add_matched_line(&width, line, view->ncols - 9, 9,
4014 view->window, regmatch);
4021 err = format_line(&wline, &width, line,
4022 view->ncols - 9, 9);
4023 waddwstr(view->window, wline);
4029 if (width <= view->ncols - 1)
4030 waddch(view->window, '\n');
4031 if (++nprinted == 1)
4032 *first_displayed_line = lineno;
4035 *last_displayed_line = lineno;
4042 static const struct got_error *
4043 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4045 const struct got_error *err = NULL;
4046 struct tog_blame_cb_args *a = arg;
4047 struct tog_blame_line *line;
4050 if (nlines != a->nlines ||
4051 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4052 return got_error(GOT_ERR_RANGE);
4054 errcode = pthread_mutex_lock(&tog_mutex);
4056 return got_error_set_errno(errcode, "pthread_mutex_lock");
4058 if (*a->quit) { /* user has quit the blame view */
4059 err = got_error(GOT_ERR_ITER_COMPLETED);
4064 goto done; /* no change in this commit */
4066 line = &a->lines[lineno - 1];
4067 if (line->annotated)
4070 line->id = got_object_id_dup(id);
4071 if (line->id == NULL) {
4072 err = got_error_from_errno("got_object_id_dup");
4075 line->annotated = 1;
4077 errcode = pthread_mutex_unlock(&tog_mutex);
4079 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4084 blame_thread(void *arg)
4086 const struct got_error *err;
4087 struct tog_blame_thread_args *ta = arg;
4088 struct tog_blame_cb_args *a = ta->cb_args;
4091 err = block_signals_used_by_main_thread();
4095 err = got_blame(ta->path, a->commit_id, ta->repo,
4096 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4097 if (err && err->code == GOT_ERR_CANCELLED)
4100 errcode = pthread_mutex_lock(&tog_mutex);
4102 return (void *)got_error_set_errno(errcode,
4103 "pthread_mutex_lock");
4105 got_repo_close(ta->repo);
4109 errcode = pthread_mutex_unlock(&tog_mutex);
4110 if (errcode && err == NULL)
4111 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4116 static struct got_object_id *
4117 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4118 int first_displayed_line, int selected_line)
4120 struct tog_blame_line *line;
4125 line = &lines[first_displayed_line - 1 + selected_line - 1];
4126 if (!line->annotated)
4132 static const struct got_error *
4133 stop_blame(struct tog_blame *blame)
4135 const struct got_error *err = NULL;
4138 if (blame->thread) {
4140 errcode = pthread_mutex_unlock(&tog_mutex);
4142 return got_error_set_errno(errcode,
4143 "pthread_mutex_unlock");
4144 errcode = pthread_join(blame->thread, (void **)&err);
4146 return got_error_set_errno(errcode, "pthread_join");
4147 errcode = pthread_mutex_lock(&tog_mutex);
4149 return got_error_set_errno(errcode,
4150 "pthread_mutex_lock");
4151 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4153 blame->thread = NULL;
4155 if (blame->thread_args.repo) {
4156 got_repo_close(blame->thread_args.repo);
4157 blame->thread_args.repo = NULL;
4160 if (fclose(blame->f) != 0 && err == NULL)
4161 err = got_error_from_errno("fclose");
4165 for (i = 0; i < blame->nlines; i++)
4166 free(blame->lines[i].id);
4168 blame->lines = NULL;
4170 free(blame->cb_args.commit_id);
4171 blame->cb_args.commit_id = NULL;
4176 static const struct got_error *
4177 cancel_blame_view(void *arg)
4179 const struct got_error *err = NULL;
4183 errcode = pthread_mutex_lock(&tog_mutex);
4185 return got_error_set_errno(errcode,
4186 "pthread_mutex_unlock");
4189 err = got_error(GOT_ERR_CANCELLED);
4191 errcode = pthread_mutex_unlock(&tog_mutex);
4193 return got_error_set_errno(errcode,
4194 "pthread_mutex_lock");
4199 static const struct got_error *
4200 run_blame(struct tog_view *view)
4202 struct tog_blame_view_state *s = &view->state.blame;
4203 struct tog_blame *blame = &s->blame;
4204 const struct got_error *err = NULL;
4205 struct got_blob_object *blob = NULL;
4206 struct got_repository *thread_repo = NULL;
4207 struct got_object_id *obj_id = NULL;
4210 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4215 err = got_object_get_type(&obj_type, s->repo, obj_id);
4219 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4220 err = got_error(GOT_ERR_OBJ_TYPE);
4224 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4227 blame->f = got_opentemp();
4228 if (blame->f == NULL) {
4229 err = got_error_from_errno("got_opentemp");
4232 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4233 &blame->line_offsets, blame->f, blob);
4234 if (err || blame->nlines == 0)
4237 /* Don't include \n at EOF in the blame line count. */
4238 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4241 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4242 if (blame->lines == NULL) {
4243 err = got_error_from_errno("calloc");
4247 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4251 blame->cb_args.view = view;
4252 blame->cb_args.lines = blame->lines;
4253 blame->cb_args.nlines = blame->nlines;
4254 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4255 if (blame->cb_args.commit_id == NULL) {
4256 err = got_error_from_errno("got_object_id_dup");
4259 blame->cb_args.quit = &s->done;
4261 blame->thread_args.path = s->path;
4262 blame->thread_args.repo = thread_repo;
4263 blame->thread_args.cb_args = &blame->cb_args;
4264 blame->thread_args.complete = &s->blame_complete;
4265 blame->thread_args.cancel_cb = cancel_blame_view;
4266 blame->thread_args.cancel_arg = &s->done;
4267 s->blame_complete = 0;
4271 got_object_blob_close(blob);
4278 static const struct got_error *
4279 open_blame_view(struct tog_view *view, char *path,
4280 struct got_object_id *commit_id, struct got_repository *repo)
4282 const struct got_error *err = NULL;
4283 struct tog_blame_view_state *s = &view->state.blame;
4285 SIMPLEQ_INIT(&s->blamed_commits);
4287 s->path = strdup(path);
4288 if (s->path == NULL)
4289 return got_error_from_errno("strdup");
4291 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4297 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4298 s->first_displayed_line = 1;
4299 s->last_displayed_line = view->nlines;
4300 s->selected_line = 1;
4301 s->blame_complete = 0;
4303 s->commit_id = commit_id;
4304 memset(&s->blame, 0, sizeof(s->blame));
4306 SIMPLEQ_INIT(&s->colors);
4307 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4308 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4309 get_color_value("TOG_COLOR_COMMIT"));
4314 view->show = show_blame_view;
4315 view->input = input_blame_view;
4316 view->close = close_blame_view;
4317 view->search_start = search_start_blame_view;
4318 view->search_next = search_next_blame_view;
4320 return run_blame(view);
4323 static const struct got_error *
4324 close_blame_view(struct tog_view *view)
4326 const struct got_error *err = NULL;
4327 struct tog_blame_view_state *s = &view->state.blame;
4329 if (s->blame.thread)
4330 err = stop_blame(&s->blame);
4332 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4333 struct got_object_qid *blamed_commit;
4334 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4335 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4336 got_object_qid_free(blamed_commit);
4340 free_colors(&s->colors);
4345 static const struct got_error *
4346 search_start_blame_view(struct tog_view *view)
4348 struct tog_blame_view_state *s = &view->state.blame;
4350 s->matched_line = 0;
4354 static const struct got_error *
4355 search_next_blame_view(struct tog_view *view)
4357 struct tog_blame_view_state *s = &view->state.blame;
4360 if (!view->searching) {
4361 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4365 if (s->matched_line) {
4366 if (view->searching == TOG_SEARCH_FORWARD)
4367 lineno = s->matched_line + 1;
4369 lineno = s->matched_line - 1;
4371 if (view->searching == TOG_SEARCH_FORWARD)
4374 lineno = s->blame.nlines;
4382 if (lineno <= 0 || lineno > s->blame.nlines) {
4383 if (s->matched_line == 0) {
4384 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4389 if (view->searching == TOG_SEARCH_FORWARD)
4392 lineno = s->blame.nlines;
4395 offset = s->blame.line_offsets[lineno - 1];
4396 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4398 return got_error_from_errno("fseeko");
4401 line = parse_next_line(s->blame.f, &len);
4403 match_line(line, &view->regex, 1, &view->regmatch)) {
4404 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4405 s->matched_line = lineno;
4410 if (view->searching == TOG_SEARCH_FORWARD)
4416 if (s->matched_line) {
4417 s->first_displayed_line = s->matched_line;
4418 s->selected_line = 1;
4424 static const struct got_error *
4425 show_blame_view(struct tog_view *view)
4427 const struct got_error *err = NULL;
4428 struct tog_blame_view_state *s = &view->state.blame;
4431 if (s->blame.thread == NULL) {
4432 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4433 &s->blame.thread_args);
4435 return got_error_set_errno(errcode, "pthread_create");
4437 halfdelay(1); /* fast refresh while annotating */
4440 if (s->blame_complete)
4441 halfdelay(10); /* disable fast refresh */
4443 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4444 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4445 s->selected_line, &s->first_displayed_line,
4446 &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4447 s->matched_line, &view->regmatch);
4453 static const struct got_error *
4454 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4455 struct tog_view **focus_view, struct tog_view *view, int ch)
4457 const struct got_error *err = NULL, *thread_err = NULL;
4458 struct tog_view *diff_view;
4459 struct tog_blame_view_state *s = &view->state.blame;
4468 if (s->selected_line > 1)
4470 else if (s->selected_line == 1 &&
4471 s->first_displayed_line > 1)
4472 s->first_displayed_line--;
4476 if (s->first_displayed_line == 1) {
4477 s->selected_line = 1;
4480 if (s->first_displayed_line > view->nlines - 2)
4481 s->first_displayed_line -=
4484 s->first_displayed_line = 1;
4488 if (s->selected_line < view->nlines - 2 &&
4489 s->first_displayed_line +
4490 s->selected_line <= s->blame.nlines)
4492 else if (s->last_displayed_line <
4494 s->first_displayed_line++;
4498 struct got_object_id *id = NULL;
4499 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4500 s->first_displayed_line, s->selected_line);
4504 struct got_commit_object *commit;
4505 struct got_object_qid *pid;
4506 struct got_object_id *blob_id = NULL;
4508 err = got_object_open_as_commit(&commit,
4512 pid = SIMPLEQ_FIRST(
4513 got_object_commit_get_parent_ids(commit));
4515 got_object_commit_close(commit);
4518 /* Check if path history ends here. */
4519 err = got_object_id_by_path(&blob_id, s->repo,
4522 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4524 got_object_commit_close(commit);
4527 err = got_object_get_type(&obj_type, s->repo,
4530 /* Can't blame non-blob type objects. */
4531 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4532 got_object_commit_close(commit);
4535 err = got_object_qid_alloc(&s->blamed_commit,
4537 got_object_commit_close(commit);
4539 if (got_object_id_cmp(id,
4540 s->blamed_commit->id) == 0)
4542 err = got_object_qid_alloc(&s->blamed_commit,
4548 thread_err = stop_blame(&s->blame);
4552 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4553 s->blamed_commit, entry);
4554 err = run_blame(view);
4560 struct got_object_qid *first;
4561 first = SIMPLEQ_FIRST(&s->blamed_commits);
4562 if (!got_object_id_cmp(first->id, s->commit_id))
4565 thread_err = stop_blame(&s->blame);
4569 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4570 got_object_qid_free(s->blamed_commit);
4572 SIMPLEQ_FIRST(&s->blamed_commits);
4573 err = run_blame(view);
4580 struct got_object_id *id = NULL;
4581 struct got_object_qid *pid;
4582 struct got_commit_object *commit = NULL;
4583 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4584 s->first_displayed_line, s->selected_line);
4587 err = got_object_open_as_commit(&commit, s->repo, id);
4590 pid = SIMPLEQ_FIRST(
4591 got_object_commit_get_parent_ids(commit));
4592 if (view_is_parent_view(view))
4593 begin_x = view_split_begin_x(view->begin_x);
4594 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4595 if (diff_view == NULL) {
4596 got_object_commit_close(commit);
4597 err = got_error_from_errno("view_open");
4600 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4601 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4602 got_object_commit_close(commit);
4604 view_close(diff_view);
4607 if (view_is_parent_view(view)) {
4608 err = view_close_child(view);
4611 err = view_set_child(view, diff_view);
4613 view_close(diff_view);
4616 *focus_view = diff_view;
4617 view->child_focussed = 1;
4619 *new_view = diff_view;
4627 if (s->last_displayed_line >= s->blame.nlines &&
4628 s->selected_line >= MIN(s->blame.nlines,
4629 view->nlines - 2)) {
4632 if (s->last_displayed_line >= s->blame.nlines &&
4633 s->selected_line < view->nlines - 2) {
4634 s->selected_line = MIN(s->blame.nlines,
4638 if (s->last_displayed_line + view->nlines - 2
4640 s->first_displayed_line +=
4643 s->first_displayed_line =
4648 if (s->selected_line > view->nlines - 2) {
4649 s->selected_line = MIN(s->blame.nlines,
4656 return thread_err ? thread_err : err;
4659 static const struct got_error *
4660 cmd_blame(int argc, char *argv[])
4662 const struct got_error *error;
4663 struct got_repository *repo = NULL;
4664 struct got_worktree *worktree = NULL;
4665 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4666 char *link_target = NULL;
4667 struct got_object_id *commit_id = NULL;
4668 char *commit_id_str = NULL;
4670 struct tog_view *view;
4673 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4678 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4681 commit_id_str = optarg;
4684 repo_path = realpath(optarg, NULL);
4685 if (repo_path == NULL)
4686 return got_error_from_errno2("realpath",
4701 cwd = getcwd(NULL, 0);
4703 return got_error_from_errno("getcwd");
4705 error = got_worktree_open(&worktree, cwd);
4706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4709 if (repo_path == NULL) {
4712 strdup(got_worktree_get_repo_path(worktree));
4714 repo_path = strdup(cwd);
4716 if (repo_path == NULL) {
4717 error = got_error_from_errno("strdup");
4721 error = got_repo_open(&repo, repo_path, NULL);
4725 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4732 error = apply_unveil(got_repo_get_path(repo), NULL);
4736 if (commit_id_str == NULL) {
4737 struct got_reference *head_ref;
4738 error = got_ref_open(&head_ref, repo, worktree ?
4739 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4742 error = got_ref_resolve(&commit_id, repo, head_ref);
4743 got_ref_close(head_ref);
4745 error = got_repo_match_object_id(&commit_id, NULL,
4746 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4751 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4753 error = got_error_from_errno("view_open");
4757 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4762 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4767 /* Release work tree lock. */
4768 got_worktree_close(worktree);
4771 error = view_loop(view);
4779 got_worktree_close(worktree);
4781 got_repo_close(repo);
4785 static const struct got_error *
4786 draw_tree_entries(struct tog_view *view,
4787 struct got_tree_entry **first_displayed_entry,
4788 struct got_tree_entry **last_displayed_entry,
4789 struct got_tree_entry **selected_entry, int *ndisplayed,
4790 const char *label, int show_ids, const char *parent_path,
4791 struct got_tree_object *tree, int selected, int limit,
4792 int isroot, struct tog_colors *colors, struct got_repository *repo)
4794 const struct got_error *err = NULL;
4795 struct got_tree_entry *te;
4797 struct tog_color *tc;
4798 int width, n, i, nentries;
4802 werase(view->window);
4807 err = format_line(&wline, &width, label, view->ncols, 0);
4810 if (view_needs_focus_indication(view))
4811 wstandout(view->window);
4812 tc = get_color(colors, TOG_COLOR_COMMIT);
4814 wattr_on(view->window,
4815 COLOR_PAIR(tc->colorpair), NULL);
4816 waddwstr(view->window, wline);
4818 wattr_off(view->window,
4819 COLOR_PAIR(tc->colorpair), NULL);
4820 if (view_needs_focus_indication(view))
4821 wstandend(view->window);
4824 if (width < view->ncols - 1)
4825 waddch(view->window, '\n');
4828 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4831 waddwstr(view->window, wline);
4834 if (width < view->ncols - 1)
4835 waddch(view->window, '\n');
4838 waddch(view->window, '\n');
4842 if (*first_displayed_entry == NULL) {
4843 te = got_object_tree_get_first_entry(tree);
4844 if (selected == 0) {
4846 wstandout(view->window);
4847 *selected_entry = NULL;
4849 waddstr(view->window, " ..\n"); /* parent directory */
4850 if (selected == 0 && view->focussed)
4851 wstandend(view->window);
4858 te = *first_displayed_entry;
4861 nentries = got_object_tree_get_nentries(tree);
4862 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4863 char *line = NULL, *id_str = NULL, *link_target = NULL;
4864 const char *modestr = "";
4867 te = got_object_tree_get_entry(tree, i);
4868 mode = got_tree_entry_get_mode(te);
4871 err = got_object_id_str(&id_str,
4872 got_tree_entry_get_id(te));
4874 return got_error_from_errno(
4875 "got_object_id_str");
4877 if (got_object_tree_entry_is_submodule(te))
4879 else if (S_ISLNK(mode)) {
4882 err = got_tree_entry_get_symlink_target(&link_target,
4888 for (i = 0; i < strlen(link_target); i++) {
4889 if (!isprint((unsigned char)link_target[i]))
4890 link_target[i] = '?';
4894 else if (S_ISDIR(mode))
4896 else if (mode & S_IXUSR)
4898 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4899 got_tree_entry_get_name(te), modestr,
4900 link_target ? " -> ": "",
4901 link_target ? link_target : "") == -1) {
4904 return got_error_from_errno("asprintf");
4908 err = format_line(&wline, &width, line, view->ncols, 0);
4913 if (n == selected) {
4915 wstandout(view->window);
4916 *selected_entry = te;
4918 tc = match_color(colors, line);
4920 wattr_on(view->window,
4921 COLOR_PAIR(tc->colorpair), NULL);
4922 waddwstr(view->window, wline);
4924 wattr_off(view->window,
4925 COLOR_PAIR(tc->colorpair), NULL);
4926 if (width < view->ncols - 1)
4927 waddch(view->window, '\n');
4928 if (n == selected && view->focussed)
4929 wstandend(view->window);
4935 *last_displayed_entry = te;
4944 tree_scroll_up(struct tog_view *view, int maxscroll)
4946 struct tog_tree_view_state *s = &view->state.tree;
4947 struct got_tree_entry *te;
4948 int isroot = s->tree == s->root;
4951 if (s->first_displayed_entry == NULL)
4954 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4955 while (i++ < maxscroll) {
4958 s->first_displayed_entry = NULL;
4961 s->first_displayed_entry = te;
4962 te = got_tree_entry_get_prev(s->tree, te);
4967 tree_scroll_down(struct tog_view *view, int maxscroll)
4969 struct tog_tree_view_state *s = &view->state.tree;
4970 struct got_tree_entry *next, *last;
4973 if (s->first_displayed_entry)
4974 next = got_tree_entry_get_next(s->tree,
4975 s->first_displayed_entry);
4977 next = got_object_tree_get_first_entry(s->tree);
4979 last = s->last_displayed_entry;
4980 while (next && last && n++ < maxscroll) {
4981 last = got_tree_entry_get_next(s->tree, last);
4983 s->first_displayed_entry = next;
4984 next = got_tree_entry_get_next(s->tree, next);
4989 static const struct got_error *
4990 tree_entry_path(char **path, struct tog_parent_trees *parents,
4991 struct got_tree_entry *te)
4993 const struct got_error *err = NULL;
4994 struct tog_parent_tree *pt;
4995 size_t len = 2; /* for leading slash and NUL */
4997 TAILQ_FOREACH(pt, parents, entry)
4998 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5001 len += strlen(got_tree_entry_get_name(te));
5003 *path = calloc(1, len);
5005 return got_error_from_errno("calloc");
5008 pt = TAILQ_LAST(parents, tog_parent_trees);
5010 const char *name = got_tree_entry_get_name(pt->selected_entry);
5011 if (strlcat(*path, name, len) >= len) {
5012 err = got_error(GOT_ERR_NO_SPACE);
5015 if (strlcat(*path, "/", len) >= len) {
5016 err = got_error(GOT_ERR_NO_SPACE);
5019 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5022 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5023 err = got_error(GOT_ERR_NO_SPACE);
5035 static const struct got_error *
5036 blame_tree_entry(struct tog_view **new_view, int begin_x,
5037 struct got_tree_entry *te, struct tog_parent_trees *parents,
5038 struct got_object_id *commit_id, struct got_repository *repo)
5040 const struct got_error *err = NULL;
5042 struct tog_view *blame_view;
5046 err = tree_entry_path(&path, parents, te);
5050 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5051 if (blame_view == NULL) {
5052 err = got_error_from_errno("view_open");
5056 err = open_blame_view(blame_view, path, commit_id, repo);
5058 if (err->code == GOT_ERR_CANCELLED)
5060 view_close(blame_view);
5062 *new_view = blame_view;
5068 static const struct got_error *
5069 log_tree_entry(struct tog_view **new_view, int begin_x,
5070 struct got_tree_entry *te, struct tog_parent_trees *parents,
5071 struct got_object_id *commit_id, struct got_repository *repo)
5073 struct tog_view *log_view;
5074 const struct got_error *err = NULL;
5079 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5080 if (log_view == NULL)
5081 return got_error_from_errno("view_open");
5083 err = tree_entry_path(&path, parents, te);
5087 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5089 view_close(log_view);
5091 *new_view = log_view;
5096 static const struct got_error *
5097 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5098 struct got_object_id *commit_id, struct got_repository *repo)
5100 const struct got_error *err = NULL;
5101 char *commit_id_str = NULL;
5102 struct tog_tree_view_state *s = &view->state.tree;
5104 TAILQ_INIT(&s->parents);
5106 err = got_object_id_str(&commit_id_str, commit_id);
5110 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5111 err = got_error_from_errno("asprintf");
5115 s->root = s->tree = root;
5116 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5117 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5118 s->commit_id = got_object_id_dup(commit_id);
5119 if (s->commit_id == NULL) {
5120 err = got_error_from_errno("got_object_id_dup");
5125 SIMPLEQ_INIT(&s->colors);
5127 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5128 err = add_color(&s->colors, "\\$$",
5129 TOG_COLOR_TREE_SUBMODULE,
5130 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5133 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5134 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5136 free_colors(&s->colors);
5139 err = add_color(&s->colors, "/$",
5140 TOG_COLOR_TREE_DIRECTORY,
5141 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5143 free_colors(&s->colors);
5147 err = add_color(&s->colors, "\\*$",
5148 TOG_COLOR_TREE_EXECUTABLE,
5149 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5151 free_colors(&s->colors);
5155 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5156 get_color_value("TOG_COLOR_COMMIT"));
5158 free_colors(&s->colors);
5163 view->show = show_tree_view;
5164 view->input = input_tree_view;
5165 view->close = close_tree_view;
5166 view->search_start = search_start_tree_view;
5167 view->search_next = search_next_tree_view;
5169 free(commit_id_str);
5171 free(s->tree_label);
5172 s->tree_label = NULL;
5177 static const struct got_error *
5178 close_tree_view(struct tog_view *view)
5180 struct tog_tree_view_state *s = &view->state.tree;
5182 free_colors(&s->colors);
5183 free(s->tree_label);
5184 s->tree_label = NULL;
5186 s->commit_id = NULL;
5187 while (!TAILQ_EMPTY(&s->parents)) {
5188 struct tog_parent_tree *parent;
5189 parent = TAILQ_FIRST(&s->parents);
5190 TAILQ_REMOVE(&s->parents, parent, entry);
5194 if (s->tree != s->root)
5195 got_object_tree_close(s->tree);
5196 got_object_tree_close(s->root);
5200 static const struct got_error *
5201 search_start_tree_view(struct tog_view *view)
5203 struct tog_tree_view_state *s = &view->state.tree;
5205 s->matched_entry = NULL;
5210 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5212 regmatch_t regmatch;
5214 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5218 static const struct got_error *
5219 search_next_tree_view(struct tog_view *view)
5221 struct tog_tree_view_state *s = &view->state.tree;
5222 struct got_tree_entry *te = NULL;
5224 if (!view->searching) {
5225 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5229 if (s->matched_entry) {
5230 if (view->searching == TOG_SEARCH_FORWARD) {
5231 if (s->selected_entry)
5232 te = got_tree_entry_get_next(s->tree,
5235 te = got_object_tree_get_first_entry(s->tree);
5237 if (s->selected_entry == NULL)
5238 te = got_object_tree_get_last_entry(s->tree);
5240 te = got_tree_entry_get_prev(s->tree,
5244 if (view->searching == TOG_SEARCH_FORWARD)
5245 te = got_object_tree_get_first_entry(s->tree);
5247 te = got_object_tree_get_last_entry(s->tree);
5252 if (s->matched_entry == NULL) {
5253 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5256 if (view->searching == TOG_SEARCH_FORWARD)
5257 te = got_object_tree_get_first_entry(s->tree);
5259 te = got_object_tree_get_last_entry(s->tree);
5262 if (match_tree_entry(te, &view->regex)) {
5263 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5264 s->matched_entry = te;
5268 if (view->searching == TOG_SEARCH_FORWARD)
5269 te = got_tree_entry_get_next(s->tree, te);
5271 te = got_tree_entry_get_prev(s->tree, te);
5274 if (s->matched_entry) {
5275 s->first_displayed_entry = s->matched_entry;
5282 static const struct got_error *
5283 show_tree_view(struct tog_view *view)
5285 const struct got_error *err = NULL;
5286 struct tog_tree_view_state *s = &view->state.tree;
5289 err = tree_entry_path(&parent_path, &s->parents, NULL);
5293 err = draw_tree_entries(view, &s->first_displayed_entry,
5294 &s->last_displayed_entry, &s->selected_entry,
5295 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5296 s->tree, s->selected, view->nlines, s->tree == s->root,
5297 &s->colors, s->repo);
5304 static const struct got_error *
5305 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5306 struct tog_view **focus_view, struct tog_view *view, int ch)
5308 const struct got_error *err = NULL;
5309 struct tog_tree_view_state *s = &view->state.tree;
5310 struct tog_view *log_view, *ref_view;
5315 s->show_ids = !s->show_ids;
5318 if (!s->selected_entry)
5320 if (view_is_parent_view(view))
5321 begin_x = view_split_begin_x(view->begin_x);
5322 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5323 &s->parents, s->commit_id, s->repo);
5324 if (view_is_parent_view(view)) {
5325 err = view_close_child(view);
5328 err = view_set_child(view, log_view);
5330 view_close(log_view);
5333 *focus_view = log_view;
5334 view->child_focussed = 1;
5336 *new_view = log_view;
5339 if (view_is_parent_view(view))
5340 begin_x = view_split_begin_x(view->begin_x);
5341 ref_view = view_open(view->nlines, view->ncols,
5342 view->begin_y, begin_x, TOG_VIEW_REF);
5343 if (ref_view == NULL)
5344 return got_error_from_errno("view_open");
5345 err = open_ref_view(ref_view, s->repo);
5347 view_close(ref_view);
5350 if (view_is_parent_view(view)) {
5351 err = view_close_child(view);
5354 err = view_set_child(view, ref_view);
5356 view_close(ref_view);
5359 *focus_view = ref_view;
5360 view->child_focussed = 1;
5362 *new_view = ref_view;
5366 if (s->selected > 0) {
5370 tree_scroll_up(view, 1);
5374 if (s->tree == s->root) {
5375 if (got_object_tree_get_first_entry(s->tree) ==
5376 s->first_displayed_entry)
5379 if (s->first_displayed_entry == NULL)
5382 tree_scroll_up(view, MAX(0, view->nlines - 3));
5386 if (s->selected < s->ndisplayed - 1) {
5390 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5392 /* can't scroll any further */
5394 tree_scroll_down(view, 1);
5398 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5400 /* can't scroll any further; move cursor down */
5401 if (s->selected < s->ndisplayed - 1)
5402 s->selected = s->ndisplayed - 1;
5405 tree_scroll_down(view, view->nlines - 3);
5410 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5411 struct tog_parent_tree *parent;
5412 /* user selected '..' */
5413 if (s->tree == s->root)
5415 parent = TAILQ_FIRST(&s->parents);
5416 TAILQ_REMOVE(&s->parents, parent,
5418 got_object_tree_close(s->tree);
5419 s->tree = parent->tree;
5420 s->first_displayed_entry =
5421 parent->first_displayed_entry;
5423 parent->selected_entry;
5424 s->selected = parent->selected;
5426 } else if (S_ISDIR(got_tree_entry_get_mode(
5427 s->selected_entry))) {
5428 struct got_tree_object *subtree;
5429 err = got_object_open_as_tree(&subtree, s->repo,
5430 got_tree_entry_get_id(s->selected_entry));
5433 err = tree_view_visit_subtree(subtree, s);
5435 got_object_tree_close(subtree);
5438 } else if (S_ISREG(got_tree_entry_get_mode(
5439 s->selected_entry))) {
5440 struct tog_view *blame_view;
5441 int begin_x = view_is_parent_view(view) ?
5442 view_split_begin_x(view->begin_x) : 0;
5444 err = blame_tree_entry(&blame_view, begin_x,
5445 s->selected_entry, &s->parents,
5446 s->commit_id, s->repo);
5449 if (view_is_parent_view(view)) {
5450 err = view_close_child(view);
5453 err = view_set_child(view, blame_view);
5455 view_close(blame_view);
5458 *focus_view = blame_view;
5459 view->child_focussed = 1;
5461 *new_view = blame_view;
5465 if (s->selected > view->nlines)
5466 s->selected = s->ndisplayed - 1;
5479 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5484 static const struct got_error *
5485 cmd_tree(int argc, char *argv[])
5487 const struct got_error *error;
5488 struct got_repository *repo = NULL;
5489 struct got_worktree *worktree = NULL;
5490 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5491 struct got_object_id *commit_id = NULL;
5492 char *commit_id_arg = NULL;
5493 struct got_commit_object *commit = NULL;
5494 struct got_tree_object *tree = NULL;
5496 struct tog_view *view;
5499 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5504 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5507 commit_id_arg = optarg;
5510 repo_path = realpath(optarg, NULL);
5511 if (repo_path == NULL)
5512 return got_error_from_errno2("realpath",
5527 cwd = getcwd(NULL, 0);
5529 return got_error_from_errno("getcwd");
5531 error = got_worktree_open(&worktree, cwd);
5532 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5535 if (repo_path == NULL) {
5538 strdup(got_worktree_get_repo_path(worktree));
5540 repo_path = strdup(cwd);
5542 if (repo_path == NULL) {
5543 error = got_error_from_errno("strdup");
5547 error = got_repo_open(&repo, repo_path, NULL);
5551 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5558 error = apply_unveil(got_repo_get_path(repo), NULL);
5562 error = got_repo_match_object_id(&commit_id, NULL,
5563 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5564 GOT_OBJ_TYPE_COMMIT, 1, repo);
5568 error = got_object_open_as_commit(&commit, repo, commit_id);
5572 error = got_object_open_as_tree(&tree, repo,
5573 got_object_commit_get_tree_id(commit));
5577 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5579 error = got_error_from_errno("view_open");
5582 error = open_tree_view(view, tree, commit_id, repo);
5585 if (!got_path_is_root_dir(in_repo_path)) {
5586 error = tree_view_walk_path(&view->state.tree, commit_id,
5587 in_repo_path, repo);
5593 /* Release work tree lock. */
5594 got_worktree_close(worktree);
5597 error = view_loop(view);
5603 got_object_commit_close(commit);
5605 got_object_tree_close(tree);
5607 got_repo_close(repo);
5611 static const struct got_error *
5612 ref_view_load_refs(struct tog_ref_view_state *s)
5614 const struct got_error *err;
5615 struct got_reflist_entry *sre;
5616 struct tog_reflist_entry *re;
5618 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5619 got_ref_cmp_by_name, NULL);
5624 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5625 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5628 re = malloc(sizeof(*re));
5630 return got_error_from_errno("malloc");
5633 re->idx = s->nrefs++;
5634 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5641 ref_view_free_refs(struct tog_ref_view_state *s)
5643 struct tog_reflist_entry *re;
5645 while (!TAILQ_EMPTY(&s->refs)) {
5646 re = TAILQ_FIRST(&s->refs);
5647 TAILQ_REMOVE(&s->refs, re, entry);
5650 got_ref_list_free(&s->simplerefs);
5653 static const struct got_error *
5654 open_ref_view(struct tog_view *view, struct got_repository *repo)
5656 const struct got_error *err = NULL;
5657 struct tog_ref_view_state *s = &view->state.ref;
5659 s->selected_entry = 0;
5662 SIMPLEQ_INIT(&s->simplerefs);
5663 TAILQ_INIT(&s->refs);
5664 SIMPLEQ_INIT(&s->colors);
5666 err = ref_view_load_refs(s);
5670 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5672 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5673 err = add_color(&s->colors, "^refs/heads/",
5674 TOG_COLOR_REFS_HEADS,
5675 get_color_value("TOG_COLOR_REFS_HEADS"));
5679 err = add_color(&s->colors, "^refs/tags/",
5680 TOG_COLOR_REFS_TAGS,
5681 get_color_value("TOG_COLOR_REFS_TAGS"));
5685 err = add_color(&s->colors, "^refs/remotes/",
5686 TOG_COLOR_REFS_REMOTES,
5687 get_color_value("TOG_COLOR_REFS_REMOTES"));
5692 view->show = show_ref_view;
5693 view->input = input_ref_view;
5694 view->close = close_ref_view;
5695 view->search_start = search_start_ref_view;
5696 view->search_next = search_next_ref_view;
5699 free_colors(&s->colors);
5703 static const struct got_error *
5704 close_ref_view(struct tog_view *view)
5706 struct tog_ref_view_state *s = &view->state.ref;
5708 ref_view_free_refs(s);
5709 free_colors(&s->colors);
5714 static const struct got_error *
5715 resolve_reflist_entry(struct got_object_id **commit_id,
5716 struct tog_reflist_entry *re, struct got_repository *repo)
5718 const struct got_error *err = NULL;
5719 struct got_object_id *obj_id;
5720 struct got_tag_object *tag = NULL;
5725 err = got_ref_resolve(&obj_id, repo, re->ref);
5729 err = got_object_get_type(&obj_type, repo, obj_id);
5734 case GOT_OBJ_TYPE_COMMIT:
5735 *commit_id = obj_id;
5737 case GOT_OBJ_TYPE_TAG:
5738 err = got_object_open_as_tag(&tag, repo, obj_id);
5742 err = got_object_get_type(&obj_type, repo,
5743 got_object_tag_get_object_id(tag));
5746 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5747 err = got_error(GOT_ERR_OBJ_TYPE);
5750 *commit_id = got_object_id_dup(
5751 got_object_tag_get_object_id(tag));
5752 if (*commit_id == NULL) {
5753 err = got_error_from_errno("got_object_id_dup");
5758 err = got_error(GOT_ERR_OBJ_TYPE);
5764 got_object_tag_close(tag);
5772 static const struct got_error *
5773 log_ref_entry(struct tog_view **new_view, int begin_x,
5774 struct tog_reflist_entry *re, struct got_repository *repo)
5776 struct tog_view *log_view;
5777 const struct got_error *err = NULL;
5778 struct got_object_id *commit_id = NULL;
5782 err = resolve_reflist_entry(&commit_id, re, repo);
5784 if (err->code != GOT_ERR_OBJ_TYPE)
5790 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5791 if (log_view == NULL) {
5792 err = got_error_from_errno("view_open");
5796 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5799 view_close(log_view);
5801 *new_view = log_view;
5807 ref_scroll_up(struct tog_view *view, int maxscroll)
5809 struct tog_ref_view_state *s = &view->state.ref;
5810 struct tog_reflist_entry *re;
5813 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5816 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5817 while (i++ < maxscroll) {
5820 s->first_displayed_entry = re;
5821 re = TAILQ_PREV(re, tog_reflist_head, entry);
5826 ref_scroll_down(struct tog_view *view, int maxscroll)
5828 struct tog_ref_view_state *s = &view->state.ref;
5829 struct tog_reflist_entry *next, *last;
5832 if (s->first_displayed_entry)
5833 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5835 next = TAILQ_FIRST(&s->refs);
5837 last = s->last_displayed_entry;
5838 while (next && last && n++ < maxscroll) {
5839 last = TAILQ_NEXT(last, entry);
5841 s->first_displayed_entry = next;
5842 next = TAILQ_NEXT(next, entry);
5847 static const struct got_error *
5848 search_start_ref_view(struct tog_view *view)
5850 struct tog_ref_view_state *s = &view->state.ref;
5852 s->matched_entry = NULL;
5857 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5859 regmatch_t regmatch;
5861 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5865 static const struct got_error *
5866 search_next_ref_view(struct tog_view *view)
5868 struct tog_ref_view_state *s = &view->state.ref;
5869 struct tog_reflist_entry *re = NULL;
5871 if (!view->searching) {
5872 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5876 if (s->matched_entry) {
5877 if (view->searching == TOG_SEARCH_FORWARD) {
5878 if (s->selected_entry)
5879 re = TAILQ_NEXT(s->selected_entry, entry);
5881 re = TAILQ_PREV(s->selected_entry,
5882 tog_reflist_head, entry);
5884 if (s->selected_entry == NULL)
5885 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5887 re = TAILQ_PREV(s->selected_entry,
5888 tog_reflist_head, entry);
5891 if (view->searching == TOG_SEARCH_FORWARD)
5892 re = TAILQ_FIRST(&s->refs);
5894 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5899 if (s->matched_entry == NULL) {
5900 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5903 if (view->searching == TOG_SEARCH_FORWARD)
5904 re = TAILQ_FIRST(&s->refs);
5906 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5909 if (match_reflist_entry(re, &view->regex)) {
5910 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5911 s->matched_entry = re;
5915 if (view->searching == TOG_SEARCH_FORWARD)
5916 re = TAILQ_NEXT(re, entry);
5918 re = TAILQ_PREV(re, tog_reflist_head, entry);
5921 if (s->matched_entry) {
5922 s->first_displayed_entry = s->matched_entry;
5929 static const struct got_error *
5930 show_ref_view(struct tog_view *view)
5932 const struct got_error *err = NULL;
5933 struct tog_ref_view_state *s = &view->state.ref;
5934 struct tog_reflist_entry *re;
5937 struct tog_color *tc;
5939 int limit = view->nlines;
5941 werase(view->window);
5948 re = s->first_displayed_entry;
5950 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5952 return got_error_from_errno("asprintf");
5954 err = format_line(&wline, &width, line, view->ncols, 0);
5959 if (view_needs_focus_indication(view))
5960 wstandout(view->window);
5961 waddwstr(view->window, wline);
5962 if (view_needs_focus_indication(view))
5963 wstandend(view->window);
5968 if (width < view->ncols - 1)
5969 waddch(view->window, '\n');
5974 while (re && limit > 0) {
5977 if (got_ref_is_symbolic(re->ref)) {
5978 if (asprintf(&line, "%s -> %s",
5979 got_ref_get_name(re->ref),
5980 got_ref_get_symref_target(re->ref)) == -1)
5981 return got_error_from_errno("asprintf");
5982 } else if (s->show_ids) {
5983 struct got_object_id *id;
5985 err = got_ref_resolve(&id, s->repo, re->ref);
5988 err = got_object_id_str(&id_str, id);
5993 if (asprintf(&line, "%s: %s",
5994 got_ref_get_name(re->ref), id_str) == -1) {
5995 err = got_error_from_errno("asprintf");
6003 line = strdup(got_ref_get_name(re->ref));
6005 return got_error_from_errno("strdup");
6008 err = format_line(&wline, &width, line, view->ncols, 0);
6013 if (n == s->selected) {
6015 wstandout(view->window);
6016 s->selected_entry = re;
6018 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6020 wattr_on(view->window,
6021 COLOR_PAIR(tc->colorpair), NULL);
6022 waddwstr(view->window, wline);
6024 wattr_off(view->window,
6025 COLOR_PAIR(tc->colorpair), NULL);
6026 if (width < view->ncols - 1)
6027 waddch(view->window, '\n');
6028 if (n == s->selected && view->focussed)
6029 wstandend(view->window);
6035 s->last_displayed_entry = re;
6038 re = TAILQ_NEXT(re, entry);
6045 static const struct got_error *
6046 browse_ref_tree(struct tog_view **new_view, int begin_x,
6047 struct tog_reflist_entry *re, struct got_repository *repo)
6049 const struct got_error *err = NULL;
6050 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6051 struct got_tree_object *tree = NULL;
6052 struct tog_view *tree_view;
6056 err = resolve_reflist_entry(&commit_id, re, repo);
6058 if (err->code != GOT_ERR_OBJ_TYPE)
6064 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6068 err = got_object_open_as_tree(&tree, repo, tree_id);
6072 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6073 if (tree_view == NULL) {
6074 err = got_error_from_errno("view_open");
6078 err = open_tree_view(tree_view, tree, commit_id, repo);
6082 *new_view = tree_view;
6088 got_object_tree_close(tree);
6092 static const struct got_error *
6093 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6094 struct tog_view **focus_view, struct tog_view *view, int ch)
6096 const struct got_error *err = NULL;
6097 struct tog_ref_view_state *s = &view->state.ref;
6098 struct tog_view *log_view, *tree_view;
6103 s->show_ids = !s->show_ids;
6107 if (!s->selected_entry)
6109 if (view_is_parent_view(view))
6110 begin_x = view_split_begin_x(view->begin_x);
6111 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6113 if (view_is_parent_view(view)) {
6114 err = view_close_child(view);
6117 err = view_set_child(view, log_view);
6119 view_close(log_view);
6122 *focus_view = log_view;
6123 view->child_focussed = 1;
6125 *new_view = log_view;
6128 if (!s->selected_entry)
6130 if (view_is_parent_view(view))
6131 begin_x = view_split_begin_x(view->begin_x);
6132 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6134 if (err || tree_view == NULL)
6136 if (view_is_parent_view(view)) {
6137 err = view_close_child(view);
6140 err = view_set_child(view, tree_view);
6142 view_close(tree_view);
6145 *focus_view = tree_view;
6146 view->child_focussed = 1;
6148 *new_view = tree_view;
6152 if (s->selected > 0) {
6156 ref_scroll_up(view, 1);
6160 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6162 ref_scroll_up(view, MAX(0, view->nlines - 1));
6166 if (s->selected < s->ndisplayed - 1) {
6170 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6171 /* can't scroll any further */
6173 ref_scroll_down(view, 1);
6177 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6178 /* can't scroll any further; move cursor down */
6179 if (s->selected < s->ndisplayed - 1)
6180 s->selected = s->ndisplayed - 1;
6183 ref_scroll_down(view, view->nlines - 1);
6186 ref_view_free_refs(s);
6187 err = ref_view_load_refs(s);
6190 if (s->selected > view->nlines)
6191 s->selected = s->ndisplayed - 1;
6204 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6209 static const struct got_error *
6210 cmd_ref(int argc, char *argv[])
6212 const struct got_error *error;
6213 struct got_repository *repo = NULL;
6214 struct got_worktree *worktree = NULL;
6215 char *cwd = NULL, *repo_path = NULL;
6217 struct tog_view *view;
6220 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6225 while ((ch = getopt(argc, argv, "r:")) != -1) {
6228 repo_path = realpath(optarg, NULL);
6229 if (repo_path == NULL)
6230 return got_error_from_errno2("realpath",
6245 cwd = getcwd(NULL, 0);
6247 return got_error_from_errno("getcwd");
6249 error = got_worktree_open(&worktree, cwd);
6250 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6253 if (repo_path == NULL) {
6256 strdup(got_worktree_get_repo_path(worktree));
6258 repo_path = strdup(cwd);
6260 if (repo_path == NULL) {
6261 error = got_error_from_errno("strdup");
6265 error = got_repo_open(&repo, repo_path, NULL);
6271 error = apply_unveil(got_repo_get_path(repo), NULL);
6275 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6277 error = got_error_from_errno("view_open");
6281 error = open_ref_view(view, repo);
6286 /* Release work tree lock. */
6287 got_worktree_close(worktree);
6290 error = view_loop(view);
6295 got_repo_close(repo);
6300 list_commands(FILE *fp)
6304 fprintf(fp, "commands:");
6305 for (i = 0; i < nitems(tog_commands); i++) {
6306 struct tog_cmd *cmd = &tog_commands[i];
6307 fprintf(fp, " %s", cmd->name);
6313 usage(int hflag, int status)
6315 FILE *fp = (status == 0) ? stdout : stderr;
6317 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6320 fprintf(fp, "lazy usage: %s path\n", getprogname());
6327 make_argv(int argc, ...)
6335 argv = calloc(argc, sizeof(char *));
6338 for (i = 0; i < argc; i++) {
6339 argv[i] = strdup(va_arg(ap, char *));
6340 if (argv[i] == NULL)
6349 * Try to convert 'tog path' into a 'tog log path' command.
6350 * The user could simply have mistyped the command rather than knowingly
6351 * provided a path. So check whether argv[0] can in fact be resolved
6352 * to a path in the HEAD commit and print a special error if not.
6353 * This hack is for mpi@ <3
6355 static const struct got_error *
6356 tog_log_with_path(int argc, char *argv[])
6358 const struct got_error *error = NULL;
6359 struct tog_cmd *cmd = NULL;
6360 struct got_repository *repo = NULL;
6361 struct got_worktree *worktree = NULL;
6362 struct got_object_id *commit_id = NULL, *id = NULL;
6363 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6364 char *commit_id_str = NULL, **cmd_argv = NULL;
6366 cwd = getcwd(NULL, 0);
6368 return got_error_from_errno("getcwd");
6370 error = got_worktree_open(&worktree, cwd);
6371 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6375 repo_path = strdup(got_worktree_get_repo_path(worktree));
6377 repo_path = strdup(cwd);
6378 if (repo_path == NULL) {
6379 error = got_error_from_errno("strdup");
6383 error = got_repo_open(&repo, repo_path, NULL);
6387 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6392 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6393 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6394 GOT_OBJ_TYPE_COMMIT, 1, repo);
6399 got_worktree_close(worktree);
6403 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6405 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6407 fprintf(stderr, "%s: '%s' is no known command or path\n",
6408 getprogname(), argv[0]);
6413 got_repo_close(repo);
6416 error = got_object_id_str(&commit_id_str, commit_id);
6420 cmd = &tog_commands[0]; /* log */
6422 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6423 error = cmd->cmd_main(argc, cmd_argv);
6426 got_repo_close(repo);
6428 got_worktree_close(worktree);
6430 free(commit_id_str);
6437 for (i = 0; i < argc; i++)
6445 main(int argc, char *argv[])
6447 const struct got_error *error = NULL;
6448 struct tog_cmd *cmd = NULL;
6449 int ch, hflag = 0, Vflag = 0;
6450 char **cmd_argv = NULL;
6451 static struct option longopts[] = {
6452 { "version", no_argument, NULL, 'V' },
6456 setlocale(LC_CTYPE, "");
6458 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6478 got_version_print_str();
6485 /* Build an argument vector which runs a default command. */
6486 cmd = &tog_commands[0];
6488 cmd_argv = make_argv(argc, cmd->name);
6492 /* Did the user specify a command? */
6493 for (i = 0; i < nitems(tog_commands); i++) {
6494 if (strncmp(tog_commands[i].name, argv[0],
6495 strlen(argv[0])) == 0) {
6496 cmd = &tog_commands[i];
6505 /* No command specified; try log with a path */
6506 error = tog_log_with_path(argc, argv);
6511 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6518 for (i = 0; i < argc; i++)
6523 if (error && error->code != GOT_ERR_CANCELLED)
6524 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);