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 if (s->thread_args.load_all) {
2393 if (ch == KEY_BACKSPACE)
2394 s->thread_args.load_all = 0;
2395 else if (s->thread_args.log_complete) {
2396 s->thread_args.load_all = 0;
2397 log_scroll_down(view, s->commits.ncommits);
2398 s->selected = MIN(view->nlines - 2,
2399 s->commits.ncommits - 1);
2400 select_commit(s);
2402 return NULL;
2405 switch (ch) {
2406 case 'q':
2407 s->quit = 1;
2408 break;
2409 case 'k':
2410 case KEY_UP:
2411 case '<':
2412 case ',':
2413 if (s->first_displayed_entry == NULL)
2414 break;
2415 if (s->selected > 0)
2416 s->selected--;
2417 else
2418 log_scroll_up(s, 1);
2419 select_commit(s);
2420 break;
2421 case 'g':
2422 case KEY_HOME:
2423 if (s->first_displayed_entry == NULL)
2424 break;
2426 s->selected = 0;
2427 log_scroll_up(s, s->commits.ncommits);
2428 select_commit(s);
2429 break;
2430 case KEY_PPAGE:
2431 case CTRL('b'):
2432 if (s->first_displayed_entry == NULL)
2433 break;
2434 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2435 s->selected = 0;
2436 else
2437 log_scroll_up(s, view->nlines - 1);
2438 select_commit(s);
2439 break;
2440 case 'j':
2441 case KEY_DOWN:
2442 case '>':
2443 case '.':
2444 if (s->first_displayed_entry == NULL)
2445 break;
2446 if (s->selected < MIN(view->nlines - 2,
2447 s->commits.ncommits - 1))
2448 s->selected++;
2449 else {
2450 err = log_scroll_down(view, 1);
2451 if (err)
2452 break;
2454 select_commit(s);
2455 break;
2456 case 'G':
2457 case KEY_END: {
2458 /* We don't know yet how many commits, so we're forced to
2459 * traverse them all. */
2460 if (!s->thread_args.log_complete) {
2461 s->thread_args.load_all = 1;
2462 return trigger_log_thread(view, 0);
2465 log_scroll_down(view, s->commits.ncommits);
2466 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2467 select_commit(s);
2468 break;
2470 case KEY_NPAGE:
2471 case CTRL('f'): {
2472 struct commit_queue_entry *first;
2473 first = s->first_displayed_entry;
2474 if (first == NULL)
2475 break;
2476 err = log_scroll_down(view, view->nlines - 1);
2477 if (err)
2478 break;
2479 if (first == s->first_displayed_entry &&
2480 s->selected < MIN(view->nlines - 2,
2481 s->commits.ncommits - 1)) {
2482 /* can't scroll further down */
2483 s->selected = MIN(view->nlines - 2,
2484 s->commits.ncommits - 1);
2486 select_commit(s);
2487 break;
2489 case KEY_RESIZE:
2490 if (s->selected > view->nlines - 2)
2491 s->selected = view->nlines - 2;
2492 if (s->selected > s->commits.ncommits - 1)
2493 s->selected = s->commits.ncommits - 1;
2494 select_commit(s);
2495 if (s->commits.ncommits < view->nlines - 1 &&
2496 !s->thread_args.log_complete) {
2497 s->thread_args.commits_needed += (view->nlines - 1) -
2498 s->commits.ncommits;
2499 err = trigger_log_thread(view, 1);
2501 break;
2502 case KEY_ENTER:
2503 case ' ':
2504 case '\r':
2505 if (s->selected_entry == NULL)
2506 break;
2507 if (view_is_parent_view(view))
2508 begin_x = view_split_begin_x(view->begin_x);
2509 err = open_diff_view_for_commit(&diff_view, begin_x,
2510 s->selected_entry->commit, s->selected_entry->id,
2511 view, s->repo);
2512 if (err)
2513 break;
2514 view->focussed = 0;
2515 diff_view->focussed = 1;
2516 if (view_is_parent_view(view)) {
2517 err = view_close_child(view);
2518 if (err)
2519 return err;
2520 view_set_child(view, diff_view);
2521 view->focus_child = 1;
2522 } else
2523 *new_view = diff_view;
2524 break;
2525 case 't':
2526 if (s->selected_entry == NULL)
2527 break;
2528 if (view_is_parent_view(view))
2529 begin_x = view_split_begin_x(view->begin_x);
2530 err = browse_commit_tree(&tree_view, begin_x,
2531 s->selected_entry, s->in_repo_path, s->head_ref_name,
2532 s->repo);
2533 if (err)
2534 break;
2535 view->focussed = 0;
2536 tree_view->focussed = 1;
2537 if (view_is_parent_view(view)) {
2538 err = view_close_child(view);
2539 if (err)
2540 return err;
2541 view_set_child(view, tree_view);
2542 view->focus_child = 1;
2543 } else
2544 *new_view = tree_view;
2545 break;
2546 case KEY_BACKSPACE:
2547 case CTRL('l'):
2548 case 'B':
2549 if (ch == KEY_BACKSPACE &&
2550 got_path_is_root_dir(s->in_repo_path))
2551 break;
2552 err = stop_log_thread(s);
2553 if (err)
2554 return err;
2555 if (ch == KEY_BACKSPACE) {
2556 char *parent_path;
2557 err = got_path_dirname(&parent_path, s->in_repo_path);
2558 if (err)
2559 return err;
2560 free(s->in_repo_path);
2561 s->in_repo_path = parent_path;
2562 s->thread_args.in_repo_path = s->in_repo_path;
2563 } else if (ch == CTRL('l')) {
2564 struct got_object_id *start_id;
2565 err = got_repo_match_object_id(&start_id, NULL,
2566 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2567 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2568 if (err)
2569 return err;
2570 free(s->start_id);
2571 s->start_id = start_id;
2572 s->thread_args.start_id = s->start_id;
2573 } else /* 'B' */
2574 s->log_branches = !s->log_branches;
2576 err = got_repo_open(&s->thread_args.repo,
2577 got_repo_get_path(s->repo), NULL);
2578 if (err)
2579 return err;
2580 tog_free_refs();
2581 err = tog_load_refs(s->repo);
2582 if (err)
2583 return err;
2584 err = got_commit_graph_open(&s->thread_args.graph,
2585 s->in_repo_path, !s->log_branches);
2586 if (err)
2587 return err;
2588 err = got_commit_graph_iter_start(s->thread_args.graph,
2589 s->start_id, s->repo, NULL, NULL);
2590 if (err)
2591 return err;
2592 free_commits(&s->commits);
2593 s->first_displayed_entry = NULL;
2594 s->last_displayed_entry = NULL;
2595 s->selected_entry = NULL;
2596 s->selected = 0;
2597 s->thread_args.log_complete = 0;
2598 s->quit = 0;
2599 s->thread_args.commits_needed = view->nlines;
2600 break;
2601 case 'r':
2602 if (view_is_parent_view(view))
2603 begin_x = view_split_begin_x(view->begin_x);
2604 ref_view = view_open(view->nlines, view->ncols,
2605 view->begin_y, begin_x, TOG_VIEW_REF);
2606 if (ref_view == NULL)
2607 return got_error_from_errno("view_open");
2608 err = open_ref_view(ref_view, s->repo);
2609 if (err) {
2610 view_close(ref_view);
2611 return err;
2613 view->focussed = 0;
2614 ref_view->focussed = 1;
2615 if (view_is_parent_view(view)) {
2616 err = view_close_child(view);
2617 if (err)
2618 return err;
2619 view_set_child(view, ref_view);
2620 view->focus_child = 1;
2621 } else
2622 *new_view = ref_view;
2623 break;
2624 default:
2625 break;
2628 return err;
2631 static const struct got_error *
2632 apply_unveil(const char *repo_path, const char *worktree_path)
2634 const struct got_error *error;
2636 #ifdef PROFILE
2637 if (unveil("gmon.out", "rwc") != 0)
2638 return got_error_from_errno2("unveil", "gmon.out");
2639 #endif
2640 if (repo_path && unveil(repo_path, "r") != 0)
2641 return got_error_from_errno2("unveil", repo_path);
2643 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2644 return got_error_from_errno2("unveil", worktree_path);
2646 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2647 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2649 error = got_privsep_unveil_exec_helpers();
2650 if (error != NULL)
2651 return error;
2653 if (unveil(NULL, NULL) != 0)
2654 return got_error_from_errno("unveil");
2656 return NULL;
2659 static void
2660 init_curses(void)
2662 initscr();
2663 cbreak();
2664 halfdelay(1); /* Do fast refresh while initial view is loading. */
2665 noecho();
2666 nonl();
2667 intrflush(stdscr, FALSE);
2668 keypad(stdscr, TRUE);
2669 curs_set(0);
2670 if (getenv("TOG_COLORS") != NULL) {
2671 start_color();
2672 use_default_colors();
2674 signal(SIGWINCH, tog_sigwinch);
2675 signal(SIGPIPE, tog_sigpipe);
2676 signal(SIGCONT, tog_sigcont);
2679 static const struct got_error *
2680 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2681 struct got_repository *repo, struct got_worktree *worktree)
2683 const struct got_error *err = NULL;
2685 if (argc == 0) {
2686 *in_repo_path = strdup("/");
2687 if (*in_repo_path == NULL)
2688 return got_error_from_errno("strdup");
2689 return NULL;
2692 if (worktree) {
2693 const char *prefix = got_worktree_get_path_prefix(worktree);
2694 char *p;
2696 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2697 if (err)
2698 return err;
2699 if (asprintf(in_repo_path, "%s%s%s", prefix,
2700 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2701 p) == -1) {
2702 err = got_error_from_errno("asprintf");
2703 *in_repo_path = NULL;
2705 free(p);
2706 } else
2707 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2709 return err;
2712 static const struct got_error *
2713 cmd_log(int argc, char *argv[])
2715 const struct got_error *error;
2716 struct got_repository *repo = NULL;
2717 struct got_worktree *worktree = NULL;
2718 struct got_object_id *start_id = NULL;
2719 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2720 char *start_commit = NULL, *label = NULL;
2721 struct got_reference *ref = NULL;
2722 const char *head_ref_name = NULL;
2723 int ch, log_branches = 0;
2724 struct tog_view *view;
2726 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2727 switch (ch) {
2728 case 'b':
2729 log_branches = 1;
2730 break;
2731 case 'c':
2732 start_commit = optarg;
2733 break;
2734 case 'r':
2735 repo_path = realpath(optarg, NULL);
2736 if (repo_path == NULL)
2737 return got_error_from_errno2("realpath",
2738 optarg);
2739 break;
2740 default:
2741 usage_log();
2742 /* NOTREACHED */
2746 argc -= optind;
2747 argv += optind;
2749 if (argc > 1)
2750 usage_log();
2752 if (repo_path == NULL) {
2753 cwd = getcwd(NULL, 0);
2754 if (cwd == NULL)
2755 return got_error_from_errno("getcwd");
2756 error = got_worktree_open(&worktree, cwd);
2757 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2758 goto done;
2759 if (worktree)
2760 repo_path =
2761 strdup(got_worktree_get_repo_path(worktree));
2762 else
2763 repo_path = strdup(cwd);
2764 if (repo_path == NULL) {
2765 error = got_error_from_errno("strdup");
2766 goto done;
2770 error = got_repo_open(&repo, repo_path, NULL);
2771 if (error != NULL)
2772 goto done;
2774 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2775 repo, worktree);
2776 if (error)
2777 goto done;
2779 init_curses();
2781 error = apply_unveil(got_repo_get_path(repo),
2782 worktree ? got_worktree_get_root_path(worktree) : NULL);
2783 if (error)
2784 goto done;
2786 /* already loaded by tog_log_with_path()? */
2787 if (TAILQ_EMPTY(&tog_refs)) {
2788 error = tog_load_refs(repo);
2789 if (error)
2790 goto done;
2793 if (start_commit == NULL) {
2794 error = got_repo_match_object_id(&start_id, &label,
2795 worktree ? got_worktree_get_head_ref_name(worktree) :
2796 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2797 if (error)
2798 goto done;
2799 head_ref_name = label;
2800 } else {
2801 error = got_ref_open(&ref, repo, start_commit, 0);
2802 if (error == NULL)
2803 head_ref_name = got_ref_get_name(ref);
2804 else if (error->code != GOT_ERR_NOT_REF)
2805 goto done;
2806 error = got_repo_match_object_id(&start_id, NULL,
2807 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2808 if (error)
2809 goto done;
2812 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2813 if (view == NULL) {
2814 error = got_error_from_errno("view_open");
2815 goto done;
2817 error = open_log_view(view, start_id, repo, head_ref_name,
2818 in_repo_path, log_branches);
2819 if (error)
2820 goto done;
2821 if (worktree) {
2822 /* Release work tree lock. */
2823 got_worktree_close(worktree);
2824 worktree = NULL;
2826 error = view_loop(view);
2827 done:
2828 free(in_repo_path);
2829 free(repo_path);
2830 free(cwd);
2831 free(start_id);
2832 free(label);
2833 if (ref)
2834 got_ref_close(ref);
2835 if (repo) {
2836 const struct got_error *close_err = got_repo_close(repo);
2837 if (error == NULL)
2838 error = close_err;
2840 if (worktree)
2841 got_worktree_close(worktree);
2842 tog_free_refs();
2843 return error;
2846 __dead static void
2847 usage_diff(void)
2849 endwin();
2850 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2851 "[-w] object1 object2\n", getprogname());
2852 exit(1);
2855 static int
2856 match_line(const char *line, regex_t *regex, size_t nmatch,
2857 regmatch_t *regmatch)
2859 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2862 struct tog_color *
2863 match_color(struct tog_colors *colors, const char *line)
2865 struct tog_color *tc = NULL;
2867 STAILQ_FOREACH(tc, colors, entry) {
2868 if (match_line(line, &tc->regex, 0, NULL))
2869 return tc;
2872 return NULL;
2875 static const struct got_error *
2876 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2877 WINDOW *window, regmatch_t *regmatch)
2879 const struct got_error *err = NULL;
2880 wchar_t *wline;
2881 int width;
2882 char *s;
2884 *wtotal = 0;
2886 s = strndup(line, regmatch->rm_so);
2887 if (s == NULL)
2888 return got_error_from_errno("strndup");
2890 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2891 if (err) {
2892 free(s);
2893 return err;
2895 waddwstr(window, wline);
2896 free(wline);
2897 free(s);
2898 wlimit -= width;
2899 *wtotal += width;
2901 if (wlimit > 0) {
2902 s = strndup(line + regmatch->rm_so,
2903 regmatch->rm_eo - regmatch->rm_so);
2904 if (s == NULL) {
2905 err = got_error_from_errno("strndup");
2906 free(s);
2907 return err;
2909 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2910 if (err) {
2911 free(s);
2912 return err;
2914 wattr_on(window, A_STANDOUT, NULL);
2915 waddwstr(window, wline);
2916 wattr_off(window, A_STANDOUT, NULL);
2917 free(wline);
2918 free(s);
2919 wlimit -= width;
2920 *wtotal += width;
2923 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2924 err = format_line(&wline, &width,
2925 line + regmatch->rm_eo, wlimit, col_tab_align);
2926 if (err)
2927 return err;
2928 waddwstr(window, wline);
2929 free(wline);
2930 *wtotal += width;
2933 return NULL;
2936 static const struct got_error *
2937 draw_file(struct tog_view *view, const char *header)
2939 struct tog_diff_view_state *s = &view->state.diff;
2940 regmatch_t *regmatch = &view->regmatch;
2941 const struct got_error *err;
2942 int nprinted = 0;
2943 char *line;
2944 size_t linesize = 0;
2945 ssize_t linelen;
2946 struct tog_color *tc;
2947 wchar_t *wline;
2948 int width;
2949 int max_lines = view->nlines;
2950 int nlines = s->nlines;
2951 off_t line_offset;
2953 line_offset = s->line_offsets[s->first_displayed_line - 1];
2954 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2955 return got_error_from_errno("fseek");
2957 werase(view->window);
2959 if (header) {
2960 if (asprintf(&line, "[%d/%d] %s",
2961 s->first_displayed_line - 1 + s->selected_line, nlines,
2962 header) == -1)
2963 return got_error_from_errno("asprintf");
2964 err = format_line(&wline, &width, line, view->ncols, 0);
2965 free(line);
2966 if (err)
2967 return err;
2969 if (view_needs_focus_indication(view))
2970 wstandout(view->window);
2971 waddwstr(view->window, wline);
2972 free(wline);
2973 wline = NULL;
2974 if (view_needs_focus_indication(view))
2975 wstandend(view->window);
2976 if (width <= view->ncols - 1)
2977 waddch(view->window, '\n');
2979 if (max_lines <= 1)
2980 return NULL;
2981 max_lines--;
2984 s->eof = 0;
2985 line = NULL;
2986 while (max_lines > 0 && nprinted < max_lines) {
2987 linelen = getline(&line, &linesize, s->f);
2988 if (linelen == -1) {
2989 if (feof(s->f)) {
2990 s->eof = 1;
2991 break;
2993 free(line);
2994 return got_ferror(s->f, GOT_ERR_IO);
2997 tc = match_color(&s->colors, line);
2998 if (tc)
2999 wattr_on(view->window,
3000 COLOR_PAIR(tc->colorpair), NULL);
3001 if (s->first_displayed_line + nprinted == s->matched_line &&
3002 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3003 err = add_matched_line(&width, line, view->ncols, 0,
3004 view->window, regmatch);
3005 if (err) {
3006 free(line);
3007 return err;
3009 } else {
3010 err = format_line(&wline, &width, line, view->ncols, 0);
3011 if (err) {
3012 free(line);
3013 return err;
3015 waddwstr(view->window, wline);
3016 free(wline);
3017 wline = NULL;
3019 if (tc)
3020 wattr_off(view->window,
3021 COLOR_PAIR(tc->colorpair), NULL);
3022 if (width <= view->ncols - 1)
3023 waddch(view->window, '\n');
3024 nprinted++;
3026 free(line);
3027 if (nprinted >= 1)
3028 s->last_displayed_line = s->first_displayed_line +
3029 (nprinted - 1);
3030 else
3031 s->last_displayed_line = s->first_displayed_line;
3033 view_vborder(view);
3035 if (s->eof) {
3036 while (nprinted < view->nlines) {
3037 waddch(view->window, '\n');
3038 nprinted++;
3041 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3042 if (err) {
3043 return err;
3046 wstandout(view->window);
3047 waddwstr(view->window, wline);
3048 free(wline);
3049 wline = NULL;
3050 wstandend(view->window);
3053 return NULL;
3056 static char *
3057 get_datestr(time_t *time, char *datebuf)
3059 struct tm mytm, *tm;
3060 char *p, *s;
3062 tm = gmtime_r(time, &mytm);
3063 if (tm == NULL)
3064 return NULL;
3065 s = asctime_r(tm, datebuf);
3066 if (s == NULL)
3067 return NULL;
3068 p = strchr(s, '\n');
3069 if (p)
3070 *p = '\0';
3071 return s;
3074 static const struct got_error *
3075 get_changed_paths(struct got_pathlist_head *paths,
3076 struct got_commit_object *commit, struct got_repository *repo)
3078 const struct got_error *err = NULL;
3079 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3080 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3081 struct got_object_qid *qid;
3083 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3084 if (qid != NULL) {
3085 struct got_commit_object *pcommit;
3086 err = got_object_open_as_commit(&pcommit, repo,
3087 qid->id);
3088 if (err)
3089 return err;
3091 tree_id1 = got_object_id_dup(
3092 got_object_commit_get_tree_id(pcommit));
3093 if (tree_id1 == NULL) {
3094 got_object_commit_close(pcommit);
3095 return got_error_from_errno("got_object_id_dup");
3097 got_object_commit_close(pcommit);
3101 if (tree_id1) {
3102 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3103 if (err)
3104 goto done;
3107 tree_id2 = got_object_commit_get_tree_id(commit);
3108 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3109 if (err)
3110 goto done;
3112 err = got_diff_tree(tree1, tree2, "", "", repo,
3113 got_diff_tree_collect_changed_paths, paths, 0);
3114 done:
3115 if (tree1)
3116 got_object_tree_close(tree1);
3117 if (tree2)
3118 got_object_tree_close(tree2);
3119 free(tree_id1);
3120 return err;
3123 static const struct got_error *
3124 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3126 off_t *p;
3128 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3129 if (p == NULL)
3130 return got_error_from_errno("reallocarray");
3131 *line_offsets = p;
3132 (*line_offsets)[*nlines] = off;
3133 (*nlines)++;
3134 return NULL;
3137 static const struct got_error *
3138 write_commit_info(off_t **line_offsets, size_t *nlines,
3139 struct got_object_id *commit_id, struct got_reflist_head *refs,
3140 struct got_repository *repo, FILE *outfile)
3142 const struct got_error *err = NULL;
3143 char datebuf[26], *datestr;
3144 struct got_commit_object *commit;
3145 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3146 time_t committer_time;
3147 const char *author, *committer;
3148 char *refs_str = NULL;
3149 struct got_pathlist_head changed_paths;
3150 struct got_pathlist_entry *pe;
3151 off_t outoff = 0;
3152 int n;
3154 TAILQ_INIT(&changed_paths);
3156 if (refs) {
3157 err = build_refs_str(&refs_str, refs, commit_id, repo);
3158 if (err)
3159 return err;
3162 err = got_object_open_as_commit(&commit, repo, commit_id);
3163 if (err)
3164 return err;
3166 err = got_object_id_str(&id_str, commit_id);
3167 if (err) {
3168 err = got_error_from_errno("got_object_id_str");
3169 goto done;
3172 err = add_line_offset(line_offsets, nlines, 0);
3173 if (err)
3174 goto done;
3176 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3177 refs_str ? refs_str : "", refs_str ? ")" : "");
3178 if (n < 0) {
3179 err = got_error_from_errno("fprintf");
3180 goto done;
3182 outoff += n;
3183 err = add_line_offset(line_offsets, nlines, outoff);
3184 if (err)
3185 goto done;
3187 n = fprintf(outfile, "from: %s\n",
3188 got_object_commit_get_author(commit));
3189 if (n < 0) {
3190 err = got_error_from_errno("fprintf");
3191 goto done;
3193 outoff += n;
3194 err = add_line_offset(line_offsets, nlines, outoff);
3195 if (err)
3196 goto done;
3198 committer_time = got_object_commit_get_committer_time(commit);
3199 datestr = get_datestr(&committer_time, datebuf);
3200 if (datestr) {
3201 n = fprintf(outfile, "date: %s UTC\n", datestr);
3202 if (n < 0) {
3203 err = got_error_from_errno("fprintf");
3204 goto done;
3206 outoff += n;
3207 err = add_line_offset(line_offsets, nlines, outoff);
3208 if (err)
3209 goto done;
3211 author = got_object_commit_get_author(commit);
3212 committer = got_object_commit_get_committer(commit);
3213 if (strcmp(author, committer) != 0) {
3214 n = fprintf(outfile, "via: %s\n", committer);
3215 if (n < 0) {
3216 err = got_error_from_errno("fprintf");
3217 goto done;
3219 outoff += n;
3220 err = add_line_offset(line_offsets, nlines, outoff);
3221 if (err)
3222 goto done;
3224 err = got_object_commit_get_logmsg(&logmsg, commit);
3225 if (err)
3226 goto done;
3227 s = logmsg;
3228 while ((line = strsep(&s, "\n")) != NULL) {
3229 n = fprintf(outfile, "%s\n", line);
3230 if (n < 0) {
3231 err = got_error_from_errno("fprintf");
3232 goto done;
3234 outoff += n;
3235 err = add_line_offset(line_offsets, nlines, outoff);
3236 if (err)
3237 goto done;
3240 err = get_changed_paths(&changed_paths, commit, repo);
3241 if (err)
3242 goto done;
3243 TAILQ_FOREACH(pe, &changed_paths, entry) {
3244 struct got_diff_changed_path *cp = pe->data;
3245 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3246 if (n < 0) {
3247 err = got_error_from_errno("fprintf");
3248 goto done;
3250 outoff += n;
3251 err = add_line_offset(line_offsets, nlines, outoff);
3252 if (err)
3253 goto done;
3254 free((char *)pe->path);
3255 free(pe->data);
3258 fputc('\n', outfile);
3259 outoff++;
3260 err = add_line_offset(line_offsets, nlines, outoff);
3261 done:
3262 got_pathlist_free(&changed_paths);
3263 free(id_str);
3264 free(logmsg);
3265 free(refs_str);
3266 got_object_commit_close(commit);
3267 if (err) {
3268 free(*line_offsets);
3269 *line_offsets = NULL;
3270 *nlines = 0;
3272 return err;
3275 static const struct got_error *
3276 create_diff(struct tog_diff_view_state *s)
3278 const struct got_error *err = NULL;
3279 FILE *f = NULL;
3280 int obj_type;
3282 free(s->line_offsets);
3283 s->line_offsets = malloc(sizeof(off_t));
3284 if (s->line_offsets == NULL)
3285 return got_error_from_errno("malloc");
3286 s->nlines = 0;
3288 f = got_opentemp();
3289 if (f == NULL) {
3290 err = got_error_from_errno("got_opentemp");
3291 goto done;
3293 if (s->f && fclose(s->f) == EOF) {
3294 err = got_error_from_errno("fclose");
3295 goto done;
3297 s->f = f;
3299 if (s->id1)
3300 err = got_object_get_type(&obj_type, s->repo, s->id1);
3301 else
3302 err = got_object_get_type(&obj_type, s->repo, s->id2);
3303 if (err)
3304 goto done;
3306 switch (obj_type) {
3307 case GOT_OBJ_TYPE_BLOB:
3308 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3309 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3310 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3311 break;
3312 case GOT_OBJ_TYPE_TREE:
3313 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3314 s->id1, s->id2, "", "", s->diff_context,
3315 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3316 break;
3317 case GOT_OBJ_TYPE_COMMIT: {
3318 const struct got_object_id_queue *parent_ids;
3319 struct got_object_qid *pid;
3320 struct got_commit_object *commit2;
3321 struct got_reflist_head *refs;
3323 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3324 if (err)
3325 goto done;
3326 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3327 /* Show commit info if we're diffing to a parent/root commit. */
3328 if (s->id1 == NULL) {
3329 err = write_commit_info(&s->line_offsets, &s->nlines,
3330 s->id2, refs, s->repo, s->f);
3331 if (err)
3332 goto done;
3333 } else {
3334 parent_ids = got_object_commit_get_parent_ids(commit2);
3335 STAILQ_FOREACH(pid, parent_ids, entry) {
3336 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3337 err = write_commit_info(
3338 &s->line_offsets, &s->nlines,
3339 s->id2, refs, s->repo, s->f);
3340 if (err)
3341 goto done;
3342 break;
3346 got_object_commit_close(commit2);
3348 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3349 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3350 s->force_text_diff, s->repo, s->f);
3351 break;
3353 default:
3354 err = got_error(GOT_ERR_OBJ_TYPE);
3355 break;
3357 if (err)
3358 goto done;
3359 done:
3360 if (s->f && fflush(s->f) != 0 && err == NULL)
3361 err = got_error_from_errno("fflush");
3362 return err;
3365 static void
3366 diff_view_indicate_progress(struct tog_view *view)
3368 mvwaddstr(view->window, 0, 0, "diffing...");
3369 update_panels();
3370 doupdate();
3373 static const struct got_error *
3374 search_start_diff_view(struct tog_view *view)
3376 struct tog_diff_view_state *s = &view->state.diff;
3378 s->matched_line = 0;
3379 return NULL;
3382 static const struct got_error *
3383 search_next_diff_view(struct tog_view *view)
3385 struct tog_diff_view_state *s = &view->state.diff;
3386 int lineno;
3387 char *line = NULL;
3388 size_t linesize = 0;
3389 ssize_t linelen;
3391 if (!view->searching) {
3392 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3393 return NULL;
3396 if (s->matched_line) {
3397 if (view->searching == TOG_SEARCH_FORWARD)
3398 lineno = s->matched_line + 1;
3399 else
3400 lineno = s->matched_line - 1;
3401 } else {
3402 if (view->searching == TOG_SEARCH_FORWARD)
3403 lineno = 1;
3404 else
3405 lineno = s->nlines;
3408 while (1) {
3409 off_t offset;
3411 if (lineno <= 0 || lineno > s->nlines) {
3412 if (s->matched_line == 0) {
3413 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3414 break;
3417 if (view->searching == TOG_SEARCH_FORWARD)
3418 lineno = 1;
3419 else
3420 lineno = s->nlines;
3423 offset = s->line_offsets[lineno - 1];
3424 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3425 free(line);
3426 return got_error_from_errno("fseeko");
3428 linelen = getline(&line, &linesize, s->f);
3429 if (linelen != -1 &&
3430 match_line(line, &view->regex, 1, &view->regmatch)) {
3431 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3432 s->matched_line = lineno;
3433 break;
3435 if (view->searching == TOG_SEARCH_FORWARD)
3436 lineno++;
3437 else
3438 lineno--;
3440 free(line);
3442 if (s->matched_line) {
3443 s->first_displayed_line = s->matched_line;
3444 s->selected_line = 1;
3447 return NULL;
3450 static const struct got_error *
3451 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3452 struct got_object_id *id2, const char *label1, const char *label2,
3453 int diff_context, int ignore_whitespace, int force_text_diff,
3454 struct tog_view *log_view, struct got_repository *repo)
3456 const struct got_error *err;
3457 struct tog_diff_view_state *s = &view->state.diff;
3459 if (id1 != NULL && id2 != NULL) {
3460 int type1, type2;
3461 err = got_object_get_type(&type1, repo, id1);
3462 if (err)
3463 return err;
3464 err = got_object_get_type(&type2, repo, id2);
3465 if (err)
3466 return err;
3468 if (type1 != type2)
3469 return got_error(GOT_ERR_OBJ_TYPE);
3471 s->first_displayed_line = 1;
3472 s->last_displayed_line = view->nlines;
3473 s->selected_line = 1;
3474 s->repo = repo;
3475 s->id1 = id1;
3476 s->id2 = id2;
3477 s->label1 = label1;
3478 s->label2 = label2;
3480 if (id1) {
3481 s->id1 = got_object_id_dup(id1);
3482 if (s->id1 == NULL)
3483 return got_error_from_errno("got_object_id_dup");
3484 } else
3485 s->id1 = NULL;
3487 s->id2 = got_object_id_dup(id2);
3488 if (s->id2 == NULL) {
3489 free(s->id1);
3490 s->id1 = NULL;
3491 return got_error_from_errno("got_object_id_dup");
3493 s->f = NULL;
3494 s->first_displayed_line = 1;
3495 s->last_displayed_line = view->nlines;
3496 s->diff_context = diff_context;
3497 s->ignore_whitespace = ignore_whitespace;
3498 s->force_text_diff = force_text_diff;
3499 s->log_view = log_view;
3500 s->repo = repo;
3502 STAILQ_INIT(&s->colors);
3503 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3504 err = add_color(&s->colors,
3505 "^-", TOG_COLOR_DIFF_MINUS,
3506 get_color_value("TOG_COLOR_DIFF_MINUS"));
3507 if (err)
3508 return err;
3509 err = add_color(&s->colors, "^\\+",
3510 TOG_COLOR_DIFF_PLUS,
3511 get_color_value("TOG_COLOR_DIFF_PLUS"));
3512 if (err) {
3513 free_colors(&s->colors);
3514 return err;
3516 err = add_color(&s->colors,
3517 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3518 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3519 if (err) {
3520 free_colors(&s->colors);
3521 return err;
3524 err = add_color(&s->colors,
3525 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3526 TOG_COLOR_DIFF_META,
3527 get_color_value("TOG_COLOR_DIFF_META"));
3528 if (err) {
3529 free_colors(&s->colors);
3530 return err;
3533 err = add_color(&s->colors,
3534 "^(from|via): ", TOG_COLOR_AUTHOR,
3535 get_color_value("TOG_COLOR_AUTHOR"));
3536 if (err) {
3537 free_colors(&s->colors);
3538 return err;
3541 err = add_color(&s->colors,
3542 "^date: ", TOG_COLOR_DATE,
3543 get_color_value("TOG_COLOR_DATE"));
3544 if (err) {
3545 free_colors(&s->colors);
3546 return err;
3550 if (log_view && view_is_splitscreen(view))
3551 show_log_view(log_view); /* draw vborder */
3552 diff_view_indicate_progress(view);
3554 s->line_offsets = NULL;
3555 s->nlines = 0;
3556 err = create_diff(s);
3557 if (err) {
3558 free(s->id1);
3559 s->id1 = NULL;
3560 free(s->id2);
3561 s->id2 = NULL;
3562 free_colors(&s->colors);
3563 return err;
3566 view->show = show_diff_view;
3567 view->input = input_diff_view;
3568 view->close = close_diff_view;
3569 view->search_start = search_start_diff_view;
3570 view->search_next = search_next_diff_view;
3572 return NULL;
3575 static const struct got_error *
3576 close_diff_view(struct tog_view *view)
3578 const struct got_error *err = NULL;
3579 struct tog_diff_view_state *s = &view->state.diff;
3581 free(s->id1);
3582 s->id1 = NULL;
3583 free(s->id2);
3584 s->id2 = NULL;
3585 if (s->f && fclose(s->f) == EOF)
3586 err = got_error_from_errno("fclose");
3587 free_colors(&s->colors);
3588 free(s->line_offsets);
3589 s->line_offsets = NULL;
3590 s->nlines = 0;
3591 return err;
3594 static const struct got_error *
3595 show_diff_view(struct tog_view *view)
3597 const struct got_error *err;
3598 struct tog_diff_view_state *s = &view->state.diff;
3599 char *id_str1 = NULL, *id_str2, *header;
3600 const char *label1, *label2;
3602 if (s->id1) {
3603 err = got_object_id_str(&id_str1, s->id1);
3604 if (err)
3605 return err;
3606 label1 = s->label1 ? : id_str1;
3607 } else
3608 label1 = "/dev/null";
3610 err = got_object_id_str(&id_str2, s->id2);
3611 if (err)
3612 return err;
3613 label2 = s->label2 ? : id_str2;
3615 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3616 err = got_error_from_errno("asprintf");
3617 free(id_str1);
3618 free(id_str2);
3619 return err;
3621 free(id_str1);
3622 free(id_str2);
3624 err = draw_file(view, header);
3625 free(header);
3626 return err;
3629 static const struct got_error *
3630 set_selected_commit(struct tog_diff_view_state *s,
3631 struct commit_queue_entry *entry)
3633 const struct got_error *err;
3634 const struct got_object_id_queue *parent_ids;
3635 struct got_commit_object *selected_commit;
3636 struct got_object_qid *pid;
3638 free(s->id2);
3639 s->id2 = got_object_id_dup(entry->id);
3640 if (s->id2 == NULL)
3641 return got_error_from_errno("got_object_id_dup");
3643 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3644 if (err)
3645 return err;
3646 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3647 free(s->id1);
3648 pid = STAILQ_FIRST(parent_ids);
3649 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3650 got_object_commit_close(selected_commit);
3651 return NULL;
3654 static const struct got_error *
3655 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3657 const struct got_error *err = NULL;
3658 struct tog_diff_view_state *s = &view->state.diff;
3659 struct tog_log_view_state *ls;
3660 struct commit_queue_entry *old_selected_entry;
3661 char *line = NULL;
3662 size_t linesize = 0;
3663 ssize_t linelen;
3664 int i;
3666 switch (ch) {
3667 case 'a':
3668 case 'w':
3669 if (ch == 'a')
3670 s->force_text_diff = !s->force_text_diff;
3671 if (ch == 'w')
3672 s->ignore_whitespace = !s->ignore_whitespace;
3673 wclear(view->window);
3674 s->first_displayed_line = 1;
3675 s->last_displayed_line = view->nlines;
3676 diff_view_indicate_progress(view);
3677 err = create_diff(s);
3678 break;
3679 case 'g':
3680 case KEY_HOME:
3681 s->first_displayed_line = 1;
3682 break;
3683 case 'G':
3684 case KEY_END:
3685 if (s->eof)
3686 break;
3688 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3689 s->eof = 1;
3690 break;
3691 case 'k':
3692 case KEY_UP:
3693 if (s->first_displayed_line > 1)
3694 s->first_displayed_line--;
3695 break;
3696 case KEY_PPAGE:
3697 case CTRL('b'):
3698 if (s->first_displayed_line == 1)
3699 break;
3700 i = 0;
3701 while (i++ < view->nlines - 1 &&
3702 s->first_displayed_line > 1)
3703 s->first_displayed_line--;
3704 break;
3705 case 'j':
3706 case KEY_DOWN:
3707 if (!s->eof)
3708 s->first_displayed_line++;
3709 break;
3710 case KEY_NPAGE:
3711 case CTRL('f'):
3712 case ' ':
3713 if (s->eof)
3714 break;
3715 i = 0;
3716 while (!s->eof && i++ < view->nlines - 1) {
3717 linelen = getline(&line, &linesize, s->f);
3718 s->first_displayed_line++;
3719 if (linelen == -1) {
3720 if (feof(s->f)) {
3721 s->eof = 1;
3722 } else
3723 err = got_ferror(s->f, GOT_ERR_IO);
3724 break;
3727 free(line);
3728 break;
3729 case '[':
3730 if (s->diff_context > 0) {
3731 s->diff_context--;
3732 diff_view_indicate_progress(view);
3733 err = create_diff(s);
3734 if (s->first_displayed_line + view->nlines - 1 >
3735 s->nlines) {
3736 s->first_displayed_line = 1;
3737 s->last_displayed_line = view->nlines;
3740 break;
3741 case ']':
3742 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3743 s->diff_context++;
3744 diff_view_indicate_progress(view);
3745 err = create_diff(s);
3747 break;
3748 case '<':
3749 case ',':
3750 if (s->log_view == NULL)
3751 break;
3752 ls = &s->log_view->state.log;
3753 old_selected_entry = ls->selected_entry;
3755 err = input_log_view(NULL, s->log_view, KEY_UP);
3756 if (err)
3757 break;
3759 if (old_selected_entry == ls->selected_entry)
3760 break;
3762 err = set_selected_commit(s, ls->selected_entry);
3763 if (err)
3764 break;
3766 s->first_displayed_line = 1;
3767 s->last_displayed_line = view->nlines;
3769 diff_view_indicate_progress(view);
3770 err = create_diff(s);
3771 break;
3772 case '>':
3773 case '.':
3774 if (s->log_view == NULL)
3775 break;
3776 ls = &s->log_view->state.log;
3777 old_selected_entry = ls->selected_entry;
3779 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3780 if (err)
3781 break;
3783 if (old_selected_entry == ls->selected_entry)
3784 break;
3786 err = set_selected_commit(s, ls->selected_entry);
3787 if (err)
3788 break;
3790 s->first_displayed_line = 1;
3791 s->last_displayed_line = view->nlines;
3793 diff_view_indicate_progress(view);
3794 err = create_diff(s);
3795 break;
3796 default:
3797 break;
3800 return err;
3803 static const struct got_error *
3804 cmd_diff(int argc, char *argv[])
3806 const struct got_error *error = NULL;
3807 struct got_repository *repo = NULL;
3808 struct got_worktree *worktree = NULL;
3809 struct got_object_id *id1 = NULL, *id2 = NULL;
3810 char *repo_path = NULL, *cwd = NULL;
3811 char *id_str1 = NULL, *id_str2 = NULL;
3812 char *label1 = NULL, *label2 = NULL;
3813 int diff_context = 3, ignore_whitespace = 0;
3814 int ch, force_text_diff = 0;
3815 const char *errstr;
3816 struct tog_view *view;
3818 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3819 switch (ch) {
3820 case 'a':
3821 force_text_diff = 1;
3822 break;
3823 case 'C':
3824 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3825 &errstr);
3826 if (errstr != NULL)
3827 err(1, "-C option %s", errstr);
3828 break;
3829 case 'r':
3830 repo_path = realpath(optarg, NULL);
3831 if (repo_path == NULL)
3832 return got_error_from_errno2("realpath",
3833 optarg);
3834 got_path_strip_trailing_slashes(repo_path);
3835 break;
3836 case 'w':
3837 ignore_whitespace = 1;
3838 break;
3839 default:
3840 usage_diff();
3841 /* NOTREACHED */
3845 argc -= optind;
3846 argv += optind;
3848 if (argc == 0) {
3849 usage_diff(); /* TODO show local worktree changes */
3850 } else if (argc == 2) {
3851 id_str1 = argv[0];
3852 id_str2 = argv[1];
3853 } else
3854 usage_diff();
3856 if (repo_path == NULL) {
3857 cwd = getcwd(NULL, 0);
3858 if (cwd == NULL)
3859 return got_error_from_errno("getcwd");
3860 error = got_worktree_open(&worktree, cwd);
3861 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3862 goto done;
3863 if (worktree)
3864 repo_path =
3865 strdup(got_worktree_get_repo_path(worktree));
3866 else
3867 repo_path = strdup(cwd);
3868 if (repo_path == NULL) {
3869 error = got_error_from_errno("strdup");
3870 goto done;
3874 error = got_repo_open(&repo, repo_path, NULL);
3875 if (error)
3876 goto done;
3878 init_curses();
3880 error = apply_unveil(got_repo_get_path(repo), NULL);
3881 if (error)
3882 goto done;
3884 error = tog_load_refs(repo);
3885 if (error)
3886 goto done;
3888 error = got_repo_match_object_id(&id1, &label1, id_str1,
3889 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3890 if (error)
3891 goto done;
3893 error = got_repo_match_object_id(&id2, &label2, id_str2,
3894 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3895 if (error)
3896 goto done;
3898 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3899 if (view == NULL) {
3900 error = got_error_from_errno("view_open");
3901 goto done;
3903 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3904 ignore_whitespace, force_text_diff, NULL, repo);
3905 if (error)
3906 goto done;
3907 error = view_loop(view);
3908 done:
3909 free(label1);
3910 free(label2);
3911 free(repo_path);
3912 free(cwd);
3913 if (repo) {
3914 const struct got_error *close_err = got_repo_close(repo);
3915 if (error == NULL)
3916 error = close_err;
3918 if (worktree)
3919 got_worktree_close(worktree);
3920 tog_free_refs();
3921 return error;
3924 __dead static void
3925 usage_blame(void)
3927 endwin();
3928 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3929 getprogname());
3930 exit(1);
3933 struct tog_blame_line {
3934 int annotated;
3935 struct got_object_id *id;
3938 static const struct got_error *
3939 draw_blame(struct tog_view *view)
3941 struct tog_blame_view_state *s = &view->state.blame;
3942 struct tog_blame *blame = &s->blame;
3943 regmatch_t *regmatch = &view->regmatch;
3944 const struct got_error *err;
3945 int lineno = 0, nprinted = 0;
3946 char *line = NULL;
3947 size_t linesize = 0;
3948 ssize_t linelen;
3949 wchar_t *wline;
3950 int width;
3951 struct tog_blame_line *blame_line;
3952 struct got_object_id *prev_id = NULL;
3953 char *id_str;
3954 struct tog_color *tc;
3956 err = got_object_id_str(&id_str, s->blamed_commit->id);
3957 if (err)
3958 return err;
3960 rewind(blame->f);
3961 werase(view->window);
3963 if (asprintf(&line, "commit %s", id_str) == -1) {
3964 err = got_error_from_errno("asprintf");
3965 free(id_str);
3966 return err;
3969 err = format_line(&wline, &width, line, view->ncols, 0);
3970 free(line);
3971 line = NULL;
3972 if (err)
3973 return err;
3974 if (view_needs_focus_indication(view))
3975 wstandout(view->window);
3976 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3977 if (tc)
3978 wattr_on(view->window,
3979 COLOR_PAIR(tc->colorpair), NULL);
3980 waddwstr(view->window, wline);
3981 if (tc)
3982 wattr_off(view->window,
3983 COLOR_PAIR(tc->colorpair), NULL);
3984 if (view_needs_focus_indication(view))
3985 wstandend(view->window);
3986 free(wline);
3987 wline = NULL;
3988 if (width < view->ncols - 1)
3989 waddch(view->window, '\n');
3991 if (asprintf(&line, "[%d/%d] %s%s",
3992 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3993 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3994 free(id_str);
3995 return got_error_from_errno("asprintf");
3997 free(id_str);
3998 err = format_line(&wline, &width, line, view->ncols, 0);
3999 free(line);
4000 line = NULL;
4001 if (err)
4002 return err;
4003 waddwstr(view->window, wline);
4004 free(wline);
4005 wline = NULL;
4006 if (width < view->ncols - 1)
4007 waddch(view->window, '\n');
4009 s->eof = 0;
4010 while (nprinted < view->nlines - 2) {
4011 linelen = getline(&line, &linesize, blame->f);
4012 if (linelen == -1) {
4013 if (feof(blame->f)) {
4014 s->eof = 1;
4015 break;
4017 free(line);
4018 return got_ferror(blame->f, GOT_ERR_IO);
4020 if (++lineno < s->first_displayed_line)
4021 continue;
4023 if (view->focussed && nprinted == s->selected_line - 1)
4024 wstandout(view->window);
4026 if (blame->nlines > 0) {
4027 blame_line = &blame->lines[lineno - 1];
4028 if (blame_line->annotated && prev_id &&
4029 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4030 !(view->focussed &&
4031 nprinted == s->selected_line - 1)) {
4032 waddstr(view->window, " ");
4033 } else if (blame_line->annotated) {
4034 char *id_str;
4035 err = got_object_id_str(&id_str, blame_line->id);
4036 if (err) {
4037 free(line);
4038 return err;
4040 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4041 if (tc)
4042 wattr_on(view->window,
4043 COLOR_PAIR(tc->colorpair), NULL);
4044 wprintw(view->window, "%.8s", id_str);
4045 if (tc)
4046 wattr_off(view->window,
4047 COLOR_PAIR(tc->colorpair), NULL);
4048 free(id_str);
4049 prev_id = blame_line->id;
4050 } else {
4051 waddstr(view->window, "........");
4052 prev_id = NULL;
4054 } else {
4055 waddstr(view->window, "........");
4056 prev_id = NULL;
4059 if (view->focussed && nprinted == s->selected_line - 1)
4060 wstandend(view->window);
4061 waddstr(view->window, " ");
4063 if (view->ncols <= 9) {
4064 width = 9;
4065 wline = wcsdup(L"");
4066 if (wline == NULL) {
4067 err = got_error_from_errno("wcsdup");
4068 free(line);
4069 return err;
4071 } else if (s->first_displayed_line + nprinted ==
4072 s->matched_line &&
4073 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4074 err = add_matched_line(&width, line, view->ncols - 9, 9,
4075 view->window, regmatch);
4076 if (err) {
4077 free(line);
4078 return err;
4080 width += 9;
4081 } else {
4082 err = format_line(&wline, &width, line,
4083 view->ncols - 9, 9);
4084 waddwstr(view->window, wline);
4085 free(wline);
4086 wline = NULL;
4087 width += 9;
4090 if (width <= view->ncols - 1)
4091 waddch(view->window, '\n');
4092 if (++nprinted == 1)
4093 s->first_displayed_line = lineno;
4095 free(line);
4096 s->last_displayed_line = lineno;
4098 view_vborder(view);
4100 return NULL;
4103 static const struct got_error *
4104 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4106 const struct got_error *err = NULL;
4107 struct tog_blame_cb_args *a = arg;
4108 struct tog_blame_line *line;
4109 int errcode;
4111 if (nlines != a->nlines ||
4112 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4113 return got_error(GOT_ERR_RANGE);
4115 errcode = pthread_mutex_lock(&tog_mutex);
4116 if (errcode)
4117 return got_error_set_errno(errcode, "pthread_mutex_lock");
4119 if (*a->quit) { /* user has quit the blame view */
4120 err = got_error(GOT_ERR_ITER_COMPLETED);
4121 goto done;
4124 if (lineno == -1)
4125 goto done; /* no change in this commit */
4127 line = &a->lines[lineno - 1];
4128 if (line->annotated)
4129 goto done;
4131 line->id = got_object_id_dup(id);
4132 if (line->id == NULL) {
4133 err = got_error_from_errno("got_object_id_dup");
4134 goto done;
4136 line->annotated = 1;
4137 done:
4138 errcode = pthread_mutex_unlock(&tog_mutex);
4139 if (errcode)
4140 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4141 return err;
4144 static void *
4145 blame_thread(void *arg)
4147 const struct got_error *err, *close_err;
4148 struct tog_blame_thread_args *ta = arg;
4149 struct tog_blame_cb_args *a = ta->cb_args;
4150 int errcode;
4152 err = block_signals_used_by_main_thread();
4153 if (err)
4154 return (void *)err;
4156 err = got_blame(ta->path, a->commit_id, ta->repo,
4157 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4158 if (err && err->code == GOT_ERR_CANCELLED)
4159 err = NULL;
4161 errcode = pthread_mutex_lock(&tog_mutex);
4162 if (errcode)
4163 return (void *)got_error_set_errno(errcode,
4164 "pthread_mutex_lock");
4166 close_err = got_repo_close(ta->repo);
4167 if (err == NULL)
4168 err = close_err;
4169 ta->repo = NULL;
4170 *ta->complete = 1;
4172 errcode = pthread_mutex_unlock(&tog_mutex);
4173 if (errcode && err == NULL)
4174 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4176 return (void *)err;
4179 static struct got_object_id *
4180 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4181 int first_displayed_line, int selected_line)
4183 struct tog_blame_line *line;
4185 if (nlines <= 0)
4186 return NULL;
4188 line = &lines[first_displayed_line - 1 + selected_line - 1];
4189 if (!line->annotated)
4190 return NULL;
4192 return line->id;
4195 static const struct got_error *
4196 stop_blame(struct tog_blame *blame)
4198 const struct got_error *err = NULL;
4199 int i;
4201 if (blame->thread) {
4202 int errcode;
4203 errcode = pthread_mutex_unlock(&tog_mutex);
4204 if (errcode)
4205 return got_error_set_errno(errcode,
4206 "pthread_mutex_unlock");
4207 errcode = pthread_join(blame->thread, (void **)&err);
4208 if (errcode)
4209 return got_error_set_errno(errcode, "pthread_join");
4210 errcode = pthread_mutex_lock(&tog_mutex);
4211 if (errcode)
4212 return got_error_set_errno(errcode,
4213 "pthread_mutex_lock");
4214 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4215 err = NULL;
4216 blame->thread = NULL;
4218 if (blame->thread_args.repo) {
4219 const struct got_error *close_err;
4220 close_err = got_repo_close(blame->thread_args.repo);
4221 if (err == NULL)
4222 err = close_err;
4223 blame->thread_args.repo = NULL;
4225 if (blame->f) {
4226 if (fclose(blame->f) == EOF && err == NULL)
4227 err = got_error_from_errno("fclose");
4228 blame->f = NULL;
4230 if (blame->lines) {
4231 for (i = 0; i < blame->nlines; i++)
4232 free(blame->lines[i].id);
4233 free(blame->lines);
4234 blame->lines = NULL;
4236 free(blame->cb_args.commit_id);
4237 blame->cb_args.commit_id = NULL;
4239 return err;
4242 static const struct got_error *
4243 cancel_blame_view(void *arg)
4245 const struct got_error *err = NULL;
4246 int *done = arg;
4247 int errcode;
4249 errcode = pthread_mutex_lock(&tog_mutex);
4250 if (errcode)
4251 return got_error_set_errno(errcode,
4252 "pthread_mutex_unlock");
4254 if (*done)
4255 err = got_error(GOT_ERR_CANCELLED);
4257 errcode = pthread_mutex_unlock(&tog_mutex);
4258 if (errcode)
4259 return got_error_set_errno(errcode,
4260 "pthread_mutex_lock");
4262 return err;
4265 static const struct got_error *
4266 run_blame(struct tog_view *view)
4268 struct tog_blame_view_state *s = &view->state.blame;
4269 struct tog_blame *blame = &s->blame;
4270 const struct got_error *err = NULL;
4271 struct got_blob_object *blob = NULL;
4272 struct got_repository *thread_repo = NULL;
4273 struct got_object_id *obj_id = NULL;
4274 int obj_type;
4276 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4277 s->path);
4278 if (err)
4279 return err;
4281 err = got_object_get_type(&obj_type, s->repo, obj_id);
4282 if (err)
4283 goto done;
4285 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4286 err = got_error(GOT_ERR_OBJ_TYPE);
4287 goto done;
4290 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4291 if (err)
4292 goto done;
4293 blame->f = got_opentemp();
4294 if (blame->f == NULL) {
4295 err = got_error_from_errno("got_opentemp");
4296 goto done;
4298 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4299 &blame->line_offsets, blame->f, blob);
4300 if (err)
4301 goto done;
4302 if (blame->nlines == 0) {
4303 s->blame_complete = 1;
4304 goto done;
4307 /* Don't include \n at EOF in the blame line count. */
4308 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4309 blame->nlines--;
4311 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4312 if (blame->lines == NULL) {
4313 err = got_error_from_errno("calloc");
4314 goto done;
4317 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4318 if (err)
4319 goto done;
4321 blame->cb_args.view = view;
4322 blame->cb_args.lines = blame->lines;
4323 blame->cb_args.nlines = blame->nlines;
4324 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4325 if (blame->cb_args.commit_id == NULL) {
4326 err = got_error_from_errno("got_object_id_dup");
4327 goto done;
4329 blame->cb_args.quit = &s->done;
4331 blame->thread_args.path = s->path;
4332 blame->thread_args.repo = thread_repo;
4333 blame->thread_args.cb_args = &blame->cb_args;
4334 blame->thread_args.complete = &s->blame_complete;
4335 blame->thread_args.cancel_cb = cancel_blame_view;
4336 blame->thread_args.cancel_arg = &s->done;
4337 s->blame_complete = 0;
4339 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4340 s->first_displayed_line = 1;
4341 s->last_displayed_line = view->nlines;
4342 s->selected_line = 1;
4345 done:
4346 if (blob)
4347 got_object_blob_close(blob);
4348 free(obj_id);
4349 if (err)
4350 stop_blame(blame);
4351 return err;
4354 static const struct got_error *
4355 open_blame_view(struct tog_view *view, char *path,
4356 struct got_object_id *commit_id, struct got_repository *repo)
4358 const struct got_error *err = NULL;
4359 struct tog_blame_view_state *s = &view->state.blame;
4361 STAILQ_INIT(&s->blamed_commits);
4363 s->path = strdup(path);
4364 if (s->path == NULL)
4365 return got_error_from_errno("strdup");
4367 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4368 if (err) {
4369 free(s->path);
4370 return err;
4373 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4374 s->first_displayed_line = 1;
4375 s->last_displayed_line = view->nlines;
4376 s->selected_line = 1;
4377 s->blame_complete = 0;
4378 s->repo = repo;
4379 s->commit_id = commit_id;
4380 memset(&s->blame, 0, sizeof(s->blame));
4382 STAILQ_INIT(&s->colors);
4383 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4384 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4385 get_color_value("TOG_COLOR_COMMIT"));
4386 if (err)
4387 return err;
4390 view->show = show_blame_view;
4391 view->input = input_blame_view;
4392 view->close = close_blame_view;
4393 view->search_start = search_start_blame_view;
4394 view->search_next = search_next_blame_view;
4396 return run_blame(view);
4399 static const struct got_error *
4400 close_blame_view(struct tog_view *view)
4402 const struct got_error *err = NULL;
4403 struct tog_blame_view_state *s = &view->state.blame;
4405 if (s->blame.thread)
4406 err = stop_blame(&s->blame);
4408 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4409 struct got_object_qid *blamed_commit;
4410 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4411 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4412 got_object_qid_free(blamed_commit);
4415 free(s->path);
4416 free_colors(&s->colors);
4418 return err;
4421 static const struct got_error *
4422 search_start_blame_view(struct tog_view *view)
4424 struct tog_blame_view_state *s = &view->state.blame;
4426 s->matched_line = 0;
4427 return NULL;
4430 static const struct got_error *
4431 search_next_blame_view(struct tog_view *view)
4433 struct tog_blame_view_state *s = &view->state.blame;
4434 int lineno;
4435 char *line = NULL;
4436 size_t linesize = 0;
4437 ssize_t linelen;
4439 if (!view->searching) {
4440 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4441 return NULL;
4444 if (s->matched_line) {
4445 if (view->searching == TOG_SEARCH_FORWARD)
4446 lineno = s->matched_line + 1;
4447 else
4448 lineno = s->matched_line - 1;
4449 } else {
4450 if (view->searching == TOG_SEARCH_FORWARD)
4451 lineno = 1;
4452 else
4453 lineno = s->blame.nlines;
4456 while (1) {
4457 off_t offset;
4459 if (lineno <= 0 || lineno > s->blame.nlines) {
4460 if (s->matched_line == 0) {
4461 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4462 break;
4465 if (view->searching == TOG_SEARCH_FORWARD)
4466 lineno = 1;
4467 else
4468 lineno = s->blame.nlines;
4471 offset = s->blame.line_offsets[lineno - 1];
4472 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4473 free(line);
4474 return got_error_from_errno("fseeko");
4476 linelen = getline(&line, &linesize, s->blame.f);
4477 if (linelen != -1 &&
4478 match_line(line, &view->regex, 1, &view->regmatch)) {
4479 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4480 s->matched_line = lineno;
4481 break;
4483 if (view->searching == TOG_SEARCH_FORWARD)
4484 lineno++;
4485 else
4486 lineno--;
4488 free(line);
4490 if (s->matched_line) {
4491 s->first_displayed_line = s->matched_line;
4492 s->selected_line = 1;
4495 return NULL;
4498 static const struct got_error *
4499 show_blame_view(struct tog_view *view)
4501 const struct got_error *err = NULL;
4502 struct tog_blame_view_state *s = &view->state.blame;
4503 int errcode;
4505 if (s->blame.thread == NULL && !s->blame_complete) {
4506 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4507 &s->blame.thread_args);
4508 if (errcode)
4509 return got_error_set_errno(errcode, "pthread_create");
4511 halfdelay(1); /* fast refresh while annotating */
4514 if (s->blame_complete)
4515 halfdelay(10); /* disable fast refresh */
4517 err = draw_blame(view);
4519 view_vborder(view);
4520 return err;
4523 static const struct got_error *
4524 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4526 const struct got_error *err = NULL, *thread_err = NULL;
4527 struct tog_view *diff_view;
4528 struct tog_blame_view_state *s = &view->state.blame;
4529 int begin_x = 0;
4531 switch (ch) {
4532 case 'q':
4533 s->done = 1;
4534 break;
4535 case 'k':
4536 case KEY_UP:
4537 if (s->selected_line > 1)
4538 s->selected_line--;
4539 else if (s->selected_line == 1 &&
4540 s->first_displayed_line > 1)
4541 s->first_displayed_line--;
4542 break;
4543 case KEY_PPAGE:
4544 case CTRL('b'):
4545 if (s->first_displayed_line == 1) {
4546 s->selected_line = 1;
4547 break;
4549 if (s->first_displayed_line > view->nlines - 2)
4550 s->first_displayed_line -=
4551 (view->nlines - 2);
4552 else
4553 s->first_displayed_line = 1;
4554 break;
4555 case 'j':
4556 case KEY_DOWN:
4557 if (s->selected_line < view->nlines - 2 &&
4558 s->first_displayed_line +
4559 s->selected_line <= s->blame.nlines)
4560 s->selected_line++;
4561 else if (s->last_displayed_line <
4562 s->blame.nlines)
4563 s->first_displayed_line++;
4564 break;
4565 case 'b':
4566 case 'p': {
4567 struct got_object_id *id = NULL;
4568 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4569 s->first_displayed_line, s->selected_line);
4570 if (id == NULL)
4571 break;
4572 if (ch == 'p') {
4573 struct got_commit_object *commit;
4574 struct got_object_qid *pid;
4575 struct got_object_id *blob_id = NULL;
4576 int obj_type;
4577 err = got_object_open_as_commit(&commit,
4578 s->repo, id);
4579 if (err)
4580 break;
4581 pid = STAILQ_FIRST(
4582 got_object_commit_get_parent_ids(commit));
4583 if (pid == NULL) {
4584 got_object_commit_close(commit);
4585 break;
4587 /* Check if path history ends here. */
4588 err = got_object_id_by_path(&blob_id, s->repo,
4589 pid->id, s->path);
4590 if (err) {
4591 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4592 err = NULL;
4593 got_object_commit_close(commit);
4594 break;
4596 err = got_object_get_type(&obj_type, s->repo,
4597 blob_id);
4598 free(blob_id);
4599 /* Can't blame non-blob type objects. */
4600 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4601 got_object_commit_close(commit);
4602 break;
4604 err = got_object_qid_alloc(&s->blamed_commit,
4605 pid->id);
4606 got_object_commit_close(commit);
4607 } else {
4608 if (got_object_id_cmp(id,
4609 s->blamed_commit->id) == 0)
4610 break;
4611 err = got_object_qid_alloc(&s->blamed_commit,
4612 id);
4614 if (err)
4615 break;
4616 s->done = 1;
4617 thread_err = stop_blame(&s->blame);
4618 s->done = 0;
4619 if (thread_err)
4620 break;
4621 STAILQ_INSERT_HEAD(&s->blamed_commits,
4622 s->blamed_commit, entry);
4623 err = run_blame(view);
4624 if (err)
4625 break;
4626 break;
4628 case 'B': {
4629 struct got_object_qid *first;
4630 first = STAILQ_FIRST(&s->blamed_commits);
4631 if (!got_object_id_cmp(first->id, s->commit_id))
4632 break;
4633 s->done = 1;
4634 thread_err = stop_blame(&s->blame);
4635 s->done = 0;
4636 if (thread_err)
4637 break;
4638 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4639 got_object_qid_free(s->blamed_commit);
4640 s->blamed_commit =
4641 STAILQ_FIRST(&s->blamed_commits);
4642 err = run_blame(view);
4643 if (err)
4644 break;
4645 break;
4647 case KEY_ENTER:
4648 case '\r': {
4649 struct got_object_id *id = NULL;
4650 struct got_object_qid *pid;
4651 struct got_commit_object *commit = NULL;
4652 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4653 s->first_displayed_line, s->selected_line);
4654 if (id == NULL)
4655 break;
4656 err = got_object_open_as_commit(&commit, s->repo, id);
4657 if (err)
4658 break;
4659 pid = STAILQ_FIRST(
4660 got_object_commit_get_parent_ids(commit));
4661 if (view_is_parent_view(view))
4662 begin_x = view_split_begin_x(view->begin_x);
4663 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4664 if (diff_view == NULL) {
4665 got_object_commit_close(commit);
4666 err = got_error_from_errno("view_open");
4667 break;
4669 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4670 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4671 got_object_commit_close(commit);
4672 if (err) {
4673 view_close(diff_view);
4674 break;
4676 view->focussed = 0;
4677 diff_view->focussed = 1;
4678 if (view_is_parent_view(view)) {
4679 err = view_close_child(view);
4680 if (err)
4681 break;
4682 view_set_child(view, diff_view);
4683 view->focus_child = 1;
4684 } else
4685 *new_view = diff_view;
4686 if (err)
4687 break;
4688 break;
4690 case KEY_NPAGE:
4691 case CTRL('f'):
4692 case ' ':
4693 if (s->last_displayed_line >= s->blame.nlines &&
4694 s->selected_line >= MIN(s->blame.nlines,
4695 view->nlines - 2)) {
4696 break;
4698 if (s->last_displayed_line >= s->blame.nlines &&
4699 s->selected_line < view->nlines - 2) {
4700 s->selected_line = MIN(s->blame.nlines,
4701 view->nlines - 2);
4702 break;
4704 if (s->last_displayed_line + view->nlines - 2
4705 <= s->blame.nlines)
4706 s->first_displayed_line +=
4707 view->nlines - 2;
4708 else
4709 s->first_displayed_line =
4710 s->blame.nlines -
4711 (view->nlines - 3);
4712 break;
4713 case KEY_RESIZE:
4714 if (s->selected_line > view->nlines - 2) {
4715 s->selected_line = MIN(s->blame.nlines,
4716 view->nlines - 2);
4718 break;
4719 default:
4720 break;
4722 return thread_err ? thread_err : err;
4725 static const struct got_error *
4726 cmd_blame(int argc, char *argv[])
4728 const struct got_error *error;
4729 struct got_repository *repo = NULL;
4730 struct got_worktree *worktree = NULL;
4731 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4732 char *link_target = NULL;
4733 struct got_object_id *commit_id = NULL;
4734 char *commit_id_str = NULL;
4735 int ch;
4736 struct tog_view *view;
4738 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4739 switch (ch) {
4740 case 'c':
4741 commit_id_str = optarg;
4742 break;
4743 case 'r':
4744 repo_path = realpath(optarg, NULL);
4745 if (repo_path == NULL)
4746 return got_error_from_errno2("realpath",
4747 optarg);
4748 break;
4749 default:
4750 usage_blame();
4751 /* NOTREACHED */
4755 argc -= optind;
4756 argv += optind;
4758 if (argc != 1)
4759 usage_blame();
4761 if (repo_path == NULL) {
4762 cwd = getcwd(NULL, 0);
4763 if (cwd == NULL)
4764 return got_error_from_errno("getcwd");
4765 error = got_worktree_open(&worktree, cwd);
4766 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4767 goto done;
4768 if (worktree)
4769 repo_path =
4770 strdup(got_worktree_get_repo_path(worktree));
4771 else
4772 repo_path = strdup(cwd);
4773 if (repo_path == NULL) {
4774 error = got_error_from_errno("strdup");
4775 goto done;
4779 error = got_repo_open(&repo, repo_path, NULL);
4780 if (error != NULL)
4781 goto done;
4783 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4784 worktree);
4785 if (error)
4786 goto done;
4788 init_curses();
4790 error = apply_unveil(got_repo_get_path(repo), NULL);
4791 if (error)
4792 goto done;
4794 error = tog_load_refs(repo);
4795 if (error)
4796 goto done;
4798 if (commit_id_str == NULL) {
4799 struct got_reference *head_ref;
4800 error = got_ref_open(&head_ref, repo, worktree ?
4801 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4802 if (error != NULL)
4803 goto done;
4804 error = got_ref_resolve(&commit_id, repo, head_ref);
4805 got_ref_close(head_ref);
4806 } else {
4807 error = got_repo_match_object_id(&commit_id, NULL,
4808 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4810 if (error != NULL)
4811 goto done;
4813 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4814 if (view == NULL) {
4815 error = got_error_from_errno("view_open");
4816 goto done;
4819 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4820 commit_id, repo);
4821 if (error)
4822 goto done;
4824 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4825 commit_id, repo);
4826 if (error)
4827 goto done;
4828 if (worktree) {
4829 /* Release work tree lock. */
4830 got_worktree_close(worktree);
4831 worktree = NULL;
4833 error = view_loop(view);
4834 done:
4835 free(repo_path);
4836 free(in_repo_path);
4837 free(link_target);
4838 free(cwd);
4839 free(commit_id);
4840 if (worktree)
4841 got_worktree_close(worktree);
4842 if (repo) {
4843 const struct got_error *close_err = got_repo_close(repo);
4844 if (error == NULL)
4845 error = close_err;
4847 tog_free_refs();
4848 return error;
4851 static const struct got_error *
4852 draw_tree_entries(struct tog_view *view, const char *parent_path)
4854 struct tog_tree_view_state *s = &view->state.tree;
4855 const struct got_error *err = NULL;
4856 struct got_tree_entry *te;
4857 wchar_t *wline;
4858 struct tog_color *tc;
4859 int width, n, i, nentries;
4860 int limit = view->nlines;
4862 s->ndisplayed = 0;
4864 werase(view->window);
4866 if (limit == 0)
4867 return NULL;
4869 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4870 if (err)
4871 return err;
4872 if (view_needs_focus_indication(view))
4873 wstandout(view->window);
4874 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4875 if (tc)
4876 wattr_on(view->window,
4877 COLOR_PAIR(tc->colorpair), NULL);
4878 waddwstr(view->window, wline);
4879 if (tc)
4880 wattr_off(view->window,
4881 COLOR_PAIR(tc->colorpair), NULL);
4882 if (view_needs_focus_indication(view))
4883 wstandend(view->window);
4884 free(wline);
4885 wline = NULL;
4886 if (width < view->ncols - 1)
4887 waddch(view->window, '\n');
4888 if (--limit <= 0)
4889 return NULL;
4890 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4891 if (err)
4892 return err;
4893 waddwstr(view->window, wline);
4894 free(wline);
4895 wline = NULL;
4896 if (width < view->ncols - 1)
4897 waddch(view->window, '\n');
4898 if (--limit <= 0)
4899 return NULL;
4900 waddch(view->window, '\n');
4901 if (--limit <= 0)
4902 return NULL;
4904 if (s->first_displayed_entry == NULL) {
4905 te = got_object_tree_get_first_entry(s->tree);
4906 if (s->selected == 0) {
4907 if (view->focussed)
4908 wstandout(view->window);
4909 s->selected_entry = NULL;
4911 waddstr(view->window, " ..\n"); /* parent directory */
4912 if (s->selected == 0 && view->focussed)
4913 wstandend(view->window);
4914 s->ndisplayed++;
4915 if (--limit <= 0)
4916 return NULL;
4917 n = 1;
4918 } else {
4919 n = 0;
4920 te = s->first_displayed_entry;
4923 nentries = got_object_tree_get_nentries(s->tree);
4924 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4925 char *line = NULL, *id_str = NULL, *link_target = NULL;
4926 const char *modestr = "";
4927 mode_t mode;
4929 te = got_object_tree_get_entry(s->tree, i);
4930 mode = got_tree_entry_get_mode(te);
4932 if (s->show_ids) {
4933 err = got_object_id_str(&id_str,
4934 got_tree_entry_get_id(te));
4935 if (err)
4936 return got_error_from_errno(
4937 "got_object_id_str");
4939 if (got_object_tree_entry_is_submodule(te))
4940 modestr = "$";
4941 else if (S_ISLNK(mode)) {
4942 int i;
4944 err = got_tree_entry_get_symlink_target(&link_target,
4945 te, s->repo);
4946 if (err) {
4947 free(id_str);
4948 return err;
4950 for (i = 0; i < strlen(link_target); i++) {
4951 if (!isprint((unsigned char)link_target[i]))
4952 link_target[i] = '?';
4954 modestr = "@";
4956 else if (S_ISDIR(mode))
4957 modestr = "/";
4958 else if (mode & S_IXUSR)
4959 modestr = "*";
4960 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4961 got_tree_entry_get_name(te), modestr,
4962 link_target ? " -> ": "",
4963 link_target ? link_target : "") == -1) {
4964 free(id_str);
4965 free(link_target);
4966 return got_error_from_errno("asprintf");
4968 free(id_str);
4969 free(link_target);
4970 err = format_line(&wline, &width, line, view->ncols, 0);
4971 if (err) {
4972 free(line);
4973 break;
4975 if (n == s->selected) {
4976 if (view->focussed)
4977 wstandout(view->window);
4978 s->selected_entry = te;
4980 tc = match_color(&s->colors, line);
4981 if (tc)
4982 wattr_on(view->window,
4983 COLOR_PAIR(tc->colorpair), NULL);
4984 waddwstr(view->window, wline);
4985 if (tc)
4986 wattr_off(view->window,
4987 COLOR_PAIR(tc->colorpair), NULL);
4988 if (width < view->ncols - 1)
4989 waddch(view->window, '\n');
4990 if (n == s->selected && view->focussed)
4991 wstandend(view->window);
4992 free(line);
4993 free(wline);
4994 wline = NULL;
4995 n++;
4996 s->ndisplayed++;
4997 s->last_displayed_entry = te;
4998 if (--limit <= 0)
4999 break;
5002 return err;
5005 static void
5006 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5008 struct got_tree_entry *te;
5009 int isroot = s->tree == s->root;
5010 int i = 0;
5012 if (s->first_displayed_entry == NULL)
5013 return;
5015 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5016 while (i++ < maxscroll) {
5017 if (te == NULL) {
5018 if (!isroot)
5019 s->first_displayed_entry = NULL;
5020 break;
5022 s->first_displayed_entry = te;
5023 te = got_tree_entry_get_prev(s->tree, te);
5027 static void
5028 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5030 struct got_tree_entry *next, *last;
5031 int n = 0;
5033 if (s->first_displayed_entry)
5034 next = got_tree_entry_get_next(s->tree,
5035 s->first_displayed_entry);
5036 else
5037 next = got_object_tree_get_first_entry(s->tree);
5039 last = s->last_displayed_entry;
5040 while (next && last && n++ < maxscroll) {
5041 last = got_tree_entry_get_next(s->tree, last);
5042 if (last) {
5043 s->first_displayed_entry = next;
5044 next = got_tree_entry_get_next(s->tree, next);
5049 static const struct got_error *
5050 tree_entry_path(char **path, struct tog_parent_trees *parents,
5051 struct got_tree_entry *te)
5053 const struct got_error *err = NULL;
5054 struct tog_parent_tree *pt;
5055 size_t len = 2; /* for leading slash and NUL */
5057 TAILQ_FOREACH(pt, parents, entry)
5058 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5059 + 1 /* slash */;
5060 if (te)
5061 len += strlen(got_tree_entry_get_name(te));
5063 *path = calloc(1, len);
5064 if (path == NULL)
5065 return got_error_from_errno("calloc");
5067 (*path)[0] = '/';
5068 pt = TAILQ_LAST(parents, tog_parent_trees);
5069 while (pt) {
5070 const char *name = got_tree_entry_get_name(pt->selected_entry);
5071 if (strlcat(*path, name, len) >= len) {
5072 err = got_error(GOT_ERR_NO_SPACE);
5073 goto done;
5075 if (strlcat(*path, "/", len) >= len) {
5076 err = got_error(GOT_ERR_NO_SPACE);
5077 goto done;
5079 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5081 if (te) {
5082 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5083 err = got_error(GOT_ERR_NO_SPACE);
5084 goto done;
5087 done:
5088 if (err) {
5089 free(*path);
5090 *path = NULL;
5092 return err;
5095 static const struct got_error *
5096 blame_tree_entry(struct tog_view **new_view, int begin_x,
5097 struct got_tree_entry *te, struct tog_parent_trees *parents,
5098 struct got_object_id *commit_id, struct got_repository *repo)
5100 const struct got_error *err = NULL;
5101 char *path;
5102 struct tog_view *blame_view;
5104 *new_view = NULL;
5106 err = tree_entry_path(&path, parents, te);
5107 if (err)
5108 return err;
5110 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5111 if (blame_view == NULL) {
5112 err = got_error_from_errno("view_open");
5113 goto done;
5116 err = open_blame_view(blame_view, path, commit_id, repo);
5117 if (err) {
5118 if (err->code == GOT_ERR_CANCELLED)
5119 err = NULL;
5120 view_close(blame_view);
5121 } else
5122 *new_view = blame_view;
5123 done:
5124 free(path);
5125 return err;
5128 static const struct got_error *
5129 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5130 struct tog_tree_view_state *s)
5132 struct tog_view *log_view;
5133 const struct got_error *err = NULL;
5134 char *path;
5136 *new_view = NULL;
5138 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5139 if (log_view == NULL)
5140 return got_error_from_errno("view_open");
5142 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5143 if (err)
5144 return err;
5146 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5147 path, 0);
5148 if (err)
5149 view_close(log_view);
5150 else
5151 *new_view = log_view;
5152 free(path);
5153 return err;
5156 static const struct got_error *
5157 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5158 const char *head_ref_name, struct got_repository *repo)
5160 const struct got_error *err = NULL;
5161 char *commit_id_str = NULL;
5162 struct tog_tree_view_state *s = &view->state.tree;
5163 struct got_commit_object *commit = NULL;
5165 TAILQ_INIT(&s->parents);
5166 STAILQ_INIT(&s->colors);
5168 s->commit_id = got_object_id_dup(commit_id);
5169 if (s->commit_id == NULL)
5170 return got_error_from_errno("got_object_id_dup");
5172 err = got_object_open_as_commit(&commit, repo, commit_id);
5173 if (err)
5174 goto done;
5177 * The root is opened here and will be closed when the view is closed.
5178 * Any visited subtrees and their path-wise parents are opened and
5179 * closed on demand.
5181 err = got_object_open_as_tree(&s->root, repo,
5182 got_object_commit_get_tree_id(commit));
5183 if (err)
5184 goto done;
5185 s->tree = s->root;
5187 err = got_object_id_str(&commit_id_str, commit_id);
5188 if (err != NULL)
5189 goto done;
5191 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5192 err = got_error_from_errno("asprintf");
5193 goto done;
5196 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5197 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5198 if (head_ref_name) {
5199 s->head_ref_name = strdup(head_ref_name);
5200 if (s->head_ref_name == NULL) {
5201 err = got_error_from_errno("strdup");
5202 goto done;
5205 s->repo = repo;
5207 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5208 err = add_color(&s->colors, "\\$$",
5209 TOG_COLOR_TREE_SUBMODULE,
5210 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5211 if (err)
5212 goto done;
5213 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5214 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5215 if (err)
5216 goto done;
5217 err = add_color(&s->colors, "/$",
5218 TOG_COLOR_TREE_DIRECTORY,
5219 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5220 if (err)
5221 goto done;
5223 err = add_color(&s->colors, "\\*$",
5224 TOG_COLOR_TREE_EXECUTABLE,
5225 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5226 if (err)
5227 goto done;
5229 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5230 get_color_value("TOG_COLOR_COMMIT"));
5231 if (err)
5232 goto done;
5235 view->show = show_tree_view;
5236 view->input = input_tree_view;
5237 view->close = close_tree_view;
5238 view->search_start = search_start_tree_view;
5239 view->search_next = search_next_tree_view;
5240 done:
5241 free(commit_id_str);
5242 if (commit)
5243 got_object_commit_close(commit);
5244 if (err)
5245 close_tree_view(view);
5246 return err;
5249 static const struct got_error *
5250 close_tree_view(struct tog_view *view)
5252 struct tog_tree_view_state *s = &view->state.tree;
5254 free_colors(&s->colors);
5255 free(s->tree_label);
5256 s->tree_label = NULL;
5257 free(s->commit_id);
5258 s->commit_id = NULL;
5259 free(s->head_ref_name);
5260 s->head_ref_name = NULL;
5261 while (!TAILQ_EMPTY(&s->parents)) {
5262 struct tog_parent_tree *parent;
5263 parent = TAILQ_FIRST(&s->parents);
5264 TAILQ_REMOVE(&s->parents, parent, entry);
5265 if (parent->tree != s->root)
5266 got_object_tree_close(parent->tree);
5267 free(parent);
5270 if (s->tree != NULL && s->tree != s->root)
5271 got_object_tree_close(s->tree);
5272 if (s->root)
5273 got_object_tree_close(s->root);
5274 return NULL;
5277 static const struct got_error *
5278 search_start_tree_view(struct tog_view *view)
5280 struct tog_tree_view_state *s = &view->state.tree;
5282 s->matched_entry = NULL;
5283 return NULL;
5286 static int
5287 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5289 regmatch_t regmatch;
5291 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5292 0) == 0;
5295 static const struct got_error *
5296 search_next_tree_view(struct tog_view *view)
5298 struct tog_tree_view_state *s = &view->state.tree;
5299 struct got_tree_entry *te = NULL;
5301 if (!view->searching) {
5302 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5303 return NULL;
5306 if (s->matched_entry) {
5307 if (view->searching == TOG_SEARCH_FORWARD) {
5308 if (s->selected_entry)
5309 te = got_tree_entry_get_next(s->tree,
5310 s->selected_entry);
5311 else
5312 te = got_object_tree_get_first_entry(s->tree);
5313 } else {
5314 if (s->selected_entry == NULL)
5315 te = got_object_tree_get_last_entry(s->tree);
5316 else
5317 te = got_tree_entry_get_prev(s->tree,
5318 s->selected_entry);
5320 } else {
5321 if (view->searching == TOG_SEARCH_FORWARD)
5322 te = got_object_tree_get_first_entry(s->tree);
5323 else
5324 te = got_object_tree_get_last_entry(s->tree);
5327 while (1) {
5328 if (te == NULL) {
5329 if (s->matched_entry == NULL) {
5330 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5331 return NULL;
5333 if (view->searching == TOG_SEARCH_FORWARD)
5334 te = got_object_tree_get_first_entry(s->tree);
5335 else
5336 te = got_object_tree_get_last_entry(s->tree);
5339 if (match_tree_entry(te, &view->regex)) {
5340 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5341 s->matched_entry = te;
5342 break;
5345 if (view->searching == TOG_SEARCH_FORWARD)
5346 te = got_tree_entry_get_next(s->tree, te);
5347 else
5348 te = got_tree_entry_get_prev(s->tree, te);
5351 if (s->matched_entry) {
5352 s->first_displayed_entry = s->matched_entry;
5353 s->selected = 0;
5356 return NULL;
5359 static const struct got_error *
5360 show_tree_view(struct tog_view *view)
5362 const struct got_error *err = NULL;
5363 struct tog_tree_view_state *s = &view->state.tree;
5364 char *parent_path;
5366 err = tree_entry_path(&parent_path, &s->parents, NULL);
5367 if (err)
5368 return err;
5370 err = draw_tree_entries(view, parent_path);
5371 free(parent_path);
5373 view_vborder(view);
5374 return err;
5377 static const struct got_error *
5378 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5380 const struct got_error *err = NULL;
5381 struct tog_tree_view_state *s = &view->state.tree;
5382 struct tog_view *log_view, *ref_view;
5383 int begin_x = 0;
5385 switch (ch) {
5386 case 'i':
5387 s->show_ids = !s->show_ids;
5388 break;
5389 case 'l':
5390 if (!s->selected_entry)
5391 break;
5392 if (view_is_parent_view(view))
5393 begin_x = view_split_begin_x(view->begin_x);
5394 err = log_selected_tree_entry(&log_view, begin_x, s);
5395 view->focussed = 0;
5396 log_view->focussed = 1;
5397 if (view_is_parent_view(view)) {
5398 err = view_close_child(view);
5399 if (err)
5400 return err;
5401 view_set_child(view, log_view);
5402 view->focus_child = 1;
5403 } else
5404 *new_view = log_view;
5405 break;
5406 case 'r':
5407 if (view_is_parent_view(view))
5408 begin_x = view_split_begin_x(view->begin_x);
5409 ref_view = view_open(view->nlines, view->ncols,
5410 view->begin_y, begin_x, TOG_VIEW_REF);
5411 if (ref_view == NULL)
5412 return got_error_from_errno("view_open");
5413 err = open_ref_view(ref_view, s->repo);
5414 if (err) {
5415 view_close(ref_view);
5416 return err;
5418 view->focussed = 0;
5419 ref_view->focussed = 1;
5420 if (view_is_parent_view(view)) {
5421 err = view_close_child(view);
5422 if (err)
5423 return err;
5424 view_set_child(view, ref_view);
5425 view->focus_child = 1;
5426 } else
5427 *new_view = ref_view;
5428 break;
5429 case 'k':
5430 case KEY_UP:
5431 if (s->selected > 0) {
5432 s->selected--;
5433 break;
5435 tree_scroll_up(s, 1);
5436 break;
5437 case KEY_PPAGE:
5438 case CTRL('b'):
5439 if (s->tree == s->root) {
5440 if (got_object_tree_get_first_entry(s->tree) ==
5441 s->first_displayed_entry)
5442 s->selected = 0;
5443 } else {
5444 if (s->first_displayed_entry == NULL)
5445 s->selected = 0;
5447 tree_scroll_up(s, MAX(0, view->nlines - 3));
5448 break;
5449 case 'j':
5450 case KEY_DOWN:
5451 if (s->selected < s->ndisplayed - 1) {
5452 s->selected++;
5453 break;
5455 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5456 == NULL)
5457 /* can't scroll any further */
5458 break;
5459 tree_scroll_down(s, 1);
5460 break;
5461 case KEY_NPAGE:
5462 case CTRL('f'):
5463 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5464 == NULL) {
5465 /* can't scroll any further; move cursor down */
5466 if (s->selected < s->ndisplayed - 1)
5467 s->selected = s->ndisplayed - 1;
5468 break;
5470 tree_scroll_down(s, view->nlines - 3);
5471 break;
5472 case KEY_ENTER:
5473 case '\r':
5474 case KEY_BACKSPACE:
5475 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5476 struct tog_parent_tree *parent;
5477 /* user selected '..' */
5478 if (s->tree == s->root)
5479 break;
5480 parent = TAILQ_FIRST(&s->parents);
5481 TAILQ_REMOVE(&s->parents, parent,
5482 entry);
5483 got_object_tree_close(s->tree);
5484 s->tree = parent->tree;
5485 s->first_displayed_entry =
5486 parent->first_displayed_entry;
5487 s->selected_entry =
5488 parent->selected_entry;
5489 s->selected = parent->selected;
5490 free(parent);
5491 } else if (S_ISDIR(got_tree_entry_get_mode(
5492 s->selected_entry))) {
5493 struct got_tree_object *subtree;
5494 err = got_object_open_as_tree(&subtree, s->repo,
5495 got_tree_entry_get_id(s->selected_entry));
5496 if (err)
5497 break;
5498 err = tree_view_visit_subtree(s, subtree);
5499 if (err) {
5500 got_object_tree_close(subtree);
5501 break;
5503 } else if (S_ISREG(got_tree_entry_get_mode(
5504 s->selected_entry))) {
5505 struct tog_view *blame_view;
5506 int begin_x = view_is_parent_view(view) ?
5507 view_split_begin_x(view->begin_x) : 0;
5509 err = blame_tree_entry(&blame_view, begin_x,
5510 s->selected_entry, &s->parents,
5511 s->commit_id, s->repo);
5512 if (err)
5513 break;
5514 view->focussed = 0;
5515 blame_view->focussed = 1;
5516 if (view_is_parent_view(view)) {
5517 err = view_close_child(view);
5518 if (err)
5519 return err;
5520 view_set_child(view, blame_view);
5521 view->focus_child = 1;
5522 } else
5523 *new_view = blame_view;
5525 break;
5526 case KEY_RESIZE:
5527 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5528 s->selected = view->nlines - 4;
5529 break;
5530 default:
5531 break;
5534 return err;
5537 __dead static void
5538 usage_tree(void)
5540 endwin();
5541 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5542 getprogname());
5543 exit(1);
5546 static const struct got_error *
5547 cmd_tree(int argc, char *argv[])
5549 const struct got_error *error;
5550 struct got_repository *repo = NULL;
5551 struct got_worktree *worktree = NULL;
5552 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5553 struct got_object_id *commit_id = NULL;
5554 const char *commit_id_arg = NULL;
5555 char *label = NULL;
5556 struct got_reference *ref = NULL;
5557 const char *head_ref_name = NULL;
5558 int ch;
5559 struct tog_view *view;
5561 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5562 switch (ch) {
5563 case 'c':
5564 commit_id_arg = optarg;
5565 break;
5566 case 'r':
5567 repo_path = realpath(optarg, NULL);
5568 if (repo_path == NULL)
5569 return got_error_from_errno2("realpath",
5570 optarg);
5571 break;
5572 default:
5573 usage_tree();
5574 /* NOTREACHED */
5578 argc -= optind;
5579 argv += optind;
5581 if (argc > 1)
5582 usage_tree();
5584 if (repo_path == NULL) {
5585 cwd = getcwd(NULL, 0);
5586 if (cwd == NULL)
5587 return got_error_from_errno("getcwd");
5588 error = got_worktree_open(&worktree, cwd);
5589 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5590 goto done;
5591 if (worktree)
5592 repo_path =
5593 strdup(got_worktree_get_repo_path(worktree));
5594 else
5595 repo_path = strdup(cwd);
5596 if (repo_path == NULL) {
5597 error = got_error_from_errno("strdup");
5598 goto done;
5602 error = got_repo_open(&repo, repo_path, NULL);
5603 if (error != NULL)
5604 goto done;
5606 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5607 repo, worktree);
5608 if (error)
5609 goto done;
5611 init_curses();
5613 error = apply_unveil(got_repo_get_path(repo), NULL);
5614 if (error)
5615 goto done;
5617 error = tog_load_refs(repo);
5618 if (error)
5619 goto done;
5621 if (commit_id_arg == NULL) {
5622 error = got_repo_match_object_id(&commit_id, &label,
5623 worktree ? got_worktree_get_head_ref_name(worktree) :
5624 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5625 if (error)
5626 goto done;
5627 head_ref_name = label;
5628 } else {
5629 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5630 if (error == NULL)
5631 head_ref_name = got_ref_get_name(ref);
5632 else if (error->code != GOT_ERR_NOT_REF)
5633 goto done;
5634 error = got_repo_match_object_id(&commit_id, NULL,
5635 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5636 if (error)
5637 goto done;
5640 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5641 if (view == NULL) {
5642 error = got_error_from_errno("view_open");
5643 goto done;
5645 error = open_tree_view(view, commit_id, head_ref_name, repo);
5646 if (error)
5647 goto done;
5648 if (!got_path_is_root_dir(in_repo_path)) {
5649 error = tree_view_walk_path(&view->state.tree, commit_id,
5650 in_repo_path);
5651 if (error)
5652 goto done;
5655 if (worktree) {
5656 /* Release work tree lock. */
5657 got_worktree_close(worktree);
5658 worktree = NULL;
5660 error = view_loop(view);
5661 done:
5662 free(repo_path);
5663 free(cwd);
5664 free(commit_id);
5665 free(label);
5666 if (ref)
5667 got_ref_close(ref);
5668 if (repo) {
5669 const struct got_error *close_err = got_repo_close(repo);
5670 if (error == NULL)
5671 error = close_err;
5673 tog_free_refs();
5674 return error;
5677 static const struct got_error *
5678 ref_view_load_refs(struct tog_ref_view_state *s)
5680 struct got_reflist_entry *sre;
5681 struct tog_reflist_entry *re;
5683 s->nrefs = 0;
5684 TAILQ_FOREACH(sre, &tog_refs, entry) {
5685 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5686 continue;
5688 re = malloc(sizeof(*re));
5689 if (re == NULL)
5690 return got_error_from_errno("malloc");
5692 re->ref = got_ref_dup(sre->ref);
5693 if (re->ref == NULL)
5694 return got_error_from_errno("got_ref_dup");
5695 re->idx = s->nrefs++;
5696 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5699 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5700 return NULL;
5703 void
5704 ref_view_free_refs(struct tog_ref_view_state *s)
5706 struct tog_reflist_entry *re;
5708 while (!TAILQ_EMPTY(&s->refs)) {
5709 re = TAILQ_FIRST(&s->refs);
5710 TAILQ_REMOVE(&s->refs, re, entry);
5711 got_ref_close(re->ref);
5712 free(re);
5716 static const struct got_error *
5717 open_ref_view(struct tog_view *view, struct got_repository *repo)
5719 const struct got_error *err = NULL;
5720 struct tog_ref_view_state *s = &view->state.ref;
5722 s->selected_entry = 0;
5723 s->repo = repo;
5725 TAILQ_INIT(&s->refs);
5726 STAILQ_INIT(&s->colors);
5728 err = ref_view_load_refs(s);
5729 if (err)
5730 return err;
5732 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5733 err = add_color(&s->colors, "^refs/heads/",
5734 TOG_COLOR_REFS_HEADS,
5735 get_color_value("TOG_COLOR_REFS_HEADS"));
5736 if (err)
5737 goto done;
5739 err = add_color(&s->colors, "^refs/tags/",
5740 TOG_COLOR_REFS_TAGS,
5741 get_color_value("TOG_COLOR_REFS_TAGS"));
5742 if (err)
5743 goto done;
5745 err = add_color(&s->colors, "^refs/remotes/",
5746 TOG_COLOR_REFS_REMOTES,
5747 get_color_value("TOG_COLOR_REFS_REMOTES"));
5748 if (err)
5749 goto done;
5752 view->show = show_ref_view;
5753 view->input = input_ref_view;
5754 view->close = close_ref_view;
5755 view->search_start = search_start_ref_view;
5756 view->search_next = search_next_ref_view;
5757 done:
5758 if (err)
5759 free_colors(&s->colors);
5760 return err;
5763 static const struct got_error *
5764 close_ref_view(struct tog_view *view)
5766 struct tog_ref_view_state *s = &view->state.ref;
5768 ref_view_free_refs(s);
5769 free_colors(&s->colors);
5771 return NULL;
5774 static const struct got_error *
5775 resolve_reflist_entry(struct got_object_id **commit_id,
5776 struct tog_reflist_entry *re, struct got_repository *repo)
5778 const struct got_error *err = NULL;
5779 struct got_object_id *obj_id;
5780 struct got_tag_object *tag = NULL;
5781 int obj_type;
5783 *commit_id = NULL;
5785 err = got_ref_resolve(&obj_id, repo, re->ref);
5786 if (err)
5787 return err;
5789 err = got_object_get_type(&obj_type, repo, obj_id);
5790 if (err)
5791 goto done;
5793 switch (obj_type) {
5794 case GOT_OBJ_TYPE_COMMIT:
5795 *commit_id = obj_id;
5796 break;
5797 case GOT_OBJ_TYPE_TAG:
5798 err = got_object_open_as_tag(&tag, repo, obj_id);
5799 if (err)
5800 goto done;
5801 free(obj_id);
5802 err = got_object_get_type(&obj_type, repo,
5803 got_object_tag_get_object_id(tag));
5804 if (err)
5805 goto done;
5806 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5807 err = got_error(GOT_ERR_OBJ_TYPE);
5808 goto done;
5810 *commit_id = got_object_id_dup(
5811 got_object_tag_get_object_id(tag));
5812 if (*commit_id == NULL) {
5813 err = got_error_from_errno("got_object_id_dup");
5814 goto done;
5816 break;
5817 default:
5818 err = got_error(GOT_ERR_OBJ_TYPE);
5819 break;
5822 done:
5823 if (tag)
5824 got_object_tag_close(tag);
5825 if (err) {
5826 free(*commit_id);
5827 *commit_id = NULL;
5829 return err;
5832 static const struct got_error *
5833 log_ref_entry(struct tog_view **new_view, int begin_x,
5834 struct tog_reflist_entry *re, struct got_repository *repo)
5836 struct tog_view *log_view;
5837 const struct got_error *err = NULL;
5838 struct got_object_id *commit_id = NULL;
5840 *new_view = NULL;
5842 err = resolve_reflist_entry(&commit_id, re, repo);
5843 if (err) {
5844 if (err->code != GOT_ERR_OBJ_TYPE)
5845 return err;
5846 else
5847 return NULL;
5850 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5851 if (log_view == NULL) {
5852 err = got_error_from_errno("view_open");
5853 goto done;
5856 err = open_log_view(log_view, commit_id, repo,
5857 got_ref_get_name(re->ref), "", 0);
5858 done:
5859 if (err)
5860 view_close(log_view);
5861 else
5862 *new_view = log_view;
5863 free(commit_id);
5864 return err;
5867 static void
5868 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5870 struct tog_reflist_entry *re;
5871 int i = 0;
5873 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5874 return;
5876 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5877 while (i++ < maxscroll) {
5878 if (re == NULL)
5879 break;
5880 s->first_displayed_entry = re;
5881 re = TAILQ_PREV(re, tog_reflist_head, entry);
5885 static void
5886 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5888 struct tog_reflist_entry *next, *last;
5889 int n = 0;
5891 if (s->first_displayed_entry)
5892 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5893 else
5894 next = TAILQ_FIRST(&s->refs);
5896 last = s->last_displayed_entry;
5897 while (next && last && n++ < maxscroll) {
5898 last = TAILQ_NEXT(last, entry);
5899 if (last) {
5900 s->first_displayed_entry = next;
5901 next = TAILQ_NEXT(next, entry);
5906 static const struct got_error *
5907 search_start_ref_view(struct tog_view *view)
5909 struct tog_ref_view_state *s = &view->state.ref;
5911 s->matched_entry = NULL;
5912 return NULL;
5915 static int
5916 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5918 regmatch_t regmatch;
5920 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5921 0) == 0;
5924 static const struct got_error *
5925 search_next_ref_view(struct tog_view *view)
5927 struct tog_ref_view_state *s = &view->state.ref;
5928 struct tog_reflist_entry *re = NULL;
5930 if (!view->searching) {
5931 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5932 return NULL;
5935 if (s->matched_entry) {
5936 if (view->searching == TOG_SEARCH_FORWARD) {
5937 if (s->selected_entry)
5938 re = TAILQ_NEXT(s->selected_entry, entry);
5939 else
5940 re = TAILQ_PREV(s->selected_entry,
5941 tog_reflist_head, entry);
5942 } else {
5943 if (s->selected_entry == NULL)
5944 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5945 else
5946 re = TAILQ_PREV(s->selected_entry,
5947 tog_reflist_head, entry);
5949 } else {
5950 if (view->searching == TOG_SEARCH_FORWARD)
5951 re = TAILQ_FIRST(&s->refs);
5952 else
5953 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5956 while (1) {
5957 if (re == NULL) {
5958 if (s->matched_entry == NULL) {
5959 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5960 return NULL;
5962 if (view->searching == TOG_SEARCH_FORWARD)
5963 re = TAILQ_FIRST(&s->refs);
5964 else
5965 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5968 if (match_reflist_entry(re, &view->regex)) {
5969 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5970 s->matched_entry = re;
5971 break;
5974 if (view->searching == TOG_SEARCH_FORWARD)
5975 re = TAILQ_NEXT(re, entry);
5976 else
5977 re = TAILQ_PREV(re, tog_reflist_head, entry);
5980 if (s->matched_entry) {
5981 s->first_displayed_entry = s->matched_entry;
5982 s->selected = 0;
5985 return NULL;
5988 static const struct got_error *
5989 show_ref_view(struct tog_view *view)
5991 const struct got_error *err = NULL;
5992 struct tog_ref_view_state *s = &view->state.ref;
5993 struct tog_reflist_entry *re;
5994 char *line = NULL;
5995 wchar_t *wline;
5996 struct tog_color *tc;
5997 int width, n;
5998 int limit = view->nlines;
6000 werase(view->window);
6002 s->ndisplayed = 0;
6004 if (limit == 0)
6005 return NULL;
6007 re = s->first_displayed_entry;
6009 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6010 s->nrefs) == -1)
6011 return got_error_from_errno("asprintf");
6013 err = format_line(&wline, &width, line, view->ncols, 0);
6014 if (err) {
6015 free(line);
6016 return err;
6018 if (view_needs_focus_indication(view))
6019 wstandout(view->window);
6020 waddwstr(view->window, wline);
6021 if (view_needs_focus_indication(view))
6022 wstandend(view->window);
6023 free(wline);
6024 wline = NULL;
6025 free(line);
6026 line = NULL;
6027 if (width < view->ncols - 1)
6028 waddch(view->window, '\n');
6029 if (--limit <= 0)
6030 return NULL;
6032 n = 0;
6033 while (re && limit > 0) {
6034 char *line = NULL;
6036 if (got_ref_is_symbolic(re->ref)) {
6037 if (asprintf(&line, "%s -> %s",
6038 got_ref_get_name(re->ref),
6039 got_ref_get_symref_target(re->ref)) == -1)
6040 return got_error_from_errno("asprintf");
6041 } else if (s->show_ids) {
6042 struct got_object_id *id;
6043 char *id_str;
6044 err = got_ref_resolve(&id, s->repo, re->ref);
6045 if (err)
6046 return err;
6047 err = got_object_id_str(&id_str, id);
6048 if (err) {
6049 free(id);
6050 return err;
6052 if (asprintf(&line, "%s: %s",
6053 got_ref_get_name(re->ref), id_str) == -1) {
6054 err = got_error_from_errno("asprintf");
6055 free(id);
6056 free(id_str);
6057 return err;
6059 free(id);
6060 free(id_str);
6061 } else {
6062 line = strdup(got_ref_get_name(re->ref));
6063 if (line == NULL)
6064 return got_error_from_errno("strdup");
6067 err = format_line(&wline, &width, line, view->ncols, 0);
6068 if (err) {
6069 free(line);
6070 return err;
6072 if (n == s->selected) {
6073 if (view->focussed)
6074 wstandout(view->window);
6075 s->selected_entry = re;
6077 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6078 if (tc)
6079 wattr_on(view->window,
6080 COLOR_PAIR(tc->colorpair), NULL);
6081 waddwstr(view->window, wline);
6082 if (tc)
6083 wattr_off(view->window,
6084 COLOR_PAIR(tc->colorpair), NULL);
6085 if (width < view->ncols - 1)
6086 waddch(view->window, '\n');
6087 if (n == s->selected && view->focussed)
6088 wstandend(view->window);
6089 free(line);
6090 free(wline);
6091 wline = NULL;
6092 n++;
6093 s->ndisplayed++;
6094 s->last_displayed_entry = re;
6096 limit--;
6097 re = TAILQ_NEXT(re, entry);
6100 view_vborder(view);
6101 return err;
6104 static const struct got_error *
6105 browse_ref_tree(struct tog_view **new_view, int begin_x,
6106 struct tog_reflist_entry *re, struct got_repository *repo)
6108 const struct got_error *err = NULL;
6109 struct got_object_id *commit_id = NULL;
6110 struct tog_view *tree_view;
6112 *new_view = NULL;
6114 err = resolve_reflist_entry(&commit_id, re, repo);
6115 if (err) {
6116 if (err->code != GOT_ERR_OBJ_TYPE)
6117 return err;
6118 else
6119 return NULL;
6123 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6124 if (tree_view == NULL) {
6125 err = got_error_from_errno("view_open");
6126 goto done;
6129 err = open_tree_view(tree_view, commit_id,
6130 got_ref_get_name(re->ref), repo);
6131 if (err)
6132 goto done;
6134 *new_view = tree_view;
6135 done:
6136 free(commit_id);
6137 return err;
6139 static const struct got_error *
6140 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6142 const struct got_error *err = NULL;
6143 struct tog_ref_view_state *s = &view->state.ref;
6144 struct tog_view *log_view, *tree_view;
6145 int begin_x = 0;
6147 switch (ch) {
6148 case 'i':
6149 s->show_ids = !s->show_ids;
6150 break;
6151 case KEY_ENTER:
6152 case '\r':
6153 if (!s->selected_entry)
6154 break;
6155 if (view_is_parent_view(view))
6156 begin_x = view_split_begin_x(view->begin_x);
6157 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6158 s->repo);
6159 view->focussed = 0;
6160 log_view->focussed = 1;
6161 if (view_is_parent_view(view)) {
6162 err = view_close_child(view);
6163 if (err)
6164 return err;
6165 view_set_child(view, log_view);
6166 view->focus_child = 1;
6167 } else
6168 *new_view = log_view;
6169 break;
6170 case 't':
6171 if (!s->selected_entry)
6172 break;
6173 if (view_is_parent_view(view))
6174 begin_x = view_split_begin_x(view->begin_x);
6175 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6176 s->repo);
6177 if (err || tree_view == NULL)
6178 break;
6179 view->focussed = 0;
6180 tree_view->focussed = 1;
6181 if (view_is_parent_view(view)) {
6182 err = view_close_child(view);
6183 if (err)
6184 return err;
6185 view_set_child(view, tree_view);
6186 view->focus_child = 1;
6187 } else
6188 *new_view = tree_view;
6189 break;
6190 case 'k':
6191 case KEY_UP:
6192 if (s->selected > 0) {
6193 s->selected--;
6194 break;
6196 ref_scroll_up(s, 1);
6197 break;
6198 case KEY_PPAGE:
6199 case CTRL('b'):
6200 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6201 s->selected = 0;
6202 ref_scroll_up(s, MAX(0, view->nlines - 1));
6203 break;
6204 case 'j':
6205 case KEY_DOWN:
6206 if (s->selected < s->ndisplayed - 1) {
6207 s->selected++;
6208 break;
6210 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6211 /* can't scroll any further */
6212 break;
6213 ref_scroll_down(s, 1);
6214 break;
6215 case KEY_NPAGE:
6216 case CTRL('f'):
6217 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6218 /* can't scroll any further; move cursor down */
6219 if (s->selected < s->ndisplayed - 1)
6220 s->selected = s->ndisplayed - 1;
6221 break;
6223 ref_scroll_down(s, view->nlines - 1);
6224 break;
6225 case CTRL('l'):
6226 tog_free_refs();
6227 err = tog_load_refs(s->repo);
6228 if (err)
6229 break;
6230 ref_view_free_refs(s);
6231 err = ref_view_load_refs(s);
6232 break;
6233 case KEY_RESIZE:
6234 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6235 s->selected = view->nlines - 2;
6236 break;
6237 default:
6238 break;
6241 return err;
6244 __dead static void
6245 usage_ref(void)
6247 endwin();
6248 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6249 getprogname());
6250 exit(1);
6253 static const struct got_error *
6254 cmd_ref(int argc, char *argv[])
6256 const struct got_error *error;
6257 struct got_repository *repo = NULL;
6258 struct got_worktree *worktree = NULL;
6259 char *cwd = NULL, *repo_path = NULL;
6260 int ch;
6261 struct tog_view *view;
6263 while ((ch = getopt(argc, argv, "r:")) != -1) {
6264 switch (ch) {
6265 case 'r':
6266 repo_path = realpath(optarg, NULL);
6267 if (repo_path == NULL)
6268 return got_error_from_errno2("realpath",
6269 optarg);
6270 break;
6271 default:
6272 usage_ref();
6273 /* NOTREACHED */
6277 argc -= optind;
6278 argv += optind;
6280 if (argc > 1)
6281 usage_ref();
6283 if (repo_path == NULL) {
6284 cwd = getcwd(NULL, 0);
6285 if (cwd == NULL)
6286 return got_error_from_errno("getcwd");
6287 error = got_worktree_open(&worktree, cwd);
6288 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6289 goto done;
6290 if (worktree)
6291 repo_path =
6292 strdup(got_worktree_get_repo_path(worktree));
6293 else
6294 repo_path = strdup(cwd);
6295 if (repo_path == NULL) {
6296 error = got_error_from_errno("strdup");
6297 goto done;
6301 error = got_repo_open(&repo, repo_path, NULL);
6302 if (error != NULL)
6303 goto done;
6305 init_curses();
6307 error = apply_unveil(got_repo_get_path(repo), NULL);
6308 if (error)
6309 goto done;
6311 error = tog_load_refs(repo);
6312 if (error)
6313 goto done;
6315 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6316 if (view == NULL) {
6317 error = got_error_from_errno("view_open");
6318 goto done;
6321 error = open_ref_view(view, repo);
6322 if (error)
6323 goto done;
6325 if (worktree) {
6326 /* Release work tree lock. */
6327 got_worktree_close(worktree);
6328 worktree = NULL;
6330 error = view_loop(view);
6331 done:
6332 free(repo_path);
6333 free(cwd);
6334 if (repo) {
6335 const struct got_error *close_err = got_repo_close(repo);
6336 if (close_err)
6337 error = close_err;
6339 tog_free_refs();
6340 return error;
6343 static void
6344 list_commands(FILE *fp)
6346 size_t i;
6348 fprintf(fp, "commands:");
6349 for (i = 0; i < nitems(tog_commands); i++) {
6350 struct tog_cmd *cmd = &tog_commands[i];
6351 fprintf(fp, " %s", cmd->name);
6353 fputc('\n', fp);
6356 __dead static void
6357 usage(int hflag, int status)
6359 FILE *fp = (status == 0) ? stdout : stderr;
6361 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6362 getprogname());
6363 if (hflag) {
6364 fprintf(fp, "lazy usage: %s path\n", getprogname());
6365 list_commands(fp);
6367 exit(status);
6370 static char **
6371 make_argv(int argc, ...)
6373 va_list ap;
6374 char **argv;
6375 int i;
6377 va_start(ap, argc);
6379 argv = calloc(argc, sizeof(char *));
6380 if (argv == NULL)
6381 err(1, "calloc");
6382 for (i = 0; i < argc; i++) {
6383 argv[i] = strdup(va_arg(ap, char *));
6384 if (argv[i] == NULL)
6385 err(1, "strdup");
6388 va_end(ap);
6389 return argv;
6393 * Try to convert 'tog path' into a 'tog log path' command.
6394 * The user could simply have mistyped the command rather than knowingly
6395 * provided a path. So check whether argv[0] can in fact be resolved
6396 * to a path in the HEAD commit and print a special error if not.
6397 * This hack is for mpi@ <3
6399 static const struct got_error *
6400 tog_log_with_path(int argc, char *argv[])
6402 const struct got_error *error = NULL, *close_err;
6403 struct tog_cmd *cmd = NULL;
6404 struct got_repository *repo = NULL;
6405 struct got_worktree *worktree = NULL;
6406 struct got_object_id *commit_id = NULL, *id = NULL;
6407 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6408 char *commit_id_str = NULL, **cmd_argv = NULL;
6410 cwd = getcwd(NULL, 0);
6411 if (cwd == NULL)
6412 return got_error_from_errno("getcwd");
6414 error = got_worktree_open(&worktree, cwd);
6415 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6416 goto done;
6418 if (worktree)
6419 repo_path = strdup(got_worktree_get_repo_path(worktree));
6420 else
6421 repo_path = strdup(cwd);
6422 if (repo_path == NULL) {
6423 error = got_error_from_errno("strdup");
6424 goto done;
6427 error = got_repo_open(&repo, repo_path, NULL);
6428 if (error != NULL)
6429 goto done;
6431 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6432 repo, worktree);
6433 if (error)
6434 goto done;
6436 error = tog_load_refs(repo);
6437 if (error)
6438 goto done;
6439 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6440 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6441 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6442 if (error)
6443 goto done;
6445 if (worktree) {
6446 got_worktree_close(worktree);
6447 worktree = NULL;
6450 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6451 if (error) {
6452 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6453 goto done;
6454 fprintf(stderr, "%s: '%s' is no known command or path\n",
6455 getprogname(), argv[0]);
6456 usage(1, 1);
6457 /* not reached */
6460 close_err = got_repo_close(repo);
6461 if (error == NULL)
6462 error = close_err;
6463 repo = NULL;
6465 error = got_object_id_str(&commit_id_str, commit_id);
6466 if (error)
6467 goto done;
6469 cmd = &tog_commands[0]; /* log */
6470 argc = 4;
6471 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6472 error = cmd->cmd_main(argc, cmd_argv);
6473 done:
6474 if (repo) {
6475 close_err = got_repo_close(repo);
6476 if (error == NULL)
6477 error = close_err;
6479 if (worktree)
6480 got_worktree_close(worktree);
6481 free(id);
6482 free(commit_id_str);
6483 free(commit_id);
6484 free(cwd);
6485 free(repo_path);
6486 free(in_repo_path);
6487 if (cmd_argv) {
6488 int i;
6489 for (i = 0; i < argc; i++)
6490 free(cmd_argv[i]);
6491 free(cmd_argv);
6493 tog_free_refs();
6494 return error;
6497 int
6498 main(int argc, char *argv[])
6500 const struct got_error *error = NULL;
6501 struct tog_cmd *cmd = NULL;
6502 int ch, hflag = 0, Vflag = 0;
6503 char **cmd_argv = NULL;
6504 static struct option longopts[] = {
6505 { "version", no_argument, NULL, 'V' },
6506 { NULL, 0, NULL, 0}
6509 setlocale(LC_CTYPE, "");
6511 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6512 switch (ch) {
6513 case 'h':
6514 hflag = 1;
6515 break;
6516 case 'V':
6517 Vflag = 1;
6518 break;
6519 default:
6520 usage(hflag, 1);
6521 /* NOTREACHED */
6525 argc -= optind;
6526 argv += optind;
6527 optind = 1;
6528 optreset = 1;
6530 if (Vflag) {
6531 got_version_print_str();
6532 return 0;
6535 #ifndef PROFILE
6536 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6537 NULL) == -1)
6538 err(1, "pledge");
6539 #endif
6541 if (argc == 0) {
6542 if (hflag)
6543 usage(hflag, 0);
6544 /* Build an argument vector which runs a default command. */
6545 cmd = &tog_commands[0];
6546 argc = 1;
6547 cmd_argv = make_argv(argc, cmd->name);
6548 } else {
6549 size_t i;
6551 /* Did the user specify a command? */
6552 for (i = 0; i < nitems(tog_commands); i++) {
6553 if (strncmp(tog_commands[i].name, argv[0],
6554 strlen(argv[0])) == 0) {
6555 cmd = &tog_commands[i];
6556 break;
6561 if (cmd == NULL) {
6562 if (argc != 1)
6563 usage(0, 1);
6564 /* No command specified; try log with a path */
6565 error = tog_log_with_path(argc, argv);
6566 } else {
6567 if (hflag)
6568 cmd->cmd_usage();
6569 else
6570 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6573 endwin();
6574 putchar('\n');
6575 if (cmd_argv) {
6576 int i;
6577 for (i = 0; i < argc; i++)
6578 free(cmd_argv[i]);
6579 free(cmd_argv);
6582 if (error && error->code != GOT_ERR_CANCELLED)
6583 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6584 return 0;