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 struct got_commit_graph *graph;
308 struct commit_queue *commits;
309 const char *in_repo_path;
310 struct got_object_id *start_id;
311 struct got_repository *repo;
312 int log_complete;
313 sig_atomic_t *quit;
314 struct commit_queue_entry **first_displayed_entry;
315 struct commit_queue_entry **selected_entry;
316 int *searching;
317 int *search_next_done;
318 regex_t *regex;
319 };
321 struct tog_log_view_state {
322 struct commit_queue commits;
323 struct commit_queue_entry *first_displayed_entry;
324 struct commit_queue_entry *last_displayed_entry;
325 struct commit_queue_entry *selected_entry;
326 int selected;
327 char *in_repo_path;
328 char *head_ref_name;
329 int log_branches;
330 struct got_repository *repo;
331 struct got_object_id *start_id;
332 sig_atomic_t quit;
333 pthread_t thread;
334 struct tog_log_thread_args thread_args;
335 struct commit_queue_entry *matched_entry;
336 struct commit_queue_entry *search_entry;
337 struct tog_colors colors;
338 };
340 #define TOG_COLOR_DIFF_MINUS 1
341 #define TOG_COLOR_DIFF_PLUS 2
342 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
343 #define TOG_COLOR_DIFF_META 4
344 #define TOG_COLOR_TREE_SUBMODULE 5
345 #define TOG_COLOR_TREE_SYMLINK 6
346 #define TOG_COLOR_TREE_DIRECTORY 7
347 #define TOG_COLOR_TREE_EXECUTABLE 8
348 #define TOG_COLOR_COMMIT 9
349 #define TOG_COLOR_AUTHOR 10
350 #define TOG_COLOR_DATE 11
351 #define TOG_COLOR_REFS_HEADS 12
352 #define TOG_COLOR_REFS_TAGS 13
353 #define TOG_COLOR_REFS_REMOTES 14
355 struct tog_blame_cb_args {
356 struct tog_blame_line *lines; /* one per line */
357 int nlines;
359 struct tog_view *view;
360 struct got_object_id *commit_id;
361 int *quit;
362 };
364 struct tog_blame_thread_args {
365 const char *path;
366 struct got_repository *repo;
367 struct tog_blame_cb_args *cb_args;
368 int *complete;
369 got_cancel_cb cancel_cb;
370 void *cancel_arg;
371 };
373 struct tog_blame {
374 FILE *f;
375 off_t filesize;
376 struct tog_blame_line *lines;
377 int nlines;
378 off_t *line_offsets;
379 pthread_t thread;
380 struct tog_blame_thread_args thread_args;
381 struct tog_blame_cb_args cb_args;
382 const char *path;
383 };
385 struct tog_blame_view_state {
386 int first_displayed_line;
387 int last_displayed_line;
388 int selected_line;
389 int blame_complete;
390 int eof;
391 int done;
392 struct got_object_id_queue blamed_commits;
393 struct got_object_qid *blamed_commit;
394 char *path;
395 struct got_repository *repo;
396 struct got_object_id *commit_id;
397 struct tog_blame blame;
398 int matched_line;
399 struct tog_colors colors;
400 };
402 struct tog_parent_tree {
403 TAILQ_ENTRY(tog_parent_tree) entry;
404 struct got_tree_object *tree;
405 struct got_tree_entry *first_displayed_entry;
406 struct got_tree_entry *selected_entry;
407 int selected;
408 };
410 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
412 struct tog_tree_view_state {
413 char *tree_label;
414 struct got_object_id *commit_id;/* commit which this tree belongs to */
415 struct got_tree_object *root; /* the commit's root tree entry */
416 struct got_tree_object *tree; /* currently displayed (sub-)tree */
417 struct got_tree_entry *first_displayed_entry;
418 struct got_tree_entry *last_displayed_entry;
419 struct got_tree_entry *selected_entry;
420 int ndisplayed, selected, show_ids;
421 struct tog_parent_trees parents; /* parent trees of current sub-tree */
422 char *head_ref_name;
423 struct got_repository *repo;
424 struct got_tree_entry *matched_entry;
425 struct tog_colors colors;
426 };
428 struct tog_reflist_entry {
429 TAILQ_ENTRY(tog_reflist_entry) entry;
430 struct got_reference *ref;
431 int idx;
432 };
434 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
436 struct tog_ref_view_state {
437 struct tog_reflist_head refs;
438 struct tog_reflist_entry *first_displayed_entry;
439 struct tog_reflist_entry *last_displayed_entry;
440 struct tog_reflist_entry *selected_entry;
441 int nrefs, ndisplayed, selected, show_ids;
442 struct got_repository *repo;
443 struct tog_reflist_entry *matched_entry;
444 struct tog_colors colors;
445 };
447 /*
448 * We implement two types of views: parent views and child views.
450 * The 'Tab' key switches focus between a parent view and its child view.
451 * Child views are shown side-by-side to their parent view, provided
452 * there is enough screen estate.
454 * When a new view is opened from within a parent view, this new view
455 * becomes a child view of the parent view, replacing any existing child.
457 * When a new view is opened from within a child view, this new view
458 * becomes a parent view which will obscure the views below until the
459 * user quits the new parent view by typing 'q'.
461 * This list of views contains parent views only.
462 * Child views are only pointed to by their parent view.
463 */
464 TAILQ_HEAD(tog_view_list_head, tog_view);
466 struct tog_view {
467 TAILQ_ENTRY(tog_view) entry;
468 WINDOW *window;
469 PANEL *panel;
470 int nlines, ncols, begin_y, begin_x;
471 int lines, cols; /* copies of LINES and COLS */
472 int focussed; /* Only set on one parent or child view at a time. */
473 int dying;
474 struct tog_view *parent;
475 struct tog_view *child;
477 /*
478 * This flag is initially set on parent views when a new child view
479 * is created. It gets toggled when the 'Tab' key switches focus
480 * between parent and child.
481 * The flag indicates whether focus should be passed on to our child
482 * view if this parent view gets picked for focus after another parent
483 * view was closed. This prevents child views from losing focus in such
484 * situations.
485 */
486 int focus_child;
488 /* type-specific state */
489 enum tog_view_type type;
490 union {
491 struct tog_diff_view_state diff;
492 struct tog_log_view_state log;
493 struct tog_blame_view_state blame;
494 struct tog_tree_view_state tree;
495 struct tog_ref_view_state ref;
496 } state;
498 const struct got_error *(*show)(struct tog_view *);
499 const struct got_error *(*input)(struct tog_view **,
500 struct tog_view *, int);
501 const struct got_error *(*close)(struct tog_view *);
503 const struct got_error *(*search_start)(struct tog_view *);
504 const struct got_error *(*search_next)(struct tog_view *);
505 int search_started;
506 int searching;
507 #define TOG_SEARCH_FORWARD 1
508 #define TOG_SEARCH_BACKWARD 2
509 int search_next_done;
510 #define TOG_SEARCH_HAVE_MORE 1
511 #define TOG_SEARCH_NO_MORE 2
512 #define TOG_SEARCH_HAVE_NONE 3
513 regex_t regex;
514 regmatch_t regmatch;
515 };
517 static const struct got_error *open_diff_view(struct tog_view *,
518 struct got_object_id *, struct got_object_id *,
519 const char *, const char *, int, int, int, struct tog_view *,
520 struct got_repository *);
521 static const struct got_error *show_diff_view(struct tog_view *);
522 static const struct got_error *input_diff_view(struct tog_view **,
523 struct tog_view *, int);
524 static const struct got_error* close_diff_view(struct tog_view *);
525 static const struct got_error *search_start_diff_view(struct tog_view *);
526 static const struct got_error *search_next_diff_view(struct tog_view *);
528 static const struct got_error *open_log_view(struct tog_view *,
529 struct got_object_id *, struct got_repository *,
530 const char *, const char *, int);
531 static const struct got_error * show_log_view(struct tog_view *);
532 static const struct got_error *input_log_view(struct tog_view **,
533 struct tog_view *, int);
534 static const struct got_error *close_log_view(struct tog_view *);
535 static const struct got_error *search_start_log_view(struct tog_view *);
536 static const struct got_error *search_next_log_view(struct tog_view *);
538 static const struct got_error *open_blame_view(struct tog_view *, char *,
539 struct got_object_id *, struct got_repository *);
540 static const struct got_error *show_blame_view(struct tog_view *);
541 static const struct got_error *input_blame_view(struct tog_view **,
542 struct tog_view *, int);
543 static const struct got_error *close_blame_view(struct tog_view *);
544 static const struct got_error *search_start_blame_view(struct tog_view *);
545 static const struct got_error *search_next_blame_view(struct tog_view *);
547 static const struct got_error *open_tree_view(struct tog_view *,
548 struct got_object_id *, const char *, struct got_repository *);
549 static const struct got_error *show_tree_view(struct tog_view *);
550 static const struct got_error *input_tree_view(struct tog_view **,
551 struct tog_view *, int);
552 static const struct got_error *close_tree_view(struct tog_view *);
553 static const struct got_error *search_start_tree_view(struct tog_view *);
554 static const struct got_error *search_next_tree_view(struct tog_view *);
556 static const struct got_error *open_ref_view(struct tog_view *,
557 struct got_repository *);
558 static const struct got_error *show_ref_view(struct tog_view *);
559 static const struct got_error *input_ref_view(struct tog_view **,
560 struct tog_view *, int);
561 static const struct got_error *close_ref_view(struct tog_view *);
562 static const struct got_error *search_start_ref_view(struct tog_view *);
563 static const struct got_error *search_next_ref_view(struct tog_view *);
565 static volatile sig_atomic_t tog_sigwinch_received;
566 static volatile sig_atomic_t tog_sigpipe_received;
567 static volatile sig_atomic_t tog_sigcont_received;
569 static void
570 tog_sigwinch(int signo)
572 tog_sigwinch_received = 1;
575 static void
576 tog_sigpipe(int signo)
578 tog_sigpipe_received = 1;
581 static void
582 tog_sigcont(int signo)
584 tog_sigcont_received = 1;
587 static const struct got_error *
588 view_close(struct tog_view *view)
590 const struct got_error *err = NULL;
592 if (view->child) {
593 view_close(view->child);
594 view->child = NULL;
596 if (view->close)
597 err = view->close(view);
598 if (view->panel)
599 del_panel(view->panel);
600 if (view->window)
601 delwin(view->window);
602 free(view);
603 return err;
606 static struct tog_view *
607 view_open(int nlines, int ncols, int begin_y, int begin_x,
608 enum tog_view_type type)
610 struct tog_view *view = calloc(1, sizeof(*view));
612 if (view == NULL)
613 return NULL;
615 view->type = type;
616 view->lines = LINES;
617 view->cols = COLS;
618 view->nlines = nlines ? nlines : LINES - begin_y;
619 view->ncols = ncols ? ncols : COLS - begin_x;
620 view->begin_y = begin_y;
621 view->begin_x = begin_x;
622 view->window = newwin(nlines, ncols, begin_y, begin_x);
623 if (view->window == NULL) {
624 view_close(view);
625 return NULL;
627 view->panel = new_panel(view->window);
628 if (view->panel == NULL ||
629 set_panel_userptr(view->panel, view) != OK) {
630 view_close(view);
631 return NULL;
634 keypad(view->window, TRUE);
635 return view;
638 static int
639 view_split_begin_x(int begin_x)
641 if (begin_x > 0 || COLS < 120)
642 return 0;
643 return (COLS - MAX(COLS / 2, 80));
646 static const struct got_error *view_resize(struct tog_view *);
648 static const struct got_error *
649 view_splitscreen(struct tog_view *view)
651 const struct got_error *err = NULL;
653 view->begin_y = 0;
654 view->begin_x = view_split_begin_x(0);
655 view->nlines = LINES;
656 view->ncols = COLS - view->begin_x;
657 view->lines = LINES;
658 view->cols = COLS;
659 err = view_resize(view);
660 if (err)
661 return err;
663 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
664 return got_error_from_errno("mvwin");
666 return NULL;
669 static const struct got_error *
670 view_fullscreen(struct tog_view *view)
672 const struct got_error *err = NULL;
674 view->begin_x = 0;
675 view->begin_y = 0;
676 view->nlines = LINES;
677 view->ncols = COLS;
678 view->lines = LINES;
679 view->cols = COLS;
680 err = view_resize(view);
681 if (err)
682 return err;
684 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
685 return got_error_from_errno("mvwin");
687 return NULL;
690 static int
691 view_is_parent_view(struct tog_view *view)
693 return view->parent == NULL;
696 static const struct got_error *
697 view_resize(struct tog_view *view)
699 int nlines, ncols;
701 if (view->lines > LINES)
702 nlines = view->nlines - (view->lines - LINES);
703 else
704 nlines = view->nlines + (LINES - view->lines);
706 if (view->cols > COLS)
707 ncols = view->ncols - (view->cols - COLS);
708 else
709 ncols = view->ncols + (COLS - view->cols);
711 if (wresize(view->window, nlines, ncols) == ERR)
712 return got_error_from_errno("wresize");
713 if (replace_panel(view->panel, view->window) == ERR)
714 return got_error_from_errno("replace_panel");
715 wclear(view->window);
717 view->nlines = nlines;
718 view->ncols = ncols;
719 view->lines = LINES;
720 view->cols = COLS;
722 if (view->child) {
723 view->child->begin_x = view_split_begin_x(view->begin_x);
724 if (view->child->begin_x == 0) {
725 view_fullscreen(view->child);
726 if (view->child->focussed)
727 show_panel(view->child->panel);
728 else
729 show_panel(view->panel);
730 } else {
731 view_splitscreen(view->child);
732 show_panel(view->child->panel);
736 return NULL;
739 static const struct got_error *
740 view_close_child(struct tog_view *view)
742 const struct got_error *err = NULL;
744 if (view->child == NULL)
745 return NULL;
747 err = view_close(view->child);
748 view->child = NULL;
749 return err;
752 static void
753 view_set_child(struct tog_view *view, struct tog_view *child)
755 view->child = child;
756 child->parent = view;
759 static int
760 view_is_splitscreen(struct tog_view *view)
762 return view->begin_x > 0;
765 static void
766 tog_resizeterm(void)
768 int cols, lines;
769 struct winsize size;
771 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
772 cols = 80; /* Default */
773 lines = 24;
774 } else {
775 cols = size.ws_col;
776 lines = size.ws_row;
778 resize_term(lines, cols);
781 static const struct got_error *
782 view_search_start(struct tog_view *view)
784 const struct got_error *err = NULL;
785 char pattern[1024];
786 int ret;
788 if (view->search_started) {
789 regfree(&view->regex);
790 view->searching = 0;
791 memset(&view->regmatch, 0, sizeof(view->regmatch));
793 view->search_started = 0;
795 if (view->nlines < 1)
796 return NULL;
798 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
799 wclrtoeol(view->window);
801 nocbreak();
802 echo();
803 ret = wgetnstr(view->window, pattern, sizeof(pattern));
804 cbreak();
805 noecho();
806 if (ret == ERR)
807 return NULL;
809 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
810 err = view->search_start(view);
811 if (err) {
812 regfree(&view->regex);
813 return err;
815 view->search_started = 1;
816 view->searching = TOG_SEARCH_FORWARD;
817 view->search_next_done = 0;
818 view->search_next(view);
821 return NULL;
824 static const struct got_error *
825 view_input(struct tog_view **new, int *done, struct tog_view *view,
826 struct tog_view_list_head *views)
828 const struct got_error *err = NULL;
829 struct tog_view *v;
830 int ch, errcode;
832 *new = NULL;
834 /* Clear "no matches" indicator. */
835 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
836 view->search_next_done == TOG_SEARCH_HAVE_NONE)
837 view->search_next_done = TOG_SEARCH_HAVE_MORE;
839 if (view->searching && !view->search_next_done) {
840 errcode = pthread_mutex_unlock(&tog_mutex);
841 if (errcode)
842 return got_error_set_errno(errcode,
843 "pthread_mutex_unlock");
844 pthread_yield();
845 errcode = pthread_mutex_lock(&tog_mutex);
846 if (errcode)
847 return got_error_set_errno(errcode,
848 "pthread_mutex_lock");
849 view->search_next(view);
850 return NULL;
853 nodelay(stdscr, FALSE);
854 /* Allow threads to make progress while we are waiting for input. */
855 errcode = pthread_mutex_unlock(&tog_mutex);
856 if (errcode)
857 return got_error_set_errno(errcode, "pthread_mutex_unlock");
858 ch = wgetch(view->window);
859 errcode = pthread_mutex_lock(&tog_mutex);
860 if (errcode)
861 return got_error_set_errno(errcode, "pthread_mutex_lock");
862 nodelay(stdscr, TRUE);
864 if (tog_sigwinch_received || tog_sigcont_received) {
865 tog_resizeterm();
866 tog_sigwinch_received = 0;
867 tog_sigcont_received = 0;
868 TAILQ_FOREACH(v, views, entry) {
869 err = view_resize(v);
870 if (err)
871 return err;
872 err = v->input(new, v, KEY_RESIZE);
873 if (err)
874 return err;
875 if (v->child) {
876 err = view_resize(v->child);
877 if (err)
878 return err;
879 err = v->child->input(new, v->child,
880 KEY_RESIZE);
881 if (err)
882 return err;
887 switch (ch) {
888 case ERR:
889 break;
890 case '\t':
891 if (view->child) {
892 view->focussed = 0;
893 view->child->focussed = 1;
894 view->focus_child = 1;
895 } else if (view->parent) {
896 view->focussed = 0;
897 view->parent->focussed = 1;
898 view->parent->focus_child = 0;
900 break;
901 case 'q':
902 err = view->input(new, view, ch);
903 view->dying = 1;
904 break;
905 case 'Q':
906 *done = 1;
907 break;
908 case 'f':
909 if (view_is_parent_view(view)) {
910 if (view->child == NULL)
911 break;
912 if (view_is_splitscreen(view->child)) {
913 view->focussed = 0;
914 view->child->focussed = 1;
915 err = view_fullscreen(view->child);
916 } else
917 err = view_splitscreen(view->child);
918 if (err)
919 break;
920 err = view->child->input(new, view->child,
921 KEY_RESIZE);
922 } else {
923 if (view_is_splitscreen(view)) {
924 view->parent->focussed = 0;
925 view->focussed = 1;
926 err = view_fullscreen(view);
927 } else {
928 err = view_splitscreen(view);
930 if (err)
931 break;
932 err = view->input(new, view, KEY_RESIZE);
934 break;
935 case KEY_RESIZE:
936 break;
937 case '/':
938 if (view->search_start)
939 view_search_start(view);
940 else
941 err = view->input(new, view, ch);
942 break;
943 case 'N':
944 case 'n':
945 if (view->search_started && view->search_next) {
946 view->searching = (ch == 'n' ?
947 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
948 view->search_next_done = 0;
949 view->search_next(view);
950 } else
951 err = view->input(new, view, ch);
952 break;
953 default:
954 err = view->input(new, view, ch);
955 break;
958 return err;
961 void
962 view_vborder(struct tog_view *view)
964 PANEL *panel;
965 const struct tog_view *view_above;
967 if (view->parent)
968 return view_vborder(view->parent);
970 panel = panel_above(view->panel);
971 if (panel == NULL)
972 return;
974 view_above = panel_userptr(panel);
975 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
976 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
979 int
980 view_needs_focus_indication(struct tog_view *view)
982 if (view_is_parent_view(view)) {
983 if (view->child == NULL || view->child->focussed)
984 return 0;
985 if (!view_is_splitscreen(view->child))
986 return 0;
987 } else if (!view_is_splitscreen(view))
988 return 0;
990 return view->focussed;
993 static const struct got_error *
994 view_loop(struct tog_view *view)
996 const struct got_error *err = NULL;
997 struct tog_view_list_head views;
998 struct tog_view *new_view;
999 int fast_refresh = 10;
1000 int done = 0, errcode;
1002 errcode = pthread_mutex_lock(&tog_mutex);
1003 if (errcode)
1004 return got_error_set_errno(errcode, "pthread_mutex_lock");
1006 TAILQ_INIT(&views);
1007 TAILQ_INSERT_HEAD(&views, view, entry);
1009 view->focussed = 1;
1010 err = view->show(view);
1011 if (err)
1012 return err;
1013 update_panels();
1014 doupdate();
1015 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1016 /* Refresh fast during initialization, then become slower. */
1017 if (fast_refresh && fast_refresh-- == 0)
1018 halfdelay(10); /* switch to once per second */
1020 err = view_input(&new_view, &done, view, &views);
1021 if (err)
1022 break;
1023 if (view->dying) {
1024 struct tog_view *v, *prev = NULL;
1026 if (view_is_parent_view(view))
1027 prev = TAILQ_PREV(view, tog_view_list_head,
1028 entry);
1029 else if (view->parent)
1030 prev = view->parent;
1032 if (view->parent) {
1033 view->parent->child = NULL;
1034 view->parent->focus_child = 0;
1035 } else
1036 TAILQ_REMOVE(&views, view, entry);
1038 err = view_close(view);
1039 if (err)
1040 goto done;
1042 view = NULL;
1043 TAILQ_FOREACH(v, &views, entry) {
1044 if (v->focussed)
1045 break;
1047 if (view == NULL && new_view == NULL) {
1048 /* No view has focus. Try to pick one. */
1049 if (prev)
1050 view = prev;
1051 else if (!TAILQ_EMPTY(&views)) {
1052 view = TAILQ_LAST(&views,
1053 tog_view_list_head);
1055 if (view) {
1056 if (view->focus_child) {
1057 view->child->focussed = 1;
1058 view = view->child;
1059 } else
1060 view->focussed = 1;
1064 if (new_view) {
1065 struct tog_view *v, *t;
1066 /* Only allow one parent view per type. */
1067 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1068 if (v->type != new_view->type)
1069 continue;
1070 TAILQ_REMOVE(&views, v, entry);
1071 err = view_close(v);
1072 if (err)
1073 goto done;
1074 break;
1076 TAILQ_INSERT_TAIL(&views, new_view, entry);
1077 view = new_view;
1079 if (view) {
1080 if (view_is_parent_view(view)) {
1081 if (view->child && view->child->focussed)
1082 view = view->child;
1083 } else {
1084 if (view->parent && view->parent->focussed)
1085 view = view->parent;
1087 show_panel(view->panel);
1088 if (view->child && view_is_splitscreen(view->child))
1089 show_panel(view->child->panel);
1090 if (view->parent && view_is_splitscreen(view)) {
1091 err = view->parent->show(view->parent);
1092 if (err)
1093 goto done;
1095 err = view->show(view);
1096 if (err)
1097 goto done;
1098 if (view->child) {
1099 err = view->child->show(view->child);
1100 if (err)
1101 goto done;
1103 update_panels();
1104 doupdate();
1107 done:
1108 while (!TAILQ_EMPTY(&views)) {
1109 view = TAILQ_FIRST(&views);
1110 TAILQ_REMOVE(&views, view, entry);
1111 view_close(view);
1114 errcode = pthread_mutex_unlock(&tog_mutex);
1115 if (errcode && err == NULL)
1116 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1118 return err;
1121 __dead static void
1122 usage_log(void)
1124 endwin();
1125 fprintf(stderr,
1126 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1127 getprogname());
1128 exit(1);
1131 /* Create newly allocated wide-character string equivalent to a byte string. */
1132 static const struct got_error *
1133 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1135 char *vis = NULL;
1136 const struct got_error *err = NULL;
1138 *ws = NULL;
1139 *wlen = mbstowcs(NULL, s, 0);
1140 if (*wlen == (size_t)-1) {
1141 int vislen;
1142 if (errno != EILSEQ)
1143 return got_error_from_errno("mbstowcs");
1145 /* byte string invalid in current encoding; try to "fix" it */
1146 err = got_mbsavis(&vis, &vislen, s);
1147 if (err)
1148 return err;
1149 *wlen = mbstowcs(NULL, vis, 0);
1150 if (*wlen == (size_t)-1) {
1151 err = got_error_from_errno("mbstowcs"); /* give up */
1152 goto done;
1156 *ws = calloc(*wlen + 1, sizeof(**ws));
1157 if (*ws == NULL) {
1158 err = got_error_from_errno("calloc");
1159 goto done;
1162 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1163 err = got_error_from_errno("mbstowcs");
1164 done:
1165 free(vis);
1166 if (err) {
1167 free(*ws);
1168 *ws = NULL;
1169 *wlen = 0;
1171 return err;
1174 /* Format a line for display, ensuring that it won't overflow a width limit. */
1175 static const struct got_error *
1176 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1177 int col_tab_align)
1179 const struct got_error *err = NULL;
1180 int cols = 0;
1181 wchar_t *wline = NULL;
1182 size_t wlen;
1183 int i;
1185 *wlinep = NULL;
1186 *widthp = 0;
1188 err = mbs2ws(&wline, &wlen, line);
1189 if (err)
1190 return err;
1192 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1193 wline[wlen - 1] = L'\0';
1194 wlen--;
1196 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1197 wline[wlen - 1] = L'\0';
1198 wlen--;
1201 i = 0;
1202 while (i < wlen) {
1203 int width = wcwidth(wline[i]);
1205 if (width == 0) {
1206 i++;
1207 continue;
1210 if (width == 1 || width == 2) {
1211 if (cols + width > wlimit)
1212 break;
1213 cols += width;
1214 i++;
1215 } else if (width == -1) {
1216 if (wline[i] == L'\t') {
1217 width = TABSIZE -
1218 ((cols + col_tab_align) % TABSIZE);
1219 } else {
1220 width = 1;
1221 wline[i] = L'.';
1223 if (cols + width > wlimit)
1224 break;
1225 cols += width;
1226 i++;
1227 } else {
1228 err = got_error_from_errno("wcwidth");
1229 goto done;
1232 wline[i] = L'\0';
1233 if (widthp)
1234 *widthp = cols;
1235 done:
1236 if (err)
1237 free(wline);
1238 else
1239 *wlinep = wline;
1240 return err;
1243 static const struct got_error*
1244 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1245 struct got_object_id *id, struct got_repository *repo)
1247 static const struct got_error *err = NULL;
1248 struct got_reflist_entry *re;
1249 char *s;
1250 const char *name;
1252 *refs_str = NULL;
1254 TAILQ_FOREACH(re, refs, entry) {
1255 struct got_tag_object *tag = NULL;
1256 struct got_object_id *ref_id;
1257 int cmp;
1259 name = got_ref_get_name(re->ref);
1260 if (strcmp(name, GOT_REF_HEAD) == 0)
1261 continue;
1262 if (strncmp(name, "refs/", 5) == 0)
1263 name += 5;
1264 if (strncmp(name, "got/", 4) == 0)
1265 continue;
1266 if (strncmp(name, "heads/", 6) == 0)
1267 name += 6;
1268 if (strncmp(name, "remotes/", 8) == 0) {
1269 name += 8;
1270 s = strstr(name, "/" GOT_REF_HEAD);
1271 if (s != NULL && s[strlen(s)] == '\0')
1272 continue;
1274 err = got_ref_resolve(&ref_id, repo, re->ref);
1275 if (err)
1276 break;
1277 if (strncmp(name, "tags/", 5) == 0) {
1278 err = got_object_open_as_tag(&tag, repo, ref_id);
1279 if (err) {
1280 if (err->code != GOT_ERR_OBJ_TYPE) {
1281 free(ref_id);
1282 break;
1284 /* Ref points at something other than a tag. */
1285 err = NULL;
1286 tag = NULL;
1289 cmp = got_object_id_cmp(tag ?
1290 got_object_tag_get_object_id(tag) : ref_id, id);
1291 free(ref_id);
1292 if (tag)
1293 got_object_tag_close(tag);
1294 if (cmp != 0)
1295 continue;
1296 s = *refs_str;
1297 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1298 s ? ", " : "", name) == -1) {
1299 err = got_error_from_errno("asprintf");
1300 free(s);
1301 *refs_str = NULL;
1302 break;
1304 free(s);
1307 return err;
1310 static const struct got_error *
1311 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1312 int col_tab_align)
1314 char *smallerthan;
1316 smallerthan = strchr(author, '<');
1317 if (smallerthan && smallerthan[1] != '\0')
1318 author = smallerthan + 1;
1319 author[strcspn(author, "@>")] = '\0';
1320 return format_line(wauthor, author_width, author, limit, col_tab_align);
1323 static const struct got_error *
1324 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1325 struct got_object_id *id, const size_t date_display_cols,
1326 int author_display_cols)
1328 struct tog_log_view_state *s = &view->state.log;
1329 const struct got_error *err = NULL;
1330 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1331 char *logmsg0 = NULL, *logmsg = NULL;
1332 char *author = NULL;
1333 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1334 int author_width, logmsg_width;
1335 char *newline, *line = NULL;
1336 int col, limit;
1337 const int avail = view->ncols;
1338 struct tm tm;
1339 time_t committer_time;
1340 struct tog_color *tc;
1342 committer_time = got_object_commit_get_committer_time(commit);
1343 if (localtime_r(&committer_time, &tm) == NULL)
1344 return got_error_from_errno("localtime_r");
1345 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1346 return got_error(GOT_ERR_NO_SPACE);
1348 if (avail <= date_display_cols)
1349 limit = MIN(sizeof(datebuf) - 1, avail);
1350 else
1351 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1352 tc = get_color(&s->colors, TOG_COLOR_DATE);
1353 if (tc)
1354 wattr_on(view->window,
1355 COLOR_PAIR(tc->colorpair), NULL);
1356 waddnstr(view->window, datebuf, limit);
1357 if (tc)
1358 wattr_off(view->window,
1359 COLOR_PAIR(tc->colorpair), NULL);
1360 col = limit;
1361 if (col > avail)
1362 goto done;
1364 if (avail >= 120) {
1365 char *id_str;
1366 err = got_object_id_str(&id_str, id);
1367 if (err)
1368 goto done;
1369 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1370 if (tc)
1371 wattr_on(view->window,
1372 COLOR_PAIR(tc->colorpair), NULL);
1373 wprintw(view->window, "%.8s ", id_str);
1374 if (tc)
1375 wattr_off(view->window,
1376 COLOR_PAIR(tc->colorpair), NULL);
1377 free(id_str);
1378 col += 9;
1379 if (col > avail)
1380 goto done;
1383 author = strdup(got_object_commit_get_author(commit));
1384 if (author == NULL) {
1385 err = got_error_from_errno("strdup");
1386 goto done;
1388 err = format_author(&wauthor, &author_width, author, avail - col, col);
1389 if (err)
1390 goto done;
1391 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1392 if (tc)
1393 wattr_on(view->window,
1394 COLOR_PAIR(tc->colorpair), NULL);
1395 waddwstr(view->window, wauthor);
1396 if (tc)
1397 wattr_off(view->window,
1398 COLOR_PAIR(tc->colorpair), NULL);
1399 col += author_width;
1400 while (col < avail && author_width < author_display_cols + 2) {
1401 waddch(view->window, ' ');
1402 col++;
1403 author_width++;
1405 if (col > avail)
1406 goto done;
1408 err = got_object_commit_get_logmsg(&logmsg0, commit);
1409 if (err)
1410 goto done;
1411 logmsg = logmsg0;
1412 while (*logmsg == '\n')
1413 logmsg++;
1414 newline = strchr(logmsg, '\n');
1415 if (newline)
1416 *newline = '\0';
1417 limit = avail - col;
1418 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1419 if (err)
1420 goto done;
1421 waddwstr(view->window, wlogmsg);
1422 col += logmsg_width;
1423 while (col < avail) {
1424 waddch(view->window, ' ');
1425 col++;
1427 done:
1428 free(logmsg0);
1429 free(wlogmsg);
1430 free(author);
1431 free(wauthor);
1432 free(line);
1433 return err;
1436 static struct commit_queue_entry *
1437 alloc_commit_queue_entry(struct got_commit_object *commit,
1438 struct got_object_id *id)
1440 struct commit_queue_entry *entry;
1442 entry = calloc(1, sizeof(*entry));
1443 if (entry == NULL)
1444 return NULL;
1446 entry->id = id;
1447 entry->commit = commit;
1448 return entry;
1451 static void
1452 pop_commit(struct commit_queue *commits)
1454 struct commit_queue_entry *entry;
1456 entry = TAILQ_FIRST(&commits->head);
1457 TAILQ_REMOVE(&commits->head, entry, entry);
1458 got_object_commit_close(entry->commit);
1459 commits->ncommits--;
1460 /* Don't free entry->id! It is owned by the commit graph. */
1461 free(entry);
1464 static void
1465 free_commits(struct commit_queue *commits)
1467 while (!TAILQ_EMPTY(&commits->head))
1468 pop_commit(commits);
1471 static const struct got_error *
1472 match_commit(int *have_match, struct got_object_id *id,
1473 struct got_commit_object *commit, regex_t *regex)
1475 const struct got_error *err = NULL;
1476 regmatch_t regmatch;
1477 char *id_str = NULL, *logmsg = NULL;
1479 *have_match = 0;
1481 err = got_object_id_str(&id_str, id);
1482 if (err)
1483 return err;
1485 err = got_object_commit_get_logmsg(&logmsg, commit);
1486 if (err)
1487 goto done;
1489 if (regexec(regex, got_object_commit_get_author(commit), 1,
1490 &regmatch, 0) == 0 ||
1491 regexec(regex, got_object_commit_get_committer(commit), 1,
1492 &regmatch, 0) == 0 ||
1493 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1494 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1495 *have_match = 1;
1496 done:
1497 free(id_str);
1498 free(logmsg);
1499 return err;
1502 static const struct got_error *
1503 queue_commits(struct tog_log_thread_args *a)
1505 const struct got_error *err = NULL;
1508 * We keep all commits open throughout the lifetime of the log
1509 * view in order to avoid having to re-fetch commits from disk
1510 * while updating the display.
1512 do {
1513 struct got_object_id *id;
1514 struct got_commit_object *commit;
1515 struct commit_queue_entry *entry;
1516 int errcode;
1518 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1519 NULL, NULL);
1520 if (err || id == NULL)
1521 break;
1523 err = got_object_open_as_commit(&commit, a->repo, id);
1524 if (err)
1525 break;
1526 entry = alloc_commit_queue_entry(commit, id);
1527 if (entry == NULL) {
1528 err = got_error_from_errno("alloc_commit_queue_entry");
1529 break;
1532 errcode = pthread_mutex_lock(&tog_mutex);
1533 if (errcode) {
1534 err = got_error_set_errno(errcode,
1535 "pthread_mutex_lock");
1536 break;
1539 entry->idx = a->commits->ncommits;
1540 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1541 a->commits->ncommits++;
1543 if (*a->searching == TOG_SEARCH_FORWARD &&
1544 !*a->search_next_done) {
1545 int have_match;
1546 err = match_commit(&have_match, id, commit, a->regex);
1547 if (err)
1548 break;
1549 if (have_match)
1550 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1553 errcode = pthread_mutex_unlock(&tog_mutex);
1554 if (errcode && err == NULL)
1555 err = got_error_set_errno(errcode,
1556 "pthread_mutex_unlock");
1557 if (err)
1558 break;
1559 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1561 return err;
1564 static void
1565 select_commit(struct tog_log_view_state *s)
1567 struct commit_queue_entry *entry;
1568 int ncommits = 0;
1570 entry = s->first_displayed_entry;
1571 while (entry) {
1572 if (ncommits == s->selected) {
1573 s->selected_entry = entry;
1574 break;
1576 entry = TAILQ_NEXT(entry, entry);
1577 ncommits++;
1581 static const struct got_error *
1582 draw_commits(struct tog_view *view)
1584 const struct got_error *err = NULL;
1585 struct tog_log_view_state *s = &view->state.log;
1586 struct commit_queue_entry *entry = s->selected_entry;
1587 const int limit = view->nlines;
1588 int width;
1589 int ncommits, author_cols = 4;
1590 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1591 char *refs_str = NULL;
1592 wchar_t *wline;
1593 struct tog_color *tc;
1594 static const size_t date_display_cols = 12;
1596 if (s->selected_entry &&
1597 !(view->searching && view->search_next_done == 0)) {
1598 struct got_reflist_head *refs;
1599 err = got_object_id_str(&id_str, s->selected_entry->id);
1600 if (err)
1601 return err;
1602 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1603 s->selected_entry->id);
1604 if (refs) {
1605 err = build_refs_str(&refs_str, refs,
1606 s->selected_entry->id, s->repo);
1607 if (err)
1608 goto done;
1612 if (s->thread_args.commits_needed == 0)
1613 halfdelay(10); /* disable fast refresh */
1615 if (s->thread_args.commits_needed > 0) {
1616 if (asprintf(&ncommits_str, " [%d/%d] %s",
1617 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1618 (view->searching && !view->search_next_done) ?
1619 "searching..." : "loading...") == -1) {
1620 err = got_error_from_errno("asprintf");
1621 goto done;
1623 } else {
1624 const char *search_str = NULL;
1626 if (view->searching) {
1627 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1628 search_str = "no more matches";
1629 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1630 search_str = "no matches found";
1631 else if (!view->search_next_done)
1632 search_str = "searching...";
1635 if (asprintf(&ncommits_str, " [%d/%d] %s",
1636 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1637 search_str ? search_str :
1638 (refs_str ? refs_str : "")) == -1) {
1639 err = got_error_from_errno("asprintf");
1640 goto done;
1644 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1645 if (asprintf(&header, "commit %s %s%s",
1646 id_str ? id_str : "........................................",
1647 s->in_repo_path, ncommits_str) == -1) {
1648 err = got_error_from_errno("asprintf");
1649 header = NULL;
1650 goto done;
1652 } else if (asprintf(&header, "commit %s%s",
1653 id_str ? id_str : "........................................",
1654 ncommits_str) == -1) {
1655 err = got_error_from_errno("asprintf");
1656 header = NULL;
1657 goto done;
1659 err = format_line(&wline, &width, header, view->ncols, 0);
1660 if (err)
1661 goto done;
1663 werase(view->window);
1665 if (view_needs_focus_indication(view))
1666 wstandout(view->window);
1667 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1668 if (tc)
1669 wattr_on(view->window,
1670 COLOR_PAIR(tc->colorpair), NULL);
1671 waddwstr(view->window, wline);
1672 if (tc)
1673 wattr_off(view->window,
1674 COLOR_PAIR(tc->colorpair), NULL);
1675 while (width < view->ncols) {
1676 waddch(view->window, ' ');
1677 width++;
1679 if (view_needs_focus_indication(view))
1680 wstandend(view->window);
1681 free(wline);
1682 if (limit <= 1)
1683 goto done;
1685 /* Grow author column size if necessary. */
1686 entry = s->first_displayed_entry;
1687 ncommits = 0;
1688 while (entry) {
1689 char *author;
1690 wchar_t *wauthor;
1691 int width;
1692 if (ncommits >= limit - 1)
1693 break;
1694 author = strdup(got_object_commit_get_author(entry->commit));
1695 if (author == NULL) {
1696 err = got_error_from_errno("strdup");
1697 goto done;
1699 err = format_author(&wauthor, &width, author, COLS,
1700 date_display_cols);
1701 if (author_cols < width)
1702 author_cols = width;
1703 free(wauthor);
1704 free(author);
1705 ncommits++;
1706 entry = TAILQ_NEXT(entry, entry);
1709 entry = s->first_displayed_entry;
1710 s->last_displayed_entry = s->first_displayed_entry;
1711 ncommits = 0;
1712 while (entry) {
1713 if (ncommits >= limit - 1)
1714 break;
1715 if (ncommits == s->selected)
1716 wstandout(view->window);
1717 err = draw_commit(view, entry->commit, entry->id,
1718 date_display_cols, author_cols);
1719 if (ncommits == s->selected)
1720 wstandend(view->window);
1721 if (err)
1722 goto done;
1723 ncommits++;
1724 s->last_displayed_entry = entry;
1725 entry = TAILQ_NEXT(entry, entry);
1728 view_vborder(view);
1729 done:
1730 free(id_str);
1731 free(refs_str);
1732 free(ncommits_str);
1733 free(header);
1734 return err;
1737 static void
1738 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1740 struct commit_queue_entry *entry;
1741 int nscrolled = 0;
1743 entry = TAILQ_FIRST(&s->commits.head);
1744 if (s->first_displayed_entry == entry)
1745 return;
1747 entry = s->first_displayed_entry;
1748 while (entry && nscrolled < maxscroll) {
1749 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1750 if (entry) {
1751 s->first_displayed_entry = entry;
1752 nscrolled++;
1757 static const struct got_error *
1758 trigger_log_thread(struct tog_view *view, int wait)
1760 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1761 int errcode;
1763 halfdelay(1); /* fast refresh while loading commits */
1765 while (ta->commits_needed > 0) {
1766 if (ta->log_complete)
1767 break;
1769 /* Wake the log thread. */
1770 errcode = pthread_cond_signal(&ta->need_commits);
1771 if (errcode)
1772 return got_error_set_errno(errcode,
1773 "pthread_cond_signal");
1776 * The mutex will be released while the view loop waits
1777 * in wgetch(), at which time the log thread will run.
1779 if (!wait)
1780 break;
1782 /* Display progress update in log view. */
1783 show_log_view(view);
1784 update_panels();
1785 doupdate();
1787 /* Wait right here while next commit is being loaded. */
1788 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1789 if (errcode)
1790 return got_error_set_errno(errcode,
1791 "pthread_cond_wait");
1793 /* Display progress update in log view. */
1794 show_log_view(view);
1795 update_panels();
1796 doupdate();
1799 return NULL;
1802 static const struct got_error *
1803 log_scroll_down(struct tog_view *view, int maxscroll)
1805 struct tog_log_view_state *s = &view->state.log;
1806 const struct got_error *err = NULL;
1807 struct commit_queue_entry *pentry;
1808 int nscrolled = 0, ncommits_needed;
1810 if (s->last_displayed_entry == NULL)
1811 return NULL;
1813 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1814 if (s->commits.ncommits < ncommits_needed &&
1815 !s->thread_args.log_complete) {
1817 * Ask the log thread for required amount of commits.
1819 s->thread_args.commits_needed += maxscroll;
1820 err = trigger_log_thread(view, 1);
1821 if (err)
1822 return err;
1825 do {
1826 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1827 if (pentry == NULL)
1828 break;
1830 s->last_displayed_entry = pentry;
1832 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1833 if (pentry == NULL)
1834 break;
1835 s->first_displayed_entry = pentry;
1836 } while (++nscrolled < maxscroll);
1838 return err;
1841 static const struct got_error *
1842 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1843 struct got_commit_object *commit, struct got_object_id *commit_id,
1844 struct tog_view *log_view, struct got_repository *repo)
1846 const struct got_error *err;
1847 struct got_object_qid *parent_id;
1848 struct tog_view *diff_view;
1850 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1851 if (diff_view == NULL)
1852 return got_error_from_errno("view_open");
1854 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1855 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1856 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1857 if (err == NULL)
1858 *new_view = diff_view;
1859 return err;
1862 static const struct got_error *
1863 tree_view_visit_subtree(struct tog_tree_view_state *s,
1864 struct got_tree_object *subtree)
1866 struct tog_parent_tree *parent;
1868 parent = calloc(1, sizeof(*parent));
1869 if (parent == NULL)
1870 return got_error_from_errno("calloc");
1872 parent->tree = s->tree;
1873 parent->first_displayed_entry = s->first_displayed_entry;
1874 parent->selected_entry = s->selected_entry;
1875 parent->selected = s->selected;
1876 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1877 s->tree = subtree;
1878 s->selected = 0;
1879 s->first_displayed_entry = NULL;
1880 return NULL;
1883 static const struct got_error *
1884 tree_view_walk_path(struct tog_tree_view_state *s,
1885 struct got_object_id *commit_id, const char *path)
1887 const struct got_error *err = NULL;
1888 struct got_tree_object *tree = NULL;
1889 const char *p;
1890 char *slash, *subpath = NULL;
1892 /* Walk the path and open corresponding tree objects. */
1893 p = path;
1894 while (*p) {
1895 struct got_tree_entry *te;
1896 struct got_object_id *tree_id;
1897 char *te_name;
1899 while (p[0] == '/')
1900 p++;
1902 /* Ensure the correct subtree entry is selected. */
1903 slash = strchr(p, '/');
1904 if (slash == NULL)
1905 te_name = strdup(p);
1906 else
1907 te_name = strndup(p, slash - p);
1908 if (te_name == NULL) {
1909 err = got_error_from_errno("strndup");
1910 break;
1912 te = got_object_tree_find_entry(s->tree, te_name);
1913 if (te == NULL) {
1914 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1915 free(te_name);
1916 break;
1918 free(te_name);
1919 s->first_displayed_entry = s->selected_entry = te;
1921 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1922 break; /* jump to this file's entry */
1924 slash = strchr(p, '/');
1925 if (slash)
1926 subpath = strndup(path, slash - path);
1927 else
1928 subpath = strdup(path);
1929 if (subpath == NULL) {
1930 err = got_error_from_errno("strdup");
1931 break;
1934 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1935 subpath);
1936 if (err)
1937 break;
1939 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1940 free(tree_id);
1941 if (err)
1942 break;
1944 err = tree_view_visit_subtree(s, tree);
1945 if (err) {
1946 got_object_tree_close(tree);
1947 break;
1949 if (slash == NULL)
1950 break;
1951 free(subpath);
1952 subpath = NULL;
1953 p = slash;
1956 free(subpath);
1957 return err;
1960 static const struct got_error *
1961 browse_commit_tree(struct tog_view **new_view, int begin_x,
1962 struct commit_queue_entry *entry, const char *path,
1963 const char *head_ref_name, struct got_repository *repo)
1965 const struct got_error *err = NULL;
1966 struct tog_tree_view_state *s;
1967 struct tog_view *tree_view;
1969 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1970 if (tree_view == NULL)
1971 return got_error_from_errno("view_open");
1973 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1974 if (err)
1975 return err;
1976 s = &tree_view->state.tree;
1978 *new_view = tree_view;
1980 if (got_path_is_root_dir(path))
1981 return NULL;
1983 return tree_view_walk_path(s, entry->id, path);
1986 static const struct got_error *
1987 block_signals_used_by_main_thread(void)
1989 sigset_t sigset;
1990 int errcode;
1992 if (sigemptyset(&sigset) == -1)
1993 return got_error_from_errno("sigemptyset");
1995 /* tog handles SIGWINCH and SIGCONT */
1996 if (sigaddset(&sigset, SIGWINCH) == -1)
1997 return got_error_from_errno("sigaddset");
1998 if (sigaddset(&sigset, SIGCONT) == -1)
1999 return got_error_from_errno("sigaddset");
2001 /* ncurses handles SIGTSTP */
2002 if (sigaddset(&sigset, SIGTSTP) == -1)
2003 return got_error_from_errno("sigaddset");
2005 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2006 if (errcode)
2007 return got_error_set_errno(errcode, "pthread_sigmask");
2009 return NULL;
2012 static void *
2013 log_thread(void *arg)
2015 const struct got_error *err = NULL;
2016 int errcode = 0;
2017 struct tog_log_thread_args *a = arg;
2018 int done = 0;
2020 err = block_signals_used_by_main_thread();
2021 if (err)
2022 return (void *)err;
2024 while (!done && !err && !tog_sigpipe_received) {
2025 err = queue_commits(a);
2026 if (err) {
2027 if (err->code != GOT_ERR_ITER_COMPLETED)
2028 return (void *)err;
2029 err = NULL;
2030 done = 1;
2031 } else if (a->commits_needed > 0)
2032 a->commits_needed--;
2034 errcode = pthread_mutex_lock(&tog_mutex);
2035 if (errcode) {
2036 err = got_error_set_errno(errcode,
2037 "pthread_mutex_lock");
2038 break;
2039 } else if (*a->quit)
2040 done = 1;
2041 else if (*a->first_displayed_entry == NULL) {
2042 *a->first_displayed_entry =
2043 TAILQ_FIRST(&a->commits->head);
2044 *a->selected_entry = *a->first_displayed_entry;
2047 errcode = pthread_cond_signal(&a->commit_loaded);
2048 if (errcode) {
2049 err = got_error_set_errno(errcode,
2050 "pthread_cond_signal");
2051 pthread_mutex_unlock(&tog_mutex);
2052 break;
2055 if (done)
2056 a->commits_needed = 0;
2057 else {
2058 if (a->commits_needed == 0) {
2059 errcode = pthread_cond_wait(&a->need_commits,
2060 &tog_mutex);
2061 if (errcode)
2062 err = got_error_set_errno(errcode,
2063 "pthread_cond_wait");
2064 if (*a->quit)
2065 done = 1;
2069 errcode = pthread_mutex_unlock(&tog_mutex);
2070 if (errcode && err == NULL)
2071 err = got_error_set_errno(errcode,
2072 "pthread_mutex_unlock");
2074 a->log_complete = 1;
2075 return (void *)err;
2078 static const struct got_error *
2079 stop_log_thread(struct tog_log_view_state *s)
2081 const struct got_error *err = NULL;
2082 int errcode;
2084 if (s->thread) {
2085 s->quit = 1;
2086 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2087 if (errcode)
2088 return got_error_set_errno(errcode,
2089 "pthread_cond_signal");
2090 errcode = pthread_mutex_unlock(&tog_mutex);
2091 if (errcode)
2092 return got_error_set_errno(errcode,
2093 "pthread_mutex_unlock");
2094 errcode = pthread_join(s->thread, (void **)&err);
2095 if (errcode)
2096 return got_error_set_errno(errcode, "pthread_join");
2097 errcode = pthread_mutex_lock(&tog_mutex);
2098 if (errcode)
2099 return got_error_set_errno(errcode,
2100 "pthread_mutex_lock");
2101 s->thread = NULL;
2104 if (s->thread_args.repo) {
2105 err = got_repo_close(s->thread_args.repo);
2106 s->thread_args.repo = NULL;
2109 if (s->thread_args.graph) {
2110 got_commit_graph_close(s->thread_args.graph);
2111 s->thread_args.graph = NULL;
2114 return err;
2117 static const struct got_error *
2118 close_log_view(struct tog_view *view)
2120 const struct got_error *err = NULL;
2121 struct tog_log_view_state *s = &view->state.log;
2122 int errcode;
2124 err = stop_log_thread(s);
2126 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2127 if (errcode && err == NULL)
2128 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2130 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2131 if (errcode && err == NULL)
2132 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2134 free_commits(&s->commits);
2135 free(s->in_repo_path);
2136 s->in_repo_path = NULL;
2137 free(s->start_id);
2138 s->start_id = NULL;
2139 free(s->head_ref_name);
2140 s->head_ref_name = NULL;
2141 return err;
2144 static const struct got_error *
2145 search_start_log_view(struct tog_view *view)
2147 struct tog_log_view_state *s = &view->state.log;
2149 s->matched_entry = NULL;
2150 s->search_entry = NULL;
2151 return NULL;
2154 static const struct got_error *
2155 search_next_log_view(struct tog_view *view)
2157 const struct got_error *err = NULL;
2158 struct tog_log_view_state *s = &view->state.log;
2159 struct commit_queue_entry *entry;
2161 /* Display progress update in log view. */
2162 show_log_view(view);
2163 update_panels();
2164 doupdate();
2166 if (s->search_entry) {
2167 int errcode, ch;
2168 errcode = pthread_mutex_unlock(&tog_mutex);
2169 if (errcode)
2170 return got_error_set_errno(errcode,
2171 "pthread_mutex_unlock");
2172 ch = wgetch(view->window);
2173 errcode = pthread_mutex_lock(&tog_mutex);
2174 if (errcode)
2175 return got_error_set_errno(errcode,
2176 "pthread_mutex_lock");
2177 if (ch == KEY_BACKSPACE) {
2178 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2179 return NULL;
2181 if (view->searching == TOG_SEARCH_FORWARD)
2182 entry = TAILQ_NEXT(s->search_entry, entry);
2183 else
2184 entry = TAILQ_PREV(s->search_entry,
2185 commit_queue_head, entry);
2186 } else if (s->matched_entry) {
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(s->matched_entry, entry);
2189 else
2190 entry = TAILQ_PREV(s->matched_entry,
2191 commit_queue_head, entry);
2192 } else {
2193 if (view->searching == TOG_SEARCH_FORWARD)
2194 entry = TAILQ_FIRST(&s->commits.head);
2195 else
2196 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2199 while (1) {
2200 int have_match = 0;
2202 if (entry == NULL) {
2203 if (s->thread_args.log_complete ||
2204 view->searching == TOG_SEARCH_BACKWARD) {
2205 view->search_next_done =
2206 (s->matched_entry == NULL ?
2207 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2208 s->search_entry = NULL;
2209 return NULL;
2212 * Poke the log thread for more commits and return,
2213 * allowing the main loop to make progress. Search
2214 * will resume at s->search_entry once we come back.
2216 s->thread_args.commits_needed++;
2217 return trigger_log_thread(view, 0);
2220 err = match_commit(&have_match, entry->id, entry->commit,
2221 &view->regex);
2222 if (err)
2223 break;
2224 if (have_match) {
2225 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2226 s->matched_entry = entry;
2227 break;
2230 s->search_entry = entry;
2231 if (view->searching == TOG_SEARCH_FORWARD)
2232 entry = TAILQ_NEXT(entry, entry);
2233 else
2234 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2237 if (s->matched_entry) {
2238 int cur = s->selected_entry->idx;
2239 while (cur < s->matched_entry->idx) {
2240 err = input_log_view(NULL, view, KEY_DOWN);
2241 if (err)
2242 return err;
2243 cur++;
2245 while (cur > s->matched_entry->idx) {
2246 err = input_log_view(NULL, view, KEY_UP);
2247 if (err)
2248 return err;
2249 cur--;
2253 s->search_entry = NULL;
2255 return NULL;
2258 static const struct got_error *
2259 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2260 struct got_repository *repo, const char *head_ref_name,
2261 const char *in_repo_path, int log_branches)
2263 const struct got_error *err = NULL;
2264 struct tog_log_view_state *s = &view->state.log;
2265 struct got_repository *thread_repo = NULL;
2266 struct got_commit_graph *thread_graph = NULL;
2267 int errcode;
2269 if (in_repo_path != s->in_repo_path) {
2270 free(s->in_repo_path);
2271 s->in_repo_path = strdup(in_repo_path);
2272 if (s->in_repo_path == NULL)
2273 return got_error_from_errno("strdup");
2276 /* The commit queue only contains commits being displayed. */
2277 TAILQ_INIT(&s->commits.head);
2278 s->commits.ncommits = 0;
2280 s->repo = repo;
2281 if (head_ref_name) {
2282 s->head_ref_name = strdup(head_ref_name);
2283 if (s->head_ref_name == NULL) {
2284 err = got_error_from_errno("strdup");
2285 goto done;
2288 s->start_id = got_object_id_dup(start_id);
2289 if (s->start_id == NULL) {
2290 err = got_error_from_errno("got_object_id_dup");
2291 goto done;
2293 s->log_branches = log_branches;
2295 STAILQ_INIT(&s->colors);
2296 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2297 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2298 get_color_value("TOG_COLOR_COMMIT"));
2299 if (err)
2300 goto done;
2301 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2302 get_color_value("TOG_COLOR_AUTHOR"));
2303 if (err) {
2304 free_colors(&s->colors);
2305 goto done;
2307 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2308 get_color_value("TOG_COLOR_DATE"));
2309 if (err) {
2310 free_colors(&s->colors);
2311 goto done;
2315 view->show = show_log_view;
2316 view->input = input_log_view;
2317 view->close = close_log_view;
2318 view->search_start = search_start_log_view;
2319 view->search_next = search_next_log_view;
2321 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2322 if (err)
2323 goto done;
2324 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2325 !s->log_branches);
2326 if (err)
2327 goto done;
2328 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2329 s->repo, NULL, NULL);
2330 if (err)
2331 goto done;
2333 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2334 if (errcode) {
2335 err = got_error_set_errno(errcode, "pthread_cond_init");
2336 goto done;
2338 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2339 if (errcode) {
2340 err = got_error_set_errno(errcode, "pthread_cond_init");
2341 goto done;
2344 s->thread_args.commits_needed = view->nlines;
2345 s->thread_args.graph = thread_graph;
2346 s->thread_args.commits = &s->commits;
2347 s->thread_args.in_repo_path = s->in_repo_path;
2348 s->thread_args.start_id = s->start_id;
2349 s->thread_args.repo = thread_repo;
2350 s->thread_args.log_complete = 0;
2351 s->thread_args.quit = &s->quit;
2352 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2353 s->thread_args.selected_entry = &s->selected_entry;
2354 s->thread_args.searching = &view->searching;
2355 s->thread_args.search_next_done = &view->search_next_done;
2356 s->thread_args.regex = &view->regex;
2357 done:
2358 if (err)
2359 close_log_view(view);
2360 return err;
2363 static const struct got_error *
2364 show_log_view(struct tog_view *view)
2366 const struct got_error *err;
2367 struct tog_log_view_state *s = &view->state.log;
2369 if (s->thread == NULL) {
2370 int errcode = pthread_create(&s->thread, NULL, log_thread,
2371 &s->thread_args);
2372 if (errcode)
2373 return got_error_set_errno(errcode, "pthread_create");
2374 if (s->thread_args.commits_needed > 0) {
2375 err = trigger_log_thread(view, 1);
2376 if (err)
2377 return err;
2381 return draw_commits(view);
2384 static const struct got_error *
2385 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2387 const struct got_error *err = NULL;
2388 struct tog_log_view_state *s = &view->state.log;
2389 struct tog_view *diff_view = NULL, *tree_view = NULL;
2390 struct tog_view *ref_view = NULL;
2391 int begin_x = 0;
2393 switch (ch) {
2394 case 'q':
2395 s->quit = 1;
2396 break;
2397 case 'k':
2398 case KEY_UP:
2399 case '<':
2400 case ',':
2401 if (s->first_displayed_entry == NULL)
2402 break;
2403 if (s->selected > 0)
2404 s->selected--;
2405 else
2406 log_scroll_up(s, 1);
2407 select_commit(s);
2408 break;
2409 case KEY_PPAGE:
2410 case CTRL('b'):
2411 if (s->first_displayed_entry == NULL)
2412 break;
2413 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2414 s->selected = 0;
2415 else
2416 log_scroll_up(s, view->nlines - 1);
2417 select_commit(s);
2418 break;
2419 case 'j':
2420 case KEY_DOWN:
2421 case '>':
2422 case '.':
2423 if (s->first_displayed_entry == NULL)
2424 break;
2425 if (s->selected < MIN(view->nlines - 2,
2426 s->commits.ncommits - 1))
2427 s->selected++;
2428 else {
2429 err = log_scroll_down(view, 1);
2430 if (err)
2431 break;
2433 select_commit(s);
2434 break;
2435 case KEY_NPAGE:
2436 case CTRL('f'): {
2437 struct commit_queue_entry *first;
2438 first = s->first_displayed_entry;
2439 if (first == NULL)
2440 break;
2441 err = log_scroll_down(view, view->nlines - 1);
2442 if (err)
2443 break;
2444 if (first == s->first_displayed_entry &&
2445 s->selected < MIN(view->nlines - 2,
2446 s->commits.ncommits - 1)) {
2447 /* can't scroll further down */
2448 s->selected = MIN(view->nlines - 2,
2449 s->commits.ncommits - 1);
2451 select_commit(s);
2452 break;
2454 case KEY_RESIZE:
2455 if (s->selected > view->nlines - 2)
2456 s->selected = view->nlines - 2;
2457 if (s->selected > s->commits.ncommits - 1)
2458 s->selected = s->commits.ncommits - 1;
2459 select_commit(s);
2460 if (s->commits.ncommits < view->nlines - 1 &&
2461 !s->thread_args.log_complete) {
2462 s->thread_args.commits_needed += (view->nlines - 1) -
2463 s->commits.ncommits;
2464 err = trigger_log_thread(view, 1);
2466 break;
2467 case KEY_ENTER:
2468 case ' ':
2469 case '\r':
2470 if (s->selected_entry == NULL)
2471 break;
2472 if (view_is_parent_view(view))
2473 begin_x = view_split_begin_x(view->begin_x);
2474 err = open_diff_view_for_commit(&diff_view, begin_x,
2475 s->selected_entry->commit, s->selected_entry->id,
2476 view, s->repo);
2477 if (err)
2478 break;
2479 view->focussed = 0;
2480 diff_view->focussed = 1;
2481 if (view_is_parent_view(view)) {
2482 err = view_close_child(view);
2483 if (err)
2484 return err;
2485 view_set_child(view, diff_view);
2486 view->focus_child = 1;
2487 } else
2488 *new_view = diff_view;
2489 break;
2490 case 't':
2491 if (s->selected_entry == NULL)
2492 break;
2493 if (view_is_parent_view(view))
2494 begin_x = view_split_begin_x(view->begin_x);
2495 err = browse_commit_tree(&tree_view, begin_x,
2496 s->selected_entry, s->in_repo_path, s->head_ref_name,
2497 s->repo);
2498 if (err)
2499 break;
2500 view->focussed = 0;
2501 tree_view->focussed = 1;
2502 if (view_is_parent_view(view)) {
2503 err = view_close_child(view);
2504 if (err)
2505 return err;
2506 view_set_child(view, tree_view);
2507 view->focus_child = 1;
2508 } else
2509 *new_view = tree_view;
2510 break;
2511 case KEY_BACKSPACE:
2512 case CTRL('l'):
2513 case 'B':
2514 if (ch == KEY_BACKSPACE &&
2515 got_path_is_root_dir(s->in_repo_path))
2516 break;
2517 err = stop_log_thread(s);
2518 if (err)
2519 return err;
2520 if (ch == KEY_BACKSPACE) {
2521 char *parent_path;
2522 err = got_path_dirname(&parent_path, s->in_repo_path);
2523 if (err)
2524 return err;
2525 free(s->in_repo_path);
2526 s->in_repo_path = parent_path;
2527 s->thread_args.in_repo_path = s->in_repo_path;
2528 } else if (ch == CTRL('l')) {
2529 struct got_object_id *start_id;
2530 err = got_repo_match_object_id(&start_id, NULL,
2531 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2532 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2533 if (err)
2534 return err;
2535 free(s->start_id);
2536 s->start_id = start_id;
2537 s->thread_args.start_id = s->start_id;
2538 } else /* 'B' */
2539 s->log_branches = !s->log_branches;
2541 err = got_repo_open(&s->thread_args.repo,
2542 got_repo_get_path(s->repo), NULL);
2543 if (err)
2544 return err;
2545 tog_free_refs();
2546 err = tog_load_refs(s->repo);
2547 if (err)
2548 return err;
2549 err = got_commit_graph_open(&s->thread_args.graph,
2550 s->in_repo_path, !s->log_branches);
2551 if (err)
2552 return err;
2553 err = got_commit_graph_iter_start(s->thread_args.graph,
2554 s->start_id, s->repo, NULL, NULL);
2555 if (err)
2556 return err;
2557 free_commits(&s->commits);
2558 s->first_displayed_entry = NULL;
2559 s->last_displayed_entry = NULL;
2560 s->selected_entry = NULL;
2561 s->selected = 0;
2562 s->thread_args.log_complete = 0;
2563 s->quit = 0;
2564 s->thread_args.commits_needed = view->nlines;
2565 break;
2566 case 'r':
2567 if (view_is_parent_view(view))
2568 begin_x = view_split_begin_x(view->begin_x);
2569 ref_view = view_open(view->nlines, view->ncols,
2570 view->begin_y, begin_x, TOG_VIEW_REF);
2571 if (ref_view == NULL)
2572 return got_error_from_errno("view_open");
2573 err = open_ref_view(ref_view, s->repo);
2574 if (err) {
2575 view_close(ref_view);
2576 return err;
2578 view->focussed = 0;
2579 ref_view->focussed = 1;
2580 if (view_is_parent_view(view)) {
2581 err = view_close_child(view);
2582 if (err)
2583 return err;
2584 view_set_child(view, ref_view);
2585 view->focus_child = 1;
2586 } else
2587 *new_view = ref_view;
2588 break;
2589 default:
2590 break;
2593 return err;
2596 static const struct got_error *
2597 apply_unveil(const char *repo_path, const char *worktree_path)
2599 const struct got_error *error;
2601 #ifdef PROFILE
2602 if (unveil("gmon.out", "rwc") != 0)
2603 return got_error_from_errno2("unveil", "gmon.out");
2604 #endif
2605 if (repo_path && unveil(repo_path, "r") != 0)
2606 return got_error_from_errno2("unveil", repo_path);
2608 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2609 return got_error_from_errno2("unveil", worktree_path);
2611 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2612 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2614 error = got_privsep_unveil_exec_helpers();
2615 if (error != NULL)
2616 return error;
2618 if (unveil(NULL, NULL) != 0)
2619 return got_error_from_errno("unveil");
2621 return NULL;
2624 static void
2625 init_curses(void)
2627 initscr();
2628 cbreak();
2629 halfdelay(1); /* Do fast refresh while initial view is loading. */
2630 noecho();
2631 nonl();
2632 intrflush(stdscr, FALSE);
2633 keypad(stdscr, TRUE);
2634 curs_set(0);
2635 if (getenv("TOG_COLORS") != NULL) {
2636 start_color();
2637 use_default_colors();
2639 signal(SIGWINCH, tog_sigwinch);
2640 signal(SIGPIPE, tog_sigpipe);
2641 signal(SIGCONT, tog_sigcont);
2644 static const struct got_error *
2645 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2646 struct got_repository *repo, struct got_worktree *worktree)
2648 const struct got_error *err = NULL;
2650 if (argc == 0) {
2651 *in_repo_path = strdup("/");
2652 if (*in_repo_path == NULL)
2653 return got_error_from_errno("strdup");
2654 return NULL;
2657 if (worktree) {
2658 const char *prefix = got_worktree_get_path_prefix(worktree);
2659 char *p;
2661 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2662 if (err)
2663 return err;
2664 if (asprintf(in_repo_path, "%s%s%s", prefix,
2665 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2666 p) == -1) {
2667 err = got_error_from_errno("asprintf");
2668 *in_repo_path = NULL;
2670 free(p);
2671 } else
2672 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2674 return err;
2677 static const struct got_error *
2678 cmd_log(int argc, char *argv[])
2680 const struct got_error *error;
2681 struct got_repository *repo = NULL;
2682 struct got_worktree *worktree = NULL;
2683 struct got_object_id *start_id = NULL;
2684 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2685 char *start_commit = NULL, *label = NULL;
2686 struct got_reference *ref = NULL;
2687 const char *head_ref_name = NULL;
2688 int ch, log_branches = 0;
2689 struct tog_view *view;
2691 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2692 switch (ch) {
2693 case 'b':
2694 log_branches = 1;
2695 break;
2696 case 'c':
2697 start_commit = optarg;
2698 break;
2699 case 'r':
2700 repo_path = realpath(optarg, NULL);
2701 if (repo_path == NULL)
2702 return got_error_from_errno2("realpath",
2703 optarg);
2704 break;
2705 default:
2706 usage_log();
2707 /* NOTREACHED */
2711 argc -= optind;
2712 argv += optind;
2714 if (argc > 1)
2715 usage_log();
2717 if (repo_path == NULL) {
2718 cwd = getcwd(NULL, 0);
2719 if (cwd == NULL)
2720 return got_error_from_errno("getcwd");
2721 error = got_worktree_open(&worktree, cwd);
2722 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2723 goto done;
2724 if (worktree)
2725 repo_path =
2726 strdup(got_worktree_get_repo_path(worktree));
2727 else
2728 repo_path = strdup(cwd);
2729 if (repo_path == NULL) {
2730 error = got_error_from_errno("strdup");
2731 goto done;
2735 error = got_repo_open(&repo, repo_path, NULL);
2736 if (error != NULL)
2737 goto done;
2739 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2740 repo, worktree);
2741 if (error)
2742 goto done;
2744 init_curses();
2746 error = apply_unveil(got_repo_get_path(repo),
2747 worktree ? got_worktree_get_root_path(worktree) : NULL);
2748 if (error)
2749 goto done;
2751 /* already loaded by tog_log_with_path()? */
2752 if (TAILQ_EMPTY(&tog_refs)) {
2753 error = tog_load_refs(repo);
2754 if (error)
2755 goto done;
2758 if (start_commit == NULL) {
2759 error = got_repo_match_object_id(&start_id, &label,
2760 worktree ? got_worktree_get_head_ref_name(worktree) :
2761 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2762 if (error)
2763 goto done;
2764 head_ref_name = label;
2765 } else {
2766 error = got_ref_open(&ref, repo, start_commit, 0);
2767 if (error == NULL)
2768 head_ref_name = got_ref_get_name(ref);
2769 else if (error->code != GOT_ERR_NOT_REF)
2770 goto done;
2771 error = got_repo_match_object_id(&start_id, NULL,
2772 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2773 if (error)
2774 goto done;
2777 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2778 if (view == NULL) {
2779 error = got_error_from_errno("view_open");
2780 goto done;
2782 error = open_log_view(view, start_id, repo, head_ref_name,
2783 in_repo_path, log_branches);
2784 if (error)
2785 goto done;
2786 if (worktree) {
2787 /* Release work tree lock. */
2788 got_worktree_close(worktree);
2789 worktree = NULL;
2791 error = view_loop(view);
2792 done:
2793 free(in_repo_path);
2794 free(repo_path);
2795 free(cwd);
2796 free(start_id);
2797 free(label);
2798 if (ref)
2799 got_ref_close(ref);
2800 if (repo) {
2801 const struct got_error *close_err = got_repo_close(repo);
2802 if (error == NULL)
2803 error = close_err;
2805 if (worktree)
2806 got_worktree_close(worktree);
2807 tog_free_refs();
2808 return error;
2811 __dead static void
2812 usage_diff(void)
2814 endwin();
2815 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2816 "[-w] object1 object2\n", getprogname());
2817 exit(1);
2820 static int
2821 match_line(const char *line, regex_t *regex, size_t nmatch,
2822 regmatch_t *regmatch)
2824 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2827 struct tog_color *
2828 match_color(struct tog_colors *colors, const char *line)
2830 struct tog_color *tc = NULL;
2832 STAILQ_FOREACH(tc, colors, entry) {
2833 if (match_line(line, &tc->regex, 0, NULL))
2834 return tc;
2837 return NULL;
2840 static const struct got_error *
2841 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2842 WINDOW *window, regmatch_t *regmatch)
2844 const struct got_error *err = NULL;
2845 wchar_t *wline;
2846 int width;
2847 char *s;
2849 *wtotal = 0;
2851 s = strndup(line, regmatch->rm_so);
2852 if (s == NULL)
2853 return got_error_from_errno("strndup");
2855 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2856 if (err) {
2857 free(s);
2858 return err;
2860 waddwstr(window, wline);
2861 free(wline);
2862 free(s);
2863 wlimit -= width;
2864 *wtotal += width;
2866 if (wlimit > 0) {
2867 s = strndup(line + regmatch->rm_so,
2868 regmatch->rm_eo - regmatch->rm_so);
2869 if (s == NULL) {
2870 err = got_error_from_errno("strndup");
2871 free(s);
2872 return err;
2874 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2875 if (err) {
2876 free(s);
2877 return err;
2879 wattr_on(window, A_STANDOUT, NULL);
2880 waddwstr(window, wline);
2881 wattr_off(window, A_STANDOUT, NULL);
2882 free(wline);
2883 free(s);
2884 wlimit -= width;
2885 *wtotal += width;
2888 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2889 err = format_line(&wline, &width,
2890 line + regmatch->rm_eo, wlimit, col_tab_align);
2891 if (err)
2892 return err;
2893 waddwstr(window, wline);
2894 free(wline);
2895 *wtotal += width;
2898 return NULL;
2901 static const struct got_error *
2902 draw_file(struct tog_view *view, const char *header)
2904 struct tog_diff_view_state *s = &view->state.diff;
2905 regmatch_t *regmatch = &view->regmatch;
2906 const struct got_error *err;
2907 int nprinted = 0;
2908 char *line;
2909 size_t linesize = 0;
2910 ssize_t linelen;
2911 struct tog_color *tc;
2912 wchar_t *wline;
2913 int width;
2914 int max_lines = view->nlines;
2915 int nlines = s->nlines;
2916 off_t line_offset;
2918 line_offset = s->line_offsets[s->first_displayed_line - 1];
2919 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2920 return got_error_from_errno("fseek");
2922 werase(view->window);
2924 if (header) {
2925 if (asprintf(&line, "[%d/%d] %s",
2926 s->first_displayed_line - 1 + s->selected_line, nlines,
2927 header) == -1)
2928 return got_error_from_errno("asprintf");
2929 err = format_line(&wline, &width, line, view->ncols, 0);
2930 free(line);
2931 if (err)
2932 return err;
2934 if (view_needs_focus_indication(view))
2935 wstandout(view->window);
2936 waddwstr(view->window, wline);
2937 free(wline);
2938 wline = NULL;
2939 if (view_needs_focus_indication(view))
2940 wstandend(view->window);
2941 if (width <= view->ncols - 1)
2942 waddch(view->window, '\n');
2944 if (max_lines <= 1)
2945 return NULL;
2946 max_lines--;
2949 s->eof = 0;
2950 line = NULL;
2951 while (max_lines > 0 && nprinted < max_lines) {
2952 linelen = getline(&line, &linesize, s->f);
2953 if (linelen == -1) {
2954 if (feof(s->f)) {
2955 s->eof = 1;
2956 break;
2958 free(line);
2959 return got_ferror(s->f, GOT_ERR_IO);
2962 tc = match_color(&s->colors, line);
2963 if (tc)
2964 wattr_on(view->window,
2965 COLOR_PAIR(tc->colorpair), NULL);
2966 if (s->first_displayed_line + nprinted == s->matched_line &&
2967 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2968 err = add_matched_line(&width, line, view->ncols, 0,
2969 view->window, regmatch);
2970 if (err) {
2971 free(line);
2972 return err;
2974 } else {
2975 err = format_line(&wline, &width, line, view->ncols, 0);
2976 if (err) {
2977 free(line);
2978 return err;
2980 waddwstr(view->window, wline);
2981 free(wline);
2982 wline = NULL;
2984 if (tc)
2985 wattr_off(view->window,
2986 COLOR_PAIR(tc->colorpair), NULL);
2987 if (width <= view->ncols - 1)
2988 waddch(view->window, '\n');
2989 nprinted++;
2991 free(line);
2992 if (nprinted >= 1)
2993 s->last_displayed_line = s->first_displayed_line +
2994 (nprinted - 1);
2995 else
2996 s->last_displayed_line = s->first_displayed_line;
2998 view_vborder(view);
3000 if (s->eof) {
3001 while (nprinted < view->nlines) {
3002 waddch(view->window, '\n');
3003 nprinted++;
3006 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3007 if (err) {
3008 return err;
3011 wstandout(view->window);
3012 waddwstr(view->window, wline);
3013 free(wline);
3014 wline = NULL;
3015 wstandend(view->window);
3018 return NULL;
3021 static char *
3022 get_datestr(time_t *time, char *datebuf)
3024 struct tm mytm, *tm;
3025 char *p, *s;
3027 tm = gmtime_r(time, &mytm);
3028 if (tm == NULL)
3029 return NULL;
3030 s = asctime_r(tm, datebuf);
3031 if (s == NULL)
3032 return NULL;
3033 p = strchr(s, '\n');
3034 if (p)
3035 *p = '\0';
3036 return s;
3039 static const struct got_error *
3040 get_changed_paths(struct got_pathlist_head *paths,
3041 struct got_commit_object *commit, struct got_repository *repo)
3043 const struct got_error *err = NULL;
3044 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3045 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3046 struct got_object_qid *qid;
3048 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3049 if (qid != NULL) {
3050 struct got_commit_object *pcommit;
3051 err = got_object_open_as_commit(&pcommit, repo,
3052 qid->id);
3053 if (err)
3054 return err;
3056 tree_id1 = got_object_id_dup(
3057 got_object_commit_get_tree_id(pcommit));
3058 if (tree_id1 == NULL) {
3059 got_object_commit_close(pcommit);
3060 return got_error_from_errno("got_object_id_dup");
3062 got_object_commit_close(pcommit);
3066 if (tree_id1) {
3067 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3068 if (err)
3069 goto done;
3072 tree_id2 = got_object_commit_get_tree_id(commit);
3073 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3074 if (err)
3075 goto done;
3077 err = got_diff_tree(tree1, tree2, "", "", repo,
3078 got_diff_tree_collect_changed_paths, paths, 0);
3079 done:
3080 if (tree1)
3081 got_object_tree_close(tree1);
3082 if (tree2)
3083 got_object_tree_close(tree2);
3084 free(tree_id1);
3085 return err;
3088 static const struct got_error *
3089 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3091 off_t *p;
3093 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3094 if (p == NULL)
3095 return got_error_from_errno("reallocarray");
3096 *line_offsets = p;
3097 (*line_offsets)[*nlines] = off;
3098 (*nlines)++;
3099 return NULL;
3102 static const struct got_error *
3103 write_commit_info(off_t **line_offsets, size_t *nlines,
3104 struct got_object_id *commit_id, struct got_reflist_head *refs,
3105 struct got_repository *repo, FILE *outfile)
3107 const struct got_error *err = NULL;
3108 char datebuf[26], *datestr;
3109 struct got_commit_object *commit;
3110 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3111 time_t committer_time;
3112 const char *author, *committer;
3113 char *refs_str = NULL;
3114 struct got_pathlist_head changed_paths;
3115 struct got_pathlist_entry *pe;
3116 off_t outoff = 0;
3117 int n;
3119 TAILQ_INIT(&changed_paths);
3121 if (refs) {
3122 err = build_refs_str(&refs_str, refs, commit_id, repo);
3123 if (err)
3124 return err;
3127 err = got_object_open_as_commit(&commit, repo, commit_id);
3128 if (err)
3129 return err;
3131 err = got_object_id_str(&id_str, commit_id);
3132 if (err) {
3133 err = got_error_from_errno("got_object_id_str");
3134 goto done;
3137 err = add_line_offset(line_offsets, nlines, 0);
3138 if (err)
3139 goto done;
3141 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3142 refs_str ? refs_str : "", refs_str ? ")" : "");
3143 if (n < 0) {
3144 err = got_error_from_errno("fprintf");
3145 goto done;
3147 outoff += n;
3148 err = add_line_offset(line_offsets, nlines, outoff);
3149 if (err)
3150 goto done;
3152 n = fprintf(outfile, "from: %s\n",
3153 got_object_commit_get_author(commit));
3154 if (n < 0) {
3155 err = got_error_from_errno("fprintf");
3156 goto done;
3158 outoff += n;
3159 err = add_line_offset(line_offsets, nlines, outoff);
3160 if (err)
3161 goto done;
3163 committer_time = got_object_commit_get_committer_time(commit);
3164 datestr = get_datestr(&committer_time, datebuf);
3165 if (datestr) {
3166 n = fprintf(outfile, "date: %s UTC\n", datestr);
3167 if (n < 0) {
3168 err = got_error_from_errno("fprintf");
3169 goto done;
3171 outoff += n;
3172 err = add_line_offset(line_offsets, nlines, outoff);
3173 if (err)
3174 goto done;
3176 author = got_object_commit_get_author(commit);
3177 committer = got_object_commit_get_committer(commit);
3178 if (strcmp(author, committer) != 0) {
3179 n = fprintf(outfile, "via: %s\n", committer);
3180 if (n < 0) {
3181 err = got_error_from_errno("fprintf");
3182 goto done;
3184 outoff += n;
3185 err = add_line_offset(line_offsets, nlines, outoff);
3186 if (err)
3187 goto done;
3189 err = got_object_commit_get_logmsg(&logmsg, commit);
3190 if (err)
3191 goto done;
3192 s = logmsg;
3193 while ((line = strsep(&s, "\n")) != NULL) {
3194 n = fprintf(outfile, "%s\n", line);
3195 if (n < 0) {
3196 err = got_error_from_errno("fprintf");
3197 goto done;
3199 outoff += n;
3200 err = add_line_offset(line_offsets, nlines, outoff);
3201 if (err)
3202 goto done;
3205 err = get_changed_paths(&changed_paths, commit, repo);
3206 if (err)
3207 goto done;
3208 TAILQ_FOREACH(pe, &changed_paths, entry) {
3209 struct got_diff_changed_path *cp = pe->data;
3210 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3211 if (n < 0) {
3212 err = got_error_from_errno("fprintf");
3213 goto done;
3215 outoff += n;
3216 err = add_line_offset(line_offsets, nlines, outoff);
3217 if (err)
3218 goto done;
3219 free((char *)pe->path);
3220 free(pe->data);
3223 fputc('\n', outfile);
3224 outoff++;
3225 err = add_line_offset(line_offsets, nlines, outoff);
3226 done:
3227 got_pathlist_free(&changed_paths);
3228 free(id_str);
3229 free(logmsg);
3230 free(refs_str);
3231 got_object_commit_close(commit);
3232 if (err) {
3233 free(*line_offsets);
3234 *line_offsets = NULL;
3235 *nlines = 0;
3237 return err;
3240 static const struct got_error *
3241 create_diff(struct tog_diff_view_state *s)
3243 const struct got_error *err = NULL;
3244 FILE *f = NULL;
3245 int obj_type;
3247 free(s->line_offsets);
3248 s->line_offsets = malloc(sizeof(off_t));
3249 if (s->line_offsets == NULL)
3250 return got_error_from_errno("malloc");
3251 s->nlines = 0;
3253 f = got_opentemp();
3254 if (f == NULL) {
3255 err = got_error_from_errno("got_opentemp");
3256 goto done;
3258 if (s->f && fclose(s->f) == EOF) {
3259 err = got_error_from_errno("fclose");
3260 goto done;
3262 s->f = f;
3264 if (s->id1)
3265 err = got_object_get_type(&obj_type, s->repo, s->id1);
3266 else
3267 err = got_object_get_type(&obj_type, s->repo, s->id2);
3268 if (err)
3269 goto done;
3271 switch (obj_type) {
3272 case GOT_OBJ_TYPE_BLOB:
3273 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3274 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3275 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3276 break;
3277 case GOT_OBJ_TYPE_TREE:
3278 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3279 s->id1, s->id2, "", "", s->diff_context,
3280 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3281 break;
3282 case GOT_OBJ_TYPE_COMMIT: {
3283 const struct got_object_id_queue *parent_ids;
3284 struct got_object_qid *pid;
3285 struct got_commit_object *commit2;
3286 struct got_reflist_head *refs;
3288 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3289 if (err)
3290 goto done;
3291 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3292 /* Show commit info if we're diffing to a parent/root commit. */
3293 if (s->id1 == NULL) {
3294 err = write_commit_info(&s->line_offsets, &s->nlines,
3295 s->id2, refs, s->repo, s->f);
3296 if (err)
3297 goto done;
3298 } else {
3299 parent_ids = got_object_commit_get_parent_ids(commit2);
3300 STAILQ_FOREACH(pid, parent_ids, entry) {
3301 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3302 err = write_commit_info(
3303 &s->line_offsets, &s->nlines,
3304 s->id2, refs, s->repo, s->f);
3305 if (err)
3306 goto done;
3307 break;
3311 got_object_commit_close(commit2);
3313 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3314 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3315 s->force_text_diff, s->repo, s->f);
3316 break;
3318 default:
3319 err = got_error(GOT_ERR_OBJ_TYPE);
3320 break;
3322 if (err)
3323 goto done;
3324 done:
3325 if (s->f && fflush(s->f) != 0 && err == NULL)
3326 err = got_error_from_errno("fflush");
3327 return err;
3330 static void
3331 diff_view_indicate_progress(struct tog_view *view)
3333 mvwaddstr(view->window, 0, 0, "diffing...");
3334 update_panels();
3335 doupdate();
3338 static const struct got_error *
3339 search_start_diff_view(struct tog_view *view)
3341 struct tog_diff_view_state *s = &view->state.diff;
3343 s->matched_line = 0;
3344 return NULL;
3347 static const struct got_error *
3348 search_next_diff_view(struct tog_view *view)
3350 struct tog_diff_view_state *s = &view->state.diff;
3351 int lineno;
3352 char *line = NULL;
3353 size_t linesize = 0;
3354 ssize_t linelen;
3356 if (!view->searching) {
3357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3358 return NULL;
3361 if (s->matched_line) {
3362 if (view->searching == TOG_SEARCH_FORWARD)
3363 lineno = s->matched_line + 1;
3364 else
3365 lineno = s->matched_line - 1;
3366 } else {
3367 if (view->searching == TOG_SEARCH_FORWARD)
3368 lineno = 1;
3369 else
3370 lineno = s->nlines;
3373 while (1) {
3374 off_t offset;
3376 if (lineno <= 0 || lineno > s->nlines) {
3377 if (s->matched_line == 0) {
3378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3379 break;
3382 if (view->searching == TOG_SEARCH_FORWARD)
3383 lineno = 1;
3384 else
3385 lineno = s->nlines;
3388 offset = s->line_offsets[lineno - 1];
3389 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3390 free(line);
3391 return got_error_from_errno("fseeko");
3393 linelen = getline(&line, &linesize, s->f);
3394 if (linelen != -1 &&
3395 match_line(line, &view->regex, 1, &view->regmatch)) {
3396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3397 s->matched_line = lineno;
3398 break;
3400 if (view->searching == TOG_SEARCH_FORWARD)
3401 lineno++;
3402 else
3403 lineno--;
3405 free(line);
3407 if (s->matched_line) {
3408 s->first_displayed_line = s->matched_line;
3409 s->selected_line = 1;
3412 return NULL;
3415 static const struct got_error *
3416 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3417 struct got_object_id *id2, const char *label1, const char *label2,
3418 int diff_context, int ignore_whitespace, int force_text_diff,
3419 struct tog_view *log_view, struct got_repository *repo)
3421 const struct got_error *err;
3422 struct tog_diff_view_state *s = &view->state.diff;
3424 if (id1 != NULL && id2 != NULL) {
3425 int type1, type2;
3426 err = got_object_get_type(&type1, repo, id1);
3427 if (err)
3428 return err;
3429 err = got_object_get_type(&type2, repo, id2);
3430 if (err)
3431 return err;
3433 if (type1 != type2)
3434 return got_error(GOT_ERR_OBJ_TYPE);
3436 s->first_displayed_line = 1;
3437 s->last_displayed_line = view->nlines;
3438 s->selected_line = 1;
3439 s->repo = repo;
3440 s->id1 = id1;
3441 s->id2 = id2;
3442 s->label1 = label1;
3443 s->label2 = label2;
3445 if (id1) {
3446 s->id1 = got_object_id_dup(id1);
3447 if (s->id1 == NULL)
3448 return got_error_from_errno("got_object_id_dup");
3449 } else
3450 s->id1 = NULL;
3452 s->id2 = got_object_id_dup(id2);
3453 if (s->id2 == NULL) {
3454 free(s->id1);
3455 s->id1 = NULL;
3456 return got_error_from_errno("got_object_id_dup");
3458 s->f = NULL;
3459 s->first_displayed_line = 1;
3460 s->last_displayed_line = view->nlines;
3461 s->diff_context = diff_context;
3462 s->ignore_whitespace = ignore_whitespace;
3463 s->force_text_diff = force_text_diff;
3464 s->log_view = log_view;
3465 s->repo = repo;
3467 STAILQ_INIT(&s->colors);
3468 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3469 err = add_color(&s->colors,
3470 "^-", TOG_COLOR_DIFF_MINUS,
3471 get_color_value("TOG_COLOR_DIFF_MINUS"));
3472 if (err)
3473 return err;
3474 err = add_color(&s->colors, "^\\+",
3475 TOG_COLOR_DIFF_PLUS,
3476 get_color_value("TOG_COLOR_DIFF_PLUS"));
3477 if (err) {
3478 free_colors(&s->colors);
3479 return err;
3481 err = add_color(&s->colors,
3482 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3483 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3484 if (err) {
3485 free_colors(&s->colors);
3486 return err;
3489 err = add_color(&s->colors,
3490 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3491 TOG_COLOR_DIFF_META,
3492 get_color_value("TOG_COLOR_DIFF_META"));
3493 if (err) {
3494 free_colors(&s->colors);
3495 return err;
3498 err = add_color(&s->colors,
3499 "^(from|via): ", TOG_COLOR_AUTHOR,
3500 get_color_value("TOG_COLOR_AUTHOR"));
3501 if (err) {
3502 free_colors(&s->colors);
3503 return err;
3506 err = add_color(&s->colors,
3507 "^date: ", TOG_COLOR_DATE,
3508 get_color_value("TOG_COLOR_DATE"));
3509 if (err) {
3510 free_colors(&s->colors);
3511 return err;
3515 if (log_view && view_is_splitscreen(view))
3516 show_log_view(log_view); /* draw vborder */
3517 diff_view_indicate_progress(view);
3519 s->line_offsets = NULL;
3520 s->nlines = 0;
3521 err = create_diff(s);
3522 if (err) {
3523 free(s->id1);
3524 s->id1 = NULL;
3525 free(s->id2);
3526 s->id2 = NULL;
3527 free_colors(&s->colors);
3528 return err;
3531 view->show = show_diff_view;
3532 view->input = input_diff_view;
3533 view->close = close_diff_view;
3534 view->search_start = search_start_diff_view;
3535 view->search_next = search_next_diff_view;
3537 return NULL;
3540 static const struct got_error *
3541 close_diff_view(struct tog_view *view)
3543 const struct got_error *err = NULL;
3544 struct tog_diff_view_state *s = &view->state.diff;
3546 free(s->id1);
3547 s->id1 = NULL;
3548 free(s->id2);
3549 s->id2 = NULL;
3550 if (s->f && fclose(s->f) == EOF)
3551 err = got_error_from_errno("fclose");
3552 free_colors(&s->colors);
3553 free(s->line_offsets);
3554 s->line_offsets = NULL;
3555 s->nlines = 0;
3556 return err;
3559 static const struct got_error *
3560 show_diff_view(struct tog_view *view)
3562 const struct got_error *err;
3563 struct tog_diff_view_state *s = &view->state.diff;
3564 char *id_str1 = NULL, *id_str2, *header;
3565 const char *label1, *label2;
3567 if (s->id1) {
3568 err = got_object_id_str(&id_str1, s->id1);
3569 if (err)
3570 return err;
3571 label1 = s->label1 ? : id_str1;
3572 } else
3573 label1 = "/dev/null";
3575 err = got_object_id_str(&id_str2, s->id2);
3576 if (err)
3577 return err;
3578 label2 = s->label2 ? : id_str2;
3580 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3581 err = got_error_from_errno("asprintf");
3582 free(id_str1);
3583 free(id_str2);
3584 return err;
3586 free(id_str1);
3587 free(id_str2);
3589 err = draw_file(view, header);
3590 free(header);
3591 return err;
3594 static const struct got_error *
3595 set_selected_commit(struct tog_diff_view_state *s,
3596 struct commit_queue_entry *entry)
3598 const struct got_error *err;
3599 const struct got_object_id_queue *parent_ids;
3600 struct got_commit_object *selected_commit;
3601 struct got_object_qid *pid;
3603 free(s->id2);
3604 s->id2 = got_object_id_dup(entry->id);
3605 if (s->id2 == NULL)
3606 return got_error_from_errno("got_object_id_dup");
3608 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3609 if (err)
3610 return err;
3611 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3612 free(s->id1);
3613 pid = STAILQ_FIRST(parent_ids);
3614 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3615 got_object_commit_close(selected_commit);
3616 return NULL;
3619 static const struct got_error *
3620 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3622 const struct got_error *err = NULL;
3623 struct tog_diff_view_state *s = &view->state.diff;
3624 struct tog_log_view_state *ls;
3625 struct commit_queue_entry *old_selected_entry;
3626 char *line = NULL;
3627 size_t linesize = 0;
3628 ssize_t linelen;
3629 int i;
3631 switch (ch) {
3632 case 'a':
3633 case 'w':
3634 if (ch == 'a')
3635 s->force_text_diff = !s->force_text_diff;
3636 if (ch == 'w')
3637 s->ignore_whitespace = !s->ignore_whitespace;
3638 wclear(view->window);
3639 s->first_displayed_line = 1;
3640 s->last_displayed_line = view->nlines;
3641 diff_view_indicate_progress(view);
3642 err = create_diff(s);
3643 break;
3644 case 'k':
3645 case KEY_UP:
3646 if (s->first_displayed_line > 1)
3647 s->first_displayed_line--;
3648 break;
3649 case KEY_PPAGE:
3650 case CTRL('b'):
3651 if (s->first_displayed_line == 1)
3652 break;
3653 i = 0;
3654 while (i++ < view->nlines - 1 &&
3655 s->first_displayed_line > 1)
3656 s->first_displayed_line--;
3657 break;
3658 case 'j':
3659 case KEY_DOWN:
3660 if (!s->eof)
3661 s->first_displayed_line++;
3662 break;
3663 case KEY_NPAGE:
3664 case CTRL('f'):
3665 case ' ':
3666 if (s->eof)
3667 break;
3668 i = 0;
3669 while (!s->eof && i++ < view->nlines - 1) {
3670 linelen = getline(&line, &linesize, s->f);
3671 s->first_displayed_line++;
3672 if (linelen == -1) {
3673 if (feof(s->f)) {
3674 s->eof = 1;
3675 } else
3676 err = got_ferror(s->f, GOT_ERR_IO);
3677 break;
3680 free(line);
3681 break;
3682 case '[':
3683 if (s->diff_context > 0) {
3684 s->diff_context--;
3685 diff_view_indicate_progress(view);
3686 err = create_diff(s);
3687 if (s->first_displayed_line + view->nlines - 1 >
3688 s->nlines) {
3689 s->first_displayed_line = 1;
3690 s->last_displayed_line = view->nlines;
3693 break;
3694 case ']':
3695 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3696 s->diff_context++;
3697 diff_view_indicate_progress(view);
3698 err = create_diff(s);
3700 break;
3701 case '<':
3702 case ',':
3703 if (s->log_view == NULL)
3704 break;
3705 ls = &s->log_view->state.log;
3706 old_selected_entry = ls->selected_entry;
3708 err = input_log_view(NULL, s->log_view, KEY_UP);
3709 if (err)
3710 break;
3712 if (old_selected_entry == ls->selected_entry)
3713 break;
3715 err = set_selected_commit(s, ls->selected_entry);
3716 if (err)
3717 break;
3719 s->first_displayed_line = 1;
3720 s->last_displayed_line = view->nlines;
3722 diff_view_indicate_progress(view);
3723 err = create_diff(s);
3724 break;
3725 case '>':
3726 case '.':
3727 if (s->log_view == NULL)
3728 break;
3729 ls = &s->log_view->state.log;
3730 old_selected_entry = ls->selected_entry;
3732 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3733 if (err)
3734 break;
3736 if (old_selected_entry == ls->selected_entry)
3737 break;
3739 err = set_selected_commit(s, ls->selected_entry);
3740 if (err)
3741 break;
3743 s->first_displayed_line = 1;
3744 s->last_displayed_line = view->nlines;
3746 diff_view_indicate_progress(view);
3747 err = create_diff(s);
3748 break;
3749 default:
3750 break;
3753 return err;
3756 static const struct got_error *
3757 cmd_diff(int argc, char *argv[])
3759 const struct got_error *error = NULL;
3760 struct got_repository *repo = NULL;
3761 struct got_worktree *worktree = NULL;
3762 struct got_object_id *id1 = NULL, *id2 = NULL;
3763 char *repo_path = NULL, *cwd = NULL;
3764 char *id_str1 = NULL, *id_str2 = NULL;
3765 char *label1 = NULL, *label2 = NULL;
3766 int diff_context = 3, ignore_whitespace = 0;
3767 int ch, force_text_diff = 0;
3768 const char *errstr;
3769 struct tog_view *view;
3771 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3772 switch (ch) {
3773 case 'a':
3774 force_text_diff = 1;
3775 break;
3776 case 'C':
3777 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3778 &errstr);
3779 if (errstr != NULL)
3780 err(1, "-C option %s", errstr);
3781 break;
3782 case 'r':
3783 repo_path = realpath(optarg, NULL);
3784 if (repo_path == NULL)
3785 return got_error_from_errno2("realpath",
3786 optarg);
3787 got_path_strip_trailing_slashes(repo_path);
3788 break;
3789 case 'w':
3790 ignore_whitespace = 1;
3791 break;
3792 default:
3793 usage_diff();
3794 /* NOTREACHED */
3798 argc -= optind;
3799 argv += optind;
3801 if (argc == 0) {
3802 usage_diff(); /* TODO show local worktree changes */
3803 } else if (argc == 2) {
3804 id_str1 = argv[0];
3805 id_str2 = argv[1];
3806 } else
3807 usage_diff();
3809 if (repo_path == NULL) {
3810 cwd = getcwd(NULL, 0);
3811 if (cwd == NULL)
3812 return got_error_from_errno("getcwd");
3813 error = got_worktree_open(&worktree, cwd);
3814 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3815 goto done;
3816 if (worktree)
3817 repo_path =
3818 strdup(got_worktree_get_repo_path(worktree));
3819 else
3820 repo_path = strdup(cwd);
3821 if (repo_path == NULL) {
3822 error = got_error_from_errno("strdup");
3823 goto done;
3827 error = got_repo_open(&repo, repo_path, NULL);
3828 if (error)
3829 goto done;
3831 init_curses();
3833 error = apply_unveil(got_repo_get_path(repo), NULL);
3834 if (error)
3835 goto done;
3837 error = tog_load_refs(repo);
3838 if (error)
3839 goto done;
3841 error = got_repo_match_object_id(&id1, &label1, id_str1,
3842 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3843 if (error)
3844 goto done;
3846 error = got_repo_match_object_id(&id2, &label2, id_str2,
3847 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3848 if (error)
3849 goto done;
3851 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3852 if (view == NULL) {
3853 error = got_error_from_errno("view_open");
3854 goto done;
3856 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3857 ignore_whitespace, force_text_diff, NULL, repo);
3858 if (error)
3859 goto done;
3860 error = view_loop(view);
3861 done:
3862 free(label1);
3863 free(label2);
3864 free(repo_path);
3865 free(cwd);
3866 if (repo) {
3867 const struct got_error *close_err = got_repo_close(repo);
3868 if (error == NULL)
3869 error = close_err;
3871 if (worktree)
3872 got_worktree_close(worktree);
3873 tog_free_refs();
3874 return error;
3877 __dead static void
3878 usage_blame(void)
3880 endwin();
3881 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3882 getprogname());
3883 exit(1);
3886 struct tog_blame_line {
3887 int annotated;
3888 struct got_object_id *id;
3891 static const struct got_error *
3892 draw_blame(struct tog_view *view)
3894 struct tog_blame_view_state *s = &view->state.blame;
3895 struct tog_blame *blame = &s->blame;
3896 regmatch_t *regmatch = &view->regmatch;
3897 const struct got_error *err;
3898 int lineno = 0, nprinted = 0;
3899 char *line = NULL;
3900 size_t linesize = 0;
3901 ssize_t linelen;
3902 wchar_t *wline;
3903 int width;
3904 struct tog_blame_line *blame_line;
3905 struct got_object_id *prev_id = NULL;
3906 char *id_str;
3907 struct tog_color *tc;
3909 err = got_object_id_str(&id_str, s->blamed_commit->id);
3910 if (err)
3911 return err;
3913 rewind(blame->f);
3914 werase(view->window);
3916 if (asprintf(&line, "commit %s", id_str) == -1) {
3917 err = got_error_from_errno("asprintf");
3918 free(id_str);
3919 return err;
3922 err = format_line(&wline, &width, line, view->ncols, 0);
3923 free(line);
3924 line = NULL;
3925 if (err)
3926 return err;
3927 if (view_needs_focus_indication(view))
3928 wstandout(view->window);
3929 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3930 if (tc)
3931 wattr_on(view->window,
3932 COLOR_PAIR(tc->colorpair), NULL);
3933 waddwstr(view->window, wline);
3934 if (tc)
3935 wattr_off(view->window,
3936 COLOR_PAIR(tc->colorpair), NULL);
3937 if (view_needs_focus_indication(view))
3938 wstandend(view->window);
3939 free(wline);
3940 wline = NULL;
3941 if (width < view->ncols - 1)
3942 waddch(view->window, '\n');
3944 if (asprintf(&line, "[%d/%d] %s%s",
3945 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3946 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3947 free(id_str);
3948 return got_error_from_errno("asprintf");
3950 free(id_str);
3951 err = format_line(&wline, &width, line, view->ncols, 0);
3952 free(line);
3953 line = NULL;
3954 if (err)
3955 return err;
3956 waddwstr(view->window, wline);
3957 free(wline);
3958 wline = NULL;
3959 if (width < view->ncols - 1)
3960 waddch(view->window, '\n');
3962 s->eof = 0;
3963 while (nprinted < view->nlines - 2) {
3964 linelen = getline(&line, &linesize, blame->f);
3965 if (linelen == -1) {
3966 if (feof(blame->f)) {
3967 s->eof = 1;
3968 break;
3970 free(line);
3971 return got_ferror(blame->f, GOT_ERR_IO);
3973 if (++lineno < s->first_displayed_line)
3974 continue;
3976 if (view->focussed && nprinted == s->selected_line - 1)
3977 wstandout(view->window);
3979 if (blame->nlines > 0) {
3980 blame_line = &blame->lines[lineno - 1];
3981 if (blame_line->annotated && prev_id &&
3982 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3983 !(view->focussed &&
3984 nprinted == s->selected_line - 1)) {
3985 waddstr(view->window, " ");
3986 } else if (blame_line->annotated) {
3987 char *id_str;
3988 err = got_object_id_str(&id_str, blame_line->id);
3989 if (err) {
3990 free(line);
3991 return err;
3993 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3994 if (tc)
3995 wattr_on(view->window,
3996 COLOR_PAIR(tc->colorpair), NULL);
3997 wprintw(view->window, "%.8s", id_str);
3998 if (tc)
3999 wattr_off(view->window,
4000 COLOR_PAIR(tc->colorpair), NULL);
4001 free(id_str);
4002 prev_id = blame_line->id;
4003 } else {
4004 waddstr(view->window, "........");
4005 prev_id = NULL;
4007 } else {
4008 waddstr(view->window, "........");
4009 prev_id = NULL;
4012 if (view->focussed && nprinted == s->selected_line - 1)
4013 wstandend(view->window);
4014 waddstr(view->window, " ");
4016 if (view->ncols <= 9) {
4017 width = 9;
4018 wline = wcsdup(L"");
4019 if (wline == NULL) {
4020 err = got_error_from_errno("wcsdup");
4021 free(line);
4022 return err;
4024 } else if (s->first_displayed_line + nprinted ==
4025 s->matched_line &&
4026 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4027 err = add_matched_line(&width, line, view->ncols - 9, 9,
4028 view->window, regmatch);
4029 if (err) {
4030 free(line);
4031 return err;
4033 width += 9;
4034 } else {
4035 err = format_line(&wline, &width, line,
4036 view->ncols - 9, 9);
4037 waddwstr(view->window, wline);
4038 free(wline);
4039 wline = NULL;
4040 width += 9;
4043 if (width <= view->ncols - 1)
4044 waddch(view->window, '\n');
4045 if (++nprinted == 1)
4046 s->first_displayed_line = lineno;
4048 free(line);
4049 s->last_displayed_line = lineno;
4051 view_vborder(view);
4053 return NULL;
4056 static const struct got_error *
4057 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4059 const struct got_error *err = NULL;
4060 struct tog_blame_cb_args *a = arg;
4061 struct tog_blame_line *line;
4062 int errcode;
4064 if (nlines != a->nlines ||
4065 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4066 return got_error(GOT_ERR_RANGE);
4068 errcode = pthread_mutex_lock(&tog_mutex);
4069 if (errcode)
4070 return got_error_set_errno(errcode, "pthread_mutex_lock");
4072 if (*a->quit) { /* user has quit the blame view */
4073 err = got_error(GOT_ERR_ITER_COMPLETED);
4074 goto done;
4077 if (lineno == -1)
4078 goto done; /* no change in this commit */
4080 line = &a->lines[lineno - 1];
4081 if (line->annotated)
4082 goto done;
4084 line->id = got_object_id_dup(id);
4085 if (line->id == NULL) {
4086 err = got_error_from_errno("got_object_id_dup");
4087 goto done;
4089 line->annotated = 1;
4090 done:
4091 errcode = pthread_mutex_unlock(&tog_mutex);
4092 if (errcode)
4093 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4094 return err;
4097 static void *
4098 blame_thread(void *arg)
4100 const struct got_error *err, *close_err;
4101 struct tog_blame_thread_args *ta = arg;
4102 struct tog_blame_cb_args *a = ta->cb_args;
4103 int errcode;
4105 err = block_signals_used_by_main_thread();
4106 if (err)
4107 return (void *)err;
4109 err = got_blame(ta->path, a->commit_id, ta->repo,
4110 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4111 if (err && err->code == GOT_ERR_CANCELLED)
4112 err = NULL;
4114 errcode = pthread_mutex_lock(&tog_mutex);
4115 if (errcode)
4116 return (void *)got_error_set_errno(errcode,
4117 "pthread_mutex_lock");
4119 close_err = got_repo_close(ta->repo);
4120 if (err == NULL)
4121 err = close_err;
4122 ta->repo = NULL;
4123 *ta->complete = 1;
4125 errcode = pthread_mutex_unlock(&tog_mutex);
4126 if (errcode && err == NULL)
4127 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4129 return (void *)err;
4132 static struct got_object_id *
4133 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4134 int first_displayed_line, int selected_line)
4136 struct tog_blame_line *line;
4138 if (nlines <= 0)
4139 return NULL;
4141 line = &lines[first_displayed_line - 1 + selected_line - 1];
4142 if (!line->annotated)
4143 return NULL;
4145 return line->id;
4148 static const struct got_error *
4149 stop_blame(struct tog_blame *blame)
4151 const struct got_error *err = NULL;
4152 int i;
4154 if (blame->thread) {
4155 int errcode;
4156 errcode = pthread_mutex_unlock(&tog_mutex);
4157 if (errcode)
4158 return got_error_set_errno(errcode,
4159 "pthread_mutex_unlock");
4160 errcode = pthread_join(blame->thread, (void **)&err);
4161 if (errcode)
4162 return got_error_set_errno(errcode, "pthread_join");
4163 errcode = pthread_mutex_lock(&tog_mutex);
4164 if (errcode)
4165 return got_error_set_errno(errcode,
4166 "pthread_mutex_lock");
4167 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4168 err = NULL;
4169 blame->thread = NULL;
4171 if (blame->thread_args.repo) {
4172 const struct got_error *close_err;
4173 close_err = got_repo_close(blame->thread_args.repo);
4174 if (err == NULL)
4175 err = close_err;
4176 blame->thread_args.repo = NULL;
4178 if (blame->f) {
4179 if (fclose(blame->f) == EOF && err == NULL)
4180 err = got_error_from_errno("fclose");
4181 blame->f = NULL;
4183 if (blame->lines) {
4184 for (i = 0; i < blame->nlines; i++)
4185 free(blame->lines[i].id);
4186 free(blame->lines);
4187 blame->lines = NULL;
4189 free(blame->cb_args.commit_id);
4190 blame->cb_args.commit_id = NULL;
4192 return err;
4195 static const struct got_error *
4196 cancel_blame_view(void *arg)
4198 const struct got_error *err = NULL;
4199 int *done = arg;
4200 int errcode;
4202 errcode = pthread_mutex_lock(&tog_mutex);
4203 if (errcode)
4204 return got_error_set_errno(errcode,
4205 "pthread_mutex_unlock");
4207 if (*done)
4208 err = got_error(GOT_ERR_CANCELLED);
4210 errcode = pthread_mutex_unlock(&tog_mutex);
4211 if (errcode)
4212 return got_error_set_errno(errcode,
4213 "pthread_mutex_lock");
4215 return err;
4218 static const struct got_error *
4219 run_blame(struct tog_view *view)
4221 struct tog_blame_view_state *s = &view->state.blame;
4222 struct tog_blame *blame = &s->blame;
4223 const struct got_error *err = NULL;
4224 struct got_blob_object *blob = NULL;
4225 struct got_repository *thread_repo = NULL;
4226 struct got_object_id *obj_id = NULL;
4227 int obj_type;
4229 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4230 s->path);
4231 if (err)
4232 return err;
4234 err = got_object_get_type(&obj_type, s->repo, obj_id);
4235 if (err)
4236 goto done;
4238 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4239 err = got_error(GOT_ERR_OBJ_TYPE);
4240 goto done;
4243 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4244 if (err)
4245 goto done;
4246 blame->f = got_opentemp();
4247 if (blame->f == NULL) {
4248 err = got_error_from_errno("got_opentemp");
4249 goto done;
4251 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4252 &blame->line_offsets, blame->f, blob);
4253 if (err)
4254 goto done;
4255 if (blame->nlines == 0) {
4256 s->blame_complete = 1;
4257 goto done;
4260 /* Don't include \n at EOF in the blame line count. */
4261 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4262 blame->nlines--;
4264 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4265 if (blame->lines == NULL) {
4266 err = got_error_from_errno("calloc");
4267 goto done;
4270 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4271 if (err)
4272 goto done;
4274 blame->cb_args.view = view;
4275 blame->cb_args.lines = blame->lines;
4276 blame->cb_args.nlines = blame->nlines;
4277 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4278 if (blame->cb_args.commit_id == NULL) {
4279 err = got_error_from_errno("got_object_id_dup");
4280 goto done;
4282 blame->cb_args.quit = &s->done;
4284 blame->thread_args.path = s->path;
4285 blame->thread_args.repo = thread_repo;
4286 blame->thread_args.cb_args = &blame->cb_args;
4287 blame->thread_args.complete = &s->blame_complete;
4288 blame->thread_args.cancel_cb = cancel_blame_view;
4289 blame->thread_args.cancel_arg = &s->done;
4290 s->blame_complete = 0;
4292 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4293 s->first_displayed_line = 1;
4294 s->last_displayed_line = view->nlines;
4295 s->selected_line = 1;
4298 done:
4299 if (blob)
4300 got_object_blob_close(blob);
4301 free(obj_id);
4302 if (err)
4303 stop_blame(blame);
4304 return err;
4307 static const struct got_error *
4308 open_blame_view(struct tog_view *view, char *path,
4309 struct got_object_id *commit_id, struct got_repository *repo)
4311 const struct got_error *err = NULL;
4312 struct tog_blame_view_state *s = &view->state.blame;
4314 STAILQ_INIT(&s->blamed_commits);
4316 s->path = strdup(path);
4317 if (s->path == NULL)
4318 return got_error_from_errno("strdup");
4320 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4321 if (err) {
4322 free(s->path);
4323 return err;
4326 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4327 s->first_displayed_line = 1;
4328 s->last_displayed_line = view->nlines;
4329 s->selected_line = 1;
4330 s->blame_complete = 0;
4331 s->repo = repo;
4332 s->commit_id = commit_id;
4333 memset(&s->blame, 0, sizeof(s->blame));
4335 STAILQ_INIT(&s->colors);
4336 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4337 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4338 get_color_value("TOG_COLOR_COMMIT"));
4339 if (err)
4340 return err;
4343 view->show = show_blame_view;
4344 view->input = input_blame_view;
4345 view->close = close_blame_view;
4346 view->search_start = search_start_blame_view;
4347 view->search_next = search_next_blame_view;
4349 return run_blame(view);
4352 static const struct got_error *
4353 close_blame_view(struct tog_view *view)
4355 const struct got_error *err = NULL;
4356 struct tog_blame_view_state *s = &view->state.blame;
4358 if (s->blame.thread)
4359 err = stop_blame(&s->blame);
4361 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4362 struct got_object_qid *blamed_commit;
4363 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4364 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4365 got_object_qid_free(blamed_commit);
4368 free(s->path);
4369 free_colors(&s->colors);
4371 return err;
4374 static const struct got_error *
4375 search_start_blame_view(struct tog_view *view)
4377 struct tog_blame_view_state *s = &view->state.blame;
4379 s->matched_line = 0;
4380 return NULL;
4383 static const struct got_error *
4384 search_next_blame_view(struct tog_view *view)
4386 struct tog_blame_view_state *s = &view->state.blame;
4387 int lineno;
4388 char *line = NULL;
4389 size_t linesize = 0;
4390 ssize_t linelen;
4392 if (!view->searching) {
4393 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4394 return NULL;
4397 if (s->matched_line) {
4398 if (view->searching == TOG_SEARCH_FORWARD)
4399 lineno = s->matched_line + 1;
4400 else
4401 lineno = s->matched_line - 1;
4402 } else {
4403 if (view->searching == TOG_SEARCH_FORWARD)
4404 lineno = 1;
4405 else
4406 lineno = s->blame.nlines;
4409 while (1) {
4410 off_t offset;
4412 if (lineno <= 0 || lineno > s->blame.nlines) {
4413 if (s->matched_line == 0) {
4414 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4415 break;
4418 if (view->searching == TOG_SEARCH_FORWARD)
4419 lineno = 1;
4420 else
4421 lineno = s->blame.nlines;
4424 offset = s->blame.line_offsets[lineno - 1];
4425 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4426 free(line);
4427 return got_error_from_errno("fseeko");
4429 linelen = getline(&line, &linesize, s->blame.f);
4430 if (linelen != -1 &&
4431 match_line(line, &view->regex, 1, &view->regmatch)) {
4432 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4433 s->matched_line = lineno;
4434 break;
4436 if (view->searching == TOG_SEARCH_FORWARD)
4437 lineno++;
4438 else
4439 lineno--;
4441 free(line);
4443 if (s->matched_line) {
4444 s->first_displayed_line = s->matched_line;
4445 s->selected_line = 1;
4448 return NULL;
4451 static const struct got_error *
4452 show_blame_view(struct tog_view *view)
4454 const struct got_error *err = NULL;
4455 struct tog_blame_view_state *s = &view->state.blame;
4456 int errcode;
4458 if (s->blame.thread == NULL && !s->blame_complete) {
4459 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4460 &s->blame.thread_args);
4461 if (errcode)
4462 return got_error_set_errno(errcode, "pthread_create");
4464 halfdelay(1); /* fast refresh while annotating */
4467 if (s->blame_complete)
4468 halfdelay(10); /* disable fast refresh */
4470 err = draw_blame(view);
4472 view_vborder(view);
4473 return err;
4476 static const struct got_error *
4477 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4479 const struct got_error *err = NULL, *thread_err = NULL;
4480 struct tog_view *diff_view;
4481 struct tog_blame_view_state *s = &view->state.blame;
4482 int begin_x = 0;
4484 switch (ch) {
4485 case 'q':
4486 s->done = 1;
4487 break;
4488 case 'k':
4489 case KEY_UP:
4490 if (s->selected_line > 1)
4491 s->selected_line--;
4492 else if (s->selected_line == 1 &&
4493 s->first_displayed_line > 1)
4494 s->first_displayed_line--;
4495 break;
4496 case KEY_PPAGE:
4497 case CTRL('b'):
4498 if (s->first_displayed_line == 1) {
4499 s->selected_line = 1;
4500 break;
4502 if (s->first_displayed_line > view->nlines - 2)
4503 s->first_displayed_line -=
4504 (view->nlines - 2);
4505 else
4506 s->first_displayed_line = 1;
4507 break;
4508 case 'j':
4509 case KEY_DOWN:
4510 if (s->selected_line < view->nlines - 2 &&
4511 s->first_displayed_line +
4512 s->selected_line <= s->blame.nlines)
4513 s->selected_line++;
4514 else if (s->last_displayed_line <
4515 s->blame.nlines)
4516 s->first_displayed_line++;
4517 break;
4518 case 'b':
4519 case 'p': {
4520 struct got_object_id *id = NULL;
4521 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4522 s->first_displayed_line, s->selected_line);
4523 if (id == NULL)
4524 break;
4525 if (ch == 'p') {
4526 struct got_commit_object *commit;
4527 struct got_object_qid *pid;
4528 struct got_object_id *blob_id = NULL;
4529 int obj_type;
4530 err = got_object_open_as_commit(&commit,
4531 s->repo, id);
4532 if (err)
4533 break;
4534 pid = STAILQ_FIRST(
4535 got_object_commit_get_parent_ids(commit));
4536 if (pid == NULL) {
4537 got_object_commit_close(commit);
4538 break;
4540 /* Check if path history ends here. */
4541 err = got_object_id_by_path(&blob_id, s->repo,
4542 pid->id, s->path);
4543 if (err) {
4544 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4545 err = NULL;
4546 got_object_commit_close(commit);
4547 break;
4549 err = got_object_get_type(&obj_type, s->repo,
4550 blob_id);
4551 free(blob_id);
4552 /* Can't blame non-blob type objects. */
4553 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4554 got_object_commit_close(commit);
4555 break;
4557 err = got_object_qid_alloc(&s->blamed_commit,
4558 pid->id);
4559 got_object_commit_close(commit);
4560 } else {
4561 if (got_object_id_cmp(id,
4562 s->blamed_commit->id) == 0)
4563 break;
4564 err = got_object_qid_alloc(&s->blamed_commit,
4565 id);
4567 if (err)
4568 break;
4569 s->done = 1;
4570 thread_err = stop_blame(&s->blame);
4571 s->done = 0;
4572 if (thread_err)
4573 break;
4574 STAILQ_INSERT_HEAD(&s->blamed_commits,
4575 s->blamed_commit, entry);
4576 err = run_blame(view);
4577 if (err)
4578 break;
4579 break;
4581 case 'B': {
4582 struct got_object_qid *first;
4583 first = STAILQ_FIRST(&s->blamed_commits);
4584 if (!got_object_id_cmp(first->id, s->commit_id))
4585 break;
4586 s->done = 1;
4587 thread_err = stop_blame(&s->blame);
4588 s->done = 0;
4589 if (thread_err)
4590 break;
4591 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4592 got_object_qid_free(s->blamed_commit);
4593 s->blamed_commit =
4594 STAILQ_FIRST(&s->blamed_commits);
4595 err = run_blame(view);
4596 if (err)
4597 break;
4598 break;
4600 case KEY_ENTER:
4601 case '\r': {
4602 struct got_object_id *id = NULL;
4603 struct got_object_qid *pid;
4604 struct got_commit_object *commit = NULL;
4605 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4606 s->first_displayed_line, s->selected_line);
4607 if (id == NULL)
4608 break;
4609 err = got_object_open_as_commit(&commit, s->repo, id);
4610 if (err)
4611 break;
4612 pid = STAILQ_FIRST(
4613 got_object_commit_get_parent_ids(commit));
4614 if (view_is_parent_view(view))
4615 begin_x = view_split_begin_x(view->begin_x);
4616 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4617 if (diff_view == NULL) {
4618 got_object_commit_close(commit);
4619 err = got_error_from_errno("view_open");
4620 break;
4622 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4623 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4624 got_object_commit_close(commit);
4625 if (err) {
4626 view_close(diff_view);
4627 break;
4629 view->focussed = 0;
4630 diff_view->focussed = 1;
4631 if (view_is_parent_view(view)) {
4632 err = view_close_child(view);
4633 if (err)
4634 break;
4635 view_set_child(view, diff_view);
4636 view->focus_child = 1;
4637 } else
4638 *new_view = diff_view;
4639 if (err)
4640 break;
4641 break;
4643 case KEY_NPAGE:
4644 case CTRL('f'):
4645 case ' ':
4646 if (s->last_displayed_line >= s->blame.nlines &&
4647 s->selected_line >= MIN(s->blame.nlines,
4648 view->nlines - 2)) {
4649 break;
4651 if (s->last_displayed_line >= s->blame.nlines &&
4652 s->selected_line < view->nlines - 2) {
4653 s->selected_line = MIN(s->blame.nlines,
4654 view->nlines - 2);
4655 break;
4657 if (s->last_displayed_line + view->nlines - 2
4658 <= s->blame.nlines)
4659 s->first_displayed_line +=
4660 view->nlines - 2;
4661 else
4662 s->first_displayed_line =
4663 s->blame.nlines -
4664 (view->nlines - 3);
4665 break;
4666 case KEY_RESIZE:
4667 if (s->selected_line > view->nlines - 2) {
4668 s->selected_line = MIN(s->blame.nlines,
4669 view->nlines - 2);
4671 break;
4672 default:
4673 break;
4675 return thread_err ? thread_err : err;
4678 static const struct got_error *
4679 cmd_blame(int argc, char *argv[])
4681 const struct got_error *error;
4682 struct got_repository *repo = NULL;
4683 struct got_worktree *worktree = NULL;
4684 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4685 char *link_target = NULL;
4686 struct got_object_id *commit_id = NULL;
4687 char *commit_id_str = NULL;
4688 int ch;
4689 struct tog_view *view;
4691 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4692 switch (ch) {
4693 case 'c':
4694 commit_id_str = optarg;
4695 break;
4696 case 'r':
4697 repo_path = realpath(optarg, NULL);
4698 if (repo_path == NULL)
4699 return got_error_from_errno2("realpath",
4700 optarg);
4701 break;
4702 default:
4703 usage_blame();
4704 /* NOTREACHED */
4708 argc -= optind;
4709 argv += optind;
4711 if (argc != 1)
4712 usage_blame();
4714 if (repo_path == NULL) {
4715 cwd = getcwd(NULL, 0);
4716 if (cwd == NULL)
4717 return got_error_from_errno("getcwd");
4718 error = got_worktree_open(&worktree, cwd);
4719 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4720 goto done;
4721 if (worktree)
4722 repo_path =
4723 strdup(got_worktree_get_repo_path(worktree));
4724 else
4725 repo_path = strdup(cwd);
4726 if (repo_path == NULL) {
4727 error = got_error_from_errno("strdup");
4728 goto done;
4732 error = got_repo_open(&repo, repo_path, NULL);
4733 if (error != NULL)
4734 goto done;
4736 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4737 worktree);
4738 if (error)
4739 goto done;
4741 init_curses();
4743 error = apply_unveil(got_repo_get_path(repo), NULL);
4744 if (error)
4745 goto done;
4747 error = tog_load_refs(repo);
4748 if (error)
4749 goto done;
4751 if (commit_id_str == NULL) {
4752 struct got_reference *head_ref;
4753 error = got_ref_open(&head_ref, repo, worktree ?
4754 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4755 if (error != NULL)
4756 goto done;
4757 error = got_ref_resolve(&commit_id, repo, head_ref);
4758 got_ref_close(head_ref);
4759 } else {
4760 error = got_repo_match_object_id(&commit_id, NULL,
4761 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4763 if (error != NULL)
4764 goto done;
4766 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4767 if (view == NULL) {
4768 error = got_error_from_errno("view_open");
4769 goto done;
4772 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4773 commit_id, repo);
4774 if (error)
4775 goto done;
4777 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4778 commit_id, repo);
4779 if (error)
4780 goto done;
4781 if (worktree) {
4782 /* Release work tree lock. */
4783 got_worktree_close(worktree);
4784 worktree = NULL;
4786 error = view_loop(view);
4787 done:
4788 free(repo_path);
4789 free(in_repo_path);
4790 free(link_target);
4791 free(cwd);
4792 free(commit_id);
4793 if (worktree)
4794 got_worktree_close(worktree);
4795 if (repo) {
4796 const struct got_error *close_err = got_repo_close(repo);
4797 if (error == NULL)
4798 error = close_err;
4800 tog_free_refs();
4801 return error;
4804 static const struct got_error *
4805 draw_tree_entries(struct tog_view *view, const char *parent_path)
4807 struct tog_tree_view_state *s = &view->state.tree;
4808 const struct got_error *err = NULL;
4809 struct got_tree_entry *te;
4810 wchar_t *wline;
4811 struct tog_color *tc;
4812 int width, n, i, nentries;
4813 int limit = view->nlines;
4815 s->ndisplayed = 0;
4817 werase(view->window);
4819 if (limit == 0)
4820 return NULL;
4822 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4823 if (err)
4824 return err;
4825 if (view_needs_focus_indication(view))
4826 wstandout(view->window);
4827 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4828 if (tc)
4829 wattr_on(view->window,
4830 COLOR_PAIR(tc->colorpair), NULL);
4831 waddwstr(view->window, wline);
4832 if (tc)
4833 wattr_off(view->window,
4834 COLOR_PAIR(tc->colorpair), NULL);
4835 if (view_needs_focus_indication(view))
4836 wstandend(view->window);
4837 free(wline);
4838 wline = NULL;
4839 if (width < view->ncols - 1)
4840 waddch(view->window, '\n');
4841 if (--limit <= 0)
4842 return NULL;
4843 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4844 if (err)
4845 return err;
4846 waddwstr(view->window, wline);
4847 free(wline);
4848 wline = NULL;
4849 if (width < view->ncols - 1)
4850 waddch(view->window, '\n');
4851 if (--limit <= 0)
4852 return NULL;
4853 waddch(view->window, '\n');
4854 if (--limit <= 0)
4855 return NULL;
4857 if (s->first_displayed_entry == NULL) {
4858 te = got_object_tree_get_first_entry(s->tree);
4859 if (s->selected == 0) {
4860 if (view->focussed)
4861 wstandout(view->window);
4862 s->selected_entry = NULL;
4864 waddstr(view->window, " ..\n"); /* parent directory */
4865 if (s->selected == 0 && view->focussed)
4866 wstandend(view->window);
4867 s->ndisplayed++;
4868 if (--limit <= 0)
4869 return NULL;
4870 n = 1;
4871 } else {
4872 n = 0;
4873 te = s->first_displayed_entry;
4876 nentries = got_object_tree_get_nentries(s->tree);
4877 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4878 char *line = NULL, *id_str = NULL, *link_target = NULL;
4879 const char *modestr = "";
4880 mode_t mode;
4882 te = got_object_tree_get_entry(s->tree, i);
4883 mode = got_tree_entry_get_mode(te);
4885 if (s->show_ids) {
4886 err = got_object_id_str(&id_str,
4887 got_tree_entry_get_id(te));
4888 if (err)
4889 return got_error_from_errno(
4890 "got_object_id_str");
4892 if (got_object_tree_entry_is_submodule(te))
4893 modestr = "$";
4894 else if (S_ISLNK(mode)) {
4895 int i;
4897 err = got_tree_entry_get_symlink_target(&link_target,
4898 te, s->repo);
4899 if (err) {
4900 free(id_str);
4901 return err;
4903 for (i = 0; i < strlen(link_target); i++) {
4904 if (!isprint((unsigned char)link_target[i]))
4905 link_target[i] = '?';
4907 modestr = "@";
4909 else if (S_ISDIR(mode))
4910 modestr = "/";
4911 else if (mode & S_IXUSR)
4912 modestr = "*";
4913 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4914 got_tree_entry_get_name(te), modestr,
4915 link_target ? " -> ": "",
4916 link_target ? link_target : "") == -1) {
4917 free(id_str);
4918 free(link_target);
4919 return got_error_from_errno("asprintf");
4921 free(id_str);
4922 free(link_target);
4923 err = format_line(&wline, &width, line, view->ncols, 0);
4924 if (err) {
4925 free(line);
4926 break;
4928 if (n == s->selected) {
4929 if (view->focussed)
4930 wstandout(view->window);
4931 s->selected_entry = te;
4933 tc = match_color(&s->colors, line);
4934 if (tc)
4935 wattr_on(view->window,
4936 COLOR_PAIR(tc->colorpair), NULL);
4937 waddwstr(view->window, wline);
4938 if (tc)
4939 wattr_off(view->window,
4940 COLOR_PAIR(tc->colorpair), NULL);
4941 if (width < view->ncols - 1)
4942 waddch(view->window, '\n');
4943 if (n == s->selected && view->focussed)
4944 wstandend(view->window);
4945 free(line);
4946 free(wline);
4947 wline = NULL;
4948 n++;
4949 s->ndisplayed++;
4950 s->last_displayed_entry = te;
4951 if (--limit <= 0)
4952 break;
4955 return err;
4958 static void
4959 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4961 struct got_tree_entry *te;
4962 int isroot = s->tree == s->root;
4963 int i = 0;
4965 if (s->first_displayed_entry == NULL)
4966 return;
4968 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4969 while (i++ < maxscroll) {
4970 if (te == NULL) {
4971 if (!isroot)
4972 s->first_displayed_entry = NULL;
4973 break;
4975 s->first_displayed_entry = te;
4976 te = got_tree_entry_get_prev(s->tree, te);
4980 static void
4981 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4983 struct got_tree_entry *next, *last;
4984 int n = 0;
4986 if (s->first_displayed_entry)
4987 next = got_tree_entry_get_next(s->tree,
4988 s->first_displayed_entry);
4989 else
4990 next = got_object_tree_get_first_entry(s->tree);
4992 last = s->last_displayed_entry;
4993 while (next && last && n++ < maxscroll) {
4994 last = got_tree_entry_get_next(s->tree, last);
4995 if (last) {
4996 s->first_displayed_entry = next;
4997 next = got_tree_entry_get_next(s->tree, next);
5002 static const struct got_error *
5003 tree_entry_path(char **path, struct tog_parent_trees *parents,
5004 struct got_tree_entry *te)
5006 const struct got_error *err = NULL;
5007 struct tog_parent_tree *pt;
5008 size_t len = 2; /* for leading slash and NUL */
5010 TAILQ_FOREACH(pt, parents, entry)
5011 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5012 + 1 /* slash */;
5013 if (te)
5014 len += strlen(got_tree_entry_get_name(te));
5016 *path = calloc(1, len);
5017 if (path == NULL)
5018 return got_error_from_errno("calloc");
5020 (*path)[0] = '/';
5021 pt = TAILQ_LAST(parents, tog_parent_trees);
5022 while (pt) {
5023 const char *name = got_tree_entry_get_name(pt->selected_entry);
5024 if (strlcat(*path, name, len) >= len) {
5025 err = got_error(GOT_ERR_NO_SPACE);
5026 goto done;
5028 if (strlcat(*path, "/", len) >= len) {
5029 err = got_error(GOT_ERR_NO_SPACE);
5030 goto done;
5032 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5034 if (te) {
5035 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5036 err = got_error(GOT_ERR_NO_SPACE);
5037 goto done;
5040 done:
5041 if (err) {
5042 free(*path);
5043 *path = NULL;
5045 return err;
5048 static const struct got_error *
5049 blame_tree_entry(struct tog_view **new_view, int begin_x,
5050 struct got_tree_entry *te, struct tog_parent_trees *parents,
5051 struct got_object_id *commit_id, struct got_repository *repo)
5053 const struct got_error *err = NULL;
5054 char *path;
5055 struct tog_view *blame_view;
5057 *new_view = NULL;
5059 err = tree_entry_path(&path, parents, te);
5060 if (err)
5061 return err;
5063 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5064 if (blame_view == NULL) {
5065 err = got_error_from_errno("view_open");
5066 goto done;
5069 err = open_blame_view(blame_view, path, commit_id, repo);
5070 if (err) {
5071 if (err->code == GOT_ERR_CANCELLED)
5072 err = NULL;
5073 view_close(blame_view);
5074 } else
5075 *new_view = blame_view;
5076 done:
5077 free(path);
5078 return err;
5081 static const struct got_error *
5082 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5083 struct tog_tree_view_state *s)
5085 struct tog_view *log_view;
5086 const struct got_error *err = NULL;
5087 char *path;
5089 *new_view = NULL;
5091 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5092 if (log_view == NULL)
5093 return got_error_from_errno("view_open");
5095 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5096 if (err)
5097 return err;
5099 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5100 path, 0);
5101 if (err)
5102 view_close(log_view);
5103 else
5104 *new_view = log_view;
5105 free(path);
5106 return err;
5109 static const struct got_error *
5110 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5111 const char *head_ref_name, struct got_repository *repo)
5113 const struct got_error *err = NULL;
5114 char *commit_id_str = NULL;
5115 struct tog_tree_view_state *s = &view->state.tree;
5116 struct got_commit_object *commit = NULL;
5118 TAILQ_INIT(&s->parents);
5119 STAILQ_INIT(&s->colors);
5121 s->commit_id = got_object_id_dup(commit_id);
5122 if (s->commit_id == NULL)
5123 return got_error_from_errno("got_object_id_dup");
5125 err = got_object_open_as_commit(&commit, repo, commit_id);
5126 if (err)
5127 goto done;
5130 * The root is opened here and will be closed when the view is closed.
5131 * Any visited subtrees and their path-wise parents are opened and
5132 * closed on demand.
5134 err = got_object_open_as_tree(&s->root, repo,
5135 got_object_commit_get_tree_id(commit));
5136 if (err)
5137 goto done;
5138 s->tree = s->root;
5140 err = got_object_id_str(&commit_id_str, commit_id);
5141 if (err != NULL)
5142 goto done;
5144 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5145 err = got_error_from_errno("asprintf");
5146 goto done;
5149 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5150 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5151 if (head_ref_name) {
5152 s->head_ref_name = strdup(head_ref_name);
5153 if (s->head_ref_name == NULL) {
5154 err = got_error_from_errno("strdup");
5155 goto done;
5158 s->repo = repo;
5160 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5161 err = add_color(&s->colors, "\\$$",
5162 TOG_COLOR_TREE_SUBMODULE,
5163 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5164 if (err)
5165 goto done;
5166 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5167 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5168 if (err)
5169 goto done;
5170 err = add_color(&s->colors, "/$",
5171 TOG_COLOR_TREE_DIRECTORY,
5172 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5173 if (err)
5174 goto done;
5176 err = add_color(&s->colors, "\\*$",
5177 TOG_COLOR_TREE_EXECUTABLE,
5178 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5179 if (err)
5180 goto done;
5182 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5183 get_color_value("TOG_COLOR_COMMIT"));
5184 if (err)
5185 goto done;
5188 view->show = show_tree_view;
5189 view->input = input_tree_view;
5190 view->close = close_tree_view;
5191 view->search_start = search_start_tree_view;
5192 view->search_next = search_next_tree_view;
5193 done:
5194 free(commit_id_str);
5195 if (commit)
5196 got_object_commit_close(commit);
5197 if (err)
5198 close_tree_view(view);
5199 return err;
5202 static const struct got_error *
5203 close_tree_view(struct tog_view *view)
5205 struct tog_tree_view_state *s = &view->state.tree;
5207 free_colors(&s->colors);
5208 free(s->tree_label);
5209 s->tree_label = NULL;
5210 free(s->commit_id);
5211 s->commit_id = NULL;
5212 free(s->head_ref_name);
5213 s->head_ref_name = NULL;
5214 while (!TAILQ_EMPTY(&s->parents)) {
5215 struct tog_parent_tree *parent;
5216 parent = TAILQ_FIRST(&s->parents);
5217 TAILQ_REMOVE(&s->parents, parent, entry);
5218 if (parent->tree != s->root)
5219 got_object_tree_close(parent->tree);
5220 free(parent);
5223 if (s->tree != NULL && s->tree != s->root)
5224 got_object_tree_close(s->tree);
5225 if (s->root)
5226 got_object_tree_close(s->root);
5227 return NULL;
5230 static const struct got_error *
5231 search_start_tree_view(struct tog_view *view)
5233 struct tog_tree_view_state *s = &view->state.tree;
5235 s->matched_entry = NULL;
5236 return NULL;
5239 static int
5240 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5242 regmatch_t regmatch;
5244 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5245 0) == 0;
5248 static const struct got_error *
5249 search_next_tree_view(struct tog_view *view)
5251 struct tog_tree_view_state *s = &view->state.tree;
5252 struct got_tree_entry *te = NULL;
5254 if (!view->searching) {
5255 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5256 return NULL;
5259 if (s->matched_entry) {
5260 if (view->searching == TOG_SEARCH_FORWARD) {
5261 if (s->selected_entry)
5262 te = got_tree_entry_get_next(s->tree,
5263 s->selected_entry);
5264 else
5265 te = got_object_tree_get_first_entry(s->tree);
5266 } else {
5267 if (s->selected_entry == NULL)
5268 te = got_object_tree_get_last_entry(s->tree);
5269 else
5270 te = got_tree_entry_get_prev(s->tree,
5271 s->selected_entry);
5273 } else {
5274 if (view->searching == TOG_SEARCH_FORWARD)
5275 te = got_object_tree_get_first_entry(s->tree);
5276 else
5277 te = got_object_tree_get_last_entry(s->tree);
5280 while (1) {
5281 if (te == NULL) {
5282 if (s->matched_entry == NULL) {
5283 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5284 return NULL;
5286 if (view->searching == TOG_SEARCH_FORWARD)
5287 te = got_object_tree_get_first_entry(s->tree);
5288 else
5289 te = got_object_tree_get_last_entry(s->tree);
5292 if (match_tree_entry(te, &view->regex)) {
5293 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5294 s->matched_entry = te;
5295 break;
5298 if (view->searching == TOG_SEARCH_FORWARD)
5299 te = got_tree_entry_get_next(s->tree, te);
5300 else
5301 te = got_tree_entry_get_prev(s->tree, te);
5304 if (s->matched_entry) {
5305 s->first_displayed_entry = s->matched_entry;
5306 s->selected = 0;
5309 return NULL;
5312 static const struct got_error *
5313 show_tree_view(struct tog_view *view)
5315 const struct got_error *err = NULL;
5316 struct tog_tree_view_state *s = &view->state.tree;
5317 char *parent_path;
5319 err = tree_entry_path(&parent_path, &s->parents, NULL);
5320 if (err)
5321 return err;
5323 err = draw_tree_entries(view, parent_path);
5324 free(parent_path);
5326 view_vborder(view);
5327 return err;
5330 static const struct got_error *
5331 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5333 const struct got_error *err = NULL;
5334 struct tog_tree_view_state *s = &view->state.tree;
5335 struct tog_view *log_view, *ref_view;
5336 int begin_x = 0;
5338 switch (ch) {
5339 case 'i':
5340 s->show_ids = !s->show_ids;
5341 break;
5342 case 'l':
5343 if (!s->selected_entry)
5344 break;
5345 if (view_is_parent_view(view))
5346 begin_x = view_split_begin_x(view->begin_x);
5347 err = log_selected_tree_entry(&log_view, begin_x, s);
5348 view->focussed = 0;
5349 log_view->focussed = 1;
5350 if (view_is_parent_view(view)) {
5351 err = view_close_child(view);
5352 if (err)
5353 return err;
5354 view_set_child(view, log_view);
5355 view->focus_child = 1;
5356 } else
5357 *new_view = log_view;
5358 break;
5359 case 'r':
5360 if (view_is_parent_view(view))
5361 begin_x = view_split_begin_x(view->begin_x);
5362 ref_view = view_open(view->nlines, view->ncols,
5363 view->begin_y, begin_x, TOG_VIEW_REF);
5364 if (ref_view == NULL)
5365 return got_error_from_errno("view_open");
5366 err = open_ref_view(ref_view, s->repo);
5367 if (err) {
5368 view_close(ref_view);
5369 return err;
5371 view->focussed = 0;
5372 ref_view->focussed = 1;
5373 if (view_is_parent_view(view)) {
5374 err = view_close_child(view);
5375 if (err)
5376 return err;
5377 view_set_child(view, ref_view);
5378 view->focus_child = 1;
5379 } else
5380 *new_view = ref_view;
5381 break;
5382 case 'k':
5383 case KEY_UP:
5384 if (s->selected > 0) {
5385 s->selected--;
5386 break;
5388 tree_scroll_up(s, 1);
5389 break;
5390 case KEY_PPAGE:
5391 case CTRL('b'):
5392 if (s->tree == s->root) {
5393 if (got_object_tree_get_first_entry(s->tree) ==
5394 s->first_displayed_entry)
5395 s->selected = 0;
5396 } else {
5397 if (s->first_displayed_entry == NULL)
5398 s->selected = 0;
5400 tree_scroll_up(s, MAX(0, view->nlines - 3));
5401 break;
5402 case 'j':
5403 case KEY_DOWN:
5404 if (s->selected < s->ndisplayed - 1) {
5405 s->selected++;
5406 break;
5408 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5409 == NULL)
5410 /* can't scroll any further */
5411 break;
5412 tree_scroll_down(s, 1);
5413 break;
5414 case KEY_NPAGE:
5415 case CTRL('f'):
5416 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5417 == NULL) {
5418 /* can't scroll any further; move cursor down */
5419 if (s->selected < s->ndisplayed - 1)
5420 s->selected = s->ndisplayed - 1;
5421 break;
5423 tree_scroll_down(s, view->nlines - 3);
5424 break;
5425 case KEY_ENTER:
5426 case '\r':
5427 case KEY_BACKSPACE:
5428 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5429 struct tog_parent_tree *parent;
5430 /* user selected '..' */
5431 if (s->tree == s->root)
5432 break;
5433 parent = TAILQ_FIRST(&s->parents);
5434 TAILQ_REMOVE(&s->parents, parent,
5435 entry);
5436 got_object_tree_close(s->tree);
5437 s->tree = parent->tree;
5438 s->first_displayed_entry =
5439 parent->first_displayed_entry;
5440 s->selected_entry =
5441 parent->selected_entry;
5442 s->selected = parent->selected;
5443 free(parent);
5444 } else if (S_ISDIR(got_tree_entry_get_mode(
5445 s->selected_entry))) {
5446 struct got_tree_object *subtree;
5447 err = got_object_open_as_tree(&subtree, s->repo,
5448 got_tree_entry_get_id(s->selected_entry));
5449 if (err)
5450 break;
5451 err = tree_view_visit_subtree(s, subtree);
5452 if (err) {
5453 got_object_tree_close(subtree);
5454 break;
5456 } else if (S_ISREG(got_tree_entry_get_mode(
5457 s->selected_entry))) {
5458 struct tog_view *blame_view;
5459 int begin_x = view_is_parent_view(view) ?
5460 view_split_begin_x(view->begin_x) : 0;
5462 err = blame_tree_entry(&blame_view, begin_x,
5463 s->selected_entry, &s->parents,
5464 s->commit_id, s->repo);
5465 if (err)
5466 break;
5467 view->focussed = 0;
5468 blame_view->focussed = 1;
5469 if (view_is_parent_view(view)) {
5470 err = view_close_child(view);
5471 if (err)
5472 return err;
5473 view_set_child(view, blame_view);
5474 view->focus_child = 1;
5475 } else
5476 *new_view = blame_view;
5478 break;
5479 case KEY_RESIZE:
5480 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5481 s->selected = view->nlines - 4;
5482 break;
5483 default:
5484 break;
5487 return err;
5490 __dead static void
5491 usage_tree(void)
5493 endwin();
5494 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5495 getprogname());
5496 exit(1);
5499 static const struct got_error *
5500 cmd_tree(int argc, char *argv[])
5502 const struct got_error *error;
5503 struct got_repository *repo = NULL;
5504 struct got_worktree *worktree = NULL;
5505 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5506 struct got_object_id *commit_id = NULL;
5507 const char *commit_id_arg = NULL;
5508 char *label = NULL;
5509 struct got_reference *ref = NULL;
5510 const char *head_ref_name = NULL;
5511 int ch;
5512 struct tog_view *view;
5514 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5515 switch (ch) {
5516 case 'c':
5517 commit_id_arg = optarg;
5518 break;
5519 case 'r':
5520 repo_path = realpath(optarg, NULL);
5521 if (repo_path == NULL)
5522 return got_error_from_errno2("realpath",
5523 optarg);
5524 break;
5525 default:
5526 usage_tree();
5527 /* NOTREACHED */
5531 argc -= optind;
5532 argv += optind;
5534 if (argc > 1)
5535 usage_tree();
5537 if (repo_path == NULL) {
5538 cwd = getcwd(NULL, 0);
5539 if (cwd == NULL)
5540 return got_error_from_errno("getcwd");
5541 error = got_worktree_open(&worktree, cwd);
5542 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5543 goto done;
5544 if (worktree)
5545 repo_path =
5546 strdup(got_worktree_get_repo_path(worktree));
5547 else
5548 repo_path = strdup(cwd);
5549 if (repo_path == NULL) {
5550 error = got_error_from_errno("strdup");
5551 goto done;
5555 error = got_repo_open(&repo, repo_path, NULL);
5556 if (error != NULL)
5557 goto done;
5559 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5560 repo, worktree);
5561 if (error)
5562 goto done;
5564 init_curses();
5566 error = apply_unveil(got_repo_get_path(repo), NULL);
5567 if (error)
5568 goto done;
5570 error = tog_load_refs(repo);
5571 if (error)
5572 goto done;
5574 if (commit_id_arg == NULL) {
5575 error = got_repo_match_object_id(&commit_id, &label,
5576 worktree ? got_worktree_get_head_ref_name(worktree) :
5577 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5578 if (error)
5579 goto done;
5580 head_ref_name = label;
5581 } else {
5582 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5583 if (error == NULL)
5584 head_ref_name = got_ref_get_name(ref);
5585 else if (error->code != GOT_ERR_NOT_REF)
5586 goto done;
5587 error = got_repo_match_object_id(&commit_id, NULL,
5588 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5589 if (error)
5590 goto done;
5593 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5594 if (view == NULL) {
5595 error = got_error_from_errno("view_open");
5596 goto done;
5598 error = open_tree_view(view, commit_id, head_ref_name, repo);
5599 if (error)
5600 goto done;
5601 if (!got_path_is_root_dir(in_repo_path)) {
5602 error = tree_view_walk_path(&view->state.tree, commit_id,
5603 in_repo_path);
5604 if (error)
5605 goto done;
5608 if (worktree) {
5609 /* Release work tree lock. */
5610 got_worktree_close(worktree);
5611 worktree = NULL;
5613 error = view_loop(view);
5614 done:
5615 free(repo_path);
5616 free(cwd);
5617 free(commit_id);
5618 free(label);
5619 if (ref)
5620 got_ref_close(ref);
5621 if (repo) {
5622 const struct got_error *close_err = got_repo_close(repo);
5623 if (error == NULL)
5624 error = close_err;
5626 tog_free_refs();
5627 return error;
5630 static const struct got_error *
5631 ref_view_load_refs(struct tog_ref_view_state *s)
5633 struct got_reflist_entry *sre;
5634 struct tog_reflist_entry *re;
5636 s->nrefs = 0;
5637 TAILQ_FOREACH(sre, &tog_refs, entry) {
5638 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5639 continue;
5641 re = malloc(sizeof(*re));
5642 if (re == NULL)
5643 return got_error_from_errno("malloc");
5645 re->ref = got_ref_dup(sre->ref);
5646 if (re->ref == NULL)
5647 return got_error_from_errno("got_ref_dup");
5648 re->idx = s->nrefs++;
5649 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5652 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5653 return NULL;
5656 void
5657 ref_view_free_refs(struct tog_ref_view_state *s)
5659 struct tog_reflist_entry *re;
5661 while (!TAILQ_EMPTY(&s->refs)) {
5662 re = TAILQ_FIRST(&s->refs);
5663 TAILQ_REMOVE(&s->refs, re, entry);
5664 got_ref_close(re->ref);
5665 free(re);
5669 static const struct got_error *
5670 open_ref_view(struct tog_view *view, struct got_repository *repo)
5672 const struct got_error *err = NULL;
5673 struct tog_ref_view_state *s = &view->state.ref;
5675 s->selected_entry = 0;
5676 s->repo = repo;
5678 TAILQ_INIT(&s->refs);
5679 STAILQ_INIT(&s->colors);
5681 err = ref_view_load_refs(s);
5682 if (err)
5683 return err;
5685 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5686 err = add_color(&s->colors, "^refs/heads/",
5687 TOG_COLOR_REFS_HEADS,
5688 get_color_value("TOG_COLOR_REFS_HEADS"));
5689 if (err)
5690 goto done;
5692 err = add_color(&s->colors, "^refs/tags/",
5693 TOG_COLOR_REFS_TAGS,
5694 get_color_value("TOG_COLOR_REFS_TAGS"));
5695 if (err)
5696 goto done;
5698 err = add_color(&s->colors, "^refs/remotes/",
5699 TOG_COLOR_REFS_REMOTES,
5700 get_color_value("TOG_COLOR_REFS_REMOTES"));
5701 if (err)
5702 goto done;
5705 view->show = show_ref_view;
5706 view->input = input_ref_view;
5707 view->close = close_ref_view;
5708 view->search_start = search_start_ref_view;
5709 view->search_next = search_next_ref_view;
5710 done:
5711 if (err)
5712 free_colors(&s->colors);
5713 return err;
5716 static const struct got_error *
5717 close_ref_view(struct tog_view *view)
5719 struct tog_ref_view_state *s = &view->state.ref;
5721 ref_view_free_refs(s);
5722 free_colors(&s->colors);
5724 return NULL;
5727 static const struct got_error *
5728 resolve_reflist_entry(struct got_object_id **commit_id,
5729 struct tog_reflist_entry *re, struct got_repository *repo)
5731 const struct got_error *err = NULL;
5732 struct got_object_id *obj_id;
5733 struct got_tag_object *tag = NULL;
5734 int obj_type;
5736 *commit_id = NULL;
5738 err = got_ref_resolve(&obj_id, repo, re->ref);
5739 if (err)
5740 return err;
5742 err = got_object_get_type(&obj_type, repo, obj_id);
5743 if (err)
5744 goto done;
5746 switch (obj_type) {
5747 case GOT_OBJ_TYPE_COMMIT:
5748 *commit_id = obj_id;
5749 break;
5750 case GOT_OBJ_TYPE_TAG:
5751 err = got_object_open_as_tag(&tag, repo, obj_id);
5752 if (err)
5753 goto done;
5754 free(obj_id);
5755 err = got_object_get_type(&obj_type, repo,
5756 got_object_tag_get_object_id(tag));
5757 if (err)
5758 goto done;
5759 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5760 err = got_error(GOT_ERR_OBJ_TYPE);
5761 goto done;
5763 *commit_id = got_object_id_dup(
5764 got_object_tag_get_object_id(tag));
5765 if (*commit_id == NULL) {
5766 err = got_error_from_errno("got_object_id_dup");
5767 goto done;
5769 break;
5770 default:
5771 err = got_error(GOT_ERR_OBJ_TYPE);
5772 break;
5775 done:
5776 if (tag)
5777 got_object_tag_close(tag);
5778 if (err) {
5779 free(*commit_id);
5780 *commit_id = NULL;
5782 return err;
5785 static const struct got_error *
5786 log_ref_entry(struct tog_view **new_view, int begin_x,
5787 struct tog_reflist_entry *re, struct got_repository *repo)
5789 struct tog_view *log_view;
5790 const struct got_error *err = NULL;
5791 struct got_object_id *commit_id = NULL;
5793 *new_view = NULL;
5795 err = resolve_reflist_entry(&commit_id, re, repo);
5796 if (err) {
5797 if (err->code != GOT_ERR_OBJ_TYPE)
5798 return err;
5799 else
5800 return NULL;
5803 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5804 if (log_view == NULL) {
5805 err = got_error_from_errno("view_open");
5806 goto done;
5809 err = open_log_view(log_view, commit_id, repo,
5810 got_ref_get_name(re->ref), "", 0);
5811 done:
5812 if (err)
5813 view_close(log_view);
5814 else
5815 *new_view = log_view;
5816 free(commit_id);
5817 return err;
5820 static void
5821 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5823 struct tog_reflist_entry *re;
5824 int i = 0;
5826 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5827 return;
5829 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5830 while (i++ < maxscroll) {
5831 if (re == NULL)
5832 break;
5833 s->first_displayed_entry = re;
5834 re = TAILQ_PREV(re, tog_reflist_head, entry);
5838 static void
5839 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5841 struct tog_reflist_entry *next, *last;
5842 int n = 0;
5844 if (s->first_displayed_entry)
5845 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5846 else
5847 next = TAILQ_FIRST(&s->refs);
5849 last = s->last_displayed_entry;
5850 while (next && last && n++ < maxscroll) {
5851 last = TAILQ_NEXT(last, entry);
5852 if (last) {
5853 s->first_displayed_entry = next;
5854 next = TAILQ_NEXT(next, entry);
5859 static const struct got_error *
5860 search_start_ref_view(struct tog_view *view)
5862 struct tog_ref_view_state *s = &view->state.ref;
5864 s->matched_entry = NULL;
5865 return NULL;
5868 static int
5869 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5871 regmatch_t regmatch;
5873 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5874 0) == 0;
5877 static const struct got_error *
5878 search_next_ref_view(struct tog_view *view)
5880 struct tog_ref_view_state *s = &view->state.ref;
5881 struct tog_reflist_entry *re = NULL;
5883 if (!view->searching) {
5884 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5885 return NULL;
5888 if (s->matched_entry) {
5889 if (view->searching == TOG_SEARCH_FORWARD) {
5890 if (s->selected_entry)
5891 re = TAILQ_NEXT(s->selected_entry, entry);
5892 else
5893 re = TAILQ_PREV(s->selected_entry,
5894 tog_reflist_head, entry);
5895 } else {
5896 if (s->selected_entry == NULL)
5897 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5898 else
5899 re = TAILQ_PREV(s->selected_entry,
5900 tog_reflist_head, entry);
5902 } else {
5903 if (view->searching == TOG_SEARCH_FORWARD)
5904 re = TAILQ_FIRST(&s->refs);
5905 else
5906 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5909 while (1) {
5910 if (re == NULL) {
5911 if (s->matched_entry == NULL) {
5912 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5913 return NULL;
5915 if (view->searching == TOG_SEARCH_FORWARD)
5916 re = TAILQ_FIRST(&s->refs);
5917 else
5918 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5921 if (match_reflist_entry(re, &view->regex)) {
5922 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5923 s->matched_entry = re;
5924 break;
5927 if (view->searching == TOG_SEARCH_FORWARD)
5928 re = TAILQ_NEXT(re, entry);
5929 else
5930 re = TAILQ_PREV(re, tog_reflist_head, entry);
5933 if (s->matched_entry) {
5934 s->first_displayed_entry = s->matched_entry;
5935 s->selected = 0;
5938 return NULL;
5941 static const struct got_error *
5942 show_ref_view(struct tog_view *view)
5944 const struct got_error *err = NULL;
5945 struct tog_ref_view_state *s = &view->state.ref;
5946 struct tog_reflist_entry *re;
5947 char *line = NULL;
5948 wchar_t *wline;
5949 struct tog_color *tc;
5950 int width, n;
5951 int limit = view->nlines;
5953 werase(view->window);
5955 s->ndisplayed = 0;
5957 if (limit == 0)
5958 return NULL;
5960 re = s->first_displayed_entry;
5962 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5963 s->nrefs) == -1)
5964 return got_error_from_errno("asprintf");
5966 err = format_line(&wline, &width, line, view->ncols, 0);
5967 if (err) {
5968 free(line);
5969 return err;
5971 if (view_needs_focus_indication(view))
5972 wstandout(view->window);
5973 waddwstr(view->window, wline);
5974 if (view_needs_focus_indication(view))
5975 wstandend(view->window);
5976 free(wline);
5977 wline = NULL;
5978 free(line);
5979 line = NULL;
5980 if (width < view->ncols - 1)
5981 waddch(view->window, '\n');
5982 if (--limit <= 0)
5983 return NULL;
5985 n = 0;
5986 while (re && limit > 0) {
5987 char *line = NULL;
5989 if (got_ref_is_symbolic(re->ref)) {
5990 if (asprintf(&line, "%s -> %s",
5991 got_ref_get_name(re->ref),
5992 got_ref_get_symref_target(re->ref)) == -1)
5993 return got_error_from_errno("asprintf");
5994 } else if (s->show_ids) {
5995 struct got_object_id *id;
5996 char *id_str;
5997 err = got_ref_resolve(&id, s->repo, re->ref);
5998 if (err)
5999 return err;
6000 err = got_object_id_str(&id_str, id);
6001 if (err) {
6002 free(id);
6003 return err;
6005 if (asprintf(&line, "%s: %s",
6006 got_ref_get_name(re->ref), id_str) == -1) {
6007 err = got_error_from_errno("asprintf");
6008 free(id);
6009 free(id_str);
6010 return err;
6012 free(id);
6013 free(id_str);
6014 } else {
6015 line = strdup(got_ref_get_name(re->ref));
6016 if (line == NULL)
6017 return got_error_from_errno("strdup");
6020 err = format_line(&wline, &width, line, view->ncols, 0);
6021 if (err) {
6022 free(line);
6023 return err;
6025 if (n == s->selected) {
6026 if (view->focussed)
6027 wstandout(view->window);
6028 s->selected_entry = re;
6030 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6031 if (tc)
6032 wattr_on(view->window,
6033 COLOR_PAIR(tc->colorpair), NULL);
6034 waddwstr(view->window, wline);
6035 if (tc)
6036 wattr_off(view->window,
6037 COLOR_PAIR(tc->colorpair), NULL);
6038 if (width < view->ncols - 1)
6039 waddch(view->window, '\n');
6040 if (n == s->selected && view->focussed)
6041 wstandend(view->window);
6042 free(line);
6043 free(wline);
6044 wline = NULL;
6045 n++;
6046 s->ndisplayed++;
6047 s->last_displayed_entry = re;
6049 limit--;
6050 re = TAILQ_NEXT(re, entry);
6053 view_vborder(view);
6054 return err;
6057 static const struct got_error *
6058 browse_ref_tree(struct tog_view **new_view, int begin_x,
6059 struct tog_reflist_entry *re, struct got_repository *repo)
6061 const struct got_error *err = NULL;
6062 struct got_object_id *commit_id = NULL;
6063 struct tog_view *tree_view;
6065 *new_view = NULL;
6067 err = resolve_reflist_entry(&commit_id, re, repo);
6068 if (err) {
6069 if (err->code != GOT_ERR_OBJ_TYPE)
6070 return err;
6071 else
6072 return NULL;
6076 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6077 if (tree_view == NULL) {
6078 err = got_error_from_errno("view_open");
6079 goto done;
6082 err = open_tree_view(tree_view, commit_id,
6083 got_ref_get_name(re->ref), repo);
6084 if (err)
6085 goto done;
6087 *new_view = tree_view;
6088 done:
6089 free(commit_id);
6090 return err;
6092 static const struct got_error *
6093 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6095 const struct got_error *err = NULL;
6096 struct tog_ref_view_state *s = &view->state.ref;
6097 struct tog_view *log_view, *tree_view;
6098 int begin_x = 0;
6100 switch (ch) {
6101 case 'i':
6102 s->show_ids = !s->show_ids;
6103 break;
6104 case KEY_ENTER:
6105 case '\r':
6106 if (!s->selected_entry)
6107 break;
6108 if (view_is_parent_view(view))
6109 begin_x = view_split_begin_x(view->begin_x);
6110 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6111 s->repo);
6112 view->focussed = 0;
6113 log_view->focussed = 1;
6114 if (view_is_parent_view(view)) {
6115 err = view_close_child(view);
6116 if (err)
6117 return err;
6118 view_set_child(view, log_view);
6119 view->focus_child = 1;
6120 } else
6121 *new_view = log_view;
6122 break;
6123 case 't':
6124 if (!s->selected_entry)
6125 break;
6126 if (view_is_parent_view(view))
6127 begin_x = view_split_begin_x(view->begin_x);
6128 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6129 s->repo);
6130 if (err || tree_view == NULL)
6131 break;
6132 view->focussed = 0;
6133 tree_view->focussed = 1;
6134 if (view_is_parent_view(view)) {
6135 err = view_close_child(view);
6136 if (err)
6137 return err;
6138 view_set_child(view, tree_view);
6139 view->focus_child = 1;
6140 } else
6141 *new_view = tree_view;
6142 break;
6143 case 'k':
6144 case KEY_UP:
6145 if (s->selected > 0) {
6146 s->selected--;
6147 break;
6149 ref_scroll_up(s, 1);
6150 break;
6151 case KEY_PPAGE:
6152 case CTRL('b'):
6153 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6154 s->selected = 0;
6155 ref_scroll_up(s, MAX(0, view->nlines - 1));
6156 break;
6157 case 'j':
6158 case KEY_DOWN:
6159 if (s->selected < s->ndisplayed - 1) {
6160 s->selected++;
6161 break;
6163 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6164 /* can't scroll any further */
6165 break;
6166 ref_scroll_down(s, 1);
6167 break;
6168 case KEY_NPAGE:
6169 case CTRL('f'):
6170 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6171 /* can't scroll any further; move cursor down */
6172 if (s->selected < s->ndisplayed - 1)
6173 s->selected = s->ndisplayed - 1;
6174 break;
6176 ref_scroll_down(s, view->nlines - 1);
6177 break;
6178 case CTRL('l'):
6179 tog_free_refs();
6180 err = tog_load_refs(s->repo);
6181 if (err)
6182 break;
6183 ref_view_free_refs(s);
6184 err = ref_view_load_refs(s);
6185 break;
6186 case KEY_RESIZE:
6187 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6188 s->selected = view->nlines - 2;
6189 break;
6190 default:
6191 break;
6194 return err;
6197 __dead static void
6198 usage_ref(void)
6200 endwin();
6201 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6202 getprogname());
6203 exit(1);
6206 static const struct got_error *
6207 cmd_ref(int argc, char *argv[])
6209 const struct got_error *error;
6210 struct got_repository *repo = NULL;
6211 struct got_worktree *worktree = NULL;
6212 char *cwd = NULL, *repo_path = NULL;
6213 int ch;
6214 struct tog_view *view;
6216 while ((ch = getopt(argc, argv, "r:")) != -1) {
6217 switch (ch) {
6218 case 'r':
6219 repo_path = realpath(optarg, NULL);
6220 if (repo_path == NULL)
6221 return got_error_from_errno2("realpath",
6222 optarg);
6223 break;
6224 default:
6225 usage_ref();
6226 /* NOTREACHED */
6230 argc -= optind;
6231 argv += optind;
6233 if (argc > 1)
6234 usage_ref();
6236 if (repo_path == NULL) {
6237 cwd = getcwd(NULL, 0);
6238 if (cwd == NULL)
6239 return got_error_from_errno("getcwd");
6240 error = got_worktree_open(&worktree, cwd);
6241 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6242 goto done;
6243 if (worktree)
6244 repo_path =
6245 strdup(got_worktree_get_repo_path(worktree));
6246 else
6247 repo_path = strdup(cwd);
6248 if (repo_path == NULL) {
6249 error = got_error_from_errno("strdup");
6250 goto done;
6254 error = got_repo_open(&repo, repo_path, NULL);
6255 if (error != NULL)
6256 goto done;
6258 init_curses();
6260 error = apply_unveil(got_repo_get_path(repo), NULL);
6261 if (error)
6262 goto done;
6264 error = tog_load_refs(repo);
6265 if (error)
6266 goto done;
6268 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6269 if (view == NULL) {
6270 error = got_error_from_errno("view_open");
6271 goto done;
6274 error = open_ref_view(view, repo);
6275 if (error)
6276 goto done;
6278 if (worktree) {
6279 /* Release work tree lock. */
6280 got_worktree_close(worktree);
6281 worktree = NULL;
6283 error = view_loop(view);
6284 done:
6285 free(repo_path);
6286 free(cwd);
6287 if (repo) {
6288 const struct got_error *close_err = got_repo_close(repo);
6289 if (close_err)
6290 error = close_err;
6292 tog_free_refs();
6293 return error;
6296 static void
6297 list_commands(FILE *fp)
6299 size_t i;
6301 fprintf(fp, "commands:");
6302 for (i = 0; i < nitems(tog_commands); i++) {
6303 struct tog_cmd *cmd = &tog_commands[i];
6304 fprintf(fp, " %s", cmd->name);
6306 fputc('\n', fp);
6309 __dead static void
6310 usage(int hflag, int status)
6312 FILE *fp = (status == 0) ? stdout : stderr;
6314 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6315 getprogname());
6316 if (hflag) {
6317 fprintf(fp, "lazy usage: %s path\n", getprogname());
6318 list_commands(fp);
6320 exit(status);
6323 static char **
6324 make_argv(int argc, ...)
6326 va_list ap;
6327 char **argv;
6328 int i;
6330 va_start(ap, argc);
6332 argv = calloc(argc, sizeof(char *));
6333 if (argv == NULL)
6334 err(1, "calloc");
6335 for (i = 0; i < argc; i++) {
6336 argv[i] = strdup(va_arg(ap, char *));
6337 if (argv[i] == NULL)
6338 err(1, "strdup");
6341 va_end(ap);
6342 return argv;
6346 * Try to convert 'tog path' into a 'tog log path' command.
6347 * The user could simply have mistyped the command rather than knowingly
6348 * provided a path. So check whether argv[0] can in fact be resolved
6349 * to a path in the HEAD commit and print a special error if not.
6350 * This hack is for mpi@ <3
6352 static const struct got_error *
6353 tog_log_with_path(int argc, char *argv[])
6355 const struct got_error *error = NULL, *close_err;
6356 struct tog_cmd *cmd = NULL;
6357 struct got_repository *repo = NULL;
6358 struct got_worktree *worktree = NULL;
6359 struct got_object_id *commit_id = NULL, *id = NULL;
6360 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6361 char *commit_id_str = NULL, **cmd_argv = NULL;
6363 cwd = getcwd(NULL, 0);
6364 if (cwd == NULL)
6365 return got_error_from_errno("getcwd");
6367 error = got_worktree_open(&worktree, cwd);
6368 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6369 goto done;
6371 if (worktree)
6372 repo_path = strdup(got_worktree_get_repo_path(worktree));
6373 else
6374 repo_path = strdup(cwd);
6375 if (repo_path == NULL) {
6376 error = got_error_from_errno("strdup");
6377 goto done;
6380 error = got_repo_open(&repo, repo_path, NULL);
6381 if (error != NULL)
6382 goto done;
6384 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6385 repo, worktree);
6386 if (error)
6387 goto done;
6389 error = tog_load_refs(repo);
6390 if (error)
6391 goto done;
6392 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6393 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6394 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6395 if (error)
6396 goto done;
6398 if (worktree) {
6399 got_worktree_close(worktree);
6400 worktree = NULL;
6403 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6404 if (error) {
6405 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6406 goto done;
6407 fprintf(stderr, "%s: '%s' is no known command or path\n",
6408 getprogname(), argv[0]);
6409 usage(1, 1);
6410 /* not reached */
6413 close_err = got_repo_close(repo);
6414 if (error == NULL)
6415 error = close_err;
6416 repo = NULL;
6418 error = got_object_id_str(&commit_id_str, commit_id);
6419 if (error)
6420 goto done;
6422 cmd = &tog_commands[0]; /* log */
6423 argc = 4;
6424 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6425 error = cmd->cmd_main(argc, cmd_argv);
6426 done:
6427 if (repo) {
6428 close_err = got_repo_close(repo);
6429 if (error == NULL)
6430 error = close_err;
6432 if (worktree)
6433 got_worktree_close(worktree);
6434 free(id);
6435 free(commit_id_str);
6436 free(commit_id);
6437 free(cwd);
6438 free(repo_path);
6439 free(in_repo_path);
6440 if (cmd_argv) {
6441 int i;
6442 for (i = 0; i < argc; i++)
6443 free(cmd_argv[i]);
6444 free(cmd_argv);
6446 tog_free_refs();
6447 return error;
6450 int
6451 main(int argc, char *argv[])
6453 const struct got_error *error = NULL;
6454 struct tog_cmd *cmd = NULL;
6455 int ch, hflag = 0, Vflag = 0;
6456 char **cmd_argv = NULL;
6457 static struct option longopts[] = {
6458 { "version", no_argument, NULL, 'V' },
6459 { NULL, 0, NULL, 0}
6462 setlocale(LC_CTYPE, "");
6464 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6465 switch (ch) {
6466 case 'h':
6467 hflag = 1;
6468 break;
6469 case 'V':
6470 Vflag = 1;
6471 break;
6472 default:
6473 usage(hflag, 1);
6474 /* NOTREACHED */
6478 argc -= optind;
6479 argv += optind;
6480 optind = 1;
6481 optreset = 1;
6483 if (Vflag) {
6484 got_version_print_str();
6485 return 0;
6488 #ifndef PROFILE
6489 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6490 NULL) == -1)
6491 err(1, "pledge");
6492 #endif
6494 if (argc == 0) {
6495 if (hflag)
6496 usage(hflag, 0);
6497 /* Build an argument vector which runs a default command. */
6498 cmd = &tog_commands[0];
6499 argc = 1;
6500 cmd_argv = make_argv(argc, cmd->name);
6501 } else {
6502 size_t i;
6504 /* Did the user specify a command? */
6505 for (i = 0; i < nitems(tog_commands); i++) {
6506 if (strncmp(tog_commands[i].name, argv[0],
6507 strlen(argv[0])) == 0) {
6508 cmd = &tog_commands[i];
6509 break;
6514 if (cmd == NULL) {
6515 if (argc != 1)
6516 usage(0, 1);
6517 /* No command specified; try log with a path */
6518 error = tog_log_with_path(argc, argv);
6519 } else {
6520 if (hflag)
6521 cmd->cmd_usage();
6522 else
6523 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6526 endwin();
6527 putchar('\n');
6528 if (cmd_argv) {
6529 int i;
6530 for (i = 0; i < argc; i++)
6531 free(cmd_argv[i]);
6532 free(cmd_argv);
6535 if (error && error->code != GOT_ERR_CANCELLED)
6536 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6537 return 0;