Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED
24 #include <curses.h>
25 #undef _XOPEN_SOURCE_EXTENDED
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
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"
48 #include "got_diff.h"
49 #include "got_opentemp.h"
50 #include "got_utf8.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_path.h"
56 #include "got_worktree.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #ifndef MAX
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #endif
66 #define CTRL(x) ((x) & 0x1f)
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 struct tog_cmd {
73 const char *name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 };
78 __dead static void usage(int, 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);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
97 };
99 enum tog_view_type {
100 TOG_VIEW_DIFF,
101 TOG_VIEW_LOG,
102 TOG_VIEW_BLAME,
103 TOG_VIEW_TREE,
104 TOG_VIEW_REF,
105 };
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
113 int idx;
114 };
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
117 int ncommits;
118 struct commit_queue_head head;
119 };
121 struct tog_color {
122 STAILQ_ENTRY(tog_color) entry;
123 regex_t regex;
124 short colorpair;
125 };
126 STAILQ_HEAD(tog_colors, tog_color);
128 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 static struct got_reflist_object_id_map *tog_refs_idmap;
131 static const struct got_error *
132 tog_load_refs(struct got_repository *repo)
134 const struct got_error *err;
136 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
137 if (err)
138 return err;
140 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
141 repo);
144 static void
145 tog_free_refs(void)
147 if (tog_refs_idmap) {
148 got_reflist_object_id_map_free(tog_refs_idmap);
149 tog_refs_idmap = NULL;
151 got_ref_list_free(&tog_refs);
154 static const struct got_error *
155 add_color(struct tog_colors *colors, const char *pattern,
156 int idx, short color)
158 const struct got_error *err = NULL;
159 struct tog_color *tc;
160 int regerr = 0;
162 if (idx < 1 || idx > COLOR_PAIRS - 1)
163 return NULL;
165 init_pair(idx, color, -1);
167 tc = calloc(1, sizeof(*tc));
168 if (tc == NULL)
169 return got_error_from_errno("calloc");
170 regerr = regcomp(&tc->regex, pattern,
171 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
172 if (regerr) {
173 static char regerr_msg[512];
174 static char err_msg[512];
175 regerror(regerr, &tc->regex, regerr_msg,
176 sizeof(regerr_msg));
177 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
178 regerr_msg);
179 err = got_error_msg(GOT_ERR_REGEX, err_msg);
180 free(tc);
181 return err;
183 tc->colorpair = idx;
184 STAILQ_INSERT_HEAD(colors, tc, entry);
185 return NULL;
188 static void
189 free_colors(struct tog_colors *colors)
191 struct tog_color *tc;
193 while (!STAILQ_EMPTY(colors)) {
194 tc = STAILQ_FIRST(colors);
195 STAILQ_REMOVE_HEAD(colors, entry);
196 regfree(&tc->regex);
197 free(tc);
201 struct tog_color *
202 get_color(struct tog_colors *colors, int colorpair)
204 struct tog_color *tc = NULL;
206 STAILQ_FOREACH(tc, colors, entry) {
207 if (tc->colorpair == colorpair)
208 return tc;
211 return NULL;
214 static int
215 default_color_value(const char *envvar)
217 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
218 return COLOR_MAGENTA;
219 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
220 return COLOR_CYAN;
221 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
222 return COLOR_YELLOW;
223 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
224 return COLOR_GREEN;
225 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
226 return COLOR_MAGENTA;
227 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
228 return COLOR_MAGENTA;
229 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
230 return COLOR_CYAN;
231 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
232 return COLOR_GREEN;
233 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
234 return COLOR_GREEN;
235 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
236 return COLOR_CYAN;
237 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
238 return COLOR_YELLOW;
239 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
240 return COLOR_GREEN;
241 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
242 return COLOR_MAGENTA;
243 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
244 return COLOR_YELLOW;
246 return -1;
249 static int
250 get_color_value(const char *envvar)
252 const char *val = getenv(envvar);
254 if (val == NULL)
255 return default_color_value(envvar);
257 if (strcasecmp(val, "black") == 0)
258 return COLOR_BLACK;
259 if (strcasecmp(val, "red") == 0)
260 return COLOR_RED;
261 if (strcasecmp(val, "green") == 0)
262 return COLOR_GREEN;
263 if (strcasecmp(val, "yellow") == 0)
264 return COLOR_YELLOW;
265 if (strcasecmp(val, "blue") == 0)
266 return COLOR_BLUE;
267 if (strcasecmp(val, "magenta") == 0)
268 return COLOR_MAGENTA;
269 if (strcasecmp(val, "cyan") == 0)
270 return COLOR_CYAN;
271 if (strcasecmp(val, "white") == 0)
272 return COLOR_WHITE;
273 if (strcasecmp(val, "default") == 0)
274 return -1;
276 return default_color_value(envvar);
280 struct tog_diff_view_state {
281 struct got_object_id *id1, *id2;
282 const char *label1, *label2;
283 FILE *f;
284 int first_displayed_line;
285 int last_displayed_line;
286 int eof;
287 int diff_context;
288 int ignore_whitespace;
289 int force_text_diff;
290 struct got_repository *repo;
291 struct tog_colors colors;
292 size_t nlines;
293 off_t *line_offsets;
294 int matched_line;
295 int selected_line;
297 /* passed from log view; may be NULL */
298 struct tog_view *log_view;
299 };
301 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
303 struct tog_log_thread_args {
304 pthread_cond_t need_commits;
305 pthread_cond_t commit_loaded;
306 int commits_needed;
307 int load_all;
308 struct got_commit_graph *graph;
309 struct commit_queue *commits;
310 const char *in_repo_path;
311 struct got_object_id *start_id;
312 struct got_repository *repo;
313 int log_complete;
314 sig_atomic_t *quit;
315 struct commit_queue_entry **first_displayed_entry;
316 struct commit_queue_entry **selected_entry;
317 int *searching;
318 int *search_next_done;
319 regex_t *regex;
320 };
322 struct tog_log_view_state {
323 struct commit_queue commits;
324 struct commit_queue_entry *first_displayed_entry;
325 struct commit_queue_entry *last_displayed_entry;
326 struct commit_queue_entry *selected_entry;
327 int selected;
328 char *in_repo_path;
329 char *head_ref_name;
330 int log_branches;
331 struct got_repository *repo;
332 struct got_object_id *start_id;
333 sig_atomic_t quit;
334 pthread_t thread;
335 struct tog_log_thread_args thread_args;
336 struct commit_queue_entry *matched_entry;
337 struct commit_queue_entry *search_entry;
338 struct tog_colors colors;
339 };
341 #define TOG_COLOR_DIFF_MINUS 1
342 #define TOG_COLOR_DIFF_PLUS 2
343 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
344 #define TOG_COLOR_DIFF_META 4
345 #define TOG_COLOR_TREE_SUBMODULE 5
346 #define TOG_COLOR_TREE_SYMLINK 6
347 #define TOG_COLOR_TREE_DIRECTORY 7
348 #define TOG_COLOR_TREE_EXECUTABLE 8
349 #define TOG_COLOR_COMMIT 9
350 #define TOG_COLOR_AUTHOR 10
351 #define TOG_COLOR_DATE 11
352 #define TOG_COLOR_REFS_HEADS 12
353 #define TOG_COLOR_REFS_TAGS 13
354 #define TOG_COLOR_REFS_REMOTES 14
356 struct tog_blame_cb_args {
357 struct tog_blame_line *lines; /* one per line */
358 int nlines;
360 struct tog_view *view;
361 struct got_object_id *commit_id;
362 int *quit;
363 };
365 struct tog_blame_thread_args {
366 const char *path;
367 struct got_repository *repo;
368 struct tog_blame_cb_args *cb_args;
369 int *complete;
370 got_cancel_cb cancel_cb;
371 void *cancel_arg;
372 };
374 struct tog_blame {
375 FILE *f;
376 off_t filesize;
377 struct tog_blame_line *lines;
378 int nlines;
379 off_t *line_offsets;
380 pthread_t thread;
381 struct tog_blame_thread_args thread_args;
382 struct tog_blame_cb_args cb_args;
383 const char *path;
384 };
386 struct tog_blame_view_state {
387 int first_displayed_line;
388 int last_displayed_line;
389 int selected_line;
390 int blame_complete;
391 int eof;
392 int done;
393 struct got_object_id_queue blamed_commits;
394 struct got_object_qid *blamed_commit;
395 char *path;
396 struct got_repository *repo;
397 struct got_object_id *commit_id;
398 struct tog_blame blame;
399 int matched_line;
400 struct tog_colors colors;
401 };
403 struct tog_parent_tree {
404 TAILQ_ENTRY(tog_parent_tree) entry;
405 struct got_tree_object *tree;
406 struct got_tree_entry *first_displayed_entry;
407 struct got_tree_entry *selected_entry;
408 int selected;
409 };
411 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
413 struct tog_tree_view_state {
414 char *tree_label;
415 struct got_object_id *commit_id;/* commit which this tree belongs to */
416 struct got_tree_object *root; /* the commit's root tree entry */
417 struct got_tree_object *tree; /* currently displayed (sub-)tree */
418 struct got_tree_entry *first_displayed_entry;
419 struct got_tree_entry *last_displayed_entry;
420 struct got_tree_entry *selected_entry;
421 int ndisplayed, selected, show_ids;
422 struct tog_parent_trees parents; /* parent trees of current sub-tree */
423 char *head_ref_name;
424 struct got_repository *repo;
425 struct got_tree_entry *matched_entry;
426 struct tog_colors colors;
427 };
429 struct tog_reflist_entry {
430 TAILQ_ENTRY(tog_reflist_entry) entry;
431 struct got_reference *ref;
432 int idx;
433 };
435 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
437 struct tog_ref_view_state {
438 struct tog_reflist_head refs;
439 struct tog_reflist_entry *first_displayed_entry;
440 struct tog_reflist_entry *last_displayed_entry;
441 struct tog_reflist_entry *selected_entry;
442 int nrefs, ndisplayed, selected, show_ids;
443 struct got_repository *repo;
444 struct tog_reflist_entry *matched_entry;
445 struct tog_colors colors;
446 };
448 /*
449 * We implement two types of views: parent views and child views.
451 * The 'Tab' key switches focus between a parent view and its child view.
452 * Child views are shown side-by-side to their parent view, provided
453 * there is enough screen estate.
455 * When a new view is opened from within a parent view, this new view
456 * becomes a child view of the parent view, replacing any existing child.
458 * When a new view is opened from within a child view, this new view
459 * becomes a parent view which will obscure the views below until the
460 * user quits the new parent view by typing 'q'.
462 * This list of views contains parent views only.
463 * Child views are only pointed to by their parent view.
464 */
465 TAILQ_HEAD(tog_view_list_head, tog_view);
467 struct tog_view {
468 TAILQ_ENTRY(tog_view) entry;
469 WINDOW *window;
470 PANEL *panel;
471 int nlines, ncols, begin_y, begin_x;
472 int lines, cols; /* copies of LINES and COLS */
473 int focussed; /* Only set on one parent or child view at a time. */
474 int dying;
475 struct tog_view *parent;
476 struct tog_view *child;
478 /*
479 * This flag is initially set on parent views when a new child view
480 * is created. It gets toggled when the 'Tab' key switches focus
481 * between parent and child.
482 * The flag indicates whether focus should be passed on to our child
483 * view if this parent view gets picked for focus after another parent
484 * view was closed. This prevents child views from losing focus in such
485 * situations.
486 */
487 int focus_child;
489 /* type-specific state */
490 enum tog_view_type type;
491 union {
492 struct tog_diff_view_state diff;
493 struct tog_log_view_state log;
494 struct tog_blame_view_state blame;
495 struct tog_tree_view_state tree;
496 struct tog_ref_view_state ref;
497 } state;
499 const struct got_error *(*show)(struct tog_view *);
500 const struct got_error *(*input)(struct tog_view **,
501 struct tog_view *, int);
502 const struct got_error *(*close)(struct tog_view *);
504 const struct got_error *(*search_start)(struct tog_view *);
505 const struct got_error *(*search_next)(struct tog_view *);
506 int search_started;
507 int searching;
508 #define TOG_SEARCH_FORWARD 1
509 #define TOG_SEARCH_BACKWARD 2
510 int search_next_done;
511 #define TOG_SEARCH_HAVE_MORE 1
512 #define TOG_SEARCH_NO_MORE 2
513 #define TOG_SEARCH_HAVE_NONE 3
514 regex_t regex;
515 regmatch_t regmatch;
516 };
518 static const struct got_error *open_diff_view(struct tog_view *,
519 struct got_object_id *, struct got_object_id *,
520 const char *, const char *, int, int, int, struct tog_view *,
521 struct got_repository *);
522 static const struct got_error *show_diff_view(struct tog_view *);
523 static const struct got_error *input_diff_view(struct tog_view **,
524 struct tog_view *, int);
525 static const struct got_error* close_diff_view(struct tog_view *);
526 static const struct got_error *search_start_diff_view(struct tog_view *);
527 static const struct got_error *search_next_diff_view(struct tog_view *);
529 static const struct got_error *open_log_view(struct tog_view *,
530 struct got_object_id *, struct got_repository *,
531 const char *, const char *, int);
532 static const struct got_error * show_log_view(struct tog_view *);
533 static const struct got_error *input_log_view(struct tog_view **,
534 struct tog_view *, int);
535 static const struct got_error *close_log_view(struct tog_view *);
536 static const struct got_error *search_start_log_view(struct tog_view *);
537 static const struct got_error *search_next_log_view(struct tog_view *);
539 static const struct got_error *open_blame_view(struct tog_view *, char *,
540 struct got_object_id *, struct got_repository *);
541 static const struct got_error *show_blame_view(struct tog_view *);
542 static const struct got_error *input_blame_view(struct tog_view **,
543 struct tog_view *, int);
544 static const struct got_error *close_blame_view(struct tog_view *);
545 static const struct got_error *search_start_blame_view(struct tog_view *);
546 static const struct got_error *search_next_blame_view(struct tog_view *);
548 static const struct got_error *open_tree_view(struct tog_view *,
549 struct got_object_id *, const char *, struct got_repository *);
550 static const struct got_error *show_tree_view(struct tog_view *);
551 static const struct got_error *input_tree_view(struct tog_view **,
552 struct tog_view *, int);
553 static const struct got_error *close_tree_view(struct tog_view *);
554 static const struct got_error *search_start_tree_view(struct tog_view *);
555 static const struct got_error *search_next_tree_view(struct tog_view *);
557 static const struct got_error *open_ref_view(struct tog_view *,
558 struct got_repository *);
559 static const struct got_error *show_ref_view(struct tog_view *);
560 static const struct got_error *input_ref_view(struct tog_view **,
561 struct tog_view *, int);
562 static const struct got_error *close_ref_view(struct tog_view *);
563 static const struct got_error *search_start_ref_view(struct tog_view *);
564 static const struct got_error *search_next_ref_view(struct tog_view *);
566 static volatile sig_atomic_t tog_sigwinch_received;
567 static volatile sig_atomic_t tog_sigpipe_received;
568 static volatile sig_atomic_t tog_sigcont_received;
570 static void
571 tog_sigwinch(int signo)
573 tog_sigwinch_received = 1;
576 static void
577 tog_sigpipe(int signo)
579 tog_sigpipe_received = 1;
582 static void
583 tog_sigcont(int signo)
585 tog_sigcont_received = 1;
588 static const struct got_error *
589 view_close(struct tog_view *view)
591 const struct got_error *err = NULL;
593 if (view->child) {
594 view_close(view->child);
595 view->child = NULL;
597 if (view->close)
598 err = view->close(view);
599 if (view->panel)
600 del_panel(view->panel);
601 if (view->window)
602 delwin(view->window);
603 free(view);
604 return err;
607 static struct tog_view *
608 view_open(int nlines, int ncols, int begin_y, int begin_x,
609 enum tog_view_type type)
611 struct tog_view *view = calloc(1, sizeof(*view));
613 if (view == NULL)
614 return NULL;
616 view->type = type;
617 view->lines = LINES;
618 view->cols = COLS;
619 view->nlines = nlines ? nlines : LINES - begin_y;
620 view->ncols = ncols ? ncols : COLS - begin_x;
621 view->begin_y = begin_y;
622 view->begin_x = begin_x;
623 view->window = newwin(nlines, ncols, begin_y, begin_x);
624 if (view->window == NULL) {
625 view_close(view);
626 return NULL;
628 view->panel = new_panel(view->window);
629 if (view->panel == NULL ||
630 set_panel_userptr(view->panel, view) != OK) {
631 view_close(view);
632 return NULL;
635 keypad(view->window, TRUE);
636 return view;
639 static int
640 view_split_begin_x(int begin_x)
642 if (begin_x > 0 || COLS < 120)
643 return 0;
644 return (COLS - MAX(COLS / 2, 80));
647 static const struct got_error *view_resize(struct tog_view *);
649 static const struct got_error *
650 view_splitscreen(struct tog_view *view)
652 const struct got_error *err = NULL;
654 view->begin_y = 0;
655 view->begin_x = view_split_begin_x(0);
656 view->nlines = LINES;
657 view->ncols = COLS - view->begin_x;
658 view->lines = LINES;
659 view->cols = COLS;
660 err = view_resize(view);
661 if (err)
662 return err;
664 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
665 return got_error_from_errno("mvwin");
667 return NULL;
670 static const struct got_error *
671 view_fullscreen(struct tog_view *view)
673 const struct got_error *err = NULL;
675 view->begin_x = 0;
676 view->begin_y = 0;
677 view->nlines = LINES;
678 view->ncols = COLS;
679 view->lines = LINES;
680 view->cols = COLS;
681 err = view_resize(view);
682 if (err)
683 return err;
685 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
686 return got_error_from_errno("mvwin");
688 return NULL;
691 static int
692 view_is_parent_view(struct tog_view *view)
694 return view->parent == NULL;
697 static const struct got_error *
698 view_resize(struct tog_view *view)
700 int nlines, ncols;
702 if (view->lines > LINES)
703 nlines = view->nlines - (view->lines - LINES);
704 else
705 nlines = view->nlines + (LINES - view->lines);
707 if (view->cols > COLS)
708 ncols = view->ncols - (view->cols - COLS);
709 else
710 ncols = view->ncols + (COLS - view->cols);
712 if (wresize(view->window, nlines, ncols) == ERR)
713 return got_error_from_errno("wresize");
714 if (replace_panel(view->panel, view->window) == ERR)
715 return got_error_from_errno("replace_panel");
716 wclear(view->window);
718 view->nlines = nlines;
719 view->ncols = ncols;
720 view->lines = LINES;
721 view->cols = COLS;
723 if (view->child) {
724 view->child->begin_x = view_split_begin_x(view->begin_x);
725 if (view->child->begin_x == 0) {
726 view_fullscreen(view->child);
727 if (view->child->focussed)
728 show_panel(view->child->panel);
729 else
730 show_panel(view->panel);
731 } else {
732 view_splitscreen(view->child);
733 show_panel(view->child->panel);
737 return NULL;
740 static const struct got_error *
741 view_close_child(struct tog_view *view)
743 const struct got_error *err = NULL;
745 if (view->child == NULL)
746 return NULL;
748 err = view_close(view->child);
749 view->child = NULL;
750 return err;
753 static void
754 view_set_child(struct tog_view *view, struct tog_view *child)
756 view->child = child;
757 child->parent = view;
760 static int
761 view_is_splitscreen(struct tog_view *view)
763 return view->begin_x > 0;
766 static void
767 tog_resizeterm(void)
769 int cols, lines;
770 struct winsize size;
772 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
773 cols = 80; /* Default */
774 lines = 24;
775 } else {
776 cols = size.ws_col;
777 lines = size.ws_row;
779 resize_term(lines, cols);
782 static const struct got_error *
783 view_search_start(struct tog_view *view)
785 const struct got_error *err = NULL;
786 char pattern[1024];
787 int ret;
789 if (view->search_started) {
790 regfree(&view->regex);
791 view->searching = 0;
792 memset(&view->regmatch, 0, sizeof(view->regmatch));
794 view->search_started = 0;
796 if (view->nlines < 1)
797 return NULL;
799 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
800 wclrtoeol(view->window);
802 nocbreak();
803 echo();
804 ret = wgetnstr(view->window, pattern, sizeof(pattern));
805 cbreak();
806 noecho();
807 if (ret == ERR)
808 return NULL;
810 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
811 err = view->search_start(view);
812 if (err) {
813 regfree(&view->regex);
814 return err;
816 view->search_started = 1;
817 view->searching = TOG_SEARCH_FORWARD;
818 view->search_next_done = 0;
819 view->search_next(view);
822 return NULL;
825 static const struct got_error *
826 view_input(struct tog_view **new, int *done, struct tog_view *view,
827 struct tog_view_list_head *views)
829 const struct got_error *err = NULL;
830 struct tog_view *v;
831 int ch, errcode;
833 *new = NULL;
835 /* Clear "no matches" indicator. */
836 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
837 view->search_next_done == TOG_SEARCH_HAVE_NONE)
838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
840 if (view->searching && !view->search_next_done) {
841 errcode = pthread_mutex_unlock(&tog_mutex);
842 if (errcode)
843 return got_error_set_errno(errcode,
844 "pthread_mutex_unlock");
845 pthread_yield();
846 errcode = pthread_mutex_lock(&tog_mutex);
847 if (errcode)
848 return got_error_set_errno(errcode,
849 "pthread_mutex_lock");
850 view->search_next(view);
851 return NULL;
854 nodelay(stdscr, FALSE);
855 /* Allow threads to make progress while we are waiting for input. */
856 errcode = pthread_mutex_unlock(&tog_mutex);
857 if (errcode)
858 return got_error_set_errno(errcode, "pthread_mutex_unlock");
859 ch = wgetch(view->window);
860 errcode = pthread_mutex_lock(&tog_mutex);
861 if (errcode)
862 return got_error_set_errno(errcode, "pthread_mutex_lock");
863 nodelay(stdscr, TRUE);
865 if (tog_sigwinch_received || tog_sigcont_received) {
866 tog_resizeterm();
867 tog_sigwinch_received = 0;
868 tog_sigcont_received = 0;
869 TAILQ_FOREACH(v, views, entry) {
870 err = view_resize(v);
871 if (err)
872 return err;
873 err = v->input(new, v, KEY_RESIZE);
874 if (err)
875 return err;
876 if (v->child) {
877 err = view_resize(v->child);
878 if (err)
879 return err;
880 err = v->child->input(new, v->child,
881 KEY_RESIZE);
882 if (err)
883 return err;
888 switch (ch) {
889 case '\t':
890 if (view->child) {
891 view->focussed = 0;
892 view->child->focussed = 1;
893 view->focus_child = 1;
894 } else if (view->parent) {
895 view->focussed = 0;
896 view->parent->focussed = 1;
897 view->parent->focus_child = 0;
899 break;
900 case 'q':
901 err = view->input(new, view, ch);
902 view->dying = 1;
903 break;
904 case 'Q':
905 *done = 1;
906 break;
907 case 'f':
908 if (view_is_parent_view(view)) {
909 if (view->child == NULL)
910 break;
911 if (view_is_splitscreen(view->child)) {
912 view->focussed = 0;
913 view->child->focussed = 1;
914 err = view_fullscreen(view->child);
915 } else
916 err = view_splitscreen(view->child);
917 if (err)
918 break;
919 err = view->child->input(new, view->child,
920 KEY_RESIZE);
921 } else {
922 if (view_is_splitscreen(view)) {
923 view->parent->focussed = 0;
924 view->focussed = 1;
925 err = view_fullscreen(view);
926 } else {
927 err = view_splitscreen(view);
929 if (err)
930 break;
931 err = view->input(new, view, KEY_RESIZE);
933 break;
934 case KEY_RESIZE:
935 break;
936 case '/':
937 if (view->search_start)
938 view_search_start(view);
939 else
940 err = view->input(new, view, ch);
941 break;
942 case 'N':
943 case 'n':
944 if (view->search_started && view->search_next) {
945 view->searching = (ch == 'n' ?
946 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
947 view->search_next_done = 0;
948 view->search_next(view);
949 } else
950 err = view->input(new, view, ch);
951 break;
952 default:
953 err = view->input(new, view, ch);
954 break;
957 return err;
960 void
961 view_vborder(struct tog_view *view)
963 PANEL *panel;
964 const struct tog_view *view_above;
966 if (view->parent)
967 return view_vborder(view->parent);
969 panel = panel_above(view->panel);
970 if (panel == NULL)
971 return;
973 view_above = panel_userptr(panel);
974 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
975 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
978 int
979 view_needs_focus_indication(struct tog_view *view)
981 if (view_is_parent_view(view)) {
982 if (view->child == NULL || view->child->focussed)
983 return 0;
984 if (!view_is_splitscreen(view->child))
985 return 0;
986 } else if (!view_is_splitscreen(view))
987 return 0;
989 return view->focussed;
992 static const struct got_error *
993 view_loop(struct tog_view *view)
995 const struct got_error *err = NULL;
996 struct tog_view_list_head views;
997 struct tog_view *new_view;
998 int fast_refresh = 10;
999 int done = 0, errcode;
1001 errcode = pthread_mutex_lock(&tog_mutex);
1002 if (errcode)
1003 return got_error_set_errno(errcode, "pthread_mutex_lock");
1005 TAILQ_INIT(&views);
1006 TAILQ_INSERT_HEAD(&views, view, entry);
1008 view->focussed = 1;
1009 err = view->show(view);
1010 if (err)
1011 return err;
1012 update_panels();
1013 doupdate();
1014 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1015 /* Refresh fast during initialization, then become slower. */
1016 if (fast_refresh && fast_refresh-- == 0)
1017 halfdelay(10); /* switch to once per second */
1019 err = view_input(&new_view, &done, view, &views);
1020 if (err)
1021 break;
1022 if (view->dying) {
1023 struct tog_view *v, *prev = NULL;
1025 if (view_is_parent_view(view))
1026 prev = TAILQ_PREV(view, tog_view_list_head,
1027 entry);
1028 else if (view->parent)
1029 prev = view->parent;
1031 if (view->parent) {
1032 view->parent->child = NULL;
1033 view->parent->focus_child = 0;
1034 } else
1035 TAILQ_REMOVE(&views, view, entry);
1037 err = view_close(view);
1038 if (err)
1039 goto done;
1041 view = NULL;
1042 TAILQ_FOREACH(v, &views, entry) {
1043 if (v->focussed)
1044 break;
1046 if (view == NULL && new_view == NULL) {
1047 /* No view has focus. Try to pick one. */
1048 if (prev)
1049 view = prev;
1050 else if (!TAILQ_EMPTY(&views)) {
1051 view = TAILQ_LAST(&views,
1052 tog_view_list_head);
1054 if (view) {
1055 if (view->focus_child) {
1056 view->child->focussed = 1;
1057 view = view->child;
1058 } else
1059 view->focussed = 1;
1063 if (new_view) {
1064 struct tog_view *v, *t;
1065 /* Only allow one parent view per type. */
1066 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1067 if (v->type != new_view->type)
1068 continue;
1069 TAILQ_REMOVE(&views, v, entry);
1070 err = view_close(v);
1071 if (err)
1072 goto done;
1073 break;
1075 TAILQ_INSERT_TAIL(&views, new_view, entry);
1076 view = new_view;
1078 if (view) {
1079 if (view_is_parent_view(view)) {
1080 if (view->child && view->child->focussed)
1081 view = view->child;
1082 } else {
1083 if (view->parent && view->parent->focussed)
1084 view = view->parent;
1086 show_panel(view->panel);
1087 if (view->child && view_is_splitscreen(view->child))
1088 show_panel(view->child->panel);
1089 if (view->parent && view_is_splitscreen(view)) {
1090 err = view->parent->show(view->parent);
1091 if (err)
1092 goto done;
1094 err = view->show(view);
1095 if (err)
1096 goto done;
1097 if (view->child) {
1098 err = view->child->show(view->child);
1099 if (err)
1100 goto done;
1102 update_panels();
1103 doupdate();
1106 done:
1107 while (!TAILQ_EMPTY(&views)) {
1108 view = TAILQ_FIRST(&views);
1109 TAILQ_REMOVE(&views, view, entry);
1110 view_close(view);
1113 errcode = pthread_mutex_unlock(&tog_mutex);
1114 if (errcode && err == NULL)
1115 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1117 return err;
1120 __dead static void
1121 usage_log(void)
1123 endwin();
1124 fprintf(stderr,
1125 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1126 getprogname());
1127 exit(1);
1130 /* Create newly allocated wide-character string equivalent to a byte string. */
1131 static const struct got_error *
1132 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1134 char *vis = NULL;
1135 const struct got_error *err = NULL;
1137 *ws = NULL;
1138 *wlen = mbstowcs(NULL, s, 0);
1139 if (*wlen == (size_t)-1) {
1140 int vislen;
1141 if (errno != EILSEQ)
1142 return got_error_from_errno("mbstowcs");
1144 /* byte string invalid in current encoding; try to "fix" it */
1145 err = got_mbsavis(&vis, &vislen, s);
1146 if (err)
1147 return err;
1148 *wlen = mbstowcs(NULL, vis, 0);
1149 if (*wlen == (size_t)-1) {
1150 err = got_error_from_errno("mbstowcs"); /* give up */
1151 goto done;
1155 *ws = calloc(*wlen + 1, sizeof(**ws));
1156 if (*ws == NULL) {
1157 err = got_error_from_errno("calloc");
1158 goto done;
1161 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1162 err = got_error_from_errno("mbstowcs");
1163 done:
1164 free(vis);
1165 if (err) {
1166 free(*ws);
1167 *ws = NULL;
1168 *wlen = 0;
1170 return err;
1173 /* Format a line for display, ensuring that it won't overflow a width limit. */
1174 static const struct got_error *
1175 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1176 int col_tab_align)
1178 const struct got_error *err = NULL;
1179 int cols = 0;
1180 wchar_t *wline = NULL;
1181 size_t wlen;
1182 int i;
1184 *wlinep = NULL;
1185 *widthp = 0;
1187 err = mbs2ws(&wline, &wlen, line);
1188 if (err)
1189 return err;
1191 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1192 wline[wlen - 1] = L'\0';
1193 wlen--;
1195 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1196 wline[wlen - 1] = L'\0';
1197 wlen--;
1200 i = 0;
1201 while (i < wlen) {
1202 int width = wcwidth(wline[i]);
1204 if (width == 0) {
1205 i++;
1206 continue;
1209 if (width == 1 || width == 2) {
1210 if (cols + width > wlimit)
1211 break;
1212 cols += width;
1213 i++;
1214 } else if (width == -1) {
1215 if (wline[i] == L'\t') {
1216 width = TABSIZE -
1217 ((cols + col_tab_align) % TABSIZE);
1218 } else {
1219 width = 1;
1220 wline[i] = L'.';
1222 if (cols + width > wlimit)
1223 break;
1224 cols += width;
1225 i++;
1226 } else {
1227 err = got_error_from_errno("wcwidth");
1228 goto done;
1231 wline[i] = L'\0';
1232 if (widthp)
1233 *widthp = cols;
1234 done:
1235 if (err)
1236 free(wline);
1237 else
1238 *wlinep = wline;
1239 return err;
1242 static const struct got_error*
1243 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1244 struct got_object_id *id, struct got_repository *repo)
1246 static const struct got_error *err = NULL;
1247 struct got_reflist_entry *re;
1248 char *s;
1249 const char *name;
1251 *refs_str = NULL;
1253 TAILQ_FOREACH(re, refs, entry) {
1254 struct got_tag_object *tag = NULL;
1255 struct got_object_id *ref_id;
1256 int cmp;
1258 name = got_ref_get_name(re->ref);
1259 if (strcmp(name, GOT_REF_HEAD) == 0)
1260 continue;
1261 if (strncmp(name, "refs/", 5) == 0)
1262 name += 5;
1263 if (strncmp(name, "got/", 4) == 0)
1264 continue;
1265 if (strncmp(name, "heads/", 6) == 0)
1266 name += 6;
1267 if (strncmp(name, "remotes/", 8) == 0) {
1268 name += 8;
1269 s = strstr(name, "/" GOT_REF_HEAD);
1270 if (s != NULL && s[strlen(s)] == '\0')
1271 continue;
1273 err = got_ref_resolve(&ref_id, repo, re->ref);
1274 if (err)
1275 break;
1276 if (strncmp(name, "tags/", 5) == 0) {
1277 err = got_object_open_as_tag(&tag, repo, ref_id);
1278 if (err) {
1279 if (err->code != GOT_ERR_OBJ_TYPE) {
1280 free(ref_id);
1281 break;
1283 /* Ref points at something other than a tag. */
1284 err = NULL;
1285 tag = NULL;
1288 cmp = got_object_id_cmp(tag ?
1289 got_object_tag_get_object_id(tag) : ref_id, id);
1290 free(ref_id);
1291 if (tag)
1292 got_object_tag_close(tag);
1293 if (cmp != 0)
1294 continue;
1295 s = *refs_str;
1296 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1297 s ? ", " : "", name) == -1) {
1298 err = got_error_from_errno("asprintf");
1299 free(s);
1300 *refs_str = NULL;
1301 break;
1303 free(s);
1306 return err;
1309 static const struct got_error *
1310 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1311 int col_tab_align)
1313 char *smallerthan;
1315 smallerthan = strchr(author, '<');
1316 if (smallerthan && smallerthan[1] != '\0')
1317 author = smallerthan + 1;
1318 author[strcspn(author, "@>")] = '\0';
1319 return format_line(wauthor, author_width, author, limit, col_tab_align);
1322 static const struct got_error *
1323 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1324 struct got_object_id *id, const size_t date_display_cols,
1325 int author_display_cols)
1327 struct tog_log_view_state *s = &view->state.log;
1328 const struct got_error *err = NULL;
1329 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1330 char *logmsg0 = NULL, *logmsg = NULL;
1331 char *author = NULL;
1332 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1333 int author_width, logmsg_width;
1334 char *newline, *line = NULL;
1335 int col, limit;
1336 const int avail = view->ncols;
1337 struct tm tm;
1338 time_t committer_time;
1339 struct tog_color *tc;
1341 committer_time = got_object_commit_get_committer_time(commit);
1342 if (gmtime_r(&committer_time, &tm) == NULL)
1343 return got_error_from_errno("gmtime_r");
1344 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1345 return got_error(GOT_ERR_NO_SPACE);
1347 if (avail <= date_display_cols)
1348 limit = MIN(sizeof(datebuf) - 1, avail);
1349 else
1350 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1351 tc = get_color(&s->colors, TOG_COLOR_DATE);
1352 if (tc)
1353 wattr_on(view->window,
1354 COLOR_PAIR(tc->colorpair), NULL);
1355 waddnstr(view->window, datebuf, limit);
1356 if (tc)
1357 wattr_off(view->window,
1358 COLOR_PAIR(tc->colorpair), NULL);
1359 col = limit;
1360 if (col > avail)
1361 goto done;
1363 if (avail >= 120) {
1364 char *id_str;
1365 err = got_object_id_str(&id_str, id);
1366 if (err)
1367 goto done;
1368 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1369 if (tc)
1370 wattr_on(view->window,
1371 COLOR_PAIR(tc->colorpair), NULL);
1372 wprintw(view->window, "%.8s ", id_str);
1373 if (tc)
1374 wattr_off(view->window,
1375 COLOR_PAIR(tc->colorpair), NULL);
1376 free(id_str);
1377 col += 9;
1378 if (col > avail)
1379 goto done;
1382 author = strdup(got_object_commit_get_author(commit));
1383 if (author == NULL) {
1384 err = got_error_from_errno("strdup");
1385 goto done;
1387 err = format_author(&wauthor, &author_width, author, avail - col, col);
1388 if (err)
1389 goto done;
1390 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1391 if (tc)
1392 wattr_on(view->window,
1393 COLOR_PAIR(tc->colorpair), NULL);
1394 waddwstr(view->window, wauthor);
1395 if (tc)
1396 wattr_off(view->window,
1397 COLOR_PAIR(tc->colorpair), NULL);
1398 col += author_width;
1399 while (col < avail && author_width < author_display_cols + 2) {
1400 waddch(view->window, ' ');
1401 col++;
1402 author_width++;
1404 if (col > avail)
1405 goto done;
1407 err = got_object_commit_get_logmsg(&logmsg0, commit);
1408 if (err)
1409 goto done;
1410 logmsg = logmsg0;
1411 while (*logmsg == '\n')
1412 logmsg++;
1413 newline = strchr(logmsg, '\n');
1414 if (newline)
1415 *newline = '\0';
1416 limit = avail - col;
1417 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1418 if (err)
1419 goto done;
1420 waddwstr(view->window, wlogmsg);
1421 col += logmsg_width;
1422 while (col < avail) {
1423 waddch(view->window, ' ');
1424 col++;
1426 done:
1427 free(logmsg0);
1428 free(wlogmsg);
1429 free(author);
1430 free(wauthor);
1431 free(line);
1432 return err;
1435 static struct commit_queue_entry *
1436 alloc_commit_queue_entry(struct got_commit_object *commit,
1437 struct got_object_id *id)
1439 struct commit_queue_entry *entry;
1441 entry = calloc(1, sizeof(*entry));
1442 if (entry == NULL)
1443 return NULL;
1445 entry->id = id;
1446 entry->commit = commit;
1447 return entry;
1450 static void
1451 pop_commit(struct commit_queue *commits)
1453 struct commit_queue_entry *entry;
1455 entry = TAILQ_FIRST(&commits->head);
1456 TAILQ_REMOVE(&commits->head, entry, entry);
1457 got_object_commit_close(entry->commit);
1458 commits->ncommits--;
1459 /* Don't free entry->id! It is owned by the commit graph. */
1460 free(entry);
1463 static void
1464 free_commits(struct commit_queue *commits)
1466 while (!TAILQ_EMPTY(&commits->head))
1467 pop_commit(commits);
1470 static const struct got_error *
1471 match_commit(int *have_match, struct got_object_id *id,
1472 struct got_commit_object *commit, regex_t *regex)
1474 const struct got_error *err = NULL;
1475 regmatch_t regmatch;
1476 char *id_str = NULL, *logmsg = NULL;
1478 *have_match = 0;
1480 err = got_object_id_str(&id_str, id);
1481 if (err)
1482 return err;
1484 err = got_object_commit_get_logmsg(&logmsg, commit);
1485 if (err)
1486 goto done;
1488 if (regexec(regex, got_object_commit_get_author(commit), 1,
1489 &regmatch, 0) == 0 ||
1490 regexec(regex, got_object_commit_get_committer(commit), 1,
1491 &regmatch, 0) == 0 ||
1492 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1493 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1494 *have_match = 1;
1495 done:
1496 free(id_str);
1497 free(logmsg);
1498 return err;
1501 static const struct got_error *
1502 queue_commits(struct tog_log_thread_args *a)
1504 const struct got_error *err = NULL;
1507 * We keep all commits open throughout the lifetime of the log
1508 * view in order to avoid having to re-fetch commits from disk
1509 * while updating the display.
1511 do {
1512 struct got_object_id *id;
1513 struct got_commit_object *commit;
1514 struct commit_queue_entry *entry;
1515 int errcode;
1517 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1518 NULL, NULL);
1519 if (err || id == NULL)
1520 break;
1522 err = got_object_open_as_commit(&commit, a->repo, id);
1523 if (err)
1524 break;
1525 entry = alloc_commit_queue_entry(commit, id);
1526 if (entry == NULL) {
1527 err = got_error_from_errno("alloc_commit_queue_entry");
1528 break;
1531 errcode = pthread_mutex_lock(&tog_mutex);
1532 if (errcode) {
1533 err = got_error_set_errno(errcode,
1534 "pthread_mutex_lock");
1535 break;
1538 entry->idx = a->commits->ncommits;
1539 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1540 a->commits->ncommits++;
1542 if (*a->searching == TOG_SEARCH_FORWARD &&
1543 !*a->search_next_done) {
1544 int have_match;
1545 err = match_commit(&have_match, id, commit, a->regex);
1546 if (err)
1547 break;
1548 if (have_match)
1549 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1552 errcode = pthread_mutex_unlock(&tog_mutex);
1553 if (errcode && err == NULL)
1554 err = got_error_set_errno(errcode,
1555 "pthread_mutex_unlock");
1556 if (err)
1557 break;
1558 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1560 return err;
1563 static void
1564 select_commit(struct tog_log_view_state *s)
1566 struct commit_queue_entry *entry;
1567 int ncommits = 0;
1569 entry = s->first_displayed_entry;
1570 while (entry) {
1571 if (ncommits == s->selected) {
1572 s->selected_entry = entry;
1573 break;
1575 entry = TAILQ_NEXT(entry, entry);
1576 ncommits++;
1580 static const struct got_error *
1581 draw_commits(struct tog_view *view)
1583 const struct got_error *err = NULL;
1584 struct tog_log_view_state *s = &view->state.log;
1585 struct commit_queue_entry *entry = s->selected_entry;
1586 const int limit = view->nlines;
1587 int width;
1588 int ncommits, author_cols = 4;
1589 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1590 char *refs_str = NULL;
1591 wchar_t *wline;
1592 struct tog_color *tc;
1593 static const size_t date_display_cols = 12;
1595 if (s->selected_entry &&
1596 !(view->searching && view->search_next_done == 0)) {
1597 struct got_reflist_head *refs;
1598 err = got_object_id_str(&id_str, s->selected_entry->id);
1599 if (err)
1600 return err;
1601 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1602 s->selected_entry->id);
1603 if (refs) {
1604 err = build_refs_str(&refs_str, refs,
1605 s->selected_entry->id, s->repo);
1606 if (err)
1607 goto done;
1611 if (s->thread_args.commits_needed == 0)
1612 halfdelay(10); /* disable fast refresh */
1614 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1615 if (asprintf(&ncommits_str, " [%d/%d] %s",
1616 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1617 (view->searching && !view->search_next_done) ?
1618 "searching..." : "loading...") == -1) {
1619 err = got_error_from_errno("asprintf");
1620 goto done;
1622 } else {
1623 const char *search_str = NULL;
1625 if (view->searching) {
1626 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1627 search_str = "no more matches";
1628 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1629 search_str = "no matches found";
1630 else if (!view->search_next_done)
1631 search_str = "searching...";
1634 if (asprintf(&ncommits_str, " [%d/%d] %s",
1635 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1636 search_str ? search_str :
1637 (refs_str ? refs_str : "")) == -1) {
1638 err = got_error_from_errno("asprintf");
1639 goto done;
1643 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1644 if (asprintf(&header, "commit %s %s%s",
1645 id_str ? id_str : "........................................",
1646 s->in_repo_path, ncommits_str) == -1) {
1647 err = got_error_from_errno("asprintf");
1648 header = NULL;
1649 goto done;
1651 } else if (asprintf(&header, "commit %s%s",
1652 id_str ? id_str : "........................................",
1653 ncommits_str) == -1) {
1654 err = got_error_from_errno("asprintf");
1655 header = NULL;
1656 goto done;
1658 err = format_line(&wline, &width, header, view->ncols, 0);
1659 if (err)
1660 goto done;
1662 werase(view->window);
1664 if (view_needs_focus_indication(view))
1665 wstandout(view->window);
1666 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1667 if (tc)
1668 wattr_on(view->window,
1669 COLOR_PAIR(tc->colorpair), NULL);
1670 waddwstr(view->window, wline);
1671 if (tc)
1672 wattr_off(view->window,
1673 COLOR_PAIR(tc->colorpair), NULL);
1674 while (width < view->ncols) {
1675 waddch(view->window, ' ');
1676 width++;
1678 if (view_needs_focus_indication(view))
1679 wstandend(view->window);
1680 free(wline);
1681 if (limit <= 1)
1682 goto done;
1684 /* Grow author column size if necessary. */
1685 entry = s->first_displayed_entry;
1686 ncommits = 0;
1687 while (entry) {
1688 char *author;
1689 wchar_t *wauthor;
1690 int width;
1691 if (ncommits >= limit - 1)
1692 break;
1693 author = strdup(got_object_commit_get_author(entry->commit));
1694 if (author == NULL) {
1695 err = got_error_from_errno("strdup");
1696 goto done;
1698 err = format_author(&wauthor, &width, author, COLS,
1699 date_display_cols);
1700 if (author_cols < width)
1701 author_cols = width;
1702 free(wauthor);
1703 free(author);
1704 ncommits++;
1705 entry = TAILQ_NEXT(entry, entry);
1708 entry = s->first_displayed_entry;
1709 s->last_displayed_entry = s->first_displayed_entry;
1710 ncommits = 0;
1711 while (entry) {
1712 if (ncommits >= limit - 1)
1713 break;
1714 if (ncommits == s->selected)
1715 wstandout(view->window);
1716 err = draw_commit(view, entry->commit, entry->id,
1717 date_display_cols, author_cols);
1718 if (ncommits == s->selected)
1719 wstandend(view->window);
1720 if (err)
1721 goto done;
1722 ncommits++;
1723 s->last_displayed_entry = entry;
1724 entry = TAILQ_NEXT(entry, entry);
1727 view_vborder(view);
1728 done:
1729 free(id_str);
1730 free(refs_str);
1731 free(ncommits_str);
1732 free(header);
1733 return err;
1736 static void
1737 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1739 struct commit_queue_entry *entry;
1740 int nscrolled = 0;
1742 entry = TAILQ_FIRST(&s->commits.head);
1743 if (s->first_displayed_entry == entry)
1744 return;
1746 entry = s->first_displayed_entry;
1747 while (entry && nscrolled < maxscroll) {
1748 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1749 if (entry) {
1750 s->first_displayed_entry = entry;
1751 nscrolled++;
1756 static const struct got_error *
1757 trigger_log_thread(struct tog_view *view, int wait)
1759 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1760 int errcode;
1762 halfdelay(1); /* fast refresh while loading commits */
1764 while (ta->commits_needed > 0 || ta->load_all) {
1765 if (ta->log_complete)
1766 break;
1768 /* Wake the log thread. */
1769 errcode = pthread_cond_signal(&ta->need_commits);
1770 if (errcode)
1771 return got_error_set_errno(errcode,
1772 "pthread_cond_signal");
1775 * The mutex will be released while the view loop waits
1776 * in wgetch(), at which time the log thread will run.
1778 if (!wait)
1779 break;
1781 /* Display progress update in log view. */
1782 show_log_view(view);
1783 update_panels();
1784 doupdate();
1786 /* Wait right here while next commit is being loaded. */
1787 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1788 if (errcode)
1789 return got_error_set_errno(errcode,
1790 "pthread_cond_wait");
1792 /* Display progress update in log view. */
1793 show_log_view(view);
1794 update_panels();
1795 doupdate();
1798 return NULL;
1801 static const struct got_error *
1802 log_scroll_down(struct tog_view *view, int maxscroll)
1804 struct tog_log_view_state *s = &view->state.log;
1805 const struct got_error *err = NULL;
1806 struct commit_queue_entry *pentry;
1807 int nscrolled = 0, ncommits_needed;
1809 if (s->last_displayed_entry == NULL)
1810 return NULL;
1812 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1813 if (s->commits.ncommits < ncommits_needed &&
1814 !s->thread_args.log_complete) {
1816 * Ask the log thread for required amount of commits.
1818 s->thread_args.commits_needed += maxscroll;
1819 err = trigger_log_thread(view, 1);
1820 if (err)
1821 return err;
1824 do {
1825 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1826 if (pentry == NULL)
1827 break;
1829 s->last_displayed_entry = pentry;
1831 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1832 if (pentry == NULL)
1833 break;
1834 s->first_displayed_entry = pentry;
1835 } while (++nscrolled < maxscroll);
1837 return err;
1840 static const struct got_error *
1841 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1842 struct got_commit_object *commit, struct got_object_id *commit_id,
1843 struct tog_view *log_view, struct got_repository *repo)
1845 const struct got_error *err;
1846 struct got_object_qid *parent_id;
1847 struct tog_view *diff_view;
1849 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1850 if (diff_view == NULL)
1851 return got_error_from_errno("view_open");
1853 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1854 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1855 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1856 if (err == NULL)
1857 *new_view = diff_view;
1858 return err;
1861 static const struct got_error *
1862 tree_view_visit_subtree(struct tog_tree_view_state *s,
1863 struct got_tree_object *subtree)
1865 struct tog_parent_tree *parent;
1867 parent = calloc(1, sizeof(*parent));
1868 if (parent == NULL)
1869 return got_error_from_errno("calloc");
1871 parent->tree = s->tree;
1872 parent->first_displayed_entry = s->first_displayed_entry;
1873 parent->selected_entry = s->selected_entry;
1874 parent->selected = s->selected;
1875 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1876 s->tree = subtree;
1877 s->selected = 0;
1878 s->first_displayed_entry = NULL;
1879 return NULL;
1882 static const struct got_error *
1883 tree_view_walk_path(struct tog_tree_view_state *s,
1884 struct got_object_id *commit_id, const char *path)
1886 const struct got_error *err = NULL;
1887 struct got_tree_object *tree = NULL;
1888 const char *p;
1889 char *slash, *subpath = NULL;
1891 /* Walk the path and open corresponding tree objects. */
1892 p = path;
1893 while (*p) {
1894 struct got_tree_entry *te;
1895 struct got_object_id *tree_id;
1896 char *te_name;
1898 while (p[0] == '/')
1899 p++;
1901 /* Ensure the correct subtree entry is selected. */
1902 slash = strchr(p, '/');
1903 if (slash == NULL)
1904 te_name = strdup(p);
1905 else
1906 te_name = strndup(p, slash - p);
1907 if (te_name == NULL) {
1908 err = got_error_from_errno("strndup");
1909 break;
1911 te = got_object_tree_find_entry(s->tree, te_name);
1912 if (te == NULL) {
1913 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1914 free(te_name);
1915 break;
1917 free(te_name);
1918 s->first_displayed_entry = s->selected_entry = te;
1920 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1921 break; /* jump to this file's entry */
1923 slash = strchr(p, '/');
1924 if (slash)
1925 subpath = strndup(path, slash - path);
1926 else
1927 subpath = strdup(path);
1928 if (subpath == NULL) {
1929 err = got_error_from_errno("strdup");
1930 break;
1933 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1934 subpath);
1935 if (err)
1936 break;
1938 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1939 free(tree_id);
1940 if (err)
1941 break;
1943 err = tree_view_visit_subtree(s, tree);
1944 if (err) {
1945 got_object_tree_close(tree);
1946 break;
1948 if (slash == NULL)
1949 break;
1950 free(subpath);
1951 subpath = NULL;
1952 p = slash;
1955 free(subpath);
1956 return err;
1959 static const struct got_error *
1960 browse_commit_tree(struct tog_view **new_view, int begin_x,
1961 struct commit_queue_entry *entry, const char *path,
1962 const char *head_ref_name, struct got_repository *repo)
1964 const struct got_error *err = NULL;
1965 struct tog_tree_view_state *s;
1966 struct tog_view *tree_view;
1968 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1969 if (tree_view == NULL)
1970 return got_error_from_errno("view_open");
1972 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1973 if (err)
1974 return err;
1975 s = &tree_view->state.tree;
1977 *new_view = tree_view;
1979 if (got_path_is_root_dir(path))
1980 return NULL;
1982 return tree_view_walk_path(s, entry->id, path);
1985 static const struct got_error *
1986 block_signals_used_by_main_thread(void)
1988 sigset_t sigset;
1989 int errcode;
1991 if (sigemptyset(&sigset) == -1)
1992 return got_error_from_errno("sigemptyset");
1994 /* tog handles SIGWINCH and SIGCONT */
1995 if (sigaddset(&sigset, SIGWINCH) == -1)
1996 return got_error_from_errno("sigaddset");
1997 if (sigaddset(&sigset, SIGCONT) == -1)
1998 return got_error_from_errno("sigaddset");
2000 /* ncurses handles SIGTSTP */
2001 if (sigaddset(&sigset, SIGTSTP) == -1)
2002 return got_error_from_errno("sigaddset");
2004 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2005 if (errcode)
2006 return got_error_set_errno(errcode, "pthread_sigmask");
2008 return NULL;
2011 static void *
2012 log_thread(void *arg)
2014 const struct got_error *err = NULL;
2015 int errcode = 0;
2016 struct tog_log_thread_args *a = arg;
2017 int done = 0;
2019 err = block_signals_used_by_main_thread();
2020 if (err)
2021 return (void *)err;
2023 while (!done && !err && !tog_sigpipe_received) {
2024 err = queue_commits(a);
2025 if (err) {
2026 if (err->code != GOT_ERR_ITER_COMPLETED)
2027 return (void *)err;
2028 err = NULL;
2029 done = 1;
2030 } else if (a->commits_needed > 0 && !a->load_all)
2031 a->commits_needed--;
2033 errcode = pthread_mutex_lock(&tog_mutex);
2034 if (errcode) {
2035 err = got_error_set_errno(errcode,
2036 "pthread_mutex_lock");
2037 break;
2038 } else if (*a->quit)
2039 done = 1;
2040 else if (*a->first_displayed_entry == NULL) {
2041 *a->first_displayed_entry =
2042 TAILQ_FIRST(&a->commits->head);
2043 *a->selected_entry = *a->first_displayed_entry;
2046 errcode = pthread_cond_signal(&a->commit_loaded);
2047 if (errcode) {
2048 err = got_error_set_errno(errcode,
2049 "pthread_cond_signal");
2050 pthread_mutex_unlock(&tog_mutex);
2051 break;
2054 if (done)
2055 a->commits_needed = 0;
2056 else {
2057 if (a->commits_needed == 0 && !a->load_all) {
2058 errcode = pthread_cond_wait(&a->need_commits,
2059 &tog_mutex);
2060 if (errcode)
2061 err = got_error_set_errno(errcode,
2062 "pthread_cond_wait");
2063 if (*a->quit)
2064 done = 1;
2068 errcode = pthread_mutex_unlock(&tog_mutex);
2069 if (errcode && err == NULL)
2070 err = got_error_set_errno(errcode,
2071 "pthread_mutex_unlock");
2073 a->log_complete = 1;
2074 return (void *)err;
2077 static const struct got_error *
2078 stop_log_thread(struct tog_log_view_state *s)
2080 const struct got_error *err = NULL;
2081 int errcode;
2083 if (s->thread) {
2084 s->quit = 1;
2085 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2086 if (errcode)
2087 return got_error_set_errno(errcode,
2088 "pthread_cond_signal");
2089 errcode = pthread_mutex_unlock(&tog_mutex);
2090 if (errcode)
2091 return got_error_set_errno(errcode,
2092 "pthread_mutex_unlock");
2093 errcode = pthread_join(s->thread, (void **)&err);
2094 if (errcode)
2095 return got_error_set_errno(errcode, "pthread_join");
2096 errcode = pthread_mutex_lock(&tog_mutex);
2097 if (errcode)
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_lock");
2100 s->thread = NULL;
2103 if (s->thread_args.repo) {
2104 err = got_repo_close(s->thread_args.repo);
2105 s->thread_args.repo = NULL;
2108 if (s->thread_args.graph) {
2109 got_commit_graph_close(s->thread_args.graph);
2110 s->thread_args.graph = NULL;
2113 return err;
2116 static const struct got_error *
2117 close_log_view(struct tog_view *view)
2119 const struct got_error *err = NULL;
2120 struct tog_log_view_state *s = &view->state.log;
2121 int errcode;
2123 err = stop_log_thread(s);
2125 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2126 if (errcode && err == NULL)
2127 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2129 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2130 if (errcode && err == NULL)
2131 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2133 free_commits(&s->commits);
2134 free(s->in_repo_path);
2135 s->in_repo_path = NULL;
2136 free(s->start_id);
2137 s->start_id = NULL;
2138 free(s->head_ref_name);
2139 s->head_ref_name = NULL;
2140 return err;
2143 static const struct got_error *
2144 search_start_log_view(struct tog_view *view)
2146 struct tog_log_view_state *s = &view->state.log;
2148 s->matched_entry = NULL;
2149 s->search_entry = NULL;
2150 return NULL;
2153 static const struct got_error *
2154 search_next_log_view(struct tog_view *view)
2156 const struct got_error *err = NULL;
2157 struct tog_log_view_state *s = &view->state.log;
2158 struct commit_queue_entry *entry;
2160 /* Display progress update in log view. */
2161 show_log_view(view);
2162 update_panels();
2163 doupdate();
2165 if (s->search_entry) {
2166 int errcode, ch;
2167 errcode = pthread_mutex_unlock(&tog_mutex);
2168 if (errcode)
2169 return got_error_set_errno(errcode,
2170 "pthread_mutex_unlock");
2171 ch = wgetch(view->window);
2172 errcode = pthread_mutex_lock(&tog_mutex);
2173 if (errcode)
2174 return got_error_set_errno(errcode,
2175 "pthread_mutex_lock");
2176 if (ch == KEY_BACKSPACE) {
2177 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2178 return NULL;
2180 if (view->searching == TOG_SEARCH_FORWARD)
2181 entry = TAILQ_NEXT(s->search_entry, entry);
2182 else
2183 entry = TAILQ_PREV(s->search_entry,
2184 commit_queue_head, entry);
2185 } else if (s->matched_entry) {
2186 if (view->searching == TOG_SEARCH_FORWARD)
2187 entry = TAILQ_NEXT(s->matched_entry, entry);
2188 else
2189 entry = TAILQ_PREV(s->matched_entry,
2190 commit_queue_head, entry);
2191 } else {
2192 if (view->searching == TOG_SEARCH_FORWARD)
2193 entry = TAILQ_FIRST(&s->commits.head);
2194 else
2195 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2198 while (1) {
2199 int have_match = 0;
2201 if (entry == NULL) {
2202 if (s->thread_args.log_complete ||
2203 view->searching == TOG_SEARCH_BACKWARD) {
2204 view->search_next_done =
2205 (s->matched_entry == NULL ?
2206 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2207 s->search_entry = NULL;
2208 return NULL;
2211 * Poke the log thread for more commits and return,
2212 * allowing the main loop to make progress. Search
2213 * will resume at s->search_entry once we come back.
2215 s->thread_args.commits_needed++;
2216 return trigger_log_thread(view, 0);
2219 err = match_commit(&have_match, entry->id, entry->commit,
2220 &view->regex);
2221 if (err)
2222 break;
2223 if (have_match) {
2224 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2225 s->matched_entry = entry;
2226 break;
2229 s->search_entry = entry;
2230 if (view->searching == TOG_SEARCH_FORWARD)
2231 entry = TAILQ_NEXT(entry, entry);
2232 else
2233 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2236 if (s->matched_entry) {
2237 int cur = s->selected_entry->idx;
2238 while (cur < s->matched_entry->idx) {
2239 err = input_log_view(NULL, view, KEY_DOWN);
2240 if (err)
2241 return err;
2242 cur++;
2244 while (cur > s->matched_entry->idx) {
2245 err = input_log_view(NULL, view, KEY_UP);
2246 if (err)
2247 return err;
2248 cur--;
2252 s->search_entry = NULL;
2254 return NULL;
2257 static const struct got_error *
2258 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2259 struct got_repository *repo, const char *head_ref_name,
2260 const char *in_repo_path, int log_branches)
2262 const struct got_error *err = NULL;
2263 struct tog_log_view_state *s = &view->state.log;
2264 struct got_repository *thread_repo = NULL;
2265 struct got_commit_graph *thread_graph = NULL;
2266 int errcode;
2268 if (in_repo_path != s->in_repo_path) {
2269 free(s->in_repo_path);
2270 s->in_repo_path = strdup(in_repo_path);
2271 if (s->in_repo_path == NULL)
2272 return got_error_from_errno("strdup");
2275 /* The commit queue only contains commits being displayed. */
2276 TAILQ_INIT(&s->commits.head);
2277 s->commits.ncommits = 0;
2279 s->repo = repo;
2280 if (head_ref_name) {
2281 s->head_ref_name = strdup(head_ref_name);
2282 if (s->head_ref_name == NULL) {
2283 err = got_error_from_errno("strdup");
2284 goto done;
2287 s->start_id = got_object_id_dup(start_id);
2288 if (s->start_id == NULL) {
2289 err = got_error_from_errno("got_object_id_dup");
2290 goto done;
2292 s->log_branches = log_branches;
2294 STAILQ_INIT(&s->colors);
2295 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2296 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2297 get_color_value("TOG_COLOR_COMMIT"));
2298 if (err)
2299 goto done;
2300 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2301 get_color_value("TOG_COLOR_AUTHOR"));
2302 if (err) {
2303 free_colors(&s->colors);
2304 goto done;
2306 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2307 get_color_value("TOG_COLOR_DATE"));
2308 if (err) {
2309 free_colors(&s->colors);
2310 goto done;
2314 view->show = show_log_view;
2315 view->input = input_log_view;
2316 view->close = close_log_view;
2317 view->search_start = search_start_log_view;
2318 view->search_next = search_next_log_view;
2320 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2321 if (err)
2322 goto done;
2323 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2324 !s->log_branches);
2325 if (err)
2326 goto done;
2327 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2328 s->repo, NULL, NULL);
2329 if (err)
2330 goto done;
2332 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2333 if (errcode) {
2334 err = got_error_set_errno(errcode, "pthread_cond_init");
2335 goto done;
2337 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2338 if (errcode) {
2339 err = got_error_set_errno(errcode, "pthread_cond_init");
2340 goto done;
2343 s->thread_args.commits_needed = view->nlines;
2344 s->thread_args.graph = thread_graph;
2345 s->thread_args.commits = &s->commits;
2346 s->thread_args.in_repo_path = s->in_repo_path;
2347 s->thread_args.start_id = s->start_id;
2348 s->thread_args.repo = thread_repo;
2349 s->thread_args.log_complete = 0;
2350 s->thread_args.quit = &s->quit;
2351 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2352 s->thread_args.selected_entry = &s->selected_entry;
2353 s->thread_args.searching = &view->searching;
2354 s->thread_args.search_next_done = &view->search_next_done;
2355 s->thread_args.regex = &view->regex;
2356 done:
2357 if (err)
2358 close_log_view(view);
2359 return err;
2362 static const struct got_error *
2363 show_log_view(struct tog_view *view)
2365 const struct got_error *err;
2366 struct tog_log_view_state *s = &view->state.log;
2368 if (s->thread == NULL) {
2369 int errcode = pthread_create(&s->thread, NULL, log_thread,
2370 &s->thread_args);
2371 if (errcode)
2372 return got_error_set_errno(errcode, "pthread_create");
2373 if (s->thread_args.commits_needed > 0) {
2374 err = trigger_log_thread(view, 1);
2375 if (err)
2376 return err;
2380 return draw_commits(view);
2383 static const struct got_error *
2384 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2386 const struct got_error *err = NULL;
2387 struct tog_log_view_state *s = &view->state.log;
2388 struct tog_view *diff_view = NULL, *tree_view = NULL;
2389 struct tog_view *ref_view = NULL;
2390 int begin_x = 0;
2392 switch (ch) {
2393 case ERR: /* no user input from wgetch() */
2394 if (s->thread_args.load_all && s->thread_args.log_complete) {
2395 s->thread_args.load_all = 0;
2396 log_scroll_down(view, s->commits.ncommits);
2397 s->selected = MIN(view->nlines - 2,
2398 s->commits.ncommits - 1);
2399 select_commit(s);
2401 break;
2402 case 'q':
2403 s->quit = 1;
2404 break;
2405 case 'k':
2406 case KEY_UP:
2407 case '<':
2408 case ',':
2409 if (s->first_displayed_entry == NULL)
2410 break;
2411 if (s->selected > 0)
2412 s->selected--;
2413 else
2414 log_scroll_up(s, 1);
2415 select_commit(s);
2416 break;
2417 case 'g':
2418 case KEY_HOME:
2419 if (s->first_displayed_entry == NULL)
2420 break;
2422 s->selected = 0;
2423 log_scroll_up(s, s->commits.ncommits);
2424 select_commit(s);
2425 break;
2426 case KEY_PPAGE:
2427 case CTRL('b'):
2428 if (s->first_displayed_entry == NULL)
2429 break;
2430 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2431 s->selected = 0;
2432 else
2433 log_scroll_up(s, view->nlines - 1);
2434 select_commit(s);
2435 break;
2436 case 'j':
2437 case KEY_DOWN:
2438 case '>':
2439 case '.':
2440 if (s->first_displayed_entry == NULL)
2441 break;
2442 if (s->selected < MIN(view->nlines - 2,
2443 s->commits.ncommits - 1))
2444 s->selected++;
2445 else {
2446 err = log_scroll_down(view, 1);
2447 if (err)
2448 break;
2450 select_commit(s);
2451 break;
2452 case 'G':
2453 case KEY_END: {
2454 /* We don't know yet how many commits, so we're forced to
2455 * traverse them all. */
2456 if (!s->thread_args.log_complete) {
2457 s->thread_args.load_all = 1;
2458 return trigger_log_thread(view, 0);
2461 log_scroll_down(view, s->commits.ncommits);
2462 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2463 select_commit(s);
2464 break;
2466 case KEY_NPAGE:
2467 case CTRL('f'): {
2468 struct commit_queue_entry *first;
2469 first = s->first_displayed_entry;
2470 if (first == NULL)
2471 break;
2472 err = log_scroll_down(view, view->nlines - 1);
2473 if (err)
2474 break;
2475 if (first == s->first_displayed_entry &&
2476 s->selected < MIN(view->nlines - 2,
2477 s->commits.ncommits - 1)) {
2478 /* can't scroll further down */
2479 s->selected = MIN(view->nlines - 2,
2480 s->commits.ncommits - 1);
2482 select_commit(s);
2483 break;
2485 case KEY_RESIZE:
2486 if (s->selected > view->nlines - 2)
2487 s->selected = view->nlines - 2;
2488 if (s->selected > s->commits.ncommits - 1)
2489 s->selected = s->commits.ncommits - 1;
2490 select_commit(s);
2491 if (s->commits.ncommits < view->nlines - 1 &&
2492 !s->thread_args.log_complete) {
2493 s->thread_args.commits_needed += (view->nlines - 1) -
2494 s->commits.ncommits;
2495 err = trigger_log_thread(view, 1);
2497 break;
2498 case KEY_ENTER:
2499 case ' ':
2500 case '\r':
2501 if (s->selected_entry == NULL)
2502 break;
2503 if (view_is_parent_view(view))
2504 begin_x = view_split_begin_x(view->begin_x);
2505 err = open_diff_view_for_commit(&diff_view, begin_x,
2506 s->selected_entry->commit, s->selected_entry->id,
2507 view, s->repo);
2508 if (err)
2509 break;
2510 view->focussed = 0;
2511 diff_view->focussed = 1;
2512 if (view_is_parent_view(view)) {
2513 err = view_close_child(view);
2514 if (err)
2515 return err;
2516 view_set_child(view, diff_view);
2517 view->focus_child = 1;
2518 } else
2519 *new_view = diff_view;
2520 break;
2521 case 't':
2522 if (s->selected_entry == NULL)
2523 break;
2524 if (view_is_parent_view(view))
2525 begin_x = view_split_begin_x(view->begin_x);
2526 err = browse_commit_tree(&tree_view, begin_x,
2527 s->selected_entry, s->in_repo_path, s->head_ref_name,
2528 s->repo);
2529 if (err)
2530 break;
2531 view->focussed = 0;
2532 tree_view->focussed = 1;
2533 if (view_is_parent_view(view)) {
2534 err = view_close_child(view);
2535 if (err)
2536 return err;
2537 view_set_child(view, tree_view);
2538 view->focus_child = 1;
2539 } else
2540 *new_view = tree_view;
2541 break;
2542 case KEY_BACKSPACE:
2543 case CTRL('l'):
2544 case 'B':
2545 if (s->thread_args.load_all) {
2546 if (ch == KEY_BACKSPACE)
2547 s->thread_args.load_all = 0;
2548 break;
2550 if (ch == KEY_BACKSPACE &&
2551 got_path_is_root_dir(s->in_repo_path))
2552 break;
2553 err = stop_log_thread(s);
2554 if (err)
2555 return err;
2556 if (ch == KEY_BACKSPACE) {
2557 char *parent_path;
2558 err = got_path_dirname(&parent_path, s->in_repo_path);
2559 if (err)
2560 return err;
2561 free(s->in_repo_path);
2562 s->in_repo_path = parent_path;
2563 s->thread_args.in_repo_path = s->in_repo_path;
2564 } else if (ch == CTRL('l')) {
2565 struct got_object_id *start_id;
2566 err = got_repo_match_object_id(&start_id, NULL,
2567 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2568 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2569 if (err)
2570 return err;
2571 free(s->start_id);
2572 s->start_id = start_id;
2573 s->thread_args.start_id = s->start_id;
2574 } else /* 'B' */
2575 s->log_branches = !s->log_branches;
2577 err = got_repo_open(&s->thread_args.repo,
2578 got_repo_get_path(s->repo), NULL);
2579 if (err)
2580 return err;
2581 tog_free_refs();
2582 err = tog_load_refs(s->repo);
2583 if (err)
2584 return err;
2585 err = got_commit_graph_open(&s->thread_args.graph,
2586 s->in_repo_path, !s->log_branches);
2587 if (err)
2588 return err;
2589 err = got_commit_graph_iter_start(s->thread_args.graph,
2590 s->start_id, s->repo, NULL, NULL);
2591 if (err)
2592 return err;
2593 free_commits(&s->commits);
2594 s->first_displayed_entry = NULL;
2595 s->last_displayed_entry = NULL;
2596 s->selected_entry = NULL;
2597 s->selected = 0;
2598 s->thread_args.log_complete = 0;
2599 s->quit = 0;
2600 s->thread_args.commits_needed = view->nlines;
2601 break;
2602 case 'r':
2603 if (view_is_parent_view(view))
2604 begin_x = view_split_begin_x(view->begin_x);
2605 ref_view = view_open(view->nlines, view->ncols,
2606 view->begin_y, begin_x, TOG_VIEW_REF);
2607 if (ref_view == NULL)
2608 return got_error_from_errno("view_open");
2609 err = open_ref_view(ref_view, s->repo);
2610 if (err) {
2611 view_close(ref_view);
2612 return err;
2614 view->focussed = 0;
2615 ref_view->focussed = 1;
2616 if (view_is_parent_view(view)) {
2617 err = view_close_child(view);
2618 if (err)
2619 return err;
2620 view_set_child(view, ref_view);
2621 view->focus_child = 1;
2622 } else
2623 *new_view = ref_view;
2624 break;
2625 default:
2626 break;
2629 return err;
2632 static const struct got_error *
2633 apply_unveil(const char *repo_path, const char *worktree_path)
2635 const struct got_error *error;
2637 #ifdef PROFILE
2638 if (unveil("gmon.out", "rwc") != 0)
2639 return got_error_from_errno2("unveil", "gmon.out");
2640 #endif
2641 if (repo_path && unveil(repo_path, "r") != 0)
2642 return got_error_from_errno2("unveil", repo_path);
2644 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2645 return got_error_from_errno2("unveil", worktree_path);
2647 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2648 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2650 error = got_privsep_unveil_exec_helpers();
2651 if (error != NULL)
2652 return error;
2654 if (unveil(NULL, NULL) != 0)
2655 return got_error_from_errno("unveil");
2657 return NULL;
2660 static void
2661 init_curses(void)
2663 initscr();
2664 cbreak();
2665 halfdelay(1); /* Do fast refresh while initial view is loading. */
2666 noecho();
2667 nonl();
2668 intrflush(stdscr, FALSE);
2669 keypad(stdscr, TRUE);
2670 curs_set(0);
2671 if (getenv("TOG_COLORS") != NULL) {
2672 start_color();
2673 use_default_colors();
2675 signal(SIGWINCH, tog_sigwinch);
2676 signal(SIGPIPE, tog_sigpipe);
2677 signal(SIGCONT, tog_sigcont);
2680 static const struct got_error *
2681 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2682 struct got_repository *repo, struct got_worktree *worktree)
2684 const struct got_error *err = NULL;
2686 if (argc == 0) {
2687 *in_repo_path = strdup("/");
2688 if (*in_repo_path == NULL)
2689 return got_error_from_errno("strdup");
2690 return NULL;
2693 if (worktree) {
2694 const char *prefix = got_worktree_get_path_prefix(worktree);
2695 char *p;
2697 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2698 if (err)
2699 return err;
2700 if (asprintf(in_repo_path, "%s%s%s", prefix,
2701 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2702 p) == -1) {
2703 err = got_error_from_errno("asprintf");
2704 *in_repo_path = NULL;
2706 free(p);
2707 } else
2708 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2710 return err;
2713 static const struct got_error *
2714 cmd_log(int argc, char *argv[])
2716 const struct got_error *error;
2717 struct got_repository *repo = NULL;
2718 struct got_worktree *worktree = NULL;
2719 struct got_object_id *start_id = NULL;
2720 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2721 char *start_commit = NULL, *label = NULL;
2722 struct got_reference *ref = NULL;
2723 const char *head_ref_name = NULL;
2724 int ch, log_branches = 0;
2725 struct tog_view *view;
2727 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2728 switch (ch) {
2729 case 'b':
2730 log_branches = 1;
2731 break;
2732 case 'c':
2733 start_commit = optarg;
2734 break;
2735 case 'r':
2736 repo_path = realpath(optarg, NULL);
2737 if (repo_path == NULL)
2738 return got_error_from_errno2("realpath",
2739 optarg);
2740 break;
2741 default:
2742 usage_log();
2743 /* NOTREACHED */
2747 argc -= optind;
2748 argv += optind;
2750 if (argc > 1)
2751 usage_log();
2753 if (repo_path == NULL) {
2754 cwd = getcwd(NULL, 0);
2755 if (cwd == NULL)
2756 return got_error_from_errno("getcwd");
2757 error = got_worktree_open(&worktree, cwd);
2758 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2759 goto done;
2760 if (worktree)
2761 repo_path =
2762 strdup(got_worktree_get_repo_path(worktree));
2763 else
2764 repo_path = strdup(cwd);
2765 if (repo_path == NULL) {
2766 error = got_error_from_errno("strdup");
2767 goto done;
2771 error = got_repo_open(&repo, repo_path, NULL);
2772 if (error != NULL)
2773 goto done;
2775 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2776 repo, worktree);
2777 if (error)
2778 goto done;
2780 init_curses();
2782 error = apply_unveil(got_repo_get_path(repo),
2783 worktree ? got_worktree_get_root_path(worktree) : NULL);
2784 if (error)
2785 goto done;
2787 /* already loaded by tog_log_with_path()? */
2788 if (TAILQ_EMPTY(&tog_refs)) {
2789 error = tog_load_refs(repo);
2790 if (error)
2791 goto done;
2794 if (start_commit == NULL) {
2795 error = got_repo_match_object_id(&start_id, &label,
2796 worktree ? got_worktree_get_head_ref_name(worktree) :
2797 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2798 if (error)
2799 goto done;
2800 head_ref_name = label;
2801 } else {
2802 error = got_ref_open(&ref, repo, start_commit, 0);
2803 if (error == NULL)
2804 head_ref_name = got_ref_get_name(ref);
2805 else if (error->code != GOT_ERR_NOT_REF)
2806 goto done;
2807 error = got_repo_match_object_id(&start_id, NULL,
2808 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2809 if (error)
2810 goto done;
2813 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2814 if (view == NULL) {
2815 error = got_error_from_errno("view_open");
2816 goto done;
2818 error = open_log_view(view, start_id, repo, head_ref_name,
2819 in_repo_path, log_branches);
2820 if (error)
2821 goto done;
2822 if (worktree) {
2823 /* Release work tree lock. */
2824 got_worktree_close(worktree);
2825 worktree = NULL;
2827 error = view_loop(view);
2828 done:
2829 free(in_repo_path);
2830 free(repo_path);
2831 free(cwd);
2832 free(start_id);
2833 free(label);
2834 if (ref)
2835 got_ref_close(ref);
2836 if (repo) {
2837 const struct got_error *close_err = got_repo_close(repo);
2838 if (error == NULL)
2839 error = close_err;
2841 if (worktree)
2842 got_worktree_close(worktree);
2843 tog_free_refs();
2844 return error;
2847 __dead static void
2848 usage_diff(void)
2850 endwin();
2851 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2852 "[-w] object1 object2\n", getprogname());
2853 exit(1);
2856 static int
2857 match_line(const char *line, regex_t *regex, size_t nmatch,
2858 regmatch_t *regmatch)
2860 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2863 struct tog_color *
2864 match_color(struct tog_colors *colors, const char *line)
2866 struct tog_color *tc = NULL;
2868 STAILQ_FOREACH(tc, colors, entry) {
2869 if (match_line(line, &tc->regex, 0, NULL))
2870 return tc;
2873 return NULL;
2876 static const struct got_error *
2877 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2878 WINDOW *window, regmatch_t *regmatch)
2880 const struct got_error *err = NULL;
2881 wchar_t *wline;
2882 int width;
2883 char *s;
2885 *wtotal = 0;
2887 s = strndup(line, regmatch->rm_so);
2888 if (s == NULL)
2889 return got_error_from_errno("strndup");
2891 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2892 if (err) {
2893 free(s);
2894 return err;
2896 waddwstr(window, wline);
2897 free(wline);
2898 free(s);
2899 wlimit -= width;
2900 *wtotal += width;
2902 if (wlimit > 0) {
2903 s = strndup(line + regmatch->rm_so,
2904 regmatch->rm_eo - regmatch->rm_so);
2905 if (s == NULL) {
2906 err = got_error_from_errno("strndup");
2907 free(s);
2908 return err;
2910 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2911 if (err) {
2912 free(s);
2913 return err;
2915 wattr_on(window, A_STANDOUT, NULL);
2916 waddwstr(window, wline);
2917 wattr_off(window, A_STANDOUT, NULL);
2918 free(wline);
2919 free(s);
2920 wlimit -= width;
2921 *wtotal += width;
2924 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2925 err = format_line(&wline, &width,
2926 line + regmatch->rm_eo, wlimit, col_tab_align);
2927 if (err)
2928 return err;
2929 waddwstr(window, wline);
2930 free(wline);
2931 *wtotal += width;
2934 return NULL;
2937 static const struct got_error *
2938 draw_file(struct tog_view *view, const char *header)
2940 struct tog_diff_view_state *s = &view->state.diff;
2941 regmatch_t *regmatch = &view->regmatch;
2942 const struct got_error *err;
2943 int nprinted = 0;
2944 char *line;
2945 size_t linesize = 0;
2946 ssize_t linelen;
2947 struct tog_color *tc;
2948 wchar_t *wline;
2949 int width;
2950 int max_lines = view->nlines;
2951 int nlines = s->nlines;
2952 off_t line_offset;
2954 line_offset = s->line_offsets[s->first_displayed_line - 1];
2955 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2956 return got_error_from_errno("fseek");
2958 werase(view->window);
2960 if (header) {
2961 if (asprintf(&line, "[%d/%d] %s",
2962 s->first_displayed_line - 1 + s->selected_line, nlines,
2963 header) == -1)
2964 return got_error_from_errno("asprintf");
2965 err = format_line(&wline, &width, line, view->ncols, 0);
2966 free(line);
2967 if (err)
2968 return err;
2970 if (view_needs_focus_indication(view))
2971 wstandout(view->window);
2972 waddwstr(view->window, wline);
2973 free(wline);
2974 wline = NULL;
2975 if (view_needs_focus_indication(view))
2976 wstandend(view->window);
2977 if (width <= view->ncols - 1)
2978 waddch(view->window, '\n');
2980 if (max_lines <= 1)
2981 return NULL;
2982 max_lines--;
2985 s->eof = 0;
2986 line = NULL;
2987 while (max_lines > 0 && nprinted < max_lines) {
2988 linelen = getline(&line, &linesize, s->f);
2989 if (linelen == -1) {
2990 if (feof(s->f)) {
2991 s->eof = 1;
2992 break;
2994 free(line);
2995 return got_ferror(s->f, GOT_ERR_IO);
2998 tc = match_color(&s->colors, line);
2999 if (tc)
3000 wattr_on(view->window,
3001 COLOR_PAIR(tc->colorpair), NULL);
3002 if (s->first_displayed_line + nprinted == s->matched_line &&
3003 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3004 err = add_matched_line(&width, line, view->ncols, 0,
3005 view->window, regmatch);
3006 if (err) {
3007 free(line);
3008 return err;
3010 } else {
3011 err = format_line(&wline, &width, line, view->ncols, 0);
3012 if (err) {
3013 free(line);
3014 return err;
3016 waddwstr(view->window, wline);
3017 free(wline);
3018 wline = NULL;
3020 if (tc)
3021 wattr_off(view->window,
3022 COLOR_PAIR(tc->colorpair), NULL);
3023 if (width <= view->ncols - 1)
3024 waddch(view->window, '\n');
3025 nprinted++;
3027 free(line);
3028 if (nprinted >= 1)
3029 s->last_displayed_line = s->first_displayed_line +
3030 (nprinted - 1);
3031 else
3032 s->last_displayed_line = s->first_displayed_line;
3034 view_vborder(view);
3036 if (s->eof) {
3037 while (nprinted < view->nlines) {
3038 waddch(view->window, '\n');
3039 nprinted++;
3042 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3043 if (err) {
3044 return err;
3047 wstandout(view->window);
3048 waddwstr(view->window, wline);
3049 free(wline);
3050 wline = NULL;
3051 wstandend(view->window);
3054 return NULL;
3057 static char *
3058 get_datestr(time_t *time, char *datebuf)
3060 struct tm mytm, *tm;
3061 char *p, *s;
3063 tm = gmtime_r(time, &mytm);
3064 if (tm == NULL)
3065 return NULL;
3066 s = asctime_r(tm, datebuf);
3067 if (s == NULL)
3068 return NULL;
3069 p = strchr(s, '\n');
3070 if (p)
3071 *p = '\0';
3072 return s;
3075 static const struct got_error *
3076 get_changed_paths(struct got_pathlist_head *paths,
3077 struct got_commit_object *commit, struct got_repository *repo)
3079 const struct got_error *err = NULL;
3080 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3081 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3082 struct got_object_qid *qid;
3084 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3085 if (qid != NULL) {
3086 struct got_commit_object *pcommit;
3087 err = got_object_open_as_commit(&pcommit, repo,
3088 qid->id);
3089 if (err)
3090 return err;
3092 tree_id1 = got_object_id_dup(
3093 got_object_commit_get_tree_id(pcommit));
3094 if (tree_id1 == NULL) {
3095 got_object_commit_close(pcommit);
3096 return got_error_from_errno("got_object_id_dup");
3098 got_object_commit_close(pcommit);
3102 if (tree_id1) {
3103 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3104 if (err)
3105 goto done;
3108 tree_id2 = got_object_commit_get_tree_id(commit);
3109 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3110 if (err)
3111 goto done;
3113 err = got_diff_tree(tree1, tree2, "", "", repo,
3114 got_diff_tree_collect_changed_paths, paths, 0);
3115 done:
3116 if (tree1)
3117 got_object_tree_close(tree1);
3118 if (tree2)
3119 got_object_tree_close(tree2);
3120 free(tree_id1);
3121 return err;
3124 static const struct got_error *
3125 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3127 off_t *p;
3129 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3130 if (p == NULL)
3131 return got_error_from_errno("reallocarray");
3132 *line_offsets = p;
3133 (*line_offsets)[*nlines] = off;
3134 (*nlines)++;
3135 return NULL;
3138 static const struct got_error *
3139 write_commit_info(off_t **line_offsets, size_t *nlines,
3140 struct got_object_id *commit_id, struct got_reflist_head *refs,
3141 struct got_repository *repo, FILE *outfile)
3143 const struct got_error *err = NULL;
3144 char datebuf[26], *datestr;
3145 struct got_commit_object *commit;
3146 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3147 time_t committer_time;
3148 const char *author, *committer;
3149 char *refs_str = NULL;
3150 struct got_pathlist_head changed_paths;
3151 struct got_pathlist_entry *pe;
3152 off_t outoff = 0;
3153 int n;
3155 TAILQ_INIT(&changed_paths);
3157 if (refs) {
3158 err = build_refs_str(&refs_str, refs, commit_id, repo);
3159 if (err)
3160 return err;
3163 err = got_object_open_as_commit(&commit, repo, commit_id);
3164 if (err)
3165 return err;
3167 err = got_object_id_str(&id_str, commit_id);
3168 if (err) {
3169 err = got_error_from_errno("got_object_id_str");
3170 goto done;
3173 err = add_line_offset(line_offsets, nlines, 0);
3174 if (err)
3175 goto done;
3177 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3178 refs_str ? refs_str : "", refs_str ? ")" : "");
3179 if (n < 0) {
3180 err = got_error_from_errno("fprintf");
3181 goto done;
3183 outoff += n;
3184 err = add_line_offset(line_offsets, nlines, outoff);
3185 if (err)
3186 goto done;
3188 n = fprintf(outfile, "from: %s\n",
3189 got_object_commit_get_author(commit));
3190 if (n < 0) {
3191 err = got_error_from_errno("fprintf");
3192 goto done;
3194 outoff += n;
3195 err = add_line_offset(line_offsets, nlines, outoff);
3196 if (err)
3197 goto done;
3199 committer_time = got_object_commit_get_committer_time(commit);
3200 datestr = get_datestr(&committer_time, datebuf);
3201 if (datestr) {
3202 n = fprintf(outfile, "date: %s UTC\n", datestr);
3203 if (n < 0) {
3204 err = got_error_from_errno("fprintf");
3205 goto done;
3207 outoff += n;
3208 err = add_line_offset(line_offsets, nlines, outoff);
3209 if (err)
3210 goto done;
3212 author = got_object_commit_get_author(commit);
3213 committer = got_object_commit_get_committer(commit);
3214 if (strcmp(author, committer) != 0) {
3215 n = fprintf(outfile, "via: %s\n", committer);
3216 if (n < 0) {
3217 err = got_error_from_errno("fprintf");
3218 goto done;
3220 outoff += n;
3221 err = add_line_offset(line_offsets, nlines, outoff);
3222 if (err)
3223 goto done;
3225 err = got_object_commit_get_logmsg(&logmsg, commit);
3226 if (err)
3227 goto done;
3228 s = logmsg;
3229 while ((line = strsep(&s, "\n")) != NULL) {
3230 n = fprintf(outfile, "%s\n", line);
3231 if (n < 0) {
3232 err = got_error_from_errno("fprintf");
3233 goto done;
3235 outoff += n;
3236 err = add_line_offset(line_offsets, nlines, outoff);
3237 if (err)
3238 goto done;
3241 err = get_changed_paths(&changed_paths, commit, repo);
3242 if (err)
3243 goto done;
3244 TAILQ_FOREACH(pe, &changed_paths, entry) {
3245 struct got_diff_changed_path *cp = pe->data;
3246 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3247 if (n < 0) {
3248 err = got_error_from_errno("fprintf");
3249 goto done;
3251 outoff += n;
3252 err = add_line_offset(line_offsets, nlines, outoff);
3253 if (err)
3254 goto done;
3255 free((char *)pe->path);
3256 free(pe->data);
3259 fputc('\n', outfile);
3260 outoff++;
3261 err = add_line_offset(line_offsets, nlines, outoff);
3262 done:
3263 got_pathlist_free(&changed_paths);
3264 free(id_str);
3265 free(logmsg);
3266 free(refs_str);
3267 got_object_commit_close(commit);
3268 if (err) {
3269 free(*line_offsets);
3270 *line_offsets = NULL;
3271 *nlines = 0;
3273 return err;
3276 static const struct got_error *
3277 create_diff(struct tog_diff_view_state *s)
3279 const struct got_error *err = NULL;
3280 FILE *f = NULL;
3281 int obj_type;
3283 free(s->line_offsets);
3284 s->line_offsets = malloc(sizeof(off_t));
3285 if (s->line_offsets == NULL)
3286 return got_error_from_errno("malloc");
3287 s->nlines = 0;
3289 f = got_opentemp();
3290 if (f == NULL) {
3291 err = got_error_from_errno("got_opentemp");
3292 goto done;
3294 if (s->f && fclose(s->f) == EOF) {
3295 err = got_error_from_errno("fclose");
3296 goto done;
3298 s->f = f;
3300 if (s->id1)
3301 err = got_object_get_type(&obj_type, s->repo, s->id1);
3302 else
3303 err = got_object_get_type(&obj_type, s->repo, s->id2);
3304 if (err)
3305 goto done;
3307 switch (obj_type) {
3308 case GOT_OBJ_TYPE_BLOB:
3309 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3310 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3311 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3312 break;
3313 case GOT_OBJ_TYPE_TREE:
3314 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3315 s->id1, s->id2, "", "", s->diff_context,
3316 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3317 break;
3318 case GOT_OBJ_TYPE_COMMIT: {
3319 const struct got_object_id_queue *parent_ids;
3320 struct got_object_qid *pid;
3321 struct got_commit_object *commit2;
3322 struct got_reflist_head *refs;
3324 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3325 if (err)
3326 goto done;
3327 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3328 /* Show commit info if we're diffing to a parent/root commit. */
3329 if (s->id1 == NULL) {
3330 err = write_commit_info(&s->line_offsets, &s->nlines,
3331 s->id2, refs, s->repo, s->f);
3332 if (err)
3333 goto done;
3334 } else {
3335 parent_ids = got_object_commit_get_parent_ids(commit2);
3336 STAILQ_FOREACH(pid, parent_ids, entry) {
3337 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3338 err = write_commit_info(
3339 &s->line_offsets, &s->nlines,
3340 s->id2, refs, s->repo, s->f);
3341 if (err)
3342 goto done;
3343 break;
3347 got_object_commit_close(commit2);
3349 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3350 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3351 s->force_text_diff, s->repo, s->f);
3352 break;
3354 default:
3355 err = got_error(GOT_ERR_OBJ_TYPE);
3356 break;
3358 if (err)
3359 goto done;
3360 done:
3361 if (s->f && fflush(s->f) != 0 && err == NULL)
3362 err = got_error_from_errno("fflush");
3363 return err;
3366 static void
3367 diff_view_indicate_progress(struct tog_view *view)
3369 mvwaddstr(view->window, 0, 0, "diffing...");
3370 update_panels();
3371 doupdate();
3374 static const struct got_error *
3375 search_start_diff_view(struct tog_view *view)
3377 struct tog_diff_view_state *s = &view->state.diff;
3379 s->matched_line = 0;
3380 return NULL;
3383 static const struct got_error *
3384 search_next_diff_view(struct tog_view *view)
3386 struct tog_diff_view_state *s = &view->state.diff;
3387 int lineno;
3388 char *line = NULL;
3389 size_t linesize = 0;
3390 ssize_t linelen;
3392 if (!view->searching) {
3393 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3394 return NULL;
3397 if (s->matched_line) {
3398 if (view->searching == TOG_SEARCH_FORWARD)
3399 lineno = s->matched_line + 1;
3400 else
3401 lineno = s->matched_line - 1;
3402 } else {
3403 if (view->searching == TOG_SEARCH_FORWARD)
3404 lineno = 1;
3405 else
3406 lineno = s->nlines;
3409 while (1) {
3410 off_t offset;
3412 if (lineno <= 0 || lineno > s->nlines) {
3413 if (s->matched_line == 0) {
3414 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3415 break;
3418 if (view->searching == TOG_SEARCH_FORWARD)
3419 lineno = 1;
3420 else
3421 lineno = s->nlines;
3424 offset = s->line_offsets[lineno - 1];
3425 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3426 free(line);
3427 return got_error_from_errno("fseeko");
3429 linelen = getline(&line, &linesize, s->f);
3430 if (linelen != -1 &&
3431 match_line(line, &view->regex, 1, &view->regmatch)) {
3432 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3433 s->matched_line = lineno;
3434 break;
3436 if (view->searching == TOG_SEARCH_FORWARD)
3437 lineno++;
3438 else
3439 lineno--;
3441 free(line);
3443 if (s->matched_line) {
3444 s->first_displayed_line = s->matched_line;
3445 s->selected_line = 1;
3448 return NULL;
3451 static const struct got_error *
3452 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3453 struct got_object_id *id2, const char *label1, const char *label2,
3454 int diff_context, int ignore_whitespace, int force_text_diff,
3455 struct tog_view *log_view, struct got_repository *repo)
3457 const struct got_error *err;
3458 struct tog_diff_view_state *s = &view->state.diff;
3460 if (id1 != NULL && id2 != NULL) {
3461 int type1, type2;
3462 err = got_object_get_type(&type1, repo, id1);
3463 if (err)
3464 return err;
3465 err = got_object_get_type(&type2, repo, id2);
3466 if (err)
3467 return err;
3469 if (type1 != type2)
3470 return got_error(GOT_ERR_OBJ_TYPE);
3472 s->first_displayed_line = 1;
3473 s->last_displayed_line = view->nlines;
3474 s->selected_line = 1;
3475 s->repo = repo;
3476 s->id1 = id1;
3477 s->id2 = id2;
3478 s->label1 = label1;
3479 s->label2 = label2;
3481 if (id1) {
3482 s->id1 = got_object_id_dup(id1);
3483 if (s->id1 == NULL)
3484 return got_error_from_errno("got_object_id_dup");
3485 } else
3486 s->id1 = NULL;
3488 s->id2 = got_object_id_dup(id2);
3489 if (s->id2 == NULL) {
3490 free(s->id1);
3491 s->id1 = NULL;
3492 return got_error_from_errno("got_object_id_dup");
3494 s->f = NULL;
3495 s->first_displayed_line = 1;
3496 s->last_displayed_line = view->nlines;
3497 s->diff_context = diff_context;
3498 s->ignore_whitespace = ignore_whitespace;
3499 s->force_text_diff = force_text_diff;
3500 s->log_view = log_view;
3501 s->repo = repo;
3503 STAILQ_INIT(&s->colors);
3504 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3505 err = add_color(&s->colors,
3506 "^-", TOG_COLOR_DIFF_MINUS,
3507 get_color_value("TOG_COLOR_DIFF_MINUS"));
3508 if (err)
3509 return err;
3510 err = add_color(&s->colors, "^\\+",
3511 TOG_COLOR_DIFF_PLUS,
3512 get_color_value("TOG_COLOR_DIFF_PLUS"));
3513 if (err) {
3514 free_colors(&s->colors);
3515 return err;
3517 err = add_color(&s->colors,
3518 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3519 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3520 if (err) {
3521 free_colors(&s->colors);
3522 return err;
3525 err = add_color(&s->colors,
3526 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3527 TOG_COLOR_DIFF_META,
3528 get_color_value("TOG_COLOR_DIFF_META"));
3529 if (err) {
3530 free_colors(&s->colors);
3531 return err;
3534 err = add_color(&s->colors,
3535 "^(from|via): ", TOG_COLOR_AUTHOR,
3536 get_color_value("TOG_COLOR_AUTHOR"));
3537 if (err) {
3538 free_colors(&s->colors);
3539 return err;
3542 err = add_color(&s->colors,
3543 "^date: ", TOG_COLOR_DATE,
3544 get_color_value("TOG_COLOR_DATE"));
3545 if (err) {
3546 free_colors(&s->colors);
3547 return err;
3551 if (log_view && view_is_splitscreen(view))
3552 show_log_view(log_view); /* draw vborder */
3553 diff_view_indicate_progress(view);
3555 s->line_offsets = NULL;
3556 s->nlines = 0;
3557 err = create_diff(s);
3558 if (err) {
3559 free(s->id1);
3560 s->id1 = NULL;
3561 free(s->id2);
3562 s->id2 = NULL;
3563 free_colors(&s->colors);
3564 return err;
3567 view->show = show_diff_view;
3568 view->input = input_diff_view;
3569 view->close = close_diff_view;
3570 view->search_start = search_start_diff_view;
3571 view->search_next = search_next_diff_view;
3573 return NULL;
3576 static const struct got_error *
3577 close_diff_view(struct tog_view *view)
3579 const struct got_error *err = NULL;
3580 struct tog_diff_view_state *s = &view->state.diff;
3582 free(s->id1);
3583 s->id1 = NULL;
3584 free(s->id2);
3585 s->id2 = NULL;
3586 if (s->f && fclose(s->f) == EOF)
3587 err = got_error_from_errno("fclose");
3588 free_colors(&s->colors);
3589 free(s->line_offsets);
3590 s->line_offsets = NULL;
3591 s->nlines = 0;
3592 return err;
3595 static const struct got_error *
3596 show_diff_view(struct tog_view *view)
3598 const struct got_error *err;
3599 struct tog_diff_view_state *s = &view->state.diff;
3600 char *id_str1 = NULL, *id_str2, *header;
3601 const char *label1, *label2;
3603 if (s->id1) {
3604 err = got_object_id_str(&id_str1, s->id1);
3605 if (err)
3606 return err;
3607 label1 = s->label1 ? : id_str1;
3608 } else
3609 label1 = "/dev/null";
3611 err = got_object_id_str(&id_str2, s->id2);
3612 if (err)
3613 return err;
3614 label2 = s->label2 ? : id_str2;
3616 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3617 err = got_error_from_errno("asprintf");
3618 free(id_str1);
3619 free(id_str2);
3620 return err;
3622 free(id_str1);
3623 free(id_str2);
3625 err = draw_file(view, header);
3626 free(header);
3627 return err;
3630 static const struct got_error *
3631 set_selected_commit(struct tog_diff_view_state *s,
3632 struct commit_queue_entry *entry)
3634 const struct got_error *err;
3635 const struct got_object_id_queue *parent_ids;
3636 struct got_commit_object *selected_commit;
3637 struct got_object_qid *pid;
3639 free(s->id2);
3640 s->id2 = got_object_id_dup(entry->id);
3641 if (s->id2 == NULL)
3642 return got_error_from_errno("got_object_id_dup");
3644 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3645 if (err)
3646 return err;
3647 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3648 free(s->id1);
3649 pid = STAILQ_FIRST(parent_ids);
3650 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3651 got_object_commit_close(selected_commit);
3652 return NULL;
3655 static const struct got_error *
3656 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3658 const struct got_error *err = NULL;
3659 struct tog_diff_view_state *s = &view->state.diff;
3660 struct tog_log_view_state *ls;
3661 struct commit_queue_entry *old_selected_entry;
3662 char *line = NULL;
3663 size_t linesize = 0;
3664 ssize_t linelen;
3665 int i;
3667 switch (ch) {
3668 case 'a':
3669 case 'w':
3670 if (ch == 'a')
3671 s->force_text_diff = !s->force_text_diff;
3672 if (ch == 'w')
3673 s->ignore_whitespace = !s->ignore_whitespace;
3674 wclear(view->window);
3675 s->first_displayed_line = 1;
3676 s->last_displayed_line = view->nlines;
3677 diff_view_indicate_progress(view);
3678 err = create_diff(s);
3679 break;
3680 case 'g':
3681 case KEY_HOME:
3682 s->first_displayed_line = 1;
3683 break;
3684 case 'G':
3685 case KEY_END:
3686 if (s->eof)
3687 break;
3689 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3690 s->eof = 1;
3691 break;
3692 case 'k':
3693 case KEY_UP:
3694 if (s->first_displayed_line > 1)
3695 s->first_displayed_line--;
3696 break;
3697 case KEY_PPAGE:
3698 case CTRL('b'):
3699 if (s->first_displayed_line == 1)
3700 break;
3701 i = 0;
3702 while (i++ < view->nlines - 1 &&
3703 s->first_displayed_line > 1)
3704 s->first_displayed_line--;
3705 break;
3706 case 'j':
3707 case KEY_DOWN:
3708 if (!s->eof)
3709 s->first_displayed_line++;
3710 break;
3711 case KEY_NPAGE:
3712 case CTRL('f'):
3713 case ' ':
3714 if (s->eof)
3715 break;
3716 i = 0;
3717 while (!s->eof && i++ < view->nlines - 1) {
3718 linelen = getline(&line, &linesize, s->f);
3719 s->first_displayed_line++;
3720 if (linelen == -1) {
3721 if (feof(s->f)) {
3722 s->eof = 1;
3723 } else
3724 err = got_ferror(s->f, GOT_ERR_IO);
3725 break;
3728 free(line);
3729 break;
3730 case '[':
3731 if (s->diff_context > 0) {
3732 s->diff_context--;
3733 diff_view_indicate_progress(view);
3734 err = create_diff(s);
3735 if (s->first_displayed_line + view->nlines - 1 >
3736 s->nlines) {
3737 s->first_displayed_line = 1;
3738 s->last_displayed_line = view->nlines;
3741 break;
3742 case ']':
3743 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3744 s->diff_context++;
3745 diff_view_indicate_progress(view);
3746 err = create_diff(s);
3748 break;
3749 case '<':
3750 case ',':
3751 if (s->log_view == NULL)
3752 break;
3753 ls = &s->log_view->state.log;
3754 old_selected_entry = ls->selected_entry;
3756 err = input_log_view(NULL, s->log_view, KEY_UP);
3757 if (err)
3758 break;
3760 if (old_selected_entry == ls->selected_entry)
3761 break;
3763 err = set_selected_commit(s, ls->selected_entry);
3764 if (err)
3765 break;
3767 s->first_displayed_line = 1;
3768 s->last_displayed_line = view->nlines;
3770 diff_view_indicate_progress(view);
3771 err = create_diff(s);
3772 break;
3773 case '>':
3774 case '.':
3775 if (s->log_view == NULL)
3776 break;
3777 ls = &s->log_view->state.log;
3778 old_selected_entry = ls->selected_entry;
3780 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3781 if (err)
3782 break;
3784 if (old_selected_entry == ls->selected_entry)
3785 break;
3787 err = set_selected_commit(s, ls->selected_entry);
3788 if (err)
3789 break;
3791 s->first_displayed_line = 1;
3792 s->last_displayed_line = view->nlines;
3794 diff_view_indicate_progress(view);
3795 err = create_diff(s);
3796 break;
3797 default:
3798 break;
3801 return err;
3804 static const struct got_error *
3805 cmd_diff(int argc, char *argv[])
3807 const struct got_error *error = NULL;
3808 struct got_repository *repo = NULL;
3809 struct got_worktree *worktree = NULL;
3810 struct got_object_id *id1 = NULL, *id2 = NULL;
3811 char *repo_path = NULL, *cwd = NULL;
3812 char *id_str1 = NULL, *id_str2 = NULL;
3813 char *label1 = NULL, *label2 = NULL;
3814 int diff_context = 3, ignore_whitespace = 0;
3815 int ch, force_text_diff = 0;
3816 const char *errstr;
3817 struct tog_view *view;
3819 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3820 switch (ch) {
3821 case 'a':
3822 force_text_diff = 1;
3823 break;
3824 case 'C':
3825 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3826 &errstr);
3827 if (errstr != NULL)
3828 err(1, "-C option %s", errstr);
3829 break;
3830 case 'r':
3831 repo_path = realpath(optarg, NULL);
3832 if (repo_path == NULL)
3833 return got_error_from_errno2("realpath",
3834 optarg);
3835 got_path_strip_trailing_slashes(repo_path);
3836 break;
3837 case 'w':
3838 ignore_whitespace = 1;
3839 break;
3840 default:
3841 usage_diff();
3842 /* NOTREACHED */
3846 argc -= optind;
3847 argv += optind;
3849 if (argc == 0) {
3850 usage_diff(); /* TODO show local worktree changes */
3851 } else if (argc == 2) {
3852 id_str1 = argv[0];
3853 id_str2 = argv[1];
3854 } else
3855 usage_diff();
3857 if (repo_path == NULL) {
3858 cwd = getcwd(NULL, 0);
3859 if (cwd == NULL)
3860 return got_error_from_errno("getcwd");
3861 error = got_worktree_open(&worktree, cwd);
3862 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3863 goto done;
3864 if (worktree)
3865 repo_path =
3866 strdup(got_worktree_get_repo_path(worktree));
3867 else
3868 repo_path = strdup(cwd);
3869 if (repo_path == NULL) {
3870 error = got_error_from_errno("strdup");
3871 goto done;
3875 error = got_repo_open(&repo, repo_path, NULL);
3876 if (error)
3877 goto done;
3879 init_curses();
3881 error = apply_unveil(got_repo_get_path(repo), NULL);
3882 if (error)
3883 goto done;
3885 error = tog_load_refs(repo);
3886 if (error)
3887 goto done;
3889 error = got_repo_match_object_id(&id1, &label1, id_str1,
3890 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3891 if (error)
3892 goto done;
3894 error = got_repo_match_object_id(&id2, &label2, id_str2,
3895 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3896 if (error)
3897 goto done;
3899 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3900 if (view == NULL) {
3901 error = got_error_from_errno("view_open");
3902 goto done;
3904 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3905 ignore_whitespace, force_text_diff, NULL, repo);
3906 if (error)
3907 goto done;
3908 error = view_loop(view);
3909 done:
3910 free(label1);
3911 free(label2);
3912 free(repo_path);
3913 free(cwd);
3914 if (repo) {
3915 const struct got_error *close_err = got_repo_close(repo);
3916 if (error == NULL)
3917 error = close_err;
3919 if (worktree)
3920 got_worktree_close(worktree);
3921 tog_free_refs();
3922 return error;
3925 __dead static void
3926 usage_blame(void)
3928 endwin();
3929 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3930 getprogname());
3931 exit(1);
3934 struct tog_blame_line {
3935 int annotated;
3936 struct got_object_id *id;
3939 static const struct got_error *
3940 draw_blame(struct tog_view *view)
3942 struct tog_blame_view_state *s = &view->state.blame;
3943 struct tog_blame *blame = &s->blame;
3944 regmatch_t *regmatch = &view->regmatch;
3945 const struct got_error *err;
3946 int lineno = 0, nprinted = 0;
3947 char *line = NULL;
3948 size_t linesize = 0;
3949 ssize_t linelen;
3950 wchar_t *wline;
3951 int width;
3952 struct tog_blame_line *blame_line;
3953 struct got_object_id *prev_id = NULL;
3954 char *id_str;
3955 struct tog_color *tc;
3957 err = got_object_id_str(&id_str, s->blamed_commit->id);
3958 if (err)
3959 return err;
3961 rewind(blame->f);
3962 werase(view->window);
3964 if (asprintf(&line, "commit %s", id_str) == -1) {
3965 err = got_error_from_errno("asprintf");
3966 free(id_str);
3967 return err;
3970 err = format_line(&wline, &width, line, view->ncols, 0);
3971 free(line);
3972 line = NULL;
3973 if (err)
3974 return err;
3975 if (view_needs_focus_indication(view))
3976 wstandout(view->window);
3977 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3978 if (tc)
3979 wattr_on(view->window,
3980 COLOR_PAIR(tc->colorpair), NULL);
3981 waddwstr(view->window, wline);
3982 if (tc)
3983 wattr_off(view->window,
3984 COLOR_PAIR(tc->colorpair), NULL);
3985 if (view_needs_focus_indication(view))
3986 wstandend(view->window);
3987 free(wline);
3988 wline = NULL;
3989 if (width < view->ncols - 1)
3990 waddch(view->window, '\n');
3992 if (asprintf(&line, "[%d/%d] %s%s",
3993 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3994 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3995 free(id_str);
3996 return got_error_from_errno("asprintf");
3998 free(id_str);
3999 err = format_line(&wline, &width, line, view->ncols, 0);
4000 free(line);
4001 line = NULL;
4002 if (err)
4003 return err;
4004 waddwstr(view->window, wline);
4005 free(wline);
4006 wline = NULL;
4007 if (width < view->ncols - 1)
4008 waddch(view->window, '\n');
4010 s->eof = 0;
4011 while (nprinted < view->nlines - 2) {
4012 linelen = getline(&line, &linesize, blame->f);
4013 if (linelen == -1) {
4014 if (feof(blame->f)) {
4015 s->eof = 1;
4016 break;
4018 free(line);
4019 return got_ferror(blame->f, GOT_ERR_IO);
4021 if (++lineno < s->first_displayed_line)
4022 continue;
4024 if (view->focussed && nprinted == s->selected_line - 1)
4025 wstandout(view->window);
4027 if (blame->nlines > 0) {
4028 blame_line = &blame->lines[lineno - 1];
4029 if (blame_line->annotated && prev_id &&
4030 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4031 !(view->focussed &&
4032 nprinted == s->selected_line - 1)) {
4033 waddstr(view->window, " ");
4034 } else if (blame_line->annotated) {
4035 char *id_str;
4036 err = got_object_id_str(&id_str, blame_line->id);
4037 if (err) {
4038 free(line);
4039 return err;
4041 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4042 if (tc)
4043 wattr_on(view->window,
4044 COLOR_PAIR(tc->colorpair), NULL);
4045 wprintw(view->window, "%.8s", id_str);
4046 if (tc)
4047 wattr_off(view->window,
4048 COLOR_PAIR(tc->colorpair), NULL);
4049 free(id_str);
4050 prev_id = blame_line->id;
4051 } else {
4052 waddstr(view->window, "........");
4053 prev_id = NULL;
4055 } else {
4056 waddstr(view->window, "........");
4057 prev_id = NULL;
4060 if (view->focussed && nprinted == s->selected_line - 1)
4061 wstandend(view->window);
4062 waddstr(view->window, " ");
4064 if (view->ncols <= 9) {
4065 width = 9;
4066 wline = wcsdup(L"");
4067 if (wline == NULL) {
4068 err = got_error_from_errno("wcsdup");
4069 free(line);
4070 return err;
4072 } else if (s->first_displayed_line + nprinted ==
4073 s->matched_line &&
4074 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4075 err = add_matched_line(&width, line, view->ncols - 9, 9,
4076 view->window, regmatch);
4077 if (err) {
4078 free(line);
4079 return err;
4081 width += 9;
4082 } else {
4083 err = format_line(&wline, &width, line,
4084 view->ncols - 9, 9);
4085 waddwstr(view->window, wline);
4086 free(wline);
4087 wline = NULL;
4088 width += 9;
4091 if (width <= view->ncols - 1)
4092 waddch(view->window, '\n');
4093 if (++nprinted == 1)
4094 s->first_displayed_line = lineno;
4096 free(line);
4097 s->last_displayed_line = lineno;
4099 view_vborder(view);
4101 return NULL;
4104 static const struct got_error *
4105 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4107 const struct got_error *err = NULL;
4108 struct tog_blame_cb_args *a = arg;
4109 struct tog_blame_line *line;
4110 int errcode;
4112 if (nlines != a->nlines ||
4113 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4114 return got_error(GOT_ERR_RANGE);
4116 errcode = pthread_mutex_lock(&tog_mutex);
4117 if (errcode)
4118 return got_error_set_errno(errcode, "pthread_mutex_lock");
4120 if (*a->quit) { /* user has quit the blame view */
4121 err = got_error(GOT_ERR_ITER_COMPLETED);
4122 goto done;
4125 if (lineno == -1)
4126 goto done; /* no change in this commit */
4128 line = &a->lines[lineno - 1];
4129 if (line->annotated)
4130 goto done;
4132 line->id = got_object_id_dup(id);
4133 if (line->id == NULL) {
4134 err = got_error_from_errno("got_object_id_dup");
4135 goto done;
4137 line->annotated = 1;
4138 done:
4139 errcode = pthread_mutex_unlock(&tog_mutex);
4140 if (errcode)
4141 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4142 return err;
4145 static void *
4146 blame_thread(void *arg)
4148 const struct got_error *err, *close_err;
4149 struct tog_blame_thread_args *ta = arg;
4150 struct tog_blame_cb_args *a = ta->cb_args;
4151 int errcode;
4153 err = block_signals_used_by_main_thread();
4154 if (err)
4155 return (void *)err;
4157 err = got_blame(ta->path, a->commit_id, ta->repo,
4158 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4159 if (err && err->code == GOT_ERR_CANCELLED)
4160 err = NULL;
4162 errcode = pthread_mutex_lock(&tog_mutex);
4163 if (errcode)
4164 return (void *)got_error_set_errno(errcode,
4165 "pthread_mutex_lock");
4167 close_err = got_repo_close(ta->repo);
4168 if (err == NULL)
4169 err = close_err;
4170 ta->repo = NULL;
4171 *ta->complete = 1;
4173 errcode = pthread_mutex_unlock(&tog_mutex);
4174 if (errcode && err == NULL)
4175 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4177 return (void *)err;
4180 static struct got_object_id *
4181 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4182 int first_displayed_line, int selected_line)
4184 struct tog_blame_line *line;
4186 if (nlines <= 0)
4187 return NULL;
4189 line = &lines[first_displayed_line - 1 + selected_line - 1];
4190 if (!line->annotated)
4191 return NULL;
4193 return line->id;
4196 static const struct got_error *
4197 stop_blame(struct tog_blame *blame)
4199 const struct got_error *err = NULL;
4200 int i;
4202 if (blame->thread) {
4203 int errcode;
4204 errcode = pthread_mutex_unlock(&tog_mutex);
4205 if (errcode)
4206 return got_error_set_errno(errcode,
4207 "pthread_mutex_unlock");
4208 errcode = pthread_join(blame->thread, (void **)&err);
4209 if (errcode)
4210 return got_error_set_errno(errcode, "pthread_join");
4211 errcode = pthread_mutex_lock(&tog_mutex);
4212 if (errcode)
4213 return got_error_set_errno(errcode,
4214 "pthread_mutex_lock");
4215 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4216 err = NULL;
4217 blame->thread = NULL;
4219 if (blame->thread_args.repo) {
4220 const struct got_error *close_err;
4221 close_err = got_repo_close(blame->thread_args.repo);
4222 if (err == NULL)
4223 err = close_err;
4224 blame->thread_args.repo = NULL;
4226 if (blame->f) {
4227 if (fclose(blame->f) == EOF && err == NULL)
4228 err = got_error_from_errno("fclose");
4229 blame->f = NULL;
4231 if (blame->lines) {
4232 for (i = 0; i < blame->nlines; i++)
4233 free(blame->lines[i].id);
4234 free(blame->lines);
4235 blame->lines = NULL;
4237 free(blame->cb_args.commit_id);
4238 blame->cb_args.commit_id = NULL;
4240 return err;
4243 static const struct got_error *
4244 cancel_blame_view(void *arg)
4246 const struct got_error *err = NULL;
4247 int *done = arg;
4248 int errcode;
4250 errcode = pthread_mutex_lock(&tog_mutex);
4251 if (errcode)
4252 return got_error_set_errno(errcode,
4253 "pthread_mutex_unlock");
4255 if (*done)
4256 err = got_error(GOT_ERR_CANCELLED);
4258 errcode = pthread_mutex_unlock(&tog_mutex);
4259 if (errcode)
4260 return got_error_set_errno(errcode,
4261 "pthread_mutex_lock");
4263 return err;
4266 static const struct got_error *
4267 run_blame(struct tog_view *view)
4269 struct tog_blame_view_state *s = &view->state.blame;
4270 struct tog_blame *blame = &s->blame;
4271 const struct got_error *err = NULL;
4272 struct got_blob_object *blob = NULL;
4273 struct got_repository *thread_repo = NULL;
4274 struct got_object_id *obj_id = NULL;
4275 int obj_type;
4277 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4278 s->path);
4279 if (err)
4280 return err;
4282 err = got_object_get_type(&obj_type, s->repo, obj_id);
4283 if (err)
4284 goto done;
4286 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4287 err = got_error(GOT_ERR_OBJ_TYPE);
4288 goto done;
4291 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4292 if (err)
4293 goto done;
4294 blame->f = got_opentemp();
4295 if (blame->f == NULL) {
4296 err = got_error_from_errno("got_opentemp");
4297 goto done;
4299 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4300 &blame->line_offsets, blame->f, blob);
4301 if (err)
4302 goto done;
4303 if (blame->nlines == 0) {
4304 s->blame_complete = 1;
4305 goto done;
4308 /* Don't include \n at EOF in the blame line count. */
4309 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4310 blame->nlines--;
4312 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4313 if (blame->lines == NULL) {
4314 err = got_error_from_errno("calloc");
4315 goto done;
4318 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4319 if (err)
4320 goto done;
4322 blame->cb_args.view = view;
4323 blame->cb_args.lines = blame->lines;
4324 blame->cb_args.nlines = blame->nlines;
4325 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4326 if (blame->cb_args.commit_id == NULL) {
4327 err = got_error_from_errno("got_object_id_dup");
4328 goto done;
4330 blame->cb_args.quit = &s->done;
4332 blame->thread_args.path = s->path;
4333 blame->thread_args.repo = thread_repo;
4334 blame->thread_args.cb_args = &blame->cb_args;
4335 blame->thread_args.complete = &s->blame_complete;
4336 blame->thread_args.cancel_cb = cancel_blame_view;
4337 blame->thread_args.cancel_arg = &s->done;
4338 s->blame_complete = 0;
4340 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4341 s->first_displayed_line = 1;
4342 s->last_displayed_line = view->nlines;
4343 s->selected_line = 1;
4346 done:
4347 if (blob)
4348 got_object_blob_close(blob);
4349 free(obj_id);
4350 if (err)
4351 stop_blame(blame);
4352 return err;
4355 static const struct got_error *
4356 open_blame_view(struct tog_view *view, char *path,
4357 struct got_object_id *commit_id, struct got_repository *repo)
4359 const struct got_error *err = NULL;
4360 struct tog_blame_view_state *s = &view->state.blame;
4362 STAILQ_INIT(&s->blamed_commits);
4364 s->path = strdup(path);
4365 if (s->path == NULL)
4366 return got_error_from_errno("strdup");
4368 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4369 if (err) {
4370 free(s->path);
4371 return err;
4374 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4375 s->first_displayed_line = 1;
4376 s->last_displayed_line = view->nlines;
4377 s->selected_line = 1;
4378 s->blame_complete = 0;
4379 s->repo = repo;
4380 s->commit_id = commit_id;
4381 memset(&s->blame, 0, sizeof(s->blame));
4383 STAILQ_INIT(&s->colors);
4384 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4385 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4386 get_color_value("TOG_COLOR_COMMIT"));
4387 if (err)
4388 return err;
4391 view->show = show_blame_view;
4392 view->input = input_blame_view;
4393 view->close = close_blame_view;
4394 view->search_start = search_start_blame_view;
4395 view->search_next = search_next_blame_view;
4397 return run_blame(view);
4400 static const struct got_error *
4401 close_blame_view(struct tog_view *view)
4403 const struct got_error *err = NULL;
4404 struct tog_blame_view_state *s = &view->state.blame;
4406 if (s->blame.thread)
4407 err = stop_blame(&s->blame);
4409 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4410 struct got_object_qid *blamed_commit;
4411 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4412 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4413 got_object_qid_free(blamed_commit);
4416 free(s->path);
4417 free_colors(&s->colors);
4419 return err;
4422 static const struct got_error *
4423 search_start_blame_view(struct tog_view *view)
4425 struct tog_blame_view_state *s = &view->state.blame;
4427 s->matched_line = 0;
4428 return NULL;
4431 static const struct got_error *
4432 search_next_blame_view(struct tog_view *view)
4434 struct tog_blame_view_state *s = &view->state.blame;
4435 int lineno;
4436 char *line = NULL;
4437 size_t linesize = 0;
4438 ssize_t linelen;
4440 if (!view->searching) {
4441 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4442 return NULL;
4445 if (s->matched_line) {
4446 if (view->searching == TOG_SEARCH_FORWARD)
4447 lineno = s->matched_line + 1;
4448 else
4449 lineno = s->matched_line - 1;
4450 } else {
4451 if (view->searching == TOG_SEARCH_FORWARD)
4452 lineno = 1;
4453 else
4454 lineno = s->blame.nlines;
4457 while (1) {
4458 off_t offset;
4460 if (lineno <= 0 || lineno > s->blame.nlines) {
4461 if (s->matched_line == 0) {
4462 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4463 break;
4466 if (view->searching == TOG_SEARCH_FORWARD)
4467 lineno = 1;
4468 else
4469 lineno = s->blame.nlines;
4472 offset = s->blame.line_offsets[lineno - 1];
4473 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4474 free(line);
4475 return got_error_from_errno("fseeko");
4477 linelen = getline(&line, &linesize, s->blame.f);
4478 if (linelen != -1 &&
4479 match_line(line, &view->regex, 1, &view->regmatch)) {
4480 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4481 s->matched_line = lineno;
4482 break;
4484 if (view->searching == TOG_SEARCH_FORWARD)
4485 lineno++;
4486 else
4487 lineno--;
4489 free(line);
4491 if (s->matched_line) {
4492 s->first_displayed_line = s->matched_line;
4493 s->selected_line = 1;
4496 return NULL;
4499 static const struct got_error *
4500 show_blame_view(struct tog_view *view)
4502 const struct got_error *err = NULL;
4503 struct tog_blame_view_state *s = &view->state.blame;
4504 int errcode;
4506 if (s->blame.thread == NULL && !s->blame_complete) {
4507 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4508 &s->blame.thread_args);
4509 if (errcode)
4510 return got_error_set_errno(errcode, "pthread_create");
4512 halfdelay(1); /* fast refresh while annotating */
4515 if (s->blame_complete)
4516 halfdelay(10); /* disable fast refresh */
4518 err = draw_blame(view);
4520 view_vborder(view);
4521 return err;
4524 static const struct got_error *
4525 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4527 const struct got_error *err = NULL, *thread_err = NULL;
4528 struct tog_view *diff_view;
4529 struct tog_blame_view_state *s = &view->state.blame;
4530 int begin_x = 0;
4532 switch (ch) {
4533 case 'q':
4534 s->done = 1;
4535 break;
4536 case 'k':
4537 case KEY_UP:
4538 if (s->selected_line > 1)
4539 s->selected_line--;
4540 else if (s->selected_line == 1 &&
4541 s->first_displayed_line > 1)
4542 s->first_displayed_line--;
4543 break;
4544 case KEY_PPAGE:
4545 case CTRL('b'):
4546 if (s->first_displayed_line == 1) {
4547 s->selected_line = 1;
4548 break;
4550 if (s->first_displayed_line > view->nlines - 2)
4551 s->first_displayed_line -=
4552 (view->nlines - 2);
4553 else
4554 s->first_displayed_line = 1;
4555 break;
4556 case 'j':
4557 case KEY_DOWN:
4558 if (s->selected_line < view->nlines - 2 &&
4559 s->first_displayed_line +
4560 s->selected_line <= s->blame.nlines)
4561 s->selected_line++;
4562 else if (s->last_displayed_line <
4563 s->blame.nlines)
4564 s->first_displayed_line++;
4565 break;
4566 case 'b':
4567 case 'p': {
4568 struct got_object_id *id = NULL;
4569 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4570 s->first_displayed_line, s->selected_line);
4571 if (id == NULL)
4572 break;
4573 if (ch == 'p') {
4574 struct got_commit_object *commit;
4575 struct got_object_qid *pid;
4576 struct got_object_id *blob_id = NULL;
4577 int obj_type;
4578 err = got_object_open_as_commit(&commit,
4579 s->repo, id);
4580 if (err)
4581 break;
4582 pid = STAILQ_FIRST(
4583 got_object_commit_get_parent_ids(commit));
4584 if (pid == NULL) {
4585 got_object_commit_close(commit);
4586 break;
4588 /* Check if path history ends here. */
4589 err = got_object_id_by_path(&blob_id, s->repo,
4590 pid->id, s->path);
4591 if (err) {
4592 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4593 err = NULL;
4594 got_object_commit_close(commit);
4595 break;
4597 err = got_object_get_type(&obj_type, s->repo,
4598 blob_id);
4599 free(blob_id);
4600 /* Can't blame non-blob type objects. */
4601 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4602 got_object_commit_close(commit);
4603 break;
4605 err = got_object_qid_alloc(&s->blamed_commit,
4606 pid->id);
4607 got_object_commit_close(commit);
4608 } else {
4609 if (got_object_id_cmp(id,
4610 s->blamed_commit->id) == 0)
4611 break;
4612 err = got_object_qid_alloc(&s->blamed_commit,
4613 id);
4615 if (err)
4616 break;
4617 s->done = 1;
4618 thread_err = stop_blame(&s->blame);
4619 s->done = 0;
4620 if (thread_err)
4621 break;
4622 STAILQ_INSERT_HEAD(&s->blamed_commits,
4623 s->blamed_commit, entry);
4624 err = run_blame(view);
4625 if (err)
4626 break;
4627 break;
4629 case 'B': {
4630 struct got_object_qid *first;
4631 first = STAILQ_FIRST(&s->blamed_commits);
4632 if (!got_object_id_cmp(first->id, s->commit_id))
4633 break;
4634 s->done = 1;
4635 thread_err = stop_blame(&s->blame);
4636 s->done = 0;
4637 if (thread_err)
4638 break;
4639 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4640 got_object_qid_free(s->blamed_commit);
4641 s->blamed_commit =
4642 STAILQ_FIRST(&s->blamed_commits);
4643 err = run_blame(view);
4644 if (err)
4645 break;
4646 break;
4648 case KEY_ENTER:
4649 case '\r': {
4650 struct got_object_id *id = NULL;
4651 struct got_object_qid *pid;
4652 struct got_commit_object *commit = NULL;
4653 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4654 s->first_displayed_line, s->selected_line);
4655 if (id == NULL)
4656 break;
4657 err = got_object_open_as_commit(&commit, s->repo, id);
4658 if (err)
4659 break;
4660 pid = STAILQ_FIRST(
4661 got_object_commit_get_parent_ids(commit));
4662 if (view_is_parent_view(view))
4663 begin_x = view_split_begin_x(view->begin_x);
4664 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4665 if (diff_view == NULL) {
4666 got_object_commit_close(commit);
4667 err = got_error_from_errno("view_open");
4668 break;
4670 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4671 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4672 got_object_commit_close(commit);
4673 if (err) {
4674 view_close(diff_view);
4675 break;
4677 view->focussed = 0;
4678 diff_view->focussed = 1;
4679 if (view_is_parent_view(view)) {
4680 err = view_close_child(view);
4681 if (err)
4682 break;
4683 view_set_child(view, diff_view);
4684 view->focus_child = 1;
4685 } else
4686 *new_view = diff_view;
4687 if (err)
4688 break;
4689 break;
4691 case KEY_NPAGE:
4692 case CTRL('f'):
4693 case ' ':
4694 if (s->last_displayed_line >= s->blame.nlines &&
4695 s->selected_line >= MIN(s->blame.nlines,
4696 view->nlines - 2)) {
4697 break;
4699 if (s->last_displayed_line >= s->blame.nlines &&
4700 s->selected_line < view->nlines - 2) {
4701 s->selected_line = MIN(s->blame.nlines,
4702 view->nlines - 2);
4703 break;
4705 if (s->last_displayed_line + view->nlines - 2
4706 <= s->blame.nlines)
4707 s->first_displayed_line +=
4708 view->nlines - 2;
4709 else
4710 s->first_displayed_line =
4711 s->blame.nlines -
4712 (view->nlines - 3);
4713 break;
4714 case KEY_RESIZE:
4715 if (s->selected_line > view->nlines - 2) {
4716 s->selected_line = MIN(s->blame.nlines,
4717 view->nlines - 2);
4719 break;
4720 default:
4721 break;
4723 return thread_err ? thread_err : err;
4726 static const struct got_error *
4727 cmd_blame(int argc, char *argv[])
4729 const struct got_error *error;
4730 struct got_repository *repo = NULL;
4731 struct got_worktree *worktree = NULL;
4732 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4733 char *link_target = NULL;
4734 struct got_object_id *commit_id = NULL;
4735 char *commit_id_str = NULL;
4736 int ch;
4737 struct tog_view *view;
4739 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4740 switch (ch) {
4741 case 'c':
4742 commit_id_str = optarg;
4743 break;
4744 case 'r':
4745 repo_path = realpath(optarg, NULL);
4746 if (repo_path == NULL)
4747 return got_error_from_errno2("realpath",
4748 optarg);
4749 break;
4750 default:
4751 usage_blame();
4752 /* NOTREACHED */
4756 argc -= optind;
4757 argv += optind;
4759 if (argc != 1)
4760 usage_blame();
4762 if (repo_path == NULL) {
4763 cwd = getcwd(NULL, 0);
4764 if (cwd == NULL)
4765 return got_error_from_errno("getcwd");
4766 error = got_worktree_open(&worktree, cwd);
4767 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4768 goto done;
4769 if (worktree)
4770 repo_path =
4771 strdup(got_worktree_get_repo_path(worktree));
4772 else
4773 repo_path = strdup(cwd);
4774 if (repo_path == NULL) {
4775 error = got_error_from_errno("strdup");
4776 goto done;
4780 error = got_repo_open(&repo, repo_path, NULL);
4781 if (error != NULL)
4782 goto done;
4784 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4785 worktree);
4786 if (error)
4787 goto done;
4789 init_curses();
4791 error = apply_unveil(got_repo_get_path(repo), NULL);
4792 if (error)
4793 goto done;
4795 error = tog_load_refs(repo);
4796 if (error)
4797 goto done;
4799 if (commit_id_str == NULL) {
4800 struct got_reference *head_ref;
4801 error = got_ref_open(&head_ref, repo, worktree ?
4802 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4803 if (error != NULL)
4804 goto done;
4805 error = got_ref_resolve(&commit_id, repo, head_ref);
4806 got_ref_close(head_ref);
4807 } else {
4808 error = got_repo_match_object_id(&commit_id, NULL,
4809 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4811 if (error != NULL)
4812 goto done;
4814 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4815 if (view == NULL) {
4816 error = got_error_from_errno("view_open");
4817 goto done;
4820 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4821 commit_id, repo);
4822 if (error)
4823 goto done;
4825 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4826 commit_id, repo);
4827 if (error)
4828 goto done;
4829 if (worktree) {
4830 /* Release work tree lock. */
4831 got_worktree_close(worktree);
4832 worktree = NULL;
4834 error = view_loop(view);
4835 done:
4836 free(repo_path);
4837 free(in_repo_path);
4838 free(link_target);
4839 free(cwd);
4840 free(commit_id);
4841 if (worktree)
4842 got_worktree_close(worktree);
4843 if (repo) {
4844 const struct got_error *close_err = got_repo_close(repo);
4845 if (error == NULL)
4846 error = close_err;
4848 tog_free_refs();
4849 return error;
4852 static const struct got_error *
4853 draw_tree_entries(struct tog_view *view, const char *parent_path)
4855 struct tog_tree_view_state *s = &view->state.tree;
4856 const struct got_error *err = NULL;
4857 struct got_tree_entry *te;
4858 wchar_t *wline;
4859 struct tog_color *tc;
4860 int width, n, i, nentries;
4861 int limit = view->nlines;
4863 s->ndisplayed = 0;
4865 werase(view->window);
4867 if (limit == 0)
4868 return NULL;
4870 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4871 if (err)
4872 return err;
4873 if (view_needs_focus_indication(view))
4874 wstandout(view->window);
4875 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4876 if (tc)
4877 wattr_on(view->window,
4878 COLOR_PAIR(tc->colorpair), NULL);
4879 waddwstr(view->window, wline);
4880 if (tc)
4881 wattr_off(view->window,
4882 COLOR_PAIR(tc->colorpair), NULL);
4883 if (view_needs_focus_indication(view))
4884 wstandend(view->window);
4885 free(wline);
4886 wline = NULL;
4887 if (width < view->ncols - 1)
4888 waddch(view->window, '\n');
4889 if (--limit <= 0)
4890 return NULL;
4891 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4892 if (err)
4893 return err;
4894 waddwstr(view->window, wline);
4895 free(wline);
4896 wline = NULL;
4897 if (width < view->ncols - 1)
4898 waddch(view->window, '\n');
4899 if (--limit <= 0)
4900 return NULL;
4901 waddch(view->window, '\n');
4902 if (--limit <= 0)
4903 return NULL;
4905 if (s->first_displayed_entry == NULL) {
4906 te = got_object_tree_get_first_entry(s->tree);
4907 if (s->selected == 0) {
4908 if (view->focussed)
4909 wstandout(view->window);
4910 s->selected_entry = NULL;
4912 waddstr(view->window, " ..\n"); /* parent directory */
4913 if (s->selected == 0 && view->focussed)
4914 wstandend(view->window);
4915 s->ndisplayed++;
4916 if (--limit <= 0)
4917 return NULL;
4918 n = 1;
4919 } else {
4920 n = 0;
4921 te = s->first_displayed_entry;
4924 nentries = got_object_tree_get_nentries(s->tree);
4925 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4926 char *line = NULL, *id_str = NULL, *link_target = NULL;
4927 const char *modestr = "";
4928 mode_t mode;
4930 te = got_object_tree_get_entry(s->tree, i);
4931 mode = got_tree_entry_get_mode(te);
4933 if (s->show_ids) {
4934 err = got_object_id_str(&id_str,
4935 got_tree_entry_get_id(te));
4936 if (err)
4937 return got_error_from_errno(
4938 "got_object_id_str");
4940 if (got_object_tree_entry_is_submodule(te))
4941 modestr = "$";
4942 else if (S_ISLNK(mode)) {
4943 int i;
4945 err = got_tree_entry_get_symlink_target(&link_target,
4946 te, s->repo);
4947 if (err) {
4948 free(id_str);
4949 return err;
4951 for (i = 0; i < strlen(link_target); i++) {
4952 if (!isprint((unsigned char)link_target[i]))
4953 link_target[i] = '?';
4955 modestr = "@";
4957 else if (S_ISDIR(mode))
4958 modestr = "/";
4959 else if (mode & S_IXUSR)
4960 modestr = "*";
4961 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4962 got_tree_entry_get_name(te), modestr,
4963 link_target ? " -> ": "",
4964 link_target ? link_target : "") == -1) {
4965 free(id_str);
4966 free(link_target);
4967 return got_error_from_errno("asprintf");
4969 free(id_str);
4970 free(link_target);
4971 err = format_line(&wline, &width, line, view->ncols, 0);
4972 if (err) {
4973 free(line);
4974 break;
4976 if (n == s->selected) {
4977 if (view->focussed)
4978 wstandout(view->window);
4979 s->selected_entry = te;
4981 tc = match_color(&s->colors, line);
4982 if (tc)
4983 wattr_on(view->window,
4984 COLOR_PAIR(tc->colorpair), NULL);
4985 waddwstr(view->window, wline);
4986 if (tc)
4987 wattr_off(view->window,
4988 COLOR_PAIR(tc->colorpair), NULL);
4989 if (width < view->ncols - 1)
4990 waddch(view->window, '\n');
4991 if (n == s->selected && view->focussed)
4992 wstandend(view->window);
4993 free(line);
4994 free(wline);
4995 wline = NULL;
4996 n++;
4997 s->ndisplayed++;
4998 s->last_displayed_entry = te;
4999 if (--limit <= 0)
5000 break;
5003 return err;
5006 static void
5007 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5009 struct got_tree_entry *te;
5010 int isroot = s->tree == s->root;
5011 int i = 0;
5013 if (s->first_displayed_entry == NULL)
5014 return;
5016 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5017 while (i++ < maxscroll) {
5018 if (te == NULL) {
5019 if (!isroot)
5020 s->first_displayed_entry = NULL;
5021 break;
5023 s->first_displayed_entry = te;
5024 te = got_tree_entry_get_prev(s->tree, te);
5028 static void
5029 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5031 struct got_tree_entry *next, *last;
5032 int n = 0;
5034 if (s->first_displayed_entry)
5035 next = got_tree_entry_get_next(s->tree,
5036 s->first_displayed_entry);
5037 else
5038 next = got_object_tree_get_first_entry(s->tree);
5040 last = s->last_displayed_entry;
5041 while (next && last && n++ < maxscroll) {
5042 last = got_tree_entry_get_next(s->tree, last);
5043 if (last) {
5044 s->first_displayed_entry = next;
5045 next = got_tree_entry_get_next(s->tree, next);
5050 static const struct got_error *
5051 tree_entry_path(char **path, struct tog_parent_trees *parents,
5052 struct got_tree_entry *te)
5054 const struct got_error *err = NULL;
5055 struct tog_parent_tree *pt;
5056 size_t len = 2; /* for leading slash and NUL */
5058 TAILQ_FOREACH(pt, parents, entry)
5059 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5060 + 1 /* slash */;
5061 if (te)
5062 len += strlen(got_tree_entry_get_name(te));
5064 *path = calloc(1, len);
5065 if (path == NULL)
5066 return got_error_from_errno("calloc");
5068 (*path)[0] = '/';
5069 pt = TAILQ_LAST(parents, tog_parent_trees);
5070 while (pt) {
5071 const char *name = got_tree_entry_get_name(pt->selected_entry);
5072 if (strlcat(*path, name, len) >= len) {
5073 err = got_error(GOT_ERR_NO_SPACE);
5074 goto done;
5076 if (strlcat(*path, "/", len) >= len) {
5077 err = got_error(GOT_ERR_NO_SPACE);
5078 goto done;
5080 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5082 if (te) {
5083 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5084 err = got_error(GOT_ERR_NO_SPACE);
5085 goto done;
5088 done:
5089 if (err) {
5090 free(*path);
5091 *path = NULL;
5093 return err;
5096 static const struct got_error *
5097 blame_tree_entry(struct tog_view **new_view, int begin_x,
5098 struct got_tree_entry *te, struct tog_parent_trees *parents,
5099 struct got_object_id *commit_id, struct got_repository *repo)
5101 const struct got_error *err = NULL;
5102 char *path;
5103 struct tog_view *blame_view;
5105 *new_view = NULL;
5107 err = tree_entry_path(&path, parents, te);
5108 if (err)
5109 return err;
5111 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5112 if (blame_view == NULL) {
5113 err = got_error_from_errno("view_open");
5114 goto done;
5117 err = open_blame_view(blame_view, path, commit_id, repo);
5118 if (err) {
5119 if (err->code == GOT_ERR_CANCELLED)
5120 err = NULL;
5121 view_close(blame_view);
5122 } else
5123 *new_view = blame_view;
5124 done:
5125 free(path);
5126 return err;
5129 static const struct got_error *
5130 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5131 struct tog_tree_view_state *s)
5133 struct tog_view *log_view;
5134 const struct got_error *err = NULL;
5135 char *path;
5137 *new_view = NULL;
5139 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5140 if (log_view == NULL)
5141 return got_error_from_errno("view_open");
5143 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5144 if (err)
5145 return err;
5147 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5148 path, 0);
5149 if (err)
5150 view_close(log_view);
5151 else
5152 *new_view = log_view;
5153 free(path);
5154 return err;
5157 static const struct got_error *
5158 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5159 const char *head_ref_name, struct got_repository *repo)
5161 const struct got_error *err = NULL;
5162 char *commit_id_str = NULL;
5163 struct tog_tree_view_state *s = &view->state.tree;
5164 struct got_commit_object *commit = NULL;
5166 TAILQ_INIT(&s->parents);
5167 STAILQ_INIT(&s->colors);
5169 s->commit_id = got_object_id_dup(commit_id);
5170 if (s->commit_id == NULL)
5171 return got_error_from_errno("got_object_id_dup");
5173 err = got_object_open_as_commit(&commit, repo, commit_id);
5174 if (err)
5175 goto done;
5178 * The root is opened here and will be closed when the view is closed.
5179 * Any visited subtrees and their path-wise parents are opened and
5180 * closed on demand.
5182 err = got_object_open_as_tree(&s->root, repo,
5183 got_object_commit_get_tree_id(commit));
5184 if (err)
5185 goto done;
5186 s->tree = s->root;
5188 err = got_object_id_str(&commit_id_str, commit_id);
5189 if (err != NULL)
5190 goto done;
5192 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5193 err = got_error_from_errno("asprintf");
5194 goto done;
5197 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5198 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5199 if (head_ref_name) {
5200 s->head_ref_name = strdup(head_ref_name);
5201 if (s->head_ref_name == NULL) {
5202 err = got_error_from_errno("strdup");
5203 goto done;
5206 s->repo = repo;
5208 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5209 err = add_color(&s->colors, "\\$$",
5210 TOG_COLOR_TREE_SUBMODULE,
5211 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5212 if (err)
5213 goto done;
5214 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5215 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5216 if (err)
5217 goto done;
5218 err = add_color(&s->colors, "/$",
5219 TOG_COLOR_TREE_DIRECTORY,
5220 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5221 if (err)
5222 goto done;
5224 err = add_color(&s->colors, "\\*$",
5225 TOG_COLOR_TREE_EXECUTABLE,
5226 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5227 if (err)
5228 goto done;
5230 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5231 get_color_value("TOG_COLOR_COMMIT"));
5232 if (err)
5233 goto done;
5236 view->show = show_tree_view;
5237 view->input = input_tree_view;
5238 view->close = close_tree_view;
5239 view->search_start = search_start_tree_view;
5240 view->search_next = search_next_tree_view;
5241 done:
5242 free(commit_id_str);
5243 if (commit)
5244 got_object_commit_close(commit);
5245 if (err)
5246 close_tree_view(view);
5247 return err;
5250 static const struct got_error *
5251 close_tree_view(struct tog_view *view)
5253 struct tog_tree_view_state *s = &view->state.tree;
5255 free_colors(&s->colors);
5256 free(s->tree_label);
5257 s->tree_label = NULL;
5258 free(s->commit_id);
5259 s->commit_id = NULL;
5260 free(s->head_ref_name);
5261 s->head_ref_name = NULL;
5262 while (!TAILQ_EMPTY(&s->parents)) {
5263 struct tog_parent_tree *parent;
5264 parent = TAILQ_FIRST(&s->parents);
5265 TAILQ_REMOVE(&s->parents, parent, entry);
5266 if (parent->tree != s->root)
5267 got_object_tree_close(parent->tree);
5268 free(parent);
5271 if (s->tree != NULL && s->tree != s->root)
5272 got_object_tree_close(s->tree);
5273 if (s->root)
5274 got_object_tree_close(s->root);
5275 return NULL;
5278 static const struct got_error *
5279 search_start_tree_view(struct tog_view *view)
5281 struct tog_tree_view_state *s = &view->state.tree;
5283 s->matched_entry = NULL;
5284 return NULL;
5287 static int
5288 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5290 regmatch_t regmatch;
5292 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5293 0) == 0;
5296 static const struct got_error *
5297 search_next_tree_view(struct tog_view *view)
5299 struct tog_tree_view_state *s = &view->state.tree;
5300 struct got_tree_entry *te = NULL;
5302 if (!view->searching) {
5303 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5304 return NULL;
5307 if (s->matched_entry) {
5308 if (view->searching == TOG_SEARCH_FORWARD) {
5309 if (s->selected_entry)
5310 te = got_tree_entry_get_next(s->tree,
5311 s->selected_entry);
5312 else
5313 te = got_object_tree_get_first_entry(s->tree);
5314 } else {
5315 if (s->selected_entry == NULL)
5316 te = got_object_tree_get_last_entry(s->tree);
5317 else
5318 te = got_tree_entry_get_prev(s->tree,
5319 s->selected_entry);
5321 } else {
5322 if (view->searching == TOG_SEARCH_FORWARD)
5323 te = got_object_tree_get_first_entry(s->tree);
5324 else
5325 te = got_object_tree_get_last_entry(s->tree);
5328 while (1) {
5329 if (te == NULL) {
5330 if (s->matched_entry == NULL) {
5331 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5332 return NULL;
5334 if (view->searching == TOG_SEARCH_FORWARD)
5335 te = got_object_tree_get_first_entry(s->tree);
5336 else
5337 te = got_object_tree_get_last_entry(s->tree);
5340 if (match_tree_entry(te, &view->regex)) {
5341 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5342 s->matched_entry = te;
5343 break;
5346 if (view->searching == TOG_SEARCH_FORWARD)
5347 te = got_tree_entry_get_next(s->tree, te);
5348 else
5349 te = got_tree_entry_get_prev(s->tree, te);
5352 if (s->matched_entry) {
5353 s->first_displayed_entry = s->matched_entry;
5354 s->selected = 0;
5357 return NULL;
5360 static const struct got_error *
5361 show_tree_view(struct tog_view *view)
5363 const struct got_error *err = NULL;
5364 struct tog_tree_view_state *s = &view->state.tree;
5365 char *parent_path;
5367 err = tree_entry_path(&parent_path, &s->parents, NULL);
5368 if (err)
5369 return err;
5371 err = draw_tree_entries(view, parent_path);
5372 free(parent_path);
5374 view_vborder(view);
5375 return err;
5378 static const struct got_error *
5379 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5381 const struct got_error *err = NULL;
5382 struct tog_tree_view_state *s = &view->state.tree;
5383 struct tog_view *log_view, *ref_view;
5384 int begin_x = 0;
5386 switch (ch) {
5387 case 'i':
5388 s->show_ids = !s->show_ids;
5389 break;
5390 case 'l':
5391 if (!s->selected_entry)
5392 break;
5393 if (view_is_parent_view(view))
5394 begin_x = view_split_begin_x(view->begin_x);
5395 err = log_selected_tree_entry(&log_view, begin_x, s);
5396 view->focussed = 0;
5397 log_view->focussed = 1;
5398 if (view_is_parent_view(view)) {
5399 err = view_close_child(view);
5400 if (err)
5401 return err;
5402 view_set_child(view, log_view);
5403 view->focus_child = 1;
5404 } else
5405 *new_view = log_view;
5406 break;
5407 case 'r':
5408 if (view_is_parent_view(view))
5409 begin_x = view_split_begin_x(view->begin_x);
5410 ref_view = view_open(view->nlines, view->ncols,
5411 view->begin_y, begin_x, TOG_VIEW_REF);
5412 if (ref_view == NULL)
5413 return got_error_from_errno("view_open");
5414 err = open_ref_view(ref_view, s->repo);
5415 if (err) {
5416 view_close(ref_view);
5417 return err;
5419 view->focussed = 0;
5420 ref_view->focussed = 1;
5421 if (view_is_parent_view(view)) {
5422 err = view_close_child(view);
5423 if (err)
5424 return err;
5425 view_set_child(view, ref_view);
5426 view->focus_child = 1;
5427 } else
5428 *new_view = ref_view;
5429 break;
5430 case 'k':
5431 case KEY_UP:
5432 if (s->selected > 0) {
5433 s->selected--;
5434 break;
5436 tree_scroll_up(s, 1);
5437 break;
5438 case KEY_PPAGE:
5439 case CTRL('b'):
5440 if (s->tree == s->root) {
5441 if (got_object_tree_get_first_entry(s->tree) ==
5442 s->first_displayed_entry)
5443 s->selected = 0;
5444 } else {
5445 if (s->first_displayed_entry == NULL)
5446 s->selected = 0;
5448 tree_scroll_up(s, MAX(0, view->nlines - 3));
5449 break;
5450 case 'j':
5451 case KEY_DOWN:
5452 if (s->selected < s->ndisplayed - 1) {
5453 s->selected++;
5454 break;
5456 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5457 == NULL)
5458 /* can't scroll any further */
5459 break;
5460 tree_scroll_down(s, 1);
5461 break;
5462 case KEY_NPAGE:
5463 case CTRL('f'):
5464 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5465 == NULL) {
5466 /* can't scroll any further; move cursor down */
5467 if (s->selected < s->ndisplayed - 1)
5468 s->selected = s->ndisplayed - 1;
5469 break;
5471 tree_scroll_down(s, view->nlines - 3);
5472 break;
5473 case KEY_ENTER:
5474 case '\r':
5475 case KEY_BACKSPACE:
5476 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5477 struct tog_parent_tree *parent;
5478 /* user selected '..' */
5479 if (s->tree == s->root)
5480 break;
5481 parent = TAILQ_FIRST(&s->parents);
5482 TAILQ_REMOVE(&s->parents, parent,
5483 entry);
5484 got_object_tree_close(s->tree);
5485 s->tree = parent->tree;
5486 s->first_displayed_entry =
5487 parent->first_displayed_entry;
5488 s->selected_entry =
5489 parent->selected_entry;
5490 s->selected = parent->selected;
5491 free(parent);
5492 } else if (S_ISDIR(got_tree_entry_get_mode(
5493 s->selected_entry))) {
5494 struct got_tree_object *subtree;
5495 err = got_object_open_as_tree(&subtree, s->repo,
5496 got_tree_entry_get_id(s->selected_entry));
5497 if (err)
5498 break;
5499 err = tree_view_visit_subtree(s, subtree);
5500 if (err) {
5501 got_object_tree_close(subtree);
5502 break;
5504 } else if (S_ISREG(got_tree_entry_get_mode(
5505 s->selected_entry))) {
5506 struct tog_view *blame_view;
5507 int begin_x = view_is_parent_view(view) ?
5508 view_split_begin_x(view->begin_x) : 0;
5510 err = blame_tree_entry(&blame_view, begin_x,
5511 s->selected_entry, &s->parents,
5512 s->commit_id, s->repo);
5513 if (err)
5514 break;
5515 view->focussed = 0;
5516 blame_view->focussed = 1;
5517 if (view_is_parent_view(view)) {
5518 err = view_close_child(view);
5519 if (err)
5520 return err;
5521 view_set_child(view, blame_view);
5522 view->focus_child = 1;
5523 } else
5524 *new_view = blame_view;
5526 break;
5527 case KEY_RESIZE:
5528 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5529 s->selected = view->nlines - 4;
5530 break;
5531 default:
5532 break;
5535 return err;
5538 __dead static void
5539 usage_tree(void)
5541 endwin();
5542 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5543 getprogname());
5544 exit(1);
5547 static const struct got_error *
5548 cmd_tree(int argc, char *argv[])
5550 const struct got_error *error;
5551 struct got_repository *repo = NULL;
5552 struct got_worktree *worktree = NULL;
5553 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5554 struct got_object_id *commit_id = NULL;
5555 const char *commit_id_arg = NULL;
5556 char *label = NULL;
5557 struct got_reference *ref = NULL;
5558 const char *head_ref_name = NULL;
5559 int ch;
5560 struct tog_view *view;
5562 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5563 switch (ch) {
5564 case 'c':
5565 commit_id_arg = optarg;
5566 break;
5567 case 'r':
5568 repo_path = realpath(optarg, NULL);
5569 if (repo_path == NULL)
5570 return got_error_from_errno2("realpath",
5571 optarg);
5572 break;
5573 default:
5574 usage_tree();
5575 /* NOTREACHED */
5579 argc -= optind;
5580 argv += optind;
5582 if (argc > 1)
5583 usage_tree();
5585 if (repo_path == NULL) {
5586 cwd = getcwd(NULL, 0);
5587 if (cwd == NULL)
5588 return got_error_from_errno("getcwd");
5589 error = got_worktree_open(&worktree, cwd);
5590 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5591 goto done;
5592 if (worktree)
5593 repo_path =
5594 strdup(got_worktree_get_repo_path(worktree));
5595 else
5596 repo_path = strdup(cwd);
5597 if (repo_path == NULL) {
5598 error = got_error_from_errno("strdup");
5599 goto done;
5603 error = got_repo_open(&repo, repo_path, NULL);
5604 if (error != NULL)
5605 goto done;
5607 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5608 repo, worktree);
5609 if (error)
5610 goto done;
5612 init_curses();
5614 error = apply_unveil(got_repo_get_path(repo), NULL);
5615 if (error)
5616 goto done;
5618 error = tog_load_refs(repo);
5619 if (error)
5620 goto done;
5622 if (commit_id_arg == NULL) {
5623 error = got_repo_match_object_id(&commit_id, &label,
5624 worktree ? got_worktree_get_head_ref_name(worktree) :
5625 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5626 if (error)
5627 goto done;
5628 head_ref_name = label;
5629 } else {
5630 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5631 if (error == NULL)
5632 head_ref_name = got_ref_get_name(ref);
5633 else if (error->code != GOT_ERR_NOT_REF)
5634 goto done;
5635 error = got_repo_match_object_id(&commit_id, NULL,
5636 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5637 if (error)
5638 goto done;
5641 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5642 if (view == NULL) {
5643 error = got_error_from_errno("view_open");
5644 goto done;
5646 error = open_tree_view(view, commit_id, head_ref_name, repo);
5647 if (error)
5648 goto done;
5649 if (!got_path_is_root_dir(in_repo_path)) {
5650 error = tree_view_walk_path(&view->state.tree, commit_id,
5651 in_repo_path);
5652 if (error)
5653 goto done;
5656 if (worktree) {
5657 /* Release work tree lock. */
5658 got_worktree_close(worktree);
5659 worktree = NULL;
5661 error = view_loop(view);
5662 done:
5663 free(repo_path);
5664 free(cwd);
5665 free(commit_id);
5666 free(label);
5667 if (ref)
5668 got_ref_close(ref);
5669 if (repo) {
5670 const struct got_error *close_err = got_repo_close(repo);
5671 if (error == NULL)
5672 error = close_err;
5674 tog_free_refs();
5675 return error;
5678 static const struct got_error *
5679 ref_view_load_refs(struct tog_ref_view_state *s)
5681 struct got_reflist_entry *sre;
5682 struct tog_reflist_entry *re;
5684 s->nrefs = 0;
5685 TAILQ_FOREACH(sre, &tog_refs, entry) {
5686 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5687 continue;
5689 re = malloc(sizeof(*re));
5690 if (re == NULL)
5691 return got_error_from_errno("malloc");
5693 re->ref = got_ref_dup(sre->ref);
5694 if (re->ref == NULL)
5695 return got_error_from_errno("got_ref_dup");
5696 re->idx = s->nrefs++;
5697 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5700 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5701 return NULL;
5704 void
5705 ref_view_free_refs(struct tog_ref_view_state *s)
5707 struct tog_reflist_entry *re;
5709 while (!TAILQ_EMPTY(&s->refs)) {
5710 re = TAILQ_FIRST(&s->refs);
5711 TAILQ_REMOVE(&s->refs, re, entry);
5712 got_ref_close(re->ref);
5713 free(re);
5717 static const struct got_error *
5718 open_ref_view(struct tog_view *view, struct got_repository *repo)
5720 const struct got_error *err = NULL;
5721 struct tog_ref_view_state *s = &view->state.ref;
5723 s->selected_entry = 0;
5724 s->repo = repo;
5726 TAILQ_INIT(&s->refs);
5727 STAILQ_INIT(&s->colors);
5729 err = ref_view_load_refs(s);
5730 if (err)
5731 return err;
5733 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5734 err = add_color(&s->colors, "^refs/heads/",
5735 TOG_COLOR_REFS_HEADS,
5736 get_color_value("TOG_COLOR_REFS_HEADS"));
5737 if (err)
5738 goto done;
5740 err = add_color(&s->colors, "^refs/tags/",
5741 TOG_COLOR_REFS_TAGS,
5742 get_color_value("TOG_COLOR_REFS_TAGS"));
5743 if (err)
5744 goto done;
5746 err = add_color(&s->colors, "^refs/remotes/",
5747 TOG_COLOR_REFS_REMOTES,
5748 get_color_value("TOG_COLOR_REFS_REMOTES"));
5749 if (err)
5750 goto done;
5753 view->show = show_ref_view;
5754 view->input = input_ref_view;
5755 view->close = close_ref_view;
5756 view->search_start = search_start_ref_view;
5757 view->search_next = search_next_ref_view;
5758 done:
5759 if (err)
5760 free_colors(&s->colors);
5761 return err;
5764 static const struct got_error *
5765 close_ref_view(struct tog_view *view)
5767 struct tog_ref_view_state *s = &view->state.ref;
5769 ref_view_free_refs(s);
5770 free_colors(&s->colors);
5772 return NULL;
5775 static const struct got_error *
5776 resolve_reflist_entry(struct got_object_id **commit_id,
5777 struct tog_reflist_entry *re, struct got_repository *repo)
5779 const struct got_error *err = NULL;
5780 struct got_object_id *obj_id;
5781 struct got_tag_object *tag = NULL;
5782 int obj_type;
5784 *commit_id = NULL;
5786 err = got_ref_resolve(&obj_id, repo, re->ref);
5787 if (err)
5788 return err;
5790 err = got_object_get_type(&obj_type, repo, obj_id);
5791 if (err)
5792 goto done;
5794 switch (obj_type) {
5795 case GOT_OBJ_TYPE_COMMIT:
5796 *commit_id = obj_id;
5797 break;
5798 case GOT_OBJ_TYPE_TAG:
5799 err = got_object_open_as_tag(&tag, repo, obj_id);
5800 if (err)
5801 goto done;
5802 free(obj_id);
5803 err = got_object_get_type(&obj_type, repo,
5804 got_object_tag_get_object_id(tag));
5805 if (err)
5806 goto done;
5807 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5808 err = got_error(GOT_ERR_OBJ_TYPE);
5809 goto done;
5811 *commit_id = got_object_id_dup(
5812 got_object_tag_get_object_id(tag));
5813 if (*commit_id == NULL) {
5814 err = got_error_from_errno("got_object_id_dup");
5815 goto done;
5817 break;
5818 default:
5819 err = got_error(GOT_ERR_OBJ_TYPE);
5820 break;
5823 done:
5824 if (tag)
5825 got_object_tag_close(tag);
5826 if (err) {
5827 free(*commit_id);
5828 *commit_id = NULL;
5830 return err;
5833 static const struct got_error *
5834 log_ref_entry(struct tog_view **new_view, int begin_x,
5835 struct tog_reflist_entry *re, struct got_repository *repo)
5837 struct tog_view *log_view;
5838 const struct got_error *err = NULL;
5839 struct got_object_id *commit_id = NULL;
5841 *new_view = NULL;
5843 err = resolve_reflist_entry(&commit_id, re, repo);
5844 if (err) {
5845 if (err->code != GOT_ERR_OBJ_TYPE)
5846 return err;
5847 else
5848 return NULL;
5851 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5852 if (log_view == NULL) {
5853 err = got_error_from_errno("view_open");
5854 goto done;
5857 err = open_log_view(log_view, commit_id, repo,
5858 got_ref_get_name(re->ref), "", 0);
5859 done:
5860 if (err)
5861 view_close(log_view);
5862 else
5863 *new_view = log_view;
5864 free(commit_id);
5865 return err;
5868 static void
5869 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5871 struct tog_reflist_entry *re;
5872 int i = 0;
5874 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5875 return;
5877 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5878 while (i++ < maxscroll) {
5879 if (re == NULL)
5880 break;
5881 s->first_displayed_entry = re;
5882 re = TAILQ_PREV(re, tog_reflist_head, entry);
5886 static void
5887 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5889 struct tog_reflist_entry *next, *last;
5890 int n = 0;
5892 if (s->first_displayed_entry)
5893 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5894 else
5895 next = TAILQ_FIRST(&s->refs);
5897 last = s->last_displayed_entry;
5898 while (next && last && n++ < maxscroll) {
5899 last = TAILQ_NEXT(last, entry);
5900 if (last) {
5901 s->first_displayed_entry = next;
5902 next = TAILQ_NEXT(next, entry);
5907 static const struct got_error *
5908 search_start_ref_view(struct tog_view *view)
5910 struct tog_ref_view_state *s = &view->state.ref;
5912 s->matched_entry = NULL;
5913 return NULL;
5916 static int
5917 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5919 regmatch_t regmatch;
5921 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5922 0) == 0;
5925 static const struct got_error *
5926 search_next_ref_view(struct tog_view *view)
5928 struct tog_ref_view_state *s = &view->state.ref;
5929 struct tog_reflist_entry *re = NULL;
5931 if (!view->searching) {
5932 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5933 return NULL;
5936 if (s->matched_entry) {
5937 if (view->searching == TOG_SEARCH_FORWARD) {
5938 if (s->selected_entry)
5939 re = TAILQ_NEXT(s->selected_entry, entry);
5940 else
5941 re = TAILQ_PREV(s->selected_entry,
5942 tog_reflist_head, entry);
5943 } else {
5944 if (s->selected_entry == NULL)
5945 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5946 else
5947 re = TAILQ_PREV(s->selected_entry,
5948 tog_reflist_head, entry);
5950 } else {
5951 if (view->searching == TOG_SEARCH_FORWARD)
5952 re = TAILQ_FIRST(&s->refs);
5953 else
5954 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5957 while (1) {
5958 if (re == NULL) {
5959 if (s->matched_entry == NULL) {
5960 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5961 return NULL;
5963 if (view->searching == TOG_SEARCH_FORWARD)
5964 re = TAILQ_FIRST(&s->refs);
5965 else
5966 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5969 if (match_reflist_entry(re, &view->regex)) {
5970 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5971 s->matched_entry = re;
5972 break;
5975 if (view->searching == TOG_SEARCH_FORWARD)
5976 re = TAILQ_NEXT(re, entry);
5977 else
5978 re = TAILQ_PREV(re, tog_reflist_head, entry);
5981 if (s->matched_entry) {
5982 s->first_displayed_entry = s->matched_entry;
5983 s->selected = 0;
5986 return NULL;
5989 static const struct got_error *
5990 show_ref_view(struct tog_view *view)
5992 const struct got_error *err = NULL;
5993 struct tog_ref_view_state *s = &view->state.ref;
5994 struct tog_reflist_entry *re;
5995 char *line = NULL;
5996 wchar_t *wline;
5997 struct tog_color *tc;
5998 int width, n;
5999 int limit = view->nlines;
6001 werase(view->window);
6003 s->ndisplayed = 0;
6005 if (limit == 0)
6006 return NULL;
6008 re = s->first_displayed_entry;
6010 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6011 s->nrefs) == -1)
6012 return got_error_from_errno("asprintf");
6014 err = format_line(&wline, &width, line, view->ncols, 0);
6015 if (err) {
6016 free(line);
6017 return err;
6019 if (view_needs_focus_indication(view))
6020 wstandout(view->window);
6021 waddwstr(view->window, wline);
6022 if (view_needs_focus_indication(view))
6023 wstandend(view->window);
6024 free(wline);
6025 wline = NULL;
6026 free(line);
6027 line = NULL;
6028 if (width < view->ncols - 1)
6029 waddch(view->window, '\n');
6030 if (--limit <= 0)
6031 return NULL;
6033 n = 0;
6034 while (re && limit > 0) {
6035 char *line = NULL;
6037 if (got_ref_is_symbolic(re->ref)) {
6038 if (asprintf(&line, "%s -> %s",
6039 got_ref_get_name(re->ref),
6040 got_ref_get_symref_target(re->ref)) == -1)
6041 return got_error_from_errno("asprintf");
6042 } else if (s->show_ids) {
6043 struct got_object_id *id;
6044 char *id_str;
6045 err = got_ref_resolve(&id, s->repo, re->ref);
6046 if (err)
6047 return err;
6048 err = got_object_id_str(&id_str, id);
6049 if (err) {
6050 free(id);
6051 return err;
6053 if (asprintf(&line, "%s: %s",
6054 got_ref_get_name(re->ref), id_str) == -1) {
6055 err = got_error_from_errno("asprintf");
6056 free(id);
6057 free(id_str);
6058 return err;
6060 free(id);
6061 free(id_str);
6062 } else {
6063 line = strdup(got_ref_get_name(re->ref));
6064 if (line == NULL)
6065 return got_error_from_errno("strdup");
6068 err = format_line(&wline, &width, line, view->ncols, 0);
6069 if (err) {
6070 free(line);
6071 return err;
6073 if (n == s->selected) {
6074 if (view->focussed)
6075 wstandout(view->window);
6076 s->selected_entry = re;
6078 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6079 if (tc)
6080 wattr_on(view->window,
6081 COLOR_PAIR(tc->colorpair), NULL);
6082 waddwstr(view->window, wline);
6083 if (tc)
6084 wattr_off(view->window,
6085 COLOR_PAIR(tc->colorpair), NULL);
6086 if (width < view->ncols - 1)
6087 waddch(view->window, '\n');
6088 if (n == s->selected && view->focussed)
6089 wstandend(view->window);
6090 free(line);
6091 free(wline);
6092 wline = NULL;
6093 n++;
6094 s->ndisplayed++;
6095 s->last_displayed_entry = re;
6097 limit--;
6098 re = TAILQ_NEXT(re, entry);
6101 view_vborder(view);
6102 return err;
6105 static const struct got_error *
6106 browse_ref_tree(struct tog_view **new_view, int begin_x,
6107 struct tog_reflist_entry *re, struct got_repository *repo)
6109 const struct got_error *err = NULL;
6110 struct got_object_id *commit_id = NULL;
6111 struct tog_view *tree_view;
6113 *new_view = NULL;
6115 err = resolve_reflist_entry(&commit_id, re, repo);
6116 if (err) {
6117 if (err->code != GOT_ERR_OBJ_TYPE)
6118 return err;
6119 else
6120 return NULL;
6124 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6125 if (tree_view == NULL) {
6126 err = got_error_from_errno("view_open");
6127 goto done;
6130 err = open_tree_view(tree_view, commit_id,
6131 got_ref_get_name(re->ref), repo);
6132 if (err)
6133 goto done;
6135 *new_view = tree_view;
6136 done:
6137 free(commit_id);
6138 return err;
6140 static const struct got_error *
6141 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6143 const struct got_error *err = NULL;
6144 struct tog_ref_view_state *s = &view->state.ref;
6145 struct tog_view *log_view, *tree_view;
6146 int begin_x = 0;
6148 switch (ch) {
6149 case 'i':
6150 s->show_ids = !s->show_ids;
6151 break;
6152 case KEY_ENTER:
6153 case '\r':
6154 if (!s->selected_entry)
6155 break;
6156 if (view_is_parent_view(view))
6157 begin_x = view_split_begin_x(view->begin_x);
6158 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6159 s->repo);
6160 view->focussed = 0;
6161 log_view->focussed = 1;
6162 if (view_is_parent_view(view)) {
6163 err = view_close_child(view);
6164 if (err)
6165 return err;
6166 view_set_child(view, log_view);
6167 view->focus_child = 1;
6168 } else
6169 *new_view = log_view;
6170 break;
6171 case 't':
6172 if (!s->selected_entry)
6173 break;
6174 if (view_is_parent_view(view))
6175 begin_x = view_split_begin_x(view->begin_x);
6176 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6177 s->repo);
6178 if (err || tree_view == NULL)
6179 break;
6180 view->focussed = 0;
6181 tree_view->focussed = 1;
6182 if (view_is_parent_view(view)) {
6183 err = view_close_child(view);
6184 if (err)
6185 return err;
6186 view_set_child(view, tree_view);
6187 view->focus_child = 1;
6188 } else
6189 *new_view = tree_view;
6190 break;
6191 case 'k':
6192 case KEY_UP:
6193 if (s->selected > 0) {
6194 s->selected--;
6195 break;
6197 ref_scroll_up(s, 1);
6198 break;
6199 case KEY_PPAGE:
6200 case CTRL('b'):
6201 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6202 s->selected = 0;
6203 ref_scroll_up(s, MAX(0, view->nlines - 1));
6204 break;
6205 case 'j':
6206 case KEY_DOWN:
6207 if (s->selected < s->ndisplayed - 1) {
6208 s->selected++;
6209 break;
6211 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6212 /* can't scroll any further */
6213 break;
6214 ref_scroll_down(s, 1);
6215 break;
6216 case KEY_NPAGE:
6217 case CTRL('f'):
6218 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6219 /* can't scroll any further; move cursor down */
6220 if (s->selected < s->ndisplayed - 1)
6221 s->selected = s->ndisplayed - 1;
6222 break;
6224 ref_scroll_down(s, view->nlines - 1);
6225 break;
6226 case CTRL('l'):
6227 tog_free_refs();
6228 err = tog_load_refs(s->repo);
6229 if (err)
6230 break;
6231 ref_view_free_refs(s);
6232 err = ref_view_load_refs(s);
6233 break;
6234 case KEY_RESIZE:
6235 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6236 s->selected = view->nlines - 2;
6237 break;
6238 default:
6239 break;
6242 return err;
6245 __dead static void
6246 usage_ref(void)
6248 endwin();
6249 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6250 getprogname());
6251 exit(1);
6254 static const struct got_error *
6255 cmd_ref(int argc, char *argv[])
6257 const struct got_error *error;
6258 struct got_repository *repo = NULL;
6259 struct got_worktree *worktree = NULL;
6260 char *cwd = NULL, *repo_path = NULL;
6261 int ch;
6262 struct tog_view *view;
6264 while ((ch = getopt(argc, argv, "r:")) != -1) {
6265 switch (ch) {
6266 case 'r':
6267 repo_path = realpath(optarg, NULL);
6268 if (repo_path == NULL)
6269 return got_error_from_errno2("realpath",
6270 optarg);
6271 break;
6272 default:
6273 usage_ref();
6274 /* NOTREACHED */
6278 argc -= optind;
6279 argv += optind;
6281 if (argc > 1)
6282 usage_ref();
6284 if (repo_path == NULL) {
6285 cwd = getcwd(NULL, 0);
6286 if (cwd == NULL)
6287 return got_error_from_errno("getcwd");
6288 error = got_worktree_open(&worktree, cwd);
6289 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6290 goto done;
6291 if (worktree)
6292 repo_path =
6293 strdup(got_worktree_get_repo_path(worktree));
6294 else
6295 repo_path = strdup(cwd);
6296 if (repo_path == NULL) {
6297 error = got_error_from_errno("strdup");
6298 goto done;
6302 error = got_repo_open(&repo, repo_path, NULL);
6303 if (error != NULL)
6304 goto done;
6306 init_curses();
6308 error = apply_unveil(got_repo_get_path(repo), NULL);
6309 if (error)
6310 goto done;
6312 error = tog_load_refs(repo);
6313 if (error)
6314 goto done;
6316 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6317 if (view == NULL) {
6318 error = got_error_from_errno("view_open");
6319 goto done;
6322 error = open_ref_view(view, repo);
6323 if (error)
6324 goto done;
6326 if (worktree) {
6327 /* Release work tree lock. */
6328 got_worktree_close(worktree);
6329 worktree = NULL;
6331 error = view_loop(view);
6332 done:
6333 free(repo_path);
6334 free(cwd);
6335 if (repo) {
6336 const struct got_error *close_err = got_repo_close(repo);
6337 if (close_err)
6338 error = close_err;
6340 tog_free_refs();
6341 return error;
6344 static void
6345 list_commands(FILE *fp)
6347 size_t i;
6349 fprintf(fp, "commands:");
6350 for (i = 0; i < nitems(tog_commands); i++) {
6351 struct tog_cmd *cmd = &tog_commands[i];
6352 fprintf(fp, " %s", cmd->name);
6354 fputc('\n', fp);
6357 __dead static void
6358 usage(int hflag, int status)
6360 FILE *fp = (status == 0) ? stdout : stderr;
6362 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6363 getprogname());
6364 if (hflag) {
6365 fprintf(fp, "lazy usage: %s path\n", getprogname());
6366 list_commands(fp);
6368 exit(status);
6371 static char **
6372 make_argv(int argc, ...)
6374 va_list ap;
6375 char **argv;
6376 int i;
6378 va_start(ap, argc);
6380 argv = calloc(argc, sizeof(char *));
6381 if (argv == NULL)
6382 err(1, "calloc");
6383 for (i = 0; i < argc; i++) {
6384 argv[i] = strdup(va_arg(ap, char *));
6385 if (argv[i] == NULL)
6386 err(1, "strdup");
6389 va_end(ap);
6390 return argv;
6394 * Try to convert 'tog path' into a 'tog log path' command.
6395 * The user could simply have mistyped the command rather than knowingly
6396 * provided a path. So check whether argv[0] can in fact be resolved
6397 * to a path in the HEAD commit and print a special error if not.
6398 * This hack is for mpi@ <3
6400 static const struct got_error *
6401 tog_log_with_path(int argc, char *argv[])
6403 const struct got_error *error = NULL, *close_err;
6404 struct tog_cmd *cmd = NULL;
6405 struct got_repository *repo = NULL;
6406 struct got_worktree *worktree = NULL;
6407 struct got_object_id *commit_id = NULL, *id = NULL;
6408 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6409 char *commit_id_str = NULL, **cmd_argv = NULL;
6411 cwd = getcwd(NULL, 0);
6412 if (cwd == NULL)
6413 return got_error_from_errno("getcwd");
6415 error = got_worktree_open(&worktree, cwd);
6416 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6417 goto done;
6419 if (worktree)
6420 repo_path = strdup(got_worktree_get_repo_path(worktree));
6421 else
6422 repo_path = strdup(cwd);
6423 if (repo_path == NULL) {
6424 error = got_error_from_errno("strdup");
6425 goto done;
6428 error = got_repo_open(&repo, repo_path, NULL);
6429 if (error != NULL)
6430 goto done;
6432 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6433 repo, worktree);
6434 if (error)
6435 goto done;
6437 error = tog_load_refs(repo);
6438 if (error)
6439 goto done;
6440 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6441 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6442 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6443 if (error)
6444 goto done;
6446 if (worktree) {
6447 got_worktree_close(worktree);
6448 worktree = NULL;
6451 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6452 if (error) {
6453 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6454 goto done;
6455 fprintf(stderr, "%s: '%s' is no known command or path\n",
6456 getprogname(), argv[0]);
6457 usage(1, 1);
6458 /* not reached */
6461 close_err = got_repo_close(repo);
6462 if (error == NULL)
6463 error = close_err;
6464 repo = NULL;
6466 error = got_object_id_str(&commit_id_str, commit_id);
6467 if (error)
6468 goto done;
6470 cmd = &tog_commands[0]; /* log */
6471 argc = 4;
6472 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6473 error = cmd->cmd_main(argc, cmd_argv);
6474 done:
6475 if (repo) {
6476 close_err = got_repo_close(repo);
6477 if (error == NULL)
6478 error = close_err;
6480 if (worktree)
6481 got_worktree_close(worktree);
6482 free(id);
6483 free(commit_id_str);
6484 free(commit_id);
6485 free(cwd);
6486 free(repo_path);
6487 free(in_repo_path);
6488 if (cmd_argv) {
6489 int i;
6490 for (i = 0; i < argc; i++)
6491 free(cmd_argv[i]);
6492 free(cmd_argv);
6494 tog_free_refs();
6495 return error;
6498 int
6499 main(int argc, char *argv[])
6501 const struct got_error *error = NULL;
6502 struct tog_cmd *cmd = NULL;
6503 int ch, hflag = 0, Vflag = 0;
6504 char **cmd_argv = NULL;
6505 static struct option longopts[] = {
6506 { "version", no_argument, NULL, 'V' },
6507 { NULL, 0, NULL, 0}
6510 setlocale(LC_CTYPE, "");
6512 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6513 switch (ch) {
6514 case 'h':
6515 hflag = 1;
6516 break;
6517 case 'V':
6518 Vflag = 1;
6519 break;
6520 default:
6521 usage(hflag, 1);
6522 /* NOTREACHED */
6526 argc -= optind;
6527 argv += optind;
6528 optind = 1;
6529 optreset = 1;
6531 if (Vflag) {
6532 got_version_print_str();
6533 return 0;
6536 #ifndef PROFILE
6537 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6538 NULL) == -1)
6539 err(1, "pledge");
6540 #endif
6542 if (argc == 0) {
6543 if (hflag)
6544 usage(hflag, 0);
6545 /* Build an argument vector which runs a default command. */
6546 cmd = &tog_commands[0];
6547 argc = 1;
6548 cmd_argv = make_argv(argc, cmd->name);
6549 } else {
6550 size_t i;
6552 /* Did the user specify a command? */
6553 for (i = 0; i < nitems(tog_commands); i++) {
6554 if (strncmp(tog_commands[i].name, argv[0],
6555 strlen(argv[0])) == 0) {
6556 cmd = &tog_commands[i];
6557 break;
6562 if (cmd == NULL) {
6563 if (argc != 1)
6564 usage(0, 1);
6565 /* No command specified; try log with a path */
6566 error = tog_log_with_path(argc, argv);
6567 } else {
6568 if (hflag)
6569 cmd->cmd_usage();
6570 else
6571 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6574 endwin();
6575 putchar('\n');
6576 if (cmd_argv) {
6577 int i;
6578 for (i = 0; i < argc; i++)
6579 free(cmd_argv[i]);
6580 free(cmd_argv);
6583 if (error && error->code != GOT_ERR_CANCELLED)
6584 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6585 return 0;