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>
22 #define _XOPEN_SOURCE_EXTENDED
24 #undef _XOPEN_SOURCE_EXTENDED
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_opentemp.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
56 #include "got_worktree.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 #define CTRL(x) ((x) & 0x1f)
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
78 __dead static void usage(int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
84 static const struct got_error* cmd_log(int, char *[]);
85 static const struct got_error* cmd_diff(int, char *[]);
86 static const struct got_error* cmd_blame(int, char *[]);
87 static const struct got_error* cmd_tree(int, char *[]);
89 static struct tog_cmd tog_commands[] = {
90 { "log", cmd_log, usage_log },
91 { "diff", cmd_diff, usage_diff },
92 { "blame", cmd_blame, usage_blame },
93 { "tree", cmd_tree, usage_tree },
103 #define TOG_EOF_STRING "(END)"
105 struct commit_queue_entry {
106 TAILQ_ENTRY(commit_queue_entry) entry;
107 struct got_object_id *id;
108 struct got_commit_object *commit;
111 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
112 struct commit_queue {
114 struct commit_queue_head head;
118 SIMPLEQ_ENTRY(tog_color) entry;
122 SIMPLEQ_HEAD(tog_colors, tog_color);
124 static const struct got_error *
125 add_color(struct tog_colors *colors, const char *pattern,
126 int idx, short color)
128 const struct got_error *err = NULL;
129 struct tog_color *tc;
132 if (idx < 1 || idx > COLOR_PAIRS - 1)
135 init_pair(idx, color, -1);
137 tc = calloc(1, sizeof(*tc));
139 return got_error_from_errno("calloc");
140 regerr = regcomp(&tc->regex, pattern,
141 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
143 static char regerr_msg[512];
144 static char err_msg[512];
145 regerror(regerr, &tc->regex, regerr_msg,
147 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
149 err = got_error_msg(GOT_ERR_REGEX, err_msg);
154 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
159 free_colors(struct tog_colors *colors)
161 struct tog_color *tc;
163 while (!SIMPLEQ_EMPTY(colors)) {
164 tc = SIMPLEQ_FIRST(colors);
165 SIMPLEQ_REMOVE_HEAD(colors, entry);
172 get_color(struct tog_colors *colors, int colorpair)
174 struct tog_color *tc = NULL;
176 SIMPLEQ_FOREACH(tc, colors, entry) {
177 if (tc->colorpair == colorpair)
185 default_color_value(const char *envvar)
187 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
188 return COLOR_MAGENTA;
189 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
191 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
193 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
195 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
196 return COLOR_MAGENTA;
197 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
198 return COLOR_MAGENTA;
199 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
201 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
203 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
205 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
207 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
214 get_color_value(const char *envvar)
216 const char *val = getenv(envvar);
219 return default_color_value(envvar);
221 if (strcasecmp(val, "black") == 0)
223 if (strcasecmp(val, "red") == 0)
225 if (strcasecmp(val, "green") == 0)
227 if (strcasecmp(val, "yellow") == 0)
229 if (strcasecmp(val, "blue") == 0)
231 if (strcasecmp(val, "magenta") == 0)
232 return COLOR_MAGENTA;
233 if (strcasecmp(val, "cyan") == 0)
235 if (strcasecmp(val, "white") == 0)
237 if (strcasecmp(val, "default") == 0)
240 return default_color_value(envvar);
244 struct tog_diff_view_state {
245 struct got_object_id *id1, *id2;
247 int first_displayed_line;
248 int last_displayed_line;
251 struct got_repository *repo;
252 struct got_reflist_head *refs;
253 struct tog_colors colors;
260 /* passed from log view; may be NULL */
261 struct tog_view *log_view;
264 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
266 struct tog_log_thread_args {
267 pthread_cond_t need_commits;
269 struct got_commit_graph *graph;
270 struct commit_queue *commits;
271 const char *in_repo_path;
272 struct got_object_id *start_id;
273 struct got_repository *repo;
276 struct commit_queue_entry **first_displayed_entry;
277 struct commit_queue_entry **selected_entry;
279 int *search_next_done;
283 struct tog_log_view_state {
284 struct commit_queue commits;
285 struct commit_queue_entry *first_displayed_entry;
286 struct commit_queue_entry *last_displayed_entry;
287 struct commit_queue_entry *selected_entry;
290 const char *head_ref_name;
292 struct got_repository *repo;
293 struct got_reflist_head *refs;
294 struct got_object_id *start_id;
297 struct tog_log_thread_args thread_args;
298 struct commit_queue_entry *matched_entry;
299 struct commit_queue_entry *search_entry;
300 struct tog_colors colors;
303 #define TOG_COLOR_DIFF_MINUS 1
304 #define TOG_COLOR_DIFF_PLUS 2
305 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
306 #define TOG_COLOR_DIFF_META 4
307 #define TOG_COLOR_TREE_SUBMODULE 5
308 #define TOG_COLOR_TREE_SYMLINK 6
309 #define TOG_COLOR_TREE_DIRECTORY 7
310 #define TOG_COLOR_TREE_EXECUTABLE 8
311 #define TOG_COLOR_COMMIT 9
312 #define TOG_COLOR_AUTHOR 10
313 #define TOG_COLOR_DATE 11
315 struct tog_blame_cb_args {
316 struct tog_blame_line *lines; /* one per line */
319 struct tog_view *view;
320 struct got_object_id *commit_id;
324 struct tog_blame_thread_args {
326 struct got_repository *repo;
327 struct tog_blame_cb_args *cb_args;
329 got_cancel_cb cancel_cb;
336 struct tog_blame_line *lines;
340 struct tog_blame_thread_args thread_args;
341 struct tog_blame_cb_args cb_args;
345 struct tog_blame_view_state {
346 int first_displayed_line;
347 int last_displayed_line;
352 struct got_object_id_queue blamed_commits;
353 struct got_object_qid *blamed_commit;
355 struct got_repository *repo;
356 struct got_reflist_head *refs;
357 struct got_object_id *commit_id;
358 struct tog_blame blame;
360 struct tog_colors colors;
363 struct tog_parent_tree {
364 TAILQ_ENTRY(tog_parent_tree) entry;
365 struct got_tree_object *tree;
366 struct got_tree_entry *first_displayed_entry;
367 struct got_tree_entry *selected_entry;
371 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
373 struct tog_tree_view_state {
375 struct got_tree_object *root;
376 struct got_tree_object *tree;
377 struct got_tree_entry *first_displayed_entry;
378 struct got_tree_entry *last_displayed_entry;
379 struct got_tree_entry *selected_entry;
380 int ndisplayed, selected, show_ids;
381 struct tog_parent_trees parents;
382 struct got_object_id *commit_id;
383 struct got_repository *repo;
384 struct got_reflist_head *refs;
385 struct got_tree_entry *matched_entry;
386 struct tog_colors colors;
390 * We implement two types of views: parent views and child views.
392 * The 'Tab' key switches between a parent view and its child view.
393 * Child views are shown side-by-side to their parent view, provided
394 * there is enough screen estate.
396 * When a new view is opened from within a parent view, this new view
397 * becomes a child view of the parent view, replacing any existing child.
399 * When a new view is opened from within a child view, this new view
400 * becomes a parent view which will obscure the views below until the
401 * user quits the new parent view by typing 'q'.
403 * This list of views contains parent views only.
404 * Child views are only pointed to by their parent view.
406 TAILQ_HEAD(tog_view_list_head, tog_view);
409 TAILQ_ENTRY(tog_view) entry;
412 int nlines, ncols, begin_y, begin_x;
413 int lines, cols; /* copies of LINES and COLS */
415 struct tog_view *parent;
416 struct tog_view *child;
419 /* type-specific state */
420 enum tog_view_type type;
422 struct tog_diff_view_state diff;
423 struct tog_log_view_state log;
424 struct tog_blame_view_state blame;
425 struct tog_tree_view_state tree;
428 const struct got_error *(*show)(struct tog_view *);
429 const struct got_error *(*input)(struct tog_view **,
430 struct tog_view **, struct tog_view**, struct tog_view *, int);
431 const struct got_error *(*close)(struct tog_view *);
433 const struct got_error *(*search_start)(struct tog_view *);
434 const struct got_error *(*search_next)(struct tog_view *);
436 #define TOG_SEARCH_FORWARD 1
437 #define TOG_SEARCH_BACKWARD 2
438 int search_next_done;
442 static const struct got_error *open_diff_view(struct tog_view *,
443 struct got_object_id *, struct got_object_id *, struct tog_view *,
444 struct got_reflist_head *, struct got_repository *);
445 static const struct got_error *show_diff_view(struct tog_view *);
446 static const struct got_error *input_diff_view(struct tog_view **,
447 struct tog_view **, struct tog_view **, struct tog_view *, int);
448 static const struct got_error* close_diff_view(struct tog_view *);
449 static const struct got_error *search_start_diff_view(struct tog_view *);
450 static const struct got_error *search_next_diff_view(struct tog_view *);
452 static const struct got_error *open_log_view(struct tog_view *,
453 struct got_object_id *, struct got_reflist_head *,
454 struct got_repository *, const char *, const char *, int);
455 static const struct got_error * show_log_view(struct tog_view *);
456 static const struct got_error *input_log_view(struct tog_view **,
457 struct tog_view **, struct tog_view **, struct tog_view *, int);
458 static const struct got_error *close_log_view(struct tog_view *);
459 static const struct got_error *search_start_log_view(struct tog_view *);
460 static const struct got_error *search_next_log_view(struct tog_view *);
462 static const struct got_error *open_blame_view(struct tog_view *, char *,
463 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
464 static const struct got_error *show_blame_view(struct tog_view *);
465 static const struct got_error *input_blame_view(struct tog_view **,
466 struct tog_view **, struct tog_view **, struct tog_view *, int);
467 static const struct got_error *close_blame_view(struct tog_view *);
468 static const struct got_error *search_start_blame_view(struct tog_view *);
469 static const struct got_error *search_next_blame_view(struct tog_view *);
471 static const struct got_error *open_tree_view(struct tog_view *,
472 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
473 struct got_repository *);
474 static const struct got_error *show_tree_view(struct tog_view *);
475 static const struct got_error *input_tree_view(struct tog_view **,
476 struct tog_view **, struct tog_view **, struct tog_view *, int);
477 static const struct got_error *close_tree_view(struct tog_view *);
478 static const struct got_error *search_start_tree_view(struct tog_view *);
479 static const struct got_error *search_next_tree_view(struct tog_view *);
481 static volatile sig_atomic_t tog_sigwinch_received;
482 static volatile sig_atomic_t tog_sigpipe_received;
483 static volatile sig_atomic_t tog_sigcont_received;
486 tog_sigwinch(int signo)
488 tog_sigwinch_received = 1;
492 tog_sigpipe(int signo)
494 tog_sigpipe_received = 1;
498 tog_sigcont(int signo)
500 tog_sigcont_received = 1;
503 static const struct got_error *
504 view_close(struct tog_view *view)
506 const struct got_error *err = NULL;
509 view_close(view->child);
513 err = view->close(view);
515 del_panel(view->panel);
517 delwin(view->window);
522 static struct tog_view *
523 view_open(int nlines, int ncols, int begin_y, int begin_x,
524 enum tog_view_type type)
526 struct tog_view *view = calloc(1, sizeof(*view));
534 view->nlines = nlines ? nlines : LINES - begin_y;
535 view->ncols = ncols ? ncols : COLS - begin_x;
536 view->begin_y = begin_y;
537 view->begin_x = begin_x;
538 view->window = newwin(nlines, ncols, begin_y, begin_x);
539 if (view->window == NULL) {
543 view->panel = new_panel(view->window);
544 if (view->panel == NULL ||
545 set_panel_userptr(view->panel, view) != OK) {
550 keypad(view->window, TRUE);
555 view_split_begin_x(int begin_x)
557 if (begin_x > 0 || COLS < 120)
559 return (COLS - MAX(COLS / 2, 80));
562 static const struct got_error *view_resize(struct tog_view *);
564 static const struct got_error *
565 view_splitscreen(struct tog_view *view)
567 const struct got_error *err = NULL;
570 view->begin_x = view_split_begin_x(0);
571 view->nlines = LINES;
572 view->ncols = COLS - view->begin_x;
575 err = view_resize(view);
579 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
580 return got_error_from_errno("mvwin");
585 static const struct got_error *
586 view_fullscreen(struct tog_view *view)
588 const struct got_error *err = NULL;
592 view->nlines = LINES;
596 err = view_resize(view);
600 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
601 return got_error_from_errno("mvwin");
607 view_is_parent_view(struct tog_view *view)
609 return view->parent == NULL;
612 static const struct got_error *
613 view_resize(struct tog_view *view)
617 if (view->lines > LINES)
618 nlines = view->nlines - (view->lines - LINES);
620 nlines = view->nlines + (LINES - view->lines);
622 if (view->cols > COLS)
623 ncols = view->ncols - (view->cols - COLS);
625 ncols = view->ncols + (COLS - view->cols);
627 if (wresize(view->window, nlines, ncols) == ERR)
628 return got_error_from_errno("wresize");
629 if (replace_panel(view->panel, view->window) == ERR)
630 return got_error_from_errno("replace_panel");
631 wclear(view->window);
633 view->nlines = nlines;
639 view->child->begin_x = view_split_begin_x(view->begin_x);
640 if (view->child->begin_x == 0) {
641 view_fullscreen(view->child);
642 if (view->child->focussed)
643 show_panel(view->child->panel);
645 show_panel(view->panel);
647 view_splitscreen(view->child);
648 show_panel(view->child->panel);
655 static const struct got_error *
656 view_close_child(struct tog_view *view)
658 const struct got_error *err = NULL;
660 if (view->child == NULL)
663 err = view_close(view->child);
668 static const struct got_error *
669 view_set_child(struct tog_view *view, struct tog_view *child)
671 const struct got_error *err = NULL;
674 child->parent = view;
679 view_is_splitscreen(struct tog_view *view)
681 return view->begin_x > 0;
690 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
691 cols = 80; /* Default */
697 resize_term(lines, cols);
700 static const struct got_error *
701 view_search_start(struct tog_view *view)
703 const struct got_error *err = NULL;
707 if (view->nlines < 1)
710 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
711 wclrtoeol(view->window);
715 ret = wgetnstr(view->window, pattern, sizeof(pattern));
721 if (view->searching) {
722 regfree(&view->regex);
726 if (regcomp(&view->regex, pattern,
727 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
728 err = view->search_start(view);
730 regfree(&view->regex);
733 view->searching = TOG_SEARCH_FORWARD;
734 view->search_next_done = 0;
735 view->search_next(view);
741 static const struct got_error *
742 view_input(struct tog_view **new, struct tog_view **dead,
743 struct tog_view **focus, int *done, struct tog_view *view,
744 struct tog_view_list_head *views)
746 const struct got_error *err = NULL;
754 if (view->searching && !view->search_next_done) {
755 errcode = pthread_mutex_unlock(&tog_mutex);
757 return got_error_set_errno(errcode,
758 "pthread_mutex_unlock");
760 errcode = pthread_mutex_lock(&tog_mutex);
762 return got_error_set_errno(errcode,
763 "pthread_mutex_lock");
764 view->search_next(view);
768 nodelay(stdscr, FALSE);
769 /* Allow threads to make progress while we are waiting for input. */
770 errcode = pthread_mutex_unlock(&tog_mutex);
772 return got_error_set_errno(errcode, "pthread_mutex_unlock");
773 ch = wgetch(view->window);
774 errcode = pthread_mutex_lock(&tog_mutex);
776 return got_error_set_errno(errcode, "pthread_mutex_lock");
777 nodelay(stdscr, TRUE);
779 if (tog_sigwinch_received || tog_sigcont_received) {
781 tog_sigwinch_received = 0;
782 tog_sigcont_received = 0;
783 TAILQ_FOREACH(v, views, entry) {
784 err = view_resize(v);
787 err = v->input(new, dead, focus, v, KEY_RESIZE);
798 *focus = view->child;
799 view->child_focussed = 1;
800 } else if (view->parent) {
801 *focus = view->parent;
802 view->parent->child_focussed = 0;
806 err = view->input(new, dead, focus, view, ch);
813 if (view_is_parent_view(view)) {
814 if (view->child == NULL)
816 if (view_is_splitscreen(view->child)) {
817 *focus = view->child;
818 view->child_focussed = 1;
819 err = view_fullscreen(view->child);
821 err = view_splitscreen(view->child);
824 err = view->child->input(new, dead, focus,
825 view->child, KEY_RESIZE);
827 if (view_is_splitscreen(view)) {
829 view->parent->child_focussed = 1;
830 err = view_fullscreen(view);
832 err = view_splitscreen(view);
836 err = view->input(new, dead, focus, view,
843 if (view->search_start)
844 view_search_start(view);
846 err = view->input(new, dead, focus, view, ch);
850 if (view->search_next && view->searching) {
851 view->searching = (ch == 'n' ?
852 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
853 view->search_next_done = 0;
854 view->search_next(view);
856 err = view->input(new, dead, focus, view, ch);
859 err = view->input(new, dead, focus, view, ch);
867 view_vborder(struct tog_view *view)
870 struct tog_view *view_above;
873 return view_vborder(view->parent);
875 panel = panel_above(view->panel);
879 view_above = panel_userptr(panel);
880 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
881 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
885 view_needs_focus_indication(struct tog_view *view)
887 if (view_is_parent_view(view)) {
888 if (view->child == NULL || view->child_focussed)
890 if (!view_is_splitscreen(view->child))
892 } else if (!view_is_splitscreen(view))
895 return view->focussed;
898 static const struct got_error *
899 view_loop(struct tog_view *view)
901 const struct got_error *err = NULL;
902 struct tog_view_list_head views;
903 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
904 int fast_refresh = 10;
905 int done = 0, errcode;
907 errcode = pthread_mutex_lock(&tog_mutex);
909 return got_error_set_errno(errcode, "pthread_mutex_lock");
912 TAILQ_INSERT_HEAD(&views, view, entry);
916 err = view->show(view);
921 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
922 /* Refresh fast during initialization, then become slower. */
923 if (fast_refresh && fast_refresh-- == 0)
924 halfdelay(10); /* switch to once per second */
926 err = view_input(&new_view, &dead_view, &focus_view, &done,
931 struct tog_view *prev = NULL;
933 if (view_is_parent_view(dead_view))
934 prev = TAILQ_PREV(dead_view,
935 tog_view_list_head, entry);
936 else if (view->parent != dead_view)
939 if (dead_view->parent)
940 dead_view->parent->child = NULL;
942 TAILQ_REMOVE(&views, dead_view, entry);
944 err = view_close(dead_view);
945 if (err || (dead_view == main_view && new_view == NULL))
948 if (view == dead_view) {
953 else if (!TAILQ_EMPTY(&views))
954 view = TAILQ_LAST(&views,
959 if (view->child && view->child_focussed)
960 focus_view = view->child;
967 struct tog_view *v, *t;
968 /* Only allow one parent view per type. */
969 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
970 if (v->type != new_view->type)
972 TAILQ_REMOVE(&views, v, entry);
978 TAILQ_INSERT_TAIL(&views, new_view, entry);
980 if (focus_view == NULL)
981 focus_view = new_view;
984 show_panel(focus_view->panel);
987 focus_view->focussed = 1;
990 show_panel(new_view->panel);
991 if (view->child && view_is_splitscreen(view->child))
992 show_panel(view->child->panel);
995 if (focus_view == NULL) {
997 show_panel(view->panel);
998 if (view->child && view_is_splitscreen(view->child))
999 show_panel(view->child->panel);
1003 err = view->parent->show(view->parent);
1007 err = view->show(view);
1011 err = view->child->show(view->child);
1020 while (!TAILQ_EMPTY(&views)) {
1021 view = TAILQ_FIRST(&views);
1022 TAILQ_REMOVE(&views, view, entry);
1026 errcode = pthread_mutex_unlock(&tog_mutex);
1027 if (errcode && err == NULL)
1028 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1038 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1043 /* Create newly allocated wide-character string equivalent to a byte string. */
1044 static const struct got_error *
1045 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1048 const struct got_error *err = NULL;
1051 *wlen = mbstowcs(NULL, s, 0);
1052 if (*wlen == (size_t)-1) {
1054 if (errno != EILSEQ)
1055 return got_error_from_errno("mbstowcs");
1057 /* byte string invalid in current encoding; try to "fix" it */
1058 err = got_mbsavis(&vis, &vislen, s);
1061 *wlen = mbstowcs(NULL, vis, 0);
1062 if (*wlen == (size_t)-1) {
1063 err = got_error_from_errno("mbstowcs"); /* give up */
1068 *ws = calloc(*wlen + 1, sizeof(**ws));
1070 err = got_error_from_errno("calloc");
1074 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1075 err = got_error_from_errno("mbstowcs");
1086 /* Format a line for display, ensuring that it won't overflow a width limit. */
1087 static const struct got_error *
1088 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1091 const struct got_error *err = NULL;
1093 wchar_t *wline = NULL;
1100 err = mbs2ws(&wline, &wlen, line);
1106 int width = wcwidth(wline[i]);
1113 if (width == 1 || width == 2) {
1114 if (cols + width > wlimit)
1118 } else if (width == -1) {
1119 if (wline[i] == L'\t') {
1121 ((cols + col_tab_align) % TABSIZE);
1122 if (cols + width > wlimit)
1128 err = got_error_from_errno("wcwidth");
1143 static const struct got_error*
1144 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1145 struct got_object_id *id, struct got_repository *repo)
1147 static const struct got_error *err = NULL;
1148 struct got_reflist_entry *re;
1154 SIMPLEQ_FOREACH(re, refs, entry) {
1155 struct got_tag_object *tag = NULL;
1158 name = got_ref_get_name(re->ref);
1159 if (strcmp(name, GOT_REF_HEAD) == 0)
1161 if (strncmp(name, "refs/", 5) == 0)
1163 if (strncmp(name, "got/", 4) == 0)
1165 if (strncmp(name, "heads/", 6) == 0)
1167 if (strncmp(name, "remotes/", 8) == 0)
1169 if (strncmp(name, "tags/", 5) == 0) {
1170 err = got_object_open_as_tag(&tag, repo, re->id);
1172 if (err->code != GOT_ERR_OBJ_TYPE)
1174 /* Ref points at something other than a tag. */
1179 cmp = got_object_id_cmp(tag ?
1180 got_object_tag_get_object_id(tag) : re->id, id);
1182 got_object_tag_close(tag);
1186 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1187 s ? ", " : "", name) == -1) {
1188 err = got_error_from_errno("asprintf");
1199 static const struct got_error *
1200 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1203 char *smallerthan, *at;
1205 smallerthan = strchr(author, '<');
1206 if (smallerthan && smallerthan[1] != '\0')
1207 author = smallerthan + 1;
1208 at = strchr(author, '@');
1211 return format_line(wauthor, author_width, author, limit, col_tab_align);
1214 static const struct got_error *
1215 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1216 struct got_object_id *id, struct got_reflist_head *refs,
1217 const size_t date_display_cols, int author_display_cols,
1218 struct tog_colors *colors)
1220 const struct got_error *err = NULL;
1221 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1222 char *logmsg0 = NULL, *logmsg = NULL;
1223 char *author = NULL;
1224 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1225 int author_width, logmsg_width;
1226 char *newline, *line = NULL;
1228 const int avail = view->ncols;
1230 time_t committer_time;
1231 struct tog_color *tc;
1233 committer_time = got_object_commit_get_committer_time(commit);
1234 if (localtime_r(&committer_time, &tm) == NULL)
1235 return got_error_from_errno("localtime_r");
1236 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1238 return got_error(GOT_ERR_NO_SPACE);
1240 if (avail <= date_display_cols)
1241 limit = MIN(sizeof(datebuf) - 1, avail);
1243 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1244 tc = get_color(colors, TOG_COLOR_DATE);
1246 wattr_on(view->window,
1247 COLOR_PAIR(tc->colorpair), NULL);
1248 waddnstr(view->window, datebuf, limit);
1250 wattr_off(view->window,
1251 COLOR_PAIR(tc->colorpair), NULL);
1258 err = got_object_id_str(&id_str, id);
1261 tc = get_color(colors, TOG_COLOR_COMMIT);
1263 wattr_on(view->window,
1264 COLOR_PAIR(tc->colorpair), NULL);
1265 wprintw(view->window, "%.8s ", id_str);
1267 wattr_off(view->window,
1268 COLOR_PAIR(tc->colorpair), NULL);
1275 author = strdup(got_object_commit_get_author(commit));
1276 if (author == NULL) {
1277 err = got_error_from_errno("strdup");
1280 err = format_author(&wauthor, &author_width, author, avail - col, col);
1283 tc = get_color(colors, TOG_COLOR_AUTHOR);
1285 wattr_on(view->window,
1286 COLOR_PAIR(tc->colorpair), NULL);
1287 waddwstr(view->window, wauthor);
1289 wattr_off(view->window,
1290 COLOR_PAIR(tc->colorpair), NULL);
1291 col += author_width;
1292 while (col < avail && author_width < author_display_cols + 2) {
1293 waddch(view->window, ' ');
1300 err = got_object_commit_get_logmsg(&logmsg0, commit);
1304 while (*logmsg == '\n')
1306 newline = strchr(logmsg, '\n');
1309 limit = avail - col;
1310 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1313 waddwstr(view->window, wlogmsg);
1314 col += logmsg_width;
1315 while (col < avail) {
1316 waddch(view->window, ' ');
1328 static struct commit_queue_entry *
1329 alloc_commit_queue_entry(struct got_commit_object *commit,
1330 struct got_object_id *id)
1332 struct commit_queue_entry *entry;
1334 entry = calloc(1, sizeof(*entry));
1339 entry->commit = commit;
1344 pop_commit(struct commit_queue *commits)
1346 struct commit_queue_entry *entry;
1348 entry = TAILQ_FIRST(&commits->head);
1349 TAILQ_REMOVE(&commits->head, entry, entry);
1350 got_object_commit_close(entry->commit);
1351 commits->ncommits--;
1352 /* Don't free entry->id! It is owned by the commit graph. */
1357 free_commits(struct commit_queue *commits)
1359 while (!TAILQ_EMPTY(&commits->head))
1360 pop_commit(commits);
1363 static const struct got_error *
1364 match_commit(int *have_match, struct got_object_id *id,
1365 struct got_commit_object *commit, regex_t *regex)
1367 const struct got_error *err = NULL;
1368 regmatch_t regmatch;
1369 char *id_str = NULL, *logmsg = NULL;
1373 err = got_object_id_str(&id_str, id);
1377 err = got_object_commit_get_logmsg(&logmsg, commit);
1381 if (regexec(regex, got_object_commit_get_author(commit), 1,
1382 ®match, 0) == 0 ||
1383 regexec(regex, got_object_commit_get_committer(commit), 1,
1384 ®match, 0) == 0 ||
1385 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1386 regexec(regex, logmsg, 1, ®match, 0) == 0)
1394 static const struct got_error *
1395 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1396 int minqueue, struct got_repository *repo, const char *path,
1397 int *searching, int *search_next_done, regex_t *regex)
1399 const struct got_error *err = NULL;
1400 int nqueued = 0, have_match = 0;
1403 * We keep all commits open throughout the lifetime of the log
1404 * view in order to avoid having to re-fetch commits from disk
1405 * while updating the display.
1407 while (nqueued < minqueue ||
1408 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1409 struct got_object_id *id;
1410 struct got_commit_object *commit;
1411 struct commit_queue_entry *entry;
1414 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1415 if (err || id == NULL)
1418 err = got_object_open_as_commit(&commit, repo, id);
1421 entry = alloc_commit_queue_entry(commit, id);
1422 if (entry == NULL) {
1423 err = got_error_from_errno("alloc_commit_queue_entry");
1427 errcode = pthread_mutex_lock(&tog_mutex);
1429 err = got_error_set_errno(errcode,
1430 "pthread_mutex_lock");
1434 entry->idx = commits->ncommits;
1435 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1437 commits->ncommits++;
1439 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1440 err = match_commit(&have_match, id, commit, regex);
1443 errcode = pthread_mutex_unlock(&tog_mutex);
1444 if (errcode && err == NULL)
1445 err = got_error_set_errno(errcode,
1446 "pthread_mutex_unlock");
1448 if (err || have_match)
1455 static const struct got_error *
1456 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1457 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1458 struct commit_queue *commits, int selected_idx, int limit,
1459 struct got_reflist_head *refs, const char *path, int commits_needed,
1460 struct tog_colors *colors)
1462 const struct got_error *err = NULL;
1463 struct tog_log_view_state *s = &view->state.log;
1464 struct commit_queue_entry *entry;
1466 int ncommits, author_cols = 4;
1467 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1468 char *refs_str = NULL;
1470 struct tog_color *tc;
1471 static const size_t date_display_cols = 12;
1476 if (ncommits == selected_idx) {
1480 entry = TAILQ_NEXT(entry, entry);
1484 if (*selected && !(view->searching && view->search_next_done == 0)) {
1485 err = got_object_id_str(&id_str, (*selected)->id);
1489 err = build_refs_str(&refs_str, refs, (*selected)->id,
1496 if (commits_needed == 0)
1497 halfdelay(10); /* disable fast refresh */
1499 if (asprintf(&ncommits_str, " [%d/%d] %s",
1500 entry ? entry->idx + 1 : 0, commits->ncommits,
1501 commits_needed > 0 ?
1502 (view->searching && view->search_next_done == 0
1503 ? "searching..." : "loading... ") :
1504 (refs_str ? refs_str : "")) == -1) {
1505 err = got_error_from_errno("asprintf");
1509 if (path && strcmp(path, "/") != 0) {
1510 if (asprintf(&header, "commit %s %s%s",
1511 id_str ? id_str : "........................................",
1512 path, ncommits_str) == -1) {
1513 err = got_error_from_errno("asprintf");
1517 } else if (asprintf(&header, "commit %s%s",
1518 id_str ? id_str : "........................................",
1519 ncommits_str) == -1) {
1520 err = got_error_from_errno("asprintf");
1524 err = format_line(&wline, &width, header, view->ncols, 0);
1528 werase(view->window);
1530 if (view_needs_focus_indication(view))
1531 wstandout(view->window);
1532 tc = get_color(colors, TOG_COLOR_COMMIT);
1534 wattr_on(view->window,
1535 COLOR_PAIR(tc->colorpair), NULL);
1536 waddwstr(view->window, wline);
1538 wattr_off(view->window,
1539 COLOR_PAIR(tc->colorpair), NULL);
1540 while (width < view->ncols) {
1541 waddch(view->window, ' ');
1544 if (view_needs_focus_indication(view))
1545 wstandend(view->window);
1550 /* Grow author column size if necessary. */
1557 if (ncommits >= limit - 1)
1559 author = strdup(got_object_commit_get_author(entry->commit));
1560 if (author == NULL) {
1561 err = got_error_from_errno("strdup");
1564 err = format_author(&wauthor, &width, author, COLS,
1566 if (author_cols < width)
1567 author_cols = width;
1571 entry = TAILQ_NEXT(entry, entry);
1578 if (ncommits >= limit - 1)
1580 if (ncommits == selected_idx)
1581 wstandout(view->window);
1582 err = draw_commit(view, entry->commit, entry->id, refs,
1583 date_display_cols, author_cols, colors);
1584 if (ncommits == selected_idx)
1585 wstandend(view->window);
1590 entry = TAILQ_NEXT(entry, entry);
1603 scroll_up(struct tog_view *view,
1604 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1605 struct commit_queue *commits)
1607 struct commit_queue_entry *entry;
1610 entry = TAILQ_FIRST(&commits->head);
1611 if (*first_displayed_entry == entry)
1614 entry = *first_displayed_entry;
1615 while (entry && nscrolled < maxscroll) {
1616 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1618 *first_displayed_entry = entry;
1624 static const struct got_error *
1625 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1626 pthread_cond_t *need_commits)
1631 halfdelay(1); /* fast refresh while loading commits */
1633 while (*commits_needed > 0) {
1637 /* Wake the log thread. */
1638 errcode = pthread_cond_signal(need_commits);
1640 return got_error_set_errno(errcode,
1641 "pthread_cond_signal");
1642 errcode = pthread_mutex_unlock(&tog_mutex);
1644 return got_error_set_errno(errcode,
1645 "pthread_mutex_unlock");
1647 errcode = pthread_mutex_lock(&tog_mutex);
1649 return got_error_set_errno(errcode,
1650 "pthread_mutex_lock");
1652 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1654 * Thread is not done yet; lose a key press
1655 * and let the user retry... this way the GUI
1656 * remains interactive while logging deep paths
1657 * with few commits in history.
1666 static const struct got_error *
1667 scroll_down(struct tog_view *view,
1668 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1669 struct commit_queue_entry **last_displayed_entry,
1670 struct commit_queue *commits, int *log_complete, int *commits_needed,
1671 pthread_cond_t *need_commits)
1673 const struct got_error *err = NULL;
1674 struct commit_queue_entry *pentry;
1677 if (*last_displayed_entry == NULL)
1680 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1681 if (pentry == NULL && !*log_complete) {
1683 * Ask the log thread for required amount of commits
1684 * plus some amount of pre-fetching.
1686 (*commits_needed) += maxscroll + 20;
1687 err = trigger_log_thread(0, commits_needed, log_complete,
1694 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1698 *last_displayed_entry = pentry;
1700 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1703 *first_displayed_entry = pentry;
1704 } while (++nscrolled < maxscroll);
1709 static const struct got_error *
1710 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1711 struct got_commit_object *commit, struct got_object_id *commit_id,
1712 struct tog_view *log_view, struct got_reflist_head *refs,
1713 struct got_repository *repo)
1715 const struct got_error *err;
1716 struct got_object_qid *parent_id;
1717 struct tog_view *diff_view;
1719 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1720 if (diff_view == NULL)
1721 return got_error_from_errno("view_open");
1723 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1724 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1725 commit_id, log_view, refs, repo);
1727 *new_view = diff_view;
1731 static const struct got_error *
1732 tree_view_visit_subtree(struct got_tree_object *subtree,
1733 struct tog_tree_view_state *s)
1735 struct tog_parent_tree *parent;
1737 parent = calloc(1, sizeof(*parent));
1739 return got_error_from_errno("calloc");
1741 parent->tree = s->tree;
1742 parent->first_displayed_entry = s->first_displayed_entry;
1743 parent->selected_entry = s->selected_entry;
1744 parent->selected = s->selected;
1745 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1748 s->first_displayed_entry = NULL;
1752 static const struct got_error *
1753 tree_view_walk_path(struct tog_tree_view_state *s,
1754 struct got_object_id *commit_id,
1755 const char *path, struct got_repository *repo)
1757 const struct got_error *err = NULL;
1758 struct got_tree_object *tree = NULL;
1760 char *slash, *subpath = NULL;
1762 /* Walk the path and open corresponding tree objects. */
1765 struct got_tree_entry *te;
1766 struct got_object_id *tree_id;
1772 /* Ensure the correct subtree entry is selected. */
1773 slash = strchr(p, '/');
1775 te_name = strdup(p);
1777 te_name = strndup(p, slash - p);
1778 if (te_name == NULL) {
1779 err = got_error_from_errno("strndup");
1782 te = got_object_tree_find_entry(s->tree, te_name);
1784 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1789 s->selected_entry = te;
1790 s->selected = got_tree_entry_get_index(te);
1791 if (s->tree != s->root)
1792 s->selected++; /* skip '..' */
1794 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1795 /* Jump to this file's entry. */
1796 s->first_displayed_entry = s->selected_entry;
1801 slash = strchr(p, '/');
1803 subpath = strndup(path, slash - path);
1805 subpath = strdup(path);
1806 if (subpath == NULL) {
1807 err = got_error_from_errno("strdup");
1811 err = got_object_id_by_path(&tree_id, repo, commit_id,
1816 err = got_object_open_as_tree(&tree, repo, tree_id);
1821 err = tree_view_visit_subtree(tree, s);
1823 got_object_tree_close(tree);
1837 static const struct got_error *
1838 browse_commit_tree(struct tog_view **new_view, int begin_x,
1839 struct commit_queue_entry *entry, const char *path,
1840 struct got_reflist_head *refs, struct got_repository *repo)
1842 const struct got_error *err = NULL;
1843 struct got_tree_object *tree;
1844 struct tog_tree_view_state *s;
1845 struct tog_view *tree_view;
1847 err = got_object_open_as_tree(&tree, repo,
1848 got_object_commit_get_tree_id(entry->commit));
1852 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1853 if (tree_view == NULL)
1854 return got_error_from_errno("view_open");
1856 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1858 got_object_tree_close(tree);
1861 s = &tree_view->state.tree;
1863 *new_view = tree_view;
1865 if (got_path_is_root_dir(path))
1868 return tree_view_walk_path(s, entry->id, path, repo);
1871 static const struct got_error *
1872 block_signals_used_by_main_thread(void)
1877 if (sigemptyset(&sigset) == -1)
1878 return got_error_from_errno("sigemptyset");
1880 /* tog handles SIGWINCH and SIGCONT */
1881 if (sigaddset(&sigset, SIGWINCH) == -1)
1882 return got_error_from_errno("sigaddset");
1883 if (sigaddset(&sigset, SIGCONT) == -1)
1884 return got_error_from_errno("sigaddset");
1886 /* ncurses handles SIGTSTP */
1887 if (sigaddset(&sigset, SIGTSTP) == -1)
1888 return got_error_from_errno("sigaddset");
1890 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1892 return got_error_set_errno(errcode, "pthread_sigmask");
1898 log_thread(void *arg)
1900 const struct got_error *err = NULL;
1902 struct tog_log_thread_args *a = arg;
1905 err = block_signals_used_by_main_thread();
1909 while (!done && !err && !tog_sigpipe_received) {
1910 err = queue_commits(a->graph, a->commits, 1, a->repo,
1911 a->in_repo_path, a->searching, a->search_next_done,
1914 if (err->code != GOT_ERR_ITER_COMPLETED)
1918 } else if (a->commits_needed > 0)
1919 a->commits_needed--;
1921 errcode = pthread_mutex_lock(&tog_mutex);
1923 err = got_error_set_errno(errcode,
1924 "pthread_mutex_lock");
1926 } else if (*a->quit)
1928 else if (*a->first_displayed_entry == NULL) {
1929 *a->first_displayed_entry =
1930 TAILQ_FIRST(&a->commits->head);
1931 *a->selected_entry = *a->first_displayed_entry;
1935 a->commits_needed = 0;
1936 else if (a->commits_needed == 0) {
1937 errcode = pthread_cond_wait(&a->need_commits,
1940 err = got_error_set_errno(errcode,
1941 "pthread_cond_wait");
1944 errcode = pthread_mutex_unlock(&tog_mutex);
1945 if (errcode && err == NULL)
1946 err = got_error_set_errno(errcode,
1947 "pthread_mutex_unlock");
1949 a->log_complete = 1;
1953 static const struct got_error *
1954 stop_log_thread(struct tog_log_view_state *s)
1956 const struct got_error *err = NULL;
1961 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1963 return got_error_set_errno(errcode,
1964 "pthread_cond_signal");
1965 errcode = pthread_mutex_unlock(&tog_mutex);
1967 return got_error_set_errno(errcode,
1968 "pthread_mutex_unlock");
1969 errcode = pthread_join(s->thread, (void **)&err);
1971 return got_error_set_errno(errcode, "pthread_join");
1972 errcode = pthread_mutex_lock(&tog_mutex);
1974 return got_error_set_errno(errcode,
1975 "pthread_mutex_lock");
1979 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1980 if (errcode && err == NULL)
1981 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1983 if (s->thread_args.repo) {
1984 got_repo_close(s->thread_args.repo);
1985 s->thread_args.repo = NULL;
1988 if (s->thread_args.graph) {
1989 got_commit_graph_close(s->thread_args.graph);
1990 s->thread_args.graph = NULL;
1996 static const struct got_error *
1997 close_log_view(struct tog_view *view)
1999 const struct got_error *err = NULL;
2000 struct tog_log_view_state *s = &view->state.log;
2002 err = stop_log_thread(s);
2003 free_commits(&s->commits);
2004 free(s->in_repo_path);
2005 s->in_repo_path = NULL;
2011 static const struct got_error *
2012 search_start_log_view(struct tog_view *view)
2014 struct tog_log_view_state *s = &view->state.log;
2016 s->matched_entry = NULL;
2017 s->search_entry = NULL;
2021 static const struct got_error *
2022 search_next_log_view(struct tog_view *view)
2024 const struct got_error *err = NULL;
2025 struct tog_log_view_state *s = &view->state.log;
2026 struct commit_queue_entry *entry;
2028 if (!view->searching) {
2029 view->search_next_done = 1;
2033 if (s->search_entry) {
2035 errcode = pthread_mutex_unlock(&tog_mutex);
2037 return got_error_set_errno(errcode,
2038 "pthread_mutex_unlock");
2039 ch = wgetch(view->window);
2040 errcode = pthread_mutex_lock(&tog_mutex);
2042 return got_error_set_errno(errcode,
2043 "pthread_mutex_lock");
2044 if (ch == KEY_BACKSPACE) {
2045 view->search_next_done = 1;
2048 if (view->searching == TOG_SEARCH_FORWARD)
2049 entry = TAILQ_NEXT(s->search_entry, entry);
2051 entry = TAILQ_PREV(s->search_entry,
2052 commit_queue_head, entry);
2053 } else if (s->matched_entry) {
2054 if (view->searching == TOG_SEARCH_FORWARD)
2055 entry = TAILQ_NEXT(s->selected_entry, entry);
2057 entry = TAILQ_PREV(s->selected_entry,
2058 commit_queue_head, entry);
2060 if (view->searching == TOG_SEARCH_FORWARD)
2061 entry = TAILQ_FIRST(&s->commits.head);
2063 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2069 if (entry == NULL) {
2070 if (s->thread_args.log_complete ||
2071 view->searching == TOG_SEARCH_BACKWARD) {
2072 view->search_next_done = 1;
2076 * Poke the log thread for more commits and return,
2077 * allowing the main loop to make progress. Search
2078 * will resume at s->search_entry once we come back.
2080 s->thread_args.commits_needed++;
2081 return trigger_log_thread(1,
2082 &s->thread_args.commits_needed,
2083 &s->thread_args.log_complete,
2084 &s->thread_args.need_commits);
2087 err = match_commit(&have_match, entry->id, entry->commit,
2092 view->search_next_done = 1;
2093 s->matched_entry = entry;
2097 s->search_entry = entry;
2098 if (view->searching == TOG_SEARCH_FORWARD)
2099 entry = TAILQ_NEXT(entry, entry);
2101 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2104 if (s->matched_entry) {
2105 int cur = s->selected_entry->idx;
2106 while (cur < s->matched_entry->idx) {
2107 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2112 while (cur > s->matched_entry->idx) {
2113 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2120 s->search_entry = NULL;
2125 static const struct got_error *
2126 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2127 struct got_reflist_head *refs, struct got_repository *repo,
2128 const char *head_ref_name, const char *in_repo_path,
2131 const struct got_error *err = NULL;
2132 struct tog_log_view_state *s = &view->state.log;
2133 struct got_repository *thread_repo = NULL;
2134 struct got_commit_graph *thread_graph = NULL;
2137 if (in_repo_path != s->in_repo_path) {
2138 free(s->in_repo_path);
2139 s->in_repo_path = strdup(in_repo_path);
2140 if (s->in_repo_path == NULL)
2141 return got_error_from_errno("strdup");
2144 /* The commit queue only contains commits being displayed. */
2145 TAILQ_INIT(&s->commits.head);
2146 s->commits.ncommits = 0;
2150 s->head_ref_name = head_ref_name;
2151 s->start_id = got_object_id_dup(start_id);
2152 if (s->start_id == NULL) {
2153 err = got_error_from_errno("got_object_id_dup");
2156 s->log_branches = log_branches;
2158 SIMPLEQ_INIT(&s->colors);
2159 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2160 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2161 get_color_value("TOG_COLOR_COMMIT"));
2164 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2165 get_color_value("TOG_COLOR_AUTHOR"));
2167 free_colors(&s->colors);
2170 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2171 get_color_value("TOG_COLOR_DATE"));
2173 free_colors(&s->colors);
2178 view->show = show_log_view;
2179 view->input = input_log_view;
2180 view->close = close_log_view;
2181 view->search_start = search_start_log_view;
2182 view->search_next = search_next_log_view;
2184 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2187 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2191 err = got_commit_graph_iter_start(thread_graph,
2192 s->start_id, s->repo, NULL, NULL);
2196 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2198 err = got_error_set_errno(errcode, "pthread_cond_init");
2202 s->thread_args.commits_needed = view->nlines;
2203 s->thread_args.graph = thread_graph;
2204 s->thread_args.commits = &s->commits;
2205 s->thread_args.in_repo_path = s->in_repo_path;
2206 s->thread_args.start_id = s->start_id;
2207 s->thread_args.repo = thread_repo;
2208 s->thread_args.log_complete = 0;
2209 s->thread_args.quit = &s->quit;
2210 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2211 s->thread_args.selected_entry = &s->selected_entry;
2212 s->thread_args.searching = &view->searching;
2213 s->thread_args.search_next_done = &view->search_next_done;
2214 s->thread_args.regex = &view->regex;
2217 close_log_view(view);
2221 static const struct got_error *
2222 show_log_view(struct tog_view *view)
2224 struct tog_log_view_state *s = &view->state.log;
2226 if (s->thread == NULL) {
2227 int errcode = pthread_create(&s->thread, NULL, log_thread,
2230 return got_error_set_errno(errcode, "pthread_create");
2233 return draw_commits(view, &s->last_displayed_entry,
2234 &s->selected_entry, s->first_displayed_entry,
2235 &s->commits, s->selected, view->nlines, s->refs,
2236 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2239 static const struct got_error *
2240 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2241 struct tog_view **focus_view, struct tog_view *view, int ch)
2243 const struct got_error *err = NULL;
2244 struct tog_log_view_state *s = &view->state.log;
2245 char *parent_path, *in_repo_path = NULL;
2246 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2248 struct got_object_id *start_id;
2258 if (s->first_displayed_entry == NULL)
2260 if (s->selected > 0)
2263 scroll_up(view, &s->first_displayed_entry, 1,
2268 if (s->first_displayed_entry == NULL)
2270 if (TAILQ_FIRST(&s->commits.head) ==
2271 s->first_displayed_entry) {
2275 scroll_up(view, &s->first_displayed_entry,
2276 view->nlines - 1, &s->commits);
2282 if (s->first_displayed_entry == NULL)
2284 if (s->selected < MIN(view->nlines - 2,
2285 s->commits.ncommits - 1)) {
2289 err = scroll_down(view, &s->first_displayed_entry, 1,
2290 &s->last_displayed_entry, &s->commits,
2291 &s->thread_args.log_complete,
2292 &s->thread_args.commits_needed,
2293 &s->thread_args.need_commits);
2297 struct commit_queue_entry *first;
2298 first = s->first_displayed_entry;
2301 err = scroll_down(view, &s->first_displayed_entry,
2302 view->nlines - 1, &s->last_displayed_entry,
2303 &s->commits, &s->thread_args.log_complete,
2304 &s->thread_args.commits_needed,
2305 &s->thread_args.need_commits);
2308 if (first == s->first_displayed_entry &&
2309 s->selected < MIN(view->nlines - 2,
2310 s->commits.ncommits - 1)) {
2311 /* can't scroll further down */
2312 s->selected = MIN(view->nlines - 2,
2313 s->commits.ncommits - 1);
2319 if (s->selected > view->nlines - 2)
2320 s->selected = view->nlines - 2;
2321 if (s->selected > s->commits.ncommits - 1)
2322 s->selected = s->commits.ncommits - 1;
2327 if (s->selected_entry == NULL)
2329 if (view_is_parent_view(view))
2330 begin_x = view_split_begin_x(view->begin_x);
2331 err = open_diff_view_for_commit(&diff_view, begin_x,
2332 s->selected_entry->commit, s->selected_entry->id,
2333 view, s->refs, s->repo);
2336 if (view_is_parent_view(view)) {
2337 err = view_close_child(view);
2340 err = view_set_child(view, diff_view);
2342 view_close(diff_view);
2345 *focus_view = diff_view;
2346 view->child_focussed = 1;
2348 *new_view = diff_view;
2351 if (s->selected_entry == NULL)
2353 if (view_is_parent_view(view))
2354 begin_x = view_split_begin_x(view->begin_x);
2355 err = browse_commit_tree(&tree_view, begin_x,
2356 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2359 if (view_is_parent_view(view)) {
2360 err = view_close_child(view);
2363 err = view_set_child(view, tree_view);
2365 view_close(tree_view);
2368 *focus_view = tree_view;
2369 view->child_focussed = 1;
2371 *new_view = tree_view;
2374 if (strcmp(s->in_repo_path, "/") == 0)
2376 parent_path = dirname(s->in_repo_path);
2377 if (parent_path && strcmp(parent_path, ".") != 0) {
2378 err = stop_log_thread(s);
2381 lv = view_open(view->nlines, view->ncols,
2382 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2384 return got_error_from_errno(
2386 err = open_log_view(lv, s->start_id, s->refs,
2387 s->repo, s->head_ref_name, parent_path,
2391 if (view_is_parent_view(view))
2394 view_set_child(view->parent, lv);
2401 err = stop_log_thread(s);
2404 lv = view_open(view->nlines, view->ncols,
2405 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2407 return got_error_from_errno("view_open");
2408 err = got_repo_match_object_id(&start_id, NULL,
2409 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2410 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2415 in_repo_path = strdup(s->in_repo_path);
2416 if (in_repo_path == NULL) {
2419 return got_error_from_errno("strdup");
2421 got_ref_list_free(s->refs);
2422 err = got_ref_list(s->refs, s->repo, NULL,
2423 got_ref_cmp_by_name, NULL);
2429 err = open_log_view(lv, start_id, s->refs, s->repo,
2430 s->head_ref_name, in_repo_path, s->log_branches);
2440 s->log_branches = !s->log_branches;
2441 err = stop_log_thread(s);
2444 lv = view_open(view->nlines, view->ncols,
2445 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2447 return got_error_from_errno("view_open");
2448 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2449 s->head_ref_name, s->in_repo_path, s->log_branches);
2464 static const struct got_error *
2465 apply_unveil(const char *repo_path, const char *worktree_path)
2467 const struct got_error *error;
2470 if (unveil("gmon.out", "rwc") != 0)
2471 return got_error_from_errno2("unveil", "gmon.out");
2473 if (repo_path && unveil(repo_path, "r") != 0)
2474 return got_error_from_errno2("unveil", repo_path);
2476 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2477 return got_error_from_errno2("unveil", worktree_path);
2479 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2480 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2482 error = got_privsep_unveil_exec_helpers();
2486 if (unveil(NULL, NULL) != 0)
2487 return got_error_from_errno("unveil");
2497 halfdelay(1); /* Do fast refresh while initial view is loading. */
2500 intrflush(stdscr, FALSE);
2501 keypad(stdscr, TRUE);
2503 if (getenv("TOG_COLORS") != NULL) {
2505 use_default_colors();
2507 signal(SIGWINCH, tog_sigwinch);
2508 signal(SIGPIPE, tog_sigpipe);
2509 signal(SIGCONT, tog_sigcont);
2512 static const struct got_error *
2513 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2514 struct got_repository *repo, struct got_worktree *worktree)
2516 const struct got_error *err = NULL;
2519 *in_repo_path = strdup("/");
2520 if (*in_repo_path == NULL)
2521 return got_error_from_errno("strdup");
2526 const char *prefix = got_worktree_get_path_prefix(worktree);
2529 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2533 if (asprintf(&p, "%s%s%s", prefix,
2534 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2535 err = got_error_from_errno("asprintf");
2539 err = got_repo_map_path(in_repo_path, repo, p, 0);
2543 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2548 static const struct got_error *
2549 cmd_log(int argc, char *argv[])
2551 const struct got_error *error;
2552 struct got_repository *repo = NULL;
2553 struct got_worktree *worktree = NULL;
2554 struct got_reflist_head refs;
2555 struct got_object_id *start_id = NULL;
2556 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2557 char *start_commit = NULL, *head_ref_name = NULL;
2558 int ch, log_branches = 0;
2559 struct tog_view *view;
2561 SIMPLEQ_INIT(&refs);
2564 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2569 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2575 start_commit = optarg;
2578 repo_path = realpath(optarg, NULL);
2579 if (repo_path == NULL)
2580 return got_error_from_errno2("realpath",
2595 cwd = getcwd(NULL, 0);
2597 return got_error_from_errno("getcwd");
2599 error = got_worktree_open(&worktree, cwd);
2600 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2603 if (repo_path == NULL) {
2606 strdup(got_worktree_get_repo_path(worktree));
2608 repo_path = strdup(cwd);
2610 if (repo_path == NULL) {
2611 error = got_error_from_errno("strdup");
2615 error = got_repo_open(&repo, repo_path, NULL);
2619 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2626 error = apply_unveil(got_repo_get_path(repo),
2627 worktree ? got_worktree_get_root_path(worktree) : NULL);
2631 if (start_commit == NULL)
2632 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2633 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2634 GOT_OBJ_TYPE_COMMIT, 1, repo);
2636 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2637 GOT_OBJ_TYPE_COMMIT, 1, repo);
2641 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2645 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2647 error = got_error_from_errno("view_open");
2651 head_ref_name = strdup(
2652 got_worktree_get_head_ref_name(worktree));
2653 if (head_ref_name == NULL) {
2654 error = got_error_from_errno("strdup");
2658 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2659 in_repo_path, log_branches);
2663 /* Release work tree lock. */
2664 got_worktree_close(worktree);
2667 error = view_loop(view);
2673 free(head_ref_name);
2675 got_repo_close(repo);
2677 got_worktree_close(worktree);
2678 got_ref_list_free(&refs);
2686 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2692 parse_next_line(FILE *f, size_t *len)
2697 const char delim[3] = { '\0', '\0', '\0'};
2699 line = fparseln(f, &linelen, &lineno, delim, 0);
2706 match_line(const char *line, regex_t *regex)
2708 regmatch_t regmatch;
2710 return regexec(regex, line, 1, ®match, 0) == 0;
2714 match_color(struct tog_colors *colors, const char *line)
2716 struct tog_color *tc = NULL;
2718 SIMPLEQ_FOREACH(tc, colors, entry) {
2719 if (match_line(line, &tc->regex))
2726 static const struct got_error *
2727 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2728 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2729 char *header, struct tog_colors *colors)
2731 const struct got_error *err;
2732 int lineno = 0, nprinted = 0;
2734 struct tog_color *tc;
2740 werase(view->window);
2743 err = format_line(&wline, &width, header, view->ncols, 0);
2748 if (view_needs_focus_indication(view))
2749 wstandout(view->window);
2750 waddwstr(view->window, wline);
2751 if (view_needs_focus_indication(view))
2752 wstandend(view->window);
2753 if (width <= view->ncols - 1)
2754 waddch(view->window, '\n');
2762 while (nprinted < max_lines) {
2763 line = parse_next_line(f, &len);
2768 if (++lineno < *first_displayed_line) {
2773 err = format_line(&wline, &width, line, view->ncols, 0);
2779 tc = match_color(colors, line);
2781 wattr_on(view->window,
2782 COLOR_PAIR(tc->colorpair), NULL);
2783 waddwstr(view->window, wline);
2785 wattr_off(view->window,
2786 COLOR_PAIR(tc->colorpair), NULL);
2787 if (width <= view->ncols - 1)
2788 waddch(view->window, '\n');
2789 if (++nprinted == 1)
2790 *first_displayed_line = lineno;
2795 *last_displayed_line = lineno;
2800 while (nprinted < view->nlines) {
2801 waddch(view->window, '\n');
2805 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2810 wstandout(view->window);
2811 waddwstr(view->window, wline);
2812 wstandend(view->window);
2819 get_datestr(time_t *time, char *datebuf)
2821 struct tm mytm, *tm;
2824 tm = gmtime_r(time, &mytm);
2827 s = asctime_r(tm, datebuf);
2830 p = strchr(s, '\n');
2836 static const struct got_error *
2837 write_commit_info(struct got_object_id *commit_id,
2838 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2840 const struct got_error *err = NULL;
2841 char datebuf[26], *datestr;
2842 struct got_commit_object *commit;
2843 char *id_str = NULL, *logmsg = NULL;
2844 time_t committer_time;
2845 const char *author, *committer;
2846 char *refs_str = NULL;
2849 err = build_refs_str(&refs_str, refs, commit_id, repo);
2854 err = got_object_open_as_commit(&commit, repo, commit_id);
2858 err = got_object_id_str(&id_str, commit_id);
2860 err = got_error_from_errno("got_object_id_str");
2864 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2865 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2866 err = got_error_from_errno("fprintf");
2869 if (fprintf(outfile, "from: %s\n",
2870 got_object_commit_get_author(commit)) < 0) {
2871 err = got_error_from_errno("fprintf");
2874 committer_time = got_object_commit_get_committer_time(commit);
2875 datestr = get_datestr(&committer_time, datebuf);
2876 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2877 err = got_error_from_errno("fprintf");
2880 author = got_object_commit_get_author(commit);
2881 committer = got_object_commit_get_committer(commit);
2882 if (strcmp(author, committer) != 0 &&
2883 fprintf(outfile, "via: %s\n", committer) < 0) {
2884 err = got_error_from_errno("fprintf");
2887 err = got_object_commit_get_logmsg(&logmsg, commit);
2890 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2891 err = got_error_from_errno("fprintf");
2898 got_object_commit_close(commit);
2902 const struct got_error *
2903 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2909 size_t noffsets = 0;
2913 *line_offsets = NULL;
2919 if (fseek(infile, 0, SEEK_END) == -1)
2920 return got_error_from_errno("fseek");
2921 len = ftell(infile) + 1;
2923 return got_error_from_errno("ftell");
2924 if (fseek(infile, 0, SEEK_SET) == -1)
2925 return got_error_from_errno("fseek");
2929 if ((buf = calloc(len, sizeof(char *))) == NULL)
2930 return got_error_from_errno("calloc");
2932 fread(buf, 1, len, infile);
2934 return got_error_from_errno("fread");
2937 if (line_offsets && nlines) {
2938 if (*line_offsets == NULL) {
2939 /* Have some data but perhaps no '\n'. */
2942 *line_offsets = calloc(1, sizeof(**line_offsets));
2943 if (*line_offsets == NULL)
2944 return got_error_from_errno("calloc");
2945 /* Skip forward over end of first line. */
2952 /* Scan '\n' offsets in remaining chunk of data. */
2954 if (buf[i] != '\n') {
2959 if (noffsets < *nlines) {
2960 off_t *o = recallocarray(*line_offsets,
2962 sizeof(**line_offsets));
2964 free(*line_offsets);
2965 *line_offsets = NULL;
2966 return got_error_from_errno(
2973 (*line_offsets)[*nlines - 1] = off;
2978 if (fflush(infile) != 0)
2979 return got_error_from_errno("fflush");
2988 static const struct got_error *
2989 create_diff(struct tog_diff_view_state *s)
2991 const struct got_error *err = NULL;
2997 err = got_error_from_errno("got_opentemp");
3000 if (s->f && fclose(s->f) != 0) {
3001 err = got_error_from_errno("fclose");
3007 err = got_object_get_type(&obj_type, s->repo, s->id1);
3009 err = got_object_get_type(&obj_type, s->repo, s->id2);
3014 case GOT_OBJ_TYPE_BLOB:
3015 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3016 s->diff_context, 0, s->repo, s->f);
3018 case GOT_OBJ_TYPE_TREE:
3019 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3020 s->diff_context, 0, s->repo, s->f);
3022 case GOT_OBJ_TYPE_COMMIT: {
3023 const struct got_object_id_queue *parent_ids;
3024 struct got_object_qid *pid;
3025 struct got_commit_object *commit2;
3027 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3030 /* Show commit info if we're diffing to a parent/root commit. */
3031 if (s->id1 == NULL) {
3032 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3036 parent_ids = got_object_commit_get_parent_ids(commit2);
3037 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3038 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3039 err = write_commit_info(s->id2, s->refs,
3047 got_object_commit_close(commit2);
3049 err = got_diff_objects_as_commits(s->id1, s->id2,
3050 s->diff_context, 0, s->repo, s->f);
3054 err = got_error(GOT_ERR_OBJ_TYPE);
3059 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3062 if (s->f && fflush(s->f) != 0 && err == NULL)
3063 err = got_error_from_errno("fflush");
3068 diff_view_indicate_progress(struct tog_view *view)
3070 mvwaddstr(view->window, 0, 0, "diffing...");
3075 static const struct got_error *
3076 search_start_diff_view(struct tog_view *view)
3078 struct tog_diff_view_state *s = &view->state.diff;
3080 s->matched_line = 0;
3084 static const struct got_error *
3085 search_next_diff_view(struct tog_view *view)
3087 struct tog_diff_view_state *s = &view->state.diff;
3090 if (!view->searching) {
3091 view->search_next_done = 1;
3095 if (s->matched_line) {
3096 if (view->searching == TOG_SEARCH_FORWARD)
3097 lineno = s->matched_line + 1;
3099 lineno = s->matched_line - 1;
3101 if (view->searching == TOG_SEARCH_FORWARD)
3112 if (lineno <= 0 || lineno > s->nlines) {
3113 if (s->matched_line == 0) {
3114 view->search_next_done = 1;
3119 if (view->searching == TOG_SEARCH_FORWARD)
3125 offset = s->line_offsets[lineno - 1];
3126 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3128 return got_error_from_errno("fseeko");
3131 line = parse_next_line(s->f, &len);
3132 if (line && match_line(line, &view->regex)) {
3133 view->search_next_done = 1;
3134 s->matched_line = lineno;
3139 if (view->searching == TOG_SEARCH_FORWARD)
3145 if (s->matched_line) {
3146 s->first_displayed_line = s->matched_line;
3147 s->selected_line = 1;
3153 static const struct got_error *
3154 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3155 struct got_object_id *id2, struct tog_view *log_view,
3156 struct got_reflist_head *refs, struct got_repository *repo)
3158 const struct got_error *err;
3159 struct tog_diff_view_state *s = &view->state.diff;
3161 if (id1 != NULL && id2 != NULL) {
3163 err = got_object_get_type(&type1, repo, id1);
3166 err = got_object_get_type(&type2, repo, id2);
3171 return got_error(GOT_ERR_OBJ_TYPE);
3173 s->first_displayed_line = 1;
3174 s->last_displayed_line = view->nlines;
3175 s->selected_line = 1;
3182 s->id1 = got_object_id_dup(id1);
3184 return got_error_from_errno("got_object_id_dup");
3188 s->id2 = got_object_id_dup(id2);
3189 if (s->id2 == NULL) {
3192 return got_error_from_errno("got_object_id_dup");
3195 s->first_displayed_line = 1;
3196 s->last_displayed_line = view->nlines;
3197 s->diff_context = 3;
3198 s->log_view = log_view;
3202 SIMPLEQ_INIT(&s->colors);
3203 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3204 err = add_color(&s->colors,
3205 "^-", TOG_COLOR_DIFF_MINUS,
3206 get_color_value("TOG_COLOR_DIFF_MINUS"));
3209 err = add_color(&s->colors, "^\\+",
3210 TOG_COLOR_DIFF_PLUS,
3211 get_color_value("TOG_COLOR_DIFF_PLUS"));
3213 free_colors(&s->colors);
3216 err = add_color(&s->colors,
3217 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3218 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3220 free_colors(&s->colors);
3224 err = add_color(&s->colors,
3225 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3226 get_color_value("TOG_COLOR_DIFF_META"));
3228 free_colors(&s->colors);
3232 err = add_color(&s->colors,
3233 "^(from|via): ", TOG_COLOR_AUTHOR,
3234 get_color_value("TOG_COLOR_AUTHOR"));
3236 free_colors(&s->colors);
3240 err = add_color(&s->colors,
3241 "^date: ", TOG_COLOR_DATE,
3242 get_color_value("TOG_COLOR_DATE"));
3244 free_colors(&s->colors);
3249 if (log_view && view_is_splitscreen(view))
3250 show_log_view(log_view); /* draw vborder */
3251 diff_view_indicate_progress(view);
3253 err = create_diff(s);
3262 view->show = show_diff_view;
3263 view->input = input_diff_view;
3264 view->close = close_diff_view;
3265 view->search_start = search_start_diff_view;
3266 view->search_next = search_next_diff_view;
3271 static const struct got_error *
3272 close_diff_view(struct tog_view *view)
3274 const struct got_error *err = NULL;
3275 struct tog_diff_view_state *s = &view->state.diff;
3281 if (s->f && fclose(s->f) == EOF)
3282 err = got_error_from_errno("fclose");
3283 free_colors(&s->colors);
3284 free(s->line_offsets);
3288 static const struct got_error *
3289 show_diff_view(struct tog_view *view)
3291 const struct got_error *err;
3292 struct tog_diff_view_state *s = &view->state.diff;
3293 char *id_str1 = NULL, *id_str2, *header;
3296 err = got_object_id_str(&id_str1, s->id1);
3300 err = got_object_id_str(&id_str2, s->id2);
3304 if (asprintf(&header, "diff %s %s",
3305 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3306 err = got_error_from_errno("asprintf");
3314 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3315 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3316 header, &s->colors);
3319 static const struct got_error *
3320 set_selected_commit(struct tog_diff_view_state *s,
3321 struct commit_queue_entry *entry)
3323 const struct got_error *err;
3324 const struct got_object_id_queue *parent_ids;
3325 struct got_commit_object *selected_commit;
3326 struct got_object_qid *pid;
3329 s->id2 = got_object_id_dup(entry->id);
3331 return got_error_from_errno("got_object_id_dup");
3333 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3336 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3338 pid = SIMPLEQ_FIRST(parent_ids);
3339 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3340 got_object_commit_close(selected_commit);
3344 static const struct got_error *
3345 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3346 struct tog_view **focus_view, struct tog_view *view, int ch)
3348 const struct got_error *err = NULL;
3349 struct tog_diff_view_state *s = &view->state.diff;
3350 struct tog_log_view_state *ls;
3351 struct commit_queue_entry *entry;
3357 if (s->first_displayed_line > 1)
3358 s->first_displayed_line--;
3362 if (s->first_displayed_line == 1)
3365 while (i++ < view->nlines - 1 &&
3366 s->first_displayed_line > 1)
3367 s->first_displayed_line--;
3372 s->first_displayed_line++;
3380 while (!s->eof && i++ < view->nlines - 1) {
3382 line = parse_next_line(s->f, NULL);
3383 s->first_displayed_line++;
3389 if (s->diff_context > 0) {
3391 diff_view_indicate_progress(view);
3392 err = create_diff(s);
3396 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3398 diff_view_indicate_progress(view);
3399 err = create_diff(s);
3404 if (s->log_view == NULL)
3406 ls = &s->log_view->state.log;
3407 entry = TAILQ_PREV(ls->selected_entry,
3408 commit_queue_head, entry);
3412 err = input_log_view(NULL, NULL, NULL, s->log_view,
3417 err = set_selected_commit(s, entry);
3421 s->first_displayed_line = 1;
3422 s->last_displayed_line = view->nlines;
3424 diff_view_indicate_progress(view);
3425 err = create_diff(s);
3429 if (s->log_view == NULL)
3431 ls = &s->log_view->state.log;
3433 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3434 ls->thread_args.commits_needed++;
3436 /* Display "loading..." in log view. */
3437 show_log_view(s->log_view);
3441 err = trigger_log_thread(1 /* load_all */,
3442 &ls->thread_args.commits_needed,
3443 &ls->thread_args.log_complete,
3444 &ls->thread_args.need_commits);
3448 err = input_log_view(NULL, NULL, NULL, s->log_view,
3453 entry = TAILQ_NEXT(ls->selected_entry, entry);
3457 err = set_selected_commit(s, entry);
3461 s->first_displayed_line = 1;
3462 s->last_displayed_line = view->nlines;
3464 diff_view_indicate_progress(view);
3465 err = create_diff(s);
3474 static const struct got_error *
3475 cmd_diff(int argc, char *argv[])
3477 const struct got_error *error = NULL;
3478 struct got_repository *repo = NULL;
3479 struct got_worktree *worktree = NULL;
3480 struct got_reflist_head refs;
3481 struct got_object_id *id1 = NULL, *id2 = NULL;
3482 char *repo_path = NULL, *cwd = NULL;
3483 char *id_str1 = NULL, *id_str2 = NULL;
3485 struct tog_view *view;
3487 SIMPLEQ_INIT(&refs);
3490 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3495 while ((ch = getopt(argc, argv, "r:")) != -1) {
3498 repo_path = realpath(optarg, NULL);
3499 if (repo_path == NULL)
3500 return got_error_from_errno2("realpath",
3513 usage_diff(); /* TODO show local worktree changes */
3514 } else if (argc == 2) {
3520 cwd = getcwd(NULL, 0);
3522 return got_error_from_errno("getcwd");
3524 error = got_worktree_open(&worktree, cwd);
3525 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3528 if (repo_path == NULL) {
3531 strdup(got_worktree_get_repo_path(worktree));
3533 repo_path = strdup(cwd);
3535 if (repo_path == NULL) {
3536 error = got_error_from_errno("strdup");
3540 error = got_repo_open(&repo, repo_path, NULL);
3546 error = apply_unveil(got_repo_get_path(repo), NULL);
3550 error = got_repo_match_object_id_prefix(&id1, id_str1,
3551 GOT_OBJ_TYPE_ANY, repo);
3555 error = got_repo_match_object_id_prefix(&id2, id_str2,
3556 GOT_OBJ_TYPE_ANY, repo);
3560 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3564 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3566 error = got_error_from_errno("view_open");
3569 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3572 error = view_loop(view);
3577 got_repo_close(repo);
3579 got_worktree_close(worktree);
3580 got_ref_list_free(&refs);
3588 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3593 struct tog_blame_line {
3595 struct got_object_id *id;
3598 static const struct got_error *
3599 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3600 const char *path, struct tog_blame_line *lines, int nlines,
3601 int blame_complete, int selected_line, int *first_displayed_line,
3602 int *last_displayed_line, int *eof, int max_lines,
3603 struct tog_colors *colors)
3605 const struct got_error *err;
3606 int lineno = 0, nprinted = 0;
3611 struct tog_blame_line *blame_line;
3612 struct got_object_id *prev_id = NULL;
3614 struct tog_color *tc;
3616 err = got_object_id_str(&id_str, id);
3621 werase(view->window);
3623 if (asprintf(&line, "commit %s", id_str) == -1) {
3624 err = got_error_from_errno("asprintf");
3629 err = format_line(&wline, &width, line, view->ncols, 0);
3634 if (view_needs_focus_indication(view))
3635 wstandout(view->window);
3636 tc = get_color(colors, TOG_COLOR_COMMIT);
3638 wattr_on(view->window,
3639 COLOR_PAIR(tc->colorpair), NULL);
3640 waddwstr(view->window, wline);
3642 wattr_off(view->window,
3643 COLOR_PAIR(tc->colorpair), NULL);
3644 if (view_needs_focus_indication(view))
3645 wstandend(view->window);
3648 if (width < view->ncols - 1)
3649 waddch(view->window, '\n');
3651 if (asprintf(&line, "[%d/%d] %s%s",
3652 *first_displayed_line - 1 + selected_line, nlines,
3653 blame_complete ? "" : "annotating... ", path) == -1) {
3655 return got_error_from_errno("asprintf");
3658 err = format_line(&wline, &width, line, view->ncols, 0);
3663 waddwstr(view->window, wline);
3666 if (width < view->ncols - 1)
3667 waddch(view->window, '\n');
3670 while (nprinted < max_lines - 2) {
3671 line = parse_next_line(f, &len);
3676 if (++lineno < *first_displayed_line) {
3681 if (view->ncols <= 9) {
3683 wline = wcsdup(L"");
3685 err = got_error_from_errno("wcsdup");
3687 err = format_line(&wline, &width, line,
3688 view->ncols - 9, 9);
3696 if (view->focussed && nprinted == selected_line - 1)
3697 wstandout(view->window);
3700 blame_line = &lines[lineno - 1];
3701 if (blame_line->annotated && prev_id &&
3702 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3704 nprinted == selected_line - 1)) {
3705 waddstr(view->window, " ");
3706 } else if (blame_line->annotated) {
3708 err = got_object_id_str(&id_str, blame_line->id);
3714 tc = get_color(colors, TOG_COLOR_COMMIT);
3716 wattr_on(view->window,
3717 COLOR_PAIR(tc->colorpair), NULL);
3718 wprintw(view->window, "%.8s", id_str);
3720 wattr_off(view->window,
3721 COLOR_PAIR(tc->colorpair), NULL);
3723 prev_id = blame_line->id;
3725 waddstr(view->window, "........");
3729 waddstr(view->window, "........");
3733 if (view->focussed && nprinted == selected_line - 1)
3734 wstandend(view->window);
3735 waddstr(view->window, " ");
3737 waddwstr(view->window, wline);
3738 if (width <= view->ncols - 1)
3739 waddch(view->window, '\n');
3740 if (++nprinted == 1)
3741 *first_displayed_line = lineno;
3746 *last_displayed_line = lineno;
3753 static const struct got_error *
3754 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3756 const struct got_error *err = NULL;
3757 struct tog_blame_cb_args *a = arg;
3758 struct tog_blame_line *line;
3761 if (nlines != a->nlines ||
3762 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3763 return got_error(GOT_ERR_RANGE);
3765 errcode = pthread_mutex_lock(&tog_mutex);
3767 return got_error_set_errno(errcode, "pthread_mutex_lock");
3769 if (*a->quit) { /* user has quit the blame view */
3770 err = got_error(GOT_ERR_ITER_COMPLETED);
3775 goto done; /* no change in this commit */
3777 line = &a->lines[lineno - 1];
3778 if (line->annotated)
3781 line->id = got_object_id_dup(id);
3782 if (line->id == NULL) {
3783 err = got_error_from_errno("got_object_id_dup");
3786 line->annotated = 1;
3788 errcode = pthread_mutex_unlock(&tog_mutex);
3790 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3795 blame_thread(void *arg)
3797 const struct got_error *err;
3798 struct tog_blame_thread_args *ta = arg;
3799 struct tog_blame_cb_args *a = ta->cb_args;
3802 err = block_signals_used_by_main_thread();
3806 err = got_blame(ta->path, a->commit_id, ta->repo,
3807 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3808 if (err && err->code == GOT_ERR_CANCELLED)
3811 errcode = pthread_mutex_lock(&tog_mutex);
3813 return (void *)got_error_set_errno(errcode,
3814 "pthread_mutex_lock");
3816 got_repo_close(ta->repo);
3820 errcode = pthread_mutex_unlock(&tog_mutex);
3821 if (errcode && err == NULL)
3822 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3827 static struct got_object_id *
3828 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3829 int first_displayed_line, int selected_line)
3831 struct tog_blame_line *line;
3836 line = &lines[first_displayed_line - 1 + selected_line - 1];
3837 if (!line->annotated)
3843 static const struct got_error *
3844 stop_blame(struct tog_blame *blame)
3846 const struct got_error *err = NULL;
3849 if (blame->thread) {
3851 errcode = pthread_mutex_unlock(&tog_mutex);
3853 return got_error_set_errno(errcode,
3854 "pthread_mutex_unlock");
3855 errcode = pthread_join(blame->thread, (void **)&err);
3857 return got_error_set_errno(errcode, "pthread_join");
3858 errcode = pthread_mutex_lock(&tog_mutex);
3860 return got_error_set_errno(errcode,
3861 "pthread_mutex_lock");
3862 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3864 blame->thread = NULL;
3866 if (blame->thread_args.repo) {
3867 got_repo_close(blame->thread_args.repo);
3868 blame->thread_args.repo = NULL;
3871 if (fclose(blame->f) != 0 && err == NULL)
3872 err = got_error_from_errno("fclose");
3876 for (i = 0; i < blame->nlines; i++)
3877 free(blame->lines[i].id);
3879 blame->lines = NULL;
3881 free(blame->cb_args.commit_id);
3882 blame->cb_args.commit_id = NULL;
3887 static const struct got_error *
3888 cancel_blame_view(void *arg)
3890 const struct got_error *err = NULL;
3894 errcode = pthread_mutex_lock(&tog_mutex);
3896 return got_error_set_errno(errcode,
3897 "pthread_mutex_unlock");
3900 err = got_error(GOT_ERR_CANCELLED);
3902 errcode = pthread_mutex_unlock(&tog_mutex);
3904 return got_error_set_errno(errcode,
3905 "pthread_mutex_lock");
3910 static const struct got_error *
3911 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3912 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3913 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3914 struct got_repository *repo)
3916 const struct got_error *err = NULL;
3917 struct got_blob_object *blob = NULL;
3918 struct got_repository *thread_repo = NULL;
3919 struct got_object_id *obj_id = NULL;
3922 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3926 err = got_object_get_type(&obj_type, repo, obj_id);
3930 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3931 err = got_error(GOT_ERR_OBJ_TYPE);
3935 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3938 blame->f = got_opentemp();
3939 if (blame->f == NULL) {
3940 err = got_error_from_errno("got_opentemp");
3943 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3944 &blame->line_offsets, blame->f, blob);
3945 if (err || blame->nlines == 0)
3948 /* Don't include \n at EOF in the blame line count. */
3949 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3952 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3953 if (blame->lines == NULL) {
3954 err = got_error_from_errno("calloc");
3958 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3962 blame->cb_args.view = view;
3963 blame->cb_args.lines = blame->lines;
3964 blame->cb_args.nlines = blame->nlines;
3965 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3966 if (blame->cb_args.commit_id == NULL) {
3967 err = got_error_from_errno("got_object_id_dup");
3970 blame->cb_args.quit = done;
3972 blame->thread_args.path = path;
3973 blame->thread_args.repo = thread_repo;
3974 blame->thread_args.cb_args = &blame->cb_args;
3975 blame->thread_args.complete = blame_complete;
3976 blame->thread_args.cancel_cb = cancel_blame_view;
3977 blame->thread_args.cancel_arg = done;
3978 *blame_complete = 0;
3982 got_object_blob_close(blob);
3989 static const struct got_error *
3990 open_blame_view(struct tog_view *view, char *path,
3991 struct got_object_id *commit_id, struct got_reflist_head *refs,
3992 struct got_repository *repo)
3994 const struct got_error *err = NULL;
3995 struct tog_blame_view_state *s = &view->state.blame;
3997 SIMPLEQ_INIT(&s->blamed_commits);
3999 s->path = strdup(path);
4000 if (s->path == NULL)
4001 return got_error_from_errno("strdup");
4003 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4009 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4010 s->first_displayed_line = 1;
4011 s->last_displayed_line = view->nlines;
4012 s->selected_line = 1;
4013 s->blame_complete = 0;
4016 s->commit_id = commit_id;
4017 memset(&s->blame, 0, sizeof(s->blame));
4019 SIMPLEQ_INIT(&s->colors);
4020 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4021 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4022 get_color_value("TOG_COLOR_COMMIT"));
4027 view->show = show_blame_view;
4028 view->input = input_blame_view;
4029 view->close = close_blame_view;
4030 view->search_start = search_start_blame_view;
4031 view->search_next = search_next_blame_view;
4033 return run_blame(&s->blame, view, &s->blame_complete,
4034 &s->first_displayed_line, &s->last_displayed_line,
4035 &s->selected_line, &s->done, &s->eof, s->path,
4036 s->blamed_commit->id, s->repo);
4039 static const struct got_error *
4040 close_blame_view(struct tog_view *view)
4042 const struct got_error *err = NULL;
4043 struct tog_blame_view_state *s = &view->state.blame;
4045 if (s->blame.thread)
4046 err = stop_blame(&s->blame);
4048 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4049 struct got_object_qid *blamed_commit;
4050 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4051 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4052 got_object_qid_free(blamed_commit);
4056 free_colors(&s->colors);
4061 static const struct got_error *
4062 search_start_blame_view(struct tog_view *view)
4064 struct tog_blame_view_state *s = &view->state.blame;
4066 s->matched_line = 0;
4070 static const struct got_error *
4071 search_next_blame_view(struct tog_view *view)
4073 struct tog_blame_view_state *s = &view->state.blame;
4076 if (!view->searching) {
4077 view->search_next_done = 1;
4081 if (s->matched_line) {
4082 if (view->searching == TOG_SEARCH_FORWARD)
4083 lineno = s->matched_line + 1;
4085 lineno = s->matched_line - 1;
4087 if (view->searching == TOG_SEARCH_FORWARD)
4090 lineno = s->blame.nlines;
4098 if (lineno <= 0 || lineno > s->blame.nlines) {
4099 if (s->matched_line == 0) {
4100 view->search_next_done = 1;
4105 if (view->searching == TOG_SEARCH_FORWARD)
4108 lineno = s->blame.nlines;
4111 offset = s->blame.line_offsets[lineno - 1];
4112 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4114 return got_error_from_errno("fseeko");
4117 line = parse_next_line(s->blame.f, &len);
4118 if (line && match_line(line, &view->regex)) {
4119 view->search_next_done = 1;
4120 s->matched_line = lineno;
4125 if (view->searching == TOG_SEARCH_FORWARD)
4131 if (s->matched_line) {
4132 s->first_displayed_line = s->matched_line;
4133 s->selected_line = 1;
4139 static const struct got_error *
4140 show_blame_view(struct tog_view *view)
4142 const struct got_error *err = NULL;
4143 struct tog_blame_view_state *s = &view->state.blame;
4146 if (s->blame.thread == NULL) {
4147 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4148 &s->blame.thread_args);
4150 return got_error_set_errno(errcode, "pthread_create");
4152 halfdelay(1); /* fast refresh while annotating */
4155 if (s->blame_complete)
4156 halfdelay(10); /* disable fast refresh */
4158 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4159 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4160 s->selected_line, &s->first_displayed_line,
4161 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4167 static const struct got_error *
4168 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4169 struct tog_view **focus_view, struct tog_view *view, int ch)
4171 const struct got_error *err = NULL, *thread_err = NULL;
4172 struct tog_view *diff_view;
4173 struct tog_blame_view_state *s = &view->state.blame;
4182 if (s->selected_line > 1)
4184 else if (s->selected_line == 1 &&
4185 s->first_displayed_line > 1)
4186 s->first_displayed_line--;
4190 if (s->first_displayed_line == 1) {
4191 s->selected_line = 1;
4194 if (s->first_displayed_line > view->nlines - 2)
4195 s->first_displayed_line -=
4198 s->first_displayed_line = 1;
4202 if (s->selected_line < view->nlines - 2 &&
4203 s->first_displayed_line +
4204 s->selected_line <= s->blame.nlines)
4206 else if (s->last_displayed_line <
4208 s->first_displayed_line++;
4212 struct got_object_id *id = NULL;
4213 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4214 s->first_displayed_line, s->selected_line);
4218 struct got_commit_object *commit;
4219 struct got_object_qid *pid;
4220 struct got_object_id *blob_id = NULL;
4222 err = got_object_open_as_commit(&commit,
4226 pid = SIMPLEQ_FIRST(
4227 got_object_commit_get_parent_ids(commit));
4229 got_object_commit_close(commit);
4232 /* Check if path history ends here. */
4233 err = got_object_id_by_path(&blob_id, s->repo,
4236 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4238 got_object_commit_close(commit);
4241 err = got_object_get_type(&obj_type, s->repo,
4244 /* Can't blame non-blob type objects. */
4245 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4246 got_object_commit_close(commit);
4249 err = got_object_qid_alloc(&s->blamed_commit,
4251 got_object_commit_close(commit);
4253 if (got_object_id_cmp(id,
4254 s->blamed_commit->id) == 0)
4256 err = got_object_qid_alloc(&s->blamed_commit,
4262 thread_err = stop_blame(&s->blame);
4266 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4267 s->blamed_commit, entry);
4268 err = run_blame(&s->blame, view, &s->blame_complete,
4269 &s->first_displayed_line, &s->last_displayed_line,
4270 &s->selected_line, &s->done, &s->eof,
4271 s->path, s->blamed_commit->id, s->repo);
4277 struct got_object_qid *first;
4278 first = SIMPLEQ_FIRST(&s->blamed_commits);
4279 if (!got_object_id_cmp(first->id, s->commit_id))
4282 thread_err = stop_blame(&s->blame);
4286 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4287 got_object_qid_free(s->blamed_commit);
4289 SIMPLEQ_FIRST(&s->blamed_commits);
4290 err = run_blame(&s->blame, view, &s->blame_complete,
4291 &s->first_displayed_line, &s->last_displayed_line,
4292 &s->selected_line, &s->done, &s->eof, s->path,
4293 s->blamed_commit->id, s->repo);
4300 struct got_object_id *id = NULL;
4301 struct got_object_qid *pid;
4302 struct got_commit_object *commit = NULL;
4303 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4304 s->first_displayed_line, s->selected_line);
4307 err = got_object_open_as_commit(&commit, s->repo, id);
4310 pid = SIMPLEQ_FIRST(
4311 got_object_commit_get_parent_ids(commit));
4312 if (view_is_parent_view(view))
4313 begin_x = view_split_begin_x(view->begin_x);
4314 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4315 if (diff_view == NULL) {
4316 got_object_commit_close(commit);
4317 err = got_error_from_errno("view_open");
4320 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4321 id, NULL, s->refs, s->repo);
4322 got_object_commit_close(commit);
4324 view_close(diff_view);
4327 if (view_is_parent_view(view)) {
4328 err = view_close_child(view);
4331 err = view_set_child(view, diff_view);
4333 view_close(diff_view);
4336 *focus_view = diff_view;
4337 view->child_focussed = 1;
4339 *new_view = diff_view;
4347 if (s->last_displayed_line >= s->blame.nlines &&
4348 s->selected_line >= MIN(s->blame.nlines,
4349 view->nlines - 2)) {
4352 if (s->last_displayed_line >= s->blame.nlines &&
4353 s->selected_line < view->nlines - 2) {
4354 s->selected_line = MIN(s->blame.nlines,
4358 if (s->last_displayed_line + view->nlines - 2
4360 s->first_displayed_line +=
4363 s->first_displayed_line =
4368 if (s->selected_line > view->nlines - 2) {
4369 s->selected_line = MIN(s->blame.nlines,
4376 return thread_err ? thread_err : err;
4379 static const struct got_error *
4380 cmd_blame(int argc, char *argv[])
4382 const struct got_error *error;
4383 struct got_repository *repo = NULL;
4384 struct got_reflist_head refs;
4385 struct got_worktree *worktree = NULL;
4386 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4387 struct got_object_id *commit_id = NULL;
4388 char *commit_id_str = NULL;
4390 struct tog_view *view;
4392 SIMPLEQ_INIT(&refs);
4395 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4400 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4403 commit_id_str = optarg;
4406 repo_path = realpath(optarg, NULL);
4407 if (repo_path == NULL)
4408 return got_error_from_errno2("realpath",
4423 cwd = getcwd(NULL, 0);
4425 return got_error_from_errno("getcwd");
4427 error = got_worktree_open(&worktree, cwd);
4428 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4431 if (repo_path == NULL) {
4434 strdup(got_worktree_get_repo_path(worktree));
4436 repo_path = strdup(cwd);
4438 if (repo_path == NULL) {
4439 error = got_error_from_errno("strdup");
4443 error = got_repo_open(&repo, repo_path, NULL);
4447 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4454 error = apply_unveil(got_repo_get_path(repo), NULL);
4458 if (commit_id_str == NULL) {
4459 struct got_reference *head_ref;
4460 error = got_ref_open(&head_ref, repo, worktree ?
4461 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4464 error = got_ref_resolve(&commit_id, repo, head_ref);
4465 got_ref_close(head_ref);
4467 error = got_repo_match_object_id(&commit_id, NULL,
4468 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4473 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4477 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4479 error = got_error_from_errno("view_open");
4482 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4486 /* Release work tree lock. */
4487 got_worktree_close(worktree);
4490 error = view_loop(view);
4497 got_worktree_close(worktree);
4499 got_repo_close(repo);
4500 got_ref_list_free(&refs);
4504 static const struct got_error *
4505 draw_tree_entries(struct tog_view *view,
4506 struct got_tree_entry **first_displayed_entry,
4507 struct got_tree_entry **last_displayed_entry,
4508 struct got_tree_entry **selected_entry, int *ndisplayed,
4509 const char *label, int show_ids, const char *parent_path,
4510 struct got_tree_object *tree, int selected, int limit,
4511 int isroot, struct tog_colors *colors)
4513 const struct got_error *err = NULL;
4514 struct got_tree_entry *te;
4516 struct tog_color *tc;
4517 int width, n, i, nentries;
4521 werase(view->window);
4526 err = format_line(&wline, &width, label, view->ncols, 0);
4529 if (view_needs_focus_indication(view))
4530 wstandout(view->window);
4531 tc = get_color(colors, TOG_COLOR_COMMIT);
4533 wattr_on(view->window,
4534 COLOR_PAIR(tc->colorpair), NULL);
4535 waddwstr(view->window, wline);
4537 wattr_off(view->window,
4538 COLOR_PAIR(tc->colorpair), NULL);
4539 if (view_needs_focus_indication(view))
4540 wstandend(view->window);
4543 if (width < view->ncols - 1)
4544 waddch(view->window, '\n');
4547 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4550 waddwstr(view->window, wline);
4553 if (width < view->ncols - 1)
4554 waddch(view->window, '\n');
4557 waddch(view->window, '\n');
4561 if (*first_displayed_entry == NULL) {
4562 te = got_object_tree_get_first_entry(tree);
4563 if (selected == 0) {
4565 wstandout(view->window);
4566 *selected_entry = NULL;
4568 waddstr(view->window, " ..\n"); /* parent directory */
4569 if (selected == 0 && view->focussed)
4570 wstandend(view->window);
4577 te = *first_displayed_entry;
4580 nentries = got_object_tree_get_nentries(tree);
4581 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4582 char *line = NULL, *id_str = NULL;
4583 const char *modestr = "";
4586 te = got_object_tree_get_entry(tree, i);
4587 mode = got_tree_entry_get_mode(te);
4590 err = got_object_id_str(&id_str,
4591 got_tree_entry_get_id(te));
4593 return got_error_from_errno(
4594 "got_object_id_str");
4596 if (got_object_tree_entry_is_submodule(te))
4598 else if (S_ISLNK(mode))
4600 else if (S_ISDIR(mode))
4602 else if (mode & S_IXUSR)
4604 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4605 got_tree_entry_get_name(te), modestr) == -1) {
4607 return got_error_from_errno("asprintf");
4610 err = format_line(&wline, &width, line, view->ncols, 0);
4615 if (n == selected) {
4617 wstandout(view->window);
4618 *selected_entry = te;
4620 tc = match_color(colors, line);
4622 wattr_on(view->window,
4623 COLOR_PAIR(tc->colorpair), NULL);
4624 waddwstr(view->window, wline);
4626 wattr_off(view->window,
4627 COLOR_PAIR(tc->colorpair), NULL);
4628 if (width < view->ncols - 1)
4629 waddch(view->window, '\n');
4630 if (n == selected && view->focussed)
4631 wstandend(view->window);
4637 *last_displayed_entry = te;
4646 tree_scroll_up(struct tog_view *view,
4647 struct got_tree_entry **first_displayed_entry, int maxscroll,
4648 struct got_tree_object *tree, int isroot)
4650 struct got_tree_entry *te;
4653 if (*first_displayed_entry == NULL)
4656 te = got_object_tree_get_entry(tree, 0);
4657 if (*first_displayed_entry == te) {
4659 *first_displayed_entry = NULL;
4664 while (*first_displayed_entry && i < maxscroll) {
4665 *first_displayed_entry = got_tree_entry_get_prev(tree,
4666 *first_displayed_entry);
4669 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4670 *first_displayed_entry = NULL;
4674 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4675 struct got_tree_entry *last_displayed_entry,
4676 struct got_tree_object *tree)
4678 struct got_tree_entry *next, *last;
4681 if (*first_displayed_entry)
4682 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4684 next = got_object_tree_get_first_entry(tree);
4686 last = last_displayed_entry;
4687 while (next && last && n++ < maxscroll) {
4688 last = got_tree_entry_get_next(tree, last);
4690 *first_displayed_entry = next;
4691 next = got_tree_entry_get_next(tree, next);
4697 static const struct got_error *
4698 tree_entry_path(char **path, struct tog_parent_trees *parents,
4699 struct got_tree_entry *te)
4701 const struct got_error *err = NULL;
4702 struct tog_parent_tree *pt;
4703 size_t len = 2; /* for leading slash and NUL */
4705 TAILQ_FOREACH(pt, parents, entry)
4706 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4709 len += strlen(got_tree_entry_get_name(te));
4711 *path = calloc(1, len);
4713 return got_error_from_errno("calloc");
4716 pt = TAILQ_LAST(parents, tog_parent_trees);
4718 const char *name = got_tree_entry_get_name(pt->selected_entry);
4719 if (strlcat(*path, name, len) >= len) {
4720 err = got_error(GOT_ERR_NO_SPACE);
4723 if (strlcat(*path, "/", len) >= len) {
4724 err = got_error(GOT_ERR_NO_SPACE);
4727 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4730 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4731 err = got_error(GOT_ERR_NO_SPACE);
4743 static const struct got_error *
4744 blame_tree_entry(struct tog_view **new_view, int begin_x,
4745 struct got_tree_entry *te, struct tog_parent_trees *parents,
4746 struct got_object_id *commit_id, struct got_reflist_head *refs,
4747 struct got_repository *repo)
4749 const struct got_error *err = NULL;
4751 struct tog_view *blame_view;
4755 err = tree_entry_path(&path, parents, te);
4759 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4760 if (blame_view == NULL) {
4761 err = got_error_from_errno("view_open");
4765 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4767 if (err->code == GOT_ERR_CANCELLED)
4769 view_close(blame_view);
4771 *new_view = blame_view;
4777 static const struct got_error *
4778 log_tree_entry(struct tog_view **new_view, int begin_x,
4779 struct got_tree_entry *te, struct tog_parent_trees *parents,
4780 struct got_object_id *commit_id, struct got_reflist_head *refs,
4781 struct got_repository *repo)
4783 struct tog_view *log_view;
4784 const struct got_error *err = NULL;
4789 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4790 if (log_view == NULL)
4791 return got_error_from_errno("view_open");
4793 err = tree_entry_path(&path, parents, te);
4797 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4799 view_close(log_view);
4801 *new_view = log_view;
4806 static const struct got_error *
4807 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4808 struct got_object_id *commit_id, struct got_reflist_head *refs,
4809 struct got_repository *repo)
4811 const struct got_error *err = NULL;
4812 char *commit_id_str = NULL;
4813 struct tog_tree_view_state *s = &view->state.tree;
4815 TAILQ_INIT(&s->parents);
4817 err = got_object_id_str(&commit_id_str, commit_id);
4821 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4822 err = got_error_from_errno("asprintf");
4826 s->root = s->tree = root;
4827 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4828 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4829 s->commit_id = got_object_id_dup(commit_id);
4830 if (s->commit_id == NULL) {
4831 err = got_error_from_errno("got_object_id_dup");
4837 SIMPLEQ_INIT(&s->colors);
4839 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4840 err = add_color(&s->colors, "\\$$",
4841 TOG_COLOR_TREE_SUBMODULE,
4842 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4845 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4846 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4848 free_colors(&s->colors);
4851 err = add_color(&s->colors, "/$",
4852 TOG_COLOR_TREE_DIRECTORY,
4853 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4855 free_colors(&s->colors);
4859 err = add_color(&s->colors, "\\*$",
4860 TOG_COLOR_TREE_EXECUTABLE,
4861 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4863 free_colors(&s->colors);
4867 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4868 get_color_value("TOG_COLOR_COMMIT"));
4870 free_colors(&s->colors);
4875 view->show = show_tree_view;
4876 view->input = input_tree_view;
4877 view->close = close_tree_view;
4878 view->search_start = search_start_tree_view;
4879 view->search_next = search_next_tree_view;
4881 free(commit_id_str);
4883 free(s->tree_label);
4884 s->tree_label = NULL;
4889 static const struct got_error *
4890 close_tree_view(struct tog_view *view)
4892 struct tog_tree_view_state *s = &view->state.tree;
4894 free_colors(&s->colors);
4895 free(s->tree_label);
4896 s->tree_label = NULL;
4898 s->commit_id = NULL;
4899 while (!TAILQ_EMPTY(&s->parents)) {
4900 struct tog_parent_tree *parent;
4901 parent = TAILQ_FIRST(&s->parents);
4902 TAILQ_REMOVE(&s->parents, parent, entry);
4906 if (s->tree != s->root)
4907 got_object_tree_close(s->tree);
4908 got_object_tree_close(s->root);
4913 static const struct got_error *
4914 search_start_tree_view(struct tog_view *view)
4916 struct tog_tree_view_state *s = &view->state.tree;
4918 s->matched_entry = NULL;
4923 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4925 regmatch_t regmatch;
4927 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4931 static const struct got_error *
4932 search_next_tree_view(struct tog_view *view)
4934 struct tog_tree_view_state *s = &view->state.tree;
4935 struct got_tree_entry *te = NULL;
4937 if (!view->searching) {
4938 view->search_next_done = 1;
4942 if (s->matched_entry) {
4943 if (view->searching == TOG_SEARCH_FORWARD) {
4944 if (s->selected_entry)
4945 te = got_tree_entry_get_next(s->tree,
4948 te = got_object_tree_get_first_entry(s->tree);
4950 if (s->selected_entry == NULL)
4951 te = got_object_tree_get_last_entry(s->tree);
4953 te = got_tree_entry_get_prev(s->tree,
4957 if (view->searching == TOG_SEARCH_FORWARD)
4958 te = got_object_tree_get_first_entry(s->tree);
4960 te = got_object_tree_get_last_entry(s->tree);
4965 if (s->matched_entry == NULL) {
4966 view->search_next_done = 1;
4969 if (view->searching == TOG_SEARCH_FORWARD)
4970 te = got_object_tree_get_first_entry(s->tree);
4972 te = got_object_tree_get_last_entry(s->tree);
4975 if (match_tree_entry(te, &view->regex)) {
4976 view->search_next_done = 1;
4977 s->matched_entry = te;
4981 if (view->searching == TOG_SEARCH_FORWARD)
4982 te = got_tree_entry_get_next(s->tree, te);
4984 te = got_tree_entry_get_prev(s->tree, te);
4987 if (s->matched_entry) {
4988 s->first_displayed_entry = s->matched_entry;
4995 static const struct got_error *
4996 show_tree_view(struct tog_view *view)
4998 const struct got_error *err = NULL;
4999 struct tog_tree_view_state *s = &view->state.tree;
5002 err = tree_entry_path(&parent_path, &s->parents, NULL);
5006 err = draw_tree_entries(view, &s->first_displayed_entry,
5007 &s->last_displayed_entry, &s->selected_entry,
5008 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5009 s->tree, s->selected, view->nlines, s->tree == s->root,
5017 static const struct got_error *
5018 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5019 struct tog_view **focus_view, struct tog_view *view, int ch)
5021 const struct got_error *err = NULL;
5022 struct tog_tree_view_state *s = &view->state.tree;
5023 struct tog_view *log_view;
5024 int begin_x = 0, nscrolled;
5028 s->show_ids = !s->show_ids;
5031 if (!s->selected_entry)
5033 if (view_is_parent_view(view))
5034 begin_x = view_split_begin_x(view->begin_x);
5035 err = log_tree_entry(&log_view, begin_x,
5036 s->selected_entry, &s->parents,
5037 s->commit_id, s->refs, s->repo);
5038 if (view_is_parent_view(view)) {
5039 err = view_close_child(view);
5042 err = view_set_child(view, log_view);
5044 view_close(log_view);
5047 *focus_view = log_view;
5048 view->child_focussed = 1;
5050 *new_view = log_view;
5054 if (s->selected > 0) {
5056 if (s->selected == 0)
5059 if (s->selected > 0)
5061 tree_scroll_up(view, &s->first_displayed_entry, 1,
5062 s->tree, s->tree == s->root);
5066 tree_scroll_up(view, &s->first_displayed_entry,
5067 MAX(0, view->nlines - 4 - s->selected), s->tree,
5068 s->tree == s->root);
5070 if (got_object_tree_get_first_entry(s->tree) ==
5071 s->first_displayed_entry && s->tree != s->root)
5072 s->first_displayed_entry = NULL;
5076 if (s->selected < s->ndisplayed - 1) {
5080 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5082 /* can't scroll any further */
5084 tree_scroll_down(&s->first_displayed_entry, 1,
5085 s->last_displayed_entry, s->tree);
5089 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5091 /* can't scroll any further; move cursor down */
5092 if (s->selected < s->ndisplayed - 1)
5093 s->selected = s->ndisplayed - 1;
5096 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5097 view->nlines, s->last_displayed_entry, s->tree);
5098 if (nscrolled < view->nlines) {
5100 struct got_tree_entry *te;
5101 te = s->first_displayed_entry;
5104 te = got_tree_entry_get_next(s->tree, te);
5106 s->selected = ndisplayed - 1;
5112 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5113 struct tog_parent_tree *parent;
5114 /* user selected '..' */
5115 if (s->tree == s->root)
5117 parent = TAILQ_FIRST(&s->parents);
5118 TAILQ_REMOVE(&s->parents, parent,
5120 got_object_tree_close(s->tree);
5121 s->tree = parent->tree;
5122 s->first_displayed_entry =
5123 parent->first_displayed_entry;
5125 parent->selected_entry;
5126 s->selected = parent->selected;
5128 } else if (S_ISDIR(got_tree_entry_get_mode(
5129 s->selected_entry))) {
5130 struct got_tree_object *subtree;
5131 err = got_object_open_as_tree(&subtree, s->repo,
5132 got_tree_entry_get_id(s->selected_entry));
5135 err = tree_view_visit_subtree(subtree, s);
5137 got_object_tree_close(subtree);
5140 } else if (S_ISREG(got_tree_entry_get_mode(
5141 s->selected_entry))) {
5142 struct tog_view *blame_view;
5143 int begin_x = view_is_parent_view(view) ?
5144 view_split_begin_x(view->begin_x) : 0;
5146 err = blame_tree_entry(&blame_view, begin_x,
5147 s->selected_entry, &s->parents,
5148 s->commit_id, s->refs, s->repo);
5151 if (view_is_parent_view(view)) {
5152 err = view_close_child(view);
5155 err = view_set_child(view, blame_view);
5157 view_close(blame_view);
5160 *focus_view = blame_view;
5161 view->child_focussed = 1;
5163 *new_view = blame_view;
5167 if (s->selected > view->nlines)
5168 s->selected = s->ndisplayed - 1;
5181 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5186 static const struct got_error *
5187 cmd_tree(int argc, char *argv[])
5189 const struct got_error *error;
5190 struct got_repository *repo = NULL;
5191 struct got_worktree *worktree = NULL;
5192 struct got_reflist_head refs;
5193 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5194 struct got_object_id *commit_id = NULL;
5195 char *commit_id_arg = NULL;
5196 struct got_commit_object *commit = NULL;
5197 struct got_tree_object *tree = NULL;
5199 struct tog_view *view;
5201 SIMPLEQ_INIT(&refs);
5204 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5209 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5212 commit_id_arg = optarg;
5215 repo_path = realpath(optarg, NULL);
5216 if (repo_path == NULL)
5217 return got_error_from_errno2("realpath",
5232 cwd = getcwd(NULL, 0);
5234 return got_error_from_errno("getcwd");
5236 error = got_worktree_open(&worktree, cwd);
5237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5240 if (repo_path == NULL) {
5243 strdup(got_worktree_get_repo_path(worktree));
5245 repo_path = strdup(cwd);
5247 if (repo_path == NULL) {
5248 error = got_error_from_errno("strdup");
5252 error = got_repo_open(&repo, repo_path, NULL);
5256 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5263 error = apply_unveil(got_repo_get_path(repo), NULL);
5267 error = got_repo_match_object_id(&commit_id, NULL,
5268 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5269 GOT_OBJ_TYPE_COMMIT, 1, repo);
5273 error = got_object_open_as_commit(&commit, repo, commit_id);
5277 error = got_object_open_as_tree(&tree, repo,
5278 got_object_commit_get_tree_id(commit));
5282 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5286 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5288 error = got_error_from_errno("view_open");
5291 error = open_tree_view(view, tree, commit_id, &refs, repo);
5294 if (!got_path_is_root_dir(in_repo_path)) {
5295 error = tree_view_walk_path(&view->state.tree, commit_id,
5296 in_repo_path, repo);
5302 /* Release work tree lock. */
5303 got_worktree_close(worktree);
5306 error = view_loop(view);
5312 got_object_commit_close(commit);
5314 got_object_tree_close(tree);
5316 got_repo_close(repo);
5317 got_ref_list_free(&refs);
5326 fprintf(stderr, "commands:");
5327 for (i = 0; i < nitems(tog_commands); i++) {
5328 struct tog_cmd *cmd = &tog_commands[i];
5329 fprintf(stderr, " %s", cmd->name);
5331 fputc('\n', stderr);
5337 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5338 "[arg ...]\n", getprogname());
5340 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5347 make_argv(int argc, ...)
5355 argv = calloc(argc, sizeof(char *));
5358 for (i = 0; i < argc; i++) {
5359 argv[i] = strdup(va_arg(ap, char *));
5360 if (argv[i] == NULL)
5369 * Try to convert 'tog path' into a 'tog log path' command.
5370 * The user could simply have mistyped the command rather than knowingly
5371 * provided a path. So check whether argv[0] can in fact be resolved
5372 * to a path in the HEAD commit and print a special error if not.
5373 * This hack is for mpi@ <3
5375 static const struct got_error *
5376 tog_log_with_path(int argc, char *argv[])
5378 const struct got_error *error = NULL;
5379 struct tog_cmd *cmd = NULL;
5380 struct got_repository *repo = NULL;
5381 struct got_worktree *worktree = NULL;
5382 struct got_object_id *commit_id = NULL, *id = NULL;
5383 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5384 char *commit_id_str = NULL, **cmd_argv = NULL;
5386 cwd = getcwd(NULL, 0);
5388 return got_error_from_errno("getcwd");
5390 error = got_worktree_open(&worktree, cwd);
5391 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5395 repo_path = strdup(got_worktree_get_repo_path(worktree));
5397 repo_path = strdup(cwd);
5398 if (repo_path == NULL) {
5399 error = got_error_from_errno("strdup");
5403 error = got_repo_open(&repo, repo_path, NULL);
5407 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5412 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5413 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5414 GOT_OBJ_TYPE_COMMIT, 1, repo);
5419 got_worktree_close(worktree);
5423 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5425 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5427 fprintf(stderr, "%s: '%s' is no known command or path\n",
5428 getprogname(), argv[0]);
5433 got_repo_close(repo);
5436 error = got_object_id_str(&commit_id_str, commit_id);
5440 cmd = &tog_commands[0]; /* log */
5442 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5443 error = cmd->cmd_main(argc, cmd_argv);
5446 got_repo_close(repo);
5448 got_worktree_close(worktree);
5450 free(commit_id_str);
5457 for (i = 0; i < argc; i++)
5465 main(int argc, char *argv[])
5467 const struct got_error *error = NULL;
5468 struct tog_cmd *cmd = NULL;
5469 int ch, hflag = 0, Vflag = 0;
5470 char **cmd_argv = NULL;
5471 static struct option longopts[] = {
5472 { "version", no_argument, NULL, 'V' },
5476 setlocale(LC_CTYPE, "");
5478 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5498 got_version_print_str();
5505 /* Build an argument vector which runs a default command. */
5506 cmd = &tog_commands[0];
5508 cmd_argv = make_argv(argc, cmd->name);
5512 /* Did the user specify a command? */
5513 for (i = 0; i < nitems(tog_commands); i++) {
5514 if (strncmp(tog_commands[i].name, argv[0],
5515 strlen(argv[0])) == 0) {
5516 cmd = &tog_commands[i];
5525 /* No command specified; try log with a path */
5526 error = tog_log_with_path(argc, argv);
5531 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5537 for (i = 0; i < argc; i++)
5542 if (error && error->code != GOT_ERR_CANCELLED)
5543 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);