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 SIMPLEQ_ENTRY(tog_color) entry;
123 regex_t regex;
124 short colorpair;
125 };
126 SIMPLEQ_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 SIMPLEQ_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 (!SIMPLEQ_EMPTY(colors)) {
194 tc = SIMPLEQ_FIRST(colors);
195 SIMPLEQ_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 SIMPLEQ_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_tree_object *root;
415 struct got_tree_object *tree;
416 struct got_tree_entry *first_displayed_entry;
417 struct got_tree_entry *last_displayed_entry;
418 struct got_tree_entry *selected_entry;
419 int ndisplayed, selected, show_ids;
420 struct tog_parent_trees parents;
421 struct got_object_id *commit_id;
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 searching;
506 #define TOG_SEARCH_FORWARD 1
507 #define TOG_SEARCH_BACKWARD 2
508 int search_next_done;
509 #define TOG_SEARCH_HAVE_MORE 1
510 #define TOG_SEARCH_NO_MORE 2
511 #define TOG_SEARCH_HAVE_NONE 3
512 regex_t regex;
513 regmatch_t regmatch;
514 };
516 static const struct got_error *open_diff_view(struct tog_view *,
517 struct got_object_id *, struct got_object_id *,
518 const char *, const char *, int, int, int, struct tog_view *,
519 struct got_repository *);
520 static const struct got_error *show_diff_view(struct tog_view *);
521 static const struct got_error *input_diff_view(struct tog_view **,
522 struct tog_view *, int);
523 static const struct got_error* close_diff_view(struct tog_view *);
524 static const struct got_error *search_start_diff_view(struct tog_view *);
525 static const struct got_error *search_next_diff_view(struct tog_view *);
527 static const struct got_error *open_log_view(struct tog_view *,
528 struct got_object_id *, struct got_repository *,
529 const char *, const char *, int);
530 static const struct got_error * show_log_view(struct tog_view *);
531 static const struct got_error *input_log_view(struct tog_view **,
532 struct tog_view *, int);
533 static const struct got_error *close_log_view(struct tog_view *);
534 static const struct got_error *search_start_log_view(struct tog_view *);
535 static const struct got_error *search_next_log_view(struct tog_view *);
537 static const struct got_error *open_blame_view(struct tog_view *, char *,
538 struct got_object_id *, struct got_repository *);
539 static const struct got_error *show_blame_view(struct tog_view *);
540 static const struct got_error *input_blame_view(struct tog_view **,
541 struct tog_view *, int);
542 static const struct got_error *close_blame_view(struct tog_view *);
543 static const struct got_error *search_start_blame_view(struct tog_view *);
544 static const struct got_error *search_next_blame_view(struct tog_view *);
546 static const struct got_error *open_tree_view(struct tog_view *,
547 struct got_tree_object *, struct got_object_id *, const char *,
548 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->nlines < 1)
789 return NULL;
791 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
792 wclrtoeol(view->window);
794 nocbreak();
795 echo();
796 ret = wgetnstr(view->window, pattern, sizeof(pattern));
797 cbreak();
798 noecho();
799 if (ret == ERR)
800 return NULL;
802 if (view->searching) {
803 regfree(&view->regex);
804 view->searching = 0;
807 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
808 err = view->search_start(view);
809 if (err) {
810 regfree(&view->regex);
811 return err;
813 view->searching = TOG_SEARCH_FORWARD;
814 view->search_next_done = 0;
815 view->search_next(view);
818 return NULL;
821 static const struct got_error *
822 view_input(struct tog_view **new, int *done, struct tog_view *view,
823 struct tog_view_list_head *views)
825 const struct got_error *err = NULL;
826 struct tog_view *v;
827 int ch, errcode;
829 *new = NULL;
831 /* Clear "no matches" indicator. */
832 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
833 view->search_next_done == TOG_SEARCH_HAVE_NONE)
834 view->search_next_done = TOG_SEARCH_HAVE_MORE;
836 if (view->searching && !view->search_next_done) {
837 errcode = pthread_mutex_unlock(&tog_mutex);
838 if (errcode)
839 return got_error_set_errno(errcode,
840 "pthread_mutex_unlock");
841 pthread_yield();
842 errcode = pthread_mutex_lock(&tog_mutex);
843 if (errcode)
844 return got_error_set_errno(errcode,
845 "pthread_mutex_lock");
846 view->search_next(view);
847 return NULL;
850 nodelay(stdscr, FALSE);
851 /* Allow threads to make progress while we are waiting for input. */
852 errcode = pthread_mutex_unlock(&tog_mutex);
853 if (errcode)
854 return got_error_set_errno(errcode, "pthread_mutex_unlock");
855 ch = wgetch(view->window);
856 errcode = pthread_mutex_lock(&tog_mutex);
857 if (errcode)
858 return got_error_set_errno(errcode, "pthread_mutex_lock");
859 nodelay(stdscr, TRUE);
861 if (tog_sigwinch_received || tog_sigcont_received) {
862 tog_resizeterm();
863 tog_sigwinch_received = 0;
864 tog_sigcont_received = 0;
865 TAILQ_FOREACH(v, views, entry) {
866 err = view_resize(v);
867 if (err)
868 return err;
869 err = v->input(new, v, KEY_RESIZE);
870 if (err)
871 return err;
872 if (v->child) {
873 err = view_resize(v->child);
874 if (err)
875 return err;
876 err = v->child->input(new, v->child,
877 KEY_RESIZE);
878 if (err)
879 return err;
884 switch (ch) {
885 case ERR:
886 break;
887 case '\t':
888 if (view->child) {
889 view->focussed = 0;
890 view->child->focussed = 1;
891 view->focus_child = 1;
892 } else if (view->parent) {
893 view->focussed = 0;
894 view->parent->focussed = 1;
895 view->parent->focus_child = 0;
897 break;
898 case 'q':
899 err = view->input(new, view, ch);
900 view->dying = 1;
901 break;
902 case 'Q':
903 *done = 1;
904 break;
905 case 'f':
906 if (view_is_parent_view(view)) {
907 if (view->child == NULL)
908 break;
909 if (view_is_splitscreen(view->child)) {
910 view->focussed = 0;
911 view->child->focussed = 1;
912 err = view_fullscreen(view->child);
913 } else
914 err = view_splitscreen(view->child);
915 if (err)
916 break;
917 err = view->child->input(new, view->child,
918 KEY_RESIZE);
919 } else {
920 if (view_is_splitscreen(view)) {
921 view->parent->focussed = 0;
922 view->focussed = 1;
923 err = view_fullscreen(view);
924 } else {
925 err = view_splitscreen(view);
927 if (err)
928 break;
929 err = view->input(new, view, KEY_RESIZE);
931 break;
932 case KEY_RESIZE:
933 break;
934 case '/':
935 if (view->search_start)
936 view_search_start(view);
937 else
938 err = view->input(new, view, ch);
939 break;
940 case 'N':
941 case 'n':
942 if (view->search_next) {
943 view->searching = (ch == 'n' ?
944 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
945 view->search_next_done = 0;
946 view->search_next(view);
947 } else
948 err = view->input(new, view, ch);
949 break;
950 default:
951 err = view->input(new, view, ch);
952 break;
955 return err;
958 void
959 view_vborder(struct tog_view *view)
961 PANEL *panel;
962 const struct tog_view *view_above;
964 if (view->parent)
965 return view_vborder(view->parent);
967 panel = panel_above(view->panel);
968 if (panel == NULL)
969 return;
971 view_above = panel_userptr(panel);
972 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
973 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
976 int
977 view_needs_focus_indication(struct tog_view *view)
979 if (view_is_parent_view(view)) {
980 if (view->child == NULL || view->child->focussed)
981 return 0;
982 if (!view_is_splitscreen(view->child))
983 return 0;
984 } else if (!view_is_splitscreen(view))
985 return 0;
987 return view->focussed;
990 static const struct got_error *
991 view_loop(struct tog_view *view)
993 const struct got_error *err = NULL;
994 struct tog_view_list_head views;
995 struct tog_view *new_view;
996 int fast_refresh = 10;
997 int done = 0, errcode;
999 errcode = pthread_mutex_lock(&tog_mutex);
1000 if (errcode)
1001 return got_error_set_errno(errcode, "pthread_mutex_lock");
1003 TAILQ_INIT(&views);
1004 TAILQ_INSERT_HEAD(&views, view, entry);
1006 view->focussed = 1;
1007 err = view->show(view);
1008 if (err)
1009 return err;
1010 update_panels();
1011 doupdate();
1012 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1013 /* Refresh fast during initialization, then become slower. */
1014 if (fast_refresh && fast_refresh-- == 0)
1015 halfdelay(10); /* switch to once per second */
1017 err = view_input(&new_view, &done, view, &views);
1018 if (err)
1019 break;
1020 if (view->dying) {
1021 struct tog_view *v, *prev = NULL;
1023 if (view_is_parent_view(view))
1024 prev = TAILQ_PREV(view, tog_view_list_head,
1025 entry);
1026 else if (view->parent)
1027 prev = view->parent;
1029 if (view->parent) {
1030 view->parent->child = NULL;
1031 view->parent->focus_child = 0;
1032 } else
1033 TAILQ_REMOVE(&views, view, entry);
1035 err = view_close(view);
1036 if (err)
1037 goto done;
1039 view = NULL;
1040 TAILQ_FOREACH(v, &views, entry) {
1041 if (v->focussed)
1042 break;
1044 if (view == NULL && new_view == NULL) {
1045 /* No view has focus. Try to pick one. */
1046 if (prev)
1047 view = prev;
1048 else if (!TAILQ_EMPTY(&views)) {
1049 view = TAILQ_LAST(&views,
1050 tog_view_list_head);
1052 if (view) {
1053 if (view->focus_child) {
1054 view->child->focussed = 1;
1055 view = view->child;
1056 } else
1057 view->focussed = 1;
1061 if (new_view) {
1062 struct tog_view *v, *t;
1063 /* Only allow one parent view per type. */
1064 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1065 if (v->type != new_view->type)
1066 continue;
1067 TAILQ_REMOVE(&views, v, entry);
1068 err = view_close(v);
1069 if (err)
1070 goto done;
1071 break;
1073 TAILQ_INSERT_TAIL(&views, new_view, entry);
1074 view = new_view;
1076 if (view) {
1077 if (view_is_parent_view(view)) {
1078 if (view->child && view->child->focussed)
1079 view = view->child;
1080 } else {
1081 if (view->parent && view->parent->focussed)
1082 view = view->parent;
1084 show_panel(view->panel);
1085 if (view->child && view_is_splitscreen(view->child))
1086 show_panel(view->child->panel);
1087 if (view->parent && view_is_splitscreen(view)) {
1088 err = view->parent->show(view->parent);
1089 if (err)
1090 goto done;
1092 err = view->show(view);
1093 if (err)
1094 goto done;
1095 if (view->child) {
1096 err = view->child->show(view->child);
1097 if (err)
1098 goto done;
1100 update_panels();
1101 doupdate();
1104 done:
1105 while (!TAILQ_EMPTY(&views)) {
1106 view = TAILQ_FIRST(&views);
1107 TAILQ_REMOVE(&views, view, entry);
1108 view_close(view);
1111 errcode = pthread_mutex_unlock(&tog_mutex);
1112 if (errcode && err == NULL)
1113 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1115 return err;
1118 __dead static void
1119 usage_log(void)
1121 endwin();
1122 fprintf(stderr,
1123 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1124 getprogname());
1125 exit(1);
1128 /* Create newly allocated wide-character string equivalent to a byte string. */
1129 static const struct got_error *
1130 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1132 char *vis = NULL;
1133 const struct got_error *err = NULL;
1135 *ws = NULL;
1136 *wlen = mbstowcs(NULL, s, 0);
1137 if (*wlen == (size_t)-1) {
1138 int vislen;
1139 if (errno != EILSEQ)
1140 return got_error_from_errno("mbstowcs");
1142 /* byte string invalid in current encoding; try to "fix" it */
1143 err = got_mbsavis(&vis, &vislen, s);
1144 if (err)
1145 return err;
1146 *wlen = mbstowcs(NULL, vis, 0);
1147 if (*wlen == (size_t)-1) {
1148 err = got_error_from_errno("mbstowcs"); /* give up */
1149 goto done;
1153 *ws = calloc(*wlen + 1, sizeof(**ws));
1154 if (*ws == NULL) {
1155 err = got_error_from_errno("calloc");
1156 goto done;
1159 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1160 err = got_error_from_errno("mbstowcs");
1161 done:
1162 free(vis);
1163 if (err) {
1164 free(*ws);
1165 *ws = NULL;
1166 *wlen = 0;
1168 return err;
1171 /* Format a line for display, ensuring that it won't overflow a width limit. */
1172 static const struct got_error *
1173 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1174 int col_tab_align)
1176 const struct got_error *err = NULL;
1177 int cols = 0;
1178 wchar_t *wline = NULL;
1179 size_t wlen;
1180 int i;
1182 *wlinep = NULL;
1183 *widthp = 0;
1185 err = mbs2ws(&wline, &wlen, line);
1186 if (err)
1187 return err;
1189 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1190 wline[wlen - 1] = L'\0';
1191 wlen--;
1193 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1194 wline[wlen - 1] = L'\0';
1195 wlen--;
1198 i = 0;
1199 while (i < wlen) {
1200 int width = wcwidth(wline[i]);
1202 if (width == 0) {
1203 i++;
1204 continue;
1207 if (width == 1 || width == 2) {
1208 if (cols + width > wlimit)
1209 break;
1210 cols += width;
1211 i++;
1212 } else if (width == -1) {
1213 if (wline[i] == L'\t') {
1214 width = TABSIZE -
1215 ((cols + col_tab_align) % TABSIZE);
1216 } else {
1217 width = 1;
1218 wline[i] = L'.';
1220 if (cols + width > wlimit)
1221 break;
1222 cols += width;
1223 i++;
1224 } else {
1225 err = got_error_from_errno("wcwidth");
1226 goto done;
1229 wline[i] = L'\0';
1230 if (widthp)
1231 *widthp = cols;
1232 done:
1233 if (err)
1234 free(wline);
1235 else
1236 *wlinep = wline;
1237 return err;
1240 static const struct got_error*
1241 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1242 struct got_object_id *id, struct got_repository *repo)
1244 static const struct got_error *err = NULL;
1245 struct got_reflist_entry *re;
1246 char *s;
1247 const char *name;
1249 *refs_str = NULL;
1251 TAILQ_FOREACH(re, refs, entry) {
1252 struct got_tag_object *tag = NULL;
1253 struct got_object_id *ref_id;
1254 int cmp;
1256 name = got_ref_get_name(re->ref);
1257 if (strcmp(name, GOT_REF_HEAD) == 0)
1258 continue;
1259 if (strncmp(name, "refs/", 5) == 0)
1260 name += 5;
1261 if (strncmp(name, "got/", 4) == 0)
1262 continue;
1263 if (strncmp(name, "heads/", 6) == 0)
1264 name += 6;
1265 if (strncmp(name, "remotes/", 8) == 0) {
1266 name += 8;
1267 s = strstr(name, "/" GOT_REF_HEAD);
1268 if (s != NULL && s[strlen(s)] == '\0')
1269 continue;
1271 err = got_ref_resolve(&ref_id, repo, re->ref);
1272 if (err)
1273 break;
1274 if (strncmp(name, "tags/", 5) == 0) {
1275 err = got_object_open_as_tag(&tag, repo, ref_id);
1276 if (err) {
1277 if (err->code != GOT_ERR_OBJ_TYPE) {
1278 free(ref_id);
1279 break;
1281 /* Ref points at something other than a tag. */
1282 err = NULL;
1283 tag = NULL;
1286 cmp = got_object_id_cmp(tag ?
1287 got_object_tag_get_object_id(tag) : ref_id, id);
1288 free(ref_id);
1289 if (tag)
1290 got_object_tag_close(tag);
1291 if (cmp != 0)
1292 continue;
1293 s = *refs_str;
1294 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1295 s ? ", " : "", name) == -1) {
1296 err = got_error_from_errno("asprintf");
1297 free(s);
1298 *refs_str = NULL;
1299 break;
1301 free(s);
1304 return err;
1307 static const struct got_error *
1308 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1309 int col_tab_align)
1311 char *smallerthan;
1313 smallerthan = strchr(author, '<');
1314 if (smallerthan && smallerthan[1] != '\0')
1315 author = smallerthan + 1;
1316 author[strcspn(author, "@>")] = '\0';
1317 return format_line(wauthor, author_width, author, limit, col_tab_align);
1320 static const struct got_error *
1321 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1322 struct got_object_id *id, const size_t date_display_cols,
1323 int author_display_cols)
1325 struct tog_log_view_state *s = &view->state.log;
1326 const struct got_error *err = NULL;
1327 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1328 char *logmsg0 = NULL, *logmsg = NULL;
1329 char *author = NULL;
1330 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1331 int author_width, logmsg_width;
1332 char *newline, *line = NULL;
1333 int col, limit;
1334 const int avail = view->ncols;
1335 struct tm tm;
1336 time_t committer_time;
1337 struct tog_color *tc;
1339 committer_time = got_object_commit_get_committer_time(commit);
1340 if (localtime_r(&committer_time, &tm) == NULL)
1341 return got_error_from_errno("localtime_r");
1342 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1343 >= sizeof(datebuf))
1344 return got_error(GOT_ERR_NO_SPACE);
1346 if (avail <= date_display_cols)
1347 limit = MIN(sizeof(datebuf) - 1, avail);
1348 else
1349 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1350 tc = get_color(&s->colors, TOG_COLOR_DATE);
1351 if (tc)
1352 wattr_on(view->window,
1353 COLOR_PAIR(tc->colorpair), NULL);
1354 waddnstr(view->window, datebuf, limit);
1355 if (tc)
1356 wattr_off(view->window,
1357 COLOR_PAIR(tc->colorpair), NULL);
1358 col = limit;
1359 if (col > avail)
1360 goto done;
1362 if (avail >= 120) {
1363 char *id_str;
1364 err = got_object_id_str(&id_str, id);
1365 if (err)
1366 goto done;
1367 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1368 if (tc)
1369 wattr_on(view->window,
1370 COLOR_PAIR(tc->colorpair), NULL);
1371 wprintw(view->window, "%.8s ", id_str);
1372 if (tc)
1373 wattr_off(view->window,
1374 COLOR_PAIR(tc->colorpair), NULL);
1375 free(id_str);
1376 col += 9;
1377 if (col > avail)
1378 goto done;
1381 author = strdup(got_object_commit_get_author(commit));
1382 if (author == NULL) {
1383 err = got_error_from_errno("strdup");
1384 goto done;
1386 err = format_author(&wauthor, &author_width, author, avail - col, col);
1387 if (err)
1388 goto done;
1389 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1390 if (tc)
1391 wattr_on(view->window,
1392 COLOR_PAIR(tc->colorpair), NULL);
1393 waddwstr(view->window, wauthor);
1394 if (tc)
1395 wattr_off(view->window,
1396 COLOR_PAIR(tc->colorpair), NULL);
1397 col += author_width;
1398 while (col < avail && author_width < author_display_cols + 2) {
1399 waddch(view->window, ' ');
1400 col++;
1401 author_width++;
1403 if (col > avail)
1404 goto done;
1406 err = got_object_commit_get_logmsg(&logmsg0, commit);
1407 if (err)
1408 goto done;
1409 logmsg = logmsg0;
1410 while (*logmsg == '\n')
1411 logmsg++;
1412 newline = strchr(logmsg, '\n');
1413 if (newline)
1414 *newline = '\0';
1415 limit = avail - col;
1416 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1417 if (err)
1418 goto done;
1419 waddwstr(view->window, wlogmsg);
1420 col += logmsg_width;
1421 while (col < avail) {
1422 waddch(view->window, ' ');
1423 col++;
1425 done:
1426 free(logmsg0);
1427 free(wlogmsg);
1428 free(author);
1429 free(wauthor);
1430 free(line);
1431 return err;
1434 static struct commit_queue_entry *
1435 alloc_commit_queue_entry(struct got_commit_object *commit,
1436 struct got_object_id *id)
1438 struct commit_queue_entry *entry;
1440 entry = calloc(1, sizeof(*entry));
1441 if (entry == NULL)
1442 return NULL;
1444 entry->id = id;
1445 entry->commit = commit;
1446 return entry;
1449 static void
1450 pop_commit(struct commit_queue *commits)
1452 struct commit_queue_entry *entry;
1454 entry = TAILQ_FIRST(&commits->head);
1455 TAILQ_REMOVE(&commits->head, entry, entry);
1456 got_object_commit_close(entry->commit);
1457 commits->ncommits--;
1458 /* Don't free entry->id! It is owned by the commit graph. */
1459 free(entry);
1462 static void
1463 free_commits(struct commit_queue *commits)
1465 while (!TAILQ_EMPTY(&commits->head))
1466 pop_commit(commits);
1469 static const struct got_error *
1470 match_commit(int *have_match, struct got_object_id *id,
1471 struct got_commit_object *commit, regex_t *regex)
1473 const struct got_error *err = NULL;
1474 regmatch_t regmatch;
1475 char *id_str = NULL, *logmsg = NULL;
1477 *have_match = 0;
1479 err = got_object_id_str(&id_str, id);
1480 if (err)
1481 return err;
1483 err = got_object_commit_get_logmsg(&logmsg, commit);
1484 if (err)
1485 goto done;
1487 if (regexec(regex, got_object_commit_get_author(commit), 1,
1488 &regmatch, 0) == 0 ||
1489 regexec(regex, got_object_commit_get_committer(commit), 1,
1490 &regmatch, 0) == 0 ||
1491 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1492 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1493 *have_match = 1;
1494 done:
1495 free(id_str);
1496 free(logmsg);
1497 return err;
1500 static const struct got_error *
1501 queue_commits(struct tog_log_thread_args *a)
1503 const struct got_error *err = NULL;
1506 * We keep all commits open throughout the lifetime of the log
1507 * view in order to avoid having to re-fetch commits from disk
1508 * while updating the display.
1510 do {
1511 struct got_object_id *id;
1512 struct got_commit_object *commit;
1513 struct commit_queue_entry *entry;
1514 int errcode;
1516 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1517 NULL, NULL);
1518 if (err || id == NULL)
1519 break;
1521 err = got_object_open_as_commit(&commit, a->repo, id);
1522 if (err)
1523 break;
1524 entry = alloc_commit_queue_entry(commit, id);
1525 if (entry == NULL) {
1526 err = got_error_from_errno("alloc_commit_queue_entry");
1527 break;
1530 errcode = pthread_mutex_lock(&tog_mutex);
1531 if (errcode) {
1532 err = got_error_set_errno(errcode,
1533 "pthread_mutex_lock");
1534 break;
1537 entry->idx = a->commits->ncommits;
1538 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1539 a->commits->ncommits++;
1541 if (*a->searching == TOG_SEARCH_FORWARD &&
1542 !*a->search_next_done) {
1543 int have_match;
1544 err = match_commit(&have_match, id, commit, a->regex);
1545 if (err)
1546 break;
1547 if (have_match)
1548 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1551 errcode = pthread_mutex_unlock(&tog_mutex);
1552 if (errcode && err == NULL)
1553 err = got_error_set_errno(errcode,
1554 "pthread_mutex_unlock");
1555 if (err)
1556 break;
1557 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1559 return err;
1562 static void
1563 select_commit(struct tog_log_view_state *s)
1565 struct commit_queue_entry *entry;
1566 int ncommits = 0;
1568 entry = s->first_displayed_entry;
1569 while (entry) {
1570 if (ncommits == s->selected) {
1571 s->selected_entry = entry;
1572 break;
1574 entry = TAILQ_NEXT(entry, entry);
1575 ncommits++;
1579 static const struct got_error *
1580 draw_commits(struct tog_view *view)
1582 const struct got_error *err = NULL;
1583 struct tog_log_view_state *s = &view->state.log;
1584 struct commit_queue_entry *entry = s->selected_entry;
1585 const int limit = view->nlines;
1586 int width;
1587 int ncommits, author_cols = 4;
1588 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1589 char *refs_str = NULL;
1590 wchar_t *wline;
1591 struct tog_color *tc;
1592 static const size_t date_display_cols = 12;
1594 if (s->selected_entry &&
1595 !(view->searching && view->search_next_done == 0)) {
1596 struct got_reflist_head *refs;
1597 err = got_object_id_str(&id_str, s->selected_entry->id);
1598 if (err)
1599 return err;
1600 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1601 s->selected_entry->id);
1602 if (refs) {
1603 err = build_refs_str(&refs_str, refs,
1604 s->selected_entry->id, s->repo);
1605 if (err)
1606 goto done;
1610 if (s->thread_args.commits_needed == 0)
1611 halfdelay(10); /* disable fast refresh */
1613 if (s->thread_args.commits_needed > 0) {
1614 if (asprintf(&ncommits_str, " [%d/%d] %s",
1615 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1616 (view->searching && !view->search_next_done) ?
1617 "searching..." : "loading...") == -1) {
1618 err = got_error_from_errno("asprintf");
1619 goto done;
1621 } else {
1622 const char *search_str = NULL;
1624 if (view->searching) {
1625 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1626 search_str = "no more matches";
1627 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1628 search_str = "no matches found";
1629 else if (!view->search_next_done)
1630 search_str = "searching...";
1633 if (asprintf(&ncommits_str, " [%d/%d] %s",
1634 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1635 search_str ? search_str :
1636 (refs_str ? refs_str : "")) == -1) {
1637 err = got_error_from_errno("asprintf");
1638 goto done;
1642 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1643 if (asprintf(&header, "commit %s %s%s",
1644 id_str ? id_str : "........................................",
1645 s->in_repo_path, ncommits_str) == -1) {
1646 err = got_error_from_errno("asprintf");
1647 header = NULL;
1648 goto done;
1650 } else if (asprintf(&header, "commit %s%s",
1651 id_str ? id_str : "........................................",
1652 ncommits_str) == -1) {
1653 err = got_error_from_errno("asprintf");
1654 header = NULL;
1655 goto done;
1657 err = format_line(&wline, &width, header, view->ncols, 0);
1658 if (err)
1659 goto done;
1661 werase(view->window);
1663 if (view_needs_focus_indication(view))
1664 wstandout(view->window);
1665 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1666 if (tc)
1667 wattr_on(view->window,
1668 COLOR_PAIR(tc->colorpair), NULL);
1669 waddwstr(view->window, wline);
1670 if (tc)
1671 wattr_off(view->window,
1672 COLOR_PAIR(tc->colorpair), NULL);
1673 while (width < view->ncols) {
1674 waddch(view->window, ' ');
1675 width++;
1677 if (view_needs_focus_indication(view))
1678 wstandend(view->window);
1679 free(wline);
1680 if (limit <= 1)
1681 goto done;
1683 /* Grow author column size if necessary. */
1684 entry = s->first_displayed_entry;
1685 ncommits = 0;
1686 while (entry) {
1687 char *author;
1688 wchar_t *wauthor;
1689 int width;
1690 if (ncommits >= limit - 1)
1691 break;
1692 author = strdup(got_object_commit_get_author(entry->commit));
1693 if (author == NULL) {
1694 err = got_error_from_errno("strdup");
1695 goto done;
1697 err = format_author(&wauthor, &width, author, COLS,
1698 date_display_cols);
1699 if (author_cols < width)
1700 author_cols = width;
1701 free(wauthor);
1702 free(author);
1703 ncommits++;
1704 entry = TAILQ_NEXT(entry, entry);
1707 entry = s->first_displayed_entry;
1708 s->last_displayed_entry = s->first_displayed_entry;
1709 ncommits = 0;
1710 while (entry) {
1711 if (ncommits >= limit - 1)
1712 break;
1713 if (ncommits == s->selected)
1714 wstandout(view->window);
1715 err = draw_commit(view, entry->commit, entry->id,
1716 date_display_cols, author_cols);
1717 if (ncommits == s->selected)
1718 wstandend(view->window);
1719 if (err)
1720 goto done;
1721 ncommits++;
1722 s->last_displayed_entry = entry;
1723 entry = TAILQ_NEXT(entry, entry);
1726 view_vborder(view);
1727 done:
1728 free(id_str);
1729 free(refs_str);
1730 free(ncommits_str);
1731 free(header);
1732 return err;
1735 static void
1736 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1738 struct commit_queue_entry *entry;
1739 int nscrolled = 0;
1741 entry = TAILQ_FIRST(&s->commits.head);
1742 if (s->first_displayed_entry == entry)
1743 return;
1745 entry = s->first_displayed_entry;
1746 while (entry && nscrolled < maxscroll) {
1747 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1748 if (entry) {
1749 s->first_displayed_entry = entry;
1750 nscrolled++;
1755 static const struct got_error *
1756 trigger_log_thread(struct tog_view *view, int wait)
1758 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1759 int errcode;
1761 halfdelay(1); /* fast refresh while loading commits */
1763 while (ta->commits_needed > 0) {
1764 if (ta->log_complete)
1765 break;
1767 /* Wake the log thread. */
1768 errcode = pthread_cond_signal(&ta->need_commits);
1769 if (errcode)
1770 return got_error_set_errno(errcode,
1771 "pthread_cond_signal");
1774 * The mutex will be released while the view loop waits
1775 * in wgetch(), at which time the log thread will run.
1777 if (!wait)
1778 break;
1780 /* Display progress update in log view. */
1781 show_log_view(view);
1782 update_panels();
1783 doupdate();
1785 /* Wait right here while next commit is being loaded. */
1786 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1787 if (errcode)
1788 return got_error_set_errno(errcode,
1789 "pthread_cond_wait");
1791 /* Display progress update in log view. */
1792 show_log_view(view);
1793 update_panels();
1794 doupdate();
1797 return NULL;
1800 static const struct got_error *
1801 log_scroll_down(struct tog_view *view, int maxscroll)
1803 struct tog_log_view_state *s = &view->state.log;
1804 const struct got_error *err = NULL;
1805 struct commit_queue_entry *pentry;
1806 int nscrolled = 0, ncommits_needed;
1808 if (s->last_displayed_entry == NULL)
1809 return NULL;
1811 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1812 if (s->commits.ncommits < ncommits_needed &&
1813 !s->thread_args.log_complete) {
1815 * Ask the log thread for required amount of commits.
1817 s->thread_args.commits_needed += maxscroll;
1818 err = trigger_log_thread(view, 1);
1819 if (err)
1820 return err;
1823 do {
1824 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1825 if (pentry == NULL)
1826 break;
1828 s->last_displayed_entry = pentry;
1830 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1831 if (pentry == NULL)
1832 break;
1833 s->first_displayed_entry = pentry;
1834 } while (++nscrolled < maxscroll);
1836 return err;
1839 static const struct got_error *
1840 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1841 struct got_commit_object *commit, struct got_object_id *commit_id,
1842 struct tog_view *log_view, struct got_repository *repo)
1844 const struct got_error *err;
1845 struct got_object_qid *parent_id;
1846 struct tog_view *diff_view;
1848 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1849 if (diff_view == NULL)
1850 return got_error_from_errno("view_open");
1852 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1853 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1854 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1855 if (err == NULL)
1856 *new_view = diff_view;
1857 return err;
1860 static const struct got_error *
1861 tree_view_visit_subtree(struct tog_tree_view_state *s,
1862 struct got_tree_object *subtree)
1864 struct tog_parent_tree *parent;
1866 parent = calloc(1, sizeof(*parent));
1867 if (parent == NULL)
1868 return got_error_from_errno("calloc");
1870 parent->tree = s->tree;
1871 parent->first_displayed_entry = s->first_displayed_entry;
1872 parent->selected_entry = s->selected_entry;
1873 parent->selected = s->selected;
1874 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1875 s->tree = subtree;
1876 s->selected = 0;
1877 s->first_displayed_entry = NULL;
1878 return NULL;
1881 static const struct got_error *
1882 tree_view_walk_path(struct tog_tree_view_state *s,
1883 struct got_object_id *commit_id, const char *path)
1885 const struct got_error *err = NULL;
1886 struct got_tree_object *tree = NULL;
1887 const char *p;
1888 char *slash, *subpath = NULL;
1890 /* Walk the path and open corresponding tree objects. */
1891 p = path;
1892 while (*p) {
1893 struct got_tree_entry *te;
1894 struct got_object_id *tree_id;
1895 char *te_name;
1897 while (p[0] == '/')
1898 p++;
1900 /* Ensure the correct subtree entry is selected. */
1901 slash = strchr(p, '/');
1902 if (slash == NULL)
1903 te_name = strdup(p);
1904 else
1905 te_name = strndup(p, slash - p);
1906 if (te_name == NULL) {
1907 err = got_error_from_errno("strndup");
1908 break;
1910 te = got_object_tree_find_entry(s->tree, te_name);
1911 if (te == NULL) {
1912 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1913 free(te_name);
1914 break;
1916 free(te_name);
1917 s->first_displayed_entry = s->selected_entry = te;
1919 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1920 break; /* jump to this file's entry */
1922 slash = strchr(p, '/');
1923 if (slash)
1924 subpath = strndup(path, slash - path);
1925 else
1926 subpath = strdup(path);
1927 if (subpath == NULL) {
1928 err = got_error_from_errno("strdup");
1929 break;
1932 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1933 subpath);
1934 if (err)
1935 break;
1937 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1938 free(tree_id);
1939 if (err)
1940 break;
1942 err = tree_view_visit_subtree(s, tree);
1943 if (err) {
1944 got_object_tree_close(tree);
1945 break;
1947 if (slash == NULL)
1948 break;
1949 free(subpath);
1950 subpath = NULL;
1951 p = slash;
1954 free(subpath);
1955 return err;
1958 static const struct got_error *
1959 browse_commit_tree(struct tog_view **new_view, int begin_x,
1960 struct commit_queue_entry *entry, const char *path,
1961 const char *head_ref_name, struct got_repository *repo)
1963 const struct got_error *err = NULL;
1964 struct got_tree_object *tree;
1965 struct tog_tree_view_state *s;
1966 struct tog_view *tree_view;
1968 err = got_object_open_as_tree(&tree, repo,
1969 got_object_commit_get_tree_id(entry->commit));
1970 if (err)
1971 return err;
1973 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1974 if (tree_view == NULL)
1975 return got_error_from_errno("view_open");
1977 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1978 if (err) {
1979 got_object_tree_close(tree);
1980 return err;
1982 s = &tree_view->state.tree;
1984 *new_view = tree_view;
1986 if (got_path_is_root_dir(path))
1987 return NULL;
1989 return tree_view_walk_path(s, entry->id, path);
1992 static const struct got_error *
1993 block_signals_used_by_main_thread(void)
1995 sigset_t sigset;
1996 int errcode;
1998 if (sigemptyset(&sigset) == -1)
1999 return got_error_from_errno("sigemptyset");
2001 /* tog handles SIGWINCH and SIGCONT */
2002 if (sigaddset(&sigset, SIGWINCH) == -1)
2003 return got_error_from_errno("sigaddset");
2004 if (sigaddset(&sigset, SIGCONT) == -1)
2005 return got_error_from_errno("sigaddset");
2007 /* ncurses handles SIGTSTP */
2008 if (sigaddset(&sigset, SIGTSTP) == -1)
2009 return got_error_from_errno("sigaddset");
2011 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2012 if (errcode)
2013 return got_error_set_errno(errcode, "pthread_sigmask");
2015 return NULL;
2018 static void *
2019 log_thread(void *arg)
2021 const struct got_error *err = NULL;
2022 int errcode = 0;
2023 struct tog_log_thread_args *a = arg;
2024 int done = 0;
2026 err = block_signals_used_by_main_thread();
2027 if (err)
2028 return (void *)err;
2030 while (!done && !err && !tog_sigpipe_received) {
2031 err = queue_commits(a);
2032 if (err) {
2033 if (err->code != GOT_ERR_ITER_COMPLETED)
2034 return (void *)err;
2035 err = NULL;
2036 done = 1;
2037 } else if (a->commits_needed > 0)
2038 a->commits_needed--;
2040 errcode = pthread_mutex_lock(&tog_mutex);
2041 if (errcode) {
2042 err = got_error_set_errno(errcode,
2043 "pthread_mutex_lock");
2044 break;
2045 } else if (*a->quit)
2046 done = 1;
2047 else if (*a->first_displayed_entry == NULL) {
2048 *a->first_displayed_entry =
2049 TAILQ_FIRST(&a->commits->head);
2050 *a->selected_entry = *a->first_displayed_entry;
2053 errcode = pthread_cond_signal(&a->commit_loaded);
2054 if (errcode) {
2055 err = got_error_set_errno(errcode,
2056 "pthread_cond_signal");
2057 pthread_mutex_unlock(&tog_mutex);
2058 break;
2061 if (done)
2062 a->commits_needed = 0;
2063 else {
2064 if (a->commits_needed == 0) {
2065 errcode = pthread_cond_wait(&a->need_commits,
2066 &tog_mutex);
2067 if (errcode)
2068 err = got_error_set_errno(errcode,
2069 "pthread_cond_wait");
2070 if (*a->quit)
2071 done = 1;
2075 errcode = pthread_mutex_unlock(&tog_mutex);
2076 if (errcode && err == NULL)
2077 err = got_error_set_errno(errcode,
2078 "pthread_mutex_unlock");
2080 a->log_complete = 1;
2081 return (void *)err;
2084 static const struct got_error *
2085 stop_log_thread(struct tog_log_view_state *s)
2087 const struct got_error *err = NULL;
2088 int errcode;
2090 if (s->thread) {
2091 s->quit = 1;
2092 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2093 if (errcode)
2094 return got_error_set_errno(errcode,
2095 "pthread_cond_signal");
2096 errcode = pthread_mutex_unlock(&tog_mutex);
2097 if (errcode)
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_unlock");
2100 errcode = pthread_join(s->thread, (void **)&err);
2101 if (errcode)
2102 return got_error_set_errno(errcode, "pthread_join");
2103 errcode = pthread_mutex_lock(&tog_mutex);
2104 if (errcode)
2105 return got_error_set_errno(errcode,
2106 "pthread_mutex_lock");
2107 s->thread = NULL;
2110 if (s->thread_args.repo) {
2111 got_repo_close(s->thread_args.repo);
2112 s->thread_args.repo = NULL;
2115 if (s->thread_args.graph) {
2116 got_commit_graph_close(s->thread_args.graph);
2117 s->thread_args.graph = NULL;
2120 return err;
2123 static const struct got_error *
2124 close_log_view(struct tog_view *view)
2126 const struct got_error *err = NULL;
2127 struct tog_log_view_state *s = &view->state.log;
2128 int errcode;
2130 err = stop_log_thread(s);
2132 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2133 if (errcode && err == NULL)
2134 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2136 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2137 if (errcode && err == NULL)
2138 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2140 free_commits(&s->commits);
2141 free(s->in_repo_path);
2142 s->in_repo_path = NULL;
2143 free(s->start_id);
2144 s->start_id = NULL;
2145 free(s->head_ref_name);
2146 s->head_ref_name = NULL;
2147 return err;
2150 static const struct got_error *
2151 search_start_log_view(struct tog_view *view)
2153 struct tog_log_view_state *s = &view->state.log;
2155 s->matched_entry = NULL;
2156 s->search_entry = NULL;
2157 return NULL;
2160 static const struct got_error *
2161 search_next_log_view(struct tog_view *view)
2163 const struct got_error *err = NULL;
2164 struct tog_log_view_state *s = &view->state.log;
2165 struct commit_queue_entry *entry;
2167 /* Display progress update in log view. */
2168 show_log_view(view);
2169 update_panels();
2170 doupdate();
2172 if (s->search_entry) {
2173 int errcode, ch;
2174 errcode = pthread_mutex_unlock(&tog_mutex);
2175 if (errcode)
2176 return got_error_set_errno(errcode,
2177 "pthread_mutex_unlock");
2178 ch = wgetch(view->window);
2179 errcode = pthread_mutex_lock(&tog_mutex);
2180 if (errcode)
2181 return got_error_set_errno(errcode,
2182 "pthread_mutex_lock");
2183 if (ch == KEY_BACKSPACE) {
2184 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2185 return NULL;
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(s->search_entry, entry);
2189 else
2190 entry = TAILQ_PREV(s->search_entry,
2191 commit_queue_head, entry);
2192 } else if (s->matched_entry) {
2193 if (view->searching == TOG_SEARCH_FORWARD)
2194 entry = TAILQ_NEXT(s->matched_entry, entry);
2195 else
2196 entry = TAILQ_PREV(s->matched_entry,
2197 commit_queue_head, entry);
2198 } else {
2199 if (view->searching == TOG_SEARCH_FORWARD)
2200 entry = TAILQ_FIRST(&s->commits.head);
2201 else
2202 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2205 while (1) {
2206 int have_match = 0;
2208 if (entry == NULL) {
2209 if (s->thread_args.log_complete ||
2210 view->searching == TOG_SEARCH_BACKWARD) {
2211 view->search_next_done =
2212 (s->matched_entry == NULL ?
2213 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2214 s->search_entry = NULL;
2215 return NULL;
2218 * Poke the log thread for more commits and return,
2219 * allowing the main loop to make progress. Search
2220 * will resume at s->search_entry once we come back.
2222 s->thread_args.commits_needed++;
2223 return trigger_log_thread(view, 0);
2226 err = match_commit(&have_match, entry->id, entry->commit,
2227 &view->regex);
2228 if (err)
2229 break;
2230 if (have_match) {
2231 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2232 s->matched_entry = entry;
2233 break;
2236 s->search_entry = entry;
2237 if (view->searching == TOG_SEARCH_FORWARD)
2238 entry = TAILQ_NEXT(entry, entry);
2239 else
2240 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2243 if (s->matched_entry) {
2244 int cur = s->selected_entry->idx;
2245 while (cur < s->matched_entry->idx) {
2246 err = input_log_view(NULL, view, KEY_DOWN);
2247 if (err)
2248 return err;
2249 cur++;
2251 while (cur > s->matched_entry->idx) {
2252 err = input_log_view(NULL, view, KEY_UP);
2253 if (err)
2254 return err;
2255 cur--;
2259 s->search_entry = NULL;
2261 return NULL;
2264 static const struct got_error *
2265 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2266 struct got_repository *repo, const char *head_ref_name,
2267 const char *in_repo_path, int log_branches)
2269 const struct got_error *err = NULL;
2270 struct tog_log_view_state *s = &view->state.log;
2271 struct got_repository *thread_repo = NULL;
2272 struct got_commit_graph *thread_graph = NULL;
2273 int errcode;
2275 if (in_repo_path != s->in_repo_path) {
2276 free(s->in_repo_path);
2277 s->in_repo_path = strdup(in_repo_path);
2278 if (s->in_repo_path == NULL)
2279 return got_error_from_errno("strdup");
2282 /* The commit queue only contains commits being displayed. */
2283 TAILQ_INIT(&s->commits.head);
2284 s->commits.ncommits = 0;
2286 s->repo = repo;
2287 if (head_ref_name) {
2288 s->head_ref_name = strdup(head_ref_name);
2289 if (s->head_ref_name == NULL) {
2290 err = got_error_from_errno("strdup");
2291 goto done;
2294 s->start_id = got_object_id_dup(start_id);
2295 if (s->start_id == NULL) {
2296 err = got_error_from_errno("got_object_id_dup");
2297 goto done;
2299 s->log_branches = log_branches;
2301 SIMPLEQ_INIT(&s->colors);
2302 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2303 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2304 get_color_value("TOG_COLOR_COMMIT"));
2305 if (err)
2306 goto done;
2307 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2308 get_color_value("TOG_COLOR_AUTHOR"));
2309 if (err) {
2310 free_colors(&s->colors);
2311 goto done;
2313 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2314 get_color_value("TOG_COLOR_DATE"));
2315 if (err) {
2316 free_colors(&s->colors);
2317 goto done;
2321 view->show = show_log_view;
2322 view->input = input_log_view;
2323 view->close = close_log_view;
2324 view->search_start = search_start_log_view;
2325 view->search_next = search_next_log_view;
2327 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2328 if (err)
2329 goto done;
2330 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2331 !s->log_branches);
2332 if (err)
2333 goto done;
2334 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2335 s->repo, NULL, NULL);
2336 if (err)
2337 goto done;
2339 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2340 if (errcode) {
2341 err = got_error_set_errno(errcode, "pthread_cond_init");
2342 goto done;
2344 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2345 if (errcode) {
2346 err = got_error_set_errno(errcode, "pthread_cond_init");
2347 goto done;
2350 s->thread_args.commits_needed = view->nlines;
2351 s->thread_args.graph = thread_graph;
2352 s->thread_args.commits = &s->commits;
2353 s->thread_args.in_repo_path = s->in_repo_path;
2354 s->thread_args.start_id = s->start_id;
2355 s->thread_args.repo = thread_repo;
2356 s->thread_args.log_complete = 0;
2357 s->thread_args.quit = &s->quit;
2358 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2359 s->thread_args.selected_entry = &s->selected_entry;
2360 s->thread_args.searching = &view->searching;
2361 s->thread_args.search_next_done = &view->search_next_done;
2362 s->thread_args.regex = &view->regex;
2363 done:
2364 if (err)
2365 close_log_view(view);
2366 return err;
2369 static const struct got_error *
2370 show_log_view(struct tog_view *view)
2372 const struct got_error *err;
2373 struct tog_log_view_state *s = &view->state.log;
2375 if (s->thread == NULL) {
2376 int errcode = pthread_create(&s->thread, NULL, log_thread,
2377 &s->thread_args);
2378 if (errcode)
2379 return got_error_set_errno(errcode, "pthread_create");
2380 if (s->thread_args.commits_needed > 0) {
2381 err = trigger_log_thread(view, 1);
2382 if (err)
2383 return err;
2387 return draw_commits(view);
2390 static const struct got_error *
2391 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2393 const struct got_error *err = NULL;
2394 struct tog_log_view_state *s = &view->state.log;
2395 struct tog_view *diff_view = NULL, *tree_view = NULL;
2396 struct tog_view *ref_view = NULL;
2397 int begin_x = 0;
2399 switch (ch) {
2400 case 'q':
2401 s->quit = 1;
2402 break;
2403 case 'k':
2404 case KEY_UP:
2405 case '<':
2406 case ',':
2407 if (s->first_displayed_entry == NULL)
2408 break;
2409 if (s->selected > 0)
2410 s->selected--;
2411 else
2412 log_scroll_up(s, 1);
2413 select_commit(s);
2414 break;
2415 case KEY_PPAGE:
2416 case CTRL('b'):
2417 if (s->first_displayed_entry == NULL)
2418 break;
2419 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2420 s->selected = 0;
2421 else
2422 log_scroll_up(s, view->nlines - 1);
2423 select_commit(s);
2424 break;
2425 case 'j':
2426 case KEY_DOWN:
2427 case '>':
2428 case '.':
2429 if (s->first_displayed_entry == NULL)
2430 break;
2431 if (s->selected < MIN(view->nlines - 2,
2432 s->commits.ncommits - 1))
2433 s->selected++;
2434 else {
2435 err = log_scroll_down(view, 1);
2436 if (err)
2437 break;
2439 select_commit(s);
2440 break;
2441 case KEY_NPAGE:
2442 case CTRL('f'): {
2443 struct commit_queue_entry *first;
2444 first = s->first_displayed_entry;
2445 if (first == NULL)
2446 break;
2447 err = log_scroll_down(view, view->nlines - 1);
2448 if (err)
2449 break;
2450 if (first == s->first_displayed_entry &&
2451 s->selected < MIN(view->nlines - 2,
2452 s->commits.ncommits - 1)) {
2453 /* can't scroll further down */
2454 s->selected = MIN(view->nlines - 2,
2455 s->commits.ncommits - 1);
2457 select_commit(s);
2458 break;
2460 case KEY_RESIZE:
2461 if (s->selected > view->nlines - 2)
2462 s->selected = view->nlines - 2;
2463 if (s->selected > s->commits.ncommits - 1)
2464 s->selected = s->commits.ncommits - 1;
2465 select_commit(s);
2466 if (s->commits.ncommits < view->nlines - 1 &&
2467 !s->thread_args.log_complete) {
2468 s->thread_args.commits_needed += (view->nlines - 1) -
2469 s->commits.ncommits;
2470 err = trigger_log_thread(view, 1);
2472 break;
2473 case KEY_ENTER:
2474 case ' ':
2475 case '\r':
2476 if (s->selected_entry == NULL)
2477 break;
2478 if (view_is_parent_view(view))
2479 begin_x = view_split_begin_x(view->begin_x);
2480 err = open_diff_view_for_commit(&diff_view, begin_x,
2481 s->selected_entry->commit, s->selected_entry->id,
2482 view, s->repo);
2483 if (err)
2484 break;
2485 view->focussed = 0;
2486 diff_view->focussed = 1;
2487 if (view_is_parent_view(view)) {
2488 err = view_close_child(view);
2489 if (err)
2490 return err;
2491 view_set_child(view, diff_view);
2492 view->focus_child = 1;
2493 } else
2494 *new_view = diff_view;
2495 break;
2496 case 't':
2497 if (s->selected_entry == NULL)
2498 break;
2499 if (view_is_parent_view(view))
2500 begin_x = view_split_begin_x(view->begin_x);
2501 err = browse_commit_tree(&tree_view, begin_x,
2502 s->selected_entry, s->in_repo_path, s->head_ref_name,
2503 s->repo);
2504 if (err)
2505 break;
2506 view->focussed = 0;
2507 tree_view->focussed = 1;
2508 if (view_is_parent_view(view)) {
2509 err = view_close_child(view);
2510 if (err)
2511 return err;
2512 view_set_child(view, tree_view);
2513 view->focus_child = 1;
2514 } else
2515 *new_view = tree_view;
2516 break;
2517 case KEY_BACKSPACE:
2518 case CTRL('l'):
2519 case 'B':
2520 if (ch == KEY_BACKSPACE &&
2521 got_path_is_root_dir(s->in_repo_path))
2522 break;
2523 err = stop_log_thread(s);
2524 if (err)
2525 return err;
2526 if (ch == KEY_BACKSPACE) {
2527 char *parent_path;
2528 err = got_path_dirname(&parent_path, s->in_repo_path);
2529 if (err)
2530 return err;
2531 free(s->in_repo_path);
2532 s->in_repo_path = parent_path;
2533 s->thread_args.in_repo_path = s->in_repo_path;
2534 } else if (ch == CTRL('l')) {
2535 struct got_object_id *start_id;
2536 err = got_repo_match_object_id(&start_id, NULL,
2537 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2538 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2539 if (err)
2540 return err;
2541 free(s->start_id);
2542 s->start_id = start_id;
2543 s->thread_args.start_id = s->start_id;
2544 } else /* 'B' */
2545 s->log_branches = !s->log_branches;
2547 err = got_repo_open(&s->thread_args.repo,
2548 got_repo_get_path(s->repo), NULL);
2549 if (err)
2550 return err;
2551 tog_free_refs();
2552 err = tog_load_refs(s->repo);
2553 if (err)
2554 return err;
2555 err = got_commit_graph_open(&s->thread_args.graph,
2556 s->in_repo_path, !s->log_branches);
2557 if (err)
2558 return err;
2559 err = got_commit_graph_iter_start(s->thread_args.graph,
2560 s->start_id, s->repo, NULL, NULL);
2561 if (err)
2562 return err;
2563 free_commits(&s->commits);
2564 s->first_displayed_entry = NULL;
2565 s->last_displayed_entry = NULL;
2566 s->selected_entry = NULL;
2567 s->selected = 0;
2568 s->thread_args.log_complete = 0;
2569 s->quit = 0;
2570 s->thread_args.commits_needed = view->nlines;
2571 break;
2572 case 'r':
2573 if (view_is_parent_view(view))
2574 begin_x = view_split_begin_x(view->begin_x);
2575 ref_view = view_open(view->nlines, view->ncols,
2576 view->begin_y, begin_x, TOG_VIEW_REF);
2577 if (ref_view == NULL)
2578 return got_error_from_errno("view_open");
2579 err = open_ref_view(ref_view, s->repo);
2580 if (err) {
2581 view_close(ref_view);
2582 return err;
2584 view->focussed = 0;
2585 ref_view->focussed = 1;
2586 if (view_is_parent_view(view)) {
2587 err = view_close_child(view);
2588 if (err)
2589 return err;
2590 view_set_child(view, ref_view);
2591 view->focus_child = 1;
2592 } else
2593 *new_view = ref_view;
2594 break;
2595 default:
2596 break;
2599 return err;
2602 static const struct got_error *
2603 apply_unveil(const char *repo_path, const char *worktree_path)
2605 const struct got_error *error;
2607 #ifdef PROFILE
2608 if (unveil("gmon.out", "rwc") != 0)
2609 return got_error_from_errno2("unveil", "gmon.out");
2610 #endif
2611 if (repo_path && unveil(repo_path, "r") != 0)
2612 return got_error_from_errno2("unveil", repo_path);
2614 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2615 return got_error_from_errno2("unveil", worktree_path);
2617 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2618 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2620 error = got_privsep_unveil_exec_helpers();
2621 if (error != NULL)
2622 return error;
2624 if (unveil(NULL, NULL) != 0)
2625 return got_error_from_errno("unveil");
2627 return NULL;
2630 static void
2631 init_curses(void)
2633 initscr();
2634 cbreak();
2635 halfdelay(1); /* Do fast refresh while initial view is loading. */
2636 noecho();
2637 nonl();
2638 intrflush(stdscr, FALSE);
2639 keypad(stdscr, TRUE);
2640 curs_set(0);
2641 if (getenv("TOG_COLORS") != NULL) {
2642 start_color();
2643 use_default_colors();
2645 signal(SIGWINCH, tog_sigwinch);
2646 signal(SIGPIPE, tog_sigpipe);
2647 signal(SIGCONT, tog_sigcont);
2650 static const struct got_error *
2651 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2652 struct got_repository *repo, struct got_worktree *worktree)
2654 const struct got_error *err = NULL;
2656 if (argc == 0) {
2657 *in_repo_path = strdup("/");
2658 if (*in_repo_path == NULL)
2659 return got_error_from_errno("strdup");
2660 return NULL;
2663 if (worktree) {
2664 const char *prefix = got_worktree_get_path_prefix(worktree);
2665 char *p;
2667 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2668 if (err)
2669 return err;
2670 if (asprintf(in_repo_path, "%s%s%s", prefix,
2671 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2672 p) == -1) {
2673 err = got_error_from_errno("asprintf");
2674 *in_repo_path = NULL;
2676 free(p);
2677 } else
2678 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2680 return err;
2683 static const struct got_error *
2684 cmd_log(int argc, char *argv[])
2686 const struct got_error *error;
2687 struct got_repository *repo = NULL;
2688 struct got_worktree *worktree = NULL;
2689 struct got_object_id *start_id = NULL;
2690 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2691 char *start_commit = NULL, *label = NULL;
2692 struct got_reference *ref = NULL;
2693 const char *head_ref_name = NULL;
2694 int ch, log_branches = 0;
2695 struct tog_view *view;
2697 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2698 switch (ch) {
2699 case 'b':
2700 log_branches = 1;
2701 break;
2702 case 'c':
2703 start_commit = optarg;
2704 break;
2705 case 'r':
2706 repo_path = realpath(optarg, NULL);
2707 if (repo_path == NULL)
2708 return got_error_from_errno2("realpath",
2709 optarg);
2710 break;
2711 default:
2712 usage_log();
2713 /* NOTREACHED */
2717 argc -= optind;
2718 argv += optind;
2720 if (argc > 1)
2721 usage_log();
2723 if (repo_path == NULL) {
2724 cwd = getcwd(NULL, 0);
2725 if (cwd == NULL)
2726 return got_error_from_errno("getcwd");
2727 error = got_worktree_open(&worktree, cwd);
2728 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2729 goto done;
2730 if (worktree)
2731 repo_path =
2732 strdup(got_worktree_get_repo_path(worktree));
2733 else
2734 repo_path = strdup(cwd);
2735 if (repo_path == NULL) {
2736 error = got_error_from_errno("strdup");
2737 goto done;
2741 error = got_repo_open(&repo, repo_path, NULL);
2742 if (error != NULL)
2743 goto done;
2745 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2746 repo, worktree);
2747 if (error)
2748 goto done;
2750 init_curses();
2752 error = apply_unveil(got_repo_get_path(repo),
2753 worktree ? got_worktree_get_root_path(worktree) : NULL);
2754 if (error)
2755 goto done;
2757 /* already loaded by tog_log_with_path()? */
2758 if (TAILQ_EMPTY(&tog_refs)) {
2759 error = tog_load_refs(repo);
2760 if (error)
2761 goto done;
2764 if (start_commit == NULL) {
2765 error = got_repo_match_object_id(&start_id, &label,
2766 worktree ? got_worktree_get_head_ref_name(worktree) :
2767 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2768 if (error)
2769 goto done;
2770 head_ref_name = label;
2771 } else {
2772 error = got_ref_open(&ref, repo, start_commit, 0);
2773 if (error == NULL)
2774 head_ref_name = got_ref_get_name(ref);
2775 else if (error->code != GOT_ERR_NOT_REF)
2776 goto done;
2777 error = got_repo_match_object_id(&start_id, NULL,
2778 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2779 if (error)
2780 goto done;
2783 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2784 if (view == NULL) {
2785 error = got_error_from_errno("view_open");
2786 goto done;
2788 error = open_log_view(view, start_id, repo, head_ref_name,
2789 in_repo_path, log_branches);
2790 if (error)
2791 goto done;
2792 if (worktree) {
2793 /* Release work tree lock. */
2794 got_worktree_close(worktree);
2795 worktree = NULL;
2797 error = view_loop(view);
2798 done:
2799 free(in_repo_path);
2800 free(repo_path);
2801 free(cwd);
2802 free(start_id);
2803 free(label);
2804 if (ref)
2805 got_ref_close(ref);
2806 if (repo)
2807 got_repo_close(repo);
2808 if (worktree)
2809 got_worktree_close(worktree);
2810 tog_free_refs();
2811 return error;
2814 __dead static void
2815 usage_diff(void)
2817 endwin();
2818 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2819 "[-w] object1 object2\n", getprogname());
2820 exit(1);
2823 static int
2824 match_line(const char *line, regex_t *regex, size_t nmatch,
2825 regmatch_t *regmatch)
2827 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2830 struct tog_color *
2831 match_color(struct tog_colors *colors, const char *line)
2833 struct tog_color *tc = NULL;
2835 SIMPLEQ_FOREACH(tc, colors, entry) {
2836 if (match_line(line, &tc->regex, 0, NULL))
2837 return tc;
2840 return NULL;
2843 static const struct got_error *
2844 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2845 WINDOW *window, regmatch_t *regmatch)
2847 const struct got_error *err = NULL;
2848 wchar_t *wline;
2849 int width;
2850 char *s;
2852 *wtotal = 0;
2854 s = strndup(line, regmatch->rm_so);
2855 if (s == NULL)
2856 return got_error_from_errno("strndup");
2858 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2859 if (err) {
2860 free(s);
2861 return err;
2863 waddwstr(window, wline);
2864 free(wline);
2865 free(s);
2866 wlimit -= width;
2867 *wtotal += width;
2869 if (wlimit > 0) {
2870 s = strndup(line + regmatch->rm_so,
2871 regmatch->rm_eo - regmatch->rm_so);
2872 if (s == NULL) {
2873 err = got_error_from_errno("strndup");
2874 free(s);
2875 return err;
2877 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2878 if (err) {
2879 free(s);
2880 return err;
2882 wattr_on(window, A_STANDOUT, NULL);
2883 waddwstr(window, wline);
2884 wattr_off(window, A_STANDOUT, NULL);
2885 free(wline);
2886 free(s);
2887 wlimit -= width;
2888 *wtotal += width;
2891 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2892 err = format_line(&wline, &width,
2893 line + regmatch->rm_eo, wlimit, col_tab_align);
2894 if (err)
2895 return err;
2896 waddwstr(window, wline);
2897 free(wline);
2898 *wtotal += width;
2901 return NULL;
2904 static const struct got_error *
2905 draw_file(struct tog_view *view, const char *header)
2907 struct tog_diff_view_state *s = &view->state.diff;
2908 regmatch_t *regmatch = &view->regmatch;
2909 const struct got_error *err;
2910 int nprinted = 0;
2911 char *line;
2912 size_t linesize = 0;
2913 ssize_t linelen;
2914 struct tog_color *tc;
2915 wchar_t *wline;
2916 int width;
2917 int max_lines = view->nlines;
2918 int nlines = s->nlines;
2919 off_t line_offset;
2921 line_offset = s->line_offsets[s->first_displayed_line - 1];
2922 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2923 return got_error_from_errno("fseek");
2925 werase(view->window);
2927 if (header) {
2928 if (asprintf(&line, "[%d/%d] %s",
2929 s->first_displayed_line - 1 + s->selected_line, nlines,
2930 header) == -1)
2931 return got_error_from_errno("asprintf");
2932 err = format_line(&wline, &width, line, view->ncols, 0);
2933 free(line);
2934 if (err)
2935 return err;
2937 if (view_needs_focus_indication(view))
2938 wstandout(view->window);
2939 waddwstr(view->window, wline);
2940 free(wline);
2941 wline = NULL;
2942 if (view_needs_focus_indication(view))
2943 wstandend(view->window);
2944 if (width <= view->ncols - 1)
2945 waddch(view->window, '\n');
2947 if (max_lines <= 1)
2948 return NULL;
2949 max_lines--;
2952 s->eof = 0;
2953 line = NULL;
2954 while (max_lines > 0 && nprinted < max_lines) {
2955 linelen = getline(&line, &linesize, s->f);
2956 if (linelen == -1) {
2957 if (feof(s->f)) {
2958 s->eof = 1;
2959 break;
2961 free(line);
2962 return got_ferror(s->f, GOT_ERR_IO);
2965 tc = match_color(&s->colors, line);
2966 if (tc)
2967 wattr_on(view->window,
2968 COLOR_PAIR(tc->colorpair), NULL);
2969 if (s->first_displayed_line + nprinted == s->matched_line &&
2970 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2971 err = add_matched_line(&width, line, view->ncols, 0,
2972 view->window, regmatch);
2973 if (err) {
2974 free(line);
2975 return err;
2977 } else {
2978 err = format_line(&wline, &width, line, view->ncols, 0);
2979 if (err) {
2980 free(line);
2981 return err;
2983 waddwstr(view->window, wline);
2984 free(wline);
2985 wline = NULL;
2987 if (tc)
2988 wattr_off(view->window,
2989 COLOR_PAIR(tc->colorpair), NULL);
2990 if (width <= view->ncols - 1)
2991 waddch(view->window, '\n');
2992 nprinted++;
2994 free(line);
2995 if (nprinted >= 1)
2996 s->last_displayed_line = s->first_displayed_line +
2997 (nprinted - 1);
2998 else
2999 s->last_displayed_line = s->first_displayed_line;
3001 view_vborder(view);
3003 if (s->eof) {
3004 while (nprinted < view->nlines) {
3005 waddch(view->window, '\n');
3006 nprinted++;
3009 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3010 if (err) {
3011 return err;
3014 wstandout(view->window);
3015 waddwstr(view->window, wline);
3016 free(wline);
3017 wline = NULL;
3018 wstandend(view->window);
3021 return NULL;
3024 static char *
3025 get_datestr(time_t *time, char *datebuf)
3027 struct tm mytm, *tm;
3028 char *p, *s;
3030 tm = gmtime_r(time, &mytm);
3031 if (tm == NULL)
3032 return NULL;
3033 s = asctime_r(tm, datebuf);
3034 if (s == NULL)
3035 return NULL;
3036 p = strchr(s, '\n');
3037 if (p)
3038 *p = '\0';
3039 return s;
3042 static const struct got_error *
3043 get_changed_paths(struct got_pathlist_head *paths,
3044 struct got_commit_object *commit, struct got_repository *repo)
3046 const struct got_error *err = NULL;
3047 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3048 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3049 struct got_object_qid *qid;
3051 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3052 if (qid != NULL) {
3053 struct got_commit_object *pcommit;
3054 err = got_object_open_as_commit(&pcommit, repo,
3055 qid->id);
3056 if (err)
3057 return err;
3059 tree_id1 = got_object_commit_get_tree_id(pcommit);
3060 got_object_commit_close(pcommit);
3064 if (tree_id1) {
3065 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3066 if (err)
3067 goto done;
3070 tree_id2 = got_object_commit_get_tree_id(commit);
3071 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3072 if (err)
3073 goto done;
3075 err = got_diff_tree(tree1, tree2, "", "", repo,
3076 got_diff_tree_collect_changed_paths, paths, 0);
3077 done:
3078 if (tree1)
3079 got_object_tree_close(tree1);
3080 if (tree2)
3081 got_object_tree_close(tree2);
3082 return err;
3085 static const struct got_error *
3086 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3088 off_t *p;
3090 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3091 if (p == NULL)
3092 return got_error_from_errno("reallocarray");
3093 *line_offsets = p;
3094 (*line_offsets)[*nlines] = off;
3095 (*nlines)++;
3096 return NULL;
3099 static const struct got_error *
3100 write_commit_info(off_t **line_offsets, size_t *nlines,
3101 struct got_object_id *commit_id, struct got_reflist_head *refs,
3102 struct got_repository *repo, FILE *outfile)
3104 const struct got_error *err = NULL;
3105 char datebuf[26], *datestr;
3106 struct got_commit_object *commit;
3107 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3108 time_t committer_time;
3109 const char *author, *committer;
3110 char *refs_str = NULL;
3111 struct got_pathlist_head changed_paths;
3112 struct got_pathlist_entry *pe;
3113 off_t outoff = 0;
3114 int n;
3116 TAILQ_INIT(&changed_paths);
3118 if (refs) {
3119 err = build_refs_str(&refs_str, refs, commit_id, repo);
3120 if (err)
3121 return err;
3124 err = got_object_open_as_commit(&commit, repo, commit_id);
3125 if (err)
3126 return err;
3128 err = got_object_id_str(&id_str, commit_id);
3129 if (err) {
3130 err = got_error_from_errno("got_object_id_str");
3131 goto done;
3134 err = add_line_offset(line_offsets, nlines, 0);
3135 if (err)
3136 goto done;
3138 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3139 refs_str ? refs_str : "", refs_str ? ")" : "");
3140 if (n < 0) {
3141 err = got_error_from_errno("fprintf");
3142 goto done;
3144 outoff += n;
3145 err = add_line_offset(line_offsets, nlines, outoff);
3146 if (err)
3147 goto done;
3149 n = fprintf(outfile, "from: %s\n",
3150 got_object_commit_get_author(commit));
3151 if (n < 0) {
3152 err = got_error_from_errno("fprintf");
3153 goto done;
3155 outoff += n;
3156 err = add_line_offset(line_offsets, nlines, outoff);
3157 if (err)
3158 goto done;
3160 committer_time = got_object_commit_get_committer_time(commit);
3161 datestr = get_datestr(&committer_time, datebuf);
3162 if (datestr) {
3163 n = fprintf(outfile, "date: %s UTC\n", datestr);
3164 if (n < 0) {
3165 err = got_error_from_errno("fprintf");
3166 goto done;
3168 outoff += n;
3169 err = add_line_offset(line_offsets, nlines, outoff);
3170 if (err)
3171 goto done;
3173 author = got_object_commit_get_author(commit);
3174 committer = got_object_commit_get_committer(commit);
3175 if (strcmp(author, committer) != 0) {
3176 n = fprintf(outfile, "via: %s\n", committer);
3177 if (n < 0) {
3178 err = got_error_from_errno("fprintf");
3179 goto done;
3181 outoff += n;
3182 err = add_line_offset(line_offsets, nlines, outoff);
3183 if (err)
3184 goto done;
3186 err = got_object_commit_get_logmsg(&logmsg, commit);
3187 if (err)
3188 goto done;
3189 s = logmsg;
3190 while ((line = strsep(&s, "\n")) != NULL) {
3191 n = fprintf(outfile, "%s\n", line);
3192 if (n < 0) {
3193 err = got_error_from_errno("fprintf");
3194 goto done;
3196 outoff += n;
3197 err = add_line_offset(line_offsets, nlines, outoff);
3198 if (err)
3199 goto done;
3202 err = get_changed_paths(&changed_paths, commit, repo);
3203 if (err)
3204 goto done;
3205 TAILQ_FOREACH(pe, &changed_paths, entry) {
3206 struct got_diff_changed_path *cp = pe->data;
3207 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3208 if (n < 0) {
3209 err = got_error_from_errno("fprintf");
3210 goto done;
3212 outoff += n;
3213 err = add_line_offset(line_offsets, nlines, outoff);
3214 if (err)
3215 goto done;
3216 free((char *)pe->path);
3217 free(pe->data);
3220 fputc('\n', outfile);
3221 outoff++;
3222 err = add_line_offset(line_offsets, nlines, outoff);
3223 done:
3224 got_pathlist_free(&changed_paths);
3225 free(id_str);
3226 free(logmsg);
3227 free(refs_str);
3228 got_object_commit_close(commit);
3229 if (err) {
3230 free(*line_offsets);
3231 *line_offsets = NULL;
3232 *nlines = 0;
3234 return err;
3237 static const struct got_error *
3238 create_diff(struct tog_diff_view_state *s)
3240 const struct got_error *err = NULL;
3241 FILE *f = NULL;
3242 int obj_type;
3244 free(s->line_offsets);
3245 s->line_offsets = malloc(sizeof(off_t));
3246 if (s->line_offsets == NULL)
3247 return got_error_from_errno("malloc");
3248 s->nlines = 0;
3250 f = got_opentemp();
3251 if (f == NULL) {
3252 err = got_error_from_errno("got_opentemp");
3253 goto done;
3255 if (s->f && fclose(s->f) == EOF) {
3256 err = got_error_from_errno("fclose");
3257 goto done;
3259 s->f = f;
3261 if (s->id1)
3262 err = got_object_get_type(&obj_type, s->repo, s->id1);
3263 else
3264 err = got_object_get_type(&obj_type, s->repo, s->id2);
3265 if (err)
3266 goto done;
3268 switch (obj_type) {
3269 case GOT_OBJ_TYPE_BLOB:
3270 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3271 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3272 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3273 break;
3274 case GOT_OBJ_TYPE_TREE:
3275 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3276 s->id1, s->id2, "", "", s->diff_context,
3277 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3278 break;
3279 case GOT_OBJ_TYPE_COMMIT: {
3280 const struct got_object_id_queue *parent_ids;
3281 struct got_object_qid *pid;
3282 struct got_commit_object *commit2;
3283 struct got_reflist_head *refs;
3285 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3286 if (err)
3287 goto done;
3288 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3289 /* Show commit info if we're diffing to a parent/root commit. */
3290 if (s->id1 == NULL) {
3291 err = write_commit_info(&s->line_offsets, &s->nlines,
3292 s->id2, refs, s->repo, s->f);
3293 if (err)
3294 goto done;
3295 } else {
3296 parent_ids = got_object_commit_get_parent_ids(commit2);
3297 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3298 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3299 err = write_commit_info(
3300 &s->line_offsets, &s->nlines,
3301 s->id2, refs, s->repo, s->f);
3302 if (err)
3303 goto done;
3304 break;
3308 got_object_commit_close(commit2);
3310 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3311 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3312 s->force_text_diff, s->repo, s->f);
3313 break;
3315 default:
3316 err = got_error(GOT_ERR_OBJ_TYPE);
3317 break;
3319 if (err)
3320 goto done;
3321 done:
3322 if (s->f && fflush(s->f) != 0 && err == NULL)
3323 err = got_error_from_errno("fflush");
3324 return err;
3327 static void
3328 diff_view_indicate_progress(struct tog_view *view)
3330 mvwaddstr(view->window, 0, 0, "diffing...");
3331 update_panels();
3332 doupdate();
3335 static const struct got_error *
3336 search_start_diff_view(struct tog_view *view)
3338 struct tog_diff_view_state *s = &view->state.diff;
3340 s->matched_line = 0;
3341 return NULL;
3344 static const struct got_error *
3345 search_next_diff_view(struct tog_view *view)
3347 struct tog_diff_view_state *s = &view->state.diff;
3348 int lineno;
3349 char *line = NULL;
3350 size_t linesize = 0;
3351 ssize_t linelen;
3353 if (!view->searching) {
3354 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3355 return NULL;
3358 if (s->matched_line) {
3359 if (view->searching == TOG_SEARCH_FORWARD)
3360 lineno = s->matched_line + 1;
3361 else
3362 lineno = s->matched_line - 1;
3363 } else {
3364 if (view->searching == TOG_SEARCH_FORWARD)
3365 lineno = 1;
3366 else
3367 lineno = s->nlines;
3370 while (1) {
3371 off_t offset;
3373 if (lineno <= 0 || lineno > s->nlines) {
3374 if (s->matched_line == 0) {
3375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3376 break;
3379 if (view->searching == TOG_SEARCH_FORWARD)
3380 lineno = 1;
3381 else
3382 lineno = s->nlines;
3385 offset = s->line_offsets[lineno - 1];
3386 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3387 free(line);
3388 return got_error_from_errno("fseeko");
3390 linelen = getline(&line, &linesize, s->f);
3391 if (linelen != -1 &&
3392 match_line(line, &view->regex, 1, &view->regmatch)) {
3393 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3394 s->matched_line = lineno;
3395 break;
3397 if (view->searching == TOG_SEARCH_FORWARD)
3398 lineno++;
3399 else
3400 lineno--;
3402 free(line);
3404 if (s->matched_line) {
3405 s->first_displayed_line = s->matched_line;
3406 s->selected_line = 1;
3409 return NULL;
3412 static const struct got_error *
3413 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3414 struct got_object_id *id2, const char *label1, const char *label2,
3415 int diff_context, int ignore_whitespace, int force_text_diff,
3416 struct tog_view *log_view, struct got_repository *repo)
3418 const struct got_error *err;
3419 struct tog_diff_view_state *s = &view->state.diff;
3421 if (id1 != NULL && id2 != NULL) {
3422 int type1, type2;
3423 err = got_object_get_type(&type1, repo, id1);
3424 if (err)
3425 return err;
3426 err = got_object_get_type(&type2, repo, id2);
3427 if (err)
3428 return err;
3430 if (type1 != type2)
3431 return got_error(GOT_ERR_OBJ_TYPE);
3433 s->first_displayed_line = 1;
3434 s->last_displayed_line = view->nlines;
3435 s->selected_line = 1;
3436 s->repo = repo;
3437 s->id1 = id1;
3438 s->id2 = id2;
3439 s->label1 = label1;
3440 s->label2 = label2;
3442 if (id1) {
3443 s->id1 = got_object_id_dup(id1);
3444 if (s->id1 == NULL)
3445 return got_error_from_errno("got_object_id_dup");
3446 } else
3447 s->id1 = NULL;
3449 s->id2 = got_object_id_dup(id2);
3450 if (s->id2 == NULL) {
3451 free(s->id1);
3452 s->id1 = NULL;
3453 return got_error_from_errno("got_object_id_dup");
3455 s->f = NULL;
3456 s->first_displayed_line = 1;
3457 s->last_displayed_line = view->nlines;
3458 s->diff_context = diff_context;
3459 s->ignore_whitespace = ignore_whitespace;
3460 s->force_text_diff = force_text_diff;
3461 s->log_view = log_view;
3462 s->repo = repo;
3464 SIMPLEQ_INIT(&s->colors);
3465 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3466 err = add_color(&s->colors,
3467 "^-", TOG_COLOR_DIFF_MINUS,
3468 get_color_value("TOG_COLOR_DIFF_MINUS"));
3469 if (err)
3470 return err;
3471 err = add_color(&s->colors, "^\\+",
3472 TOG_COLOR_DIFF_PLUS,
3473 get_color_value("TOG_COLOR_DIFF_PLUS"));
3474 if (err) {
3475 free_colors(&s->colors);
3476 return err;
3478 err = add_color(&s->colors,
3479 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3480 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3481 if (err) {
3482 free_colors(&s->colors);
3483 return err;
3486 err = add_color(&s->colors,
3487 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3488 TOG_COLOR_DIFF_META,
3489 get_color_value("TOG_COLOR_DIFF_META"));
3490 if (err) {
3491 free_colors(&s->colors);
3492 return err;
3495 err = add_color(&s->colors,
3496 "^(from|via): ", TOG_COLOR_AUTHOR,
3497 get_color_value("TOG_COLOR_AUTHOR"));
3498 if (err) {
3499 free_colors(&s->colors);
3500 return err;
3503 err = add_color(&s->colors,
3504 "^date: ", TOG_COLOR_DATE,
3505 get_color_value("TOG_COLOR_DATE"));
3506 if (err) {
3507 free_colors(&s->colors);
3508 return err;
3512 if (log_view && view_is_splitscreen(view))
3513 show_log_view(log_view); /* draw vborder */
3514 diff_view_indicate_progress(view);
3516 s->line_offsets = NULL;
3517 s->nlines = 0;
3518 err = create_diff(s);
3519 if (err) {
3520 free(s->id1);
3521 s->id1 = NULL;
3522 free(s->id2);
3523 s->id2 = NULL;
3524 free_colors(&s->colors);
3525 return err;
3528 view->show = show_diff_view;
3529 view->input = input_diff_view;
3530 view->close = close_diff_view;
3531 view->search_start = search_start_diff_view;
3532 view->search_next = search_next_diff_view;
3534 return NULL;
3537 static const struct got_error *
3538 close_diff_view(struct tog_view *view)
3540 const struct got_error *err = NULL;
3541 struct tog_diff_view_state *s = &view->state.diff;
3543 free(s->id1);
3544 s->id1 = NULL;
3545 free(s->id2);
3546 s->id2 = NULL;
3547 if (s->f && fclose(s->f) == EOF)
3548 err = got_error_from_errno("fclose");
3549 free_colors(&s->colors);
3550 free(s->line_offsets);
3551 s->line_offsets = NULL;
3552 s->nlines = 0;
3553 return err;
3556 static const struct got_error *
3557 show_diff_view(struct tog_view *view)
3559 const struct got_error *err;
3560 struct tog_diff_view_state *s = &view->state.diff;
3561 char *id_str1 = NULL, *id_str2, *header;
3562 const char *label1, *label2;
3564 if (s->id1) {
3565 err = got_object_id_str(&id_str1, s->id1);
3566 if (err)
3567 return err;
3568 label1 = s->label1 ? : id_str1;
3569 } else
3570 label1 = "/dev/null";
3572 err = got_object_id_str(&id_str2, s->id2);
3573 if (err)
3574 return err;
3575 label2 = s->label2 ? : id_str2;
3577 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3578 err = got_error_from_errno("asprintf");
3579 free(id_str1);
3580 free(id_str2);
3581 return err;
3583 free(id_str1);
3584 free(id_str2);
3586 return draw_file(view, header);
3589 static const struct got_error *
3590 set_selected_commit(struct tog_diff_view_state *s,
3591 struct commit_queue_entry *entry)
3593 const struct got_error *err;
3594 const struct got_object_id_queue *parent_ids;
3595 struct got_commit_object *selected_commit;
3596 struct got_object_qid *pid;
3598 free(s->id2);
3599 s->id2 = got_object_id_dup(entry->id);
3600 if (s->id2 == NULL)
3601 return got_error_from_errno("got_object_id_dup");
3603 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3604 if (err)
3605 return err;
3606 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3607 free(s->id1);
3608 pid = SIMPLEQ_FIRST(parent_ids);
3609 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3610 got_object_commit_close(selected_commit);
3611 return NULL;
3614 static const struct got_error *
3615 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3617 const struct got_error *err = NULL;
3618 struct tog_diff_view_state *s = &view->state.diff;
3619 struct tog_log_view_state *ls;
3620 struct commit_queue_entry *old_selected_entry;
3621 char *line = NULL;
3622 size_t linesize = 0;
3623 ssize_t linelen;
3624 int i;
3626 switch (ch) {
3627 case 'a':
3628 case 'w':
3629 if (ch == 'a')
3630 s->force_text_diff = !s->force_text_diff;
3631 if (ch == 'w')
3632 s->ignore_whitespace = !s->ignore_whitespace;
3633 wclear(view->window);
3634 s->first_displayed_line = 1;
3635 s->last_displayed_line = view->nlines;
3636 diff_view_indicate_progress(view);
3637 err = create_diff(s);
3638 break;
3639 case 'k':
3640 case KEY_UP:
3641 if (s->first_displayed_line > 1)
3642 s->first_displayed_line--;
3643 break;
3644 case KEY_PPAGE:
3645 case CTRL('b'):
3646 if (s->first_displayed_line == 1)
3647 break;
3648 i = 0;
3649 while (i++ < view->nlines - 1 &&
3650 s->first_displayed_line > 1)
3651 s->first_displayed_line--;
3652 break;
3653 case 'j':
3654 case KEY_DOWN:
3655 if (!s->eof)
3656 s->first_displayed_line++;
3657 break;
3658 case KEY_NPAGE:
3659 case CTRL('f'):
3660 case ' ':
3661 if (s->eof)
3662 break;
3663 i = 0;
3664 while (!s->eof && i++ < view->nlines - 1) {
3665 linelen = getline(&line, &linesize, s->f);
3666 s->first_displayed_line++;
3667 if (linelen == -1) {
3668 if (feof(s->f)) {
3669 s->eof = 1;
3670 } else
3671 err = got_ferror(s->f, GOT_ERR_IO);
3672 break;
3675 free(line);
3676 break;
3677 case '[':
3678 if (s->diff_context > 0) {
3679 s->diff_context--;
3680 diff_view_indicate_progress(view);
3681 err = create_diff(s);
3682 if (s->first_displayed_line + view->nlines - 1 >
3683 s->nlines) {
3684 s->first_displayed_line = 1;
3685 s->last_displayed_line = view->nlines;
3688 break;
3689 case ']':
3690 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3691 s->diff_context++;
3692 diff_view_indicate_progress(view);
3693 err = create_diff(s);
3695 break;
3696 case '<':
3697 case ',':
3698 if (s->log_view == NULL)
3699 break;
3700 ls = &s->log_view->state.log;
3701 old_selected_entry = ls->selected_entry;
3703 err = input_log_view(NULL, s->log_view, KEY_UP);
3704 if (err)
3705 break;
3707 if (old_selected_entry == ls->selected_entry)
3708 break;
3710 err = set_selected_commit(s, ls->selected_entry);
3711 if (err)
3712 break;
3714 s->first_displayed_line = 1;
3715 s->last_displayed_line = view->nlines;
3717 diff_view_indicate_progress(view);
3718 err = create_diff(s);
3719 break;
3720 case '>':
3721 case '.':
3722 if (s->log_view == NULL)
3723 break;
3724 ls = &s->log_view->state.log;
3725 old_selected_entry = ls->selected_entry;
3727 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3728 if (err)
3729 break;
3731 if (old_selected_entry == ls->selected_entry)
3732 break;
3734 err = set_selected_commit(s, ls->selected_entry);
3735 if (err)
3736 break;
3738 s->first_displayed_line = 1;
3739 s->last_displayed_line = view->nlines;
3741 diff_view_indicate_progress(view);
3742 err = create_diff(s);
3743 break;
3744 default:
3745 break;
3748 return err;
3751 static const struct got_error *
3752 cmd_diff(int argc, char *argv[])
3754 const struct got_error *error = NULL;
3755 struct got_repository *repo = NULL;
3756 struct got_worktree *worktree = NULL;
3757 struct got_object_id *id1 = NULL, *id2 = NULL;
3758 char *repo_path = NULL, *cwd = NULL;
3759 char *id_str1 = NULL, *id_str2 = NULL;
3760 char *label1 = NULL, *label2 = NULL;
3761 int diff_context = 3, ignore_whitespace = 0;
3762 int ch, force_text_diff = 0;
3763 const char *errstr;
3764 struct tog_view *view;
3766 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3767 switch (ch) {
3768 case 'a':
3769 force_text_diff = 1;
3770 break;
3771 case 'C':
3772 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3773 &errstr);
3774 if (errstr != NULL)
3775 err(1, "-C option %s", errstr);
3776 break;
3777 case 'r':
3778 repo_path = realpath(optarg, NULL);
3779 if (repo_path == NULL)
3780 return got_error_from_errno2("realpath",
3781 optarg);
3782 got_path_strip_trailing_slashes(repo_path);
3783 break;
3784 case 'w':
3785 ignore_whitespace = 1;
3786 break;
3787 default:
3788 usage_diff();
3789 /* NOTREACHED */
3793 argc -= optind;
3794 argv += optind;
3796 if (argc == 0) {
3797 usage_diff(); /* TODO show local worktree changes */
3798 } else if (argc == 2) {
3799 id_str1 = argv[0];
3800 id_str2 = argv[1];
3801 } else
3802 usage_diff();
3804 if (repo_path == NULL) {
3805 cwd = getcwd(NULL, 0);
3806 if (cwd == NULL)
3807 return got_error_from_errno("getcwd");
3808 error = got_worktree_open(&worktree, cwd);
3809 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3810 goto done;
3811 if (worktree)
3812 repo_path =
3813 strdup(got_worktree_get_repo_path(worktree));
3814 else
3815 repo_path = strdup(cwd);
3816 if (repo_path == NULL) {
3817 error = got_error_from_errno("strdup");
3818 goto done;
3822 error = got_repo_open(&repo, repo_path, NULL);
3823 if (error)
3824 goto done;
3826 init_curses();
3828 error = apply_unveil(got_repo_get_path(repo), NULL);
3829 if (error)
3830 goto done;
3832 error = tog_load_refs(repo);
3833 if (error)
3834 goto done;
3836 error = got_repo_match_object_id(&id1, &label1, id_str1,
3837 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3838 if (error)
3839 goto done;
3841 error = got_repo_match_object_id(&id2, &label2, id_str2,
3842 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3843 if (error)
3844 goto done;
3846 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3847 if (view == NULL) {
3848 error = got_error_from_errno("view_open");
3849 goto done;
3851 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3852 ignore_whitespace, force_text_diff, NULL, repo);
3853 if (error)
3854 goto done;
3855 error = view_loop(view);
3856 done:
3857 free(label1);
3858 free(label2);
3859 free(repo_path);
3860 free(cwd);
3861 if (repo)
3862 got_repo_close(repo);
3863 if (worktree)
3864 got_worktree_close(worktree);
3865 tog_free_refs();
3866 return error;
3869 __dead static void
3870 usage_blame(void)
3872 endwin();
3873 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3874 getprogname());
3875 exit(1);
3878 struct tog_blame_line {
3879 int annotated;
3880 struct got_object_id *id;
3883 static const struct got_error *
3884 draw_blame(struct tog_view *view)
3886 struct tog_blame_view_state *s = &view->state.blame;
3887 struct tog_blame *blame = &s->blame;
3888 regmatch_t *regmatch = &view->regmatch;
3889 const struct got_error *err;
3890 int lineno = 0, nprinted = 0;
3891 char *line = NULL;
3892 size_t linesize = 0;
3893 ssize_t linelen;
3894 wchar_t *wline;
3895 int width;
3896 struct tog_blame_line *blame_line;
3897 struct got_object_id *prev_id = NULL;
3898 char *id_str;
3899 struct tog_color *tc;
3901 err = got_object_id_str(&id_str, s->blamed_commit->id);
3902 if (err)
3903 return err;
3905 rewind(blame->f);
3906 werase(view->window);
3908 if (asprintf(&line, "commit %s", id_str) == -1) {
3909 err = got_error_from_errno("asprintf");
3910 free(id_str);
3911 return err;
3914 err = format_line(&wline, &width, line, view->ncols, 0);
3915 free(line);
3916 line = NULL;
3917 if (err)
3918 return err;
3919 if (view_needs_focus_indication(view))
3920 wstandout(view->window);
3921 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3922 if (tc)
3923 wattr_on(view->window,
3924 COLOR_PAIR(tc->colorpair), NULL);
3925 waddwstr(view->window, wline);
3926 if (tc)
3927 wattr_off(view->window,
3928 COLOR_PAIR(tc->colorpair), NULL);
3929 if (view_needs_focus_indication(view))
3930 wstandend(view->window);
3931 free(wline);
3932 wline = NULL;
3933 if (width < view->ncols - 1)
3934 waddch(view->window, '\n');
3936 if (asprintf(&line, "[%d/%d] %s%s",
3937 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3938 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3939 free(id_str);
3940 return got_error_from_errno("asprintf");
3942 free(id_str);
3943 err = format_line(&wline, &width, line, view->ncols, 0);
3944 free(line);
3945 line = NULL;
3946 if (err)
3947 return err;
3948 waddwstr(view->window, wline);
3949 free(wline);
3950 wline = NULL;
3951 if (width < view->ncols - 1)
3952 waddch(view->window, '\n');
3954 s->eof = 0;
3955 while (nprinted < view->nlines - 2) {
3956 linelen = getline(&line, &linesize, blame->f);
3957 if (linelen == -1) {
3958 if (feof(blame->f)) {
3959 s->eof = 1;
3960 break;
3962 free(line);
3963 return got_ferror(blame->f, GOT_ERR_IO);
3965 if (++lineno < s->first_displayed_line)
3966 continue;
3968 if (view->focussed && nprinted == s->selected_line - 1)
3969 wstandout(view->window);
3971 if (blame->nlines > 0) {
3972 blame_line = &blame->lines[lineno - 1];
3973 if (blame_line->annotated && prev_id &&
3974 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3975 !(view->focussed &&
3976 nprinted == s->selected_line - 1)) {
3977 waddstr(view->window, " ");
3978 } else if (blame_line->annotated) {
3979 char *id_str;
3980 err = got_object_id_str(&id_str, blame_line->id);
3981 if (err) {
3982 free(line);
3983 return err;
3985 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3986 if (tc)
3987 wattr_on(view->window,
3988 COLOR_PAIR(tc->colorpair), NULL);
3989 wprintw(view->window, "%.8s", id_str);
3990 if (tc)
3991 wattr_off(view->window,
3992 COLOR_PAIR(tc->colorpair), NULL);
3993 free(id_str);
3994 prev_id = blame_line->id;
3995 } else {
3996 waddstr(view->window, "........");
3997 prev_id = NULL;
3999 } else {
4000 waddstr(view->window, "........");
4001 prev_id = NULL;
4004 if (view->focussed && nprinted == s->selected_line - 1)
4005 wstandend(view->window);
4006 waddstr(view->window, " ");
4008 if (view->ncols <= 9) {
4009 width = 9;
4010 wline = wcsdup(L"");
4011 if (wline == NULL) {
4012 err = got_error_from_errno("wcsdup");
4013 free(line);
4014 return err;
4016 } else if (s->first_displayed_line + nprinted ==
4017 s->matched_line &&
4018 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4019 err = add_matched_line(&width, line, view->ncols - 9, 9,
4020 view->window, regmatch);
4021 if (err) {
4022 free(line);
4023 return err;
4025 width += 9;
4026 } else {
4027 err = format_line(&wline, &width, line,
4028 view->ncols - 9, 9);
4029 waddwstr(view->window, wline);
4030 free(wline);
4031 wline = NULL;
4032 width += 9;
4035 if (width <= view->ncols - 1)
4036 waddch(view->window, '\n');
4037 if (++nprinted == 1)
4038 s->first_displayed_line = lineno;
4040 free(line);
4041 s->last_displayed_line = lineno;
4043 view_vborder(view);
4045 return NULL;
4048 static const struct got_error *
4049 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4051 const struct got_error *err = NULL;
4052 struct tog_blame_cb_args *a = arg;
4053 struct tog_blame_line *line;
4054 int errcode;
4056 if (nlines != a->nlines ||
4057 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4058 return got_error(GOT_ERR_RANGE);
4060 errcode = pthread_mutex_lock(&tog_mutex);
4061 if (errcode)
4062 return got_error_set_errno(errcode, "pthread_mutex_lock");
4064 if (*a->quit) { /* user has quit the blame view */
4065 err = got_error(GOT_ERR_ITER_COMPLETED);
4066 goto done;
4069 if (lineno == -1)
4070 goto done; /* no change in this commit */
4072 line = &a->lines[lineno - 1];
4073 if (line->annotated)
4074 goto done;
4076 line->id = got_object_id_dup(id);
4077 if (line->id == NULL) {
4078 err = got_error_from_errno("got_object_id_dup");
4079 goto done;
4081 line->annotated = 1;
4082 done:
4083 errcode = pthread_mutex_unlock(&tog_mutex);
4084 if (errcode)
4085 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4086 return err;
4089 static void *
4090 blame_thread(void *arg)
4092 const struct got_error *err;
4093 struct tog_blame_thread_args *ta = arg;
4094 struct tog_blame_cb_args *a = ta->cb_args;
4095 int errcode;
4097 err = block_signals_used_by_main_thread();
4098 if (err)
4099 return (void *)err;
4101 err = got_blame(ta->path, a->commit_id, ta->repo,
4102 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4103 if (err && err->code == GOT_ERR_CANCELLED)
4104 err = NULL;
4106 errcode = pthread_mutex_lock(&tog_mutex);
4107 if (errcode)
4108 return (void *)got_error_set_errno(errcode,
4109 "pthread_mutex_lock");
4111 got_repo_close(ta->repo);
4112 ta->repo = NULL;
4113 *ta->complete = 1;
4115 errcode = pthread_mutex_unlock(&tog_mutex);
4116 if (errcode && err == NULL)
4117 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4119 return (void *)err;
4122 static struct got_object_id *
4123 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4124 int first_displayed_line, int selected_line)
4126 struct tog_blame_line *line;
4128 if (nlines <= 0)
4129 return NULL;
4131 line = &lines[first_displayed_line - 1 + selected_line - 1];
4132 if (!line->annotated)
4133 return NULL;
4135 return line->id;
4138 static const struct got_error *
4139 stop_blame(struct tog_blame *blame)
4141 const struct got_error *err = NULL;
4142 int i;
4144 if (blame->thread) {
4145 int errcode;
4146 errcode = pthread_mutex_unlock(&tog_mutex);
4147 if (errcode)
4148 return got_error_set_errno(errcode,
4149 "pthread_mutex_unlock");
4150 errcode = pthread_join(blame->thread, (void **)&err);
4151 if (errcode)
4152 return got_error_set_errno(errcode, "pthread_join");
4153 errcode = pthread_mutex_lock(&tog_mutex);
4154 if (errcode)
4155 return got_error_set_errno(errcode,
4156 "pthread_mutex_lock");
4157 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4158 err = NULL;
4159 blame->thread = NULL;
4161 if (blame->thread_args.repo) {
4162 got_repo_close(blame->thread_args.repo);
4163 blame->thread_args.repo = NULL;
4165 if (blame->f) {
4166 if (fclose(blame->f) == EOF && err == NULL)
4167 err = got_error_from_errno("fclose");
4168 blame->f = NULL;
4170 if (blame->lines) {
4171 for (i = 0; i < blame->nlines; i++)
4172 free(blame->lines[i].id);
4173 free(blame->lines);
4174 blame->lines = NULL;
4176 free(blame->cb_args.commit_id);
4177 blame->cb_args.commit_id = NULL;
4179 return err;
4182 static const struct got_error *
4183 cancel_blame_view(void *arg)
4185 const struct got_error *err = NULL;
4186 int *done = arg;
4187 int errcode;
4189 errcode = pthread_mutex_lock(&tog_mutex);
4190 if (errcode)
4191 return got_error_set_errno(errcode,
4192 "pthread_mutex_unlock");
4194 if (*done)
4195 err = got_error(GOT_ERR_CANCELLED);
4197 errcode = pthread_mutex_unlock(&tog_mutex);
4198 if (errcode)
4199 return got_error_set_errno(errcode,
4200 "pthread_mutex_lock");
4202 return err;
4205 static const struct got_error *
4206 run_blame(struct tog_view *view)
4208 struct tog_blame_view_state *s = &view->state.blame;
4209 struct tog_blame *blame = &s->blame;
4210 const struct got_error *err = NULL;
4211 struct got_blob_object *blob = NULL;
4212 struct got_repository *thread_repo = NULL;
4213 struct got_object_id *obj_id = NULL;
4214 int obj_type;
4216 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4217 s->path);
4218 if (err)
4219 return err;
4221 err = got_object_get_type(&obj_type, s->repo, obj_id);
4222 if (err)
4223 goto done;
4225 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4226 err = got_error(GOT_ERR_OBJ_TYPE);
4227 goto done;
4230 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4231 if (err)
4232 goto done;
4233 blame->f = got_opentemp();
4234 if (blame->f == NULL) {
4235 err = got_error_from_errno("got_opentemp");
4236 goto done;
4238 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4239 &blame->line_offsets, blame->f, blob);
4240 if (err)
4241 goto done;
4242 if (blame->nlines == 0) {
4243 s->blame_complete = 1;
4244 goto done;
4247 /* Don't include \n at EOF in the blame line count. */
4248 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4249 blame->nlines--;
4251 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4252 if (blame->lines == NULL) {
4253 err = got_error_from_errno("calloc");
4254 goto done;
4257 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4258 if (err)
4259 goto done;
4261 blame->cb_args.view = view;
4262 blame->cb_args.lines = blame->lines;
4263 blame->cb_args.nlines = blame->nlines;
4264 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4265 if (blame->cb_args.commit_id == NULL) {
4266 err = got_error_from_errno("got_object_id_dup");
4267 goto done;
4269 blame->cb_args.quit = &s->done;
4271 blame->thread_args.path = s->path;
4272 blame->thread_args.repo = thread_repo;
4273 blame->thread_args.cb_args = &blame->cb_args;
4274 blame->thread_args.complete = &s->blame_complete;
4275 blame->thread_args.cancel_cb = cancel_blame_view;
4276 blame->thread_args.cancel_arg = &s->done;
4277 s->blame_complete = 0;
4279 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4280 s->first_displayed_line = 1;
4281 s->last_displayed_line = view->nlines;
4282 s->selected_line = 1;
4285 done:
4286 if (blob)
4287 got_object_blob_close(blob);
4288 free(obj_id);
4289 if (err)
4290 stop_blame(blame);
4291 return err;
4294 static const struct got_error *
4295 open_blame_view(struct tog_view *view, char *path,
4296 struct got_object_id *commit_id, struct got_repository *repo)
4298 const struct got_error *err = NULL;
4299 struct tog_blame_view_state *s = &view->state.blame;
4301 SIMPLEQ_INIT(&s->blamed_commits);
4303 s->path = strdup(path);
4304 if (s->path == NULL)
4305 return got_error_from_errno("strdup");
4307 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4308 if (err) {
4309 free(s->path);
4310 return err;
4313 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4314 s->first_displayed_line = 1;
4315 s->last_displayed_line = view->nlines;
4316 s->selected_line = 1;
4317 s->blame_complete = 0;
4318 s->repo = repo;
4319 s->commit_id = commit_id;
4320 memset(&s->blame, 0, sizeof(s->blame));
4322 SIMPLEQ_INIT(&s->colors);
4323 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4324 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4325 get_color_value("TOG_COLOR_COMMIT"));
4326 if (err)
4327 return err;
4330 view->show = show_blame_view;
4331 view->input = input_blame_view;
4332 view->close = close_blame_view;
4333 view->search_start = search_start_blame_view;
4334 view->search_next = search_next_blame_view;
4336 return run_blame(view);
4339 static const struct got_error *
4340 close_blame_view(struct tog_view *view)
4342 const struct got_error *err = NULL;
4343 struct tog_blame_view_state *s = &view->state.blame;
4345 if (s->blame.thread)
4346 err = stop_blame(&s->blame);
4348 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4349 struct got_object_qid *blamed_commit;
4350 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4351 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4352 got_object_qid_free(blamed_commit);
4355 free(s->path);
4356 free_colors(&s->colors);
4358 return err;
4361 static const struct got_error *
4362 search_start_blame_view(struct tog_view *view)
4364 struct tog_blame_view_state *s = &view->state.blame;
4366 s->matched_line = 0;
4367 return NULL;
4370 static const struct got_error *
4371 search_next_blame_view(struct tog_view *view)
4373 struct tog_blame_view_state *s = &view->state.blame;
4374 int lineno;
4375 char *line = NULL;
4376 size_t linesize = 0;
4377 ssize_t linelen;
4379 if (!view->searching) {
4380 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4381 return NULL;
4384 if (s->matched_line) {
4385 if (view->searching == TOG_SEARCH_FORWARD)
4386 lineno = s->matched_line + 1;
4387 else
4388 lineno = s->matched_line - 1;
4389 } else {
4390 if (view->searching == TOG_SEARCH_FORWARD)
4391 lineno = 1;
4392 else
4393 lineno = s->blame.nlines;
4396 while (1) {
4397 off_t offset;
4399 if (lineno <= 0 || lineno > s->blame.nlines) {
4400 if (s->matched_line == 0) {
4401 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4402 break;
4405 if (view->searching == TOG_SEARCH_FORWARD)
4406 lineno = 1;
4407 else
4408 lineno = s->blame.nlines;
4411 offset = s->blame.line_offsets[lineno - 1];
4412 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4413 free(line);
4414 return got_error_from_errno("fseeko");
4416 linelen = getline(&line, &linesize, s->blame.f);
4417 if (linelen != -1 &&
4418 match_line(line, &view->regex, 1, &view->regmatch)) {
4419 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4420 s->matched_line = lineno;
4421 break;
4423 if (view->searching == TOG_SEARCH_FORWARD)
4424 lineno++;
4425 else
4426 lineno--;
4428 free(line);
4430 if (s->matched_line) {
4431 s->first_displayed_line = s->matched_line;
4432 s->selected_line = 1;
4435 return NULL;
4438 static const struct got_error *
4439 show_blame_view(struct tog_view *view)
4441 const struct got_error *err = NULL;
4442 struct tog_blame_view_state *s = &view->state.blame;
4443 int errcode;
4445 if (s->blame.thread == NULL && !s->blame_complete) {
4446 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4447 &s->blame.thread_args);
4448 if (errcode)
4449 return got_error_set_errno(errcode, "pthread_create");
4451 halfdelay(1); /* fast refresh while annotating */
4454 if (s->blame_complete)
4455 halfdelay(10); /* disable fast refresh */
4457 err = draw_blame(view);
4459 view_vborder(view);
4460 return err;
4463 static const struct got_error *
4464 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4466 const struct got_error *err = NULL, *thread_err = NULL;
4467 struct tog_view *diff_view;
4468 struct tog_blame_view_state *s = &view->state.blame;
4469 int begin_x = 0;
4471 switch (ch) {
4472 case 'q':
4473 s->done = 1;
4474 break;
4475 case 'k':
4476 case KEY_UP:
4477 if (s->selected_line > 1)
4478 s->selected_line--;
4479 else if (s->selected_line == 1 &&
4480 s->first_displayed_line > 1)
4481 s->first_displayed_line--;
4482 break;
4483 case KEY_PPAGE:
4484 case CTRL('b'):
4485 if (s->first_displayed_line == 1) {
4486 s->selected_line = 1;
4487 break;
4489 if (s->first_displayed_line > view->nlines - 2)
4490 s->first_displayed_line -=
4491 (view->nlines - 2);
4492 else
4493 s->first_displayed_line = 1;
4494 break;
4495 case 'j':
4496 case KEY_DOWN:
4497 if (s->selected_line < view->nlines - 2 &&
4498 s->first_displayed_line +
4499 s->selected_line <= s->blame.nlines)
4500 s->selected_line++;
4501 else if (s->last_displayed_line <
4502 s->blame.nlines)
4503 s->first_displayed_line++;
4504 break;
4505 case 'b':
4506 case 'p': {
4507 struct got_object_id *id = NULL;
4508 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4509 s->first_displayed_line, s->selected_line);
4510 if (id == NULL)
4511 break;
4512 if (ch == 'p') {
4513 struct got_commit_object *commit;
4514 struct got_object_qid *pid;
4515 struct got_object_id *blob_id = NULL;
4516 int obj_type;
4517 err = got_object_open_as_commit(&commit,
4518 s->repo, id);
4519 if (err)
4520 break;
4521 pid = SIMPLEQ_FIRST(
4522 got_object_commit_get_parent_ids(commit));
4523 if (pid == NULL) {
4524 got_object_commit_close(commit);
4525 break;
4527 /* Check if path history ends here. */
4528 err = got_object_id_by_path(&blob_id, s->repo,
4529 pid->id, s->path);
4530 if (err) {
4531 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4532 err = NULL;
4533 got_object_commit_close(commit);
4534 break;
4536 err = got_object_get_type(&obj_type, s->repo,
4537 blob_id);
4538 free(blob_id);
4539 /* Can't blame non-blob type objects. */
4540 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4541 got_object_commit_close(commit);
4542 break;
4544 err = got_object_qid_alloc(&s->blamed_commit,
4545 pid->id);
4546 got_object_commit_close(commit);
4547 } else {
4548 if (got_object_id_cmp(id,
4549 s->blamed_commit->id) == 0)
4550 break;
4551 err = got_object_qid_alloc(&s->blamed_commit,
4552 id);
4554 if (err)
4555 break;
4556 s->done = 1;
4557 thread_err = stop_blame(&s->blame);
4558 s->done = 0;
4559 if (thread_err)
4560 break;
4561 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4562 s->blamed_commit, entry);
4563 err = run_blame(view);
4564 if (err)
4565 break;
4566 break;
4568 case 'B': {
4569 struct got_object_qid *first;
4570 first = SIMPLEQ_FIRST(&s->blamed_commits);
4571 if (!got_object_id_cmp(first->id, s->commit_id))
4572 break;
4573 s->done = 1;
4574 thread_err = stop_blame(&s->blame);
4575 s->done = 0;
4576 if (thread_err)
4577 break;
4578 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4579 got_object_qid_free(s->blamed_commit);
4580 s->blamed_commit =
4581 SIMPLEQ_FIRST(&s->blamed_commits);
4582 err = run_blame(view);
4583 if (err)
4584 break;
4585 break;
4587 case KEY_ENTER:
4588 case '\r': {
4589 struct got_object_id *id = NULL;
4590 struct got_object_qid *pid;
4591 struct got_commit_object *commit = NULL;
4592 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4593 s->first_displayed_line, s->selected_line);
4594 if (id == NULL)
4595 break;
4596 err = got_object_open_as_commit(&commit, s->repo, id);
4597 if (err)
4598 break;
4599 pid = SIMPLEQ_FIRST(
4600 got_object_commit_get_parent_ids(commit));
4601 if (view_is_parent_view(view))
4602 begin_x = view_split_begin_x(view->begin_x);
4603 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4604 if (diff_view == NULL) {
4605 got_object_commit_close(commit);
4606 err = got_error_from_errno("view_open");
4607 break;
4609 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4610 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4611 got_object_commit_close(commit);
4612 if (err) {
4613 view_close(diff_view);
4614 break;
4616 view->focussed = 0;
4617 diff_view->focussed = 1;
4618 if (view_is_parent_view(view)) {
4619 err = view_close_child(view);
4620 if (err)
4621 break;
4622 view_set_child(view, diff_view);
4623 view->focus_child = 1;
4624 } else
4625 *new_view = diff_view;
4626 if (err)
4627 break;
4628 break;
4630 case KEY_NPAGE:
4631 case CTRL('f'):
4632 case ' ':
4633 if (s->last_displayed_line >= s->blame.nlines &&
4634 s->selected_line >= MIN(s->blame.nlines,
4635 view->nlines - 2)) {
4636 break;
4638 if (s->last_displayed_line >= s->blame.nlines &&
4639 s->selected_line < view->nlines - 2) {
4640 s->selected_line = MIN(s->blame.nlines,
4641 view->nlines - 2);
4642 break;
4644 if (s->last_displayed_line + view->nlines - 2
4645 <= s->blame.nlines)
4646 s->first_displayed_line +=
4647 view->nlines - 2;
4648 else
4649 s->first_displayed_line =
4650 s->blame.nlines -
4651 (view->nlines - 3);
4652 break;
4653 case KEY_RESIZE:
4654 if (s->selected_line > view->nlines - 2) {
4655 s->selected_line = MIN(s->blame.nlines,
4656 view->nlines - 2);
4658 break;
4659 default:
4660 break;
4662 return thread_err ? thread_err : err;
4665 static const struct got_error *
4666 cmd_blame(int argc, char *argv[])
4668 const struct got_error *error;
4669 struct got_repository *repo = NULL;
4670 struct got_worktree *worktree = NULL;
4671 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4672 char *link_target = NULL;
4673 struct got_object_id *commit_id = NULL;
4674 char *commit_id_str = NULL;
4675 int ch;
4676 struct tog_view *view;
4678 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4679 switch (ch) {
4680 case 'c':
4681 commit_id_str = optarg;
4682 break;
4683 case 'r':
4684 repo_path = realpath(optarg, NULL);
4685 if (repo_path == NULL)
4686 return got_error_from_errno2("realpath",
4687 optarg);
4688 break;
4689 default:
4690 usage_blame();
4691 /* NOTREACHED */
4695 argc -= optind;
4696 argv += optind;
4698 if (argc != 1)
4699 usage_blame();
4701 if (repo_path == NULL) {
4702 cwd = getcwd(NULL, 0);
4703 if (cwd == NULL)
4704 return got_error_from_errno("getcwd");
4705 error = got_worktree_open(&worktree, cwd);
4706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4707 goto done;
4708 if (worktree)
4709 repo_path =
4710 strdup(got_worktree_get_repo_path(worktree));
4711 else
4712 repo_path = strdup(cwd);
4713 if (repo_path == NULL) {
4714 error = got_error_from_errno("strdup");
4715 goto done;
4719 error = got_repo_open(&repo, repo_path, NULL);
4720 if (error != NULL)
4721 goto done;
4723 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4724 worktree);
4725 if (error)
4726 goto done;
4728 init_curses();
4730 error = apply_unveil(got_repo_get_path(repo), NULL);
4731 if (error)
4732 goto done;
4734 error = tog_load_refs(repo);
4735 if (error)
4736 goto done;
4738 if (commit_id_str == NULL) {
4739 struct got_reference *head_ref;
4740 error = got_ref_open(&head_ref, repo, worktree ?
4741 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4742 if (error != NULL)
4743 goto done;
4744 error = got_ref_resolve(&commit_id, repo, head_ref);
4745 got_ref_close(head_ref);
4746 } else {
4747 error = got_repo_match_object_id(&commit_id, NULL,
4748 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4750 if (error != NULL)
4751 goto done;
4753 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4754 if (view == NULL) {
4755 error = got_error_from_errno("view_open");
4756 goto done;
4759 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4760 commit_id, repo);
4761 if (error)
4762 goto done;
4764 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4765 commit_id, repo);
4766 if (error)
4767 goto done;
4768 if (worktree) {
4769 /* Release work tree lock. */
4770 got_worktree_close(worktree);
4771 worktree = NULL;
4773 error = view_loop(view);
4774 done:
4775 free(repo_path);
4776 free(in_repo_path);
4777 free(link_target);
4778 free(cwd);
4779 free(commit_id);
4780 if (worktree)
4781 got_worktree_close(worktree);
4782 if (repo)
4783 got_repo_close(repo);
4784 tog_free_refs();
4785 return error;
4788 static const struct got_error *
4789 draw_tree_entries(struct tog_view *view, const char *parent_path)
4791 struct tog_tree_view_state *s = &view->state.tree;
4792 const struct got_error *err = NULL;
4793 struct got_tree_entry *te;
4794 wchar_t *wline;
4795 struct tog_color *tc;
4796 int width, n, i, nentries;
4797 int limit = view->nlines;
4799 s->ndisplayed = 0;
4801 werase(view->window);
4803 if (limit == 0)
4804 return NULL;
4806 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4807 if (err)
4808 return err;
4809 if (view_needs_focus_indication(view))
4810 wstandout(view->window);
4811 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4812 if (tc)
4813 wattr_on(view->window,
4814 COLOR_PAIR(tc->colorpair), NULL);
4815 waddwstr(view->window, wline);
4816 if (tc)
4817 wattr_off(view->window,
4818 COLOR_PAIR(tc->colorpair), NULL);
4819 if (view_needs_focus_indication(view))
4820 wstandend(view->window);
4821 free(wline);
4822 wline = NULL;
4823 if (width < view->ncols - 1)
4824 waddch(view->window, '\n');
4825 if (--limit <= 0)
4826 return NULL;
4827 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4828 if (err)
4829 return err;
4830 waddwstr(view->window, wline);
4831 free(wline);
4832 wline = NULL;
4833 if (width < view->ncols - 1)
4834 waddch(view->window, '\n');
4835 if (--limit <= 0)
4836 return NULL;
4837 waddch(view->window, '\n');
4838 if (--limit <= 0)
4839 return NULL;
4841 if (s->first_displayed_entry == NULL) {
4842 te = got_object_tree_get_first_entry(s->tree);
4843 if (s->selected == 0) {
4844 if (view->focussed)
4845 wstandout(view->window);
4846 s->selected_entry = NULL;
4848 waddstr(view->window, " ..\n"); /* parent directory */
4849 if (s->selected == 0 && view->focussed)
4850 wstandend(view->window);
4851 s->ndisplayed++;
4852 if (--limit <= 0)
4853 return NULL;
4854 n = 1;
4855 } else {
4856 n = 0;
4857 te = s->first_displayed_entry;
4860 nentries = got_object_tree_get_nentries(s->tree);
4861 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4862 char *line = NULL, *id_str = NULL, *link_target = NULL;
4863 const char *modestr = "";
4864 mode_t mode;
4866 te = got_object_tree_get_entry(s->tree, i);
4867 mode = got_tree_entry_get_mode(te);
4869 if (s->show_ids) {
4870 err = got_object_id_str(&id_str,
4871 got_tree_entry_get_id(te));
4872 if (err)
4873 return got_error_from_errno(
4874 "got_object_id_str");
4876 if (got_object_tree_entry_is_submodule(te))
4877 modestr = "$";
4878 else if (S_ISLNK(mode)) {
4879 int i;
4881 err = got_tree_entry_get_symlink_target(&link_target,
4882 te, s->repo);
4883 if (err) {
4884 free(id_str);
4885 return err;
4887 for (i = 0; i < strlen(link_target); i++) {
4888 if (!isprint((unsigned char)link_target[i]))
4889 link_target[i] = '?';
4891 modestr = "@";
4893 else if (S_ISDIR(mode))
4894 modestr = "/";
4895 else if (mode & S_IXUSR)
4896 modestr = "*";
4897 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4898 got_tree_entry_get_name(te), modestr,
4899 link_target ? " -> ": "",
4900 link_target ? link_target : "") == -1) {
4901 free(id_str);
4902 free(link_target);
4903 return got_error_from_errno("asprintf");
4905 free(id_str);
4906 free(link_target);
4907 err = format_line(&wline, &width, line, view->ncols, 0);
4908 if (err) {
4909 free(line);
4910 break;
4912 if (n == s->selected) {
4913 if (view->focussed)
4914 wstandout(view->window);
4915 s->selected_entry = te;
4917 tc = match_color(&s->colors, line);
4918 if (tc)
4919 wattr_on(view->window,
4920 COLOR_PAIR(tc->colorpair), NULL);
4921 waddwstr(view->window, wline);
4922 if (tc)
4923 wattr_off(view->window,
4924 COLOR_PAIR(tc->colorpair), NULL);
4925 if (width < view->ncols - 1)
4926 waddch(view->window, '\n');
4927 if (n == s->selected && view->focussed)
4928 wstandend(view->window);
4929 free(line);
4930 free(wline);
4931 wline = NULL;
4932 n++;
4933 s->ndisplayed++;
4934 s->last_displayed_entry = te;
4935 if (--limit <= 0)
4936 break;
4939 return err;
4942 static void
4943 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4945 struct got_tree_entry *te;
4946 int isroot = s->tree == s->root;
4947 int i = 0;
4949 if (s->first_displayed_entry == NULL)
4950 return;
4952 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4953 while (i++ < maxscroll) {
4954 if (te == NULL) {
4955 if (!isroot)
4956 s->first_displayed_entry = NULL;
4957 break;
4959 s->first_displayed_entry = te;
4960 te = got_tree_entry_get_prev(s->tree, te);
4964 static void
4965 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4967 struct got_tree_entry *next, *last;
4968 int n = 0;
4970 if (s->first_displayed_entry)
4971 next = got_tree_entry_get_next(s->tree,
4972 s->first_displayed_entry);
4973 else
4974 next = got_object_tree_get_first_entry(s->tree);
4976 last = s->last_displayed_entry;
4977 while (next && last && n++ < maxscroll) {
4978 last = got_tree_entry_get_next(s->tree, last);
4979 if (last) {
4980 s->first_displayed_entry = next;
4981 next = got_tree_entry_get_next(s->tree, next);
4986 static const struct got_error *
4987 tree_entry_path(char **path, struct tog_parent_trees *parents,
4988 struct got_tree_entry *te)
4990 const struct got_error *err = NULL;
4991 struct tog_parent_tree *pt;
4992 size_t len = 2; /* for leading slash and NUL */
4994 TAILQ_FOREACH(pt, parents, entry)
4995 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4996 + 1 /* slash */;
4997 if (te)
4998 len += strlen(got_tree_entry_get_name(te));
5000 *path = calloc(1, len);
5001 if (path == NULL)
5002 return got_error_from_errno("calloc");
5004 (*path)[0] = '/';
5005 pt = TAILQ_LAST(parents, tog_parent_trees);
5006 while (pt) {
5007 const char *name = got_tree_entry_get_name(pt->selected_entry);
5008 if (strlcat(*path, name, len) >= len) {
5009 err = got_error(GOT_ERR_NO_SPACE);
5010 goto done;
5012 if (strlcat(*path, "/", len) >= len) {
5013 err = got_error(GOT_ERR_NO_SPACE);
5014 goto done;
5016 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5018 if (te) {
5019 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5020 err = got_error(GOT_ERR_NO_SPACE);
5021 goto done;
5024 done:
5025 if (err) {
5026 free(*path);
5027 *path = NULL;
5029 return err;
5032 static const struct got_error *
5033 blame_tree_entry(struct tog_view **new_view, int begin_x,
5034 struct got_tree_entry *te, struct tog_parent_trees *parents,
5035 struct got_object_id *commit_id, struct got_repository *repo)
5037 const struct got_error *err = NULL;
5038 char *path;
5039 struct tog_view *blame_view;
5041 *new_view = NULL;
5043 err = tree_entry_path(&path, parents, te);
5044 if (err)
5045 return err;
5047 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5048 if (blame_view == NULL) {
5049 err = got_error_from_errno("view_open");
5050 goto done;
5053 err = open_blame_view(blame_view, path, commit_id, repo);
5054 if (err) {
5055 if (err->code == GOT_ERR_CANCELLED)
5056 err = NULL;
5057 view_close(blame_view);
5058 } else
5059 *new_view = blame_view;
5060 done:
5061 free(path);
5062 return err;
5065 static const struct got_error *
5066 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5067 struct tog_tree_view_state *s)
5069 struct tog_view *log_view;
5070 const struct got_error *err = NULL;
5071 char *path;
5073 *new_view = NULL;
5075 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5076 if (log_view == NULL)
5077 return got_error_from_errno("view_open");
5079 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5080 if (err)
5081 return err;
5083 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5084 path, 0);
5085 if (err)
5086 view_close(log_view);
5087 else
5088 *new_view = log_view;
5089 free(path);
5090 return err;
5093 static const struct got_error *
5094 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5095 struct got_object_id *commit_id, const char *head_ref_name,
5096 struct got_repository *repo)
5098 const struct got_error *err = NULL;
5099 char *commit_id_str = NULL;
5100 struct tog_tree_view_state *s = &view->state.tree;
5102 TAILQ_INIT(&s->parents);
5104 err = got_object_id_str(&commit_id_str, commit_id);
5105 if (err != NULL)
5106 goto done;
5108 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5109 err = got_error_from_errno("asprintf");
5110 goto done;
5113 s->root = s->tree = root;
5114 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5115 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5116 s->commit_id = got_object_id_dup(commit_id);
5117 if (s->commit_id == NULL) {
5118 err = got_error_from_errno("got_object_id_dup");
5119 goto done;
5121 if (head_ref_name) {
5122 s->head_ref_name = strdup(head_ref_name);
5123 if (s->head_ref_name == NULL) {
5124 err = got_error_from_errno("strdup");
5125 goto done;
5128 s->repo = repo;
5130 SIMPLEQ_INIT(&s->colors);
5132 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5133 err = add_color(&s->colors, "\\$$",
5134 TOG_COLOR_TREE_SUBMODULE,
5135 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5136 if (err)
5137 goto done;
5138 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5139 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5140 if (err) {
5141 free_colors(&s->colors);
5142 goto done;
5144 err = add_color(&s->colors, "/$",
5145 TOG_COLOR_TREE_DIRECTORY,
5146 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5147 if (err) {
5148 free_colors(&s->colors);
5149 goto done;
5152 err = add_color(&s->colors, "\\*$",
5153 TOG_COLOR_TREE_EXECUTABLE,
5154 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5155 if (err) {
5156 free_colors(&s->colors);
5157 goto done;
5160 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5161 get_color_value("TOG_COLOR_COMMIT"));
5162 if (err) {
5163 free_colors(&s->colors);
5164 goto done;
5168 view->show = show_tree_view;
5169 view->input = input_tree_view;
5170 view->close = close_tree_view;
5171 view->search_start = search_start_tree_view;
5172 view->search_next = search_next_tree_view;
5173 done:
5174 free(commit_id_str);
5175 if (err) {
5176 free(s->tree_label);
5177 s->tree_label = NULL;
5179 return err;
5182 static const struct got_error *
5183 close_tree_view(struct tog_view *view)
5185 struct tog_tree_view_state *s = &view->state.tree;
5187 free_colors(&s->colors);
5188 free(s->tree_label);
5189 s->tree_label = NULL;
5190 free(s->commit_id);
5191 s->commit_id = NULL;
5192 free(s->head_ref_name);
5193 s->head_ref_name = NULL;
5194 while (!TAILQ_EMPTY(&s->parents)) {
5195 struct tog_parent_tree *parent;
5196 parent = TAILQ_FIRST(&s->parents);
5197 TAILQ_REMOVE(&s->parents, parent, entry);
5198 free(parent);
5201 if (s->tree != s->root)
5202 got_object_tree_close(s->tree);
5203 got_object_tree_close(s->root);
5204 return NULL;
5207 static const struct got_error *
5208 search_start_tree_view(struct tog_view *view)
5210 struct tog_tree_view_state *s = &view->state.tree;
5212 s->matched_entry = NULL;
5213 return NULL;
5216 static int
5217 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5219 regmatch_t regmatch;
5221 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5222 0) == 0;
5225 static const struct got_error *
5226 search_next_tree_view(struct tog_view *view)
5228 struct tog_tree_view_state *s = &view->state.tree;
5229 struct got_tree_entry *te = NULL;
5231 if (!view->searching) {
5232 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5233 return NULL;
5236 if (s->matched_entry) {
5237 if (view->searching == TOG_SEARCH_FORWARD) {
5238 if (s->selected_entry)
5239 te = got_tree_entry_get_next(s->tree,
5240 s->selected_entry);
5241 else
5242 te = got_object_tree_get_first_entry(s->tree);
5243 } else {
5244 if (s->selected_entry == NULL)
5245 te = got_object_tree_get_last_entry(s->tree);
5246 else
5247 te = got_tree_entry_get_prev(s->tree,
5248 s->selected_entry);
5250 } else {
5251 if (view->searching == TOG_SEARCH_FORWARD)
5252 te = got_object_tree_get_first_entry(s->tree);
5253 else
5254 te = got_object_tree_get_last_entry(s->tree);
5257 while (1) {
5258 if (te == NULL) {
5259 if (s->matched_entry == NULL) {
5260 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5261 return NULL;
5263 if (view->searching == TOG_SEARCH_FORWARD)
5264 te = got_object_tree_get_first_entry(s->tree);
5265 else
5266 te = got_object_tree_get_last_entry(s->tree);
5269 if (match_tree_entry(te, &view->regex)) {
5270 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5271 s->matched_entry = te;
5272 break;
5275 if (view->searching == TOG_SEARCH_FORWARD)
5276 te = got_tree_entry_get_next(s->tree, te);
5277 else
5278 te = got_tree_entry_get_prev(s->tree, te);
5281 if (s->matched_entry) {
5282 s->first_displayed_entry = s->matched_entry;
5283 s->selected = 0;
5286 return NULL;
5289 static const struct got_error *
5290 show_tree_view(struct tog_view *view)
5292 const struct got_error *err = NULL;
5293 struct tog_tree_view_state *s = &view->state.tree;
5294 char *parent_path;
5296 err = tree_entry_path(&parent_path, &s->parents, NULL);
5297 if (err)
5298 return err;
5300 err = draw_tree_entries(view, parent_path);
5301 free(parent_path);
5303 view_vborder(view);
5304 return err;
5307 static const struct got_error *
5308 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5310 const struct got_error *err = NULL;
5311 struct tog_tree_view_state *s = &view->state.tree;
5312 struct tog_view *log_view, *ref_view;
5313 int begin_x = 0;
5315 switch (ch) {
5316 case 'i':
5317 s->show_ids = !s->show_ids;
5318 break;
5319 case 'l':
5320 if (!s->selected_entry)
5321 break;
5322 if (view_is_parent_view(view))
5323 begin_x = view_split_begin_x(view->begin_x);
5324 err = log_selected_tree_entry(&log_view, begin_x, s);
5325 view->focussed = 0;
5326 log_view->focussed = 1;
5327 if (view_is_parent_view(view)) {
5328 err = view_close_child(view);
5329 if (err)
5330 return err;
5331 view_set_child(view, log_view);
5332 view->focus_child = 1;
5333 } else
5334 *new_view = log_view;
5335 break;
5336 case 'r':
5337 if (view_is_parent_view(view))
5338 begin_x = view_split_begin_x(view->begin_x);
5339 ref_view = view_open(view->nlines, view->ncols,
5340 view->begin_y, begin_x, TOG_VIEW_REF);
5341 if (ref_view == NULL)
5342 return got_error_from_errno("view_open");
5343 err = open_ref_view(ref_view, s->repo);
5344 if (err) {
5345 view_close(ref_view);
5346 return err;
5348 view->focussed = 0;
5349 ref_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, ref_view);
5355 view->focus_child = 1;
5356 } else
5357 *new_view = ref_view;
5358 break;
5359 case 'k':
5360 case KEY_UP:
5361 if (s->selected > 0) {
5362 s->selected--;
5363 break;
5365 tree_scroll_up(s, 1);
5366 break;
5367 case KEY_PPAGE:
5368 case CTRL('b'):
5369 if (s->tree == s->root) {
5370 if (got_object_tree_get_first_entry(s->tree) ==
5371 s->first_displayed_entry)
5372 s->selected = 0;
5373 } else {
5374 if (s->first_displayed_entry == NULL)
5375 s->selected = 0;
5377 tree_scroll_up(s, MAX(0, view->nlines - 3));
5378 break;
5379 case 'j':
5380 case KEY_DOWN:
5381 if (s->selected < s->ndisplayed - 1) {
5382 s->selected++;
5383 break;
5385 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5386 == NULL)
5387 /* can't scroll any further */
5388 break;
5389 tree_scroll_down(s, 1);
5390 break;
5391 case KEY_NPAGE:
5392 case CTRL('f'):
5393 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5394 == NULL) {
5395 /* can't scroll any further; move cursor down */
5396 if (s->selected < s->ndisplayed - 1)
5397 s->selected = s->ndisplayed - 1;
5398 break;
5400 tree_scroll_down(s, view->nlines - 3);
5401 break;
5402 case KEY_ENTER:
5403 case '\r':
5404 case KEY_BACKSPACE:
5405 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5406 struct tog_parent_tree *parent;
5407 /* user selected '..' */
5408 if (s->tree == s->root)
5409 break;
5410 parent = TAILQ_FIRST(&s->parents);
5411 TAILQ_REMOVE(&s->parents, parent,
5412 entry);
5413 got_object_tree_close(s->tree);
5414 s->tree = parent->tree;
5415 s->first_displayed_entry =
5416 parent->first_displayed_entry;
5417 s->selected_entry =
5418 parent->selected_entry;
5419 s->selected = parent->selected;
5420 free(parent);
5421 } else if (S_ISDIR(got_tree_entry_get_mode(
5422 s->selected_entry))) {
5423 struct got_tree_object *subtree;
5424 err = got_object_open_as_tree(&subtree, s->repo,
5425 got_tree_entry_get_id(s->selected_entry));
5426 if (err)
5427 break;
5428 err = tree_view_visit_subtree(s, subtree);
5429 if (err) {
5430 got_object_tree_close(subtree);
5431 break;
5433 } else if (S_ISREG(got_tree_entry_get_mode(
5434 s->selected_entry))) {
5435 struct tog_view *blame_view;
5436 int begin_x = view_is_parent_view(view) ?
5437 view_split_begin_x(view->begin_x) : 0;
5439 err = blame_tree_entry(&blame_view, begin_x,
5440 s->selected_entry, &s->parents,
5441 s->commit_id, s->repo);
5442 if (err)
5443 break;
5444 view->focussed = 0;
5445 blame_view->focussed = 1;
5446 if (view_is_parent_view(view)) {
5447 err = view_close_child(view);
5448 if (err)
5449 return err;
5450 view_set_child(view, blame_view);
5451 view->focus_child = 1;
5452 } else
5453 *new_view = blame_view;
5455 break;
5456 case KEY_RESIZE:
5457 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5458 s->selected = view->nlines - 4;
5459 break;
5460 default:
5461 break;
5464 return err;
5467 __dead static void
5468 usage_tree(void)
5470 endwin();
5471 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5472 getprogname());
5473 exit(1);
5476 static const struct got_error *
5477 cmd_tree(int argc, char *argv[])
5479 const struct got_error *error;
5480 struct got_repository *repo = NULL;
5481 struct got_worktree *worktree = NULL;
5482 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5483 struct got_object_id *commit_id = NULL;
5484 const char *commit_id_arg = NULL;
5485 char *label = NULL;
5486 struct got_commit_object *commit = NULL;
5487 struct got_tree_object *tree = NULL;
5488 struct got_reference *ref = NULL;
5489 const char *head_ref_name = NULL;
5490 int ch;
5491 struct tog_view *view;
5493 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5494 switch (ch) {
5495 case 'c':
5496 commit_id_arg = optarg;
5497 break;
5498 case 'r':
5499 repo_path = realpath(optarg, NULL);
5500 if (repo_path == NULL)
5501 return got_error_from_errno2("realpath",
5502 optarg);
5503 break;
5504 default:
5505 usage_tree();
5506 /* NOTREACHED */
5510 argc -= optind;
5511 argv += optind;
5513 if (argc > 1)
5514 usage_tree();
5516 if (repo_path == NULL) {
5517 cwd = getcwd(NULL, 0);
5518 if (cwd == NULL)
5519 return got_error_from_errno("getcwd");
5520 error = got_worktree_open(&worktree, cwd);
5521 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5522 goto done;
5523 if (worktree)
5524 repo_path =
5525 strdup(got_worktree_get_repo_path(worktree));
5526 else
5527 repo_path = strdup(cwd);
5528 if (repo_path == NULL) {
5529 error = got_error_from_errno("strdup");
5530 goto done;
5534 error = got_repo_open(&repo, repo_path, NULL);
5535 if (error != NULL)
5536 goto done;
5538 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5539 repo, worktree);
5540 if (error)
5541 goto done;
5543 init_curses();
5545 error = apply_unveil(got_repo_get_path(repo), NULL);
5546 if (error)
5547 goto done;
5549 error = tog_load_refs(repo);
5550 if (error)
5551 goto done;
5553 if (commit_id_arg == NULL) {
5554 error = got_repo_match_object_id(&commit_id, &label,
5555 worktree ? got_worktree_get_head_ref_name(worktree) :
5556 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5557 if (error)
5558 goto done;
5559 head_ref_name = label;
5560 } else {
5561 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5562 if (error == NULL)
5563 head_ref_name = got_ref_get_name(ref);
5564 else if (error->code != GOT_ERR_NOT_REF)
5565 goto done;
5566 error = got_repo_match_object_id(&commit_id, NULL,
5567 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5568 if (error)
5569 goto done;
5572 error = got_object_open_as_commit(&commit, repo, commit_id);
5573 if (error)
5574 goto done;
5576 error = got_object_open_as_tree(&tree, repo,
5577 got_object_commit_get_tree_id(commit));
5578 if (error)
5579 goto done;
5581 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5582 if (view == NULL) {
5583 error = got_error_from_errno("view_open");
5584 goto done;
5586 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5587 if (error)
5588 goto done;
5589 if (!got_path_is_root_dir(in_repo_path)) {
5590 error = tree_view_walk_path(&view->state.tree, commit_id,
5591 in_repo_path);
5592 if (error)
5593 goto done;
5596 if (worktree) {
5597 /* Release work tree lock. */
5598 got_worktree_close(worktree);
5599 worktree = NULL;
5601 error = view_loop(view);
5602 done:
5603 free(repo_path);
5604 free(cwd);
5605 free(commit_id);
5606 free(label);
5607 if (ref)
5608 got_ref_close(ref);
5609 if (commit)
5610 got_object_commit_close(commit);
5611 if (tree)
5612 got_object_tree_close(tree);
5613 if (repo)
5614 got_repo_close(repo);
5615 tog_free_refs();
5616 return error;
5619 static const struct got_error *
5620 ref_view_load_refs(struct tog_ref_view_state *s)
5622 struct got_reflist_entry *sre;
5623 struct tog_reflist_entry *re;
5625 s->nrefs = 0;
5626 TAILQ_FOREACH(sre, &tog_refs, entry) {
5627 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5628 continue;
5630 re = malloc(sizeof(*re));
5631 if (re == NULL)
5632 return got_error_from_errno("malloc");
5634 re->ref = got_ref_dup(sre->ref);
5635 if (re->ref == NULL)
5636 return got_error_from_errno("got_ref_dup");
5637 re->idx = s->nrefs++;
5638 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5641 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5642 return NULL;
5645 void
5646 ref_view_free_refs(struct tog_ref_view_state *s)
5648 struct tog_reflist_entry *re;
5650 while (!TAILQ_EMPTY(&s->refs)) {
5651 re = TAILQ_FIRST(&s->refs);
5652 TAILQ_REMOVE(&s->refs, re, entry);
5653 got_ref_close(re->ref);
5654 free(re);
5658 static const struct got_error *
5659 open_ref_view(struct tog_view *view, struct got_repository *repo)
5661 const struct got_error *err = NULL;
5662 struct tog_ref_view_state *s = &view->state.ref;
5664 s->selected_entry = 0;
5665 s->repo = repo;
5667 TAILQ_INIT(&s->refs);
5668 SIMPLEQ_INIT(&s->colors);
5670 err = ref_view_load_refs(s);
5671 if (err)
5672 return err;
5674 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5675 err = add_color(&s->colors, "^refs/heads/",
5676 TOG_COLOR_REFS_HEADS,
5677 get_color_value("TOG_COLOR_REFS_HEADS"));
5678 if (err)
5679 goto done;
5681 err = add_color(&s->colors, "^refs/tags/",
5682 TOG_COLOR_REFS_TAGS,
5683 get_color_value("TOG_COLOR_REFS_TAGS"));
5684 if (err)
5685 goto done;
5687 err = add_color(&s->colors, "^refs/remotes/",
5688 TOG_COLOR_REFS_REMOTES,
5689 get_color_value("TOG_COLOR_REFS_REMOTES"));
5690 if (err)
5691 goto done;
5694 view->show = show_ref_view;
5695 view->input = input_ref_view;
5696 view->close = close_ref_view;
5697 view->search_start = search_start_ref_view;
5698 view->search_next = search_next_ref_view;
5699 done:
5700 if (err)
5701 free_colors(&s->colors);
5702 return err;
5705 static const struct got_error *
5706 close_ref_view(struct tog_view *view)
5708 struct tog_ref_view_state *s = &view->state.ref;
5710 ref_view_free_refs(s);
5711 free_colors(&s->colors);
5713 return NULL;
5716 static const struct got_error *
5717 resolve_reflist_entry(struct got_object_id **commit_id,
5718 struct tog_reflist_entry *re, struct got_repository *repo)
5720 const struct got_error *err = NULL;
5721 struct got_object_id *obj_id;
5722 struct got_tag_object *tag = NULL;
5723 int obj_type;
5725 *commit_id = NULL;
5727 err = got_ref_resolve(&obj_id, repo, re->ref);
5728 if (err)
5729 return err;
5731 err = got_object_get_type(&obj_type, repo, obj_id);
5732 if (err)
5733 goto done;
5735 switch (obj_type) {
5736 case GOT_OBJ_TYPE_COMMIT:
5737 *commit_id = obj_id;
5738 break;
5739 case GOT_OBJ_TYPE_TAG:
5740 err = got_object_open_as_tag(&tag, repo, obj_id);
5741 if (err)
5742 goto done;
5743 free(obj_id);
5744 err = got_object_get_type(&obj_type, repo,
5745 got_object_tag_get_object_id(tag));
5746 if (err)
5747 goto done;
5748 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5749 err = got_error(GOT_ERR_OBJ_TYPE);
5750 goto done;
5752 *commit_id = got_object_id_dup(
5753 got_object_tag_get_object_id(tag));
5754 if (*commit_id == NULL) {
5755 err = got_error_from_errno("got_object_id_dup");
5756 goto done;
5758 break;
5759 default:
5760 err = got_error(GOT_ERR_OBJ_TYPE);
5761 break;
5764 done:
5765 if (tag)
5766 got_object_tag_close(tag);
5767 if (err) {
5768 free(*commit_id);
5769 *commit_id = NULL;
5771 return err;
5774 static const struct got_error *
5775 log_ref_entry(struct tog_view **new_view, int begin_x,
5776 struct tog_reflist_entry *re, struct got_repository *repo)
5778 struct tog_view *log_view;
5779 const struct got_error *err = NULL;
5780 struct got_object_id *commit_id = NULL;
5782 *new_view = NULL;
5784 err = resolve_reflist_entry(&commit_id, re, repo);
5785 if (err) {
5786 if (err->code != GOT_ERR_OBJ_TYPE)
5787 return err;
5788 else
5789 return NULL;
5792 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5793 if (log_view == NULL) {
5794 err = got_error_from_errno("view_open");
5795 goto done;
5798 err = open_log_view(log_view, commit_id, repo,
5799 got_ref_get_name(re->ref), "", 0);
5800 done:
5801 if (err)
5802 view_close(log_view);
5803 else
5804 *new_view = log_view;
5805 free(commit_id);
5806 return err;
5809 static void
5810 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5812 struct tog_reflist_entry *re;
5813 int i = 0;
5815 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5816 return;
5818 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5819 while (i++ < maxscroll) {
5820 if (re == NULL)
5821 break;
5822 s->first_displayed_entry = re;
5823 re = TAILQ_PREV(re, tog_reflist_head, entry);
5827 static void
5828 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5830 struct tog_reflist_entry *next, *last;
5831 int n = 0;
5833 if (s->first_displayed_entry)
5834 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5835 else
5836 next = TAILQ_FIRST(&s->refs);
5838 last = s->last_displayed_entry;
5839 while (next && last && n++ < maxscroll) {
5840 last = TAILQ_NEXT(last, entry);
5841 if (last) {
5842 s->first_displayed_entry = next;
5843 next = TAILQ_NEXT(next, entry);
5848 static const struct got_error *
5849 search_start_ref_view(struct tog_view *view)
5851 struct tog_ref_view_state *s = &view->state.ref;
5853 s->matched_entry = NULL;
5854 return NULL;
5857 static int
5858 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5860 regmatch_t regmatch;
5862 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5863 0) == 0;
5866 static const struct got_error *
5867 search_next_ref_view(struct tog_view *view)
5869 struct tog_ref_view_state *s = &view->state.ref;
5870 struct tog_reflist_entry *re = NULL;
5872 if (!view->searching) {
5873 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5874 return NULL;
5877 if (s->matched_entry) {
5878 if (view->searching == TOG_SEARCH_FORWARD) {
5879 if (s->selected_entry)
5880 re = TAILQ_NEXT(s->selected_entry, entry);
5881 else
5882 re = TAILQ_PREV(s->selected_entry,
5883 tog_reflist_head, entry);
5884 } else {
5885 if (s->selected_entry == NULL)
5886 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5887 else
5888 re = TAILQ_PREV(s->selected_entry,
5889 tog_reflist_head, entry);
5891 } else {
5892 if (view->searching == TOG_SEARCH_FORWARD)
5893 re = TAILQ_FIRST(&s->refs);
5894 else
5895 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5898 while (1) {
5899 if (re == NULL) {
5900 if (s->matched_entry == NULL) {
5901 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5902 return NULL;
5904 if (view->searching == TOG_SEARCH_FORWARD)
5905 re = TAILQ_FIRST(&s->refs);
5906 else
5907 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5910 if (match_reflist_entry(re, &view->regex)) {
5911 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5912 s->matched_entry = re;
5913 break;
5916 if (view->searching == TOG_SEARCH_FORWARD)
5917 re = TAILQ_NEXT(re, entry);
5918 else
5919 re = TAILQ_PREV(re, tog_reflist_head, entry);
5922 if (s->matched_entry) {
5923 s->first_displayed_entry = s->matched_entry;
5924 s->selected = 0;
5927 return NULL;
5930 static const struct got_error *
5931 show_ref_view(struct tog_view *view)
5933 const struct got_error *err = NULL;
5934 struct tog_ref_view_state *s = &view->state.ref;
5935 struct tog_reflist_entry *re;
5936 char *line = NULL;
5937 wchar_t *wline;
5938 struct tog_color *tc;
5939 int width, n;
5940 int limit = view->nlines;
5942 werase(view->window);
5944 s->ndisplayed = 0;
5946 if (limit == 0)
5947 return NULL;
5949 re = s->first_displayed_entry;
5951 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5952 s->nrefs) == -1)
5953 return got_error_from_errno("asprintf");
5955 err = format_line(&wline, &width, line, view->ncols, 0);
5956 if (err) {
5957 free(line);
5958 return err;
5960 if (view_needs_focus_indication(view))
5961 wstandout(view->window);
5962 waddwstr(view->window, wline);
5963 if (view_needs_focus_indication(view))
5964 wstandend(view->window);
5965 free(wline);
5966 wline = NULL;
5967 free(line);
5968 line = NULL;
5969 if (width < view->ncols - 1)
5970 waddch(view->window, '\n');
5971 if (--limit <= 0)
5972 return NULL;
5974 n = 0;
5975 while (re && limit > 0) {
5976 char *line = NULL;
5978 if (got_ref_is_symbolic(re->ref)) {
5979 if (asprintf(&line, "%s -> %s",
5980 got_ref_get_name(re->ref),
5981 got_ref_get_symref_target(re->ref)) == -1)
5982 return got_error_from_errno("asprintf");
5983 } else if (s->show_ids) {
5984 struct got_object_id *id;
5985 char *id_str;
5986 err = got_ref_resolve(&id, s->repo, re->ref);
5987 if (err)
5988 return err;
5989 err = got_object_id_str(&id_str, id);
5990 if (err) {
5991 free(id);
5992 return err;
5994 if (asprintf(&line, "%s: %s",
5995 got_ref_get_name(re->ref), id_str) == -1) {
5996 err = got_error_from_errno("asprintf");
5997 free(id);
5998 free(id_str);
5999 return err;
6001 free(id);
6002 free(id_str);
6003 } else {
6004 line = strdup(got_ref_get_name(re->ref));
6005 if (line == NULL)
6006 return got_error_from_errno("strdup");
6009 err = format_line(&wline, &width, line, view->ncols, 0);
6010 if (err) {
6011 free(line);
6012 return err;
6014 if (n == s->selected) {
6015 if (view->focussed)
6016 wstandout(view->window);
6017 s->selected_entry = re;
6019 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6020 if (tc)
6021 wattr_on(view->window,
6022 COLOR_PAIR(tc->colorpair), NULL);
6023 waddwstr(view->window, wline);
6024 if (tc)
6025 wattr_off(view->window,
6026 COLOR_PAIR(tc->colorpair), NULL);
6027 if (width < view->ncols - 1)
6028 waddch(view->window, '\n');
6029 if (n == s->selected && view->focussed)
6030 wstandend(view->window);
6031 free(line);
6032 free(wline);
6033 wline = NULL;
6034 n++;
6035 s->ndisplayed++;
6036 s->last_displayed_entry = re;
6038 limit--;
6039 re = TAILQ_NEXT(re, entry);
6042 view_vborder(view);
6043 return err;
6046 static const struct got_error *
6047 browse_ref_tree(struct tog_view **new_view, int begin_x,
6048 struct tog_reflist_entry *re, struct got_repository *repo)
6050 const struct got_error *err = NULL;
6051 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6052 struct got_tree_object *tree = NULL;
6053 struct tog_view *tree_view;
6055 *new_view = NULL;
6057 err = resolve_reflist_entry(&commit_id, re, repo);
6058 if (err) {
6059 if (err->code != GOT_ERR_OBJ_TYPE)
6060 return err;
6061 else
6062 return NULL;
6065 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6066 if (err)
6067 goto done;
6069 err = got_object_open_as_tree(&tree, repo, tree_id);
6070 if (err)
6071 goto done;
6073 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6074 if (tree_view == NULL) {
6075 err = got_error_from_errno("view_open");
6076 goto done;
6079 err = open_tree_view(tree_view, tree, commit_id,
6080 got_ref_get_name(re->ref), repo);
6081 if (err)
6082 goto done;
6084 *new_view = tree_view;
6085 done:
6086 free(commit_id);
6087 free(tree_id);
6088 if (err) {
6089 if (tree)
6090 got_object_tree_close(tree);
6092 return err;
6094 static const struct got_error *
6095 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6097 const struct got_error *err = NULL;
6098 struct tog_ref_view_state *s = &view->state.ref;
6099 struct tog_view *log_view, *tree_view;
6100 int begin_x = 0;
6102 switch (ch) {
6103 case 'i':
6104 s->show_ids = !s->show_ids;
6105 break;
6106 case KEY_ENTER:
6107 case '\r':
6108 if (!s->selected_entry)
6109 break;
6110 if (view_is_parent_view(view))
6111 begin_x = view_split_begin_x(view->begin_x);
6112 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6113 s->repo);
6114 view->focussed = 0;
6115 log_view->focussed = 1;
6116 if (view_is_parent_view(view)) {
6117 err = view_close_child(view);
6118 if (err)
6119 return err;
6120 view_set_child(view, log_view);
6121 view->focus_child = 1;
6122 } else
6123 *new_view = log_view;
6124 break;
6125 case 't':
6126 if (!s->selected_entry)
6127 break;
6128 if (view_is_parent_view(view))
6129 begin_x = view_split_begin_x(view->begin_x);
6130 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6131 s->repo);
6132 if (err || tree_view == NULL)
6133 break;
6134 view->focussed = 0;
6135 tree_view->focussed = 1;
6136 if (view_is_parent_view(view)) {
6137 err = view_close_child(view);
6138 if (err)
6139 return err;
6140 view_set_child(view, tree_view);
6141 view->focus_child = 1;
6142 } else
6143 *new_view = tree_view;
6144 break;
6145 case 'k':
6146 case KEY_UP:
6147 if (s->selected > 0) {
6148 s->selected--;
6149 break;
6151 ref_scroll_up(s, 1);
6152 break;
6153 case KEY_PPAGE:
6154 case CTRL('b'):
6155 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6156 s->selected = 0;
6157 ref_scroll_up(s, MAX(0, view->nlines - 1));
6158 break;
6159 case 'j':
6160 case KEY_DOWN:
6161 if (s->selected < s->ndisplayed - 1) {
6162 s->selected++;
6163 break;
6165 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6166 /* can't scroll any further */
6167 break;
6168 ref_scroll_down(s, 1);
6169 break;
6170 case KEY_NPAGE:
6171 case CTRL('f'):
6172 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6173 /* can't scroll any further; move cursor down */
6174 if (s->selected < s->ndisplayed - 1)
6175 s->selected = s->ndisplayed - 1;
6176 break;
6178 ref_scroll_down(s, view->nlines - 1);
6179 break;
6180 case CTRL('l'):
6181 tog_free_refs();
6182 err = tog_load_refs(s->repo);
6183 if (err)
6184 break;
6185 ref_view_free_refs(s);
6186 err = ref_view_load_refs(s);
6187 break;
6188 case KEY_RESIZE:
6189 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6190 s->selected = view->nlines - 2;
6191 break;
6192 default:
6193 break;
6196 return err;
6199 __dead static void
6200 usage_ref(void)
6202 endwin();
6203 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6204 getprogname());
6205 exit(1);
6208 static const struct got_error *
6209 cmd_ref(int argc, char *argv[])
6211 const struct got_error *error;
6212 struct got_repository *repo = NULL;
6213 struct got_worktree *worktree = NULL;
6214 char *cwd = NULL, *repo_path = NULL;
6215 int ch;
6216 struct tog_view *view;
6218 while ((ch = getopt(argc, argv, "r:")) != -1) {
6219 switch (ch) {
6220 case 'r':
6221 repo_path = realpath(optarg, NULL);
6222 if (repo_path == NULL)
6223 return got_error_from_errno2("realpath",
6224 optarg);
6225 break;
6226 default:
6227 usage_ref();
6228 /* NOTREACHED */
6232 argc -= optind;
6233 argv += optind;
6235 if (argc > 1)
6236 usage_ref();
6238 if (repo_path == NULL) {
6239 cwd = getcwd(NULL, 0);
6240 if (cwd == NULL)
6241 return got_error_from_errno("getcwd");
6242 error = got_worktree_open(&worktree, cwd);
6243 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6244 goto done;
6245 if (worktree)
6246 repo_path =
6247 strdup(got_worktree_get_repo_path(worktree));
6248 else
6249 repo_path = strdup(cwd);
6250 if (repo_path == NULL) {
6251 error = got_error_from_errno("strdup");
6252 goto done;
6256 error = got_repo_open(&repo, repo_path, NULL);
6257 if (error != NULL)
6258 goto done;
6260 init_curses();
6262 error = apply_unveil(got_repo_get_path(repo), NULL);
6263 if (error)
6264 goto done;
6266 error = tog_load_refs(repo);
6267 if (error)
6268 goto done;
6270 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6271 if (view == NULL) {
6272 error = got_error_from_errno("view_open");
6273 goto done;
6276 error = open_ref_view(view, repo);
6277 if (error)
6278 goto done;
6280 if (worktree) {
6281 /* Release work tree lock. */
6282 got_worktree_close(worktree);
6283 worktree = NULL;
6285 error = view_loop(view);
6286 done:
6287 free(repo_path);
6288 free(cwd);
6289 if (repo)
6290 got_repo_close(repo);
6291 tog_free_refs();
6292 return error;
6295 static void
6296 list_commands(FILE *fp)
6298 size_t i;
6300 fprintf(fp, "commands:");
6301 for (i = 0; i < nitems(tog_commands); i++) {
6302 struct tog_cmd *cmd = &tog_commands[i];
6303 fprintf(fp, " %s", cmd->name);
6305 fputc('\n', fp);
6308 __dead static void
6309 usage(int hflag, int status)
6311 FILE *fp = (status == 0) ? stdout : stderr;
6313 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6314 getprogname());
6315 if (hflag) {
6316 fprintf(fp, "lazy usage: %s path\n", getprogname());
6317 list_commands(fp);
6319 exit(status);
6322 static char **
6323 make_argv(int argc, ...)
6325 va_list ap;
6326 char **argv;
6327 int i;
6329 va_start(ap, argc);
6331 argv = calloc(argc, sizeof(char *));
6332 if (argv == NULL)
6333 err(1, "calloc");
6334 for (i = 0; i < argc; i++) {
6335 argv[i] = strdup(va_arg(ap, char *));
6336 if (argv[i] == NULL)
6337 err(1, "strdup");
6340 va_end(ap);
6341 return argv;
6345 * Try to convert 'tog path' into a 'tog log path' command.
6346 * The user could simply have mistyped the command rather than knowingly
6347 * provided a path. So check whether argv[0] can in fact be resolved
6348 * to a path in the HEAD commit and print a special error if not.
6349 * This hack is for mpi@ <3
6351 static const struct got_error *
6352 tog_log_with_path(int argc, char *argv[])
6354 const struct got_error *error = NULL;
6355 struct tog_cmd *cmd = NULL;
6356 struct got_repository *repo = NULL;
6357 struct got_worktree *worktree = NULL;
6358 struct got_object_id *commit_id = NULL, *id = NULL;
6359 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6360 char *commit_id_str = NULL, **cmd_argv = NULL;
6362 cwd = getcwd(NULL, 0);
6363 if (cwd == NULL)
6364 return got_error_from_errno("getcwd");
6366 error = got_worktree_open(&worktree, cwd);
6367 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6368 goto done;
6370 if (worktree)
6371 repo_path = strdup(got_worktree_get_repo_path(worktree));
6372 else
6373 repo_path = strdup(cwd);
6374 if (repo_path == NULL) {
6375 error = got_error_from_errno("strdup");
6376 goto done;
6379 error = got_repo_open(&repo, repo_path, NULL);
6380 if (error != NULL)
6381 goto done;
6383 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6384 repo, worktree);
6385 if (error)
6386 goto done;
6388 error = tog_load_refs(repo);
6389 if (error)
6390 goto done;
6391 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6392 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6393 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6394 if (error)
6395 goto done;
6397 if (worktree) {
6398 got_worktree_close(worktree);
6399 worktree = NULL;
6402 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6403 if (error) {
6404 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6405 goto done;
6406 fprintf(stderr, "%s: '%s' is no known command or path\n",
6407 getprogname(), argv[0]);
6408 usage(1, 1);
6409 /* not reached */
6412 got_repo_close(repo);
6413 repo = NULL;
6415 error = got_object_id_str(&commit_id_str, commit_id);
6416 if (error)
6417 goto done;
6419 cmd = &tog_commands[0]; /* log */
6420 argc = 4;
6421 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6422 error = cmd->cmd_main(argc, cmd_argv);
6423 done:
6424 if (repo)
6425 got_repo_close(repo);
6426 if (worktree)
6427 got_worktree_close(worktree);
6428 free(id);
6429 free(commit_id_str);
6430 free(commit_id);
6431 free(cwd);
6432 free(repo_path);
6433 free(in_repo_path);
6434 if (cmd_argv) {
6435 int i;
6436 for (i = 0; i < argc; i++)
6437 free(cmd_argv[i]);
6438 free(cmd_argv);
6440 tog_free_refs();
6441 return error;
6444 int
6445 main(int argc, char *argv[])
6447 const struct got_error *error = NULL;
6448 struct tog_cmd *cmd = NULL;
6449 int ch, hflag = 0, Vflag = 0;
6450 char **cmd_argv = NULL;
6451 static struct option longopts[] = {
6452 { "version", no_argument, NULL, 'V' },
6453 { NULL, 0, NULL, 0}
6456 setlocale(LC_CTYPE, "");
6458 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6459 switch (ch) {
6460 case 'h':
6461 hflag = 1;
6462 break;
6463 case 'V':
6464 Vflag = 1;
6465 break;
6466 default:
6467 usage(hflag, 1);
6468 /* NOTREACHED */
6472 argc -= optind;
6473 argv += optind;
6474 optind = 1;
6475 optreset = 1;
6477 if (Vflag) {
6478 got_version_print_str();
6479 return 0;
6482 #ifndef PROFILE
6483 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6484 NULL) == -1)
6485 err(1, "pledge");
6486 #endif
6488 if (argc == 0) {
6489 if (hflag)
6490 usage(hflag, 0);
6491 /* Build an argument vector which runs a default command. */
6492 cmd = &tog_commands[0];
6493 argc = 1;
6494 cmd_argv = make_argv(argc, cmd->name);
6495 } else {
6496 size_t i;
6498 /* Did the user specify a command? */
6499 for (i = 0; i < nitems(tog_commands); i++) {
6500 if (strncmp(tog_commands[i].name, argv[0],
6501 strlen(argv[0])) == 0) {
6502 cmd = &tog_commands[i];
6503 break;
6508 if (cmd == NULL) {
6509 if (argc != 1)
6510 usage(0, 1);
6511 /* No command specified; try log with a path */
6512 error = tog_log_with_path(argc, argv);
6513 } else {
6514 if (hflag)
6515 cmd->cmd_usage();
6516 else
6517 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6520 endwin();
6521 putchar('\n');
6522 if (cmd_argv) {
6523 int i;
6524 for (i = 0; i < argc; i++)
6525 free(cmd_argv[i]);
6526 free(cmd_argv);
6529 if (error && error->code != GOT_ERR_CANCELLED)
6530 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6531 return 0;