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 if (cols + width > wlimit)
1194 break;
1195 cols += width;
1197 i++;
1198 } else {
1199 err = got_error_from_errno("wcwidth");
1200 goto done;
1203 wline[i] = L'\0';
1204 if (widthp)
1205 *widthp = cols;
1206 done:
1207 if (err)
1208 free(wline);
1209 else
1210 *wlinep = wline;
1211 return err;
1214 static const struct got_error*
1215 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1216 struct got_object_id *id, struct got_repository *repo)
1218 static const struct got_error *err = NULL;
1219 struct got_reflist_entry *re;
1220 char *s;
1221 const char *name;
1223 *refs_str = NULL;
1225 SIMPLEQ_FOREACH(re, refs, entry) {
1226 struct got_tag_object *tag = NULL;
1227 struct got_object_id *ref_id;
1228 int cmp;
1230 name = got_ref_get_name(re->ref);
1231 if (strcmp(name, GOT_REF_HEAD) == 0)
1232 continue;
1233 if (strncmp(name, "refs/", 5) == 0)
1234 name += 5;
1235 if (strncmp(name, "got/", 4) == 0)
1236 continue;
1237 if (strncmp(name, "heads/", 6) == 0)
1238 name += 6;
1239 if (strncmp(name, "remotes/", 8) == 0) {
1240 name += 8;
1241 s = strstr(name, "/" GOT_REF_HEAD);
1242 if (s != NULL && s[strlen(s)] == '\0')
1243 continue;
1245 err = got_ref_resolve(&ref_id, repo, re->ref);
1246 if (err)
1247 break;
1248 if (strncmp(name, "tags/", 5) == 0) {
1249 err = got_object_open_as_tag(&tag, repo, ref_id);
1250 if (err) {
1251 if (err->code != GOT_ERR_OBJ_TYPE) {
1252 free(ref_id);
1253 break;
1255 /* Ref points at something other than a tag. */
1256 err = NULL;
1257 tag = NULL;
1260 cmp = got_object_id_cmp(tag ?
1261 got_object_tag_get_object_id(tag) : ref_id, id);
1262 free(ref_id);
1263 if (tag)
1264 got_object_tag_close(tag);
1265 if (cmp != 0)
1266 continue;
1267 s = *refs_str;
1268 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1269 s ? ", " : "", name) == -1) {
1270 err = got_error_from_errno("asprintf");
1271 free(s);
1272 *refs_str = NULL;
1273 break;
1275 free(s);
1278 return err;
1281 static const struct got_error *
1282 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1283 int col_tab_align)
1285 char *smallerthan, *at;
1287 smallerthan = strchr(author, '<');
1288 if (smallerthan && smallerthan[1] != '\0')
1289 author = smallerthan + 1;
1290 at = strchr(author, '@');
1291 if (at)
1292 *at = '\0';
1293 return format_line(wauthor, author_width, author, limit, col_tab_align);
1296 static const struct got_error *
1297 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1298 struct got_object_id *id, const size_t date_display_cols,
1299 int author_display_cols)
1301 struct tog_log_view_state *s = &view->state.log;
1302 const struct got_error *err = NULL;
1303 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1304 char *logmsg0 = NULL, *logmsg = NULL;
1305 char *author = NULL;
1306 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1307 int author_width, logmsg_width;
1308 char *newline, *line = NULL;
1309 int col, limit;
1310 const int avail = view->ncols;
1311 struct tm tm;
1312 time_t committer_time;
1313 struct tog_color *tc;
1315 committer_time = got_object_commit_get_committer_time(commit);
1316 if (localtime_r(&committer_time, &tm) == NULL)
1317 return got_error_from_errno("localtime_r");
1318 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1319 >= sizeof(datebuf))
1320 return got_error(GOT_ERR_NO_SPACE);
1322 if (avail <= date_display_cols)
1323 limit = MIN(sizeof(datebuf) - 1, avail);
1324 else
1325 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1326 tc = get_color(&s->colors, TOG_COLOR_DATE);
1327 if (tc)
1328 wattr_on(view->window,
1329 COLOR_PAIR(tc->colorpair), NULL);
1330 waddnstr(view->window, datebuf, limit);
1331 if (tc)
1332 wattr_off(view->window,
1333 COLOR_PAIR(tc->colorpair), NULL);
1334 col = limit;
1335 if (col > avail)
1336 goto done;
1338 if (avail >= 120) {
1339 char *id_str;
1340 err = got_object_id_str(&id_str, id);
1341 if (err)
1342 goto done;
1343 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1344 if (tc)
1345 wattr_on(view->window,
1346 COLOR_PAIR(tc->colorpair), NULL);
1347 wprintw(view->window, "%.8s ", id_str);
1348 if (tc)
1349 wattr_off(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1351 free(id_str);
1352 col += 9;
1353 if (col > avail)
1354 goto done;
1357 author = strdup(got_object_commit_get_author(commit));
1358 if (author == NULL) {
1359 err = got_error_from_errno("strdup");
1360 goto done;
1362 err = format_author(&wauthor, &author_width, author, avail - col, col);
1363 if (err)
1364 goto done;
1365 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1366 if (tc)
1367 wattr_on(view->window,
1368 COLOR_PAIR(tc->colorpair), NULL);
1369 waddwstr(view->window, wauthor);
1370 if (tc)
1371 wattr_off(view->window,
1372 COLOR_PAIR(tc->colorpair), NULL);
1373 col += author_width;
1374 while (col < avail && author_width < author_display_cols + 2) {
1375 waddch(view->window, ' ');
1376 col++;
1377 author_width++;
1379 if (col > avail)
1380 goto done;
1382 err = got_object_commit_get_logmsg(&logmsg0, commit);
1383 if (err)
1384 goto done;
1385 logmsg = logmsg0;
1386 while (*logmsg == '\n')
1387 logmsg++;
1388 newline = strchr(logmsg, '\n');
1389 if (newline)
1390 *newline = '\0';
1391 limit = avail - col;
1392 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1393 if (err)
1394 goto done;
1395 waddwstr(view->window, wlogmsg);
1396 col += logmsg_width;
1397 while (col < avail) {
1398 waddch(view->window, ' ');
1399 col++;
1401 done:
1402 free(logmsg0);
1403 free(wlogmsg);
1404 free(author);
1405 free(wauthor);
1406 free(line);
1407 return err;
1410 static struct commit_queue_entry *
1411 alloc_commit_queue_entry(struct got_commit_object *commit,
1412 struct got_object_id *id)
1414 struct commit_queue_entry *entry;
1416 entry = calloc(1, sizeof(*entry));
1417 if (entry == NULL)
1418 return NULL;
1420 entry->id = id;
1421 entry->commit = commit;
1422 return entry;
1425 static void
1426 pop_commit(struct commit_queue *commits)
1428 struct commit_queue_entry *entry;
1430 entry = TAILQ_FIRST(&commits->head);
1431 TAILQ_REMOVE(&commits->head, entry, entry);
1432 got_object_commit_close(entry->commit);
1433 commits->ncommits--;
1434 /* Don't free entry->id! It is owned by the commit graph. */
1435 free(entry);
1438 static void
1439 free_commits(struct commit_queue *commits)
1441 while (!TAILQ_EMPTY(&commits->head))
1442 pop_commit(commits);
1445 static const struct got_error *
1446 match_commit(int *have_match, struct got_object_id *id,
1447 struct got_commit_object *commit, regex_t *regex)
1449 const struct got_error *err = NULL;
1450 regmatch_t regmatch;
1451 char *id_str = NULL, *logmsg = NULL;
1453 *have_match = 0;
1455 err = got_object_id_str(&id_str, id);
1456 if (err)
1457 return err;
1459 err = got_object_commit_get_logmsg(&logmsg, commit);
1460 if (err)
1461 goto done;
1463 if (regexec(regex, got_object_commit_get_author(commit), 1,
1464 &regmatch, 0) == 0 ||
1465 regexec(regex, got_object_commit_get_committer(commit), 1,
1466 &regmatch, 0) == 0 ||
1467 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1468 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1469 *have_match = 1;
1470 done:
1471 free(id_str);
1472 free(logmsg);
1473 return err;
1476 static const struct got_error *
1477 queue_commits(struct tog_log_thread_args *a)
1479 const struct got_error *err = NULL;
1482 * We keep all commits open throughout the lifetime of the log
1483 * view in order to avoid having to re-fetch commits from disk
1484 * while updating the display.
1486 do {
1487 struct got_object_id *id;
1488 struct got_commit_object *commit;
1489 struct commit_queue_entry *entry;
1490 int errcode;
1492 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1493 NULL, NULL);
1494 if (err || id == NULL)
1495 break;
1497 err = got_object_open_as_commit(&commit, a->repo, id);
1498 if (err)
1499 break;
1500 entry = alloc_commit_queue_entry(commit, id);
1501 if (entry == NULL) {
1502 err = got_error_from_errno("alloc_commit_queue_entry");
1503 break;
1506 errcode = pthread_mutex_lock(&tog_mutex);
1507 if (errcode) {
1508 err = got_error_set_errno(errcode,
1509 "pthread_mutex_lock");
1510 break;
1513 entry->idx = a->commits->ncommits;
1514 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1515 a->commits->ncommits++;
1517 if (*a->searching == TOG_SEARCH_FORWARD &&
1518 !*a->search_next_done) {
1519 int have_match;
1520 err = match_commit(&have_match, id, commit, a->regex);
1521 if (err)
1522 break;
1523 if (have_match)
1524 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1527 errcode = pthread_mutex_unlock(&tog_mutex);
1528 if (errcode && err == NULL)
1529 err = got_error_set_errno(errcode,
1530 "pthread_mutex_unlock");
1531 if (err)
1532 break;
1533 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1535 return err;
1538 static void
1539 select_commit(struct tog_log_view_state *s)
1541 struct commit_queue_entry *entry;
1542 int ncommits = 0;
1544 entry = s->first_displayed_entry;
1545 while (entry) {
1546 if (ncommits == s->selected) {
1547 s->selected_entry = entry;
1548 break;
1550 entry = TAILQ_NEXT(entry, entry);
1551 ncommits++;
1555 static const struct got_error *
1556 draw_commits(struct tog_view *view)
1558 const struct got_error *err = NULL;
1559 struct tog_log_view_state *s = &view->state.log;
1560 struct commit_queue_entry *entry = s->selected_entry;
1561 const int limit = view->nlines;
1562 int width;
1563 int ncommits, author_cols = 4;
1564 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1565 char *refs_str = NULL;
1566 wchar_t *wline;
1567 struct tog_color *tc;
1568 static const size_t date_display_cols = 12;
1570 if (s->selected_entry &&
1571 !(view->searching && view->search_next_done == 0)) {
1572 err = got_object_id_str(&id_str, s->selected_entry->id);
1573 if (err)
1574 return err;
1575 err = build_refs_str(&refs_str, &s->refs,
1576 s->selected_entry->id, s->repo);
1577 if (err)
1578 goto done;
1581 if (s->thread_args.commits_needed == 0)
1582 halfdelay(10); /* disable fast refresh */
1584 if (s->thread_args.commits_needed > 0) {
1585 if (asprintf(&ncommits_str, " [%d/%d] %s",
1586 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1587 (view->searching && !view->search_next_done) ?
1588 "searching..." : "loading...") == -1) {
1589 err = got_error_from_errno("asprintf");
1590 goto done;
1592 } else {
1593 const char *search_str = NULL;
1595 if (view->searching) {
1596 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1597 search_str = "no more matches";
1598 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1599 search_str = "no matches found";
1600 else if (!view->search_next_done)
1601 search_str = "searching...";
1604 if (asprintf(&ncommits_str, " [%d/%d] %s",
1605 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1606 search_str ? search_str :
1607 (refs_str ? refs_str : "")) == -1) {
1608 err = got_error_from_errno("asprintf");
1609 goto done;
1613 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1614 if (asprintf(&header, "commit %s %s%s",
1615 id_str ? id_str : "........................................",
1616 s->in_repo_path, ncommits_str) == -1) {
1617 err = got_error_from_errno("asprintf");
1618 header = NULL;
1619 goto done;
1621 } else if (asprintf(&header, "commit %s%s",
1622 id_str ? id_str : "........................................",
1623 ncommits_str) == -1) {
1624 err = got_error_from_errno("asprintf");
1625 header = NULL;
1626 goto done;
1628 err = format_line(&wline, &width, header, view->ncols, 0);
1629 if (err)
1630 goto done;
1632 werase(view->window);
1634 if (view_needs_focus_indication(view))
1635 wstandout(view->window);
1636 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1637 if (tc)
1638 wattr_on(view->window,
1639 COLOR_PAIR(tc->colorpair), NULL);
1640 waddwstr(view->window, wline);
1641 if (tc)
1642 wattr_off(view->window,
1643 COLOR_PAIR(tc->colorpair), NULL);
1644 while (width < view->ncols) {
1645 waddch(view->window, ' ');
1646 width++;
1648 if (view_needs_focus_indication(view))
1649 wstandend(view->window);
1650 free(wline);
1651 if (limit <= 1)
1652 goto done;
1654 /* Grow author column size if necessary. */
1655 entry = s->first_displayed_entry;
1656 ncommits = 0;
1657 while (entry) {
1658 char *author;
1659 wchar_t *wauthor;
1660 int width;
1661 if (ncommits >= limit - 1)
1662 break;
1663 author = strdup(got_object_commit_get_author(entry->commit));
1664 if (author == NULL) {
1665 err = got_error_from_errno("strdup");
1666 goto done;
1668 err = format_author(&wauthor, &width, author, COLS,
1669 date_display_cols);
1670 if (author_cols < width)
1671 author_cols = width;
1672 free(wauthor);
1673 free(author);
1674 ncommits++;
1675 entry = TAILQ_NEXT(entry, entry);
1678 entry = s->first_displayed_entry;
1679 s->last_displayed_entry = s->first_displayed_entry;
1680 ncommits = 0;
1681 while (entry) {
1682 if (ncommits >= limit - 1)
1683 break;
1684 if (ncommits == s->selected)
1685 wstandout(view->window);
1686 err = draw_commit(view, entry->commit, entry->id,
1687 date_display_cols, author_cols);
1688 if (ncommits == s->selected)
1689 wstandend(view->window);
1690 if (err)
1691 goto done;
1692 ncommits++;
1693 s->last_displayed_entry = entry;
1694 entry = TAILQ_NEXT(entry, entry);
1697 view_vborder(view);
1698 done:
1699 free(id_str);
1700 free(refs_str);
1701 free(ncommits_str);
1702 free(header);
1703 return err;
1706 static void
1707 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1709 struct commit_queue_entry *entry;
1710 int nscrolled = 0;
1712 entry = TAILQ_FIRST(&s->commits.head);
1713 if (s->first_displayed_entry == entry)
1714 return;
1716 entry = s->first_displayed_entry;
1717 while (entry && nscrolled < maxscroll) {
1718 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1719 if (entry) {
1720 s->first_displayed_entry = entry;
1721 nscrolled++;
1726 static const struct got_error *
1727 trigger_log_thread(struct tog_view *view, int wait)
1729 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1730 int errcode;
1732 halfdelay(1); /* fast refresh while loading commits */
1734 while (ta->commits_needed > 0) {
1735 if (ta->log_complete)
1736 break;
1738 /* Wake the log thread. */
1739 errcode = pthread_cond_signal(&ta->need_commits);
1740 if (errcode)
1741 return got_error_set_errno(errcode,
1742 "pthread_cond_signal");
1745 * The mutex will be released while the view loop waits
1746 * in wgetch(), at which time the log thread will run.
1748 if (!wait)
1749 break;
1751 /* Display progress update in log view. */
1752 show_log_view(view);
1753 update_panels();
1754 doupdate();
1756 /* Wait right here while next commit is being loaded. */
1757 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1758 if (errcode)
1759 return got_error_set_errno(errcode,
1760 "pthread_cond_wait");
1762 /* Display progress update in log view. */
1763 show_log_view(view);
1764 update_panels();
1765 doupdate();
1768 return NULL;
1771 static const struct got_error *
1772 log_scroll_down(struct tog_view *view, int maxscroll)
1774 struct tog_log_view_state *s = &view->state.log;
1775 const struct got_error *err = NULL;
1776 struct commit_queue_entry *pentry;
1777 int nscrolled = 0, ncommits_needed;
1779 if (s->last_displayed_entry == NULL)
1780 return NULL;
1782 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1783 if (s->commits.ncommits < ncommits_needed &&
1784 !s->thread_args.log_complete) {
1786 * Ask the log thread for required amount of commits.
1788 s->thread_args.commits_needed += maxscroll;
1789 err = trigger_log_thread(view, 1);
1790 if (err)
1791 return err;
1794 do {
1795 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1796 if (pentry == NULL)
1797 break;
1799 s->last_displayed_entry = pentry;
1801 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1802 if (pentry == NULL)
1803 break;
1804 s->first_displayed_entry = pentry;
1805 } while (++nscrolled < maxscroll);
1807 return err;
1810 static const struct got_error *
1811 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1812 struct got_commit_object *commit, struct got_object_id *commit_id,
1813 struct tog_view *log_view, struct got_repository *repo)
1815 const struct got_error *err;
1816 struct got_object_qid *parent_id;
1817 struct tog_view *diff_view;
1819 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1820 if (diff_view == NULL)
1821 return got_error_from_errno("view_open");
1823 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1824 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1825 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1826 if (err == NULL)
1827 *new_view = diff_view;
1828 return err;
1831 static const struct got_error *
1832 tree_view_visit_subtree(struct tog_tree_view_state *s,
1833 struct got_tree_object *subtree)
1835 struct tog_parent_tree *parent;
1837 parent = calloc(1, sizeof(*parent));
1838 if (parent == NULL)
1839 return got_error_from_errno("calloc");
1841 parent->tree = s->tree;
1842 parent->first_displayed_entry = s->first_displayed_entry;
1843 parent->selected_entry = s->selected_entry;
1844 parent->selected = s->selected;
1845 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1846 s->tree = subtree;
1847 s->selected = 0;
1848 s->first_displayed_entry = NULL;
1849 return NULL;
1852 static const struct got_error *
1853 tree_view_walk_path(struct tog_tree_view_state *s,
1854 struct got_object_id *commit_id, const char *path)
1856 const struct got_error *err = NULL;
1857 struct got_tree_object *tree = NULL;
1858 const char *p;
1859 char *slash, *subpath = NULL;
1861 /* Walk the path and open corresponding tree objects. */
1862 p = path;
1863 while (*p) {
1864 struct got_tree_entry *te;
1865 struct got_object_id *tree_id;
1866 char *te_name;
1868 while (p[0] == '/')
1869 p++;
1871 /* Ensure the correct subtree entry is selected. */
1872 slash = strchr(p, '/');
1873 if (slash == NULL)
1874 te_name = strdup(p);
1875 else
1876 te_name = strndup(p, slash - p);
1877 if (te_name == NULL) {
1878 err = got_error_from_errno("strndup");
1879 break;
1881 te = got_object_tree_find_entry(s->tree, te_name);
1882 if (te == NULL) {
1883 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1884 free(te_name);
1885 break;
1887 free(te_name);
1888 s->first_displayed_entry = s->selected_entry = te;
1890 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1891 break; /* jump to this file's entry */
1893 slash = strchr(p, '/');
1894 if (slash)
1895 subpath = strndup(path, slash - path);
1896 else
1897 subpath = strdup(path);
1898 if (subpath == NULL) {
1899 err = got_error_from_errno("strdup");
1900 break;
1903 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1904 subpath);
1905 if (err)
1906 break;
1908 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1909 free(tree_id);
1910 if (err)
1911 break;
1913 err = tree_view_visit_subtree(s, tree);
1914 if (err) {
1915 got_object_tree_close(tree);
1916 break;
1918 if (slash == NULL)
1919 break;
1920 free(subpath);
1921 subpath = NULL;
1922 p = slash;
1925 free(subpath);
1926 return err;
1929 static const struct got_error *
1930 browse_commit_tree(struct tog_view **new_view, int begin_x,
1931 struct commit_queue_entry *entry, const char *path,
1932 const char *head_ref_name, struct got_repository *repo)
1934 const struct got_error *err = NULL;
1935 struct got_tree_object *tree;
1936 struct tog_tree_view_state *s;
1937 struct tog_view *tree_view;
1939 err = got_object_open_as_tree(&tree, repo,
1940 got_object_commit_get_tree_id(entry->commit));
1941 if (err)
1942 return err;
1944 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1945 if (tree_view == NULL)
1946 return got_error_from_errno("view_open");
1948 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1949 if (err) {
1950 got_object_tree_close(tree);
1951 return err;
1953 s = &tree_view->state.tree;
1955 *new_view = tree_view;
1957 if (got_path_is_root_dir(path))
1958 return NULL;
1960 return tree_view_walk_path(s, entry->id, path);
1963 static const struct got_error *
1964 block_signals_used_by_main_thread(void)
1966 sigset_t sigset;
1967 int errcode;
1969 if (sigemptyset(&sigset) == -1)
1970 return got_error_from_errno("sigemptyset");
1972 /* tog handles SIGWINCH and SIGCONT */
1973 if (sigaddset(&sigset, SIGWINCH) == -1)
1974 return got_error_from_errno("sigaddset");
1975 if (sigaddset(&sigset, SIGCONT) == -1)
1976 return got_error_from_errno("sigaddset");
1978 /* ncurses handles SIGTSTP */
1979 if (sigaddset(&sigset, SIGTSTP) == -1)
1980 return got_error_from_errno("sigaddset");
1982 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1983 if (errcode)
1984 return got_error_set_errno(errcode, "pthread_sigmask");
1986 return NULL;
1989 static void *
1990 log_thread(void *arg)
1992 const struct got_error *err = NULL;
1993 int errcode = 0;
1994 struct tog_log_thread_args *a = arg;
1995 int done = 0;
1997 err = block_signals_used_by_main_thread();
1998 if (err)
1999 return (void *)err;
2001 while (!done && !err && !tog_sigpipe_received) {
2002 err = queue_commits(a);
2003 if (err) {
2004 if (err->code != GOT_ERR_ITER_COMPLETED)
2005 return (void *)err;
2006 err = NULL;
2007 done = 1;
2008 } else if (a->commits_needed > 0)
2009 a->commits_needed--;
2011 errcode = pthread_mutex_lock(&tog_mutex);
2012 if (errcode) {
2013 err = got_error_set_errno(errcode,
2014 "pthread_mutex_lock");
2015 break;
2016 } else if (*a->quit)
2017 done = 1;
2018 else if (*a->first_displayed_entry == NULL) {
2019 *a->first_displayed_entry =
2020 TAILQ_FIRST(&a->commits->head);
2021 *a->selected_entry = *a->first_displayed_entry;
2024 errcode = pthread_cond_signal(&a->commit_loaded);
2025 if (errcode) {
2026 err = got_error_set_errno(errcode,
2027 "pthread_cond_signal");
2028 pthread_mutex_unlock(&tog_mutex);
2029 break;
2032 if (done)
2033 a->commits_needed = 0;
2034 else {
2035 if (a->commits_needed == 0) {
2036 errcode = pthread_cond_wait(&a->need_commits,
2037 &tog_mutex);
2038 if (errcode)
2039 err = got_error_set_errno(errcode,
2040 "pthread_cond_wait");
2041 if (*a->quit)
2042 done = 1;
2046 errcode = pthread_mutex_unlock(&tog_mutex);
2047 if (errcode && err == NULL)
2048 err = got_error_set_errno(errcode,
2049 "pthread_mutex_unlock");
2051 a->log_complete = 1;
2052 return (void *)err;
2055 static const struct got_error *
2056 stop_log_thread(struct tog_log_view_state *s)
2058 const struct got_error *err = NULL;
2059 int errcode;
2061 if (s->thread) {
2062 s->quit = 1;
2063 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2064 if (errcode)
2065 return got_error_set_errno(errcode,
2066 "pthread_cond_signal");
2067 errcode = pthread_mutex_unlock(&tog_mutex);
2068 if (errcode)
2069 return got_error_set_errno(errcode,
2070 "pthread_mutex_unlock");
2071 errcode = pthread_join(s->thread, (void **)&err);
2072 if (errcode)
2073 return got_error_set_errno(errcode, "pthread_join");
2074 errcode = pthread_mutex_lock(&tog_mutex);
2075 if (errcode)
2076 return got_error_set_errno(errcode,
2077 "pthread_mutex_lock");
2078 s->thread = NULL;
2081 if (s->thread_args.repo) {
2082 got_repo_close(s->thread_args.repo);
2083 s->thread_args.repo = NULL;
2086 if (s->thread_args.graph) {
2087 got_commit_graph_close(s->thread_args.graph);
2088 s->thread_args.graph = NULL;
2091 return err;
2094 static const struct got_error *
2095 close_log_view(struct tog_view *view)
2097 const struct got_error *err = NULL;
2098 struct tog_log_view_state *s = &view->state.log;
2099 int errcode;
2101 err = stop_log_thread(s);
2103 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2104 if (errcode && err == NULL)
2105 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2107 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2108 if (errcode && err == NULL)
2109 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2111 free_commits(&s->commits);
2112 free(s->in_repo_path);
2113 s->in_repo_path = NULL;
2114 free(s->start_id);
2115 s->start_id = NULL;
2116 free(s->head_ref_name);
2117 s->head_ref_name = NULL;
2118 got_ref_list_free(&s->refs);
2119 return err;
2122 static const struct got_error *
2123 search_start_log_view(struct tog_view *view)
2125 struct tog_log_view_state *s = &view->state.log;
2127 s->matched_entry = NULL;
2128 s->search_entry = NULL;
2129 return NULL;
2132 static const struct got_error *
2133 search_next_log_view(struct tog_view *view)
2135 const struct got_error *err = NULL;
2136 struct tog_log_view_state *s = &view->state.log;
2137 struct commit_queue_entry *entry;
2139 /* Display progress update in log view. */
2140 show_log_view(view);
2141 update_panels();
2142 doupdate();
2144 if (s->search_entry) {
2145 int errcode, ch;
2146 errcode = pthread_mutex_unlock(&tog_mutex);
2147 if (errcode)
2148 return got_error_set_errno(errcode,
2149 "pthread_mutex_unlock");
2150 ch = wgetch(view->window);
2151 errcode = pthread_mutex_lock(&tog_mutex);
2152 if (errcode)
2153 return got_error_set_errno(errcode,
2154 "pthread_mutex_lock");
2155 if (ch == KEY_BACKSPACE) {
2156 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2157 return NULL;
2159 if (view->searching == TOG_SEARCH_FORWARD)
2160 entry = TAILQ_NEXT(s->search_entry, entry);
2161 else
2162 entry = TAILQ_PREV(s->search_entry,
2163 commit_queue_head, entry);
2164 } else if (s->matched_entry) {
2165 if (view->searching == TOG_SEARCH_FORWARD)
2166 entry = TAILQ_NEXT(s->matched_entry, entry);
2167 else
2168 entry = TAILQ_PREV(s->matched_entry,
2169 commit_queue_head, entry);
2170 } else {
2171 if (view->searching == TOG_SEARCH_FORWARD)
2172 entry = TAILQ_FIRST(&s->commits.head);
2173 else
2174 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2177 while (1) {
2178 int have_match = 0;
2180 if (entry == NULL) {
2181 if (s->thread_args.log_complete ||
2182 view->searching == TOG_SEARCH_BACKWARD) {
2183 view->search_next_done =
2184 (s->matched_entry == NULL ?
2185 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2186 s->search_entry = NULL;
2187 return NULL;
2190 * Poke the log thread for more commits and return,
2191 * allowing the main loop to make progress. Search
2192 * will resume at s->search_entry once we come back.
2194 s->thread_args.commits_needed++;
2195 return trigger_log_thread(view, 0);
2198 err = match_commit(&have_match, entry->id, entry->commit,
2199 &view->regex);
2200 if (err)
2201 break;
2202 if (have_match) {
2203 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2204 s->matched_entry = entry;
2205 break;
2208 s->search_entry = entry;
2209 if (view->searching == TOG_SEARCH_FORWARD)
2210 entry = TAILQ_NEXT(entry, entry);
2211 else
2212 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2215 if (s->matched_entry) {
2216 int cur = s->selected_entry->idx;
2217 while (cur < s->matched_entry->idx) {
2218 err = input_log_view(NULL, view, KEY_DOWN);
2219 if (err)
2220 return err;
2221 cur++;
2223 while (cur > s->matched_entry->idx) {
2224 err = input_log_view(NULL, view, KEY_UP);
2225 if (err)
2226 return err;
2227 cur--;
2231 s->search_entry = NULL;
2233 return NULL;
2236 static const struct got_error *
2237 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2238 struct got_repository *repo, const char *head_ref_name,
2239 const char *in_repo_path, int log_branches)
2241 const struct got_error *err = NULL;
2242 struct tog_log_view_state *s = &view->state.log;
2243 struct got_repository *thread_repo = NULL;
2244 struct got_commit_graph *thread_graph = NULL;
2245 int errcode;
2247 SIMPLEQ_INIT(&s->refs);
2249 if (in_repo_path != s->in_repo_path) {
2250 free(s->in_repo_path);
2251 s->in_repo_path = strdup(in_repo_path);
2252 if (s->in_repo_path == NULL)
2253 return got_error_from_errno("strdup");
2256 /* The commit queue only contains commits being displayed. */
2257 TAILQ_INIT(&s->commits.head);
2258 s->commits.ncommits = 0;
2260 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2261 if (err)
2262 goto done;
2264 s->repo = repo;
2265 if (head_ref_name) {
2266 s->head_ref_name = strdup(head_ref_name);
2267 if (s->head_ref_name == NULL) {
2268 err = got_error_from_errno("strdup");
2269 goto done;
2272 s->start_id = got_object_id_dup(start_id);
2273 if (s->start_id == NULL) {
2274 err = got_error_from_errno("got_object_id_dup");
2275 goto done;
2277 s->log_branches = log_branches;
2279 SIMPLEQ_INIT(&s->colors);
2280 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2281 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2282 get_color_value("TOG_COLOR_COMMIT"));
2283 if (err)
2284 goto done;
2285 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2286 get_color_value("TOG_COLOR_AUTHOR"));
2287 if (err) {
2288 free_colors(&s->colors);
2289 goto done;
2291 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2292 get_color_value("TOG_COLOR_DATE"));
2293 if (err) {
2294 free_colors(&s->colors);
2295 goto done;
2299 view->show = show_log_view;
2300 view->input = input_log_view;
2301 view->close = close_log_view;
2302 view->search_start = search_start_log_view;
2303 view->search_next = search_next_log_view;
2305 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2306 if (err)
2307 goto done;
2308 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2309 !s->log_branches);
2310 if (err)
2311 goto done;
2312 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2313 s->repo, NULL, NULL);
2314 if (err)
2315 goto done;
2317 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2318 if (errcode) {
2319 err = got_error_set_errno(errcode, "pthread_cond_init");
2320 goto done;
2322 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2323 if (errcode) {
2324 err = got_error_set_errno(errcode, "pthread_cond_init");
2325 goto done;
2328 s->thread_args.commits_needed = view->nlines;
2329 s->thread_args.graph = thread_graph;
2330 s->thread_args.commits = &s->commits;
2331 s->thread_args.in_repo_path = s->in_repo_path;
2332 s->thread_args.start_id = s->start_id;
2333 s->thread_args.repo = thread_repo;
2334 s->thread_args.log_complete = 0;
2335 s->thread_args.quit = &s->quit;
2336 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2337 s->thread_args.selected_entry = &s->selected_entry;
2338 s->thread_args.searching = &view->searching;
2339 s->thread_args.search_next_done = &view->search_next_done;
2340 s->thread_args.regex = &view->regex;
2341 done:
2342 if (err)
2343 close_log_view(view);
2344 return err;
2347 static const struct got_error *
2348 show_log_view(struct tog_view *view)
2350 const struct got_error *err;
2351 struct tog_log_view_state *s = &view->state.log;
2353 if (s->thread == NULL) {
2354 int errcode = pthread_create(&s->thread, NULL, log_thread,
2355 &s->thread_args);
2356 if (errcode)
2357 return got_error_set_errno(errcode, "pthread_create");
2358 if (s->thread_args.commits_needed > 0) {
2359 err = trigger_log_thread(view, 1);
2360 if (err)
2361 return err;
2365 return draw_commits(view);
2368 static const struct got_error *
2369 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2371 const struct got_error *err = NULL;
2372 struct tog_log_view_state *s = &view->state.log;
2373 struct tog_view *diff_view = NULL, *tree_view = NULL;
2374 struct tog_view *ref_view = NULL;
2375 int begin_x = 0;
2377 switch (ch) {
2378 case 'q':
2379 s->quit = 1;
2380 break;
2381 case 'k':
2382 case KEY_UP:
2383 case '<':
2384 case ',':
2385 if (s->first_displayed_entry == NULL)
2386 break;
2387 if (s->selected > 0)
2388 s->selected--;
2389 else
2390 log_scroll_up(s, 1);
2391 select_commit(s);
2392 break;
2393 case KEY_PPAGE:
2394 case CTRL('b'):
2395 if (s->first_displayed_entry == NULL)
2396 break;
2397 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2398 s->selected = 0;
2399 else
2400 log_scroll_up(s, view->nlines - 1);
2401 select_commit(s);
2402 break;
2403 case 'j':
2404 case KEY_DOWN:
2405 case '>':
2406 case '.':
2407 if (s->first_displayed_entry == NULL)
2408 break;
2409 if (s->selected < MIN(view->nlines - 2,
2410 s->commits.ncommits - 1))
2411 s->selected++;
2412 else {
2413 err = log_scroll_down(view, 1);
2414 if (err)
2415 break;
2417 select_commit(s);
2418 break;
2419 case KEY_NPAGE:
2420 case CTRL('f'): {
2421 struct commit_queue_entry *first;
2422 first = s->first_displayed_entry;
2423 if (first == NULL)
2424 break;
2425 err = log_scroll_down(view, view->nlines - 1);
2426 if (err)
2427 break;
2428 if (first == s->first_displayed_entry &&
2429 s->selected < MIN(view->nlines - 2,
2430 s->commits.ncommits - 1)) {
2431 /* can't scroll further down */
2432 s->selected = MIN(view->nlines - 2,
2433 s->commits.ncommits - 1);
2435 select_commit(s);
2436 break;
2438 case KEY_RESIZE:
2439 if (s->selected > view->nlines - 2)
2440 s->selected = view->nlines - 2;
2441 if (s->selected > s->commits.ncommits - 1)
2442 s->selected = s->commits.ncommits - 1;
2443 select_commit(s);
2444 if (s->commits.ncommits < view->nlines - 1 &&
2445 !s->thread_args.log_complete) {
2446 s->thread_args.commits_needed += (view->nlines - 1) -
2447 s->commits.ncommits;
2448 err = trigger_log_thread(view, 1);
2450 break;
2451 case KEY_ENTER:
2452 case ' ':
2453 case '\r':
2454 if (s->selected_entry == NULL)
2455 break;
2456 if (view_is_parent_view(view))
2457 begin_x = view_split_begin_x(view->begin_x);
2458 err = open_diff_view_for_commit(&diff_view, begin_x,
2459 s->selected_entry->commit, s->selected_entry->id,
2460 view, s->repo);
2461 if (err)
2462 break;
2463 view->focussed = 0;
2464 diff_view->focussed = 1;
2465 if (view_is_parent_view(view)) {
2466 err = view_close_child(view);
2467 if (err)
2468 return err;
2469 view_set_child(view, diff_view);
2470 view->focus_child = 1;
2471 } else
2472 *new_view = diff_view;
2473 break;
2474 case 't':
2475 if (s->selected_entry == NULL)
2476 break;
2477 if (view_is_parent_view(view))
2478 begin_x = view_split_begin_x(view->begin_x);
2479 err = browse_commit_tree(&tree_view, begin_x,
2480 s->selected_entry, s->in_repo_path, s->head_ref_name,
2481 s->repo);
2482 if (err)
2483 break;
2484 view->focussed = 0;
2485 tree_view->focussed = 1;
2486 if (view_is_parent_view(view)) {
2487 err = view_close_child(view);
2488 if (err)
2489 return err;
2490 view_set_child(view, tree_view);
2491 view->focus_child = 1;
2492 } else
2493 *new_view = tree_view;
2494 break;
2495 case KEY_BACKSPACE:
2496 case CTRL('l'):
2497 case 'B':
2498 if (ch == KEY_BACKSPACE &&
2499 got_path_is_root_dir(s->in_repo_path))
2500 break;
2501 err = stop_log_thread(s);
2502 if (err)
2503 return err;
2504 if (ch == KEY_BACKSPACE) {
2505 char *parent_path;
2506 err = got_path_dirname(&parent_path, s->in_repo_path);
2507 if (err)
2508 return err;
2509 free(s->in_repo_path);
2510 s->in_repo_path = parent_path;
2511 s->thread_args.in_repo_path = s->in_repo_path;
2512 } else if (ch == CTRL('l')) {
2513 struct got_object_id *start_id;
2514 err = got_repo_match_object_id(&start_id, NULL,
2515 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2516 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2517 if (err)
2518 return err;
2519 free(s->start_id);
2520 s->start_id = start_id;
2521 s->thread_args.start_id = s->start_id;
2522 } else /* 'B' */
2523 s->log_branches = !s->log_branches;
2525 err = got_repo_open(&s->thread_args.repo,
2526 got_repo_get_path(s->repo), NULL);
2527 if (err)
2528 return err;
2529 got_ref_list_free(&s->refs);
2530 err = got_ref_list(&s->refs, s->repo, NULL,
2531 got_ref_cmp_by_name, NULL);
2532 if (err)
2533 return err;
2534 err = got_commit_graph_open(&s->thread_args.graph,
2535 s->in_repo_path, !s->log_branches);
2536 if (err)
2537 return err;
2538 err = got_commit_graph_iter_start(s->thread_args.graph,
2539 s->start_id, s->repo, NULL, NULL);
2540 if (err)
2541 return err;
2542 free_commits(&s->commits);
2543 s->first_displayed_entry = NULL;
2544 s->last_displayed_entry = NULL;
2545 s->selected_entry = NULL;
2546 s->selected = 0;
2547 s->thread_args.log_complete = 0;
2548 s->quit = 0;
2549 s->thread_args.commits_needed = view->nlines;
2550 break;
2551 case 'r':
2552 if (view_is_parent_view(view))
2553 begin_x = view_split_begin_x(view->begin_x);
2554 ref_view = view_open(view->nlines, view->ncols,
2555 view->begin_y, begin_x, TOG_VIEW_REF);
2556 if (ref_view == NULL)
2557 return got_error_from_errno("view_open");
2558 err = open_ref_view(ref_view, s->repo);
2559 if (err) {
2560 view_close(ref_view);
2561 return err;
2563 view->focussed = 0;
2564 ref_view->focussed = 1;
2565 if (view_is_parent_view(view)) {
2566 err = view_close_child(view);
2567 if (err)
2568 return err;
2569 view_set_child(view, ref_view);
2570 view->focus_child = 1;
2571 } else
2572 *new_view = ref_view;
2573 break;
2574 default:
2575 break;
2578 return err;
2581 static const struct got_error *
2582 apply_unveil(const char *repo_path, const char *worktree_path)
2584 const struct got_error *error;
2586 #ifdef PROFILE
2587 if (unveil("gmon.out", "rwc") != 0)
2588 return got_error_from_errno2("unveil", "gmon.out");
2589 #endif
2590 if (repo_path && unveil(repo_path, "r") != 0)
2591 return got_error_from_errno2("unveil", repo_path);
2593 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2594 return got_error_from_errno2("unveil", worktree_path);
2596 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2597 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2599 error = got_privsep_unveil_exec_helpers();
2600 if (error != NULL)
2601 return error;
2603 if (unveil(NULL, NULL) != 0)
2604 return got_error_from_errno("unveil");
2606 return NULL;
2609 static void
2610 init_curses(void)
2612 initscr();
2613 cbreak();
2614 halfdelay(1); /* Do fast refresh while initial view is loading. */
2615 noecho();
2616 nonl();
2617 intrflush(stdscr, FALSE);
2618 keypad(stdscr, TRUE);
2619 curs_set(0);
2620 if (getenv("TOG_COLORS") != NULL) {
2621 start_color();
2622 use_default_colors();
2624 signal(SIGWINCH, tog_sigwinch);
2625 signal(SIGPIPE, tog_sigpipe);
2626 signal(SIGCONT, tog_sigcont);
2629 static const struct got_error *
2630 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2631 struct got_repository *repo, struct got_worktree *worktree)
2633 const struct got_error *err = NULL;
2635 if (argc == 0) {
2636 *in_repo_path = strdup("/");
2637 if (*in_repo_path == NULL)
2638 return got_error_from_errno("strdup");
2639 return NULL;
2642 if (worktree) {
2643 const char *prefix = got_worktree_get_path_prefix(worktree);
2644 char *p;
2646 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2647 if (err)
2648 return err;
2649 if (asprintf(in_repo_path, "%s%s%s", prefix,
2650 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2651 p) == -1) {
2652 err = got_error_from_errno("asprintf");
2653 *in_repo_path = NULL;
2655 free(p);
2656 } else
2657 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2659 return err;
2662 static const struct got_error *
2663 cmd_log(int argc, char *argv[])
2665 const struct got_error *error;
2666 struct got_repository *repo = NULL;
2667 struct got_worktree *worktree = NULL;
2668 struct got_object_id *start_id = NULL;
2669 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2670 char *start_commit = NULL, *label = NULL;
2671 struct got_reference *ref = NULL;
2672 const char *head_ref_name = NULL;
2673 int ch, log_branches = 0;
2674 struct tog_view *view;
2676 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2677 switch (ch) {
2678 case 'b':
2679 log_branches = 1;
2680 break;
2681 case 'c':
2682 start_commit = optarg;
2683 break;
2684 case 'r':
2685 repo_path = realpath(optarg, NULL);
2686 if (repo_path == NULL)
2687 return got_error_from_errno2("realpath",
2688 optarg);
2689 break;
2690 default:
2691 usage_log();
2692 /* NOTREACHED */
2696 argc -= optind;
2697 argv += optind;
2699 if (argc > 1)
2700 usage_log();
2702 cwd = getcwd(NULL, 0);
2703 if (cwd == NULL)
2704 return got_error_from_errno("getcwd");
2706 error = got_worktree_open(&worktree, cwd);
2707 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2708 goto done;
2710 if (repo_path == NULL) {
2711 if (worktree)
2712 repo_path =
2713 strdup(got_worktree_get_repo_path(worktree));
2714 else
2715 repo_path = strdup(cwd);
2717 if (repo_path == NULL) {
2718 error = got_error_from_errno("strdup");
2719 goto done;
2722 error = got_repo_open(&repo, repo_path, NULL);
2723 if (error != NULL)
2724 goto done;
2726 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2727 repo, worktree);
2728 if (error)
2729 goto done;
2731 init_curses();
2733 error = apply_unveil(got_repo_get_path(repo),
2734 worktree ? got_worktree_get_root_path(worktree) : NULL);
2735 if (error)
2736 goto done;
2738 if (start_commit == NULL) {
2739 error = got_repo_match_object_id(&start_id, &label,
2740 worktree ? got_worktree_get_head_ref_name(worktree) :
2741 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2742 if (error)
2743 goto done;
2744 head_ref_name = label;
2745 } else {
2746 error = got_ref_open(&ref, repo, start_commit, 0);
2747 if (error == NULL)
2748 head_ref_name = got_ref_get_name(ref);
2749 else if (error->code != GOT_ERR_NOT_REF)
2750 goto done;
2751 error = got_repo_match_object_id(&start_id, NULL,
2752 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2753 if (error)
2754 goto done;
2757 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2758 if (view == NULL) {
2759 error = got_error_from_errno("view_open");
2760 goto done;
2762 error = open_log_view(view, start_id, repo, head_ref_name,
2763 in_repo_path, log_branches);
2764 if (error)
2765 goto done;
2766 if (worktree) {
2767 /* Release work tree lock. */
2768 got_worktree_close(worktree);
2769 worktree = NULL;
2771 error = view_loop(view);
2772 done:
2773 free(in_repo_path);
2774 free(repo_path);
2775 free(cwd);
2776 free(start_id);
2777 free(label);
2778 if (ref)
2779 got_ref_close(ref);
2780 if (repo)
2781 got_repo_close(repo);
2782 if (worktree)
2783 got_worktree_close(worktree);
2784 return error;
2787 __dead static void
2788 usage_diff(void)
2790 endwin();
2791 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2792 "[-w] object1 object2\n", getprogname());
2793 exit(1);
2796 static int
2797 match_line(const char *line, regex_t *regex, size_t nmatch,
2798 regmatch_t *regmatch)
2800 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2803 struct tog_color *
2804 match_color(struct tog_colors *colors, const char *line)
2806 struct tog_color *tc = NULL;
2808 SIMPLEQ_FOREACH(tc, colors, entry) {
2809 if (match_line(line, &tc->regex, 0, NULL))
2810 return tc;
2813 return NULL;
2816 static const struct got_error *
2817 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2818 WINDOW *window, regmatch_t *regmatch)
2820 const struct got_error *err = NULL;
2821 wchar_t *wline;
2822 int width;
2823 char *s;
2825 *wtotal = 0;
2827 s = strndup(line, regmatch->rm_so);
2828 if (s == NULL)
2829 return got_error_from_errno("strndup");
2831 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2832 if (err) {
2833 free(s);
2834 return err;
2836 waddwstr(window, wline);
2837 free(wline);
2838 free(s);
2839 wlimit -= width;
2840 *wtotal += width;
2842 if (wlimit > 0) {
2843 s = strndup(line + regmatch->rm_so,
2844 regmatch->rm_eo - regmatch->rm_so);
2845 if (s == NULL) {
2846 err = got_error_from_errno("strndup");
2847 free(s);
2848 return err;
2850 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2851 if (err) {
2852 free(s);
2853 return err;
2855 wattr_on(window, A_STANDOUT, NULL);
2856 waddwstr(window, wline);
2857 wattr_off(window, A_STANDOUT, NULL);
2858 free(wline);
2859 free(s);
2860 wlimit -= width;
2861 *wtotal += width;
2864 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2865 err = format_line(&wline, &width,
2866 line + regmatch->rm_eo, wlimit, col_tab_align);
2867 if (err)
2868 return err;
2869 waddwstr(window, wline);
2870 free(wline);
2871 *wtotal += width;
2874 return NULL;
2877 static const struct got_error *
2878 draw_file(struct tog_view *view, const char *header)
2880 struct tog_diff_view_state *s = &view->state.diff;
2881 regmatch_t *regmatch = &view->regmatch;
2882 const struct got_error *err;
2883 int nprinted = 0;
2884 char *line;
2885 size_t linesize = 0;
2886 ssize_t linelen;
2887 struct tog_color *tc;
2888 wchar_t *wline;
2889 int width;
2890 int max_lines = view->nlines;
2891 int nlines = s->nlines;
2892 off_t line_offset;
2894 line_offset = s->line_offsets[s->first_displayed_line - 1];
2895 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2896 return got_error_from_errno("fseek");
2898 werase(view->window);
2900 if (header) {
2901 if (asprintf(&line, "[%d/%d] %s",
2902 s->first_displayed_line - 1 + s->selected_line, nlines,
2903 header) == -1)
2904 return got_error_from_errno("asprintf");
2905 err = format_line(&wline, &width, line, view->ncols, 0);
2906 free(line);
2907 if (err)
2908 return err;
2910 if (view_needs_focus_indication(view))
2911 wstandout(view->window);
2912 waddwstr(view->window, wline);
2913 free(wline);
2914 wline = NULL;
2915 if (view_needs_focus_indication(view))
2916 wstandend(view->window);
2917 if (width <= view->ncols - 1)
2918 waddch(view->window, '\n');
2920 if (max_lines <= 1)
2921 return NULL;
2922 max_lines--;
2925 s->eof = 0;
2926 line = NULL;
2927 while (max_lines > 0 && nprinted < max_lines) {
2928 linelen = getline(&line, &linesize, s->f);
2929 if (linelen == -1) {
2930 if (feof(s->f)) {
2931 s->eof = 1;
2932 break;
2934 free(line);
2935 return got_ferror(s->f, GOT_ERR_IO);
2938 tc = match_color(&s->colors, line);
2939 if (tc)
2940 wattr_on(view->window,
2941 COLOR_PAIR(tc->colorpair), NULL);
2942 if (s->first_displayed_line + nprinted == s->matched_line &&
2943 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2944 err = add_matched_line(&width, line, view->ncols, 0,
2945 view->window, regmatch);
2946 if (err) {
2947 free(line);
2948 return err;
2950 } else {
2951 err = format_line(&wline, &width, line, view->ncols, 0);
2952 if (err) {
2953 free(line);
2954 return err;
2956 waddwstr(view->window, wline);
2957 free(wline);
2958 wline = NULL;
2960 if (tc)
2961 wattr_off(view->window,
2962 COLOR_PAIR(tc->colorpair), NULL);
2963 if (width <= view->ncols - 1)
2964 waddch(view->window, '\n');
2965 nprinted++;
2967 free(line);
2968 if (nprinted >= 1)
2969 s->last_displayed_line = s->first_displayed_line +
2970 (nprinted - 1);
2971 else
2972 s->last_displayed_line = s->first_displayed_line;
2974 view_vborder(view);
2976 if (s->eof) {
2977 while (nprinted < view->nlines) {
2978 waddch(view->window, '\n');
2979 nprinted++;
2982 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2983 if (err) {
2984 return err;
2987 wstandout(view->window);
2988 waddwstr(view->window, wline);
2989 free(wline);
2990 wline = NULL;
2991 wstandend(view->window);
2994 return NULL;
2997 static char *
2998 get_datestr(time_t *time, char *datebuf)
3000 struct tm mytm, *tm;
3001 char *p, *s;
3003 tm = gmtime_r(time, &mytm);
3004 if (tm == NULL)
3005 return NULL;
3006 s = asctime_r(tm, datebuf);
3007 if (s == NULL)
3008 return NULL;
3009 p = strchr(s, '\n');
3010 if (p)
3011 *p = '\0';
3012 return s;
3015 static const struct got_error *
3016 get_changed_paths(struct got_pathlist_head *paths,
3017 struct got_commit_object *commit, struct got_repository *repo)
3019 const struct got_error *err = NULL;
3020 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3021 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3022 struct got_object_qid *qid;
3024 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3025 if (qid != NULL) {
3026 struct got_commit_object *pcommit;
3027 err = got_object_open_as_commit(&pcommit, repo,
3028 qid->id);
3029 if (err)
3030 return err;
3032 tree_id1 = got_object_commit_get_tree_id(pcommit);
3033 got_object_commit_close(pcommit);
3037 if (tree_id1) {
3038 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3039 if (err)
3040 goto done;
3043 tree_id2 = got_object_commit_get_tree_id(commit);
3044 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3045 if (err)
3046 goto done;
3048 err = got_diff_tree(tree1, tree2, "", "", repo,
3049 got_diff_tree_collect_changed_paths, paths, 0);
3050 done:
3051 if (tree1)
3052 got_object_tree_close(tree1);
3053 if (tree2)
3054 got_object_tree_close(tree2);
3055 return err;
3058 static const struct got_error *
3059 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3061 off_t *p;
3063 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3064 if (p == NULL)
3065 return got_error_from_errno("reallocarray");
3066 *line_offsets = p;
3067 (*line_offsets)[*nlines] = off;
3068 (*nlines)++;
3069 return NULL;
3072 static const struct got_error *
3073 write_commit_info(off_t **line_offsets, size_t *nlines,
3074 struct got_object_id *commit_id, struct got_reflist_head *refs,
3075 struct got_repository *repo, FILE *outfile)
3077 const struct got_error *err = NULL;
3078 char datebuf[26], *datestr;
3079 struct got_commit_object *commit;
3080 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3081 time_t committer_time;
3082 const char *author, *committer;
3083 char *refs_str = NULL;
3084 struct got_pathlist_head changed_paths;
3085 struct got_pathlist_entry *pe;
3086 off_t outoff = 0;
3087 int n;
3089 TAILQ_INIT(&changed_paths);
3091 if (refs) {
3092 err = build_refs_str(&refs_str, refs, commit_id, repo);
3093 if (err)
3094 return err;
3097 err = got_object_open_as_commit(&commit, repo, commit_id);
3098 if (err)
3099 return err;
3101 err = got_object_id_str(&id_str, commit_id);
3102 if (err) {
3103 err = got_error_from_errno("got_object_id_str");
3104 goto done;
3107 err = add_line_offset(line_offsets, nlines, 0);
3108 if (err)
3109 goto done;
3111 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3112 refs_str ? refs_str : "", refs_str ? ")" : "");
3113 if (n < 0) {
3114 err = got_error_from_errno("fprintf");
3115 goto done;
3117 outoff += n;
3118 err = add_line_offset(line_offsets, nlines, outoff);
3119 if (err)
3120 goto done;
3122 n = fprintf(outfile, "from: %s\n",
3123 got_object_commit_get_author(commit));
3124 if (n < 0) {
3125 err = got_error_from_errno("fprintf");
3126 goto done;
3128 outoff += n;
3129 err = add_line_offset(line_offsets, nlines, outoff);
3130 if (err)
3131 goto done;
3133 committer_time = got_object_commit_get_committer_time(commit);
3134 datestr = get_datestr(&committer_time, datebuf);
3135 if (datestr) {
3136 n = fprintf(outfile, "date: %s UTC\n", datestr);
3137 if (n < 0) {
3138 err = got_error_from_errno("fprintf");
3139 goto done;
3141 outoff += n;
3142 err = add_line_offset(line_offsets, nlines, outoff);
3143 if (err)
3144 goto done;
3146 author = got_object_commit_get_author(commit);
3147 committer = got_object_commit_get_committer(commit);
3148 if (strcmp(author, committer) != 0) {
3149 n = fprintf(outfile, "via: %s\n", committer);
3150 if (n < 0) {
3151 err = got_error_from_errno("fprintf");
3152 goto done;
3154 outoff += n;
3155 err = add_line_offset(line_offsets, nlines, outoff);
3156 if (err)
3157 goto done;
3159 err = got_object_commit_get_logmsg(&logmsg, commit);
3160 if (err)
3161 goto done;
3162 s = logmsg;
3163 while ((line = strsep(&s, "\n")) != NULL) {
3164 n = fprintf(outfile, "%s\n", line);
3165 if (n < 0) {
3166 err = got_error_from_errno("fprintf");
3167 goto done;
3169 outoff += n;
3170 err = add_line_offset(line_offsets, nlines, outoff);
3171 if (err)
3172 goto done;
3175 err = get_changed_paths(&changed_paths, commit, repo);
3176 if (err)
3177 goto done;
3178 TAILQ_FOREACH(pe, &changed_paths, entry) {
3179 struct got_diff_changed_path *cp = pe->data;
3180 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3181 if (n < 0) {
3182 err = got_error_from_errno("fprintf");
3183 goto done;
3185 outoff += n;
3186 err = add_line_offset(line_offsets, nlines, outoff);
3187 if (err)
3188 goto done;
3189 free((char *)pe->path);
3190 free(pe->data);
3193 fputc('\n', outfile);
3194 outoff++;
3195 err = add_line_offset(line_offsets, nlines, outoff);
3196 done:
3197 got_pathlist_free(&changed_paths);
3198 free(id_str);
3199 free(logmsg);
3200 free(refs_str);
3201 got_object_commit_close(commit);
3202 if (err) {
3203 free(*line_offsets);
3204 *line_offsets = NULL;
3205 *nlines = 0;
3207 return err;
3210 static const struct got_error *
3211 create_diff(struct tog_diff_view_state *s)
3213 const struct got_error *err = NULL;
3214 FILE *f = NULL;
3215 int obj_type;
3217 free(s->line_offsets);
3218 s->line_offsets = malloc(sizeof(off_t));
3219 if (s->line_offsets == NULL)
3220 return got_error_from_errno("malloc");
3221 s->nlines = 0;
3223 f = got_opentemp();
3224 if (f == NULL) {
3225 err = got_error_from_errno("got_opentemp");
3226 goto done;
3228 if (s->f && fclose(s->f) != 0) {
3229 err = got_error_from_errno("fclose");
3230 goto done;
3232 s->f = f;
3234 if (s->id1)
3235 err = got_object_get_type(&obj_type, s->repo, s->id1);
3236 else
3237 err = got_object_get_type(&obj_type, s->repo, s->id2);
3238 if (err)
3239 goto done;
3241 switch (obj_type) {
3242 case GOT_OBJ_TYPE_BLOB:
3243 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3244 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3245 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3246 break;
3247 case GOT_OBJ_TYPE_TREE:
3248 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3249 s->id1, s->id2, "", "", s->diff_context,
3250 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3251 break;
3252 case GOT_OBJ_TYPE_COMMIT: {
3253 const struct got_object_id_queue *parent_ids;
3254 struct got_object_qid *pid;
3255 struct got_commit_object *commit2;
3257 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3258 if (err)
3259 goto done;
3260 /* Show commit info if we're diffing to a parent/root commit. */
3261 if (s->id1 == NULL) {
3262 err = write_commit_info(&s->line_offsets, &s->nlines,
3263 s->id2, &s->refs, s->repo, s->f);
3264 if (err)
3265 goto done;
3266 } else {
3267 parent_ids = got_object_commit_get_parent_ids(commit2);
3268 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3269 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3270 err = write_commit_info(
3271 &s->line_offsets, &s->nlines,
3272 s->id2, &s->refs, s->repo, s->f);
3273 if (err)
3274 goto done;
3275 break;
3279 got_object_commit_close(commit2);
3281 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3282 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3283 s->force_text_diff, s->repo, s->f);
3284 break;
3286 default:
3287 err = got_error(GOT_ERR_OBJ_TYPE);
3288 break;
3290 if (err)
3291 goto done;
3292 done:
3293 if (s->f && fflush(s->f) != 0 && err == NULL)
3294 err = got_error_from_errno("fflush");
3295 return err;
3298 static void
3299 diff_view_indicate_progress(struct tog_view *view)
3301 mvwaddstr(view->window, 0, 0, "diffing...");
3302 update_panels();
3303 doupdate();
3306 static const struct got_error *
3307 search_start_diff_view(struct tog_view *view)
3309 struct tog_diff_view_state *s = &view->state.diff;
3311 s->matched_line = 0;
3312 return NULL;
3315 static const struct got_error *
3316 search_next_diff_view(struct tog_view *view)
3318 struct tog_diff_view_state *s = &view->state.diff;
3319 int lineno;
3320 char *line = NULL;
3321 size_t linesize = 0;
3322 ssize_t linelen;
3324 if (!view->searching) {
3325 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3326 return NULL;
3329 if (s->matched_line) {
3330 if (view->searching == TOG_SEARCH_FORWARD)
3331 lineno = s->matched_line + 1;
3332 else
3333 lineno = s->matched_line - 1;
3334 } else {
3335 if (view->searching == TOG_SEARCH_FORWARD)
3336 lineno = 1;
3337 else
3338 lineno = s->nlines;
3341 while (1) {
3342 off_t offset;
3344 if (lineno <= 0 || lineno > s->nlines) {
3345 if (s->matched_line == 0) {
3346 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3347 break;
3350 if (view->searching == TOG_SEARCH_FORWARD)
3351 lineno = 1;
3352 else
3353 lineno = s->nlines;
3356 offset = s->line_offsets[lineno - 1];
3357 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3358 free(line);
3359 return got_error_from_errno("fseeko");
3361 linelen = getline(&line, &linesize, s->f);
3362 if (linelen != -1 &&
3363 match_line(line, &view->regex, 1, &view->regmatch)) {
3364 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3365 s->matched_line = lineno;
3366 break;
3368 if (view->searching == TOG_SEARCH_FORWARD)
3369 lineno++;
3370 else
3371 lineno--;
3373 free(line);
3375 if (s->matched_line) {
3376 s->first_displayed_line = s->matched_line;
3377 s->selected_line = 1;
3380 return NULL;
3383 static const struct got_error *
3384 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3385 struct got_object_id *id2, const char *label1, const char *label2,
3386 int diff_context, int ignore_whitespace, int force_text_diff,
3387 struct tog_view *log_view, struct got_repository *repo)
3389 const struct got_error *err;
3390 struct tog_diff_view_state *s = &view->state.diff;
3392 SIMPLEQ_INIT(&s->refs);
3394 if (id1 != NULL && id2 != NULL) {
3395 int type1, type2;
3396 err = got_object_get_type(&type1, repo, id1);
3397 if (err)
3398 return err;
3399 err = got_object_get_type(&type2, repo, id2);
3400 if (err)
3401 return err;
3403 if (type1 != type2)
3404 return got_error(GOT_ERR_OBJ_TYPE);
3406 s->first_displayed_line = 1;
3407 s->last_displayed_line = view->nlines;
3408 s->selected_line = 1;
3409 s->repo = repo;
3410 s->id1 = id1;
3411 s->id2 = id2;
3412 s->label1 = label1;
3413 s->label2 = label2;
3415 if (id1) {
3416 s->id1 = got_object_id_dup(id1);
3417 if (s->id1 == NULL)
3418 return got_error_from_errno("got_object_id_dup");
3419 } else
3420 s->id1 = NULL;
3422 s->id2 = got_object_id_dup(id2);
3423 if (s->id2 == NULL) {
3424 free(s->id1);
3425 s->id1 = NULL;
3426 return got_error_from_errno("got_object_id_dup");
3428 s->f = NULL;
3429 s->first_displayed_line = 1;
3430 s->last_displayed_line = view->nlines;
3431 s->diff_context = diff_context;
3432 s->ignore_whitespace = ignore_whitespace;
3433 s->force_text_diff = force_text_diff;
3434 s->log_view = log_view;
3435 s->repo = repo;
3437 SIMPLEQ_INIT(&s->colors);
3438 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3439 err = add_color(&s->colors,
3440 "^-", TOG_COLOR_DIFF_MINUS,
3441 get_color_value("TOG_COLOR_DIFF_MINUS"));
3442 if (err)
3443 return err;
3444 err = add_color(&s->colors, "^\\+",
3445 TOG_COLOR_DIFF_PLUS,
3446 get_color_value("TOG_COLOR_DIFF_PLUS"));
3447 if (err) {
3448 free_colors(&s->colors);
3449 return err;
3451 err = add_color(&s->colors,
3452 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3453 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3454 if (err) {
3455 free_colors(&s->colors);
3456 return err;
3459 err = add_color(&s->colors,
3460 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3461 TOG_COLOR_DIFF_META,
3462 get_color_value("TOG_COLOR_DIFF_META"));
3463 if (err) {
3464 free_colors(&s->colors);
3465 return err;
3468 err = add_color(&s->colors,
3469 "^(from|via): ", TOG_COLOR_AUTHOR,
3470 get_color_value("TOG_COLOR_AUTHOR"));
3471 if (err) {
3472 free_colors(&s->colors);
3473 return err;
3476 err = add_color(&s->colors,
3477 "^date: ", TOG_COLOR_DATE,
3478 get_color_value("TOG_COLOR_DATE"));
3479 if (err) {
3480 free_colors(&s->colors);
3481 return err;
3485 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3486 if (err) {
3487 free(s->id1);
3488 s->id1 = NULL;
3489 free(s->id2);
3490 s->id2 = NULL;
3491 free_colors(&s->colors);
3492 return err;
3495 if (log_view && view_is_splitscreen(view))
3496 show_log_view(log_view); /* draw vborder */
3497 diff_view_indicate_progress(view);
3499 s->line_offsets = NULL;
3500 s->nlines = 0;
3501 err = create_diff(s);
3502 if (err) {
3503 free(s->id1);
3504 s->id1 = NULL;
3505 free(s->id2);
3506 s->id2 = NULL;
3507 free_colors(&s->colors);
3508 got_ref_list_free(&s->refs);
3509 return err;
3512 view->show = show_diff_view;
3513 view->input = input_diff_view;
3514 view->close = close_diff_view;
3515 view->search_start = search_start_diff_view;
3516 view->search_next = search_next_diff_view;
3518 return NULL;
3521 static const struct got_error *
3522 close_diff_view(struct tog_view *view)
3524 const struct got_error *err = NULL;
3525 struct tog_diff_view_state *s = &view->state.diff;
3527 free(s->id1);
3528 s->id1 = NULL;
3529 free(s->id2);
3530 s->id2 = NULL;
3531 if (s->f && fclose(s->f) == EOF)
3532 err = got_error_from_errno("fclose");
3533 free_colors(&s->colors);
3534 free(s->line_offsets);
3535 s->line_offsets = NULL;
3536 s->nlines = 0;
3537 got_ref_list_free(&s->refs);
3538 return err;
3541 static const struct got_error *
3542 show_diff_view(struct tog_view *view)
3544 const struct got_error *err;
3545 struct tog_diff_view_state *s = &view->state.diff;
3546 char *id_str1 = NULL, *id_str2, *header;
3547 const char *label1, *label2;
3549 if (s->id1) {
3550 err = got_object_id_str(&id_str1, s->id1);
3551 if (err)
3552 return err;
3553 label1 = s->label1 ? : id_str1;
3554 } else
3555 label1 = "/dev/null";
3557 err = got_object_id_str(&id_str2, s->id2);
3558 if (err)
3559 return err;
3560 label2 = s->label2 ? : id_str2;
3562 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3563 err = got_error_from_errno("asprintf");
3564 free(id_str1);
3565 free(id_str2);
3566 return err;
3568 free(id_str1);
3569 free(id_str2);
3571 return draw_file(view, header);
3574 static const struct got_error *
3575 set_selected_commit(struct tog_diff_view_state *s,
3576 struct commit_queue_entry *entry)
3578 const struct got_error *err;
3579 const struct got_object_id_queue *parent_ids;
3580 struct got_commit_object *selected_commit;
3581 struct got_object_qid *pid;
3583 free(s->id2);
3584 s->id2 = got_object_id_dup(entry->id);
3585 if (s->id2 == NULL)
3586 return got_error_from_errno("got_object_id_dup");
3588 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3589 if (err)
3590 return err;
3591 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3592 free(s->id1);
3593 pid = SIMPLEQ_FIRST(parent_ids);
3594 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3595 got_object_commit_close(selected_commit);
3596 return NULL;
3599 static const struct got_error *
3600 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3602 const struct got_error *err = NULL;
3603 struct tog_diff_view_state *s = &view->state.diff;
3604 struct tog_log_view_state *ls;
3605 struct commit_queue_entry *old_selected_entry;
3606 char *line = NULL;
3607 size_t linesize = 0;
3608 ssize_t linelen;
3609 int i;
3611 switch (ch) {
3612 case 'a':
3613 case 'w':
3614 if (ch == 'a')
3615 s->force_text_diff = !s->force_text_diff;
3616 if (ch == 'w')
3617 s->ignore_whitespace = !s->ignore_whitespace;
3618 wclear(view->window);
3619 s->first_displayed_line = 1;
3620 s->last_displayed_line = view->nlines;
3621 diff_view_indicate_progress(view);
3622 err = create_diff(s);
3623 break;
3624 case 'k':
3625 case KEY_UP:
3626 if (s->first_displayed_line > 1)
3627 s->first_displayed_line--;
3628 break;
3629 case KEY_PPAGE:
3630 case CTRL('b'):
3631 if (s->first_displayed_line == 1)
3632 break;
3633 i = 0;
3634 while (i++ < view->nlines - 1 &&
3635 s->first_displayed_line > 1)
3636 s->first_displayed_line--;
3637 break;
3638 case 'j':
3639 case KEY_DOWN:
3640 if (!s->eof)
3641 s->first_displayed_line++;
3642 break;
3643 case KEY_NPAGE:
3644 case CTRL('f'):
3645 case ' ':
3646 if (s->eof)
3647 break;
3648 i = 0;
3649 while (!s->eof && i++ < view->nlines - 1) {
3650 linelen = getline(&line, &linesize, s->f);
3651 s->first_displayed_line++;
3652 if (linelen == -1) {
3653 if (feof(s->f)) {
3654 s->eof = 1;
3655 } else
3656 err = got_ferror(s->f, GOT_ERR_IO);
3657 break;
3660 free(line);
3661 break;
3662 case '[':
3663 if (s->diff_context > 0) {
3664 s->diff_context--;
3665 diff_view_indicate_progress(view);
3666 err = create_diff(s);
3667 if (s->first_displayed_line + view->nlines - 1 >
3668 s->nlines) {
3669 s->first_displayed_line = 1;
3670 s->last_displayed_line = view->nlines;
3673 break;
3674 case ']':
3675 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3676 s->diff_context++;
3677 diff_view_indicate_progress(view);
3678 err = create_diff(s);
3680 break;
3681 case '<':
3682 case ',':
3683 if (s->log_view == NULL)
3684 break;
3685 ls = &s->log_view->state.log;
3686 old_selected_entry = ls->selected_entry;
3688 err = input_log_view(NULL, s->log_view, KEY_UP);
3689 if (err)
3690 break;
3692 if (old_selected_entry == ls->selected_entry)
3693 break;
3695 err = set_selected_commit(s, ls->selected_entry);
3696 if (err)
3697 break;
3699 s->first_displayed_line = 1;
3700 s->last_displayed_line = view->nlines;
3702 diff_view_indicate_progress(view);
3703 err = create_diff(s);
3704 break;
3705 case '>':
3706 case '.':
3707 if (s->log_view == NULL)
3708 break;
3709 ls = &s->log_view->state.log;
3710 old_selected_entry = ls->selected_entry;
3712 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3713 if (err)
3714 break;
3716 if (old_selected_entry == ls->selected_entry)
3717 break;
3719 err = set_selected_commit(s, ls->selected_entry);
3720 if (err)
3721 break;
3723 s->first_displayed_line = 1;
3724 s->last_displayed_line = view->nlines;
3726 diff_view_indicate_progress(view);
3727 err = create_diff(s);
3728 break;
3729 default:
3730 break;
3733 return err;
3736 static const struct got_error *
3737 cmd_diff(int argc, char *argv[])
3739 const struct got_error *error = NULL;
3740 struct got_repository *repo = NULL;
3741 struct got_worktree *worktree = NULL;
3742 struct got_object_id *id1 = NULL, *id2 = NULL;
3743 char *repo_path = NULL, *cwd = NULL;
3744 char *id_str1 = NULL, *id_str2 = NULL;
3745 char *label1 = NULL, *label2 = NULL;
3746 int diff_context = 3, ignore_whitespace = 0;
3747 int ch, force_text_diff = 0;
3748 const char *errstr;
3749 struct tog_view *view;
3751 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3752 switch (ch) {
3753 case 'a':
3754 force_text_diff = 1;
3755 break;
3756 case 'C':
3757 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3758 &errstr);
3759 if (errstr != NULL)
3760 err(1, "-C option %s", errstr);
3761 break;
3762 case 'r':
3763 repo_path = realpath(optarg, NULL);
3764 if (repo_path == NULL)
3765 return got_error_from_errno2("realpath",
3766 optarg);
3767 got_path_strip_trailing_slashes(repo_path);
3768 break;
3769 case 'w':
3770 ignore_whitespace = 1;
3771 break;
3772 default:
3773 usage_diff();
3774 /* NOTREACHED */
3778 argc -= optind;
3779 argv += optind;
3781 if (argc == 0) {
3782 usage_diff(); /* TODO show local worktree changes */
3783 } else if (argc == 2) {
3784 id_str1 = argv[0];
3785 id_str2 = argv[1];
3786 } else
3787 usage_diff();
3789 cwd = getcwd(NULL, 0);
3790 if (cwd == NULL)
3791 return got_error_from_errno("getcwd");
3793 error = got_worktree_open(&worktree, cwd);
3794 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3795 goto done;
3797 if (repo_path == NULL) {
3798 if (worktree)
3799 repo_path =
3800 strdup(got_worktree_get_repo_path(worktree));
3801 else
3802 repo_path = strdup(cwd);
3804 if (repo_path == NULL) {
3805 error = got_error_from_errno("strdup");
3806 goto done;
3809 error = got_repo_open(&repo, repo_path, NULL);
3810 if (error)
3811 goto done;
3813 init_curses();
3815 error = apply_unveil(got_repo_get_path(repo), NULL);
3816 if (error)
3817 goto done;
3819 error = got_repo_match_object_id(&id1, &label1, id_str1,
3820 GOT_OBJ_TYPE_ANY, 1, repo);
3821 if (error)
3822 goto done;
3824 error = got_repo_match_object_id(&id2, &label2, id_str2,
3825 GOT_OBJ_TYPE_ANY, 1, repo);
3826 if (error)
3827 goto done;
3829 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3830 if (view == NULL) {
3831 error = got_error_from_errno("view_open");
3832 goto done;
3834 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3835 ignore_whitespace, force_text_diff, NULL, repo);
3836 if (error)
3837 goto done;
3838 error = view_loop(view);
3839 done:
3840 free(label1);
3841 free(label2);
3842 free(repo_path);
3843 free(cwd);
3844 if (repo)
3845 got_repo_close(repo);
3846 if (worktree)
3847 got_worktree_close(worktree);
3848 return error;
3851 __dead static void
3852 usage_blame(void)
3854 endwin();
3855 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3856 getprogname());
3857 exit(1);
3860 struct tog_blame_line {
3861 int annotated;
3862 struct got_object_id *id;
3865 static const struct got_error *
3866 draw_blame(struct tog_view *view)
3868 struct tog_blame_view_state *s = &view->state.blame;
3869 struct tog_blame *blame = &s->blame;
3870 regmatch_t *regmatch = &view->regmatch;
3871 const struct got_error *err;
3872 int lineno = 0, nprinted = 0;
3873 char *line = NULL;
3874 size_t linesize = 0;
3875 ssize_t linelen;
3876 wchar_t *wline;
3877 int width;
3878 struct tog_blame_line *blame_line;
3879 struct got_object_id *prev_id = NULL;
3880 char *id_str;
3881 struct tog_color *tc;
3883 err = got_object_id_str(&id_str, s->blamed_commit->id);
3884 if (err)
3885 return err;
3887 rewind(blame->f);
3888 werase(view->window);
3890 if (asprintf(&line, "commit %s", id_str) == -1) {
3891 err = got_error_from_errno("asprintf");
3892 free(id_str);
3893 return err;
3896 err = format_line(&wline, &width, line, view->ncols, 0);
3897 free(line);
3898 line = NULL;
3899 if (err)
3900 return err;
3901 if (view_needs_focus_indication(view))
3902 wstandout(view->window);
3903 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3904 if (tc)
3905 wattr_on(view->window,
3906 COLOR_PAIR(tc->colorpair), NULL);
3907 waddwstr(view->window, wline);
3908 if (tc)
3909 wattr_off(view->window,
3910 COLOR_PAIR(tc->colorpair), NULL);
3911 if (view_needs_focus_indication(view))
3912 wstandend(view->window);
3913 free(wline);
3914 wline = NULL;
3915 if (width < view->ncols - 1)
3916 waddch(view->window, '\n');
3918 if (asprintf(&line, "[%d/%d] %s%s",
3919 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3920 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3921 free(id_str);
3922 return got_error_from_errno("asprintf");
3924 free(id_str);
3925 err = format_line(&wline, &width, line, view->ncols, 0);
3926 free(line);
3927 line = NULL;
3928 if (err)
3929 return err;
3930 waddwstr(view->window, wline);
3931 free(wline);
3932 wline = NULL;
3933 if (width < view->ncols - 1)
3934 waddch(view->window, '\n');
3936 s->eof = 0;
3937 while (nprinted < view->nlines - 2) {
3938 linelen = getline(&line, &linesize, blame->f);
3939 if (linelen == -1) {
3940 if (feof(blame->f)) {
3941 s->eof = 1;
3942 break;
3944 free(line);
3945 return got_ferror(blame->f, GOT_ERR_IO);
3947 if (++lineno < s->first_displayed_line)
3948 continue;
3950 if (view->focussed && nprinted == s->selected_line - 1)
3951 wstandout(view->window);
3953 if (blame->nlines > 0) {
3954 blame_line = &blame->lines[lineno - 1];
3955 if (blame_line->annotated && prev_id &&
3956 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3957 !(view->focussed &&
3958 nprinted == s->selected_line - 1)) {
3959 waddstr(view->window, " ");
3960 } else if (blame_line->annotated) {
3961 char *id_str;
3962 err = got_object_id_str(&id_str, blame_line->id);
3963 if (err) {
3964 free(line);
3965 return err;
3967 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3968 if (tc)
3969 wattr_on(view->window,
3970 COLOR_PAIR(tc->colorpair), NULL);
3971 wprintw(view->window, "%.8s", id_str);
3972 if (tc)
3973 wattr_off(view->window,
3974 COLOR_PAIR(tc->colorpair), NULL);
3975 free(id_str);
3976 prev_id = blame_line->id;
3977 } else {
3978 waddstr(view->window, "........");
3979 prev_id = NULL;
3981 } else {
3982 waddstr(view->window, "........");
3983 prev_id = NULL;
3986 if (view->focussed && nprinted == s->selected_line - 1)
3987 wstandend(view->window);
3988 waddstr(view->window, " ");
3990 if (view->ncols <= 9) {
3991 width = 9;
3992 wline = wcsdup(L"");
3993 if (wline == NULL) {
3994 err = got_error_from_errno("wcsdup");
3995 free(line);
3996 return err;
3998 } else if (s->first_displayed_line + nprinted ==
3999 s->matched_line &&
4000 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4001 err = add_matched_line(&width, line, view->ncols - 9, 9,
4002 view->window, regmatch);
4003 if (err) {
4004 free(line);
4005 return err;
4007 width += 9;
4008 } else {
4009 err = format_line(&wline, &width, line,
4010 view->ncols - 9, 9);
4011 waddwstr(view->window, wline);
4012 free(wline);
4013 wline = NULL;
4014 width += 9;
4017 if (width <= view->ncols - 1)
4018 waddch(view->window, '\n');
4019 if (++nprinted == 1)
4020 s->first_displayed_line = lineno;
4022 free(line);
4023 s->last_displayed_line = lineno;
4025 view_vborder(view);
4027 return NULL;
4030 static const struct got_error *
4031 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4033 const struct got_error *err = NULL;
4034 struct tog_blame_cb_args *a = arg;
4035 struct tog_blame_line *line;
4036 int errcode;
4038 if (nlines != a->nlines ||
4039 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4040 return got_error(GOT_ERR_RANGE);
4042 errcode = pthread_mutex_lock(&tog_mutex);
4043 if (errcode)
4044 return got_error_set_errno(errcode, "pthread_mutex_lock");
4046 if (*a->quit) { /* user has quit the blame view */
4047 err = got_error(GOT_ERR_ITER_COMPLETED);
4048 goto done;
4051 if (lineno == -1)
4052 goto done; /* no change in this commit */
4054 line = &a->lines[lineno - 1];
4055 if (line->annotated)
4056 goto done;
4058 line->id = got_object_id_dup(id);
4059 if (line->id == NULL) {
4060 err = got_error_from_errno("got_object_id_dup");
4061 goto done;
4063 line->annotated = 1;
4064 done:
4065 errcode = pthread_mutex_unlock(&tog_mutex);
4066 if (errcode)
4067 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4068 return err;
4071 static void *
4072 blame_thread(void *arg)
4074 const struct got_error *err;
4075 struct tog_blame_thread_args *ta = arg;
4076 struct tog_blame_cb_args *a = ta->cb_args;
4077 int errcode;
4079 err = block_signals_used_by_main_thread();
4080 if (err)
4081 return (void *)err;
4083 err = got_blame(ta->path, a->commit_id, ta->repo,
4084 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4085 if (err && err->code == GOT_ERR_CANCELLED)
4086 err = NULL;
4088 errcode = pthread_mutex_lock(&tog_mutex);
4089 if (errcode)
4090 return (void *)got_error_set_errno(errcode,
4091 "pthread_mutex_lock");
4093 got_repo_close(ta->repo);
4094 ta->repo = NULL;
4095 *ta->complete = 1;
4097 errcode = pthread_mutex_unlock(&tog_mutex);
4098 if (errcode && err == NULL)
4099 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4101 return (void *)err;
4104 static struct got_object_id *
4105 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4106 int first_displayed_line, int selected_line)
4108 struct tog_blame_line *line;
4110 if (nlines <= 0)
4111 return NULL;
4113 line = &lines[first_displayed_line - 1 + selected_line - 1];
4114 if (!line->annotated)
4115 return NULL;
4117 return line->id;
4120 static const struct got_error *
4121 stop_blame(struct tog_blame *blame)
4123 const struct got_error *err = NULL;
4124 int i;
4126 if (blame->thread) {
4127 int errcode;
4128 errcode = pthread_mutex_unlock(&tog_mutex);
4129 if (errcode)
4130 return got_error_set_errno(errcode,
4131 "pthread_mutex_unlock");
4132 errcode = pthread_join(blame->thread, (void **)&err);
4133 if (errcode)
4134 return got_error_set_errno(errcode, "pthread_join");
4135 errcode = pthread_mutex_lock(&tog_mutex);
4136 if (errcode)
4137 return got_error_set_errno(errcode,
4138 "pthread_mutex_lock");
4139 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4140 err = NULL;
4141 blame->thread = NULL;
4143 if (blame->thread_args.repo) {
4144 got_repo_close(blame->thread_args.repo);
4145 blame->thread_args.repo = NULL;
4147 if (blame->f) {
4148 if (fclose(blame->f) != 0 && err == NULL)
4149 err = got_error_from_errno("fclose");
4150 blame->f = NULL;
4152 if (blame->lines) {
4153 for (i = 0; i < blame->nlines; i++)
4154 free(blame->lines[i].id);
4155 free(blame->lines);
4156 blame->lines = NULL;
4158 free(blame->cb_args.commit_id);
4159 blame->cb_args.commit_id = NULL;
4161 return err;
4164 static const struct got_error *
4165 cancel_blame_view(void *arg)
4167 const struct got_error *err = NULL;
4168 int *done = arg;
4169 int errcode;
4171 errcode = pthread_mutex_lock(&tog_mutex);
4172 if (errcode)
4173 return got_error_set_errno(errcode,
4174 "pthread_mutex_unlock");
4176 if (*done)
4177 err = got_error(GOT_ERR_CANCELLED);
4179 errcode = pthread_mutex_unlock(&tog_mutex);
4180 if (errcode)
4181 return got_error_set_errno(errcode,
4182 "pthread_mutex_lock");
4184 return err;
4187 static const struct got_error *
4188 run_blame(struct tog_view *view)
4190 struct tog_blame_view_state *s = &view->state.blame;
4191 struct tog_blame *blame = &s->blame;
4192 const struct got_error *err = NULL;
4193 struct got_blob_object *blob = NULL;
4194 struct got_repository *thread_repo = NULL;
4195 struct got_object_id *obj_id = NULL;
4196 int obj_type;
4198 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4199 s->path);
4200 if (err)
4201 return err;
4203 err = got_object_get_type(&obj_type, s->repo, obj_id);
4204 if (err)
4205 goto done;
4207 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4208 err = got_error(GOT_ERR_OBJ_TYPE);
4209 goto done;
4212 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4213 if (err)
4214 goto done;
4215 blame->f = got_opentemp();
4216 if (blame->f == NULL) {
4217 err = got_error_from_errno("got_opentemp");
4218 goto done;
4220 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4221 &blame->line_offsets, blame->f, blob);
4222 if (err || blame->nlines == 0)
4223 goto done;
4225 /* Don't include \n at EOF in the blame line count. */
4226 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4227 blame->nlines--;
4229 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4230 if (blame->lines == NULL) {
4231 err = got_error_from_errno("calloc");
4232 goto done;
4235 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4236 if (err)
4237 goto done;
4239 blame->cb_args.view = view;
4240 blame->cb_args.lines = blame->lines;
4241 blame->cb_args.nlines = blame->nlines;
4242 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4243 if (blame->cb_args.commit_id == NULL) {
4244 err = got_error_from_errno("got_object_id_dup");
4245 goto done;
4247 blame->cb_args.quit = &s->done;
4249 blame->thread_args.path = s->path;
4250 blame->thread_args.repo = thread_repo;
4251 blame->thread_args.cb_args = &blame->cb_args;
4252 blame->thread_args.complete = &s->blame_complete;
4253 blame->thread_args.cancel_cb = cancel_blame_view;
4254 blame->thread_args.cancel_arg = &s->done;
4255 s->blame_complete = 0;
4257 done:
4258 if (blob)
4259 got_object_blob_close(blob);
4260 free(obj_id);
4261 if (err)
4262 stop_blame(blame);
4263 return err;
4266 static const struct got_error *
4267 open_blame_view(struct tog_view *view, char *path,
4268 struct got_object_id *commit_id, struct got_repository *repo)
4270 const struct got_error *err = NULL;
4271 struct tog_blame_view_state *s = &view->state.blame;
4273 SIMPLEQ_INIT(&s->blamed_commits);
4275 s->path = strdup(path);
4276 if (s->path == NULL)
4277 return got_error_from_errno("strdup");
4279 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4280 if (err) {
4281 free(s->path);
4282 return err;
4285 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4286 s->first_displayed_line = 1;
4287 s->last_displayed_line = view->nlines;
4288 s->selected_line = 1;
4289 s->blame_complete = 0;
4290 s->repo = repo;
4291 s->commit_id = commit_id;
4292 memset(&s->blame, 0, sizeof(s->blame));
4294 SIMPLEQ_INIT(&s->colors);
4295 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4296 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4297 get_color_value("TOG_COLOR_COMMIT"));
4298 if (err)
4299 return err;
4302 view->show = show_blame_view;
4303 view->input = input_blame_view;
4304 view->close = close_blame_view;
4305 view->search_start = search_start_blame_view;
4306 view->search_next = search_next_blame_view;
4308 return run_blame(view);
4311 static const struct got_error *
4312 close_blame_view(struct tog_view *view)
4314 const struct got_error *err = NULL;
4315 struct tog_blame_view_state *s = &view->state.blame;
4317 if (s->blame.thread)
4318 err = stop_blame(&s->blame);
4320 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4321 struct got_object_qid *blamed_commit;
4322 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4323 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4324 got_object_qid_free(blamed_commit);
4327 free(s->path);
4328 free_colors(&s->colors);
4330 return err;
4333 static const struct got_error *
4334 search_start_blame_view(struct tog_view *view)
4336 struct tog_blame_view_state *s = &view->state.blame;
4338 s->matched_line = 0;
4339 return NULL;
4342 static const struct got_error *
4343 search_next_blame_view(struct tog_view *view)
4345 struct tog_blame_view_state *s = &view->state.blame;
4346 int lineno;
4347 char *line = NULL;
4348 size_t linesize = 0;
4349 ssize_t linelen;
4351 if (!view->searching) {
4352 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4353 return NULL;
4356 if (s->matched_line) {
4357 if (view->searching == TOG_SEARCH_FORWARD)
4358 lineno = s->matched_line + 1;
4359 else
4360 lineno = s->matched_line - 1;
4361 } else {
4362 if (view->searching == TOG_SEARCH_FORWARD)
4363 lineno = 1;
4364 else
4365 lineno = s->blame.nlines;
4368 while (1) {
4369 off_t offset;
4371 if (lineno <= 0 || lineno > s->blame.nlines) {
4372 if (s->matched_line == 0) {
4373 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4374 break;
4377 if (view->searching == TOG_SEARCH_FORWARD)
4378 lineno = 1;
4379 else
4380 lineno = s->blame.nlines;
4383 offset = s->blame.line_offsets[lineno - 1];
4384 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4385 free(line);
4386 return got_error_from_errno("fseeko");
4388 linelen = getline(&line, &linesize, s->blame.f);
4389 if (linelen != -1 &&
4390 match_line(line, &view->regex, 1, &view->regmatch)) {
4391 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4392 s->matched_line = lineno;
4393 break;
4395 if (view->searching == TOG_SEARCH_FORWARD)
4396 lineno++;
4397 else
4398 lineno--;
4400 free(line);
4402 if (s->matched_line) {
4403 s->first_displayed_line = s->matched_line;
4404 s->selected_line = 1;
4407 return NULL;
4410 static const struct got_error *
4411 show_blame_view(struct tog_view *view)
4413 const struct got_error *err = NULL;
4414 struct tog_blame_view_state *s = &view->state.blame;
4415 int errcode;
4417 if (s->blame.thread == NULL) {
4418 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4419 &s->blame.thread_args);
4420 if (errcode)
4421 return got_error_set_errno(errcode, "pthread_create");
4423 halfdelay(1); /* fast refresh while annotating */
4426 if (s->blame_complete)
4427 halfdelay(10); /* disable fast refresh */
4429 err = draw_blame(view);
4431 view_vborder(view);
4432 return err;
4435 static const struct got_error *
4436 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4438 const struct got_error *err = NULL, *thread_err = NULL;
4439 struct tog_view *diff_view;
4440 struct tog_blame_view_state *s = &view->state.blame;
4441 int begin_x = 0;
4443 switch (ch) {
4444 case 'q':
4445 s->done = 1;
4446 break;
4447 case 'k':
4448 case KEY_UP:
4449 if (s->selected_line > 1)
4450 s->selected_line--;
4451 else if (s->selected_line == 1 &&
4452 s->first_displayed_line > 1)
4453 s->first_displayed_line--;
4454 break;
4455 case KEY_PPAGE:
4456 case CTRL('b'):
4457 if (s->first_displayed_line == 1) {
4458 s->selected_line = 1;
4459 break;
4461 if (s->first_displayed_line > view->nlines - 2)
4462 s->first_displayed_line -=
4463 (view->nlines - 2);
4464 else
4465 s->first_displayed_line = 1;
4466 break;
4467 case 'j':
4468 case KEY_DOWN:
4469 if (s->selected_line < view->nlines - 2 &&
4470 s->first_displayed_line +
4471 s->selected_line <= s->blame.nlines)
4472 s->selected_line++;
4473 else if (s->last_displayed_line <
4474 s->blame.nlines)
4475 s->first_displayed_line++;
4476 break;
4477 case 'b':
4478 case 'p': {
4479 struct got_object_id *id = NULL;
4480 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4481 s->first_displayed_line, s->selected_line);
4482 if (id == NULL)
4483 break;
4484 if (ch == 'p') {
4485 struct got_commit_object *commit;
4486 struct got_object_qid *pid;
4487 struct got_object_id *blob_id = NULL;
4488 int obj_type;
4489 err = got_object_open_as_commit(&commit,
4490 s->repo, id);
4491 if (err)
4492 break;
4493 pid = SIMPLEQ_FIRST(
4494 got_object_commit_get_parent_ids(commit));
4495 if (pid == NULL) {
4496 got_object_commit_close(commit);
4497 break;
4499 /* Check if path history ends here. */
4500 err = got_object_id_by_path(&blob_id, s->repo,
4501 pid->id, s->path);
4502 if (err) {
4503 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4504 err = NULL;
4505 got_object_commit_close(commit);
4506 break;
4508 err = got_object_get_type(&obj_type, s->repo,
4509 blob_id);
4510 free(blob_id);
4511 /* Can't blame non-blob type objects. */
4512 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4513 got_object_commit_close(commit);
4514 break;
4516 err = got_object_qid_alloc(&s->blamed_commit,
4517 pid->id);
4518 got_object_commit_close(commit);
4519 } else {
4520 if (got_object_id_cmp(id,
4521 s->blamed_commit->id) == 0)
4522 break;
4523 err = got_object_qid_alloc(&s->blamed_commit,
4524 id);
4526 if (err)
4527 break;
4528 s->done = 1;
4529 thread_err = stop_blame(&s->blame);
4530 s->done = 0;
4531 if (thread_err)
4532 break;
4533 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4534 s->blamed_commit, entry);
4535 err = run_blame(view);
4536 if (err)
4537 break;
4538 break;
4540 case 'B': {
4541 struct got_object_qid *first;
4542 first = SIMPLEQ_FIRST(&s->blamed_commits);
4543 if (!got_object_id_cmp(first->id, s->commit_id))
4544 break;
4545 s->done = 1;
4546 thread_err = stop_blame(&s->blame);
4547 s->done = 0;
4548 if (thread_err)
4549 break;
4550 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4551 got_object_qid_free(s->blamed_commit);
4552 s->blamed_commit =
4553 SIMPLEQ_FIRST(&s->blamed_commits);
4554 err = run_blame(view);
4555 if (err)
4556 break;
4557 break;
4559 case KEY_ENTER:
4560 case '\r': {
4561 struct got_object_id *id = NULL;
4562 struct got_object_qid *pid;
4563 struct got_commit_object *commit = NULL;
4564 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4565 s->first_displayed_line, s->selected_line);
4566 if (id == NULL)
4567 break;
4568 err = got_object_open_as_commit(&commit, s->repo, id);
4569 if (err)
4570 break;
4571 pid = SIMPLEQ_FIRST(
4572 got_object_commit_get_parent_ids(commit));
4573 if (view_is_parent_view(view))
4574 begin_x = view_split_begin_x(view->begin_x);
4575 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4576 if (diff_view == NULL) {
4577 got_object_commit_close(commit);
4578 err = got_error_from_errno("view_open");
4579 break;
4581 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4582 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4583 got_object_commit_close(commit);
4584 if (err) {
4585 view_close(diff_view);
4586 break;
4588 view->focussed = 0;
4589 diff_view->focussed = 1;
4590 if (view_is_parent_view(view)) {
4591 err = view_close_child(view);
4592 if (err)
4593 break;
4594 view_set_child(view, diff_view);
4595 view->focus_child = 1;
4596 } else
4597 *new_view = diff_view;
4598 if (err)
4599 break;
4600 break;
4602 case KEY_NPAGE:
4603 case CTRL('f'):
4604 case ' ':
4605 if (s->last_displayed_line >= s->blame.nlines &&
4606 s->selected_line >= MIN(s->blame.nlines,
4607 view->nlines - 2)) {
4608 break;
4610 if (s->last_displayed_line >= s->blame.nlines &&
4611 s->selected_line < view->nlines - 2) {
4612 s->selected_line = MIN(s->blame.nlines,
4613 view->nlines - 2);
4614 break;
4616 if (s->last_displayed_line + view->nlines - 2
4617 <= s->blame.nlines)
4618 s->first_displayed_line +=
4619 view->nlines - 2;
4620 else
4621 s->first_displayed_line =
4622 s->blame.nlines -
4623 (view->nlines - 3);
4624 break;
4625 case KEY_RESIZE:
4626 if (s->selected_line > view->nlines - 2) {
4627 s->selected_line = MIN(s->blame.nlines,
4628 view->nlines - 2);
4630 break;
4631 default:
4632 break;
4634 return thread_err ? thread_err : err;
4637 static const struct got_error *
4638 cmd_blame(int argc, char *argv[])
4640 const struct got_error *error;
4641 struct got_repository *repo = NULL;
4642 struct got_worktree *worktree = NULL;
4643 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4644 char *link_target = NULL;
4645 struct got_object_id *commit_id = NULL;
4646 char *commit_id_str = NULL;
4647 int ch;
4648 struct tog_view *view;
4650 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4651 switch (ch) {
4652 case 'c':
4653 commit_id_str = optarg;
4654 break;
4655 case 'r':
4656 repo_path = realpath(optarg, NULL);
4657 if (repo_path == NULL)
4658 return got_error_from_errno2("realpath",
4659 optarg);
4660 break;
4661 default:
4662 usage_blame();
4663 /* NOTREACHED */
4667 argc -= optind;
4668 argv += optind;
4670 if (argc != 1)
4671 usage_blame();
4673 cwd = getcwd(NULL, 0);
4674 if (cwd == NULL)
4675 return got_error_from_errno("getcwd");
4677 error = got_worktree_open(&worktree, cwd);
4678 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4679 goto done;
4681 if (repo_path == NULL) {
4682 if (worktree)
4683 repo_path =
4684 strdup(got_worktree_get_repo_path(worktree));
4685 else
4686 repo_path = strdup(cwd);
4688 if (repo_path == NULL) {
4689 error = got_error_from_errno("strdup");
4690 goto done;
4693 error = got_repo_open(&repo, repo_path, NULL);
4694 if (error != NULL)
4695 goto done;
4697 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4698 worktree);
4699 if (error)
4700 goto done;
4702 init_curses();
4704 error = apply_unveil(got_repo_get_path(repo), NULL);
4705 if (error)
4706 goto done;
4708 if (commit_id_str == NULL) {
4709 struct got_reference *head_ref;
4710 error = got_ref_open(&head_ref, repo, worktree ?
4711 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4712 if (error != NULL)
4713 goto done;
4714 error = got_ref_resolve(&commit_id, repo, head_ref);
4715 got_ref_close(head_ref);
4716 } else {
4717 error = got_repo_match_object_id(&commit_id, NULL,
4718 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4720 if (error != NULL)
4721 goto done;
4723 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4724 if (view == NULL) {
4725 error = got_error_from_errno("view_open");
4726 goto done;
4729 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4730 commit_id, repo);
4731 if (error)
4732 goto done;
4734 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4735 commit_id, repo);
4736 if (error)
4737 goto done;
4738 if (worktree) {
4739 /* Release work tree lock. */
4740 got_worktree_close(worktree);
4741 worktree = NULL;
4743 error = view_loop(view);
4744 done:
4745 free(repo_path);
4746 free(in_repo_path);
4747 free(link_target);
4748 free(cwd);
4749 free(commit_id);
4750 if (worktree)
4751 got_worktree_close(worktree);
4752 if (repo)
4753 got_repo_close(repo);
4754 return error;
4757 static const struct got_error *
4758 draw_tree_entries(struct tog_view *view, const char *parent_path)
4760 struct tog_tree_view_state *s = &view->state.tree;
4761 const struct got_error *err = NULL;
4762 struct got_tree_entry *te;
4763 wchar_t *wline;
4764 struct tog_color *tc;
4765 int width, n, i, nentries;
4766 int limit = view->nlines;
4768 s->ndisplayed = 0;
4770 werase(view->window);
4772 if (limit == 0)
4773 return NULL;
4775 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4776 if (err)
4777 return err;
4778 if (view_needs_focus_indication(view))
4779 wstandout(view->window);
4780 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4781 if (tc)
4782 wattr_on(view->window,
4783 COLOR_PAIR(tc->colorpair), NULL);
4784 waddwstr(view->window, wline);
4785 if (tc)
4786 wattr_off(view->window,
4787 COLOR_PAIR(tc->colorpair), NULL);
4788 if (view_needs_focus_indication(view))
4789 wstandend(view->window);
4790 free(wline);
4791 wline = NULL;
4792 if (width < view->ncols - 1)
4793 waddch(view->window, '\n');
4794 if (--limit <= 0)
4795 return NULL;
4796 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4797 if (err)
4798 return err;
4799 waddwstr(view->window, wline);
4800 free(wline);
4801 wline = NULL;
4802 if (width < view->ncols - 1)
4803 waddch(view->window, '\n');
4804 if (--limit <= 0)
4805 return NULL;
4806 waddch(view->window, '\n');
4807 if (--limit <= 0)
4808 return NULL;
4810 if (s->first_displayed_entry == NULL) {
4811 te = got_object_tree_get_first_entry(s->tree);
4812 if (s->selected == 0) {
4813 if (view->focussed)
4814 wstandout(view->window);
4815 s->selected_entry = NULL;
4817 waddstr(view->window, " ..\n"); /* parent directory */
4818 if (s->selected == 0 && view->focussed)
4819 wstandend(view->window);
4820 s->ndisplayed++;
4821 if (--limit <= 0)
4822 return NULL;
4823 n = 1;
4824 } else {
4825 n = 0;
4826 te = s->first_displayed_entry;
4829 nentries = got_object_tree_get_nentries(s->tree);
4830 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4831 char *line = NULL, *id_str = NULL, *link_target = NULL;
4832 const char *modestr = "";
4833 mode_t mode;
4835 te = got_object_tree_get_entry(s->tree, i);
4836 mode = got_tree_entry_get_mode(te);
4838 if (s->show_ids) {
4839 err = got_object_id_str(&id_str,
4840 got_tree_entry_get_id(te));
4841 if (err)
4842 return got_error_from_errno(
4843 "got_object_id_str");
4845 if (got_object_tree_entry_is_submodule(te))
4846 modestr = "$";
4847 else if (S_ISLNK(mode)) {
4848 int i;
4850 err = got_tree_entry_get_symlink_target(&link_target,
4851 te, s->repo);
4852 if (err) {
4853 free(id_str);
4854 return err;
4856 for (i = 0; i < strlen(link_target); i++) {
4857 if (!isprint((unsigned char)link_target[i]))
4858 link_target[i] = '?';
4860 modestr = "@";
4862 else if (S_ISDIR(mode))
4863 modestr = "/";
4864 else if (mode & S_IXUSR)
4865 modestr = "*";
4866 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4867 got_tree_entry_get_name(te), modestr,
4868 link_target ? " -> ": "",
4869 link_target ? link_target : "") == -1) {
4870 free(id_str);
4871 free(link_target);
4872 return got_error_from_errno("asprintf");
4874 free(id_str);
4875 free(link_target);
4876 err = format_line(&wline, &width, line, view->ncols, 0);
4877 if (err) {
4878 free(line);
4879 break;
4881 if (n == s->selected) {
4882 if (view->focussed)
4883 wstandout(view->window);
4884 s->selected_entry = te;
4886 tc = match_color(&s->colors, line);
4887 if (tc)
4888 wattr_on(view->window,
4889 COLOR_PAIR(tc->colorpair), NULL);
4890 waddwstr(view->window, wline);
4891 if (tc)
4892 wattr_off(view->window,
4893 COLOR_PAIR(tc->colorpair), NULL);
4894 if (width < view->ncols - 1)
4895 waddch(view->window, '\n');
4896 if (n == s->selected && view->focussed)
4897 wstandend(view->window);
4898 free(line);
4899 free(wline);
4900 wline = NULL;
4901 n++;
4902 s->ndisplayed++;
4903 s->last_displayed_entry = te;
4904 if (--limit <= 0)
4905 break;
4908 return err;
4911 static void
4912 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4914 struct got_tree_entry *te;
4915 int isroot = s->tree == s->root;
4916 int i = 0;
4918 if (s->first_displayed_entry == NULL)
4919 return;
4921 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4922 while (i++ < maxscroll) {
4923 if (te == NULL) {
4924 if (!isroot)
4925 s->first_displayed_entry = NULL;
4926 break;
4928 s->first_displayed_entry = te;
4929 te = got_tree_entry_get_prev(s->tree, te);
4933 static void
4934 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4936 struct got_tree_entry *next, *last;
4937 int n = 0;
4939 if (s->first_displayed_entry)
4940 next = got_tree_entry_get_next(s->tree,
4941 s->first_displayed_entry);
4942 else
4943 next = got_object_tree_get_first_entry(s->tree);
4945 last = s->last_displayed_entry;
4946 while (next && last && n++ < maxscroll) {
4947 last = got_tree_entry_get_next(s->tree, last);
4948 if (last) {
4949 s->first_displayed_entry = next;
4950 next = got_tree_entry_get_next(s->tree, next);
4955 static const struct got_error *
4956 tree_entry_path(char **path, struct tog_parent_trees *parents,
4957 struct got_tree_entry *te)
4959 const struct got_error *err = NULL;
4960 struct tog_parent_tree *pt;
4961 size_t len = 2; /* for leading slash and NUL */
4963 TAILQ_FOREACH(pt, parents, entry)
4964 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4965 + 1 /* slash */;
4966 if (te)
4967 len += strlen(got_tree_entry_get_name(te));
4969 *path = calloc(1, len);
4970 if (path == NULL)
4971 return got_error_from_errno("calloc");
4973 (*path)[0] = '/';
4974 pt = TAILQ_LAST(parents, tog_parent_trees);
4975 while (pt) {
4976 const char *name = got_tree_entry_get_name(pt->selected_entry);
4977 if (strlcat(*path, name, len) >= len) {
4978 err = got_error(GOT_ERR_NO_SPACE);
4979 goto done;
4981 if (strlcat(*path, "/", len) >= len) {
4982 err = got_error(GOT_ERR_NO_SPACE);
4983 goto done;
4985 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4987 if (te) {
4988 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4989 err = got_error(GOT_ERR_NO_SPACE);
4990 goto done;
4993 done:
4994 if (err) {
4995 free(*path);
4996 *path = NULL;
4998 return err;
5001 static const struct got_error *
5002 blame_tree_entry(struct tog_view **new_view, int begin_x,
5003 struct got_tree_entry *te, struct tog_parent_trees *parents,
5004 struct got_object_id *commit_id, struct got_repository *repo)
5006 const struct got_error *err = NULL;
5007 char *path;
5008 struct tog_view *blame_view;
5010 *new_view = NULL;
5012 err = tree_entry_path(&path, parents, te);
5013 if (err)
5014 return err;
5016 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5017 if (blame_view == NULL) {
5018 err = got_error_from_errno("view_open");
5019 goto done;
5022 err = open_blame_view(blame_view, path, commit_id, repo);
5023 if (err) {
5024 if (err->code == GOT_ERR_CANCELLED)
5025 err = NULL;
5026 view_close(blame_view);
5027 } else
5028 *new_view = blame_view;
5029 done:
5030 free(path);
5031 return err;
5034 static const struct got_error *
5035 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5036 struct tog_tree_view_state *s)
5038 struct tog_view *log_view;
5039 const struct got_error *err = NULL;
5040 char *path;
5042 *new_view = NULL;
5044 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5045 if (log_view == NULL)
5046 return got_error_from_errno("view_open");
5048 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5049 if (err)
5050 return err;
5052 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5053 path, 0);
5054 if (err)
5055 view_close(log_view);
5056 else
5057 *new_view = log_view;
5058 free(path);
5059 return err;
5062 static const struct got_error *
5063 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5064 struct got_object_id *commit_id, const char *head_ref_name,
5065 struct got_repository *repo)
5067 const struct got_error *err = NULL;
5068 char *commit_id_str = NULL;
5069 struct tog_tree_view_state *s = &view->state.tree;
5071 TAILQ_INIT(&s->parents);
5073 err = got_object_id_str(&commit_id_str, commit_id);
5074 if (err != NULL)
5075 goto done;
5077 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5078 err = got_error_from_errno("asprintf");
5079 goto done;
5082 s->root = s->tree = root;
5083 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5084 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5085 s->commit_id = got_object_id_dup(commit_id);
5086 if (s->commit_id == NULL) {
5087 err = got_error_from_errno("got_object_id_dup");
5088 goto done;
5090 if (head_ref_name) {
5091 s->head_ref_name = strdup(head_ref_name);
5092 if (s->head_ref_name == NULL) {
5093 err = got_error_from_errno("strdup");
5094 goto done;
5097 s->repo = repo;
5099 SIMPLEQ_INIT(&s->colors);
5101 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5102 err = add_color(&s->colors, "\\$$",
5103 TOG_COLOR_TREE_SUBMODULE,
5104 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5105 if (err)
5106 goto done;
5107 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5108 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5109 if (err) {
5110 free_colors(&s->colors);
5111 goto done;
5113 err = add_color(&s->colors, "/$",
5114 TOG_COLOR_TREE_DIRECTORY,
5115 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5116 if (err) {
5117 free_colors(&s->colors);
5118 goto done;
5121 err = add_color(&s->colors, "\\*$",
5122 TOG_COLOR_TREE_EXECUTABLE,
5123 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5124 if (err) {
5125 free_colors(&s->colors);
5126 goto done;
5129 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5130 get_color_value("TOG_COLOR_COMMIT"));
5131 if (err) {
5132 free_colors(&s->colors);
5133 goto done;
5137 view->show = show_tree_view;
5138 view->input = input_tree_view;
5139 view->close = close_tree_view;
5140 view->search_start = search_start_tree_view;
5141 view->search_next = search_next_tree_view;
5142 done:
5143 free(commit_id_str);
5144 if (err) {
5145 free(s->tree_label);
5146 s->tree_label = NULL;
5148 return err;
5151 static const struct got_error *
5152 close_tree_view(struct tog_view *view)
5154 struct tog_tree_view_state *s = &view->state.tree;
5156 free_colors(&s->colors);
5157 free(s->tree_label);
5158 s->tree_label = NULL;
5159 free(s->commit_id);
5160 s->commit_id = NULL;
5161 free(s->head_ref_name);
5162 s->head_ref_name = NULL;
5163 while (!TAILQ_EMPTY(&s->parents)) {
5164 struct tog_parent_tree *parent;
5165 parent = TAILQ_FIRST(&s->parents);
5166 TAILQ_REMOVE(&s->parents, parent, entry);
5167 free(parent);
5170 if (s->tree != s->root)
5171 got_object_tree_close(s->tree);
5172 got_object_tree_close(s->root);
5173 return NULL;
5176 static const struct got_error *
5177 search_start_tree_view(struct tog_view *view)
5179 struct tog_tree_view_state *s = &view->state.tree;
5181 s->matched_entry = NULL;
5182 return NULL;
5185 static int
5186 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5188 regmatch_t regmatch;
5190 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5191 0) == 0;
5194 static const struct got_error *
5195 search_next_tree_view(struct tog_view *view)
5197 struct tog_tree_view_state *s = &view->state.tree;
5198 struct got_tree_entry *te = NULL;
5200 if (!view->searching) {
5201 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5202 return NULL;
5205 if (s->matched_entry) {
5206 if (view->searching == TOG_SEARCH_FORWARD) {
5207 if (s->selected_entry)
5208 te = got_tree_entry_get_next(s->tree,
5209 s->selected_entry);
5210 else
5211 te = got_object_tree_get_first_entry(s->tree);
5212 } else {
5213 if (s->selected_entry == NULL)
5214 te = got_object_tree_get_last_entry(s->tree);
5215 else
5216 te = got_tree_entry_get_prev(s->tree,
5217 s->selected_entry);
5219 } else {
5220 if (view->searching == TOG_SEARCH_FORWARD)
5221 te = got_object_tree_get_first_entry(s->tree);
5222 else
5223 te = got_object_tree_get_last_entry(s->tree);
5226 while (1) {
5227 if (te == NULL) {
5228 if (s->matched_entry == NULL) {
5229 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5230 return NULL;
5232 if (view->searching == TOG_SEARCH_FORWARD)
5233 te = got_object_tree_get_first_entry(s->tree);
5234 else
5235 te = got_object_tree_get_last_entry(s->tree);
5238 if (match_tree_entry(te, &view->regex)) {
5239 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5240 s->matched_entry = te;
5241 break;
5244 if (view->searching == TOG_SEARCH_FORWARD)
5245 te = got_tree_entry_get_next(s->tree, te);
5246 else
5247 te = got_tree_entry_get_prev(s->tree, te);
5250 if (s->matched_entry) {
5251 s->first_displayed_entry = s->matched_entry;
5252 s->selected = 0;
5255 return NULL;
5258 static const struct got_error *
5259 show_tree_view(struct tog_view *view)
5261 const struct got_error *err = NULL;
5262 struct tog_tree_view_state *s = &view->state.tree;
5263 char *parent_path;
5265 err = tree_entry_path(&parent_path, &s->parents, NULL);
5266 if (err)
5267 return err;
5269 err = draw_tree_entries(view, parent_path);
5270 free(parent_path);
5272 view_vborder(view);
5273 return err;
5276 static const struct got_error *
5277 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5279 const struct got_error *err = NULL;
5280 struct tog_tree_view_state *s = &view->state.tree;
5281 struct tog_view *log_view, *ref_view;
5282 int begin_x = 0;
5284 switch (ch) {
5285 case 'i':
5286 s->show_ids = !s->show_ids;
5287 break;
5288 case 'l':
5289 if (!s->selected_entry)
5290 break;
5291 if (view_is_parent_view(view))
5292 begin_x = view_split_begin_x(view->begin_x);
5293 err = log_selected_tree_entry(&log_view, begin_x, s);
5294 view->focussed = 0;
5295 log_view->focussed = 1;
5296 if (view_is_parent_view(view)) {
5297 err = view_close_child(view);
5298 if (err)
5299 return err;
5300 view_set_child(view, log_view);
5301 view->focus_child = 1;
5302 } else
5303 *new_view = log_view;
5304 break;
5305 case 'r':
5306 if (view_is_parent_view(view))
5307 begin_x = view_split_begin_x(view->begin_x);
5308 ref_view = view_open(view->nlines, view->ncols,
5309 view->begin_y, begin_x, TOG_VIEW_REF);
5310 if (ref_view == NULL)
5311 return got_error_from_errno("view_open");
5312 err = open_ref_view(ref_view, s->repo);
5313 if (err) {
5314 view_close(ref_view);
5315 return err;
5317 view->focussed = 0;
5318 ref_view->focussed = 1;
5319 if (view_is_parent_view(view)) {
5320 err = view_close_child(view);
5321 if (err)
5322 return err;
5323 view_set_child(view, ref_view);
5324 view->focus_child = 1;
5325 } else
5326 *new_view = ref_view;
5327 break;
5328 case 'k':
5329 case KEY_UP:
5330 if (s->selected > 0) {
5331 s->selected--;
5332 break;
5334 tree_scroll_up(s, 1);
5335 break;
5336 case KEY_PPAGE:
5337 case CTRL('b'):
5338 if (s->tree == s->root) {
5339 if (got_object_tree_get_first_entry(s->tree) ==
5340 s->first_displayed_entry)
5341 s->selected = 0;
5342 } else {
5343 if (s->first_displayed_entry == NULL)
5344 s->selected = 0;
5346 tree_scroll_up(s, MAX(0, view->nlines - 3));
5347 break;
5348 case 'j':
5349 case KEY_DOWN:
5350 if (s->selected < s->ndisplayed - 1) {
5351 s->selected++;
5352 break;
5354 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5355 == NULL)
5356 /* can't scroll any further */
5357 break;
5358 tree_scroll_down(s, 1);
5359 break;
5360 case KEY_NPAGE:
5361 case CTRL('f'):
5362 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5363 == NULL) {
5364 /* can't scroll any further; move cursor down */
5365 if (s->selected < s->ndisplayed - 1)
5366 s->selected = s->ndisplayed - 1;
5367 break;
5369 tree_scroll_down(s, view->nlines - 3);
5370 break;
5371 case KEY_ENTER:
5372 case '\r':
5373 case KEY_BACKSPACE:
5374 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5375 struct tog_parent_tree *parent;
5376 /* user selected '..' */
5377 if (s->tree == s->root)
5378 break;
5379 parent = TAILQ_FIRST(&s->parents);
5380 TAILQ_REMOVE(&s->parents, parent,
5381 entry);
5382 got_object_tree_close(s->tree);
5383 s->tree = parent->tree;
5384 s->first_displayed_entry =
5385 parent->first_displayed_entry;
5386 s->selected_entry =
5387 parent->selected_entry;
5388 s->selected = parent->selected;
5389 free(parent);
5390 } else if (S_ISDIR(got_tree_entry_get_mode(
5391 s->selected_entry))) {
5392 struct got_tree_object *subtree;
5393 err = got_object_open_as_tree(&subtree, s->repo,
5394 got_tree_entry_get_id(s->selected_entry));
5395 if (err)
5396 break;
5397 err = tree_view_visit_subtree(s, subtree);
5398 if (err) {
5399 got_object_tree_close(subtree);
5400 break;
5402 } else if (S_ISREG(got_tree_entry_get_mode(
5403 s->selected_entry))) {
5404 struct tog_view *blame_view;
5405 int begin_x = view_is_parent_view(view) ?
5406 view_split_begin_x(view->begin_x) : 0;
5408 err = blame_tree_entry(&blame_view, begin_x,
5409 s->selected_entry, &s->parents,
5410 s->commit_id, s->repo);
5411 if (err)
5412 break;
5413 view->focussed = 0;
5414 blame_view->focussed = 1;
5415 if (view_is_parent_view(view)) {
5416 err = view_close_child(view);
5417 if (err)
5418 return err;
5419 view_set_child(view, blame_view);
5420 view->focus_child = 1;
5421 } else
5422 *new_view = blame_view;
5424 break;
5425 case KEY_RESIZE:
5426 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5427 s->selected = view->nlines - 4;
5428 break;
5429 default:
5430 break;
5433 return err;
5436 __dead static void
5437 usage_tree(void)
5439 endwin();
5440 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5441 getprogname());
5442 exit(1);
5445 static const struct got_error *
5446 cmd_tree(int argc, char *argv[])
5448 const struct got_error *error;
5449 struct got_repository *repo = NULL;
5450 struct got_worktree *worktree = NULL;
5451 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5452 struct got_object_id *commit_id = NULL;
5453 const char *commit_id_arg = NULL;
5454 char *label = NULL;
5455 struct got_commit_object *commit = NULL;
5456 struct got_tree_object *tree = NULL;
5457 struct got_reference *ref = NULL;
5458 const char *head_ref_name = NULL;
5459 int ch;
5460 struct tog_view *view;
5462 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5463 switch (ch) {
5464 case 'c':
5465 commit_id_arg = optarg;
5466 break;
5467 case 'r':
5468 repo_path = realpath(optarg, NULL);
5469 if (repo_path == NULL)
5470 return got_error_from_errno2("realpath",
5471 optarg);
5472 break;
5473 default:
5474 usage_tree();
5475 /* NOTREACHED */
5479 argc -= optind;
5480 argv += optind;
5482 if (argc > 1)
5483 usage_tree();
5485 cwd = getcwd(NULL, 0);
5486 if (cwd == NULL)
5487 return got_error_from_errno("getcwd");
5489 error = got_worktree_open(&worktree, cwd);
5490 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5491 goto done;
5493 if (repo_path == NULL) {
5494 if (worktree)
5495 repo_path =
5496 strdup(got_worktree_get_repo_path(worktree));
5497 else
5498 repo_path = strdup(cwd);
5500 if (repo_path == NULL) {
5501 error = got_error_from_errno("strdup");
5502 goto done;
5505 error = got_repo_open(&repo, repo_path, NULL);
5506 if (error != NULL)
5507 goto done;
5509 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5510 repo, worktree);
5511 if (error)
5512 goto done;
5514 init_curses();
5516 error = apply_unveil(got_repo_get_path(repo), NULL);
5517 if (error)
5518 goto done;
5520 if (commit_id_arg == NULL) {
5521 error = got_repo_match_object_id(&commit_id, &label,
5522 worktree ? got_worktree_get_head_ref_name(worktree) :
5523 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5524 if (error)
5525 goto done;
5526 head_ref_name = label;
5527 } else {
5528 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5529 if (error == NULL)
5530 head_ref_name = got_ref_get_name(ref);
5531 else if (error->code != GOT_ERR_NOT_REF)
5532 goto done;
5533 error = got_repo_match_object_id(&commit_id, NULL,
5534 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5535 if (error)
5536 goto done;
5539 error = got_object_open_as_commit(&commit, repo, commit_id);
5540 if (error)
5541 goto done;
5543 error = got_object_open_as_tree(&tree, repo,
5544 got_object_commit_get_tree_id(commit));
5545 if (error)
5546 goto done;
5548 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5549 if (view == NULL) {
5550 error = got_error_from_errno("view_open");
5551 goto done;
5553 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5554 if (error)
5555 goto done;
5556 if (!got_path_is_root_dir(in_repo_path)) {
5557 error = tree_view_walk_path(&view->state.tree, commit_id,
5558 in_repo_path);
5559 if (error)
5560 goto done;
5563 if (worktree) {
5564 /* Release work tree lock. */
5565 got_worktree_close(worktree);
5566 worktree = NULL;
5568 error = view_loop(view);
5569 done:
5570 free(repo_path);
5571 free(cwd);
5572 free(commit_id);
5573 free(label);
5574 if (ref)
5575 got_ref_close(ref);
5576 if (commit)
5577 got_object_commit_close(commit);
5578 if (tree)
5579 got_object_tree_close(tree);
5580 if (repo)
5581 got_repo_close(repo);
5582 return error;
5585 static const struct got_error *
5586 ref_view_load_refs(struct tog_ref_view_state *s)
5588 const struct got_error *err;
5589 struct got_reflist_entry *sre;
5590 struct tog_reflist_entry *re;
5592 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5593 got_ref_cmp_by_name, NULL);
5594 if (err)
5595 return err;
5597 s->nrefs = 0;
5598 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5599 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5600 continue;
5602 re = malloc(sizeof(*re));
5603 if (re == NULL)
5604 return got_error_from_errno("malloc");
5606 re->ref = sre->ref;
5607 re->idx = s->nrefs++;
5608 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5611 return NULL;
5614 void
5615 ref_view_free_refs(struct tog_ref_view_state *s)
5617 struct tog_reflist_entry *re;
5619 while (!TAILQ_EMPTY(&s->refs)) {
5620 re = TAILQ_FIRST(&s->refs);
5621 TAILQ_REMOVE(&s->refs, re, entry);
5622 free(re);
5624 got_ref_list_free(&s->simplerefs);
5627 static const struct got_error *
5628 open_ref_view(struct tog_view *view, struct got_repository *repo)
5630 const struct got_error *err = NULL;
5631 struct tog_ref_view_state *s = &view->state.ref;
5633 s->selected_entry = 0;
5634 s->repo = repo;
5636 SIMPLEQ_INIT(&s->simplerefs);
5637 TAILQ_INIT(&s->refs);
5638 SIMPLEQ_INIT(&s->colors);
5640 err = ref_view_load_refs(s);
5641 if (err)
5642 return err;
5644 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5646 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5647 err = add_color(&s->colors, "^refs/heads/",
5648 TOG_COLOR_REFS_HEADS,
5649 get_color_value("TOG_COLOR_REFS_HEADS"));
5650 if (err)
5651 goto done;
5653 err = add_color(&s->colors, "^refs/tags/",
5654 TOG_COLOR_REFS_TAGS,
5655 get_color_value("TOG_COLOR_REFS_TAGS"));
5656 if (err)
5657 goto done;
5659 err = add_color(&s->colors, "^refs/remotes/",
5660 TOG_COLOR_REFS_REMOTES,
5661 get_color_value("TOG_COLOR_REFS_REMOTES"));
5662 if (err)
5663 goto done;
5666 view->show = show_ref_view;
5667 view->input = input_ref_view;
5668 view->close = close_ref_view;
5669 view->search_start = search_start_ref_view;
5670 view->search_next = search_next_ref_view;
5671 done:
5672 if (err)
5673 free_colors(&s->colors);
5674 return err;
5677 static const struct got_error *
5678 close_ref_view(struct tog_view *view)
5680 struct tog_ref_view_state *s = &view->state.ref;
5682 ref_view_free_refs(s);
5683 free_colors(&s->colors);
5685 return NULL;
5688 static const struct got_error *
5689 resolve_reflist_entry(struct got_object_id **commit_id,
5690 struct tog_reflist_entry *re, struct got_repository *repo)
5692 const struct got_error *err = NULL;
5693 struct got_object_id *obj_id;
5694 struct got_tag_object *tag = NULL;
5695 int obj_type;
5697 *commit_id = NULL;
5699 err = got_ref_resolve(&obj_id, repo, re->ref);
5700 if (err)
5701 return err;
5703 err = got_object_get_type(&obj_type, repo, obj_id);
5704 if (err)
5705 goto done;
5707 switch (obj_type) {
5708 case GOT_OBJ_TYPE_COMMIT:
5709 *commit_id = obj_id;
5710 break;
5711 case GOT_OBJ_TYPE_TAG:
5712 err = got_object_open_as_tag(&tag, repo, obj_id);
5713 if (err)
5714 goto done;
5715 free(obj_id);
5716 err = got_object_get_type(&obj_type, repo,
5717 got_object_tag_get_object_id(tag));
5718 if (err)
5719 goto done;
5720 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5721 err = got_error(GOT_ERR_OBJ_TYPE);
5722 goto done;
5724 *commit_id = got_object_id_dup(
5725 got_object_tag_get_object_id(tag));
5726 if (*commit_id == NULL) {
5727 err = got_error_from_errno("got_object_id_dup");
5728 goto done;
5730 break;
5731 default:
5732 err = got_error(GOT_ERR_OBJ_TYPE);
5733 break;
5736 done:
5737 if (tag)
5738 got_object_tag_close(tag);
5739 if (err) {
5740 free(*commit_id);
5741 *commit_id = NULL;
5743 return err;
5746 static const struct got_error *
5747 log_ref_entry(struct tog_view **new_view, int begin_x,
5748 struct tog_reflist_entry *re, struct got_repository *repo)
5750 struct tog_view *log_view;
5751 const struct got_error *err = NULL;
5752 struct got_object_id *commit_id = NULL;
5754 *new_view = NULL;
5756 err = resolve_reflist_entry(&commit_id, re, repo);
5757 if (err) {
5758 if (err->code != GOT_ERR_OBJ_TYPE)
5759 return err;
5760 else
5761 return NULL;
5764 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5765 if (log_view == NULL) {
5766 err = got_error_from_errno("view_open");
5767 goto done;
5770 err = open_log_view(log_view, commit_id, repo,
5771 got_ref_get_name(re->ref), "", 0);
5772 done:
5773 if (err)
5774 view_close(log_view);
5775 else
5776 *new_view = log_view;
5777 free(commit_id);
5778 return err;
5781 static void
5782 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5784 struct tog_reflist_entry *re;
5785 int i = 0;
5787 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5788 return;
5790 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5791 while (i++ < maxscroll) {
5792 if (re == NULL)
5793 break;
5794 s->first_displayed_entry = re;
5795 re = TAILQ_PREV(re, tog_reflist_head, entry);
5799 static void
5800 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5802 struct tog_reflist_entry *next, *last;
5803 int n = 0;
5805 if (s->first_displayed_entry)
5806 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5807 else
5808 next = TAILQ_FIRST(&s->refs);
5810 last = s->last_displayed_entry;
5811 while (next && last && n++ < maxscroll) {
5812 last = TAILQ_NEXT(last, entry);
5813 if (last) {
5814 s->first_displayed_entry = next;
5815 next = TAILQ_NEXT(next, entry);
5820 static const struct got_error *
5821 search_start_ref_view(struct tog_view *view)
5823 struct tog_ref_view_state *s = &view->state.ref;
5825 s->matched_entry = NULL;
5826 return NULL;
5829 static int
5830 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5832 regmatch_t regmatch;
5834 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5835 0) == 0;
5838 static const struct got_error *
5839 search_next_ref_view(struct tog_view *view)
5841 struct tog_ref_view_state *s = &view->state.ref;
5842 struct tog_reflist_entry *re = NULL;
5844 if (!view->searching) {
5845 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5846 return NULL;
5849 if (s->matched_entry) {
5850 if (view->searching == TOG_SEARCH_FORWARD) {
5851 if (s->selected_entry)
5852 re = TAILQ_NEXT(s->selected_entry, entry);
5853 else
5854 re = TAILQ_PREV(s->selected_entry,
5855 tog_reflist_head, entry);
5856 } else {
5857 if (s->selected_entry == NULL)
5858 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5859 else
5860 re = TAILQ_PREV(s->selected_entry,
5861 tog_reflist_head, entry);
5863 } else {
5864 if (view->searching == TOG_SEARCH_FORWARD)
5865 re = TAILQ_FIRST(&s->refs);
5866 else
5867 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5870 while (1) {
5871 if (re == NULL) {
5872 if (s->matched_entry == NULL) {
5873 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5874 return NULL;
5876 if (view->searching == TOG_SEARCH_FORWARD)
5877 re = TAILQ_FIRST(&s->refs);
5878 else
5879 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5882 if (match_reflist_entry(re, &view->regex)) {
5883 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5884 s->matched_entry = re;
5885 break;
5888 if (view->searching == TOG_SEARCH_FORWARD)
5889 re = TAILQ_NEXT(re, entry);
5890 else
5891 re = TAILQ_PREV(re, tog_reflist_head, entry);
5894 if (s->matched_entry) {
5895 s->first_displayed_entry = s->matched_entry;
5896 s->selected = 0;
5899 return NULL;
5902 static const struct got_error *
5903 show_ref_view(struct tog_view *view)
5905 const struct got_error *err = NULL;
5906 struct tog_ref_view_state *s = &view->state.ref;
5907 struct tog_reflist_entry *re;
5908 char *line = NULL;
5909 wchar_t *wline;
5910 struct tog_color *tc;
5911 int width, n;
5912 int limit = view->nlines;
5914 werase(view->window);
5916 s->ndisplayed = 0;
5918 if (limit == 0)
5919 return NULL;
5921 re = s->first_displayed_entry;
5923 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5924 s->nrefs) == -1)
5925 return got_error_from_errno("asprintf");
5927 err = format_line(&wline, &width, line, view->ncols, 0);
5928 if (err) {
5929 free(line);
5930 return err;
5932 if (view_needs_focus_indication(view))
5933 wstandout(view->window);
5934 waddwstr(view->window, wline);
5935 if (view_needs_focus_indication(view))
5936 wstandend(view->window);
5937 free(wline);
5938 wline = NULL;
5939 free(line);
5940 line = NULL;
5941 if (width < view->ncols - 1)
5942 waddch(view->window, '\n');
5943 if (--limit <= 0)
5944 return NULL;
5946 n = 0;
5947 while (re && limit > 0) {
5948 char *line = NULL;
5950 if (got_ref_is_symbolic(re->ref)) {
5951 if (asprintf(&line, "%s -> %s",
5952 got_ref_get_name(re->ref),
5953 got_ref_get_symref_target(re->ref)) == -1)
5954 return got_error_from_errno("asprintf");
5955 } else if (s->show_ids) {
5956 struct got_object_id *id;
5957 char *id_str;
5958 err = got_ref_resolve(&id, s->repo, re->ref);
5959 if (err)
5960 return err;
5961 err = got_object_id_str(&id_str, id);
5962 if (err) {
5963 free(id);
5964 return err;
5966 if (asprintf(&line, "%s: %s",
5967 got_ref_get_name(re->ref), id_str) == -1) {
5968 err = got_error_from_errno("asprintf");
5969 free(id);
5970 free(id_str);
5971 return err;
5973 free(id);
5974 free(id_str);
5975 } else {
5976 line = strdup(got_ref_get_name(re->ref));
5977 if (line == NULL)
5978 return got_error_from_errno("strdup");
5981 err = format_line(&wline, &width, line, view->ncols, 0);
5982 if (err) {
5983 free(line);
5984 return err;
5986 if (n == s->selected) {
5987 if (view->focussed)
5988 wstandout(view->window);
5989 s->selected_entry = re;
5991 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5992 if (tc)
5993 wattr_on(view->window,
5994 COLOR_PAIR(tc->colorpair), NULL);
5995 waddwstr(view->window, wline);
5996 if (tc)
5997 wattr_off(view->window,
5998 COLOR_PAIR(tc->colorpair), NULL);
5999 if (width < view->ncols - 1)
6000 waddch(view->window, '\n');
6001 if (n == s->selected && view->focussed)
6002 wstandend(view->window);
6003 free(line);
6004 free(wline);
6005 wline = NULL;
6006 n++;
6007 s->ndisplayed++;
6008 s->last_displayed_entry = re;
6010 limit--;
6011 re = TAILQ_NEXT(re, entry);
6014 view_vborder(view);
6015 return err;
6018 static const struct got_error *
6019 browse_ref_tree(struct tog_view **new_view, int begin_x,
6020 struct tog_reflist_entry *re, struct got_repository *repo)
6022 const struct got_error *err = NULL;
6023 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6024 struct got_tree_object *tree = NULL;
6025 struct tog_view *tree_view;
6027 *new_view = NULL;
6029 err = resolve_reflist_entry(&commit_id, re, repo);
6030 if (err) {
6031 if (err->code != GOT_ERR_OBJ_TYPE)
6032 return err;
6033 else
6034 return NULL;
6037 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6038 if (err)
6039 goto done;
6041 err = got_object_open_as_tree(&tree, repo, tree_id);
6042 if (err)
6043 goto done;
6045 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6046 if (tree_view == NULL) {
6047 err = got_error_from_errno("view_open");
6048 goto done;
6051 err = open_tree_view(tree_view, tree, commit_id,
6052 got_ref_get_name(re->ref), repo);
6053 if (err)
6054 goto done;
6056 *new_view = tree_view;
6057 done:
6058 free(commit_id);
6059 free(tree_id);
6060 if (err) {
6061 if (tree)
6062 got_object_tree_close(tree);
6064 return err;
6066 static const struct got_error *
6067 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6069 const struct got_error *err = NULL;
6070 struct tog_ref_view_state *s = &view->state.ref;
6071 struct tog_view *log_view, *tree_view;
6072 int begin_x = 0;
6074 switch (ch) {
6075 case 'i':
6076 s->show_ids = !s->show_ids;
6077 break;
6078 case KEY_ENTER:
6079 case '\r':
6080 if (!s->selected_entry)
6081 break;
6082 if (view_is_parent_view(view))
6083 begin_x = view_split_begin_x(view->begin_x);
6084 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6085 s->repo);
6086 view->focussed = 0;
6087 log_view->focussed = 1;
6088 if (view_is_parent_view(view)) {
6089 err = view_close_child(view);
6090 if (err)
6091 return err;
6092 view_set_child(view, log_view);
6093 view->focus_child = 1;
6094 } else
6095 *new_view = log_view;
6096 break;
6097 case 't':
6098 if (!s->selected_entry)
6099 break;
6100 if (view_is_parent_view(view))
6101 begin_x = view_split_begin_x(view->begin_x);
6102 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6103 s->repo);
6104 if (err || tree_view == NULL)
6105 break;
6106 view->focussed = 0;
6107 tree_view->focussed = 1;
6108 if (view_is_parent_view(view)) {
6109 err = view_close_child(view);
6110 if (err)
6111 return err;
6112 view_set_child(view, tree_view);
6113 view->focus_child = 1;
6114 } else
6115 *new_view = tree_view;
6116 break;
6117 case 'k':
6118 case KEY_UP:
6119 if (s->selected > 0) {
6120 s->selected--;
6121 break;
6123 ref_scroll_up(s, 1);
6124 break;
6125 case KEY_PPAGE:
6126 case CTRL('b'):
6127 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6128 s->selected = 0;
6129 ref_scroll_up(s, MAX(0, view->nlines - 1));
6130 break;
6131 case 'j':
6132 case KEY_DOWN:
6133 if (s->selected < s->ndisplayed - 1) {
6134 s->selected++;
6135 break;
6137 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6138 /* can't scroll any further */
6139 break;
6140 ref_scroll_down(s, 1);
6141 break;
6142 case KEY_NPAGE:
6143 case CTRL('f'):
6144 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6145 /* can't scroll any further; move cursor down */
6146 if (s->selected < s->ndisplayed - 1)
6147 s->selected = s->ndisplayed - 1;
6148 break;
6150 ref_scroll_down(s, view->nlines - 1);
6151 break;
6152 case CTRL('l'):
6153 ref_view_free_refs(s);
6154 err = ref_view_load_refs(s);
6155 break;
6156 case KEY_RESIZE:
6157 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6158 s->selected = view->nlines - 2;
6159 break;
6160 default:
6161 break;
6164 return err;
6167 __dead static void
6168 usage_ref(void)
6170 endwin();
6171 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6172 getprogname());
6173 exit(1);
6176 static const struct got_error *
6177 cmd_ref(int argc, char *argv[])
6179 const struct got_error *error;
6180 struct got_repository *repo = NULL;
6181 struct got_worktree *worktree = NULL;
6182 char *cwd = NULL, *repo_path = NULL;
6183 int ch;
6184 struct tog_view *view;
6186 while ((ch = getopt(argc, argv, "r:")) != -1) {
6187 switch (ch) {
6188 case 'r':
6189 repo_path = realpath(optarg, NULL);
6190 if (repo_path == NULL)
6191 return got_error_from_errno2("realpath",
6192 optarg);
6193 break;
6194 default:
6195 usage_ref();
6196 /* NOTREACHED */
6200 argc -= optind;
6201 argv += optind;
6203 if (argc > 1)
6204 usage_ref();
6206 cwd = getcwd(NULL, 0);
6207 if (cwd == NULL)
6208 return got_error_from_errno("getcwd");
6210 error = got_worktree_open(&worktree, cwd);
6211 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6212 goto done;
6214 if (repo_path == NULL) {
6215 if (worktree)
6216 repo_path =
6217 strdup(got_worktree_get_repo_path(worktree));
6218 else
6219 repo_path = strdup(cwd);
6221 if (repo_path == NULL) {
6222 error = got_error_from_errno("strdup");
6223 goto done;
6226 error = got_repo_open(&repo, repo_path, NULL);
6227 if (error != NULL)
6228 goto done;
6230 init_curses();
6232 error = apply_unveil(got_repo_get_path(repo), NULL);
6233 if (error)
6234 goto done;
6236 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6237 if (view == NULL) {
6238 error = got_error_from_errno("view_open");
6239 goto done;
6242 error = open_ref_view(view, repo);
6243 if (error)
6244 goto done;
6246 if (worktree) {
6247 /* Release work tree lock. */
6248 got_worktree_close(worktree);
6249 worktree = NULL;
6251 error = view_loop(view);
6252 done:
6253 free(repo_path);
6254 free(cwd);
6255 if (repo)
6256 got_repo_close(repo);
6257 return error;
6260 static void
6261 list_commands(FILE *fp)
6263 int i;
6265 fprintf(fp, "commands:");
6266 for (i = 0; i < nitems(tog_commands); i++) {
6267 struct tog_cmd *cmd = &tog_commands[i];
6268 fprintf(fp, " %s", cmd->name);
6270 fputc('\n', fp);
6273 __dead static void
6274 usage(int hflag, int status)
6276 FILE *fp = (status == 0) ? stdout : stderr;
6278 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6279 getprogname());
6280 if (hflag) {
6281 fprintf(fp, "lazy usage: %s path\n", getprogname());
6282 list_commands(fp);
6284 exit(status);
6287 static char **
6288 make_argv(int argc, ...)
6290 va_list ap;
6291 char **argv;
6292 int i;
6294 va_start(ap, argc);
6296 argv = calloc(argc, sizeof(char *));
6297 if (argv == NULL)
6298 err(1, "calloc");
6299 for (i = 0; i < argc; i++) {
6300 argv[i] = strdup(va_arg(ap, char *));
6301 if (argv[i] == NULL)
6302 err(1, "strdup");
6305 va_end(ap);
6306 return argv;
6310 * Try to convert 'tog path' into a 'tog log path' command.
6311 * The user could simply have mistyped the command rather than knowingly
6312 * provided a path. So check whether argv[0] can in fact be resolved
6313 * to a path in the HEAD commit and print a special error if not.
6314 * This hack is for mpi@ <3
6316 static const struct got_error *
6317 tog_log_with_path(int argc, char *argv[])
6319 const struct got_error *error = NULL;
6320 struct tog_cmd *cmd = NULL;
6321 struct got_repository *repo = NULL;
6322 struct got_worktree *worktree = NULL;
6323 struct got_object_id *commit_id = NULL, *id = NULL;
6324 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6325 char *commit_id_str = NULL, **cmd_argv = NULL;
6327 cwd = getcwd(NULL, 0);
6328 if (cwd == NULL)
6329 return got_error_from_errno("getcwd");
6331 error = got_worktree_open(&worktree, cwd);
6332 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6333 goto done;
6335 if (worktree)
6336 repo_path = strdup(got_worktree_get_repo_path(worktree));
6337 else
6338 repo_path = strdup(cwd);
6339 if (repo_path == NULL) {
6340 error = got_error_from_errno("strdup");
6341 goto done;
6344 error = got_repo_open(&repo, repo_path, NULL);
6345 if (error != NULL)
6346 goto done;
6348 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6349 repo, worktree);
6350 if (error)
6351 goto done;
6353 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6354 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6355 GOT_OBJ_TYPE_COMMIT, 1, repo);
6356 if (error)
6357 goto done;
6359 if (worktree) {
6360 got_worktree_close(worktree);
6361 worktree = NULL;
6364 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6365 if (error) {
6366 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6367 goto done;
6368 fprintf(stderr, "%s: '%s' is no known command or path\n",
6369 getprogname(), argv[0]);
6370 usage(1, 1);
6371 /* not reached */
6374 got_repo_close(repo);
6375 repo = NULL;
6377 error = got_object_id_str(&commit_id_str, commit_id);
6378 if (error)
6379 goto done;
6381 cmd = &tog_commands[0]; /* log */
6382 argc = 4;
6383 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6384 error = cmd->cmd_main(argc, cmd_argv);
6385 done:
6386 if (repo)
6387 got_repo_close(repo);
6388 if (worktree)
6389 got_worktree_close(worktree);
6390 free(id);
6391 free(commit_id_str);
6392 free(commit_id);
6393 free(cwd);
6394 free(repo_path);
6395 free(in_repo_path);
6396 if (cmd_argv) {
6397 int i;
6398 for (i = 0; i < argc; i++)
6399 free(cmd_argv[i]);
6400 free(cmd_argv);
6402 return error;
6405 int
6406 main(int argc, char *argv[])
6408 const struct got_error *error = NULL;
6409 struct tog_cmd *cmd = NULL;
6410 int ch, hflag = 0, Vflag = 0;
6411 char **cmd_argv = NULL;
6412 static struct option longopts[] = {
6413 { "version", no_argument, NULL, 'V' },
6414 { NULL, 0, NULL, 0}
6417 setlocale(LC_CTYPE, "");
6419 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6420 switch (ch) {
6421 case 'h':
6422 hflag = 1;
6423 break;
6424 case 'V':
6425 Vflag = 1;
6426 break;
6427 default:
6428 usage(hflag, 1);
6429 /* NOTREACHED */
6433 argc -= optind;
6434 argv += optind;
6435 optind = 1;
6436 optreset = 1;
6438 if (Vflag) {
6439 got_version_print_str();
6440 return 0;
6443 #ifndef PROFILE
6444 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6445 NULL) == -1)
6446 err(1, "pledge");
6447 #endif
6449 if (argc == 0) {
6450 if (hflag)
6451 usage(hflag, 0);
6452 /* Build an argument vector which runs a default command. */
6453 cmd = &tog_commands[0];
6454 argc = 1;
6455 cmd_argv = make_argv(argc, cmd->name);
6456 } else {
6457 int i;
6459 /* Did the user specify a command? */
6460 for (i = 0; i < nitems(tog_commands); i++) {
6461 if (strncmp(tog_commands[i].name, argv[0],
6462 strlen(argv[0])) == 0) {
6463 cmd = &tog_commands[i];
6464 break;
6469 if (cmd == NULL) {
6470 if (argc != 1)
6471 usage(0, 1);
6472 /* No command specified; try log with a path */
6473 error = tog_log_with_path(argc, argv);
6474 } else {
6475 if (hflag)
6476 cmd->cmd_usage();
6477 else
6478 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6481 endwin();
6482 putchar('\n');
6483 if (cmd_argv) {
6484 int i;
6485 for (i = 0; i < argc; i++)
6486 free(cmd_argv[i]);
6487 free(cmd_argv);
6490 if (error && error->code != GOT_ERR_CANCELLED)
6491 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6492 return 0;