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 const struct got_error *
129 add_color(struct tog_colors *colors, const char *pattern,
130 int idx, short color)
132 const struct got_error *err = NULL;
133 struct tog_color *tc;
134 int regerr = 0;
136 if (idx < 1 || idx > COLOR_PAIRS - 1)
137 return NULL;
139 init_pair(idx, color, -1);
141 tc = calloc(1, sizeof(*tc));
142 if (tc == NULL)
143 return got_error_from_errno("calloc");
144 regerr = regcomp(&tc->regex, pattern,
145 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
146 if (regerr) {
147 static char regerr_msg[512];
148 static char err_msg[512];
149 regerror(regerr, &tc->regex, regerr_msg,
150 sizeof(regerr_msg));
151 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
152 regerr_msg);
153 err = got_error_msg(GOT_ERR_REGEX, err_msg);
154 free(tc);
155 return err;
157 tc->colorpair = idx;
158 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
159 return NULL;
162 static void
163 free_colors(struct tog_colors *colors)
165 struct tog_color *tc;
167 while (!SIMPLEQ_EMPTY(colors)) {
168 tc = SIMPLEQ_FIRST(colors);
169 SIMPLEQ_REMOVE_HEAD(colors, entry);
170 regfree(&tc->regex);
171 free(tc);
175 struct tog_color *
176 get_color(struct tog_colors *colors, int colorpair)
178 struct tog_color *tc = NULL;
180 SIMPLEQ_FOREACH(tc, colors, entry) {
181 if (tc->colorpair == colorpair)
182 return tc;
185 return NULL;
188 static int
189 default_color_value(const char *envvar)
191 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
192 return COLOR_MAGENTA;
193 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
194 return COLOR_CYAN;
195 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
196 return COLOR_YELLOW;
197 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
198 return COLOR_GREEN;
199 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
200 return COLOR_MAGENTA;
201 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
202 return COLOR_MAGENTA;
203 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
204 return COLOR_CYAN;
205 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
206 return COLOR_GREEN;
207 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
208 return COLOR_GREEN;
209 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
210 return COLOR_CYAN;
211 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
212 return COLOR_YELLOW;
213 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
214 return COLOR_GREEN;
215 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
216 return COLOR_MAGENTA;
217 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
218 return COLOR_YELLOW;
220 return -1;
223 static int
224 get_color_value(const char *envvar)
226 const char *val = getenv(envvar);
228 if (val == NULL)
229 return default_color_value(envvar);
231 if (strcasecmp(val, "black") == 0)
232 return COLOR_BLACK;
233 if (strcasecmp(val, "red") == 0)
234 return COLOR_RED;
235 if (strcasecmp(val, "green") == 0)
236 return COLOR_GREEN;
237 if (strcasecmp(val, "yellow") == 0)
238 return COLOR_YELLOW;
239 if (strcasecmp(val, "blue") == 0)
240 return COLOR_BLUE;
241 if (strcasecmp(val, "magenta") == 0)
242 return COLOR_MAGENTA;
243 if (strcasecmp(val, "cyan") == 0)
244 return COLOR_CYAN;
245 if (strcasecmp(val, "white") == 0)
246 return COLOR_WHITE;
247 if (strcasecmp(val, "default") == 0)
248 return -1;
250 return default_color_value(envvar);
254 struct tog_diff_view_state {
255 struct got_object_id *id1, *id2;
256 const char *label1, *label2;
257 FILE *f;
258 int first_displayed_line;
259 int last_displayed_line;
260 int eof;
261 int diff_context;
262 int ignore_whitespace;
263 int force_text_diff;
264 struct got_repository *repo;
265 struct got_reflist_head refs;
266 struct tog_colors colors;
267 size_t nlines;
268 off_t *line_offsets;
269 int matched_line;
270 int selected_line;
272 /* passed from log view; may be NULL */
273 struct tog_view *log_view;
274 };
276 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
278 struct tog_log_thread_args {
279 pthread_cond_t need_commits;
280 pthread_cond_t commit_loaded;
281 int commits_needed;
282 struct got_commit_graph *graph;
283 struct commit_queue *commits;
284 const char *in_repo_path;
285 struct got_object_id *start_id;
286 struct got_repository *repo;
287 int log_complete;
288 sig_atomic_t *quit;
289 struct commit_queue_entry **first_displayed_entry;
290 struct commit_queue_entry **selected_entry;
291 int *searching;
292 int *search_next_done;
293 regex_t *regex;
294 };
296 struct tog_log_view_state {
297 struct commit_queue commits;
298 struct commit_queue_entry *first_displayed_entry;
299 struct commit_queue_entry *last_displayed_entry;
300 struct commit_queue_entry *selected_entry;
301 int selected;
302 char *in_repo_path;
303 char *head_ref_name;
304 int log_branches;
305 struct got_repository *repo;
306 struct got_reflist_head refs;
307 struct got_object_id *start_id;
308 sig_atomic_t quit;
309 pthread_t thread;
310 struct tog_log_thread_args thread_args;
311 struct commit_queue_entry *matched_entry;
312 struct commit_queue_entry *search_entry;
313 struct tog_colors colors;
314 };
316 #define TOG_COLOR_DIFF_MINUS 1
317 #define TOG_COLOR_DIFF_PLUS 2
318 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
319 #define TOG_COLOR_DIFF_META 4
320 #define TOG_COLOR_TREE_SUBMODULE 5
321 #define TOG_COLOR_TREE_SYMLINK 6
322 #define TOG_COLOR_TREE_DIRECTORY 7
323 #define TOG_COLOR_TREE_EXECUTABLE 8
324 #define TOG_COLOR_COMMIT 9
325 #define TOG_COLOR_AUTHOR 10
326 #define TOG_COLOR_DATE 11
327 #define TOG_COLOR_REFS_HEADS 12
328 #define TOG_COLOR_REFS_TAGS 13
329 #define TOG_COLOR_REFS_REMOTES 14
331 struct tog_blame_cb_args {
332 struct tog_blame_line *lines; /* one per line */
333 int nlines;
335 struct tog_view *view;
336 struct got_object_id *commit_id;
337 int *quit;
338 };
340 struct tog_blame_thread_args {
341 const char *path;
342 struct got_repository *repo;
343 struct tog_blame_cb_args *cb_args;
344 int *complete;
345 got_cancel_cb cancel_cb;
346 void *cancel_arg;
347 };
349 struct tog_blame {
350 FILE *f;
351 off_t filesize;
352 struct tog_blame_line *lines;
353 int nlines;
354 off_t *line_offsets;
355 pthread_t thread;
356 struct tog_blame_thread_args thread_args;
357 struct tog_blame_cb_args cb_args;
358 const char *path;
359 };
361 struct tog_blame_view_state {
362 int first_displayed_line;
363 int last_displayed_line;
364 int selected_line;
365 int blame_complete;
366 int eof;
367 int done;
368 struct got_object_id_queue blamed_commits;
369 struct got_object_qid *blamed_commit;
370 char *path;
371 struct got_repository *repo;
372 struct got_object_id *commit_id;
373 struct tog_blame blame;
374 int matched_line;
375 struct tog_colors colors;
376 };
378 struct tog_parent_tree {
379 TAILQ_ENTRY(tog_parent_tree) entry;
380 struct got_tree_object *tree;
381 struct got_tree_entry *first_displayed_entry;
382 struct got_tree_entry *selected_entry;
383 int selected;
384 };
386 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
388 struct tog_tree_view_state {
389 char *tree_label;
390 struct got_tree_object *root;
391 struct got_tree_object *tree;
392 struct got_tree_entry *first_displayed_entry;
393 struct got_tree_entry *last_displayed_entry;
394 struct got_tree_entry *selected_entry;
395 int ndisplayed, selected, show_ids;
396 struct tog_parent_trees parents;
397 struct got_object_id *commit_id;
398 char *head_ref_name;
399 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
402 };
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
407 int idx;
408 };
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
422 };
424 /*
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches focus between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
440 */
441 TAILQ_HEAD(tog_view_list_head, tog_view);
443 struct tog_view {
444 TAILQ_ENTRY(tog_view) entry;
445 WINDOW *window;
446 PANEL *panel;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
449 int focussed; /* Only set on one parent or child view at a time. */
450 int dying;
451 struct tog_view *parent;
452 struct tog_view *child;
454 /*
455 * This flag is initially set on parent views when a new child view
456 * is created. It gets toggled when the 'Tab' key switches focus
457 * between parent and child.
458 * The flag indicates whether focus should be passed on to our child
459 * view if this parent view gets picked for focus after another parent
460 * view was closed. This prevents child views from losing focus in such
461 * situations.
462 */
463 int focus_child;
465 /* type-specific state */
466 enum tog_view_type type;
467 union {
468 struct tog_diff_view_state diff;
469 struct tog_log_view_state log;
470 struct tog_blame_view_state blame;
471 struct tog_tree_view_state tree;
472 struct tog_ref_view_state ref;
473 } state;
475 const struct got_error *(*show)(struct tog_view *);
476 const struct got_error *(*input)(struct tog_view **,
477 struct tog_view *, int);
478 const struct got_error *(*close)(struct tog_view *);
480 const struct got_error *(*search_start)(struct tog_view *);
481 const struct got_error *(*search_next)(struct tog_view *);
482 int searching;
483 #define TOG_SEARCH_FORWARD 1
484 #define TOG_SEARCH_BACKWARD 2
485 int search_next_done;
486 #define TOG_SEARCH_HAVE_MORE 1
487 #define TOG_SEARCH_NO_MORE 2
488 #define TOG_SEARCH_HAVE_NONE 3
489 regex_t regex;
490 regmatch_t regmatch;
491 };
493 static const struct got_error *open_diff_view(struct tog_view *,
494 struct got_object_id *, struct got_object_id *,
495 const char *, const char *, int, int, int, struct tog_view *,
496 struct got_repository *);
497 static const struct got_error *show_diff_view(struct tog_view *);
498 static const struct got_error *input_diff_view(struct tog_view **,
499 struct tog_view *, int);
500 static const struct got_error* close_diff_view(struct tog_view *);
501 static const struct got_error *search_start_diff_view(struct tog_view *);
502 static const struct got_error *search_next_diff_view(struct tog_view *);
504 static const struct got_error *open_log_view(struct tog_view *,
505 struct got_object_id *, struct got_repository *,
506 const char *, const char *, int);
507 static const struct got_error * show_log_view(struct tog_view *);
508 static const struct got_error *input_log_view(struct tog_view **,
509 struct tog_view *, int);
510 static const struct got_error *close_log_view(struct tog_view *);
511 static const struct got_error *search_start_log_view(struct tog_view *);
512 static const struct got_error *search_next_log_view(struct tog_view *);
514 static const struct got_error *open_blame_view(struct tog_view *, char *,
515 struct got_object_id *, struct got_repository *);
516 static const struct got_error *show_blame_view(struct tog_view *);
517 static const struct got_error *input_blame_view(struct tog_view **,
518 struct tog_view *, int);
519 static const struct got_error *close_blame_view(struct tog_view *);
520 static const struct got_error *search_start_blame_view(struct tog_view *);
521 static const struct got_error *search_next_blame_view(struct tog_view *);
523 static const struct got_error *open_tree_view(struct tog_view *,
524 struct got_tree_object *, struct got_object_id *, const char *,
525 struct got_repository *);
526 static const struct got_error *show_tree_view(struct tog_view *);
527 static const struct got_error *input_tree_view(struct tog_view **,
528 struct tog_view *, int);
529 static const struct got_error *close_tree_view(struct tog_view *);
530 static const struct got_error *search_start_tree_view(struct tog_view *);
531 static const struct got_error *search_next_tree_view(struct tog_view *);
533 static const struct got_error *open_ref_view(struct tog_view *,
534 struct got_repository *);
535 static const struct got_error *show_ref_view(struct tog_view *);
536 static const struct got_error *input_ref_view(struct tog_view **,
537 struct tog_view *, int);
538 static const struct got_error *close_ref_view(struct tog_view *);
539 static const struct got_error *search_start_ref_view(struct tog_view *);
540 static const struct got_error *search_next_ref_view(struct tog_view *);
542 static volatile sig_atomic_t tog_sigwinch_received;
543 static volatile sig_atomic_t tog_sigpipe_received;
544 static volatile sig_atomic_t tog_sigcont_received;
546 static void
547 tog_sigwinch(int signo)
549 tog_sigwinch_received = 1;
552 static void
553 tog_sigpipe(int signo)
555 tog_sigpipe_received = 1;
558 static void
559 tog_sigcont(int signo)
561 tog_sigcont_received = 1;
564 static const struct got_error *
565 view_close(struct tog_view *view)
567 const struct got_error *err = NULL;
569 if (view->child) {
570 view_close(view->child);
571 view->child = NULL;
573 if (view->close)
574 err = view->close(view);
575 if (view->panel)
576 del_panel(view->panel);
577 if (view->window)
578 delwin(view->window);
579 free(view);
580 return err;
583 static struct tog_view *
584 view_open(int nlines, int ncols, int begin_y, int begin_x,
585 enum tog_view_type type)
587 struct tog_view *view = calloc(1, sizeof(*view));
589 if (view == NULL)
590 return NULL;
592 view->type = type;
593 view->lines = LINES;
594 view->cols = COLS;
595 view->nlines = nlines ? nlines : LINES - begin_y;
596 view->ncols = ncols ? ncols : COLS - begin_x;
597 view->begin_y = begin_y;
598 view->begin_x = begin_x;
599 view->window = newwin(nlines, ncols, begin_y, begin_x);
600 if (view->window == NULL) {
601 view_close(view);
602 return NULL;
604 view->panel = new_panel(view->window);
605 if (view->panel == NULL ||
606 set_panel_userptr(view->panel, view) != OK) {
607 view_close(view);
608 return NULL;
611 keypad(view->window, TRUE);
612 return view;
615 static int
616 view_split_begin_x(int begin_x)
618 if (begin_x > 0 || COLS < 120)
619 return 0;
620 return (COLS - MAX(COLS / 2, 80));
623 static const struct got_error *view_resize(struct tog_view *);
625 static const struct got_error *
626 view_splitscreen(struct tog_view *view)
628 const struct got_error *err = NULL;
630 view->begin_y = 0;
631 view->begin_x = view_split_begin_x(0);
632 view->nlines = LINES;
633 view->ncols = COLS - view->begin_x;
634 view->lines = LINES;
635 view->cols = COLS;
636 err = view_resize(view);
637 if (err)
638 return err;
640 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
641 return got_error_from_errno("mvwin");
643 return NULL;
646 static const struct got_error *
647 view_fullscreen(struct tog_view *view)
649 const struct got_error *err = NULL;
651 view->begin_x = 0;
652 view->begin_y = 0;
653 view->nlines = LINES;
654 view->ncols = COLS;
655 view->lines = LINES;
656 view->cols = COLS;
657 err = view_resize(view);
658 if (err)
659 return err;
661 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
662 return got_error_from_errno("mvwin");
664 return NULL;
667 static int
668 view_is_parent_view(struct tog_view *view)
670 return view->parent == NULL;
673 static const struct got_error *
674 view_resize(struct tog_view *view)
676 int nlines, ncols;
678 if (view->lines > LINES)
679 nlines = view->nlines - (view->lines - LINES);
680 else
681 nlines = view->nlines + (LINES - view->lines);
683 if (view->cols > COLS)
684 ncols = view->ncols - (view->cols - COLS);
685 else
686 ncols = view->ncols + (COLS - view->cols);
688 if (wresize(view->window, nlines, ncols) == ERR)
689 return got_error_from_errno("wresize");
690 if (replace_panel(view->panel, view->window) == ERR)
691 return got_error_from_errno("replace_panel");
692 wclear(view->window);
694 view->nlines = nlines;
695 view->ncols = ncols;
696 view->lines = LINES;
697 view->cols = COLS;
699 if (view->child) {
700 view->child->begin_x = view_split_begin_x(view->begin_x);
701 if (view->child->begin_x == 0) {
702 view_fullscreen(view->child);
703 if (view->child->focussed)
704 show_panel(view->child->panel);
705 else
706 show_panel(view->panel);
707 } else {
708 view_splitscreen(view->child);
709 show_panel(view->child->panel);
713 return NULL;
716 static const struct got_error *
717 view_close_child(struct tog_view *view)
719 const struct got_error *err = NULL;
721 if (view->child == NULL)
722 return NULL;
724 err = view_close(view->child);
725 view->child = NULL;
726 return err;
729 static void
730 view_set_child(struct tog_view *view, struct tog_view *child)
732 view->child = child;
733 child->parent = view;
736 static int
737 view_is_splitscreen(struct tog_view *view)
739 return view->begin_x > 0;
742 static void
743 tog_resizeterm(void)
745 int cols, lines;
746 struct winsize size;
748 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
749 cols = 80; /* Default */
750 lines = 24;
751 } else {
752 cols = size.ws_col;
753 lines = size.ws_row;
755 resize_term(lines, cols);
758 static const struct got_error *
759 view_search_start(struct tog_view *view)
761 const struct got_error *err = NULL;
762 char pattern[1024];
763 int ret;
765 if (view->nlines < 1)
766 return NULL;
768 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
769 wclrtoeol(view->window);
771 nocbreak();
772 echo();
773 ret = wgetnstr(view->window, pattern, sizeof(pattern));
774 cbreak();
775 noecho();
776 if (ret == ERR)
777 return NULL;
779 if (view->searching) {
780 regfree(&view->regex);
781 view->searching = 0;
784 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
785 err = view->search_start(view);
786 if (err) {
787 regfree(&view->regex);
788 return err;
790 view->searching = TOG_SEARCH_FORWARD;
791 view->search_next_done = 0;
792 view->search_next(view);
795 return NULL;
798 static const struct got_error *
799 view_input(struct tog_view **new, int *done, struct tog_view *view,
800 struct tog_view_list_head *views)
802 const struct got_error *err = NULL;
803 struct tog_view *v;
804 int ch, errcode;
806 *new = NULL;
808 /* Clear "no matches" indicator. */
809 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
810 view->search_next_done == TOG_SEARCH_HAVE_NONE)
811 view->search_next_done = TOG_SEARCH_HAVE_MORE;
813 if (view->searching && !view->search_next_done) {
814 errcode = pthread_mutex_unlock(&tog_mutex);
815 if (errcode)
816 return got_error_set_errno(errcode,
817 "pthread_mutex_unlock");
818 pthread_yield();
819 errcode = pthread_mutex_lock(&tog_mutex);
820 if (errcode)
821 return got_error_set_errno(errcode,
822 "pthread_mutex_lock");
823 view->search_next(view);
824 return NULL;
827 nodelay(stdscr, FALSE);
828 /* Allow threads to make progress while we are waiting for input. */
829 errcode = pthread_mutex_unlock(&tog_mutex);
830 if (errcode)
831 return got_error_set_errno(errcode, "pthread_mutex_unlock");
832 ch = wgetch(view->window);
833 errcode = pthread_mutex_lock(&tog_mutex);
834 if (errcode)
835 return got_error_set_errno(errcode, "pthread_mutex_lock");
836 nodelay(stdscr, TRUE);
838 if (tog_sigwinch_received || tog_sigcont_received) {
839 tog_resizeterm();
840 tog_sigwinch_received = 0;
841 tog_sigcont_received = 0;
842 TAILQ_FOREACH(v, views, entry) {
843 err = view_resize(v);
844 if (err)
845 return err;
846 err = v->input(new, v, KEY_RESIZE);
847 if (err)
848 return err;
849 if (v->child) {
850 err = view_resize(v->child);
851 if (err)
852 return err;
853 err = v->child->input(new, v->child,
854 KEY_RESIZE);
855 if (err)
856 return err;
861 switch (ch) {
862 case ERR:
863 break;
864 case '\t':
865 if (view->child) {
866 view->focussed = 0;
867 view->child->focussed = 1;
868 view->focus_child = 1;
869 } else if (view->parent) {
870 view->focussed = 0;
871 view->parent->focussed = 1;
872 view->parent->focus_child = 0;
874 break;
875 case 'q':
876 err = view->input(new, view, ch);
877 view->dying = 1;
878 break;
879 case 'Q':
880 *done = 1;
881 break;
882 case 'f':
883 if (view_is_parent_view(view)) {
884 if (view->child == NULL)
885 break;
886 if (view_is_splitscreen(view->child)) {
887 view->focussed = 0;
888 view->child->focussed = 1;
889 err = view_fullscreen(view->child);
890 } else
891 err = view_splitscreen(view->child);
892 if (err)
893 break;
894 err = view->child->input(new, view->child,
895 KEY_RESIZE);
896 } else {
897 if (view_is_splitscreen(view)) {
898 view->parent->focussed = 0;
899 view->focussed = 1;
900 err = view_fullscreen(view);
901 } else {
902 err = view_splitscreen(view);
904 if (err)
905 break;
906 err = view->input(new, view, KEY_RESIZE);
908 break;
909 case KEY_RESIZE:
910 break;
911 case '/':
912 if (view->search_start)
913 view_search_start(view);
914 else
915 err = view->input(new, view, ch);
916 break;
917 case 'N':
918 case 'n':
919 if (view->search_next) {
920 view->searching = (ch == 'n' ?
921 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
922 view->search_next_done = 0;
923 view->search_next(view);
924 } else
925 err = view->input(new, view, ch);
926 break;
927 default:
928 err = view->input(new, view, ch);
929 break;
932 return err;
935 void
936 view_vborder(struct tog_view *view)
938 PANEL *panel;
939 const struct tog_view *view_above;
941 if (view->parent)
942 return view_vborder(view->parent);
944 panel = panel_above(view->panel);
945 if (panel == NULL)
946 return;
948 view_above = panel_userptr(panel);
949 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
950 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
953 int
954 view_needs_focus_indication(struct tog_view *view)
956 if (view_is_parent_view(view)) {
957 if (view->child == NULL || view->child->focussed)
958 return 0;
959 if (!view_is_splitscreen(view->child))
960 return 0;
961 } else if (!view_is_splitscreen(view))
962 return 0;
964 return view->focussed;
967 static const struct got_error *
968 view_loop(struct tog_view *view)
970 const struct got_error *err = NULL;
971 struct tog_view_list_head views;
972 struct tog_view *new_view;
973 int fast_refresh = 10;
974 int done = 0, errcode;
976 errcode = pthread_mutex_lock(&tog_mutex);
977 if (errcode)
978 return got_error_set_errno(errcode, "pthread_mutex_lock");
980 TAILQ_INIT(&views);
981 TAILQ_INSERT_HEAD(&views, view, entry);
983 view->focussed = 1;
984 err = view->show(view);
985 if (err)
986 return err;
987 update_panels();
988 doupdate();
989 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
990 /* Refresh fast during initialization, then become slower. */
991 if (fast_refresh && fast_refresh-- == 0)
992 halfdelay(10); /* switch to once per second */
994 err = view_input(&new_view, &done, view, &views);
995 if (err)
996 break;
997 if (view->dying) {
998 struct tog_view *v, *prev = NULL;
1000 if (view_is_parent_view(view))
1001 prev = TAILQ_PREV(view, tog_view_list_head,
1002 entry);
1003 else if (view->parent)
1004 prev = view->parent;
1006 if (view->parent) {
1007 view->parent->child = NULL;
1008 view->parent->focus_child = 0;
1009 } else
1010 TAILQ_REMOVE(&views, view, entry);
1012 err = view_close(view);
1013 if (err)
1014 goto done;
1016 view = NULL;
1017 TAILQ_FOREACH(v, &views, entry) {
1018 if (v->focussed)
1019 break;
1021 if (view == NULL && new_view == NULL) {
1022 /* No view has focus. Try to pick one. */
1023 if (prev)
1024 view = prev;
1025 else if (!TAILQ_EMPTY(&views)) {
1026 view = TAILQ_LAST(&views,
1027 tog_view_list_head);
1029 if (view) {
1030 if (view->focus_child) {
1031 view->child->focussed = 1;
1032 view = view->child;
1033 } else
1034 view->focussed = 1;
1038 if (new_view) {
1039 struct tog_view *v, *t;
1040 /* Only allow one parent view per type. */
1041 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1042 if (v->type != new_view->type)
1043 continue;
1044 TAILQ_REMOVE(&views, v, entry);
1045 err = view_close(v);
1046 if (err)
1047 goto done;
1048 break;
1050 TAILQ_INSERT_TAIL(&views, new_view, entry);
1051 view = new_view;
1053 if (view) {
1054 if (view_is_parent_view(view)) {
1055 if (view->child && view->child->focussed)
1056 view = view->child;
1057 } else {
1058 if (view->parent && view->parent->focussed)
1059 view = view->parent;
1061 show_panel(view->panel);
1062 if (view->child && view_is_splitscreen(view->child))
1063 show_panel(view->child->panel);
1064 if (view->parent && view_is_splitscreen(view)) {
1065 err = view->parent->show(view->parent);
1066 if (err)
1067 goto done;
1069 err = view->show(view);
1070 if (err)
1071 goto done;
1072 if (view->child) {
1073 err = view->child->show(view->child);
1074 if (err)
1075 goto done;
1077 update_panels();
1078 doupdate();
1081 done:
1082 while (!TAILQ_EMPTY(&views)) {
1083 view = TAILQ_FIRST(&views);
1084 TAILQ_REMOVE(&views, view, entry);
1085 view_close(view);
1088 errcode = pthread_mutex_unlock(&tog_mutex);
1089 if (errcode && err == NULL)
1090 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1092 return err;
1095 __dead static void
1096 usage_log(void)
1098 endwin();
1099 fprintf(stderr,
1100 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1101 getprogname());
1102 exit(1);
1105 /* Create newly allocated wide-character string equivalent to a byte string. */
1106 static const struct got_error *
1107 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1109 char *vis = NULL;
1110 const struct got_error *err = NULL;
1112 *ws = NULL;
1113 *wlen = mbstowcs(NULL, s, 0);
1114 if (*wlen == (size_t)-1) {
1115 int vislen;
1116 if (errno != EILSEQ)
1117 return got_error_from_errno("mbstowcs");
1119 /* byte string invalid in current encoding; try to "fix" it */
1120 err = got_mbsavis(&vis, &vislen, s);
1121 if (err)
1122 return err;
1123 *wlen = mbstowcs(NULL, vis, 0);
1124 if (*wlen == (size_t)-1) {
1125 err = got_error_from_errno("mbstowcs"); /* give up */
1126 goto done;
1130 *ws = calloc(*wlen + 1, sizeof(**ws));
1131 if (*ws == NULL) {
1132 err = got_error_from_errno("calloc");
1133 goto done;
1136 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1137 err = got_error_from_errno("mbstowcs");
1138 done:
1139 free(vis);
1140 if (err) {
1141 free(*ws);
1142 *ws = NULL;
1143 *wlen = 0;
1145 return err;
1148 /* Format a line for display, ensuring that it won't overflow a width limit. */
1149 static const struct got_error *
1150 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1151 int col_tab_align)
1153 const struct got_error *err = NULL;
1154 int cols = 0;
1155 wchar_t *wline = NULL;
1156 size_t wlen;
1157 int i;
1159 *wlinep = NULL;
1160 *widthp = 0;
1162 err = mbs2ws(&wline, &wlen, line);
1163 if (err)
1164 return err;
1166 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1167 wline[wlen - 1] = L'\0';
1168 wlen--;
1170 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1171 wline[wlen - 1] = L'\0';
1172 wlen--;
1175 i = 0;
1176 while (i < wlen) {
1177 int width = wcwidth(wline[i]);
1179 if (width == 0) {
1180 i++;
1181 continue;
1184 if (width == 1 || width == 2) {
1185 if (cols + width > wlimit)
1186 break;
1187 cols += width;
1188 i++;
1189 } else if (width == -1) {
1190 if (wline[i] == L'\t') {
1191 width = TABSIZE -
1192 ((cols + col_tab_align) % TABSIZE);
1193 } else {
1194 width = 1;
1195 wline[i] = L'.';
1197 if (cols + width > wlimit)
1198 break;
1199 cols += width;
1200 i++;
1201 } else {
1202 err = got_error_from_errno("wcwidth");
1203 goto done;
1206 wline[i] = L'\0';
1207 if (widthp)
1208 *widthp = cols;
1209 done:
1210 if (err)
1211 free(wline);
1212 else
1213 *wlinep = wline;
1214 return err;
1217 static const struct got_error*
1218 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1219 struct got_object_id *id, struct got_repository *repo)
1221 static const struct got_error *err = NULL;
1222 struct got_reflist_entry *re;
1223 char *s;
1224 const char *name;
1226 *refs_str = NULL;
1228 SIMPLEQ_FOREACH(re, refs, entry) {
1229 struct got_tag_object *tag = NULL;
1230 struct got_object_id *ref_id;
1231 int cmp;
1233 name = got_ref_get_name(re->ref);
1234 if (strcmp(name, GOT_REF_HEAD) == 0)
1235 continue;
1236 if (strncmp(name, "refs/", 5) == 0)
1237 name += 5;
1238 if (strncmp(name, "got/", 4) == 0)
1239 continue;
1240 if (strncmp(name, "heads/", 6) == 0)
1241 name += 6;
1242 if (strncmp(name, "remotes/", 8) == 0) {
1243 name += 8;
1244 s = strstr(name, "/" GOT_REF_HEAD);
1245 if (s != NULL && s[strlen(s)] == '\0')
1246 continue;
1248 err = got_ref_resolve(&ref_id, repo, re->ref);
1249 if (err)
1250 break;
1251 if (strncmp(name, "tags/", 5) == 0) {
1252 err = got_object_open_as_tag(&tag, repo, ref_id);
1253 if (err) {
1254 if (err->code != GOT_ERR_OBJ_TYPE) {
1255 free(ref_id);
1256 break;
1258 /* Ref points at something other than a tag. */
1259 err = NULL;
1260 tag = NULL;
1263 cmp = got_object_id_cmp(tag ?
1264 got_object_tag_get_object_id(tag) : ref_id, id);
1265 free(ref_id);
1266 if (tag)
1267 got_object_tag_close(tag);
1268 if (cmp != 0)
1269 continue;
1270 s = *refs_str;
1271 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1272 s ? ", " : "", name) == -1) {
1273 err = got_error_from_errno("asprintf");
1274 free(s);
1275 *refs_str = NULL;
1276 break;
1278 free(s);
1281 return err;
1284 static const struct got_error *
1285 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1286 int col_tab_align)
1288 char *smallerthan, *at;
1290 smallerthan = strchr(author, '<');
1291 if (smallerthan && smallerthan[1] != '\0')
1292 author = smallerthan + 1;
1293 at = strchr(author, '@');
1294 if (at)
1295 *at = '\0';
1296 return format_line(wauthor, author_width, author, limit, col_tab_align);
1299 static const struct got_error *
1300 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1301 struct got_object_id *id, const size_t date_display_cols,
1302 int author_display_cols)
1304 struct tog_log_view_state *s = &view->state.log;
1305 const struct got_error *err = NULL;
1306 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1307 char *logmsg0 = NULL, *logmsg = NULL;
1308 char *author = NULL;
1309 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1310 int author_width, logmsg_width;
1311 char *newline, *line = NULL;
1312 int col, limit;
1313 const int avail = view->ncols;
1314 struct tm tm;
1315 time_t committer_time;
1316 struct tog_color *tc;
1318 committer_time = got_object_commit_get_committer_time(commit);
1319 if (localtime_r(&committer_time, &tm) == NULL)
1320 return got_error_from_errno("localtime_r");
1321 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1322 >= sizeof(datebuf))
1323 return got_error(GOT_ERR_NO_SPACE);
1325 if (avail <= date_display_cols)
1326 limit = MIN(sizeof(datebuf) - 1, avail);
1327 else
1328 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1329 tc = get_color(&s->colors, TOG_COLOR_DATE);
1330 if (tc)
1331 wattr_on(view->window,
1332 COLOR_PAIR(tc->colorpair), NULL);
1333 waddnstr(view->window, datebuf, limit);
1334 if (tc)
1335 wattr_off(view->window,
1336 COLOR_PAIR(tc->colorpair), NULL);
1337 col = limit;
1338 if (col > avail)
1339 goto done;
1341 if (avail >= 120) {
1342 char *id_str;
1343 err = got_object_id_str(&id_str, id);
1344 if (err)
1345 goto done;
1346 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1347 if (tc)
1348 wattr_on(view->window,
1349 COLOR_PAIR(tc->colorpair), NULL);
1350 wprintw(view->window, "%.8s ", id_str);
1351 if (tc)
1352 wattr_off(view->window,
1353 COLOR_PAIR(tc->colorpair), NULL);
1354 free(id_str);
1355 col += 9;
1356 if (col > avail)
1357 goto done;
1360 author = strdup(got_object_commit_get_author(commit));
1361 if (author == NULL) {
1362 err = got_error_from_errno("strdup");
1363 goto done;
1365 err = format_author(&wauthor, &author_width, author, avail - col, col);
1366 if (err)
1367 goto done;
1368 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1369 if (tc)
1370 wattr_on(view->window,
1371 COLOR_PAIR(tc->colorpair), NULL);
1372 waddwstr(view->window, wauthor);
1373 if (tc)
1374 wattr_off(view->window,
1375 COLOR_PAIR(tc->colorpair), NULL);
1376 col += author_width;
1377 while (col < avail && author_width < author_display_cols + 2) {
1378 waddch(view->window, ' ');
1379 col++;
1380 author_width++;
1382 if (col > avail)
1383 goto done;
1385 err = got_object_commit_get_logmsg(&logmsg0, commit);
1386 if (err)
1387 goto done;
1388 logmsg = logmsg0;
1389 while (*logmsg == '\n')
1390 logmsg++;
1391 newline = strchr(logmsg, '\n');
1392 if (newline)
1393 *newline = '\0';
1394 limit = avail - col;
1395 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1396 if (err)
1397 goto done;
1398 waddwstr(view->window, wlogmsg);
1399 col += logmsg_width;
1400 while (col < avail) {
1401 waddch(view->window, ' ');
1402 col++;
1404 done:
1405 free(logmsg0);
1406 free(wlogmsg);
1407 free(author);
1408 free(wauthor);
1409 free(line);
1410 return err;
1413 static struct commit_queue_entry *
1414 alloc_commit_queue_entry(struct got_commit_object *commit,
1415 struct got_object_id *id)
1417 struct commit_queue_entry *entry;
1419 entry = calloc(1, sizeof(*entry));
1420 if (entry == NULL)
1421 return NULL;
1423 entry->id = id;
1424 entry->commit = commit;
1425 return entry;
1428 static void
1429 pop_commit(struct commit_queue *commits)
1431 struct commit_queue_entry *entry;
1433 entry = TAILQ_FIRST(&commits->head);
1434 TAILQ_REMOVE(&commits->head, entry, entry);
1435 got_object_commit_close(entry->commit);
1436 commits->ncommits--;
1437 /* Don't free entry->id! It is owned by the commit graph. */
1438 free(entry);
1441 static void
1442 free_commits(struct commit_queue *commits)
1444 while (!TAILQ_EMPTY(&commits->head))
1445 pop_commit(commits);
1448 static const struct got_error *
1449 match_commit(int *have_match, struct got_object_id *id,
1450 struct got_commit_object *commit, regex_t *regex)
1452 const struct got_error *err = NULL;
1453 regmatch_t regmatch;
1454 char *id_str = NULL, *logmsg = NULL;
1456 *have_match = 0;
1458 err = got_object_id_str(&id_str, id);
1459 if (err)
1460 return err;
1462 err = got_object_commit_get_logmsg(&logmsg, commit);
1463 if (err)
1464 goto done;
1466 if (regexec(regex, got_object_commit_get_author(commit), 1,
1467 &regmatch, 0) == 0 ||
1468 regexec(regex, got_object_commit_get_committer(commit), 1,
1469 &regmatch, 0) == 0 ||
1470 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1471 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1472 *have_match = 1;
1473 done:
1474 free(id_str);
1475 free(logmsg);
1476 return err;
1479 static const struct got_error *
1480 queue_commits(struct tog_log_thread_args *a)
1482 const struct got_error *err = NULL;
1485 * We keep all commits open throughout the lifetime of the log
1486 * view in order to avoid having to re-fetch commits from disk
1487 * while updating the display.
1489 do {
1490 struct got_object_id *id;
1491 struct got_commit_object *commit;
1492 struct commit_queue_entry *entry;
1493 int errcode;
1495 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1496 NULL, NULL);
1497 if (err || id == NULL)
1498 break;
1500 err = got_object_open_as_commit(&commit, a->repo, id);
1501 if (err)
1502 break;
1503 entry = alloc_commit_queue_entry(commit, id);
1504 if (entry == NULL) {
1505 err = got_error_from_errno("alloc_commit_queue_entry");
1506 break;
1509 errcode = pthread_mutex_lock(&tog_mutex);
1510 if (errcode) {
1511 err = got_error_set_errno(errcode,
1512 "pthread_mutex_lock");
1513 break;
1516 entry->idx = a->commits->ncommits;
1517 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1518 a->commits->ncommits++;
1520 if (*a->searching == TOG_SEARCH_FORWARD &&
1521 !*a->search_next_done) {
1522 int have_match;
1523 err = match_commit(&have_match, id, commit, a->regex);
1524 if (err)
1525 break;
1526 if (have_match)
1527 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1530 errcode = pthread_mutex_unlock(&tog_mutex);
1531 if (errcode && err == NULL)
1532 err = got_error_set_errno(errcode,
1533 "pthread_mutex_unlock");
1534 if (err)
1535 break;
1536 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1538 return err;
1541 static void
1542 select_commit(struct tog_log_view_state *s)
1544 struct commit_queue_entry *entry;
1545 int ncommits = 0;
1547 entry = s->first_displayed_entry;
1548 while (entry) {
1549 if (ncommits == s->selected) {
1550 s->selected_entry = entry;
1551 break;
1553 entry = TAILQ_NEXT(entry, entry);
1554 ncommits++;
1558 static const struct got_error *
1559 draw_commits(struct tog_view *view)
1561 const struct got_error *err = NULL;
1562 struct tog_log_view_state *s = &view->state.log;
1563 struct commit_queue_entry *entry = s->selected_entry;
1564 const int limit = view->nlines;
1565 int width;
1566 int ncommits, author_cols = 4;
1567 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1568 char *refs_str = NULL;
1569 wchar_t *wline;
1570 struct tog_color *tc;
1571 static const size_t date_display_cols = 12;
1573 if (s->selected_entry &&
1574 !(view->searching && view->search_next_done == 0)) {
1575 err = got_object_id_str(&id_str, s->selected_entry->id);
1576 if (err)
1577 return err;
1578 err = build_refs_str(&refs_str, &s->refs,
1579 s->selected_entry->id, s->repo);
1580 if (err)
1581 goto done;
1584 if (s->thread_args.commits_needed == 0)
1585 halfdelay(10); /* disable fast refresh */
1587 if (s->thread_args.commits_needed > 0) {
1588 if (asprintf(&ncommits_str, " [%d/%d] %s",
1589 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1590 (view->searching && !view->search_next_done) ?
1591 "searching..." : "loading...") == -1) {
1592 err = got_error_from_errno("asprintf");
1593 goto done;
1595 } else {
1596 const char *search_str = NULL;
1598 if (view->searching) {
1599 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1600 search_str = "no more matches";
1601 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1602 search_str = "no matches found";
1603 else if (!view->search_next_done)
1604 search_str = "searching...";
1607 if (asprintf(&ncommits_str, " [%d/%d] %s",
1608 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1609 search_str ? search_str :
1610 (refs_str ? refs_str : "")) == -1) {
1611 err = got_error_from_errno("asprintf");
1612 goto done;
1616 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1617 if (asprintf(&header, "commit %s %s%s",
1618 id_str ? id_str : "........................................",
1619 s->in_repo_path, ncommits_str) == -1) {
1620 err = got_error_from_errno("asprintf");
1621 header = NULL;
1622 goto done;
1624 } else if (asprintf(&header, "commit %s%s",
1625 id_str ? id_str : "........................................",
1626 ncommits_str) == -1) {
1627 err = got_error_from_errno("asprintf");
1628 header = NULL;
1629 goto done;
1631 err = format_line(&wline, &width, header, view->ncols, 0);
1632 if (err)
1633 goto done;
1635 werase(view->window);
1637 if (view_needs_focus_indication(view))
1638 wstandout(view->window);
1639 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1640 if (tc)
1641 wattr_on(view->window,
1642 COLOR_PAIR(tc->colorpair), NULL);
1643 waddwstr(view->window, wline);
1644 if (tc)
1645 wattr_off(view->window,
1646 COLOR_PAIR(tc->colorpair), NULL);
1647 while (width < view->ncols) {
1648 waddch(view->window, ' ');
1649 width++;
1651 if (view_needs_focus_indication(view))
1652 wstandend(view->window);
1653 free(wline);
1654 if (limit <= 1)
1655 goto done;
1657 /* Grow author column size if necessary. */
1658 entry = s->first_displayed_entry;
1659 ncommits = 0;
1660 while (entry) {
1661 char *author;
1662 wchar_t *wauthor;
1663 int width;
1664 if (ncommits >= limit - 1)
1665 break;
1666 author = strdup(got_object_commit_get_author(entry->commit));
1667 if (author == NULL) {
1668 err = got_error_from_errno("strdup");
1669 goto done;
1671 err = format_author(&wauthor, &width, author, COLS,
1672 date_display_cols);
1673 if (author_cols < width)
1674 author_cols = width;
1675 free(wauthor);
1676 free(author);
1677 ncommits++;
1678 entry = TAILQ_NEXT(entry, entry);
1681 entry = s->first_displayed_entry;
1682 s->last_displayed_entry = s->first_displayed_entry;
1683 ncommits = 0;
1684 while (entry) {
1685 if (ncommits >= limit - 1)
1686 break;
1687 if (ncommits == s->selected)
1688 wstandout(view->window);
1689 err = draw_commit(view, entry->commit, entry->id,
1690 date_display_cols, author_cols);
1691 if (ncommits == s->selected)
1692 wstandend(view->window);
1693 if (err)
1694 goto done;
1695 ncommits++;
1696 s->last_displayed_entry = entry;
1697 entry = TAILQ_NEXT(entry, entry);
1700 view_vborder(view);
1701 done:
1702 free(id_str);
1703 free(refs_str);
1704 free(ncommits_str);
1705 free(header);
1706 return err;
1709 static void
1710 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1712 struct commit_queue_entry *entry;
1713 int nscrolled = 0;
1715 entry = TAILQ_FIRST(&s->commits.head);
1716 if (s->first_displayed_entry == entry)
1717 return;
1719 entry = s->first_displayed_entry;
1720 while (entry && nscrolled < maxscroll) {
1721 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1722 if (entry) {
1723 s->first_displayed_entry = entry;
1724 nscrolled++;
1729 static const struct got_error *
1730 trigger_log_thread(struct tog_view *view, int wait)
1732 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1733 int errcode;
1735 halfdelay(1); /* fast refresh while loading commits */
1737 while (ta->commits_needed > 0) {
1738 if (ta->log_complete)
1739 break;
1741 /* Wake the log thread. */
1742 errcode = pthread_cond_signal(&ta->need_commits);
1743 if (errcode)
1744 return got_error_set_errno(errcode,
1745 "pthread_cond_signal");
1748 * The mutex will be released while the view loop waits
1749 * in wgetch(), at which time the log thread will run.
1751 if (!wait)
1752 break;
1754 /* Display progress update in log view. */
1755 show_log_view(view);
1756 update_panels();
1757 doupdate();
1759 /* Wait right here while next commit is being loaded. */
1760 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1761 if (errcode)
1762 return got_error_set_errno(errcode,
1763 "pthread_cond_wait");
1765 /* Display progress update in log view. */
1766 show_log_view(view);
1767 update_panels();
1768 doupdate();
1771 return NULL;
1774 static const struct got_error *
1775 log_scroll_down(struct tog_view *view, int maxscroll)
1777 struct tog_log_view_state *s = &view->state.log;
1778 const struct got_error *err = NULL;
1779 struct commit_queue_entry *pentry;
1780 int nscrolled = 0, ncommits_needed;
1782 if (s->last_displayed_entry == NULL)
1783 return NULL;
1785 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1786 if (s->commits.ncommits < ncommits_needed &&
1787 !s->thread_args.log_complete) {
1789 * Ask the log thread for required amount of commits.
1791 s->thread_args.commits_needed += maxscroll;
1792 err = trigger_log_thread(view, 1);
1793 if (err)
1794 return err;
1797 do {
1798 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1799 if (pentry == NULL)
1800 break;
1802 s->last_displayed_entry = pentry;
1804 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1805 if (pentry == NULL)
1806 break;
1807 s->first_displayed_entry = pentry;
1808 } while (++nscrolled < maxscroll);
1810 return err;
1813 static const struct got_error *
1814 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1815 struct got_commit_object *commit, struct got_object_id *commit_id,
1816 struct tog_view *log_view, struct got_repository *repo)
1818 const struct got_error *err;
1819 struct got_object_qid *parent_id;
1820 struct tog_view *diff_view;
1822 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1823 if (diff_view == NULL)
1824 return got_error_from_errno("view_open");
1826 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1827 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1828 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1829 if (err == NULL)
1830 *new_view = diff_view;
1831 return err;
1834 static const struct got_error *
1835 tree_view_visit_subtree(struct tog_tree_view_state *s,
1836 struct got_tree_object *subtree)
1838 struct tog_parent_tree *parent;
1840 parent = calloc(1, sizeof(*parent));
1841 if (parent == NULL)
1842 return got_error_from_errno("calloc");
1844 parent->tree = s->tree;
1845 parent->first_displayed_entry = s->first_displayed_entry;
1846 parent->selected_entry = s->selected_entry;
1847 parent->selected = s->selected;
1848 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1849 s->tree = subtree;
1850 s->selected = 0;
1851 s->first_displayed_entry = NULL;
1852 return NULL;
1855 static const struct got_error *
1856 tree_view_walk_path(struct tog_tree_view_state *s,
1857 struct got_object_id *commit_id, const char *path)
1859 const struct got_error *err = NULL;
1860 struct got_tree_object *tree = NULL;
1861 const char *p;
1862 char *slash, *subpath = NULL;
1864 /* Walk the path and open corresponding tree objects. */
1865 p = path;
1866 while (*p) {
1867 struct got_tree_entry *te;
1868 struct got_object_id *tree_id;
1869 char *te_name;
1871 while (p[0] == '/')
1872 p++;
1874 /* Ensure the correct subtree entry is selected. */
1875 slash = strchr(p, '/');
1876 if (slash == NULL)
1877 te_name = strdup(p);
1878 else
1879 te_name = strndup(p, slash - p);
1880 if (te_name == NULL) {
1881 err = got_error_from_errno("strndup");
1882 break;
1884 te = got_object_tree_find_entry(s->tree, te_name);
1885 if (te == NULL) {
1886 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1887 free(te_name);
1888 break;
1890 free(te_name);
1891 s->first_displayed_entry = s->selected_entry = te;
1893 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1894 break; /* jump to this file's entry */
1896 slash = strchr(p, '/');
1897 if (slash)
1898 subpath = strndup(path, slash - path);
1899 else
1900 subpath = strdup(path);
1901 if (subpath == NULL) {
1902 err = got_error_from_errno("strdup");
1903 break;
1906 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1907 subpath);
1908 if (err)
1909 break;
1911 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1912 free(tree_id);
1913 if (err)
1914 break;
1916 err = tree_view_visit_subtree(s, tree);
1917 if (err) {
1918 got_object_tree_close(tree);
1919 break;
1921 if (slash == NULL)
1922 break;
1923 free(subpath);
1924 subpath = NULL;
1925 p = slash;
1928 free(subpath);
1929 return err;
1932 static const struct got_error *
1933 browse_commit_tree(struct tog_view **new_view, int begin_x,
1934 struct commit_queue_entry *entry, const char *path,
1935 const char *head_ref_name, struct got_repository *repo)
1937 const struct got_error *err = NULL;
1938 struct got_tree_object *tree;
1939 struct tog_tree_view_state *s;
1940 struct tog_view *tree_view;
1942 err = got_object_open_as_tree(&tree, repo,
1943 got_object_commit_get_tree_id(entry->commit));
1944 if (err)
1945 return err;
1947 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1948 if (tree_view == NULL)
1949 return got_error_from_errno("view_open");
1951 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1952 if (err) {
1953 got_object_tree_close(tree);
1954 return err;
1956 s = &tree_view->state.tree;
1958 *new_view = tree_view;
1960 if (got_path_is_root_dir(path))
1961 return NULL;
1963 return tree_view_walk_path(s, entry->id, path);
1966 static const struct got_error *
1967 block_signals_used_by_main_thread(void)
1969 sigset_t sigset;
1970 int errcode;
1972 if (sigemptyset(&sigset) == -1)
1973 return got_error_from_errno("sigemptyset");
1975 /* tog handles SIGWINCH and SIGCONT */
1976 if (sigaddset(&sigset, SIGWINCH) == -1)
1977 return got_error_from_errno("sigaddset");
1978 if (sigaddset(&sigset, SIGCONT) == -1)
1979 return got_error_from_errno("sigaddset");
1981 /* ncurses handles SIGTSTP */
1982 if (sigaddset(&sigset, SIGTSTP) == -1)
1983 return got_error_from_errno("sigaddset");
1985 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1986 if (errcode)
1987 return got_error_set_errno(errcode, "pthread_sigmask");
1989 return NULL;
1992 static void *
1993 log_thread(void *arg)
1995 const struct got_error *err = NULL;
1996 int errcode = 0;
1997 struct tog_log_thread_args *a = arg;
1998 int done = 0;
2000 err = block_signals_used_by_main_thread();
2001 if (err)
2002 return (void *)err;
2004 while (!done && !err && !tog_sigpipe_received) {
2005 err = queue_commits(a);
2006 if (err) {
2007 if (err->code != GOT_ERR_ITER_COMPLETED)
2008 return (void *)err;
2009 err = NULL;
2010 done = 1;
2011 } else if (a->commits_needed > 0)
2012 a->commits_needed--;
2014 errcode = pthread_mutex_lock(&tog_mutex);
2015 if (errcode) {
2016 err = got_error_set_errno(errcode,
2017 "pthread_mutex_lock");
2018 break;
2019 } else if (*a->quit)
2020 done = 1;
2021 else if (*a->first_displayed_entry == NULL) {
2022 *a->first_displayed_entry =
2023 TAILQ_FIRST(&a->commits->head);
2024 *a->selected_entry = *a->first_displayed_entry;
2027 errcode = pthread_cond_signal(&a->commit_loaded);
2028 if (errcode) {
2029 err = got_error_set_errno(errcode,
2030 "pthread_cond_signal");
2031 pthread_mutex_unlock(&tog_mutex);
2032 break;
2035 if (done)
2036 a->commits_needed = 0;
2037 else {
2038 if (a->commits_needed == 0) {
2039 errcode = pthread_cond_wait(&a->need_commits,
2040 &tog_mutex);
2041 if (errcode)
2042 err = got_error_set_errno(errcode,
2043 "pthread_cond_wait");
2044 if (*a->quit)
2045 done = 1;
2049 errcode = pthread_mutex_unlock(&tog_mutex);
2050 if (errcode && err == NULL)
2051 err = got_error_set_errno(errcode,
2052 "pthread_mutex_unlock");
2054 a->log_complete = 1;
2055 return (void *)err;
2058 static const struct got_error *
2059 stop_log_thread(struct tog_log_view_state *s)
2061 const struct got_error *err = NULL;
2062 int errcode;
2064 if (s->thread) {
2065 s->quit = 1;
2066 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2067 if (errcode)
2068 return got_error_set_errno(errcode,
2069 "pthread_cond_signal");
2070 errcode = pthread_mutex_unlock(&tog_mutex);
2071 if (errcode)
2072 return got_error_set_errno(errcode,
2073 "pthread_mutex_unlock");
2074 errcode = pthread_join(s->thread, (void **)&err);
2075 if (errcode)
2076 return got_error_set_errno(errcode, "pthread_join");
2077 errcode = pthread_mutex_lock(&tog_mutex);
2078 if (errcode)
2079 return got_error_set_errno(errcode,
2080 "pthread_mutex_lock");
2081 s->thread = NULL;
2084 if (s->thread_args.repo) {
2085 got_repo_close(s->thread_args.repo);
2086 s->thread_args.repo = NULL;
2089 if (s->thread_args.graph) {
2090 got_commit_graph_close(s->thread_args.graph);
2091 s->thread_args.graph = NULL;
2094 return err;
2097 static const struct got_error *
2098 close_log_view(struct tog_view *view)
2100 const struct got_error *err = NULL;
2101 struct tog_log_view_state *s = &view->state.log;
2102 int errcode;
2104 err = stop_log_thread(s);
2106 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2107 if (errcode && err == NULL)
2108 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2110 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2111 if (errcode && err == NULL)
2112 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2114 free_commits(&s->commits);
2115 free(s->in_repo_path);
2116 s->in_repo_path = NULL;
2117 free(s->start_id);
2118 s->start_id = NULL;
2119 free(s->head_ref_name);
2120 s->head_ref_name = NULL;
2121 got_ref_list_free(&s->refs);
2122 return err;
2125 static const struct got_error *
2126 search_start_log_view(struct tog_view *view)
2128 struct tog_log_view_state *s = &view->state.log;
2130 s->matched_entry = NULL;
2131 s->search_entry = NULL;
2132 return NULL;
2135 static const struct got_error *
2136 search_next_log_view(struct tog_view *view)
2138 const struct got_error *err = NULL;
2139 struct tog_log_view_state *s = &view->state.log;
2140 struct commit_queue_entry *entry;
2142 /* Display progress update in log view. */
2143 show_log_view(view);
2144 update_panels();
2145 doupdate();
2147 if (s->search_entry) {
2148 int errcode, ch;
2149 errcode = pthread_mutex_unlock(&tog_mutex);
2150 if (errcode)
2151 return got_error_set_errno(errcode,
2152 "pthread_mutex_unlock");
2153 ch = wgetch(view->window);
2154 errcode = pthread_mutex_lock(&tog_mutex);
2155 if (errcode)
2156 return got_error_set_errno(errcode,
2157 "pthread_mutex_lock");
2158 if (ch == KEY_BACKSPACE) {
2159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2160 return NULL;
2162 if (view->searching == TOG_SEARCH_FORWARD)
2163 entry = TAILQ_NEXT(s->search_entry, entry);
2164 else
2165 entry = TAILQ_PREV(s->search_entry,
2166 commit_queue_head, entry);
2167 } else if (s->matched_entry) {
2168 if (view->searching == TOG_SEARCH_FORWARD)
2169 entry = TAILQ_NEXT(s->matched_entry, entry);
2170 else
2171 entry = TAILQ_PREV(s->matched_entry,
2172 commit_queue_head, entry);
2173 } else {
2174 if (view->searching == TOG_SEARCH_FORWARD)
2175 entry = TAILQ_FIRST(&s->commits.head);
2176 else
2177 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2180 while (1) {
2181 int have_match = 0;
2183 if (entry == NULL) {
2184 if (s->thread_args.log_complete ||
2185 view->searching == TOG_SEARCH_BACKWARD) {
2186 view->search_next_done =
2187 (s->matched_entry == NULL ?
2188 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2189 s->search_entry = NULL;
2190 return NULL;
2193 * Poke the log thread for more commits and return,
2194 * allowing the main loop to make progress. Search
2195 * will resume at s->search_entry once we come back.
2197 s->thread_args.commits_needed++;
2198 return trigger_log_thread(view, 0);
2201 err = match_commit(&have_match, entry->id, entry->commit,
2202 &view->regex);
2203 if (err)
2204 break;
2205 if (have_match) {
2206 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2207 s->matched_entry = entry;
2208 break;
2211 s->search_entry = entry;
2212 if (view->searching == TOG_SEARCH_FORWARD)
2213 entry = TAILQ_NEXT(entry, entry);
2214 else
2215 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2218 if (s->matched_entry) {
2219 int cur = s->selected_entry->idx;
2220 while (cur < s->matched_entry->idx) {
2221 err = input_log_view(NULL, view, KEY_DOWN);
2222 if (err)
2223 return err;
2224 cur++;
2226 while (cur > s->matched_entry->idx) {
2227 err = input_log_view(NULL, view, KEY_UP);
2228 if (err)
2229 return err;
2230 cur--;
2234 s->search_entry = NULL;
2236 return NULL;
2239 static const struct got_error *
2240 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2241 struct got_repository *repo, const char *head_ref_name,
2242 const char *in_repo_path, int log_branches)
2244 const struct got_error *err = NULL;
2245 struct tog_log_view_state *s = &view->state.log;
2246 struct got_repository *thread_repo = NULL;
2247 struct got_commit_graph *thread_graph = NULL;
2248 int errcode;
2250 SIMPLEQ_INIT(&s->refs);
2252 if (in_repo_path != s->in_repo_path) {
2253 free(s->in_repo_path);
2254 s->in_repo_path = strdup(in_repo_path);
2255 if (s->in_repo_path == NULL)
2256 return got_error_from_errno("strdup");
2259 /* The commit queue only contains commits being displayed. */
2260 TAILQ_INIT(&s->commits.head);
2261 s->commits.ncommits = 0;
2263 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2264 if (err)
2265 goto done;
2267 s->repo = repo;
2268 if (head_ref_name) {
2269 s->head_ref_name = strdup(head_ref_name);
2270 if (s->head_ref_name == NULL) {
2271 err = got_error_from_errno("strdup");
2272 goto done;
2275 s->start_id = got_object_id_dup(start_id);
2276 if (s->start_id == NULL) {
2277 err = got_error_from_errno("got_object_id_dup");
2278 goto done;
2280 s->log_branches = log_branches;
2282 SIMPLEQ_INIT(&s->colors);
2283 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2284 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2285 get_color_value("TOG_COLOR_COMMIT"));
2286 if (err)
2287 goto done;
2288 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2289 get_color_value("TOG_COLOR_AUTHOR"));
2290 if (err) {
2291 free_colors(&s->colors);
2292 goto done;
2294 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2295 get_color_value("TOG_COLOR_DATE"));
2296 if (err) {
2297 free_colors(&s->colors);
2298 goto done;
2302 view->show = show_log_view;
2303 view->input = input_log_view;
2304 view->close = close_log_view;
2305 view->search_start = search_start_log_view;
2306 view->search_next = search_next_log_view;
2308 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2309 if (err)
2310 goto done;
2311 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2312 !s->log_branches);
2313 if (err)
2314 goto done;
2315 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2316 s->repo, NULL, NULL);
2317 if (err)
2318 goto done;
2320 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2321 if (errcode) {
2322 err = got_error_set_errno(errcode, "pthread_cond_init");
2323 goto done;
2325 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2326 if (errcode) {
2327 err = got_error_set_errno(errcode, "pthread_cond_init");
2328 goto done;
2331 s->thread_args.commits_needed = view->nlines;
2332 s->thread_args.graph = thread_graph;
2333 s->thread_args.commits = &s->commits;
2334 s->thread_args.in_repo_path = s->in_repo_path;
2335 s->thread_args.start_id = s->start_id;
2336 s->thread_args.repo = thread_repo;
2337 s->thread_args.log_complete = 0;
2338 s->thread_args.quit = &s->quit;
2339 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2340 s->thread_args.selected_entry = &s->selected_entry;
2341 s->thread_args.searching = &view->searching;
2342 s->thread_args.search_next_done = &view->search_next_done;
2343 s->thread_args.regex = &view->regex;
2344 done:
2345 if (err)
2346 close_log_view(view);
2347 return err;
2350 static const struct got_error *
2351 show_log_view(struct tog_view *view)
2353 const struct got_error *err;
2354 struct tog_log_view_state *s = &view->state.log;
2356 if (s->thread == NULL) {
2357 int errcode = pthread_create(&s->thread, NULL, log_thread,
2358 &s->thread_args);
2359 if (errcode)
2360 return got_error_set_errno(errcode, "pthread_create");
2361 if (s->thread_args.commits_needed > 0) {
2362 err = trigger_log_thread(view, 1);
2363 if (err)
2364 return err;
2368 return draw_commits(view);
2371 static const struct got_error *
2372 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2374 const struct got_error *err = NULL;
2375 struct tog_log_view_state *s = &view->state.log;
2376 struct tog_view *diff_view = NULL, *tree_view = NULL;
2377 struct tog_view *ref_view = NULL;
2378 int begin_x = 0;
2380 switch (ch) {
2381 case 'q':
2382 s->quit = 1;
2383 break;
2384 case 'k':
2385 case KEY_UP:
2386 case '<':
2387 case ',':
2388 if (s->first_displayed_entry == NULL)
2389 break;
2390 if (s->selected > 0)
2391 s->selected--;
2392 else
2393 log_scroll_up(s, 1);
2394 select_commit(s);
2395 break;
2396 case KEY_PPAGE:
2397 case CTRL('b'):
2398 if (s->first_displayed_entry == NULL)
2399 break;
2400 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2401 s->selected = 0;
2402 else
2403 log_scroll_up(s, view->nlines - 1);
2404 select_commit(s);
2405 break;
2406 case 'j':
2407 case KEY_DOWN:
2408 case '>':
2409 case '.':
2410 if (s->first_displayed_entry == NULL)
2411 break;
2412 if (s->selected < MIN(view->nlines - 2,
2413 s->commits.ncommits - 1))
2414 s->selected++;
2415 else {
2416 err = log_scroll_down(view, 1);
2417 if (err)
2418 break;
2420 select_commit(s);
2421 break;
2422 case KEY_NPAGE:
2423 case CTRL('f'): {
2424 struct commit_queue_entry *first;
2425 first = s->first_displayed_entry;
2426 if (first == NULL)
2427 break;
2428 err = log_scroll_down(view, view->nlines - 1);
2429 if (err)
2430 break;
2431 if (first == s->first_displayed_entry &&
2432 s->selected < MIN(view->nlines - 2,
2433 s->commits.ncommits - 1)) {
2434 /* can't scroll further down */
2435 s->selected = MIN(view->nlines - 2,
2436 s->commits.ncommits - 1);
2438 select_commit(s);
2439 break;
2441 case KEY_RESIZE:
2442 if (s->selected > view->nlines - 2)
2443 s->selected = view->nlines - 2;
2444 if (s->selected > s->commits.ncommits - 1)
2445 s->selected = s->commits.ncommits - 1;
2446 select_commit(s);
2447 if (s->commits.ncommits < view->nlines - 1 &&
2448 !s->thread_args.log_complete) {
2449 s->thread_args.commits_needed += (view->nlines - 1) -
2450 s->commits.ncommits;
2451 err = trigger_log_thread(view, 1);
2453 break;
2454 case KEY_ENTER:
2455 case ' ':
2456 case '\r':
2457 if (s->selected_entry == NULL)
2458 break;
2459 if (view_is_parent_view(view))
2460 begin_x = view_split_begin_x(view->begin_x);
2461 err = open_diff_view_for_commit(&diff_view, begin_x,
2462 s->selected_entry->commit, s->selected_entry->id,
2463 view, s->repo);
2464 if (err)
2465 break;
2466 view->focussed = 0;
2467 diff_view->focussed = 1;
2468 if (view_is_parent_view(view)) {
2469 err = view_close_child(view);
2470 if (err)
2471 return err;
2472 view_set_child(view, diff_view);
2473 view->focus_child = 1;
2474 } else
2475 *new_view = diff_view;
2476 break;
2477 case 't':
2478 if (s->selected_entry == NULL)
2479 break;
2480 if (view_is_parent_view(view))
2481 begin_x = view_split_begin_x(view->begin_x);
2482 err = browse_commit_tree(&tree_view, begin_x,
2483 s->selected_entry, s->in_repo_path, s->head_ref_name,
2484 s->repo);
2485 if (err)
2486 break;
2487 view->focussed = 0;
2488 tree_view->focussed = 1;
2489 if (view_is_parent_view(view)) {
2490 err = view_close_child(view);
2491 if (err)
2492 return err;
2493 view_set_child(view, tree_view);
2494 view->focus_child = 1;
2495 } else
2496 *new_view = tree_view;
2497 break;
2498 case KEY_BACKSPACE:
2499 case CTRL('l'):
2500 case 'B':
2501 if (ch == KEY_BACKSPACE &&
2502 got_path_is_root_dir(s->in_repo_path))
2503 break;
2504 err = stop_log_thread(s);
2505 if (err)
2506 return err;
2507 if (ch == KEY_BACKSPACE) {
2508 char *parent_path;
2509 err = got_path_dirname(&parent_path, s->in_repo_path);
2510 if (err)
2511 return err;
2512 free(s->in_repo_path);
2513 s->in_repo_path = parent_path;
2514 s->thread_args.in_repo_path = s->in_repo_path;
2515 } else if (ch == CTRL('l')) {
2516 struct got_object_id *start_id;
2517 err = got_repo_match_object_id(&start_id, NULL,
2518 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2519 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2520 if (err)
2521 return err;
2522 free(s->start_id);
2523 s->start_id = start_id;
2524 s->thread_args.start_id = s->start_id;
2525 } else /* 'B' */
2526 s->log_branches = !s->log_branches;
2528 err = got_repo_open(&s->thread_args.repo,
2529 got_repo_get_path(s->repo), NULL);
2530 if (err)
2531 return err;
2532 got_ref_list_free(&s->refs);
2533 err = got_ref_list(&s->refs, s->repo, NULL,
2534 got_ref_cmp_by_name, NULL);
2535 if (err)
2536 return err;
2537 err = got_commit_graph_open(&s->thread_args.graph,
2538 s->in_repo_path, !s->log_branches);
2539 if (err)
2540 return err;
2541 err = got_commit_graph_iter_start(s->thread_args.graph,
2542 s->start_id, s->repo, NULL, NULL);
2543 if (err)
2544 return err;
2545 free_commits(&s->commits);
2546 s->first_displayed_entry = NULL;
2547 s->last_displayed_entry = NULL;
2548 s->selected_entry = NULL;
2549 s->selected = 0;
2550 s->thread_args.log_complete = 0;
2551 s->quit = 0;
2552 s->thread_args.commits_needed = view->nlines;
2553 break;
2554 case 'r':
2555 if (view_is_parent_view(view))
2556 begin_x = view_split_begin_x(view->begin_x);
2557 ref_view = view_open(view->nlines, view->ncols,
2558 view->begin_y, begin_x, TOG_VIEW_REF);
2559 if (ref_view == NULL)
2560 return got_error_from_errno("view_open");
2561 err = open_ref_view(ref_view, s->repo);
2562 if (err) {
2563 view_close(ref_view);
2564 return err;
2566 view->focussed = 0;
2567 ref_view->focussed = 1;
2568 if (view_is_parent_view(view)) {
2569 err = view_close_child(view);
2570 if (err)
2571 return err;
2572 view_set_child(view, ref_view);
2573 view->focus_child = 1;
2574 } else
2575 *new_view = ref_view;
2576 break;
2577 default:
2578 break;
2581 return err;
2584 static const struct got_error *
2585 apply_unveil(const char *repo_path, const char *worktree_path)
2587 const struct got_error *error;
2589 #ifdef PROFILE
2590 if (unveil("gmon.out", "rwc") != 0)
2591 return got_error_from_errno2("unveil", "gmon.out");
2592 #endif
2593 if (repo_path && unveil(repo_path, "r") != 0)
2594 return got_error_from_errno2("unveil", repo_path);
2596 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2597 return got_error_from_errno2("unveil", worktree_path);
2599 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2600 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2602 error = got_privsep_unveil_exec_helpers();
2603 if (error != NULL)
2604 return error;
2606 if (unveil(NULL, NULL) != 0)
2607 return got_error_from_errno("unveil");
2609 return NULL;
2612 static void
2613 init_curses(void)
2615 initscr();
2616 cbreak();
2617 halfdelay(1); /* Do fast refresh while initial view is loading. */
2618 noecho();
2619 nonl();
2620 intrflush(stdscr, FALSE);
2621 keypad(stdscr, TRUE);
2622 curs_set(0);
2623 if (getenv("TOG_COLORS") != NULL) {
2624 start_color();
2625 use_default_colors();
2627 signal(SIGWINCH, tog_sigwinch);
2628 signal(SIGPIPE, tog_sigpipe);
2629 signal(SIGCONT, tog_sigcont);
2632 static const struct got_error *
2633 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2634 struct got_repository *repo, struct got_worktree *worktree)
2636 const struct got_error *err = NULL;
2638 if (argc == 0) {
2639 *in_repo_path = strdup("/");
2640 if (*in_repo_path == NULL)
2641 return got_error_from_errno("strdup");
2642 return NULL;
2645 if (worktree) {
2646 const char *prefix = got_worktree_get_path_prefix(worktree);
2647 char *p;
2649 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2650 if (err)
2651 return err;
2652 if (asprintf(in_repo_path, "%s%s%s", prefix,
2653 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2654 p) == -1) {
2655 err = got_error_from_errno("asprintf");
2656 *in_repo_path = NULL;
2658 free(p);
2659 } else
2660 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2662 return err;
2665 static const struct got_error *
2666 cmd_log(int argc, char *argv[])
2668 const struct got_error *error;
2669 struct got_repository *repo = NULL;
2670 struct got_worktree *worktree = NULL;
2671 struct got_object_id *start_id = NULL;
2672 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2673 char *start_commit = NULL, *label = NULL;
2674 struct got_reference *ref = NULL;
2675 const char *head_ref_name = NULL;
2676 int ch, log_branches = 0;
2677 struct tog_view *view;
2679 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2680 switch (ch) {
2681 case 'b':
2682 log_branches = 1;
2683 break;
2684 case 'c':
2685 start_commit = optarg;
2686 break;
2687 case 'r':
2688 repo_path = realpath(optarg, NULL);
2689 if (repo_path == NULL)
2690 return got_error_from_errno2("realpath",
2691 optarg);
2692 break;
2693 default:
2694 usage_log();
2695 /* NOTREACHED */
2699 argc -= optind;
2700 argv += optind;
2702 if (argc > 1)
2703 usage_log();
2705 cwd = getcwd(NULL, 0);
2706 if (cwd == NULL)
2707 return got_error_from_errno("getcwd");
2709 error = got_worktree_open(&worktree, cwd);
2710 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2711 goto done;
2713 if (repo_path == NULL) {
2714 if (worktree)
2715 repo_path =
2716 strdup(got_worktree_get_repo_path(worktree));
2717 else
2718 repo_path = strdup(cwd);
2720 if (repo_path == NULL) {
2721 error = got_error_from_errno("strdup");
2722 goto done;
2725 error = got_repo_open(&repo, repo_path, NULL);
2726 if (error != NULL)
2727 goto done;
2729 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2730 repo, worktree);
2731 if (error)
2732 goto done;
2734 init_curses();
2736 error = apply_unveil(got_repo_get_path(repo),
2737 worktree ? got_worktree_get_root_path(worktree) : NULL);
2738 if (error)
2739 goto done;
2741 if (start_commit == NULL) {
2742 error = got_repo_match_object_id(&start_id, &label,
2743 worktree ? got_worktree_get_head_ref_name(worktree) :
2744 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2745 if (error)
2746 goto done;
2747 head_ref_name = label;
2748 } else {
2749 error = got_ref_open(&ref, repo, start_commit, 0);
2750 if (error == NULL)
2751 head_ref_name = got_ref_get_name(ref);
2752 else if (error->code != GOT_ERR_NOT_REF)
2753 goto done;
2754 error = got_repo_match_object_id(&start_id, NULL,
2755 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2756 if (error)
2757 goto done;
2760 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2761 if (view == NULL) {
2762 error = got_error_from_errno("view_open");
2763 goto done;
2765 error = open_log_view(view, start_id, repo, head_ref_name,
2766 in_repo_path, log_branches);
2767 if (error)
2768 goto done;
2769 if (worktree) {
2770 /* Release work tree lock. */
2771 got_worktree_close(worktree);
2772 worktree = NULL;
2774 error = view_loop(view);
2775 done:
2776 free(in_repo_path);
2777 free(repo_path);
2778 free(cwd);
2779 free(start_id);
2780 free(label);
2781 if (ref)
2782 got_ref_close(ref);
2783 if (repo)
2784 got_repo_close(repo);
2785 if (worktree)
2786 got_worktree_close(worktree);
2787 return error;
2790 __dead static void
2791 usage_diff(void)
2793 endwin();
2794 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2795 "[-w] object1 object2\n", getprogname());
2796 exit(1);
2799 static int
2800 match_line(const char *line, regex_t *regex, size_t nmatch,
2801 regmatch_t *regmatch)
2803 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2806 struct tog_color *
2807 match_color(struct tog_colors *colors, const char *line)
2809 struct tog_color *tc = NULL;
2811 SIMPLEQ_FOREACH(tc, colors, entry) {
2812 if (match_line(line, &tc->regex, 0, NULL))
2813 return tc;
2816 return NULL;
2819 static const struct got_error *
2820 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2821 WINDOW *window, regmatch_t *regmatch)
2823 const struct got_error *err = NULL;
2824 wchar_t *wline;
2825 int width;
2826 char *s;
2828 *wtotal = 0;
2830 s = strndup(line, regmatch->rm_so);
2831 if (s == NULL)
2832 return got_error_from_errno("strndup");
2834 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2835 if (err) {
2836 free(s);
2837 return err;
2839 waddwstr(window, wline);
2840 free(wline);
2841 free(s);
2842 wlimit -= width;
2843 *wtotal += width;
2845 if (wlimit > 0) {
2846 s = strndup(line + regmatch->rm_so,
2847 regmatch->rm_eo - regmatch->rm_so);
2848 if (s == NULL) {
2849 err = got_error_from_errno("strndup");
2850 free(s);
2851 return err;
2853 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2854 if (err) {
2855 free(s);
2856 return err;
2858 wattr_on(window, A_STANDOUT, NULL);
2859 waddwstr(window, wline);
2860 wattr_off(window, A_STANDOUT, NULL);
2861 free(wline);
2862 free(s);
2863 wlimit -= width;
2864 *wtotal += width;
2867 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2868 err = format_line(&wline, &width,
2869 line + regmatch->rm_eo, wlimit, col_tab_align);
2870 if (err)
2871 return err;
2872 waddwstr(window, wline);
2873 free(wline);
2874 *wtotal += width;
2877 return NULL;
2880 static const struct got_error *
2881 draw_file(struct tog_view *view, const char *header)
2883 struct tog_diff_view_state *s = &view->state.diff;
2884 regmatch_t *regmatch = &view->regmatch;
2885 const struct got_error *err;
2886 int nprinted = 0;
2887 char *line;
2888 size_t linesize = 0;
2889 ssize_t linelen;
2890 struct tog_color *tc;
2891 wchar_t *wline;
2892 int width;
2893 int max_lines = view->nlines;
2894 int nlines = s->nlines;
2895 off_t line_offset;
2897 line_offset = s->line_offsets[s->first_displayed_line - 1];
2898 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2899 return got_error_from_errno("fseek");
2901 werase(view->window);
2903 if (header) {
2904 if (asprintf(&line, "[%d/%d] %s",
2905 s->first_displayed_line - 1 + s->selected_line, nlines,
2906 header) == -1)
2907 return got_error_from_errno("asprintf");
2908 err = format_line(&wline, &width, line, view->ncols, 0);
2909 free(line);
2910 if (err)
2911 return err;
2913 if (view_needs_focus_indication(view))
2914 wstandout(view->window);
2915 waddwstr(view->window, wline);
2916 free(wline);
2917 wline = NULL;
2918 if (view_needs_focus_indication(view))
2919 wstandend(view->window);
2920 if (width <= view->ncols - 1)
2921 waddch(view->window, '\n');
2923 if (max_lines <= 1)
2924 return NULL;
2925 max_lines--;
2928 s->eof = 0;
2929 line = NULL;
2930 while (max_lines > 0 && nprinted < max_lines) {
2931 linelen = getline(&line, &linesize, s->f);
2932 if (linelen == -1) {
2933 if (feof(s->f)) {
2934 s->eof = 1;
2935 break;
2937 free(line);
2938 return got_ferror(s->f, GOT_ERR_IO);
2941 tc = match_color(&s->colors, line);
2942 if (tc)
2943 wattr_on(view->window,
2944 COLOR_PAIR(tc->colorpair), NULL);
2945 if (s->first_displayed_line + nprinted == s->matched_line &&
2946 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2947 err = add_matched_line(&width, line, view->ncols, 0,
2948 view->window, regmatch);
2949 if (err) {
2950 free(line);
2951 return err;
2953 } else {
2954 err = format_line(&wline, &width, line, view->ncols, 0);
2955 if (err) {
2956 free(line);
2957 return err;
2959 waddwstr(view->window, wline);
2960 free(wline);
2961 wline = NULL;
2963 if (tc)
2964 wattr_off(view->window,
2965 COLOR_PAIR(tc->colorpair), NULL);
2966 if (width <= view->ncols - 1)
2967 waddch(view->window, '\n');
2968 nprinted++;
2970 free(line);
2971 if (nprinted >= 1)
2972 s->last_displayed_line = s->first_displayed_line +
2973 (nprinted - 1);
2974 else
2975 s->last_displayed_line = s->first_displayed_line;
2977 view_vborder(view);
2979 if (s->eof) {
2980 while (nprinted < view->nlines) {
2981 waddch(view->window, '\n');
2982 nprinted++;
2985 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2986 if (err) {
2987 return err;
2990 wstandout(view->window);
2991 waddwstr(view->window, wline);
2992 free(wline);
2993 wline = NULL;
2994 wstandend(view->window);
2997 return NULL;
3000 static char *
3001 get_datestr(time_t *time, char *datebuf)
3003 struct tm mytm, *tm;
3004 char *p, *s;
3006 tm = gmtime_r(time, &mytm);
3007 if (tm == NULL)
3008 return NULL;
3009 s = asctime_r(tm, datebuf);
3010 if (s == NULL)
3011 return NULL;
3012 p = strchr(s, '\n');
3013 if (p)
3014 *p = '\0';
3015 return s;
3018 static const struct got_error *
3019 get_changed_paths(struct got_pathlist_head *paths,
3020 struct got_commit_object *commit, struct got_repository *repo)
3022 const struct got_error *err = NULL;
3023 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3024 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3025 struct got_object_qid *qid;
3027 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3028 if (qid != NULL) {
3029 struct got_commit_object *pcommit;
3030 err = got_object_open_as_commit(&pcommit, repo,
3031 qid->id);
3032 if (err)
3033 return err;
3035 tree_id1 = got_object_commit_get_tree_id(pcommit);
3036 got_object_commit_close(pcommit);
3040 if (tree_id1) {
3041 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3042 if (err)
3043 goto done;
3046 tree_id2 = got_object_commit_get_tree_id(commit);
3047 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3048 if (err)
3049 goto done;
3051 err = got_diff_tree(tree1, tree2, "", "", repo,
3052 got_diff_tree_collect_changed_paths, paths, 0);
3053 done:
3054 if (tree1)
3055 got_object_tree_close(tree1);
3056 if (tree2)
3057 got_object_tree_close(tree2);
3058 return err;
3061 static const struct got_error *
3062 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3064 off_t *p;
3066 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3067 if (p == NULL)
3068 return got_error_from_errno("reallocarray");
3069 *line_offsets = p;
3070 (*line_offsets)[*nlines] = off;
3071 (*nlines)++;
3072 return NULL;
3075 static const struct got_error *
3076 write_commit_info(off_t **line_offsets, size_t *nlines,
3077 struct got_object_id *commit_id, struct got_reflist_head *refs,
3078 struct got_repository *repo, FILE *outfile)
3080 const struct got_error *err = NULL;
3081 char datebuf[26], *datestr;
3082 struct got_commit_object *commit;
3083 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3084 time_t committer_time;
3085 const char *author, *committer;
3086 char *refs_str = NULL;
3087 struct got_pathlist_head changed_paths;
3088 struct got_pathlist_entry *pe;
3089 off_t outoff = 0;
3090 int n;
3092 TAILQ_INIT(&changed_paths);
3094 if (refs) {
3095 err = build_refs_str(&refs_str, refs, commit_id, repo);
3096 if (err)
3097 return err;
3100 err = got_object_open_as_commit(&commit, repo, commit_id);
3101 if (err)
3102 return err;
3104 err = got_object_id_str(&id_str, commit_id);
3105 if (err) {
3106 err = got_error_from_errno("got_object_id_str");
3107 goto done;
3110 err = add_line_offset(line_offsets, nlines, 0);
3111 if (err)
3112 goto done;
3114 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3115 refs_str ? refs_str : "", refs_str ? ")" : "");
3116 if (n < 0) {
3117 err = got_error_from_errno("fprintf");
3118 goto done;
3120 outoff += n;
3121 err = add_line_offset(line_offsets, nlines, outoff);
3122 if (err)
3123 goto done;
3125 n = fprintf(outfile, "from: %s\n",
3126 got_object_commit_get_author(commit));
3127 if (n < 0) {
3128 err = got_error_from_errno("fprintf");
3129 goto done;
3131 outoff += n;
3132 err = add_line_offset(line_offsets, nlines, outoff);
3133 if (err)
3134 goto done;
3136 committer_time = got_object_commit_get_committer_time(commit);
3137 datestr = get_datestr(&committer_time, datebuf);
3138 if (datestr) {
3139 n = fprintf(outfile, "date: %s UTC\n", datestr);
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 author = got_object_commit_get_author(commit);
3150 committer = got_object_commit_get_committer(commit);
3151 if (strcmp(author, committer) != 0) {
3152 n = fprintf(outfile, "via: %s\n", committer);
3153 if (n < 0) {
3154 err = got_error_from_errno("fprintf");
3155 goto done;
3157 outoff += n;
3158 err = add_line_offset(line_offsets, nlines, outoff);
3159 if (err)
3160 goto done;
3162 err = got_object_commit_get_logmsg(&logmsg, commit);
3163 if (err)
3164 goto done;
3165 s = logmsg;
3166 while ((line = strsep(&s, "\n")) != NULL) {
3167 n = fprintf(outfile, "%s\n", line);
3168 if (n < 0) {
3169 err = got_error_from_errno("fprintf");
3170 goto done;
3172 outoff += n;
3173 err = add_line_offset(line_offsets, nlines, outoff);
3174 if (err)
3175 goto done;
3178 err = get_changed_paths(&changed_paths, commit, repo);
3179 if (err)
3180 goto done;
3181 TAILQ_FOREACH(pe, &changed_paths, entry) {
3182 struct got_diff_changed_path *cp = pe->data;
3183 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3184 if (n < 0) {
3185 err = got_error_from_errno("fprintf");
3186 goto done;
3188 outoff += n;
3189 err = add_line_offset(line_offsets, nlines, outoff);
3190 if (err)
3191 goto done;
3192 free((char *)pe->path);
3193 free(pe->data);
3196 fputc('\n', outfile);
3197 outoff++;
3198 err = add_line_offset(line_offsets, nlines, outoff);
3199 done:
3200 got_pathlist_free(&changed_paths);
3201 free(id_str);
3202 free(logmsg);
3203 free(refs_str);
3204 got_object_commit_close(commit);
3205 if (err) {
3206 free(*line_offsets);
3207 *line_offsets = NULL;
3208 *nlines = 0;
3210 return err;
3213 static const struct got_error *
3214 create_diff(struct tog_diff_view_state *s)
3216 const struct got_error *err = NULL;
3217 FILE *f = NULL;
3218 int obj_type;
3220 free(s->line_offsets);
3221 s->line_offsets = malloc(sizeof(off_t));
3222 if (s->line_offsets == NULL)
3223 return got_error_from_errno("malloc");
3224 s->nlines = 0;
3226 f = got_opentemp();
3227 if (f == NULL) {
3228 err = got_error_from_errno("got_opentemp");
3229 goto done;
3231 if (s->f && fclose(s->f) != 0) {
3232 err = got_error_from_errno("fclose");
3233 goto done;
3235 s->f = f;
3237 if (s->id1)
3238 err = got_object_get_type(&obj_type, s->repo, s->id1);
3239 else
3240 err = got_object_get_type(&obj_type, s->repo, s->id2);
3241 if (err)
3242 goto done;
3244 switch (obj_type) {
3245 case GOT_OBJ_TYPE_BLOB:
3246 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3247 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3248 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3249 break;
3250 case GOT_OBJ_TYPE_TREE:
3251 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3252 s->id1, s->id2, "", "", s->diff_context,
3253 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3254 break;
3255 case GOT_OBJ_TYPE_COMMIT: {
3256 const struct got_object_id_queue *parent_ids;
3257 struct got_object_qid *pid;
3258 struct got_commit_object *commit2;
3260 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3261 if (err)
3262 goto done;
3263 /* Show commit info if we're diffing to a parent/root commit. */
3264 if (s->id1 == NULL) {
3265 err = write_commit_info(&s->line_offsets, &s->nlines,
3266 s->id2, &s->refs, s->repo, s->f);
3267 if (err)
3268 goto done;
3269 } else {
3270 parent_ids = got_object_commit_get_parent_ids(commit2);
3271 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3272 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3273 err = write_commit_info(
3274 &s->line_offsets, &s->nlines,
3275 s->id2, &s->refs, s->repo, s->f);
3276 if (err)
3277 goto done;
3278 break;
3282 got_object_commit_close(commit2);
3284 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3285 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3286 s->force_text_diff, s->repo, s->f);
3287 break;
3289 default:
3290 err = got_error(GOT_ERR_OBJ_TYPE);
3291 break;
3293 if (err)
3294 goto done;
3295 done:
3296 if (s->f && fflush(s->f) != 0 && err == NULL)
3297 err = got_error_from_errno("fflush");
3298 return err;
3301 static void
3302 diff_view_indicate_progress(struct tog_view *view)
3304 mvwaddstr(view->window, 0, 0, "diffing...");
3305 update_panels();
3306 doupdate();
3309 static const struct got_error *
3310 search_start_diff_view(struct tog_view *view)
3312 struct tog_diff_view_state *s = &view->state.diff;
3314 s->matched_line = 0;
3315 return NULL;
3318 static const struct got_error *
3319 search_next_diff_view(struct tog_view *view)
3321 struct tog_diff_view_state *s = &view->state.diff;
3322 int lineno;
3323 char *line = NULL;
3324 size_t linesize = 0;
3325 ssize_t linelen;
3327 if (!view->searching) {
3328 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3329 return NULL;
3332 if (s->matched_line) {
3333 if (view->searching == TOG_SEARCH_FORWARD)
3334 lineno = s->matched_line + 1;
3335 else
3336 lineno = s->matched_line - 1;
3337 } else {
3338 if (view->searching == TOG_SEARCH_FORWARD)
3339 lineno = 1;
3340 else
3341 lineno = s->nlines;
3344 while (1) {
3345 off_t offset;
3347 if (lineno <= 0 || lineno > s->nlines) {
3348 if (s->matched_line == 0) {
3349 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3350 break;
3353 if (view->searching == TOG_SEARCH_FORWARD)
3354 lineno = 1;
3355 else
3356 lineno = s->nlines;
3359 offset = s->line_offsets[lineno - 1];
3360 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3361 free(line);
3362 return got_error_from_errno("fseeko");
3364 linelen = getline(&line, &linesize, s->f);
3365 if (linelen != -1 &&
3366 match_line(line, &view->regex, 1, &view->regmatch)) {
3367 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3368 s->matched_line = lineno;
3369 break;
3371 if (view->searching == TOG_SEARCH_FORWARD)
3372 lineno++;
3373 else
3374 lineno--;
3376 free(line);
3378 if (s->matched_line) {
3379 s->first_displayed_line = s->matched_line;
3380 s->selected_line = 1;
3383 return NULL;
3386 static const struct got_error *
3387 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3388 struct got_object_id *id2, const char *label1, const char *label2,
3389 int diff_context, int ignore_whitespace, int force_text_diff,
3390 struct tog_view *log_view, struct got_repository *repo)
3392 const struct got_error *err;
3393 struct tog_diff_view_state *s = &view->state.diff;
3395 SIMPLEQ_INIT(&s->refs);
3397 if (id1 != NULL && id2 != NULL) {
3398 int type1, type2;
3399 err = got_object_get_type(&type1, repo, id1);
3400 if (err)
3401 return err;
3402 err = got_object_get_type(&type2, repo, id2);
3403 if (err)
3404 return err;
3406 if (type1 != type2)
3407 return got_error(GOT_ERR_OBJ_TYPE);
3409 s->first_displayed_line = 1;
3410 s->last_displayed_line = view->nlines;
3411 s->selected_line = 1;
3412 s->repo = repo;
3413 s->id1 = id1;
3414 s->id2 = id2;
3415 s->label1 = label1;
3416 s->label2 = label2;
3418 if (id1) {
3419 s->id1 = got_object_id_dup(id1);
3420 if (s->id1 == NULL)
3421 return got_error_from_errno("got_object_id_dup");
3422 } else
3423 s->id1 = NULL;
3425 s->id2 = got_object_id_dup(id2);
3426 if (s->id2 == NULL) {
3427 free(s->id1);
3428 s->id1 = NULL;
3429 return got_error_from_errno("got_object_id_dup");
3431 s->f = NULL;
3432 s->first_displayed_line = 1;
3433 s->last_displayed_line = view->nlines;
3434 s->diff_context = diff_context;
3435 s->ignore_whitespace = ignore_whitespace;
3436 s->force_text_diff = force_text_diff;
3437 s->log_view = log_view;
3438 s->repo = repo;
3440 SIMPLEQ_INIT(&s->colors);
3441 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3442 err = add_color(&s->colors,
3443 "^-", TOG_COLOR_DIFF_MINUS,
3444 get_color_value("TOG_COLOR_DIFF_MINUS"));
3445 if (err)
3446 return err;
3447 err = add_color(&s->colors, "^\\+",
3448 TOG_COLOR_DIFF_PLUS,
3449 get_color_value("TOG_COLOR_DIFF_PLUS"));
3450 if (err) {
3451 free_colors(&s->colors);
3452 return err;
3454 err = add_color(&s->colors,
3455 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3456 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3457 if (err) {
3458 free_colors(&s->colors);
3459 return err;
3462 err = add_color(&s->colors,
3463 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3464 TOG_COLOR_DIFF_META,
3465 get_color_value("TOG_COLOR_DIFF_META"));
3466 if (err) {
3467 free_colors(&s->colors);
3468 return err;
3471 err = add_color(&s->colors,
3472 "^(from|via): ", TOG_COLOR_AUTHOR,
3473 get_color_value("TOG_COLOR_AUTHOR"));
3474 if (err) {
3475 free_colors(&s->colors);
3476 return err;
3479 err = add_color(&s->colors,
3480 "^date: ", TOG_COLOR_DATE,
3481 get_color_value("TOG_COLOR_DATE"));
3482 if (err) {
3483 free_colors(&s->colors);
3484 return err;
3488 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3489 if (err) {
3490 free(s->id1);
3491 s->id1 = NULL;
3492 free(s->id2);
3493 s->id2 = NULL;
3494 free_colors(&s->colors);
3495 return err;
3498 if (log_view && view_is_splitscreen(view))
3499 show_log_view(log_view); /* draw vborder */
3500 diff_view_indicate_progress(view);
3502 s->line_offsets = NULL;
3503 s->nlines = 0;
3504 err = create_diff(s);
3505 if (err) {
3506 free(s->id1);
3507 s->id1 = NULL;
3508 free(s->id2);
3509 s->id2 = NULL;
3510 free_colors(&s->colors);
3511 got_ref_list_free(&s->refs);
3512 return err;
3515 view->show = show_diff_view;
3516 view->input = input_diff_view;
3517 view->close = close_diff_view;
3518 view->search_start = search_start_diff_view;
3519 view->search_next = search_next_diff_view;
3521 return NULL;
3524 static const struct got_error *
3525 close_diff_view(struct tog_view *view)
3527 const struct got_error *err = NULL;
3528 struct tog_diff_view_state *s = &view->state.diff;
3530 free(s->id1);
3531 s->id1 = NULL;
3532 free(s->id2);
3533 s->id2 = NULL;
3534 if (s->f && fclose(s->f) == EOF)
3535 err = got_error_from_errno("fclose");
3536 free_colors(&s->colors);
3537 free(s->line_offsets);
3538 s->line_offsets = NULL;
3539 s->nlines = 0;
3540 got_ref_list_free(&s->refs);
3541 return err;
3544 static const struct got_error *
3545 show_diff_view(struct tog_view *view)
3547 const struct got_error *err;
3548 struct tog_diff_view_state *s = &view->state.diff;
3549 char *id_str1 = NULL, *id_str2, *header;
3550 const char *label1, *label2;
3552 if (s->id1) {
3553 err = got_object_id_str(&id_str1, s->id1);
3554 if (err)
3555 return err;
3556 label1 = s->label1 ? : id_str1;
3557 } else
3558 label1 = "/dev/null";
3560 err = got_object_id_str(&id_str2, s->id2);
3561 if (err)
3562 return err;
3563 label2 = s->label2 ? : id_str2;
3565 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3566 err = got_error_from_errno("asprintf");
3567 free(id_str1);
3568 free(id_str2);
3569 return err;
3571 free(id_str1);
3572 free(id_str2);
3574 return draw_file(view, header);
3577 static const struct got_error *
3578 set_selected_commit(struct tog_diff_view_state *s,
3579 struct commit_queue_entry *entry)
3581 const struct got_error *err;
3582 const struct got_object_id_queue *parent_ids;
3583 struct got_commit_object *selected_commit;
3584 struct got_object_qid *pid;
3586 free(s->id2);
3587 s->id2 = got_object_id_dup(entry->id);
3588 if (s->id2 == NULL)
3589 return got_error_from_errno("got_object_id_dup");
3591 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3592 if (err)
3593 return err;
3594 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3595 free(s->id1);
3596 pid = SIMPLEQ_FIRST(parent_ids);
3597 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3598 got_object_commit_close(selected_commit);
3599 return NULL;
3602 static const struct got_error *
3603 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3605 const struct got_error *err = NULL;
3606 struct tog_diff_view_state *s = &view->state.diff;
3607 struct tog_log_view_state *ls;
3608 struct commit_queue_entry *old_selected_entry;
3609 char *line = NULL;
3610 size_t linesize = 0;
3611 ssize_t linelen;
3612 int i;
3614 switch (ch) {
3615 case 'a':
3616 case 'w':
3617 if (ch == 'a')
3618 s->force_text_diff = !s->force_text_diff;
3619 if (ch == 'w')
3620 s->ignore_whitespace = !s->ignore_whitespace;
3621 wclear(view->window);
3622 s->first_displayed_line = 1;
3623 s->last_displayed_line = view->nlines;
3624 diff_view_indicate_progress(view);
3625 err = create_diff(s);
3626 break;
3627 case 'k':
3628 case KEY_UP:
3629 if (s->first_displayed_line > 1)
3630 s->first_displayed_line--;
3631 break;
3632 case KEY_PPAGE:
3633 case CTRL('b'):
3634 if (s->first_displayed_line == 1)
3635 break;
3636 i = 0;
3637 while (i++ < view->nlines - 1 &&
3638 s->first_displayed_line > 1)
3639 s->first_displayed_line--;
3640 break;
3641 case 'j':
3642 case KEY_DOWN:
3643 if (!s->eof)
3644 s->first_displayed_line++;
3645 break;
3646 case KEY_NPAGE:
3647 case CTRL('f'):
3648 case ' ':
3649 if (s->eof)
3650 break;
3651 i = 0;
3652 while (!s->eof && i++ < view->nlines - 1) {
3653 linelen = getline(&line, &linesize, s->f);
3654 s->first_displayed_line++;
3655 if (linelen == -1) {
3656 if (feof(s->f)) {
3657 s->eof = 1;
3658 } else
3659 err = got_ferror(s->f, GOT_ERR_IO);
3660 break;
3663 free(line);
3664 break;
3665 case '[':
3666 if (s->diff_context > 0) {
3667 s->diff_context--;
3668 diff_view_indicate_progress(view);
3669 err = create_diff(s);
3670 if (s->first_displayed_line + view->nlines - 1 >
3671 s->nlines) {
3672 s->first_displayed_line = 1;
3673 s->last_displayed_line = view->nlines;
3676 break;
3677 case ']':
3678 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3679 s->diff_context++;
3680 diff_view_indicate_progress(view);
3681 err = create_diff(s);
3683 break;
3684 case '<':
3685 case ',':
3686 if (s->log_view == NULL)
3687 break;
3688 ls = &s->log_view->state.log;
3689 old_selected_entry = ls->selected_entry;
3691 err = input_log_view(NULL, s->log_view, KEY_UP);
3692 if (err)
3693 break;
3695 if (old_selected_entry == ls->selected_entry)
3696 break;
3698 err = set_selected_commit(s, ls->selected_entry);
3699 if (err)
3700 break;
3702 s->first_displayed_line = 1;
3703 s->last_displayed_line = view->nlines;
3705 diff_view_indicate_progress(view);
3706 err = create_diff(s);
3707 break;
3708 case '>':
3709 case '.':
3710 if (s->log_view == NULL)
3711 break;
3712 ls = &s->log_view->state.log;
3713 old_selected_entry = ls->selected_entry;
3715 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3716 if (err)
3717 break;
3719 if (old_selected_entry == ls->selected_entry)
3720 break;
3722 err = set_selected_commit(s, ls->selected_entry);
3723 if (err)
3724 break;
3726 s->first_displayed_line = 1;
3727 s->last_displayed_line = view->nlines;
3729 diff_view_indicate_progress(view);
3730 err = create_diff(s);
3731 break;
3732 default:
3733 break;
3736 return err;
3739 static const struct got_error *
3740 cmd_diff(int argc, char *argv[])
3742 const struct got_error *error = NULL;
3743 struct got_repository *repo = NULL;
3744 struct got_worktree *worktree = NULL;
3745 struct got_object_id *id1 = NULL, *id2 = NULL;
3746 char *repo_path = NULL, *cwd = NULL;
3747 char *id_str1 = NULL, *id_str2 = NULL;
3748 char *label1 = NULL, *label2 = NULL;
3749 int diff_context = 3, ignore_whitespace = 0;
3750 int ch, force_text_diff = 0;
3751 const char *errstr;
3752 struct tog_view *view;
3754 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3755 switch (ch) {
3756 case 'a':
3757 force_text_diff = 1;
3758 break;
3759 case 'C':
3760 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3761 &errstr);
3762 if (errstr != NULL)
3763 err(1, "-C option %s", errstr);
3764 break;
3765 case 'r':
3766 repo_path = realpath(optarg, NULL);
3767 if (repo_path == NULL)
3768 return got_error_from_errno2("realpath",
3769 optarg);
3770 got_path_strip_trailing_slashes(repo_path);
3771 break;
3772 case 'w':
3773 ignore_whitespace = 1;
3774 break;
3775 default:
3776 usage_diff();
3777 /* NOTREACHED */
3781 argc -= optind;
3782 argv += optind;
3784 if (argc == 0) {
3785 usage_diff(); /* TODO show local worktree changes */
3786 } else if (argc == 2) {
3787 id_str1 = argv[0];
3788 id_str2 = argv[1];
3789 } else
3790 usage_diff();
3792 cwd = getcwd(NULL, 0);
3793 if (cwd == NULL)
3794 return got_error_from_errno("getcwd");
3796 error = got_worktree_open(&worktree, cwd);
3797 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3798 goto done;
3800 if (repo_path == NULL) {
3801 if (worktree)
3802 repo_path =
3803 strdup(got_worktree_get_repo_path(worktree));
3804 else
3805 repo_path = strdup(cwd);
3807 if (repo_path == NULL) {
3808 error = got_error_from_errno("strdup");
3809 goto done;
3812 error = got_repo_open(&repo, repo_path, NULL);
3813 if (error)
3814 goto done;
3816 init_curses();
3818 error = apply_unveil(got_repo_get_path(repo), NULL);
3819 if (error)
3820 goto done;
3822 error = got_repo_match_object_id(&id1, &label1, id_str1,
3823 GOT_OBJ_TYPE_ANY, 1, repo);
3824 if (error)
3825 goto done;
3827 error = got_repo_match_object_id(&id2, &label2, id_str2,
3828 GOT_OBJ_TYPE_ANY, 1, repo);
3829 if (error)
3830 goto done;
3832 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3833 if (view == NULL) {
3834 error = got_error_from_errno("view_open");
3835 goto done;
3837 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3838 ignore_whitespace, force_text_diff, NULL, repo);
3839 if (error)
3840 goto done;
3841 error = view_loop(view);
3842 done:
3843 free(label1);
3844 free(label2);
3845 free(repo_path);
3846 free(cwd);
3847 if (repo)
3848 got_repo_close(repo);
3849 if (worktree)
3850 got_worktree_close(worktree);
3851 return error;
3854 __dead static void
3855 usage_blame(void)
3857 endwin();
3858 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3859 getprogname());
3860 exit(1);
3863 struct tog_blame_line {
3864 int annotated;
3865 struct got_object_id *id;
3868 static const struct got_error *
3869 draw_blame(struct tog_view *view)
3871 struct tog_blame_view_state *s = &view->state.blame;
3872 struct tog_blame *blame = &s->blame;
3873 regmatch_t *regmatch = &view->regmatch;
3874 const struct got_error *err;
3875 int lineno = 0, nprinted = 0;
3876 char *line = NULL;
3877 size_t linesize = 0;
3878 ssize_t linelen;
3879 wchar_t *wline;
3880 int width;
3881 struct tog_blame_line *blame_line;
3882 struct got_object_id *prev_id = NULL;
3883 char *id_str;
3884 struct tog_color *tc;
3886 err = got_object_id_str(&id_str, s->blamed_commit->id);
3887 if (err)
3888 return err;
3890 rewind(blame->f);
3891 werase(view->window);
3893 if (asprintf(&line, "commit %s", id_str) == -1) {
3894 err = got_error_from_errno("asprintf");
3895 free(id_str);
3896 return err;
3899 err = format_line(&wline, &width, line, view->ncols, 0);
3900 free(line);
3901 line = NULL;
3902 if (err)
3903 return err;
3904 if (view_needs_focus_indication(view))
3905 wstandout(view->window);
3906 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3907 if (tc)
3908 wattr_on(view->window,
3909 COLOR_PAIR(tc->colorpair), NULL);
3910 waddwstr(view->window, wline);
3911 if (tc)
3912 wattr_off(view->window,
3913 COLOR_PAIR(tc->colorpair), NULL);
3914 if (view_needs_focus_indication(view))
3915 wstandend(view->window);
3916 free(wline);
3917 wline = NULL;
3918 if (width < view->ncols - 1)
3919 waddch(view->window, '\n');
3921 if (asprintf(&line, "[%d/%d] %s%s",
3922 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3923 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3924 free(id_str);
3925 return got_error_from_errno("asprintf");
3927 free(id_str);
3928 err = format_line(&wline, &width, line, view->ncols, 0);
3929 free(line);
3930 line = NULL;
3931 if (err)
3932 return err;
3933 waddwstr(view->window, wline);
3934 free(wline);
3935 wline = NULL;
3936 if (width < view->ncols - 1)
3937 waddch(view->window, '\n');
3939 s->eof = 0;
3940 while (nprinted < view->nlines - 2) {
3941 linelen = getline(&line, &linesize, blame->f);
3942 if (linelen == -1) {
3943 if (feof(blame->f)) {
3944 s->eof = 1;
3945 break;
3947 free(line);
3948 return got_ferror(blame->f, GOT_ERR_IO);
3950 if (++lineno < s->first_displayed_line)
3951 continue;
3953 if (view->focussed && nprinted == s->selected_line - 1)
3954 wstandout(view->window);
3956 if (blame->nlines > 0) {
3957 blame_line = &blame->lines[lineno - 1];
3958 if (blame_line->annotated && prev_id &&
3959 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3960 !(view->focussed &&
3961 nprinted == s->selected_line - 1)) {
3962 waddstr(view->window, " ");
3963 } else if (blame_line->annotated) {
3964 char *id_str;
3965 err = got_object_id_str(&id_str, blame_line->id);
3966 if (err) {
3967 free(line);
3968 return err;
3970 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3971 if (tc)
3972 wattr_on(view->window,
3973 COLOR_PAIR(tc->colorpair), NULL);
3974 wprintw(view->window, "%.8s", id_str);
3975 if (tc)
3976 wattr_off(view->window,
3977 COLOR_PAIR(tc->colorpair), NULL);
3978 free(id_str);
3979 prev_id = blame_line->id;
3980 } else {
3981 waddstr(view->window, "........");
3982 prev_id = NULL;
3984 } else {
3985 waddstr(view->window, "........");
3986 prev_id = NULL;
3989 if (view->focussed && nprinted == s->selected_line - 1)
3990 wstandend(view->window);
3991 waddstr(view->window, " ");
3993 if (view->ncols <= 9) {
3994 width = 9;
3995 wline = wcsdup(L"");
3996 if (wline == NULL) {
3997 err = got_error_from_errno("wcsdup");
3998 free(line);
3999 return err;
4001 } else if (s->first_displayed_line + nprinted ==
4002 s->matched_line &&
4003 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4004 err = add_matched_line(&width, line, view->ncols - 9, 9,
4005 view->window, regmatch);
4006 if (err) {
4007 free(line);
4008 return err;
4010 width += 9;
4011 } else {
4012 err = format_line(&wline, &width, line,
4013 view->ncols - 9, 9);
4014 waddwstr(view->window, wline);
4015 free(wline);
4016 wline = NULL;
4017 width += 9;
4020 if (width <= view->ncols - 1)
4021 waddch(view->window, '\n');
4022 if (++nprinted == 1)
4023 s->first_displayed_line = lineno;
4025 free(line);
4026 s->last_displayed_line = lineno;
4028 view_vborder(view);
4030 return NULL;
4033 static const struct got_error *
4034 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4036 const struct got_error *err = NULL;
4037 struct tog_blame_cb_args *a = arg;
4038 struct tog_blame_line *line;
4039 int errcode;
4041 if (nlines != a->nlines ||
4042 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4043 return got_error(GOT_ERR_RANGE);
4045 errcode = pthread_mutex_lock(&tog_mutex);
4046 if (errcode)
4047 return got_error_set_errno(errcode, "pthread_mutex_lock");
4049 if (*a->quit) { /* user has quit the blame view */
4050 err = got_error(GOT_ERR_ITER_COMPLETED);
4051 goto done;
4054 if (lineno == -1)
4055 goto done; /* no change in this commit */
4057 line = &a->lines[lineno - 1];
4058 if (line->annotated)
4059 goto done;
4061 line->id = got_object_id_dup(id);
4062 if (line->id == NULL) {
4063 err = got_error_from_errno("got_object_id_dup");
4064 goto done;
4066 line->annotated = 1;
4067 done:
4068 errcode = pthread_mutex_unlock(&tog_mutex);
4069 if (errcode)
4070 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4071 return err;
4074 static void *
4075 blame_thread(void *arg)
4077 const struct got_error *err;
4078 struct tog_blame_thread_args *ta = arg;
4079 struct tog_blame_cb_args *a = ta->cb_args;
4080 int errcode;
4082 err = block_signals_used_by_main_thread();
4083 if (err)
4084 return (void *)err;
4086 err = got_blame(ta->path, a->commit_id, ta->repo,
4087 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4088 if (err && err->code == GOT_ERR_CANCELLED)
4089 err = NULL;
4091 errcode = pthread_mutex_lock(&tog_mutex);
4092 if (errcode)
4093 return (void *)got_error_set_errno(errcode,
4094 "pthread_mutex_lock");
4096 got_repo_close(ta->repo);
4097 ta->repo = NULL;
4098 *ta->complete = 1;
4100 errcode = pthread_mutex_unlock(&tog_mutex);
4101 if (errcode && err == NULL)
4102 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4104 return (void *)err;
4107 static struct got_object_id *
4108 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4109 int first_displayed_line, int selected_line)
4111 struct tog_blame_line *line;
4113 if (nlines <= 0)
4114 return NULL;
4116 line = &lines[first_displayed_line - 1 + selected_line - 1];
4117 if (!line->annotated)
4118 return NULL;
4120 return line->id;
4123 static const struct got_error *
4124 stop_blame(struct tog_blame *blame)
4126 const struct got_error *err = NULL;
4127 int i;
4129 if (blame->thread) {
4130 int errcode;
4131 errcode = pthread_mutex_unlock(&tog_mutex);
4132 if (errcode)
4133 return got_error_set_errno(errcode,
4134 "pthread_mutex_unlock");
4135 errcode = pthread_join(blame->thread, (void **)&err);
4136 if (errcode)
4137 return got_error_set_errno(errcode, "pthread_join");
4138 errcode = pthread_mutex_lock(&tog_mutex);
4139 if (errcode)
4140 return got_error_set_errno(errcode,
4141 "pthread_mutex_lock");
4142 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4143 err = NULL;
4144 blame->thread = NULL;
4146 if (blame->thread_args.repo) {
4147 got_repo_close(blame->thread_args.repo);
4148 blame->thread_args.repo = NULL;
4150 if (blame->f) {
4151 if (fclose(blame->f) != 0 && err == NULL)
4152 err = got_error_from_errno("fclose");
4153 blame->f = NULL;
4155 if (blame->lines) {
4156 for (i = 0; i < blame->nlines; i++)
4157 free(blame->lines[i].id);
4158 free(blame->lines);
4159 blame->lines = NULL;
4161 free(blame->cb_args.commit_id);
4162 blame->cb_args.commit_id = NULL;
4164 return err;
4167 static const struct got_error *
4168 cancel_blame_view(void *arg)
4170 const struct got_error *err = NULL;
4171 int *done = arg;
4172 int errcode;
4174 errcode = pthread_mutex_lock(&tog_mutex);
4175 if (errcode)
4176 return got_error_set_errno(errcode,
4177 "pthread_mutex_unlock");
4179 if (*done)
4180 err = got_error(GOT_ERR_CANCELLED);
4182 errcode = pthread_mutex_unlock(&tog_mutex);
4183 if (errcode)
4184 return got_error_set_errno(errcode,
4185 "pthread_mutex_lock");
4187 return err;
4190 static const struct got_error *
4191 run_blame(struct tog_view *view)
4193 struct tog_blame_view_state *s = &view->state.blame;
4194 struct tog_blame *blame = &s->blame;
4195 const struct got_error *err = NULL;
4196 struct got_blob_object *blob = NULL;
4197 struct got_repository *thread_repo = NULL;
4198 struct got_object_id *obj_id = NULL;
4199 int obj_type;
4201 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4202 s->path);
4203 if (err)
4204 return err;
4206 err = got_object_get_type(&obj_type, s->repo, obj_id);
4207 if (err)
4208 goto done;
4210 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4211 err = got_error(GOT_ERR_OBJ_TYPE);
4212 goto done;
4215 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4216 if (err)
4217 goto done;
4218 blame->f = got_opentemp();
4219 if (blame->f == NULL) {
4220 err = got_error_from_errno("got_opentemp");
4221 goto done;
4223 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4224 &blame->line_offsets, blame->f, blob);
4225 if (err || blame->nlines == 0)
4226 goto done;
4228 /* Don't include \n at EOF in the blame line count. */
4229 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4230 blame->nlines--;
4232 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4233 if (blame->lines == NULL) {
4234 err = got_error_from_errno("calloc");
4235 goto done;
4238 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4239 if (err)
4240 goto done;
4242 blame->cb_args.view = view;
4243 blame->cb_args.lines = blame->lines;
4244 blame->cb_args.nlines = blame->nlines;
4245 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4246 if (blame->cb_args.commit_id == NULL) {
4247 err = got_error_from_errno("got_object_id_dup");
4248 goto done;
4250 blame->cb_args.quit = &s->done;
4252 blame->thread_args.path = s->path;
4253 blame->thread_args.repo = thread_repo;
4254 blame->thread_args.cb_args = &blame->cb_args;
4255 blame->thread_args.complete = &s->blame_complete;
4256 blame->thread_args.cancel_cb = cancel_blame_view;
4257 blame->thread_args.cancel_arg = &s->done;
4258 s->blame_complete = 0;
4260 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4261 s->first_displayed_line = 1;
4262 s->last_displayed_line = view->nlines;
4263 s->selected_line = 1;
4266 done:
4267 if (blob)
4268 got_object_blob_close(blob);
4269 free(obj_id);
4270 if (err)
4271 stop_blame(blame);
4272 return err;
4275 static const struct got_error *
4276 open_blame_view(struct tog_view *view, char *path,
4277 struct got_object_id *commit_id, struct got_repository *repo)
4279 const struct got_error *err = NULL;
4280 struct tog_blame_view_state *s = &view->state.blame;
4282 SIMPLEQ_INIT(&s->blamed_commits);
4284 s->path = strdup(path);
4285 if (s->path == NULL)
4286 return got_error_from_errno("strdup");
4288 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4289 if (err) {
4290 free(s->path);
4291 return err;
4294 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4295 s->first_displayed_line = 1;
4296 s->last_displayed_line = view->nlines;
4297 s->selected_line = 1;
4298 s->blame_complete = 0;
4299 s->repo = repo;
4300 s->commit_id = commit_id;
4301 memset(&s->blame, 0, sizeof(s->blame));
4303 SIMPLEQ_INIT(&s->colors);
4304 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4305 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4306 get_color_value("TOG_COLOR_COMMIT"));
4307 if (err)
4308 return err;
4311 view->show = show_blame_view;
4312 view->input = input_blame_view;
4313 view->close = close_blame_view;
4314 view->search_start = search_start_blame_view;
4315 view->search_next = search_next_blame_view;
4317 return run_blame(view);
4320 static const struct got_error *
4321 close_blame_view(struct tog_view *view)
4323 const struct got_error *err = NULL;
4324 struct tog_blame_view_state *s = &view->state.blame;
4326 if (s->blame.thread)
4327 err = stop_blame(&s->blame);
4329 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4330 struct got_object_qid *blamed_commit;
4331 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4332 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4333 got_object_qid_free(blamed_commit);
4336 free(s->path);
4337 free_colors(&s->colors);
4339 return err;
4342 static const struct got_error *
4343 search_start_blame_view(struct tog_view *view)
4345 struct tog_blame_view_state *s = &view->state.blame;
4347 s->matched_line = 0;
4348 return NULL;
4351 static const struct got_error *
4352 search_next_blame_view(struct tog_view *view)
4354 struct tog_blame_view_state *s = &view->state.blame;
4355 int lineno;
4356 char *line = NULL;
4357 size_t linesize = 0;
4358 ssize_t linelen;
4360 if (!view->searching) {
4361 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4362 return NULL;
4365 if (s->matched_line) {
4366 if (view->searching == TOG_SEARCH_FORWARD)
4367 lineno = s->matched_line + 1;
4368 else
4369 lineno = s->matched_line - 1;
4370 } else {
4371 if (view->searching == TOG_SEARCH_FORWARD)
4372 lineno = 1;
4373 else
4374 lineno = s->blame.nlines;
4377 while (1) {
4378 off_t offset;
4380 if (lineno <= 0 || lineno > s->blame.nlines) {
4381 if (s->matched_line == 0) {
4382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 break;
4386 if (view->searching == TOG_SEARCH_FORWARD)
4387 lineno = 1;
4388 else
4389 lineno = s->blame.nlines;
4392 offset = s->blame.line_offsets[lineno - 1];
4393 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4394 free(line);
4395 return got_error_from_errno("fseeko");
4397 linelen = getline(&line, &linesize, s->blame.f);
4398 if (linelen != -1 &&
4399 match_line(line, &view->regex, 1, &view->regmatch)) {
4400 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 s->matched_line = lineno;
4402 break;
4404 if (view->searching == TOG_SEARCH_FORWARD)
4405 lineno++;
4406 else
4407 lineno--;
4409 free(line);
4411 if (s->matched_line) {
4412 s->first_displayed_line = s->matched_line;
4413 s->selected_line = 1;
4416 return NULL;
4419 static const struct got_error *
4420 show_blame_view(struct tog_view *view)
4422 const struct got_error *err = NULL;
4423 struct tog_blame_view_state *s = &view->state.blame;
4424 int errcode;
4426 if (s->blame.thread == NULL) {
4427 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4428 &s->blame.thread_args);
4429 if (errcode)
4430 return got_error_set_errno(errcode, "pthread_create");
4432 halfdelay(1); /* fast refresh while annotating */
4435 if (s->blame_complete)
4436 halfdelay(10); /* disable fast refresh */
4438 err = draw_blame(view);
4440 view_vborder(view);
4441 return err;
4444 static const struct got_error *
4445 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4447 const struct got_error *err = NULL, *thread_err = NULL;
4448 struct tog_view *diff_view;
4449 struct tog_blame_view_state *s = &view->state.blame;
4450 int begin_x = 0;
4452 switch (ch) {
4453 case 'q':
4454 s->done = 1;
4455 break;
4456 case 'k':
4457 case KEY_UP:
4458 if (s->selected_line > 1)
4459 s->selected_line--;
4460 else if (s->selected_line == 1 &&
4461 s->first_displayed_line > 1)
4462 s->first_displayed_line--;
4463 break;
4464 case KEY_PPAGE:
4465 case CTRL('b'):
4466 if (s->first_displayed_line == 1) {
4467 s->selected_line = 1;
4468 break;
4470 if (s->first_displayed_line > view->nlines - 2)
4471 s->first_displayed_line -=
4472 (view->nlines - 2);
4473 else
4474 s->first_displayed_line = 1;
4475 break;
4476 case 'j':
4477 case KEY_DOWN:
4478 if (s->selected_line < view->nlines - 2 &&
4479 s->first_displayed_line +
4480 s->selected_line <= s->blame.nlines)
4481 s->selected_line++;
4482 else if (s->last_displayed_line <
4483 s->blame.nlines)
4484 s->first_displayed_line++;
4485 break;
4486 case 'b':
4487 case 'p': {
4488 struct got_object_id *id = NULL;
4489 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4490 s->first_displayed_line, s->selected_line);
4491 if (id == NULL)
4492 break;
4493 if (ch == 'p') {
4494 struct got_commit_object *commit;
4495 struct got_object_qid *pid;
4496 struct got_object_id *blob_id = NULL;
4497 int obj_type;
4498 err = got_object_open_as_commit(&commit,
4499 s->repo, id);
4500 if (err)
4501 break;
4502 pid = SIMPLEQ_FIRST(
4503 got_object_commit_get_parent_ids(commit));
4504 if (pid == NULL) {
4505 got_object_commit_close(commit);
4506 break;
4508 /* Check if path history ends here. */
4509 err = got_object_id_by_path(&blob_id, s->repo,
4510 pid->id, s->path);
4511 if (err) {
4512 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4513 err = NULL;
4514 got_object_commit_close(commit);
4515 break;
4517 err = got_object_get_type(&obj_type, s->repo,
4518 blob_id);
4519 free(blob_id);
4520 /* Can't blame non-blob type objects. */
4521 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4522 got_object_commit_close(commit);
4523 break;
4525 err = got_object_qid_alloc(&s->blamed_commit,
4526 pid->id);
4527 got_object_commit_close(commit);
4528 } else {
4529 if (got_object_id_cmp(id,
4530 s->blamed_commit->id) == 0)
4531 break;
4532 err = got_object_qid_alloc(&s->blamed_commit,
4533 id);
4535 if (err)
4536 break;
4537 s->done = 1;
4538 thread_err = stop_blame(&s->blame);
4539 s->done = 0;
4540 if (thread_err)
4541 break;
4542 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4543 s->blamed_commit, entry);
4544 err = run_blame(view);
4545 if (err)
4546 break;
4547 break;
4549 case 'B': {
4550 struct got_object_qid *first;
4551 first = SIMPLEQ_FIRST(&s->blamed_commits);
4552 if (!got_object_id_cmp(first->id, s->commit_id))
4553 break;
4554 s->done = 1;
4555 thread_err = stop_blame(&s->blame);
4556 s->done = 0;
4557 if (thread_err)
4558 break;
4559 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4560 got_object_qid_free(s->blamed_commit);
4561 s->blamed_commit =
4562 SIMPLEQ_FIRST(&s->blamed_commits);
4563 err = run_blame(view);
4564 if (err)
4565 break;
4566 break;
4568 case KEY_ENTER:
4569 case '\r': {
4570 struct got_object_id *id = NULL;
4571 struct got_object_qid *pid;
4572 struct got_commit_object *commit = NULL;
4573 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4574 s->first_displayed_line, s->selected_line);
4575 if (id == NULL)
4576 break;
4577 err = got_object_open_as_commit(&commit, s->repo, id);
4578 if (err)
4579 break;
4580 pid = SIMPLEQ_FIRST(
4581 got_object_commit_get_parent_ids(commit));
4582 if (view_is_parent_view(view))
4583 begin_x = view_split_begin_x(view->begin_x);
4584 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4585 if (diff_view == NULL) {
4586 got_object_commit_close(commit);
4587 err = got_error_from_errno("view_open");
4588 break;
4590 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4591 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4592 got_object_commit_close(commit);
4593 if (err) {
4594 view_close(diff_view);
4595 break;
4597 view->focussed = 0;
4598 diff_view->focussed = 1;
4599 if (view_is_parent_view(view)) {
4600 err = view_close_child(view);
4601 if (err)
4602 break;
4603 view_set_child(view, diff_view);
4604 view->focus_child = 1;
4605 } else
4606 *new_view = diff_view;
4607 if (err)
4608 break;
4609 break;
4611 case KEY_NPAGE:
4612 case CTRL('f'):
4613 case ' ':
4614 if (s->last_displayed_line >= s->blame.nlines &&
4615 s->selected_line >= MIN(s->blame.nlines,
4616 view->nlines - 2)) {
4617 break;
4619 if (s->last_displayed_line >= s->blame.nlines &&
4620 s->selected_line < view->nlines - 2) {
4621 s->selected_line = MIN(s->blame.nlines,
4622 view->nlines - 2);
4623 break;
4625 if (s->last_displayed_line + view->nlines - 2
4626 <= s->blame.nlines)
4627 s->first_displayed_line +=
4628 view->nlines - 2;
4629 else
4630 s->first_displayed_line =
4631 s->blame.nlines -
4632 (view->nlines - 3);
4633 break;
4634 case KEY_RESIZE:
4635 if (s->selected_line > view->nlines - 2) {
4636 s->selected_line = MIN(s->blame.nlines,
4637 view->nlines - 2);
4639 break;
4640 default:
4641 break;
4643 return thread_err ? thread_err : err;
4646 static const struct got_error *
4647 cmd_blame(int argc, char *argv[])
4649 const struct got_error *error;
4650 struct got_repository *repo = NULL;
4651 struct got_worktree *worktree = NULL;
4652 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4653 char *link_target = NULL;
4654 struct got_object_id *commit_id = NULL;
4655 char *commit_id_str = NULL;
4656 int ch;
4657 struct tog_view *view;
4659 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4660 switch (ch) {
4661 case 'c':
4662 commit_id_str = optarg;
4663 break;
4664 case 'r':
4665 repo_path = realpath(optarg, NULL);
4666 if (repo_path == NULL)
4667 return got_error_from_errno2("realpath",
4668 optarg);
4669 break;
4670 default:
4671 usage_blame();
4672 /* NOTREACHED */
4676 argc -= optind;
4677 argv += optind;
4679 if (argc != 1)
4680 usage_blame();
4682 cwd = getcwd(NULL, 0);
4683 if (cwd == NULL)
4684 return got_error_from_errno("getcwd");
4686 error = got_worktree_open(&worktree, cwd);
4687 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4688 goto done;
4690 if (repo_path == NULL) {
4691 if (worktree)
4692 repo_path =
4693 strdup(got_worktree_get_repo_path(worktree));
4694 else
4695 repo_path = strdup(cwd);
4697 if (repo_path == NULL) {
4698 error = got_error_from_errno("strdup");
4699 goto done;
4702 error = got_repo_open(&repo, repo_path, NULL);
4703 if (error != NULL)
4704 goto done;
4706 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4707 worktree);
4708 if (error)
4709 goto done;
4711 init_curses();
4713 error = apply_unveil(got_repo_get_path(repo), NULL);
4714 if (error)
4715 goto done;
4717 if (commit_id_str == NULL) {
4718 struct got_reference *head_ref;
4719 error = got_ref_open(&head_ref, repo, worktree ?
4720 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4721 if (error != NULL)
4722 goto done;
4723 error = got_ref_resolve(&commit_id, repo, head_ref);
4724 got_ref_close(head_ref);
4725 } else {
4726 error = got_repo_match_object_id(&commit_id, NULL,
4727 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4729 if (error != NULL)
4730 goto done;
4732 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4733 if (view == NULL) {
4734 error = got_error_from_errno("view_open");
4735 goto done;
4738 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4739 commit_id, repo);
4740 if (error)
4741 goto done;
4743 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4744 commit_id, repo);
4745 if (error)
4746 goto done;
4747 if (worktree) {
4748 /* Release work tree lock. */
4749 got_worktree_close(worktree);
4750 worktree = NULL;
4752 error = view_loop(view);
4753 done:
4754 free(repo_path);
4755 free(in_repo_path);
4756 free(link_target);
4757 free(cwd);
4758 free(commit_id);
4759 if (worktree)
4760 got_worktree_close(worktree);
4761 if (repo)
4762 got_repo_close(repo);
4763 return error;
4766 static const struct got_error *
4767 draw_tree_entries(struct tog_view *view, const char *parent_path)
4769 struct tog_tree_view_state *s = &view->state.tree;
4770 const struct got_error *err = NULL;
4771 struct got_tree_entry *te;
4772 wchar_t *wline;
4773 struct tog_color *tc;
4774 int width, n, i, nentries;
4775 int limit = view->nlines;
4777 s->ndisplayed = 0;
4779 werase(view->window);
4781 if (limit == 0)
4782 return NULL;
4784 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4785 if (err)
4786 return err;
4787 if (view_needs_focus_indication(view))
4788 wstandout(view->window);
4789 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4790 if (tc)
4791 wattr_on(view->window,
4792 COLOR_PAIR(tc->colorpair), NULL);
4793 waddwstr(view->window, wline);
4794 if (tc)
4795 wattr_off(view->window,
4796 COLOR_PAIR(tc->colorpair), NULL);
4797 if (view_needs_focus_indication(view))
4798 wstandend(view->window);
4799 free(wline);
4800 wline = NULL;
4801 if (width < view->ncols - 1)
4802 waddch(view->window, '\n');
4803 if (--limit <= 0)
4804 return NULL;
4805 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4806 if (err)
4807 return err;
4808 waddwstr(view->window, wline);
4809 free(wline);
4810 wline = NULL;
4811 if (width < view->ncols - 1)
4812 waddch(view->window, '\n');
4813 if (--limit <= 0)
4814 return NULL;
4815 waddch(view->window, '\n');
4816 if (--limit <= 0)
4817 return NULL;
4819 if (s->first_displayed_entry == NULL) {
4820 te = got_object_tree_get_first_entry(s->tree);
4821 if (s->selected == 0) {
4822 if (view->focussed)
4823 wstandout(view->window);
4824 s->selected_entry = NULL;
4826 waddstr(view->window, " ..\n"); /* parent directory */
4827 if (s->selected == 0 && view->focussed)
4828 wstandend(view->window);
4829 s->ndisplayed++;
4830 if (--limit <= 0)
4831 return NULL;
4832 n = 1;
4833 } else {
4834 n = 0;
4835 te = s->first_displayed_entry;
4838 nentries = got_object_tree_get_nentries(s->tree);
4839 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4840 char *line = NULL, *id_str = NULL, *link_target = NULL;
4841 const char *modestr = "";
4842 mode_t mode;
4844 te = got_object_tree_get_entry(s->tree, i);
4845 mode = got_tree_entry_get_mode(te);
4847 if (s->show_ids) {
4848 err = got_object_id_str(&id_str,
4849 got_tree_entry_get_id(te));
4850 if (err)
4851 return got_error_from_errno(
4852 "got_object_id_str");
4854 if (got_object_tree_entry_is_submodule(te))
4855 modestr = "$";
4856 else if (S_ISLNK(mode)) {
4857 int i;
4859 err = got_tree_entry_get_symlink_target(&link_target,
4860 te, s->repo);
4861 if (err) {
4862 free(id_str);
4863 return err;
4865 for (i = 0; i < strlen(link_target); i++) {
4866 if (!isprint((unsigned char)link_target[i]))
4867 link_target[i] = '?';
4869 modestr = "@";
4871 else if (S_ISDIR(mode))
4872 modestr = "/";
4873 else if (mode & S_IXUSR)
4874 modestr = "*";
4875 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4876 got_tree_entry_get_name(te), modestr,
4877 link_target ? " -> ": "",
4878 link_target ? link_target : "") == -1) {
4879 free(id_str);
4880 free(link_target);
4881 return got_error_from_errno("asprintf");
4883 free(id_str);
4884 free(link_target);
4885 err = format_line(&wline, &width, line, view->ncols, 0);
4886 if (err) {
4887 free(line);
4888 break;
4890 if (n == s->selected) {
4891 if (view->focussed)
4892 wstandout(view->window);
4893 s->selected_entry = te;
4895 tc = match_color(&s->colors, line);
4896 if (tc)
4897 wattr_on(view->window,
4898 COLOR_PAIR(tc->colorpair), NULL);
4899 waddwstr(view->window, wline);
4900 if (tc)
4901 wattr_off(view->window,
4902 COLOR_PAIR(tc->colorpair), NULL);
4903 if (width < view->ncols - 1)
4904 waddch(view->window, '\n');
4905 if (n == s->selected && view->focussed)
4906 wstandend(view->window);
4907 free(line);
4908 free(wline);
4909 wline = NULL;
4910 n++;
4911 s->ndisplayed++;
4912 s->last_displayed_entry = te;
4913 if (--limit <= 0)
4914 break;
4917 return err;
4920 static void
4921 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4923 struct got_tree_entry *te;
4924 int isroot = s->tree == s->root;
4925 int i = 0;
4927 if (s->first_displayed_entry == NULL)
4928 return;
4930 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4931 while (i++ < maxscroll) {
4932 if (te == NULL) {
4933 if (!isroot)
4934 s->first_displayed_entry = NULL;
4935 break;
4937 s->first_displayed_entry = te;
4938 te = got_tree_entry_get_prev(s->tree, te);
4942 static void
4943 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4945 struct got_tree_entry *next, *last;
4946 int n = 0;
4948 if (s->first_displayed_entry)
4949 next = got_tree_entry_get_next(s->tree,
4950 s->first_displayed_entry);
4951 else
4952 next = got_object_tree_get_first_entry(s->tree);
4954 last = s->last_displayed_entry;
4955 while (next && last && n++ < maxscroll) {
4956 last = got_tree_entry_get_next(s->tree, last);
4957 if (last) {
4958 s->first_displayed_entry = next;
4959 next = got_tree_entry_get_next(s->tree, next);
4964 static const struct got_error *
4965 tree_entry_path(char **path, struct tog_parent_trees *parents,
4966 struct got_tree_entry *te)
4968 const struct got_error *err = NULL;
4969 struct tog_parent_tree *pt;
4970 size_t len = 2; /* for leading slash and NUL */
4972 TAILQ_FOREACH(pt, parents, entry)
4973 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4974 + 1 /* slash */;
4975 if (te)
4976 len += strlen(got_tree_entry_get_name(te));
4978 *path = calloc(1, len);
4979 if (path == NULL)
4980 return got_error_from_errno("calloc");
4982 (*path)[0] = '/';
4983 pt = TAILQ_LAST(parents, tog_parent_trees);
4984 while (pt) {
4985 const char *name = got_tree_entry_get_name(pt->selected_entry);
4986 if (strlcat(*path, name, len) >= len) {
4987 err = got_error(GOT_ERR_NO_SPACE);
4988 goto done;
4990 if (strlcat(*path, "/", len) >= len) {
4991 err = got_error(GOT_ERR_NO_SPACE);
4992 goto done;
4994 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4996 if (te) {
4997 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4998 err = got_error(GOT_ERR_NO_SPACE);
4999 goto done;
5002 done:
5003 if (err) {
5004 free(*path);
5005 *path = NULL;
5007 return err;
5010 static const struct got_error *
5011 blame_tree_entry(struct tog_view **new_view, int begin_x,
5012 struct got_tree_entry *te, struct tog_parent_trees *parents,
5013 struct got_object_id *commit_id, struct got_repository *repo)
5015 const struct got_error *err = NULL;
5016 char *path;
5017 struct tog_view *blame_view;
5019 *new_view = NULL;
5021 err = tree_entry_path(&path, parents, te);
5022 if (err)
5023 return err;
5025 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5026 if (blame_view == NULL) {
5027 err = got_error_from_errno("view_open");
5028 goto done;
5031 err = open_blame_view(blame_view, path, commit_id, repo);
5032 if (err) {
5033 if (err->code == GOT_ERR_CANCELLED)
5034 err = NULL;
5035 view_close(blame_view);
5036 } else
5037 *new_view = blame_view;
5038 done:
5039 free(path);
5040 return err;
5043 static const struct got_error *
5044 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5045 struct tog_tree_view_state *s)
5047 struct tog_view *log_view;
5048 const struct got_error *err = NULL;
5049 char *path;
5051 *new_view = NULL;
5053 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5054 if (log_view == NULL)
5055 return got_error_from_errno("view_open");
5057 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5058 if (err)
5059 return err;
5061 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5062 path, 0);
5063 if (err)
5064 view_close(log_view);
5065 else
5066 *new_view = log_view;
5067 free(path);
5068 return err;
5071 static const struct got_error *
5072 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5073 struct got_object_id *commit_id, const char *head_ref_name,
5074 struct got_repository *repo)
5076 const struct got_error *err = NULL;
5077 char *commit_id_str = NULL;
5078 struct tog_tree_view_state *s = &view->state.tree;
5080 TAILQ_INIT(&s->parents);
5082 err = got_object_id_str(&commit_id_str, commit_id);
5083 if (err != NULL)
5084 goto done;
5086 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5087 err = got_error_from_errno("asprintf");
5088 goto done;
5091 s->root = s->tree = root;
5092 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5093 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5094 s->commit_id = got_object_id_dup(commit_id);
5095 if (s->commit_id == NULL) {
5096 err = got_error_from_errno("got_object_id_dup");
5097 goto done;
5099 if (head_ref_name) {
5100 s->head_ref_name = strdup(head_ref_name);
5101 if (s->head_ref_name == NULL) {
5102 err = got_error_from_errno("strdup");
5103 goto done;
5106 s->repo = repo;
5108 SIMPLEQ_INIT(&s->colors);
5110 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5111 err = add_color(&s->colors, "\\$$",
5112 TOG_COLOR_TREE_SUBMODULE,
5113 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5114 if (err)
5115 goto done;
5116 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5117 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5118 if (err) {
5119 free_colors(&s->colors);
5120 goto done;
5122 err = add_color(&s->colors, "/$",
5123 TOG_COLOR_TREE_DIRECTORY,
5124 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5125 if (err) {
5126 free_colors(&s->colors);
5127 goto done;
5130 err = add_color(&s->colors, "\\*$",
5131 TOG_COLOR_TREE_EXECUTABLE,
5132 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5133 if (err) {
5134 free_colors(&s->colors);
5135 goto done;
5138 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5139 get_color_value("TOG_COLOR_COMMIT"));
5140 if (err) {
5141 free_colors(&s->colors);
5142 goto done;
5146 view->show = show_tree_view;
5147 view->input = input_tree_view;
5148 view->close = close_tree_view;
5149 view->search_start = search_start_tree_view;
5150 view->search_next = search_next_tree_view;
5151 done:
5152 free(commit_id_str);
5153 if (err) {
5154 free(s->tree_label);
5155 s->tree_label = NULL;
5157 return err;
5160 static const struct got_error *
5161 close_tree_view(struct tog_view *view)
5163 struct tog_tree_view_state *s = &view->state.tree;
5165 free_colors(&s->colors);
5166 free(s->tree_label);
5167 s->tree_label = NULL;
5168 free(s->commit_id);
5169 s->commit_id = NULL;
5170 free(s->head_ref_name);
5171 s->head_ref_name = NULL;
5172 while (!TAILQ_EMPTY(&s->parents)) {
5173 struct tog_parent_tree *parent;
5174 parent = TAILQ_FIRST(&s->parents);
5175 TAILQ_REMOVE(&s->parents, parent, entry);
5176 free(parent);
5179 if (s->tree != s->root)
5180 got_object_tree_close(s->tree);
5181 got_object_tree_close(s->root);
5182 return NULL;
5185 static const struct got_error *
5186 search_start_tree_view(struct tog_view *view)
5188 struct tog_tree_view_state *s = &view->state.tree;
5190 s->matched_entry = NULL;
5191 return NULL;
5194 static int
5195 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5197 regmatch_t regmatch;
5199 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5200 0) == 0;
5203 static const struct got_error *
5204 search_next_tree_view(struct tog_view *view)
5206 struct tog_tree_view_state *s = &view->state.tree;
5207 struct got_tree_entry *te = NULL;
5209 if (!view->searching) {
5210 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5211 return NULL;
5214 if (s->matched_entry) {
5215 if (view->searching == TOG_SEARCH_FORWARD) {
5216 if (s->selected_entry)
5217 te = got_tree_entry_get_next(s->tree,
5218 s->selected_entry);
5219 else
5220 te = got_object_tree_get_first_entry(s->tree);
5221 } else {
5222 if (s->selected_entry == NULL)
5223 te = got_object_tree_get_last_entry(s->tree);
5224 else
5225 te = got_tree_entry_get_prev(s->tree,
5226 s->selected_entry);
5228 } else {
5229 if (view->searching == TOG_SEARCH_FORWARD)
5230 te = got_object_tree_get_first_entry(s->tree);
5231 else
5232 te = got_object_tree_get_last_entry(s->tree);
5235 while (1) {
5236 if (te == NULL) {
5237 if (s->matched_entry == NULL) {
5238 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5239 return NULL;
5241 if (view->searching == TOG_SEARCH_FORWARD)
5242 te = got_object_tree_get_first_entry(s->tree);
5243 else
5244 te = got_object_tree_get_last_entry(s->tree);
5247 if (match_tree_entry(te, &view->regex)) {
5248 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5249 s->matched_entry = te;
5250 break;
5253 if (view->searching == TOG_SEARCH_FORWARD)
5254 te = got_tree_entry_get_next(s->tree, te);
5255 else
5256 te = got_tree_entry_get_prev(s->tree, te);
5259 if (s->matched_entry) {
5260 s->first_displayed_entry = s->matched_entry;
5261 s->selected = 0;
5264 return NULL;
5267 static const struct got_error *
5268 show_tree_view(struct tog_view *view)
5270 const struct got_error *err = NULL;
5271 struct tog_tree_view_state *s = &view->state.tree;
5272 char *parent_path;
5274 err = tree_entry_path(&parent_path, &s->parents, NULL);
5275 if (err)
5276 return err;
5278 err = draw_tree_entries(view, parent_path);
5279 free(parent_path);
5281 view_vborder(view);
5282 return err;
5285 static const struct got_error *
5286 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5288 const struct got_error *err = NULL;
5289 struct tog_tree_view_state *s = &view->state.tree;
5290 struct tog_view *log_view, *ref_view;
5291 int begin_x = 0;
5293 switch (ch) {
5294 case 'i':
5295 s->show_ids = !s->show_ids;
5296 break;
5297 case 'l':
5298 if (!s->selected_entry)
5299 break;
5300 if (view_is_parent_view(view))
5301 begin_x = view_split_begin_x(view->begin_x);
5302 err = log_selected_tree_entry(&log_view, begin_x, s);
5303 view->focussed = 0;
5304 log_view->focussed = 1;
5305 if (view_is_parent_view(view)) {
5306 err = view_close_child(view);
5307 if (err)
5308 return err;
5309 view_set_child(view, log_view);
5310 view->focus_child = 1;
5311 } else
5312 *new_view = log_view;
5313 break;
5314 case 'r':
5315 if (view_is_parent_view(view))
5316 begin_x = view_split_begin_x(view->begin_x);
5317 ref_view = view_open(view->nlines, view->ncols,
5318 view->begin_y, begin_x, TOG_VIEW_REF);
5319 if (ref_view == NULL)
5320 return got_error_from_errno("view_open");
5321 err = open_ref_view(ref_view, s->repo);
5322 if (err) {
5323 view_close(ref_view);
5324 return err;
5326 view->focussed = 0;
5327 ref_view->focussed = 1;
5328 if (view_is_parent_view(view)) {
5329 err = view_close_child(view);
5330 if (err)
5331 return err;
5332 view_set_child(view, ref_view);
5333 view->focus_child = 1;
5334 } else
5335 *new_view = ref_view;
5336 break;
5337 case 'k':
5338 case KEY_UP:
5339 if (s->selected > 0) {
5340 s->selected--;
5341 break;
5343 tree_scroll_up(s, 1);
5344 break;
5345 case KEY_PPAGE:
5346 case CTRL('b'):
5347 if (s->tree == s->root) {
5348 if (got_object_tree_get_first_entry(s->tree) ==
5349 s->first_displayed_entry)
5350 s->selected = 0;
5351 } else {
5352 if (s->first_displayed_entry == NULL)
5353 s->selected = 0;
5355 tree_scroll_up(s, MAX(0, view->nlines - 3));
5356 break;
5357 case 'j':
5358 case KEY_DOWN:
5359 if (s->selected < s->ndisplayed - 1) {
5360 s->selected++;
5361 break;
5363 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5364 == NULL)
5365 /* can't scroll any further */
5366 break;
5367 tree_scroll_down(s, 1);
5368 break;
5369 case KEY_NPAGE:
5370 case CTRL('f'):
5371 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5372 == NULL) {
5373 /* can't scroll any further; move cursor down */
5374 if (s->selected < s->ndisplayed - 1)
5375 s->selected = s->ndisplayed - 1;
5376 break;
5378 tree_scroll_down(s, view->nlines - 3);
5379 break;
5380 case KEY_ENTER:
5381 case '\r':
5382 case KEY_BACKSPACE:
5383 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5384 struct tog_parent_tree *parent;
5385 /* user selected '..' */
5386 if (s->tree == s->root)
5387 break;
5388 parent = TAILQ_FIRST(&s->parents);
5389 TAILQ_REMOVE(&s->parents, parent,
5390 entry);
5391 got_object_tree_close(s->tree);
5392 s->tree = parent->tree;
5393 s->first_displayed_entry =
5394 parent->first_displayed_entry;
5395 s->selected_entry =
5396 parent->selected_entry;
5397 s->selected = parent->selected;
5398 free(parent);
5399 } else if (S_ISDIR(got_tree_entry_get_mode(
5400 s->selected_entry))) {
5401 struct got_tree_object *subtree;
5402 err = got_object_open_as_tree(&subtree, s->repo,
5403 got_tree_entry_get_id(s->selected_entry));
5404 if (err)
5405 break;
5406 err = tree_view_visit_subtree(s, subtree);
5407 if (err) {
5408 got_object_tree_close(subtree);
5409 break;
5411 } else if (S_ISREG(got_tree_entry_get_mode(
5412 s->selected_entry))) {
5413 struct tog_view *blame_view;
5414 int begin_x = view_is_parent_view(view) ?
5415 view_split_begin_x(view->begin_x) : 0;
5417 err = blame_tree_entry(&blame_view, begin_x,
5418 s->selected_entry, &s->parents,
5419 s->commit_id, s->repo);
5420 if (err)
5421 break;
5422 view->focussed = 0;
5423 blame_view->focussed = 1;
5424 if (view_is_parent_view(view)) {
5425 err = view_close_child(view);
5426 if (err)
5427 return err;
5428 view_set_child(view, blame_view);
5429 view->focus_child = 1;
5430 } else
5431 *new_view = blame_view;
5433 break;
5434 case KEY_RESIZE:
5435 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5436 s->selected = view->nlines - 4;
5437 break;
5438 default:
5439 break;
5442 return err;
5445 __dead static void
5446 usage_tree(void)
5448 endwin();
5449 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5450 getprogname());
5451 exit(1);
5454 static const struct got_error *
5455 cmd_tree(int argc, char *argv[])
5457 const struct got_error *error;
5458 struct got_repository *repo = NULL;
5459 struct got_worktree *worktree = NULL;
5460 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5461 struct got_object_id *commit_id = NULL;
5462 const char *commit_id_arg = NULL;
5463 char *label = NULL;
5464 struct got_commit_object *commit = NULL;
5465 struct got_tree_object *tree = NULL;
5466 struct got_reference *ref = NULL;
5467 const char *head_ref_name = NULL;
5468 int ch;
5469 struct tog_view *view;
5471 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5472 switch (ch) {
5473 case 'c':
5474 commit_id_arg = optarg;
5475 break;
5476 case 'r':
5477 repo_path = realpath(optarg, NULL);
5478 if (repo_path == NULL)
5479 return got_error_from_errno2("realpath",
5480 optarg);
5481 break;
5482 default:
5483 usage_tree();
5484 /* NOTREACHED */
5488 argc -= optind;
5489 argv += optind;
5491 if (argc > 1)
5492 usage_tree();
5494 cwd = getcwd(NULL, 0);
5495 if (cwd == NULL)
5496 return got_error_from_errno("getcwd");
5498 error = got_worktree_open(&worktree, cwd);
5499 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5500 goto done;
5502 if (repo_path == NULL) {
5503 if (worktree)
5504 repo_path =
5505 strdup(got_worktree_get_repo_path(worktree));
5506 else
5507 repo_path = strdup(cwd);
5509 if (repo_path == NULL) {
5510 error = got_error_from_errno("strdup");
5511 goto done;
5514 error = got_repo_open(&repo, repo_path, NULL);
5515 if (error != NULL)
5516 goto done;
5518 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5519 repo, worktree);
5520 if (error)
5521 goto done;
5523 init_curses();
5525 error = apply_unveil(got_repo_get_path(repo), NULL);
5526 if (error)
5527 goto done;
5529 if (commit_id_arg == NULL) {
5530 error = got_repo_match_object_id(&commit_id, &label,
5531 worktree ? got_worktree_get_head_ref_name(worktree) :
5532 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5533 if (error)
5534 goto done;
5535 head_ref_name = label;
5536 } else {
5537 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5538 if (error == NULL)
5539 head_ref_name = got_ref_get_name(ref);
5540 else if (error->code != GOT_ERR_NOT_REF)
5541 goto done;
5542 error = got_repo_match_object_id(&commit_id, NULL,
5543 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5544 if (error)
5545 goto done;
5548 error = got_object_open_as_commit(&commit, repo, commit_id);
5549 if (error)
5550 goto done;
5552 error = got_object_open_as_tree(&tree, repo,
5553 got_object_commit_get_tree_id(commit));
5554 if (error)
5555 goto done;
5557 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5558 if (view == NULL) {
5559 error = got_error_from_errno("view_open");
5560 goto done;
5562 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5563 if (error)
5564 goto done;
5565 if (!got_path_is_root_dir(in_repo_path)) {
5566 error = tree_view_walk_path(&view->state.tree, commit_id,
5567 in_repo_path);
5568 if (error)
5569 goto done;
5572 if (worktree) {
5573 /* Release work tree lock. */
5574 got_worktree_close(worktree);
5575 worktree = NULL;
5577 error = view_loop(view);
5578 done:
5579 free(repo_path);
5580 free(cwd);
5581 free(commit_id);
5582 free(label);
5583 if (ref)
5584 got_ref_close(ref);
5585 if (commit)
5586 got_object_commit_close(commit);
5587 if (tree)
5588 got_object_tree_close(tree);
5589 if (repo)
5590 got_repo_close(repo);
5591 return error;
5594 static const struct got_error *
5595 ref_view_load_refs(struct tog_ref_view_state *s)
5597 const struct got_error *err;
5598 struct got_reflist_entry *sre;
5599 struct tog_reflist_entry *re;
5601 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5602 got_ref_cmp_by_name, NULL);
5603 if (err)
5604 return err;
5606 s->nrefs = 0;
5607 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5608 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5609 continue;
5611 re = malloc(sizeof(*re));
5612 if (re == NULL)
5613 return got_error_from_errno("malloc");
5615 re->ref = sre->ref;
5616 re->idx = s->nrefs++;
5617 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5620 return NULL;
5623 void
5624 ref_view_free_refs(struct tog_ref_view_state *s)
5626 struct tog_reflist_entry *re;
5628 while (!TAILQ_EMPTY(&s->refs)) {
5629 re = TAILQ_FIRST(&s->refs);
5630 TAILQ_REMOVE(&s->refs, re, entry);
5631 free(re);
5633 got_ref_list_free(&s->simplerefs);
5636 static const struct got_error *
5637 open_ref_view(struct tog_view *view, struct got_repository *repo)
5639 const struct got_error *err = NULL;
5640 struct tog_ref_view_state *s = &view->state.ref;
5642 s->selected_entry = 0;
5643 s->repo = repo;
5645 SIMPLEQ_INIT(&s->simplerefs);
5646 TAILQ_INIT(&s->refs);
5647 SIMPLEQ_INIT(&s->colors);
5649 err = ref_view_load_refs(s);
5650 if (err)
5651 return err;
5653 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5655 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5656 err = add_color(&s->colors, "^refs/heads/",
5657 TOG_COLOR_REFS_HEADS,
5658 get_color_value("TOG_COLOR_REFS_HEADS"));
5659 if (err)
5660 goto done;
5662 err = add_color(&s->colors, "^refs/tags/",
5663 TOG_COLOR_REFS_TAGS,
5664 get_color_value("TOG_COLOR_REFS_TAGS"));
5665 if (err)
5666 goto done;
5668 err = add_color(&s->colors, "^refs/remotes/",
5669 TOG_COLOR_REFS_REMOTES,
5670 get_color_value("TOG_COLOR_REFS_REMOTES"));
5671 if (err)
5672 goto done;
5675 view->show = show_ref_view;
5676 view->input = input_ref_view;
5677 view->close = close_ref_view;
5678 view->search_start = search_start_ref_view;
5679 view->search_next = search_next_ref_view;
5680 done:
5681 if (err)
5682 free_colors(&s->colors);
5683 return err;
5686 static const struct got_error *
5687 close_ref_view(struct tog_view *view)
5689 struct tog_ref_view_state *s = &view->state.ref;
5691 ref_view_free_refs(s);
5692 free_colors(&s->colors);
5694 return NULL;
5697 static const struct got_error *
5698 resolve_reflist_entry(struct got_object_id **commit_id,
5699 struct tog_reflist_entry *re, struct got_repository *repo)
5701 const struct got_error *err = NULL;
5702 struct got_object_id *obj_id;
5703 struct got_tag_object *tag = NULL;
5704 int obj_type;
5706 *commit_id = NULL;
5708 err = got_ref_resolve(&obj_id, repo, re->ref);
5709 if (err)
5710 return err;
5712 err = got_object_get_type(&obj_type, repo, obj_id);
5713 if (err)
5714 goto done;
5716 switch (obj_type) {
5717 case GOT_OBJ_TYPE_COMMIT:
5718 *commit_id = obj_id;
5719 break;
5720 case GOT_OBJ_TYPE_TAG:
5721 err = got_object_open_as_tag(&tag, repo, obj_id);
5722 if (err)
5723 goto done;
5724 free(obj_id);
5725 err = got_object_get_type(&obj_type, repo,
5726 got_object_tag_get_object_id(tag));
5727 if (err)
5728 goto done;
5729 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5730 err = got_error(GOT_ERR_OBJ_TYPE);
5731 goto done;
5733 *commit_id = got_object_id_dup(
5734 got_object_tag_get_object_id(tag));
5735 if (*commit_id == NULL) {
5736 err = got_error_from_errno("got_object_id_dup");
5737 goto done;
5739 break;
5740 default:
5741 err = got_error(GOT_ERR_OBJ_TYPE);
5742 break;
5745 done:
5746 if (tag)
5747 got_object_tag_close(tag);
5748 if (err) {
5749 free(*commit_id);
5750 *commit_id = NULL;
5752 return err;
5755 static const struct got_error *
5756 log_ref_entry(struct tog_view **new_view, int begin_x,
5757 struct tog_reflist_entry *re, struct got_repository *repo)
5759 struct tog_view *log_view;
5760 const struct got_error *err = NULL;
5761 struct got_object_id *commit_id = NULL;
5763 *new_view = NULL;
5765 err = resolve_reflist_entry(&commit_id, re, repo);
5766 if (err) {
5767 if (err->code != GOT_ERR_OBJ_TYPE)
5768 return err;
5769 else
5770 return NULL;
5773 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5774 if (log_view == NULL) {
5775 err = got_error_from_errno("view_open");
5776 goto done;
5779 err = open_log_view(log_view, commit_id, repo,
5780 got_ref_get_name(re->ref), "", 0);
5781 done:
5782 if (err)
5783 view_close(log_view);
5784 else
5785 *new_view = log_view;
5786 free(commit_id);
5787 return err;
5790 static void
5791 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5793 struct tog_reflist_entry *re;
5794 int i = 0;
5796 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5797 return;
5799 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5800 while (i++ < maxscroll) {
5801 if (re == NULL)
5802 break;
5803 s->first_displayed_entry = re;
5804 re = TAILQ_PREV(re, tog_reflist_head, entry);
5808 static void
5809 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5811 struct tog_reflist_entry *next, *last;
5812 int n = 0;
5814 if (s->first_displayed_entry)
5815 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5816 else
5817 next = TAILQ_FIRST(&s->refs);
5819 last = s->last_displayed_entry;
5820 while (next && last && n++ < maxscroll) {
5821 last = TAILQ_NEXT(last, entry);
5822 if (last) {
5823 s->first_displayed_entry = next;
5824 next = TAILQ_NEXT(next, entry);
5829 static const struct got_error *
5830 search_start_ref_view(struct tog_view *view)
5832 struct tog_ref_view_state *s = &view->state.ref;
5834 s->matched_entry = NULL;
5835 return NULL;
5838 static int
5839 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5841 regmatch_t regmatch;
5843 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5844 0) == 0;
5847 static const struct got_error *
5848 search_next_ref_view(struct tog_view *view)
5850 struct tog_ref_view_state *s = &view->state.ref;
5851 struct tog_reflist_entry *re = NULL;
5853 if (!view->searching) {
5854 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5855 return NULL;
5858 if (s->matched_entry) {
5859 if (view->searching == TOG_SEARCH_FORWARD) {
5860 if (s->selected_entry)
5861 re = TAILQ_NEXT(s->selected_entry, entry);
5862 else
5863 re = TAILQ_PREV(s->selected_entry,
5864 tog_reflist_head, entry);
5865 } else {
5866 if (s->selected_entry == NULL)
5867 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5868 else
5869 re = TAILQ_PREV(s->selected_entry,
5870 tog_reflist_head, entry);
5872 } else {
5873 if (view->searching == TOG_SEARCH_FORWARD)
5874 re = TAILQ_FIRST(&s->refs);
5875 else
5876 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5879 while (1) {
5880 if (re == NULL) {
5881 if (s->matched_entry == NULL) {
5882 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5883 return NULL;
5885 if (view->searching == TOG_SEARCH_FORWARD)
5886 re = TAILQ_FIRST(&s->refs);
5887 else
5888 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5891 if (match_reflist_entry(re, &view->regex)) {
5892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5893 s->matched_entry = re;
5894 break;
5897 if (view->searching == TOG_SEARCH_FORWARD)
5898 re = TAILQ_NEXT(re, entry);
5899 else
5900 re = TAILQ_PREV(re, tog_reflist_head, entry);
5903 if (s->matched_entry) {
5904 s->first_displayed_entry = s->matched_entry;
5905 s->selected = 0;
5908 return NULL;
5911 static const struct got_error *
5912 show_ref_view(struct tog_view *view)
5914 const struct got_error *err = NULL;
5915 struct tog_ref_view_state *s = &view->state.ref;
5916 struct tog_reflist_entry *re;
5917 char *line = NULL;
5918 wchar_t *wline;
5919 struct tog_color *tc;
5920 int width, n;
5921 int limit = view->nlines;
5923 werase(view->window);
5925 s->ndisplayed = 0;
5927 if (limit == 0)
5928 return NULL;
5930 re = s->first_displayed_entry;
5932 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5933 s->nrefs) == -1)
5934 return got_error_from_errno("asprintf");
5936 err = format_line(&wline, &width, line, view->ncols, 0);
5937 if (err) {
5938 free(line);
5939 return err;
5941 if (view_needs_focus_indication(view))
5942 wstandout(view->window);
5943 waddwstr(view->window, wline);
5944 if (view_needs_focus_indication(view))
5945 wstandend(view->window);
5946 free(wline);
5947 wline = NULL;
5948 free(line);
5949 line = NULL;
5950 if (width < view->ncols - 1)
5951 waddch(view->window, '\n');
5952 if (--limit <= 0)
5953 return NULL;
5955 n = 0;
5956 while (re && limit > 0) {
5957 char *line = NULL;
5959 if (got_ref_is_symbolic(re->ref)) {
5960 if (asprintf(&line, "%s -> %s",
5961 got_ref_get_name(re->ref),
5962 got_ref_get_symref_target(re->ref)) == -1)
5963 return got_error_from_errno("asprintf");
5964 } else if (s->show_ids) {
5965 struct got_object_id *id;
5966 char *id_str;
5967 err = got_ref_resolve(&id, s->repo, re->ref);
5968 if (err)
5969 return err;
5970 err = got_object_id_str(&id_str, id);
5971 if (err) {
5972 free(id);
5973 return err;
5975 if (asprintf(&line, "%s: %s",
5976 got_ref_get_name(re->ref), id_str) == -1) {
5977 err = got_error_from_errno("asprintf");
5978 free(id);
5979 free(id_str);
5980 return err;
5982 free(id);
5983 free(id_str);
5984 } else {
5985 line = strdup(got_ref_get_name(re->ref));
5986 if (line == NULL)
5987 return got_error_from_errno("strdup");
5990 err = format_line(&wline, &width, line, view->ncols, 0);
5991 if (err) {
5992 free(line);
5993 return err;
5995 if (n == s->selected) {
5996 if (view->focussed)
5997 wstandout(view->window);
5998 s->selected_entry = re;
6000 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6001 if (tc)
6002 wattr_on(view->window,
6003 COLOR_PAIR(tc->colorpair), NULL);
6004 waddwstr(view->window, wline);
6005 if (tc)
6006 wattr_off(view->window,
6007 COLOR_PAIR(tc->colorpair), NULL);
6008 if (width < view->ncols - 1)
6009 waddch(view->window, '\n');
6010 if (n == s->selected && view->focussed)
6011 wstandend(view->window);
6012 free(line);
6013 free(wline);
6014 wline = NULL;
6015 n++;
6016 s->ndisplayed++;
6017 s->last_displayed_entry = re;
6019 limit--;
6020 re = TAILQ_NEXT(re, entry);
6023 view_vborder(view);
6024 return err;
6027 static const struct got_error *
6028 browse_ref_tree(struct tog_view **new_view, int begin_x,
6029 struct tog_reflist_entry *re, struct got_repository *repo)
6031 const struct got_error *err = NULL;
6032 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6033 struct got_tree_object *tree = NULL;
6034 struct tog_view *tree_view;
6036 *new_view = NULL;
6038 err = resolve_reflist_entry(&commit_id, re, repo);
6039 if (err) {
6040 if (err->code != GOT_ERR_OBJ_TYPE)
6041 return err;
6042 else
6043 return NULL;
6046 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6047 if (err)
6048 goto done;
6050 err = got_object_open_as_tree(&tree, repo, tree_id);
6051 if (err)
6052 goto done;
6054 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6055 if (tree_view == NULL) {
6056 err = got_error_from_errno("view_open");
6057 goto done;
6060 err = open_tree_view(tree_view, tree, commit_id,
6061 got_ref_get_name(re->ref), repo);
6062 if (err)
6063 goto done;
6065 *new_view = tree_view;
6066 done:
6067 free(commit_id);
6068 free(tree_id);
6069 if (err) {
6070 if (tree)
6071 got_object_tree_close(tree);
6073 return err;
6075 static const struct got_error *
6076 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6078 const struct got_error *err = NULL;
6079 struct tog_ref_view_state *s = &view->state.ref;
6080 struct tog_view *log_view, *tree_view;
6081 int begin_x = 0;
6083 switch (ch) {
6084 case 'i':
6085 s->show_ids = !s->show_ids;
6086 break;
6087 case KEY_ENTER:
6088 case '\r':
6089 if (!s->selected_entry)
6090 break;
6091 if (view_is_parent_view(view))
6092 begin_x = view_split_begin_x(view->begin_x);
6093 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6094 s->repo);
6095 view->focussed = 0;
6096 log_view->focussed = 1;
6097 if (view_is_parent_view(view)) {
6098 err = view_close_child(view);
6099 if (err)
6100 return err;
6101 view_set_child(view, log_view);
6102 view->focus_child = 1;
6103 } else
6104 *new_view = log_view;
6105 break;
6106 case 't':
6107 if (!s->selected_entry)
6108 break;
6109 if (view_is_parent_view(view))
6110 begin_x = view_split_begin_x(view->begin_x);
6111 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6112 s->repo);
6113 if (err || tree_view == NULL)
6114 break;
6115 view->focussed = 0;
6116 tree_view->focussed = 1;
6117 if (view_is_parent_view(view)) {
6118 err = view_close_child(view);
6119 if (err)
6120 return err;
6121 view_set_child(view, tree_view);
6122 view->focus_child = 1;
6123 } else
6124 *new_view = tree_view;
6125 break;
6126 case 'k':
6127 case KEY_UP:
6128 if (s->selected > 0) {
6129 s->selected--;
6130 break;
6132 ref_scroll_up(s, 1);
6133 break;
6134 case KEY_PPAGE:
6135 case CTRL('b'):
6136 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6137 s->selected = 0;
6138 ref_scroll_up(s, MAX(0, view->nlines - 1));
6139 break;
6140 case 'j':
6141 case KEY_DOWN:
6142 if (s->selected < s->ndisplayed - 1) {
6143 s->selected++;
6144 break;
6146 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6147 /* can't scroll any further */
6148 break;
6149 ref_scroll_down(s, 1);
6150 break;
6151 case KEY_NPAGE:
6152 case CTRL('f'):
6153 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6154 /* can't scroll any further; move cursor down */
6155 if (s->selected < s->ndisplayed - 1)
6156 s->selected = s->ndisplayed - 1;
6157 break;
6159 ref_scroll_down(s, view->nlines - 1);
6160 break;
6161 case CTRL('l'):
6162 ref_view_free_refs(s);
6163 err = ref_view_load_refs(s);
6164 break;
6165 case KEY_RESIZE:
6166 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6167 s->selected = view->nlines - 2;
6168 break;
6169 default:
6170 break;
6173 return err;
6176 __dead static void
6177 usage_ref(void)
6179 endwin();
6180 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6181 getprogname());
6182 exit(1);
6185 static const struct got_error *
6186 cmd_ref(int argc, char *argv[])
6188 const struct got_error *error;
6189 struct got_repository *repo = NULL;
6190 struct got_worktree *worktree = NULL;
6191 char *cwd = NULL, *repo_path = NULL;
6192 int ch;
6193 struct tog_view *view;
6195 while ((ch = getopt(argc, argv, "r:")) != -1) {
6196 switch (ch) {
6197 case 'r':
6198 repo_path = realpath(optarg, NULL);
6199 if (repo_path == NULL)
6200 return got_error_from_errno2("realpath",
6201 optarg);
6202 break;
6203 default:
6204 usage_ref();
6205 /* NOTREACHED */
6209 argc -= optind;
6210 argv += optind;
6212 if (argc > 1)
6213 usage_ref();
6215 cwd = getcwd(NULL, 0);
6216 if (cwd == NULL)
6217 return got_error_from_errno("getcwd");
6219 error = got_worktree_open(&worktree, cwd);
6220 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6221 goto done;
6223 if (repo_path == NULL) {
6224 if (worktree)
6225 repo_path =
6226 strdup(got_worktree_get_repo_path(worktree));
6227 else
6228 repo_path = strdup(cwd);
6230 if (repo_path == NULL) {
6231 error = got_error_from_errno("strdup");
6232 goto done;
6235 error = got_repo_open(&repo, repo_path, NULL);
6236 if (error != NULL)
6237 goto done;
6239 init_curses();
6241 error = apply_unveil(got_repo_get_path(repo), NULL);
6242 if (error)
6243 goto done;
6245 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6246 if (view == NULL) {
6247 error = got_error_from_errno("view_open");
6248 goto done;
6251 error = open_ref_view(view, repo);
6252 if (error)
6253 goto done;
6255 if (worktree) {
6256 /* Release work tree lock. */
6257 got_worktree_close(worktree);
6258 worktree = NULL;
6260 error = view_loop(view);
6261 done:
6262 free(repo_path);
6263 free(cwd);
6264 if (repo)
6265 got_repo_close(repo);
6266 return error;
6269 static void
6270 list_commands(FILE *fp)
6272 int i;
6274 fprintf(fp, "commands:");
6275 for (i = 0; i < nitems(tog_commands); i++) {
6276 struct tog_cmd *cmd = &tog_commands[i];
6277 fprintf(fp, " %s", cmd->name);
6279 fputc('\n', fp);
6282 __dead static void
6283 usage(int hflag, int status)
6285 FILE *fp = (status == 0) ? stdout : stderr;
6287 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6288 getprogname());
6289 if (hflag) {
6290 fprintf(fp, "lazy usage: %s path\n", getprogname());
6291 list_commands(fp);
6293 exit(status);
6296 static char **
6297 make_argv(int argc, ...)
6299 va_list ap;
6300 char **argv;
6301 int i;
6303 va_start(ap, argc);
6305 argv = calloc(argc, sizeof(char *));
6306 if (argv == NULL)
6307 err(1, "calloc");
6308 for (i = 0; i < argc; i++) {
6309 argv[i] = strdup(va_arg(ap, char *));
6310 if (argv[i] == NULL)
6311 err(1, "strdup");
6314 va_end(ap);
6315 return argv;
6319 * Try to convert 'tog path' into a 'tog log path' command.
6320 * The user could simply have mistyped the command rather than knowingly
6321 * provided a path. So check whether argv[0] can in fact be resolved
6322 * to a path in the HEAD commit and print a special error if not.
6323 * This hack is for mpi@ <3
6325 static const struct got_error *
6326 tog_log_with_path(int argc, char *argv[])
6328 const struct got_error *error = NULL;
6329 struct tog_cmd *cmd = NULL;
6330 struct got_repository *repo = NULL;
6331 struct got_worktree *worktree = NULL;
6332 struct got_object_id *commit_id = NULL, *id = NULL;
6333 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6334 char *commit_id_str = NULL, **cmd_argv = NULL;
6336 cwd = getcwd(NULL, 0);
6337 if (cwd == NULL)
6338 return got_error_from_errno("getcwd");
6340 error = got_worktree_open(&worktree, cwd);
6341 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6342 goto done;
6344 if (worktree)
6345 repo_path = strdup(got_worktree_get_repo_path(worktree));
6346 else
6347 repo_path = strdup(cwd);
6348 if (repo_path == NULL) {
6349 error = got_error_from_errno("strdup");
6350 goto done;
6353 error = got_repo_open(&repo, repo_path, NULL);
6354 if (error != NULL)
6355 goto done;
6357 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6358 repo, worktree);
6359 if (error)
6360 goto done;
6362 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6363 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6364 GOT_OBJ_TYPE_COMMIT, 1, repo);
6365 if (error)
6366 goto done;
6368 if (worktree) {
6369 got_worktree_close(worktree);
6370 worktree = NULL;
6373 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6374 if (error) {
6375 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6376 goto done;
6377 fprintf(stderr, "%s: '%s' is no known command or path\n",
6378 getprogname(), argv[0]);
6379 usage(1, 1);
6380 /* not reached */
6383 got_repo_close(repo);
6384 repo = NULL;
6386 error = got_object_id_str(&commit_id_str, commit_id);
6387 if (error)
6388 goto done;
6390 cmd = &tog_commands[0]; /* log */
6391 argc = 4;
6392 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6393 error = cmd->cmd_main(argc, cmd_argv);
6394 done:
6395 if (repo)
6396 got_repo_close(repo);
6397 if (worktree)
6398 got_worktree_close(worktree);
6399 free(id);
6400 free(commit_id_str);
6401 free(commit_id);
6402 free(cwd);
6403 free(repo_path);
6404 free(in_repo_path);
6405 if (cmd_argv) {
6406 int i;
6407 for (i = 0; i < argc; i++)
6408 free(cmd_argv[i]);
6409 free(cmd_argv);
6411 return error;
6414 int
6415 main(int argc, char *argv[])
6417 const struct got_error *error = NULL;
6418 struct tog_cmd *cmd = NULL;
6419 int ch, hflag = 0, Vflag = 0;
6420 char **cmd_argv = NULL;
6421 static struct option longopts[] = {
6422 { "version", no_argument, NULL, 'V' },
6423 { NULL, 0, NULL, 0}
6426 setlocale(LC_CTYPE, "");
6428 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6429 switch (ch) {
6430 case 'h':
6431 hflag = 1;
6432 break;
6433 case 'V':
6434 Vflag = 1;
6435 break;
6436 default:
6437 usage(hflag, 1);
6438 /* NOTREACHED */
6442 argc -= optind;
6443 argv += optind;
6444 optind = 1;
6445 optreset = 1;
6447 if (Vflag) {
6448 got_version_print_str();
6449 return 0;
6452 #ifndef PROFILE
6453 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6454 NULL) == -1)
6455 err(1, "pledge");
6456 #endif
6458 if (argc == 0) {
6459 if (hflag)
6460 usage(hflag, 0);
6461 /* Build an argument vector which runs a default command. */
6462 cmd = &tog_commands[0];
6463 argc = 1;
6464 cmd_argv = make_argv(argc, cmd->name);
6465 } else {
6466 int i;
6468 /* Did the user specify a command? */
6469 for (i = 0; i < nitems(tog_commands); i++) {
6470 if (strncmp(tog_commands[i].name, argv[0],
6471 strlen(argv[0])) == 0) {
6472 cmd = &tog_commands[i];
6473 break;
6478 if (cmd == NULL) {
6479 if (argc != 1)
6480 usage(0, 1);
6481 /* No command specified; try log with a path */
6482 error = tog_log_with_path(argc, argv);
6483 } else {
6484 if (hflag)
6485 cmd->cmd_usage();
6486 else
6487 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6490 endwin();
6491 putchar('\n');
6492 if (cmd_argv) {
6493 int i;
6494 for (i = 0; i < argc; i++)
6495 free(cmd_argv[i]);
6496 free(cmd_argv);
6499 if (error && error->code != GOT_ERR_CANCELLED)
6500 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6501 return 0;