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) != 0) {
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) != 0 && 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 || blame->nlines == 0)
4241 goto done;
4243 /* Don't include \n at EOF in the blame line count. */
4244 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4245 blame->nlines--;
4247 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4248 if (blame->lines == NULL) {
4249 err = got_error_from_errno("calloc");
4250 goto done;
4253 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4254 if (err)
4255 goto done;
4257 blame->cb_args.view = view;
4258 blame->cb_args.lines = blame->lines;
4259 blame->cb_args.nlines = blame->nlines;
4260 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4261 if (blame->cb_args.commit_id == NULL) {
4262 err = got_error_from_errno("got_object_id_dup");
4263 goto done;
4265 blame->cb_args.quit = &s->done;
4267 blame->thread_args.path = s->path;
4268 blame->thread_args.repo = thread_repo;
4269 blame->thread_args.cb_args = &blame->cb_args;
4270 blame->thread_args.complete = &s->blame_complete;
4271 blame->thread_args.cancel_cb = cancel_blame_view;
4272 blame->thread_args.cancel_arg = &s->done;
4273 s->blame_complete = 0;
4275 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4276 s->first_displayed_line = 1;
4277 s->last_displayed_line = view->nlines;
4278 s->selected_line = 1;
4281 done:
4282 if (blob)
4283 got_object_blob_close(blob);
4284 free(obj_id);
4285 if (err)
4286 stop_blame(blame);
4287 return err;
4290 static const struct got_error *
4291 open_blame_view(struct tog_view *view, char *path,
4292 struct got_object_id *commit_id, struct got_repository *repo)
4294 const struct got_error *err = NULL;
4295 struct tog_blame_view_state *s = &view->state.blame;
4297 SIMPLEQ_INIT(&s->blamed_commits);
4299 s->path = strdup(path);
4300 if (s->path == NULL)
4301 return got_error_from_errno("strdup");
4303 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4304 if (err) {
4305 free(s->path);
4306 return err;
4309 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4310 s->first_displayed_line = 1;
4311 s->last_displayed_line = view->nlines;
4312 s->selected_line = 1;
4313 s->blame_complete = 0;
4314 s->repo = repo;
4315 s->commit_id = commit_id;
4316 memset(&s->blame, 0, sizeof(s->blame));
4318 SIMPLEQ_INIT(&s->colors);
4319 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4320 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4321 get_color_value("TOG_COLOR_COMMIT"));
4322 if (err)
4323 return err;
4326 view->show = show_blame_view;
4327 view->input = input_blame_view;
4328 view->close = close_blame_view;
4329 view->search_start = search_start_blame_view;
4330 view->search_next = search_next_blame_view;
4332 return run_blame(view);
4335 static const struct got_error *
4336 close_blame_view(struct tog_view *view)
4338 const struct got_error *err = NULL;
4339 struct tog_blame_view_state *s = &view->state.blame;
4341 if (s->blame.thread)
4342 err = stop_blame(&s->blame);
4344 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4345 struct got_object_qid *blamed_commit;
4346 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4347 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4348 got_object_qid_free(blamed_commit);
4351 free(s->path);
4352 free_colors(&s->colors);
4354 return err;
4357 static const struct got_error *
4358 search_start_blame_view(struct tog_view *view)
4360 struct tog_blame_view_state *s = &view->state.blame;
4362 s->matched_line = 0;
4363 return NULL;
4366 static const struct got_error *
4367 search_next_blame_view(struct tog_view *view)
4369 struct tog_blame_view_state *s = &view->state.blame;
4370 int lineno;
4371 char *line = NULL;
4372 size_t linesize = 0;
4373 ssize_t linelen;
4375 if (!view->searching) {
4376 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4377 return NULL;
4380 if (s->matched_line) {
4381 if (view->searching == TOG_SEARCH_FORWARD)
4382 lineno = s->matched_line + 1;
4383 else
4384 lineno = s->matched_line - 1;
4385 } else {
4386 if (view->searching == TOG_SEARCH_FORWARD)
4387 lineno = 1;
4388 else
4389 lineno = s->blame.nlines;
4392 while (1) {
4393 off_t offset;
4395 if (lineno <= 0 || lineno > s->blame.nlines) {
4396 if (s->matched_line == 0) {
4397 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4398 break;
4401 if (view->searching == TOG_SEARCH_FORWARD)
4402 lineno = 1;
4403 else
4404 lineno = s->blame.nlines;
4407 offset = s->blame.line_offsets[lineno - 1];
4408 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4409 free(line);
4410 return got_error_from_errno("fseeko");
4412 linelen = getline(&line, &linesize, s->blame.f);
4413 if (linelen != -1 &&
4414 match_line(line, &view->regex, 1, &view->regmatch)) {
4415 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4416 s->matched_line = lineno;
4417 break;
4419 if (view->searching == TOG_SEARCH_FORWARD)
4420 lineno++;
4421 else
4422 lineno--;
4424 free(line);
4426 if (s->matched_line) {
4427 s->first_displayed_line = s->matched_line;
4428 s->selected_line = 1;
4431 return NULL;
4434 static const struct got_error *
4435 show_blame_view(struct tog_view *view)
4437 const struct got_error *err = NULL;
4438 struct tog_blame_view_state *s = &view->state.blame;
4439 int errcode;
4441 if (s->blame.thread == NULL) {
4442 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4443 &s->blame.thread_args);
4444 if (errcode)
4445 return got_error_set_errno(errcode, "pthread_create");
4447 halfdelay(1); /* fast refresh while annotating */
4450 if (s->blame_complete)
4451 halfdelay(10); /* disable fast refresh */
4453 err = draw_blame(view);
4455 view_vborder(view);
4456 return err;
4459 static const struct got_error *
4460 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4462 const struct got_error *err = NULL, *thread_err = NULL;
4463 struct tog_view *diff_view;
4464 struct tog_blame_view_state *s = &view->state.blame;
4465 int begin_x = 0;
4467 switch (ch) {
4468 case 'q':
4469 s->done = 1;
4470 break;
4471 case 'k':
4472 case KEY_UP:
4473 if (s->selected_line > 1)
4474 s->selected_line--;
4475 else if (s->selected_line == 1 &&
4476 s->first_displayed_line > 1)
4477 s->first_displayed_line--;
4478 break;
4479 case KEY_PPAGE:
4480 case CTRL('b'):
4481 if (s->first_displayed_line == 1) {
4482 s->selected_line = 1;
4483 break;
4485 if (s->first_displayed_line > view->nlines - 2)
4486 s->first_displayed_line -=
4487 (view->nlines - 2);
4488 else
4489 s->first_displayed_line = 1;
4490 break;
4491 case 'j':
4492 case KEY_DOWN:
4493 if (s->selected_line < view->nlines - 2 &&
4494 s->first_displayed_line +
4495 s->selected_line <= s->blame.nlines)
4496 s->selected_line++;
4497 else if (s->last_displayed_line <
4498 s->blame.nlines)
4499 s->first_displayed_line++;
4500 break;
4501 case 'b':
4502 case 'p': {
4503 struct got_object_id *id = NULL;
4504 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4505 s->first_displayed_line, s->selected_line);
4506 if (id == NULL)
4507 break;
4508 if (ch == 'p') {
4509 struct got_commit_object *commit;
4510 struct got_object_qid *pid;
4511 struct got_object_id *blob_id = NULL;
4512 int obj_type;
4513 err = got_object_open_as_commit(&commit,
4514 s->repo, id);
4515 if (err)
4516 break;
4517 pid = SIMPLEQ_FIRST(
4518 got_object_commit_get_parent_ids(commit));
4519 if (pid == NULL) {
4520 got_object_commit_close(commit);
4521 break;
4523 /* Check if path history ends here. */
4524 err = got_object_id_by_path(&blob_id, s->repo,
4525 pid->id, s->path);
4526 if (err) {
4527 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4528 err = NULL;
4529 got_object_commit_close(commit);
4530 break;
4532 err = got_object_get_type(&obj_type, s->repo,
4533 blob_id);
4534 free(blob_id);
4535 /* Can't blame non-blob type objects. */
4536 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4537 got_object_commit_close(commit);
4538 break;
4540 err = got_object_qid_alloc(&s->blamed_commit,
4541 pid->id);
4542 got_object_commit_close(commit);
4543 } else {
4544 if (got_object_id_cmp(id,
4545 s->blamed_commit->id) == 0)
4546 break;
4547 err = got_object_qid_alloc(&s->blamed_commit,
4548 id);
4550 if (err)
4551 break;
4552 s->done = 1;
4553 thread_err = stop_blame(&s->blame);
4554 s->done = 0;
4555 if (thread_err)
4556 break;
4557 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4558 s->blamed_commit, entry);
4559 err = run_blame(view);
4560 if (err)
4561 break;
4562 break;
4564 case 'B': {
4565 struct got_object_qid *first;
4566 first = SIMPLEQ_FIRST(&s->blamed_commits);
4567 if (!got_object_id_cmp(first->id, s->commit_id))
4568 break;
4569 s->done = 1;
4570 thread_err = stop_blame(&s->blame);
4571 s->done = 0;
4572 if (thread_err)
4573 break;
4574 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4575 got_object_qid_free(s->blamed_commit);
4576 s->blamed_commit =
4577 SIMPLEQ_FIRST(&s->blamed_commits);
4578 err = run_blame(view);
4579 if (err)
4580 break;
4581 break;
4583 case KEY_ENTER:
4584 case '\r': {
4585 struct got_object_id *id = NULL;
4586 struct got_object_qid *pid;
4587 struct got_commit_object *commit = NULL;
4588 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4589 s->first_displayed_line, s->selected_line);
4590 if (id == NULL)
4591 break;
4592 err = got_object_open_as_commit(&commit, s->repo, id);
4593 if (err)
4594 break;
4595 pid = SIMPLEQ_FIRST(
4596 got_object_commit_get_parent_ids(commit));
4597 if (view_is_parent_view(view))
4598 begin_x = view_split_begin_x(view->begin_x);
4599 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4600 if (diff_view == NULL) {
4601 got_object_commit_close(commit);
4602 err = got_error_from_errno("view_open");
4603 break;
4605 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4606 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4607 got_object_commit_close(commit);
4608 if (err) {
4609 view_close(diff_view);
4610 break;
4612 view->focussed = 0;
4613 diff_view->focussed = 1;
4614 if (view_is_parent_view(view)) {
4615 err = view_close_child(view);
4616 if (err)
4617 break;
4618 view_set_child(view, diff_view);
4619 view->focus_child = 1;
4620 } else
4621 *new_view = diff_view;
4622 if (err)
4623 break;
4624 break;
4626 case KEY_NPAGE:
4627 case CTRL('f'):
4628 case ' ':
4629 if (s->last_displayed_line >= s->blame.nlines &&
4630 s->selected_line >= MIN(s->blame.nlines,
4631 view->nlines - 2)) {
4632 break;
4634 if (s->last_displayed_line >= s->blame.nlines &&
4635 s->selected_line < view->nlines - 2) {
4636 s->selected_line = MIN(s->blame.nlines,
4637 view->nlines - 2);
4638 break;
4640 if (s->last_displayed_line + view->nlines - 2
4641 <= s->blame.nlines)
4642 s->first_displayed_line +=
4643 view->nlines - 2;
4644 else
4645 s->first_displayed_line =
4646 s->blame.nlines -
4647 (view->nlines - 3);
4648 break;
4649 case KEY_RESIZE:
4650 if (s->selected_line > view->nlines - 2) {
4651 s->selected_line = MIN(s->blame.nlines,
4652 view->nlines - 2);
4654 break;
4655 default:
4656 break;
4658 return thread_err ? thread_err : err;
4661 static const struct got_error *
4662 cmd_blame(int argc, char *argv[])
4664 const struct got_error *error;
4665 struct got_repository *repo = NULL;
4666 struct got_worktree *worktree = NULL;
4667 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4668 char *link_target = NULL;
4669 struct got_object_id *commit_id = NULL;
4670 char *commit_id_str = NULL;
4671 int ch;
4672 struct tog_view *view;
4674 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4675 switch (ch) {
4676 case 'c':
4677 commit_id_str = optarg;
4678 break;
4679 case 'r':
4680 repo_path = realpath(optarg, NULL);
4681 if (repo_path == NULL)
4682 return got_error_from_errno2("realpath",
4683 optarg);
4684 break;
4685 default:
4686 usage_blame();
4687 /* NOTREACHED */
4691 argc -= optind;
4692 argv += optind;
4694 if (argc != 1)
4695 usage_blame();
4697 if (repo_path == NULL) {
4698 cwd = getcwd(NULL, 0);
4699 if (cwd == NULL)
4700 return got_error_from_errno("getcwd");
4701 error = got_worktree_open(&worktree, cwd);
4702 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4703 goto done;
4704 if (worktree)
4705 repo_path =
4706 strdup(got_worktree_get_repo_path(worktree));
4707 else
4708 repo_path = strdup(cwd);
4709 if (repo_path == NULL) {
4710 error = got_error_from_errno("strdup");
4711 goto done;
4715 error = got_repo_open(&repo, repo_path, NULL);
4716 if (error != NULL)
4717 goto done;
4719 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4720 worktree);
4721 if (error)
4722 goto done;
4724 init_curses();
4726 error = apply_unveil(got_repo_get_path(repo), NULL);
4727 if (error)
4728 goto done;
4730 error = tog_load_refs(repo);
4731 if (error)
4732 goto done;
4734 if (commit_id_str == NULL) {
4735 struct got_reference *head_ref;
4736 error = got_ref_open(&head_ref, repo, worktree ?
4737 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4738 if (error != NULL)
4739 goto done;
4740 error = got_ref_resolve(&commit_id, repo, head_ref);
4741 got_ref_close(head_ref);
4742 } else {
4743 error = got_repo_match_object_id(&commit_id, NULL,
4744 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4746 if (error != NULL)
4747 goto done;
4749 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4750 if (view == NULL) {
4751 error = got_error_from_errno("view_open");
4752 goto done;
4755 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4756 commit_id, repo);
4757 if (error)
4758 goto done;
4760 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4761 commit_id, repo);
4762 if (error)
4763 goto done;
4764 if (worktree) {
4765 /* Release work tree lock. */
4766 got_worktree_close(worktree);
4767 worktree = NULL;
4769 error = view_loop(view);
4770 done:
4771 free(repo_path);
4772 free(in_repo_path);
4773 free(link_target);
4774 free(cwd);
4775 free(commit_id);
4776 if (worktree)
4777 got_worktree_close(worktree);
4778 if (repo)
4779 got_repo_close(repo);
4780 tog_free_refs();
4781 return error;
4784 static const struct got_error *
4785 draw_tree_entries(struct tog_view *view, const char *parent_path)
4787 struct tog_tree_view_state *s = &view->state.tree;
4788 const struct got_error *err = NULL;
4789 struct got_tree_entry *te;
4790 wchar_t *wline;
4791 struct tog_color *tc;
4792 int width, n, i, nentries;
4793 int limit = view->nlines;
4795 s->ndisplayed = 0;
4797 werase(view->window);
4799 if (limit == 0)
4800 return NULL;
4802 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4803 if (err)
4804 return err;
4805 if (view_needs_focus_indication(view))
4806 wstandout(view->window);
4807 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4808 if (tc)
4809 wattr_on(view->window,
4810 COLOR_PAIR(tc->colorpair), NULL);
4811 waddwstr(view->window, wline);
4812 if (tc)
4813 wattr_off(view->window,
4814 COLOR_PAIR(tc->colorpair), NULL);
4815 if (view_needs_focus_indication(view))
4816 wstandend(view->window);
4817 free(wline);
4818 wline = NULL;
4819 if (width < view->ncols - 1)
4820 waddch(view->window, '\n');
4821 if (--limit <= 0)
4822 return NULL;
4823 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4824 if (err)
4825 return err;
4826 waddwstr(view->window, wline);
4827 free(wline);
4828 wline = NULL;
4829 if (width < view->ncols - 1)
4830 waddch(view->window, '\n');
4831 if (--limit <= 0)
4832 return NULL;
4833 waddch(view->window, '\n');
4834 if (--limit <= 0)
4835 return NULL;
4837 if (s->first_displayed_entry == NULL) {
4838 te = got_object_tree_get_first_entry(s->tree);
4839 if (s->selected == 0) {
4840 if (view->focussed)
4841 wstandout(view->window);
4842 s->selected_entry = NULL;
4844 waddstr(view->window, " ..\n"); /* parent directory */
4845 if (s->selected == 0 && view->focussed)
4846 wstandend(view->window);
4847 s->ndisplayed++;
4848 if (--limit <= 0)
4849 return NULL;
4850 n = 1;
4851 } else {
4852 n = 0;
4853 te = s->first_displayed_entry;
4856 nentries = got_object_tree_get_nentries(s->tree);
4857 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4858 char *line = NULL, *id_str = NULL, *link_target = NULL;
4859 const char *modestr = "";
4860 mode_t mode;
4862 te = got_object_tree_get_entry(s->tree, i);
4863 mode = got_tree_entry_get_mode(te);
4865 if (s->show_ids) {
4866 err = got_object_id_str(&id_str,
4867 got_tree_entry_get_id(te));
4868 if (err)
4869 return got_error_from_errno(
4870 "got_object_id_str");
4872 if (got_object_tree_entry_is_submodule(te))
4873 modestr = "$";
4874 else if (S_ISLNK(mode)) {
4875 int i;
4877 err = got_tree_entry_get_symlink_target(&link_target,
4878 te, s->repo);
4879 if (err) {
4880 free(id_str);
4881 return err;
4883 for (i = 0; i < strlen(link_target); i++) {
4884 if (!isprint((unsigned char)link_target[i]))
4885 link_target[i] = '?';
4887 modestr = "@";
4889 else if (S_ISDIR(mode))
4890 modestr = "/";
4891 else if (mode & S_IXUSR)
4892 modestr = "*";
4893 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4894 got_tree_entry_get_name(te), modestr,
4895 link_target ? " -> ": "",
4896 link_target ? link_target : "") == -1) {
4897 free(id_str);
4898 free(link_target);
4899 return got_error_from_errno("asprintf");
4901 free(id_str);
4902 free(link_target);
4903 err = format_line(&wline, &width, line, view->ncols, 0);
4904 if (err) {
4905 free(line);
4906 break;
4908 if (n == s->selected) {
4909 if (view->focussed)
4910 wstandout(view->window);
4911 s->selected_entry = te;
4913 tc = match_color(&s->colors, line);
4914 if (tc)
4915 wattr_on(view->window,
4916 COLOR_PAIR(tc->colorpair), NULL);
4917 waddwstr(view->window, wline);
4918 if (tc)
4919 wattr_off(view->window,
4920 COLOR_PAIR(tc->colorpair), NULL);
4921 if (width < view->ncols - 1)
4922 waddch(view->window, '\n');
4923 if (n == s->selected && view->focussed)
4924 wstandend(view->window);
4925 free(line);
4926 free(wline);
4927 wline = NULL;
4928 n++;
4929 s->ndisplayed++;
4930 s->last_displayed_entry = te;
4931 if (--limit <= 0)
4932 break;
4935 return err;
4938 static void
4939 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4941 struct got_tree_entry *te;
4942 int isroot = s->tree == s->root;
4943 int i = 0;
4945 if (s->first_displayed_entry == NULL)
4946 return;
4948 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4949 while (i++ < maxscroll) {
4950 if (te == NULL) {
4951 if (!isroot)
4952 s->first_displayed_entry = NULL;
4953 break;
4955 s->first_displayed_entry = te;
4956 te = got_tree_entry_get_prev(s->tree, te);
4960 static void
4961 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4963 struct got_tree_entry *next, *last;
4964 int n = 0;
4966 if (s->first_displayed_entry)
4967 next = got_tree_entry_get_next(s->tree,
4968 s->first_displayed_entry);
4969 else
4970 next = got_object_tree_get_first_entry(s->tree);
4972 last = s->last_displayed_entry;
4973 while (next && last && n++ < maxscroll) {
4974 last = got_tree_entry_get_next(s->tree, last);
4975 if (last) {
4976 s->first_displayed_entry = next;
4977 next = got_tree_entry_get_next(s->tree, next);
4982 static const struct got_error *
4983 tree_entry_path(char **path, struct tog_parent_trees *parents,
4984 struct got_tree_entry *te)
4986 const struct got_error *err = NULL;
4987 struct tog_parent_tree *pt;
4988 size_t len = 2; /* for leading slash and NUL */
4990 TAILQ_FOREACH(pt, parents, entry)
4991 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4992 + 1 /* slash */;
4993 if (te)
4994 len += strlen(got_tree_entry_get_name(te));
4996 *path = calloc(1, len);
4997 if (path == NULL)
4998 return got_error_from_errno("calloc");
5000 (*path)[0] = '/';
5001 pt = TAILQ_LAST(parents, tog_parent_trees);
5002 while (pt) {
5003 const char *name = got_tree_entry_get_name(pt->selected_entry);
5004 if (strlcat(*path, name, len) >= len) {
5005 err = got_error(GOT_ERR_NO_SPACE);
5006 goto done;
5008 if (strlcat(*path, "/", len) >= len) {
5009 err = got_error(GOT_ERR_NO_SPACE);
5010 goto done;
5012 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5014 if (te) {
5015 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5016 err = got_error(GOT_ERR_NO_SPACE);
5017 goto done;
5020 done:
5021 if (err) {
5022 free(*path);
5023 *path = NULL;
5025 return err;
5028 static const struct got_error *
5029 blame_tree_entry(struct tog_view **new_view, int begin_x,
5030 struct got_tree_entry *te, struct tog_parent_trees *parents,
5031 struct got_object_id *commit_id, struct got_repository *repo)
5033 const struct got_error *err = NULL;
5034 char *path;
5035 struct tog_view *blame_view;
5037 *new_view = NULL;
5039 err = tree_entry_path(&path, parents, te);
5040 if (err)
5041 return err;
5043 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5044 if (blame_view == NULL) {
5045 err = got_error_from_errno("view_open");
5046 goto done;
5049 err = open_blame_view(blame_view, path, commit_id, repo);
5050 if (err) {
5051 if (err->code == GOT_ERR_CANCELLED)
5052 err = NULL;
5053 view_close(blame_view);
5054 } else
5055 *new_view = blame_view;
5056 done:
5057 free(path);
5058 return err;
5061 static const struct got_error *
5062 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5063 struct tog_tree_view_state *s)
5065 struct tog_view *log_view;
5066 const struct got_error *err = NULL;
5067 char *path;
5069 *new_view = NULL;
5071 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5072 if (log_view == NULL)
5073 return got_error_from_errno("view_open");
5075 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5076 if (err)
5077 return err;
5079 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5080 path, 0);
5081 if (err)
5082 view_close(log_view);
5083 else
5084 *new_view = log_view;
5085 free(path);
5086 return err;
5089 static const struct got_error *
5090 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5091 struct got_object_id *commit_id, const char *head_ref_name,
5092 struct got_repository *repo)
5094 const struct got_error *err = NULL;
5095 char *commit_id_str = NULL;
5096 struct tog_tree_view_state *s = &view->state.tree;
5098 TAILQ_INIT(&s->parents);
5100 err = got_object_id_str(&commit_id_str, commit_id);
5101 if (err != NULL)
5102 goto done;
5104 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5105 err = got_error_from_errno("asprintf");
5106 goto done;
5109 s->root = s->tree = root;
5110 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5111 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5112 s->commit_id = got_object_id_dup(commit_id);
5113 if (s->commit_id == NULL) {
5114 err = got_error_from_errno("got_object_id_dup");
5115 goto done;
5117 if (head_ref_name) {
5118 s->head_ref_name = strdup(head_ref_name);
5119 if (s->head_ref_name == NULL) {
5120 err = got_error_from_errno("strdup");
5121 goto done;
5124 s->repo = repo;
5126 SIMPLEQ_INIT(&s->colors);
5128 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5129 err = add_color(&s->colors, "\\$$",
5130 TOG_COLOR_TREE_SUBMODULE,
5131 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5132 if (err)
5133 goto done;
5134 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5135 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5136 if (err) {
5137 free_colors(&s->colors);
5138 goto done;
5140 err = add_color(&s->colors, "/$",
5141 TOG_COLOR_TREE_DIRECTORY,
5142 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5143 if (err) {
5144 free_colors(&s->colors);
5145 goto done;
5148 err = add_color(&s->colors, "\\*$",
5149 TOG_COLOR_TREE_EXECUTABLE,
5150 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5151 if (err) {
5152 free_colors(&s->colors);
5153 goto done;
5156 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5157 get_color_value("TOG_COLOR_COMMIT"));
5158 if (err) {
5159 free_colors(&s->colors);
5160 goto done;
5164 view->show = show_tree_view;
5165 view->input = input_tree_view;
5166 view->close = close_tree_view;
5167 view->search_start = search_start_tree_view;
5168 view->search_next = search_next_tree_view;
5169 done:
5170 free(commit_id_str);
5171 if (err) {
5172 free(s->tree_label);
5173 s->tree_label = NULL;
5175 return err;
5178 static const struct got_error *
5179 close_tree_view(struct tog_view *view)
5181 struct tog_tree_view_state *s = &view->state.tree;
5183 free_colors(&s->colors);
5184 free(s->tree_label);
5185 s->tree_label = NULL;
5186 free(s->commit_id);
5187 s->commit_id = NULL;
5188 free(s->head_ref_name);
5189 s->head_ref_name = NULL;
5190 while (!TAILQ_EMPTY(&s->parents)) {
5191 struct tog_parent_tree *parent;
5192 parent = TAILQ_FIRST(&s->parents);
5193 TAILQ_REMOVE(&s->parents, parent, entry);
5194 free(parent);
5197 if (s->tree != s->root)
5198 got_object_tree_close(s->tree);
5199 got_object_tree_close(s->root);
5200 return NULL;
5203 static const struct got_error *
5204 search_start_tree_view(struct tog_view *view)
5206 struct tog_tree_view_state *s = &view->state.tree;
5208 s->matched_entry = NULL;
5209 return NULL;
5212 static int
5213 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5215 regmatch_t regmatch;
5217 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5218 0) == 0;
5221 static const struct got_error *
5222 search_next_tree_view(struct tog_view *view)
5224 struct tog_tree_view_state *s = &view->state.tree;
5225 struct got_tree_entry *te = NULL;
5227 if (!view->searching) {
5228 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5229 return NULL;
5232 if (s->matched_entry) {
5233 if (view->searching == TOG_SEARCH_FORWARD) {
5234 if (s->selected_entry)
5235 te = got_tree_entry_get_next(s->tree,
5236 s->selected_entry);
5237 else
5238 te = got_object_tree_get_first_entry(s->tree);
5239 } else {
5240 if (s->selected_entry == NULL)
5241 te = got_object_tree_get_last_entry(s->tree);
5242 else
5243 te = got_tree_entry_get_prev(s->tree,
5244 s->selected_entry);
5246 } else {
5247 if (view->searching == TOG_SEARCH_FORWARD)
5248 te = got_object_tree_get_first_entry(s->tree);
5249 else
5250 te = got_object_tree_get_last_entry(s->tree);
5253 while (1) {
5254 if (te == NULL) {
5255 if (s->matched_entry == NULL) {
5256 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5257 return NULL;
5259 if (view->searching == TOG_SEARCH_FORWARD)
5260 te = got_object_tree_get_first_entry(s->tree);
5261 else
5262 te = got_object_tree_get_last_entry(s->tree);
5265 if (match_tree_entry(te, &view->regex)) {
5266 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5267 s->matched_entry = te;
5268 break;
5271 if (view->searching == TOG_SEARCH_FORWARD)
5272 te = got_tree_entry_get_next(s->tree, te);
5273 else
5274 te = got_tree_entry_get_prev(s->tree, te);
5277 if (s->matched_entry) {
5278 s->first_displayed_entry = s->matched_entry;
5279 s->selected = 0;
5282 return NULL;
5285 static const struct got_error *
5286 show_tree_view(struct tog_view *view)
5288 const struct got_error *err = NULL;
5289 struct tog_tree_view_state *s = &view->state.tree;
5290 char *parent_path;
5292 err = tree_entry_path(&parent_path, &s->parents, NULL);
5293 if (err)
5294 return err;
5296 err = draw_tree_entries(view, parent_path);
5297 free(parent_path);
5299 view_vborder(view);
5300 return err;
5303 static const struct got_error *
5304 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5306 const struct got_error *err = NULL;
5307 struct tog_tree_view_state *s = &view->state.tree;
5308 struct tog_view *log_view, *ref_view;
5309 int begin_x = 0;
5311 switch (ch) {
5312 case 'i':
5313 s->show_ids = !s->show_ids;
5314 break;
5315 case 'l':
5316 if (!s->selected_entry)
5317 break;
5318 if (view_is_parent_view(view))
5319 begin_x = view_split_begin_x(view->begin_x);
5320 err = log_selected_tree_entry(&log_view, begin_x, s);
5321 view->focussed = 0;
5322 log_view->focussed = 1;
5323 if (view_is_parent_view(view)) {
5324 err = view_close_child(view);
5325 if (err)
5326 return err;
5327 view_set_child(view, log_view);
5328 view->focus_child = 1;
5329 } else
5330 *new_view = log_view;
5331 break;
5332 case 'r':
5333 if (view_is_parent_view(view))
5334 begin_x = view_split_begin_x(view->begin_x);
5335 ref_view = view_open(view->nlines, view->ncols,
5336 view->begin_y, begin_x, TOG_VIEW_REF);
5337 if (ref_view == NULL)
5338 return got_error_from_errno("view_open");
5339 err = open_ref_view(ref_view, s->repo);
5340 if (err) {
5341 view_close(ref_view);
5342 return err;
5344 view->focussed = 0;
5345 ref_view->focussed = 1;
5346 if (view_is_parent_view(view)) {
5347 err = view_close_child(view);
5348 if (err)
5349 return err;
5350 view_set_child(view, ref_view);
5351 view->focus_child = 1;
5352 } else
5353 *new_view = ref_view;
5354 break;
5355 case 'k':
5356 case KEY_UP:
5357 if (s->selected > 0) {
5358 s->selected--;
5359 break;
5361 tree_scroll_up(s, 1);
5362 break;
5363 case KEY_PPAGE:
5364 case CTRL('b'):
5365 if (s->tree == s->root) {
5366 if (got_object_tree_get_first_entry(s->tree) ==
5367 s->first_displayed_entry)
5368 s->selected = 0;
5369 } else {
5370 if (s->first_displayed_entry == NULL)
5371 s->selected = 0;
5373 tree_scroll_up(s, MAX(0, view->nlines - 3));
5374 break;
5375 case 'j':
5376 case KEY_DOWN:
5377 if (s->selected < s->ndisplayed - 1) {
5378 s->selected++;
5379 break;
5381 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5382 == NULL)
5383 /* can't scroll any further */
5384 break;
5385 tree_scroll_down(s, 1);
5386 break;
5387 case KEY_NPAGE:
5388 case CTRL('f'):
5389 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5390 == NULL) {
5391 /* can't scroll any further; move cursor down */
5392 if (s->selected < s->ndisplayed - 1)
5393 s->selected = s->ndisplayed - 1;
5394 break;
5396 tree_scroll_down(s, view->nlines - 3);
5397 break;
5398 case KEY_ENTER:
5399 case '\r':
5400 case KEY_BACKSPACE:
5401 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5402 struct tog_parent_tree *parent;
5403 /* user selected '..' */
5404 if (s->tree == s->root)
5405 break;
5406 parent = TAILQ_FIRST(&s->parents);
5407 TAILQ_REMOVE(&s->parents, parent,
5408 entry);
5409 got_object_tree_close(s->tree);
5410 s->tree = parent->tree;
5411 s->first_displayed_entry =
5412 parent->first_displayed_entry;
5413 s->selected_entry =
5414 parent->selected_entry;
5415 s->selected = parent->selected;
5416 free(parent);
5417 } else if (S_ISDIR(got_tree_entry_get_mode(
5418 s->selected_entry))) {
5419 struct got_tree_object *subtree;
5420 err = got_object_open_as_tree(&subtree, s->repo,
5421 got_tree_entry_get_id(s->selected_entry));
5422 if (err)
5423 break;
5424 err = tree_view_visit_subtree(s, subtree);
5425 if (err) {
5426 got_object_tree_close(subtree);
5427 break;
5429 } else if (S_ISREG(got_tree_entry_get_mode(
5430 s->selected_entry))) {
5431 struct tog_view *blame_view;
5432 int begin_x = view_is_parent_view(view) ?
5433 view_split_begin_x(view->begin_x) : 0;
5435 err = blame_tree_entry(&blame_view, begin_x,
5436 s->selected_entry, &s->parents,
5437 s->commit_id, s->repo);
5438 if (err)
5439 break;
5440 view->focussed = 0;
5441 blame_view->focussed = 1;
5442 if (view_is_parent_view(view)) {
5443 err = view_close_child(view);
5444 if (err)
5445 return err;
5446 view_set_child(view, blame_view);
5447 view->focus_child = 1;
5448 } else
5449 *new_view = blame_view;
5451 break;
5452 case KEY_RESIZE:
5453 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5454 s->selected = view->nlines - 4;
5455 break;
5456 default:
5457 break;
5460 return err;
5463 __dead static void
5464 usage_tree(void)
5466 endwin();
5467 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5468 getprogname());
5469 exit(1);
5472 static const struct got_error *
5473 cmd_tree(int argc, char *argv[])
5475 const struct got_error *error;
5476 struct got_repository *repo = NULL;
5477 struct got_worktree *worktree = NULL;
5478 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5479 struct got_object_id *commit_id = NULL;
5480 const char *commit_id_arg = NULL;
5481 char *label = NULL;
5482 struct got_commit_object *commit = NULL;
5483 struct got_tree_object *tree = NULL;
5484 struct got_reference *ref = NULL;
5485 const char *head_ref_name = NULL;
5486 int ch;
5487 struct tog_view *view;
5489 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5490 switch (ch) {
5491 case 'c':
5492 commit_id_arg = optarg;
5493 break;
5494 case 'r':
5495 repo_path = realpath(optarg, NULL);
5496 if (repo_path == NULL)
5497 return got_error_from_errno2("realpath",
5498 optarg);
5499 break;
5500 default:
5501 usage_tree();
5502 /* NOTREACHED */
5506 argc -= optind;
5507 argv += optind;
5509 if (argc > 1)
5510 usage_tree();
5512 if (repo_path == NULL) {
5513 cwd = getcwd(NULL, 0);
5514 if (cwd == NULL)
5515 return got_error_from_errno("getcwd");
5516 error = got_worktree_open(&worktree, cwd);
5517 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5518 goto done;
5519 if (worktree)
5520 repo_path =
5521 strdup(got_worktree_get_repo_path(worktree));
5522 else
5523 repo_path = strdup(cwd);
5524 if (repo_path == NULL) {
5525 error = got_error_from_errno("strdup");
5526 goto done;
5530 error = got_repo_open(&repo, repo_path, NULL);
5531 if (error != NULL)
5532 goto done;
5534 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5535 repo, worktree);
5536 if (error)
5537 goto done;
5539 init_curses();
5541 error = apply_unveil(got_repo_get_path(repo), NULL);
5542 if (error)
5543 goto done;
5545 error = tog_load_refs(repo);
5546 if (error)
5547 goto done;
5549 if (commit_id_arg == NULL) {
5550 error = got_repo_match_object_id(&commit_id, &label,
5551 worktree ? got_worktree_get_head_ref_name(worktree) :
5552 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5553 if (error)
5554 goto done;
5555 head_ref_name = label;
5556 } else {
5557 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5558 if (error == NULL)
5559 head_ref_name = got_ref_get_name(ref);
5560 else if (error->code != GOT_ERR_NOT_REF)
5561 goto done;
5562 error = got_repo_match_object_id(&commit_id, NULL,
5563 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5564 if (error)
5565 goto done;
5568 error = got_object_open_as_commit(&commit, repo, commit_id);
5569 if (error)
5570 goto done;
5572 error = got_object_open_as_tree(&tree, repo,
5573 got_object_commit_get_tree_id(commit));
5574 if (error)
5575 goto done;
5577 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5578 if (view == NULL) {
5579 error = got_error_from_errno("view_open");
5580 goto done;
5582 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5583 if (error)
5584 goto done;
5585 if (!got_path_is_root_dir(in_repo_path)) {
5586 error = tree_view_walk_path(&view->state.tree, commit_id,
5587 in_repo_path);
5588 if (error)
5589 goto done;
5592 if (worktree) {
5593 /* Release work tree lock. */
5594 got_worktree_close(worktree);
5595 worktree = NULL;
5597 error = view_loop(view);
5598 done:
5599 free(repo_path);
5600 free(cwd);
5601 free(commit_id);
5602 free(label);
5603 if (ref)
5604 got_ref_close(ref);
5605 if (commit)
5606 got_object_commit_close(commit);
5607 if (tree)
5608 got_object_tree_close(tree);
5609 if (repo)
5610 got_repo_close(repo);
5611 tog_free_refs();
5612 return error;
5615 static const struct got_error *
5616 ref_view_load_refs(struct tog_ref_view_state *s)
5618 struct got_reflist_entry *sre;
5619 struct tog_reflist_entry *re;
5621 s->nrefs = 0;
5622 TAILQ_FOREACH(sre, &tog_refs, entry) {
5623 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5624 continue;
5626 re = malloc(sizeof(*re));
5627 if (re == NULL)
5628 return got_error_from_errno("malloc");
5630 re->ref = got_ref_dup(sre->ref);
5631 if (re->ref == NULL)
5632 return got_error_from_errno("got_ref_dup");
5633 re->idx = s->nrefs++;
5634 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5637 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5638 return NULL;
5641 void
5642 ref_view_free_refs(struct tog_ref_view_state *s)
5644 struct tog_reflist_entry *re;
5646 while (!TAILQ_EMPTY(&s->refs)) {
5647 re = TAILQ_FIRST(&s->refs);
5648 TAILQ_REMOVE(&s->refs, re, entry);
5649 got_ref_close(re->ref);
5650 free(re);
5654 static const struct got_error *
5655 open_ref_view(struct tog_view *view, struct got_repository *repo)
5657 const struct got_error *err = NULL;
5658 struct tog_ref_view_state *s = &view->state.ref;
5660 s->selected_entry = 0;
5661 s->repo = repo;
5663 TAILQ_INIT(&s->refs);
5664 SIMPLEQ_INIT(&s->colors);
5666 err = ref_view_load_refs(s);
5667 if (err)
5668 return err;
5670 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5671 err = add_color(&s->colors, "^refs/heads/",
5672 TOG_COLOR_REFS_HEADS,
5673 get_color_value("TOG_COLOR_REFS_HEADS"));
5674 if (err)
5675 goto done;
5677 err = add_color(&s->colors, "^refs/tags/",
5678 TOG_COLOR_REFS_TAGS,
5679 get_color_value("TOG_COLOR_REFS_TAGS"));
5680 if (err)
5681 goto done;
5683 err = add_color(&s->colors, "^refs/remotes/",
5684 TOG_COLOR_REFS_REMOTES,
5685 get_color_value("TOG_COLOR_REFS_REMOTES"));
5686 if (err)
5687 goto done;
5690 view->show = show_ref_view;
5691 view->input = input_ref_view;
5692 view->close = close_ref_view;
5693 view->search_start = search_start_ref_view;
5694 view->search_next = search_next_ref_view;
5695 done:
5696 if (err)
5697 free_colors(&s->colors);
5698 return err;
5701 static const struct got_error *
5702 close_ref_view(struct tog_view *view)
5704 struct tog_ref_view_state *s = &view->state.ref;
5706 ref_view_free_refs(s);
5707 free_colors(&s->colors);
5709 return NULL;
5712 static const struct got_error *
5713 resolve_reflist_entry(struct got_object_id **commit_id,
5714 struct tog_reflist_entry *re, struct got_repository *repo)
5716 const struct got_error *err = NULL;
5717 struct got_object_id *obj_id;
5718 struct got_tag_object *tag = NULL;
5719 int obj_type;
5721 *commit_id = NULL;
5723 err = got_ref_resolve(&obj_id, repo, re->ref);
5724 if (err)
5725 return err;
5727 err = got_object_get_type(&obj_type, repo, obj_id);
5728 if (err)
5729 goto done;
5731 switch (obj_type) {
5732 case GOT_OBJ_TYPE_COMMIT:
5733 *commit_id = obj_id;
5734 break;
5735 case GOT_OBJ_TYPE_TAG:
5736 err = got_object_open_as_tag(&tag, repo, obj_id);
5737 if (err)
5738 goto done;
5739 free(obj_id);
5740 err = got_object_get_type(&obj_type, repo,
5741 got_object_tag_get_object_id(tag));
5742 if (err)
5743 goto done;
5744 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5745 err = got_error(GOT_ERR_OBJ_TYPE);
5746 goto done;
5748 *commit_id = got_object_id_dup(
5749 got_object_tag_get_object_id(tag));
5750 if (*commit_id == NULL) {
5751 err = got_error_from_errno("got_object_id_dup");
5752 goto done;
5754 break;
5755 default:
5756 err = got_error(GOT_ERR_OBJ_TYPE);
5757 break;
5760 done:
5761 if (tag)
5762 got_object_tag_close(tag);
5763 if (err) {
5764 free(*commit_id);
5765 *commit_id = NULL;
5767 return err;
5770 static const struct got_error *
5771 log_ref_entry(struct tog_view **new_view, int begin_x,
5772 struct tog_reflist_entry *re, struct got_repository *repo)
5774 struct tog_view *log_view;
5775 const struct got_error *err = NULL;
5776 struct got_object_id *commit_id = NULL;
5778 *new_view = NULL;
5780 err = resolve_reflist_entry(&commit_id, re, repo);
5781 if (err) {
5782 if (err->code != GOT_ERR_OBJ_TYPE)
5783 return err;
5784 else
5785 return NULL;
5788 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5789 if (log_view == NULL) {
5790 err = got_error_from_errno("view_open");
5791 goto done;
5794 err = open_log_view(log_view, commit_id, repo,
5795 got_ref_get_name(re->ref), "", 0);
5796 done:
5797 if (err)
5798 view_close(log_view);
5799 else
5800 *new_view = log_view;
5801 free(commit_id);
5802 return err;
5805 static void
5806 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5808 struct tog_reflist_entry *re;
5809 int i = 0;
5811 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5812 return;
5814 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5815 while (i++ < maxscroll) {
5816 if (re == NULL)
5817 break;
5818 s->first_displayed_entry = re;
5819 re = TAILQ_PREV(re, tog_reflist_head, entry);
5823 static void
5824 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5826 struct tog_reflist_entry *next, *last;
5827 int n = 0;
5829 if (s->first_displayed_entry)
5830 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5831 else
5832 next = TAILQ_FIRST(&s->refs);
5834 last = s->last_displayed_entry;
5835 while (next && last && n++ < maxscroll) {
5836 last = TAILQ_NEXT(last, entry);
5837 if (last) {
5838 s->first_displayed_entry = next;
5839 next = TAILQ_NEXT(next, entry);
5844 static const struct got_error *
5845 search_start_ref_view(struct tog_view *view)
5847 struct tog_ref_view_state *s = &view->state.ref;
5849 s->matched_entry = NULL;
5850 return NULL;
5853 static int
5854 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5856 regmatch_t regmatch;
5858 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5859 0) == 0;
5862 static const struct got_error *
5863 search_next_ref_view(struct tog_view *view)
5865 struct tog_ref_view_state *s = &view->state.ref;
5866 struct tog_reflist_entry *re = NULL;
5868 if (!view->searching) {
5869 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5870 return NULL;
5873 if (s->matched_entry) {
5874 if (view->searching == TOG_SEARCH_FORWARD) {
5875 if (s->selected_entry)
5876 re = TAILQ_NEXT(s->selected_entry, entry);
5877 else
5878 re = TAILQ_PREV(s->selected_entry,
5879 tog_reflist_head, entry);
5880 } else {
5881 if (s->selected_entry == NULL)
5882 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5883 else
5884 re = TAILQ_PREV(s->selected_entry,
5885 tog_reflist_head, entry);
5887 } else {
5888 if (view->searching == TOG_SEARCH_FORWARD)
5889 re = TAILQ_FIRST(&s->refs);
5890 else
5891 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5894 while (1) {
5895 if (re == NULL) {
5896 if (s->matched_entry == NULL) {
5897 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5898 return NULL;
5900 if (view->searching == TOG_SEARCH_FORWARD)
5901 re = TAILQ_FIRST(&s->refs);
5902 else
5903 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5906 if (match_reflist_entry(re, &view->regex)) {
5907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5908 s->matched_entry = re;
5909 break;
5912 if (view->searching == TOG_SEARCH_FORWARD)
5913 re = TAILQ_NEXT(re, entry);
5914 else
5915 re = TAILQ_PREV(re, tog_reflist_head, entry);
5918 if (s->matched_entry) {
5919 s->first_displayed_entry = s->matched_entry;
5920 s->selected = 0;
5923 return NULL;
5926 static const struct got_error *
5927 show_ref_view(struct tog_view *view)
5929 const struct got_error *err = NULL;
5930 struct tog_ref_view_state *s = &view->state.ref;
5931 struct tog_reflist_entry *re;
5932 char *line = NULL;
5933 wchar_t *wline;
5934 struct tog_color *tc;
5935 int width, n;
5936 int limit = view->nlines;
5938 werase(view->window);
5940 s->ndisplayed = 0;
5942 if (limit == 0)
5943 return NULL;
5945 re = s->first_displayed_entry;
5947 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5948 s->nrefs) == -1)
5949 return got_error_from_errno("asprintf");
5951 err = format_line(&wline, &width, line, view->ncols, 0);
5952 if (err) {
5953 free(line);
5954 return err;
5956 if (view_needs_focus_indication(view))
5957 wstandout(view->window);
5958 waddwstr(view->window, wline);
5959 if (view_needs_focus_indication(view))
5960 wstandend(view->window);
5961 free(wline);
5962 wline = NULL;
5963 free(line);
5964 line = NULL;
5965 if (width < view->ncols - 1)
5966 waddch(view->window, '\n');
5967 if (--limit <= 0)
5968 return NULL;
5970 n = 0;
5971 while (re && limit > 0) {
5972 char *line = NULL;
5974 if (got_ref_is_symbolic(re->ref)) {
5975 if (asprintf(&line, "%s -> %s",
5976 got_ref_get_name(re->ref),
5977 got_ref_get_symref_target(re->ref)) == -1)
5978 return got_error_from_errno("asprintf");
5979 } else if (s->show_ids) {
5980 struct got_object_id *id;
5981 char *id_str;
5982 err = got_ref_resolve(&id, s->repo, re->ref);
5983 if (err)
5984 return err;
5985 err = got_object_id_str(&id_str, id);
5986 if (err) {
5987 free(id);
5988 return err;
5990 if (asprintf(&line, "%s: %s",
5991 got_ref_get_name(re->ref), id_str) == -1) {
5992 err = got_error_from_errno("asprintf");
5993 free(id);
5994 free(id_str);
5995 return err;
5997 free(id);
5998 free(id_str);
5999 } else {
6000 line = strdup(got_ref_get_name(re->ref));
6001 if (line == NULL)
6002 return got_error_from_errno("strdup");
6005 err = format_line(&wline, &width, line, view->ncols, 0);
6006 if (err) {
6007 free(line);
6008 return err;
6010 if (n == s->selected) {
6011 if (view->focussed)
6012 wstandout(view->window);
6013 s->selected_entry = re;
6015 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6016 if (tc)
6017 wattr_on(view->window,
6018 COLOR_PAIR(tc->colorpair), NULL);
6019 waddwstr(view->window, wline);
6020 if (tc)
6021 wattr_off(view->window,
6022 COLOR_PAIR(tc->colorpair), NULL);
6023 if (width < view->ncols - 1)
6024 waddch(view->window, '\n');
6025 if (n == s->selected && view->focussed)
6026 wstandend(view->window);
6027 free(line);
6028 free(wline);
6029 wline = NULL;
6030 n++;
6031 s->ndisplayed++;
6032 s->last_displayed_entry = re;
6034 limit--;
6035 re = TAILQ_NEXT(re, entry);
6038 view_vborder(view);
6039 return err;
6042 static const struct got_error *
6043 browse_ref_tree(struct tog_view **new_view, int begin_x,
6044 struct tog_reflist_entry *re, struct got_repository *repo)
6046 const struct got_error *err = NULL;
6047 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6048 struct got_tree_object *tree = NULL;
6049 struct tog_view *tree_view;
6051 *new_view = NULL;
6053 err = resolve_reflist_entry(&commit_id, re, repo);
6054 if (err) {
6055 if (err->code != GOT_ERR_OBJ_TYPE)
6056 return err;
6057 else
6058 return NULL;
6061 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6062 if (err)
6063 goto done;
6065 err = got_object_open_as_tree(&tree, repo, tree_id);
6066 if (err)
6067 goto done;
6069 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6070 if (tree_view == NULL) {
6071 err = got_error_from_errno("view_open");
6072 goto done;
6075 err = open_tree_view(tree_view, tree, commit_id,
6076 got_ref_get_name(re->ref), repo);
6077 if (err)
6078 goto done;
6080 *new_view = tree_view;
6081 done:
6082 free(commit_id);
6083 free(tree_id);
6084 if (err) {
6085 if (tree)
6086 got_object_tree_close(tree);
6088 return err;
6090 static const struct got_error *
6091 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6093 const struct got_error *err = NULL;
6094 struct tog_ref_view_state *s = &view->state.ref;
6095 struct tog_view *log_view, *tree_view;
6096 int begin_x = 0;
6098 switch (ch) {
6099 case 'i':
6100 s->show_ids = !s->show_ids;
6101 break;
6102 case KEY_ENTER:
6103 case '\r':
6104 if (!s->selected_entry)
6105 break;
6106 if (view_is_parent_view(view))
6107 begin_x = view_split_begin_x(view->begin_x);
6108 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6109 s->repo);
6110 view->focussed = 0;
6111 log_view->focussed = 1;
6112 if (view_is_parent_view(view)) {
6113 err = view_close_child(view);
6114 if (err)
6115 return err;
6116 view_set_child(view, log_view);
6117 view->focus_child = 1;
6118 } else
6119 *new_view = log_view;
6120 break;
6121 case 't':
6122 if (!s->selected_entry)
6123 break;
6124 if (view_is_parent_view(view))
6125 begin_x = view_split_begin_x(view->begin_x);
6126 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6127 s->repo);
6128 if (err || tree_view == NULL)
6129 break;
6130 view->focussed = 0;
6131 tree_view->focussed = 1;
6132 if (view_is_parent_view(view)) {
6133 err = view_close_child(view);
6134 if (err)
6135 return err;
6136 view_set_child(view, tree_view);
6137 view->focus_child = 1;
6138 } else
6139 *new_view = tree_view;
6140 break;
6141 case 'k':
6142 case KEY_UP:
6143 if (s->selected > 0) {
6144 s->selected--;
6145 break;
6147 ref_scroll_up(s, 1);
6148 break;
6149 case KEY_PPAGE:
6150 case CTRL('b'):
6151 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6152 s->selected = 0;
6153 ref_scroll_up(s, MAX(0, view->nlines - 1));
6154 break;
6155 case 'j':
6156 case KEY_DOWN:
6157 if (s->selected < s->ndisplayed - 1) {
6158 s->selected++;
6159 break;
6161 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6162 /* can't scroll any further */
6163 break;
6164 ref_scroll_down(s, 1);
6165 break;
6166 case KEY_NPAGE:
6167 case CTRL('f'):
6168 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6169 /* can't scroll any further; move cursor down */
6170 if (s->selected < s->ndisplayed - 1)
6171 s->selected = s->ndisplayed - 1;
6172 break;
6174 ref_scroll_down(s, view->nlines - 1);
6175 break;
6176 case CTRL('l'):
6177 tog_free_refs();
6178 err = tog_load_refs(s->repo);
6179 if (err)
6180 break;
6181 ref_view_free_refs(s);
6182 err = ref_view_load_refs(s);
6183 break;
6184 case KEY_RESIZE:
6185 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6186 s->selected = view->nlines - 2;
6187 break;
6188 default:
6189 break;
6192 return err;
6195 __dead static void
6196 usage_ref(void)
6198 endwin();
6199 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6200 getprogname());
6201 exit(1);
6204 static const struct got_error *
6205 cmd_ref(int argc, char *argv[])
6207 const struct got_error *error;
6208 struct got_repository *repo = NULL;
6209 struct got_worktree *worktree = NULL;
6210 char *cwd = NULL, *repo_path = NULL;
6211 int ch;
6212 struct tog_view *view;
6214 while ((ch = getopt(argc, argv, "r:")) != -1) {
6215 switch (ch) {
6216 case 'r':
6217 repo_path = realpath(optarg, NULL);
6218 if (repo_path == NULL)
6219 return got_error_from_errno2("realpath",
6220 optarg);
6221 break;
6222 default:
6223 usage_ref();
6224 /* NOTREACHED */
6228 argc -= optind;
6229 argv += optind;
6231 if (argc > 1)
6232 usage_ref();
6234 if (repo_path == NULL) {
6235 cwd = getcwd(NULL, 0);
6236 if (cwd == NULL)
6237 return got_error_from_errno("getcwd");
6238 error = got_worktree_open(&worktree, cwd);
6239 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6240 goto done;
6241 if (worktree)
6242 repo_path =
6243 strdup(got_worktree_get_repo_path(worktree));
6244 else
6245 repo_path = strdup(cwd);
6246 if (repo_path == NULL) {
6247 error = got_error_from_errno("strdup");
6248 goto done;
6252 error = got_repo_open(&repo, repo_path, NULL);
6253 if (error != NULL)
6254 goto done;
6256 init_curses();
6258 error = apply_unveil(got_repo_get_path(repo), NULL);
6259 if (error)
6260 goto done;
6262 error = tog_load_refs(repo);
6263 if (error)
6264 goto done;
6266 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6267 if (view == NULL) {
6268 error = got_error_from_errno("view_open");
6269 goto done;
6272 error = open_ref_view(view, repo);
6273 if (error)
6274 goto done;
6276 if (worktree) {
6277 /* Release work tree lock. */
6278 got_worktree_close(worktree);
6279 worktree = NULL;
6281 error = view_loop(view);
6282 done:
6283 free(repo_path);
6284 free(cwd);
6285 if (repo)
6286 got_repo_close(repo);
6287 tog_free_refs();
6288 return error;
6291 static void
6292 list_commands(FILE *fp)
6294 size_t i;
6296 fprintf(fp, "commands:");
6297 for (i = 0; i < nitems(tog_commands); i++) {
6298 struct tog_cmd *cmd = &tog_commands[i];
6299 fprintf(fp, " %s", cmd->name);
6301 fputc('\n', fp);
6304 __dead static void
6305 usage(int hflag, int status)
6307 FILE *fp = (status == 0) ? stdout : stderr;
6309 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6310 getprogname());
6311 if (hflag) {
6312 fprintf(fp, "lazy usage: %s path\n", getprogname());
6313 list_commands(fp);
6315 exit(status);
6318 static char **
6319 make_argv(int argc, ...)
6321 va_list ap;
6322 char **argv;
6323 int i;
6325 va_start(ap, argc);
6327 argv = calloc(argc, sizeof(char *));
6328 if (argv == NULL)
6329 err(1, "calloc");
6330 for (i = 0; i < argc; i++) {
6331 argv[i] = strdup(va_arg(ap, char *));
6332 if (argv[i] == NULL)
6333 err(1, "strdup");
6336 va_end(ap);
6337 return argv;
6341 * Try to convert 'tog path' into a 'tog log path' command.
6342 * The user could simply have mistyped the command rather than knowingly
6343 * provided a path. So check whether argv[0] can in fact be resolved
6344 * to a path in the HEAD commit and print a special error if not.
6345 * This hack is for mpi@ <3
6347 static const struct got_error *
6348 tog_log_with_path(int argc, char *argv[])
6350 const struct got_error *error = NULL;
6351 struct tog_cmd *cmd = NULL;
6352 struct got_repository *repo = NULL;
6353 struct got_worktree *worktree = NULL;
6354 struct got_object_id *commit_id = NULL, *id = NULL;
6355 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6356 char *commit_id_str = NULL, **cmd_argv = NULL;
6358 cwd = getcwd(NULL, 0);
6359 if (cwd == NULL)
6360 return got_error_from_errno("getcwd");
6362 error = got_worktree_open(&worktree, cwd);
6363 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6364 goto done;
6366 if (worktree)
6367 repo_path = strdup(got_worktree_get_repo_path(worktree));
6368 else
6369 repo_path = strdup(cwd);
6370 if (repo_path == NULL) {
6371 error = got_error_from_errno("strdup");
6372 goto done;
6375 error = got_repo_open(&repo, repo_path, NULL);
6376 if (error != NULL)
6377 goto done;
6379 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6380 repo, worktree);
6381 if (error)
6382 goto done;
6384 error = tog_load_refs(repo);
6385 if (error)
6386 goto done;
6387 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6388 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6389 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6390 if (error)
6391 goto done;
6393 if (worktree) {
6394 got_worktree_close(worktree);
6395 worktree = NULL;
6398 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6399 if (error) {
6400 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6401 goto done;
6402 fprintf(stderr, "%s: '%s' is no known command or path\n",
6403 getprogname(), argv[0]);
6404 usage(1, 1);
6405 /* not reached */
6408 got_repo_close(repo);
6409 repo = NULL;
6411 error = got_object_id_str(&commit_id_str, commit_id);
6412 if (error)
6413 goto done;
6415 cmd = &tog_commands[0]; /* log */
6416 argc = 4;
6417 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6418 error = cmd->cmd_main(argc, cmd_argv);
6419 done:
6420 if (repo)
6421 got_repo_close(repo);
6422 if (worktree)
6423 got_worktree_close(worktree);
6424 free(id);
6425 free(commit_id_str);
6426 free(commit_id);
6427 free(cwd);
6428 free(repo_path);
6429 free(in_repo_path);
6430 if (cmd_argv) {
6431 int i;
6432 for (i = 0; i < argc; i++)
6433 free(cmd_argv[i]);
6434 free(cmd_argv);
6436 tog_free_refs();
6437 return error;
6440 int
6441 main(int argc, char *argv[])
6443 const struct got_error *error = NULL;
6444 struct tog_cmd *cmd = NULL;
6445 int ch, hflag = 0, Vflag = 0;
6446 char **cmd_argv = NULL;
6447 static struct option longopts[] = {
6448 { "version", no_argument, NULL, 'V' },
6449 { NULL, 0, NULL, 0}
6452 setlocale(LC_CTYPE, "");
6454 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6455 switch (ch) {
6456 case 'h':
6457 hflag = 1;
6458 break;
6459 case 'V':
6460 Vflag = 1;
6461 break;
6462 default:
6463 usage(hflag, 1);
6464 /* NOTREACHED */
6468 argc -= optind;
6469 argv += optind;
6470 optind = 1;
6471 optreset = 1;
6473 if (Vflag) {
6474 got_version_print_str();
6475 return 0;
6478 #ifndef PROFILE
6479 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6480 NULL) == -1)
6481 err(1, "pledge");
6482 #endif
6484 if (argc == 0) {
6485 if (hflag)
6486 usage(hflag, 0);
6487 /* Build an argument vector which runs a default command. */
6488 cmd = &tog_commands[0];
6489 argc = 1;
6490 cmd_argv = make_argv(argc, cmd->name);
6491 } else {
6492 size_t i;
6494 /* Did the user specify a command? */
6495 for (i = 0; i < nitems(tog_commands); i++) {
6496 if (strncmp(tog_commands[i].name, argv[0],
6497 strlen(argv[0])) == 0) {
6498 cmd = &tog_commands[i];
6499 break;
6504 if (cmd == NULL) {
6505 if (argc != 1)
6506 usage(0, 1);
6507 /* No command specified; try log with a path */
6508 error = tog_log_with_path(argc, argv);
6509 } else {
6510 if (hflag)
6511 cmd->cmd_usage();
6512 else
6513 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6516 endwin();
6517 putchar('\n');
6518 if (cmd_argv) {
6519 int i;
6520 for (i = 0; i < argc; i++)
6521 free(cmd_argv[i]);
6522 free(cmd_argv);
6525 if (error && error->code != GOT_ERR_CANCELLED)
6526 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6527 return 0;