Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED
24 #include <curses.h>
25 #undef _XOPEN_SOURCE_EXTENDED
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_diff.h"
49 #include "got_opentemp.h"
50 #include "got_utf8.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_path.h"
56 #include "got_worktree.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #ifndef MAX
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #endif
66 #define CTRL(x) ((x) & 0x1f)
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 struct tog_cmd {
73 const char *name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 };
78 __dead static void usage(int, int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_ref(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
89 static const struct got_error* cmd_ref(int, char *[]);
91 static struct tog_cmd tog_commands[] = {
92 { "log", cmd_log, usage_log },
93 { "diff", cmd_diff, usage_diff },
94 { "blame", cmd_blame, usage_blame },
95 { "tree", cmd_tree, usage_tree },
96 { "ref", cmd_ref, usage_ref },
97 };
99 enum tog_view_type {
100 TOG_VIEW_DIFF,
101 TOG_VIEW_LOG,
102 TOG_VIEW_BLAME,
103 TOG_VIEW_TREE,
104 TOG_VIEW_REF,
105 };
107 #define TOG_EOF_STRING "(END)"
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
113 int idx;
114 };
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
117 int ncommits;
118 struct commit_queue_head head;
119 };
121 struct tog_color {
122 SIMPLEQ_ENTRY(tog_color) entry;
123 regex_t regex;
124 short colorpair;
125 };
126 SIMPLEQ_HEAD(tog_colors, tog_color);
128 static const struct got_error *
129 add_color(struct tog_colors *colors, const char *pattern,
130 int idx, short color)
132 const struct got_error *err = NULL;
133 struct tog_color *tc;
134 int regerr = 0;
136 if (idx < 1 || idx > COLOR_PAIRS - 1)
137 return NULL;
139 init_pair(idx, color, -1);
141 tc = calloc(1, sizeof(*tc));
142 if (tc == NULL)
143 return got_error_from_errno("calloc");
144 regerr = regcomp(&tc->regex, pattern,
145 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
146 if (regerr) {
147 static char regerr_msg[512];
148 static char err_msg[512];
149 regerror(regerr, &tc->regex, regerr_msg,
150 sizeof(regerr_msg));
151 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
152 regerr_msg);
153 err = got_error_msg(GOT_ERR_REGEX, err_msg);
154 free(tc);
155 return err;
157 tc->colorpair = idx;
158 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
159 return NULL;
162 static void
163 free_colors(struct tog_colors *colors)
165 struct tog_color *tc;
167 while (!SIMPLEQ_EMPTY(colors)) {
168 tc = SIMPLEQ_FIRST(colors);
169 SIMPLEQ_REMOVE_HEAD(colors, entry);
170 regfree(&tc->regex);
171 free(tc);
175 struct tog_color *
176 get_color(struct tog_colors *colors, int colorpair)
178 struct tog_color *tc = NULL;
180 SIMPLEQ_FOREACH(tc, colors, entry) {
181 if (tc->colorpair == colorpair)
182 return tc;
185 return NULL;
188 static int
189 default_color_value(const char *envvar)
191 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
192 return COLOR_MAGENTA;
193 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
194 return COLOR_CYAN;
195 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
196 return COLOR_YELLOW;
197 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
198 return COLOR_GREEN;
199 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
200 return COLOR_MAGENTA;
201 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
202 return COLOR_MAGENTA;
203 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
204 return COLOR_CYAN;
205 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
206 return COLOR_GREEN;
207 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
208 return COLOR_GREEN;
209 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
210 return COLOR_CYAN;
211 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
212 return COLOR_YELLOW;
213 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
214 return COLOR_GREEN;
215 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
216 return COLOR_MAGENTA;
217 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
218 return COLOR_YELLOW;
220 return -1;
223 static int
224 get_color_value(const char *envvar)
226 const char *val = getenv(envvar);
228 if (val == NULL)
229 return default_color_value(envvar);
231 if (strcasecmp(val, "black") == 0)
232 return COLOR_BLACK;
233 if (strcasecmp(val, "red") == 0)
234 return COLOR_RED;
235 if (strcasecmp(val, "green") == 0)
236 return COLOR_GREEN;
237 if (strcasecmp(val, "yellow") == 0)
238 return COLOR_YELLOW;
239 if (strcasecmp(val, "blue") == 0)
240 return COLOR_BLUE;
241 if (strcasecmp(val, "magenta") == 0)
242 return COLOR_MAGENTA;
243 if (strcasecmp(val, "cyan") == 0)
244 return COLOR_CYAN;
245 if (strcasecmp(val, "white") == 0)
246 return COLOR_WHITE;
247 if (strcasecmp(val, "default") == 0)
248 return -1;
250 return default_color_value(envvar);
254 struct tog_diff_view_state {
255 struct got_object_id *id1, *id2;
256 const char *label1, *label2;
257 FILE *f;
258 int first_displayed_line;
259 int last_displayed_line;
260 int eof;
261 int diff_context;
262 int ignore_whitespace;
263 int force_text_diff;
264 struct got_repository *repo;
265 struct got_reflist_head refs;
266 struct tog_colors colors;
267 size_t nlines;
268 off_t *line_offsets;
269 int matched_line;
270 int selected_line;
272 /* passed from log view; may be NULL */
273 struct tog_view *log_view;
274 };
276 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
278 struct tog_log_thread_args {
279 pthread_cond_t need_commits;
280 pthread_cond_t commit_loaded;
281 int commits_needed;
282 struct got_commit_graph *graph;
283 struct commit_queue *commits;
284 const char *in_repo_path;
285 struct got_object_id *start_id;
286 struct got_repository *repo;
287 int log_complete;
288 sig_atomic_t *quit;
289 struct commit_queue_entry **first_displayed_entry;
290 struct commit_queue_entry **selected_entry;
291 int *searching;
292 int *search_next_done;
293 regex_t *regex;
294 };
296 struct tog_log_view_state {
297 struct commit_queue commits;
298 struct commit_queue_entry *first_displayed_entry;
299 struct commit_queue_entry *last_displayed_entry;
300 struct commit_queue_entry *selected_entry;
301 int selected;
302 char *in_repo_path;
303 char *head_ref_name;
304 int log_branches;
305 struct got_repository *repo;
306 struct got_reflist_head refs;
307 struct got_object_id *start_id;
308 sig_atomic_t quit;
309 pthread_t thread;
310 struct tog_log_thread_args thread_args;
311 struct commit_queue_entry *matched_entry;
312 struct commit_queue_entry *search_entry;
313 struct tog_colors colors;
314 };
316 #define TOG_COLOR_DIFF_MINUS 1
317 #define TOG_COLOR_DIFF_PLUS 2
318 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
319 #define TOG_COLOR_DIFF_META 4
320 #define TOG_COLOR_TREE_SUBMODULE 5
321 #define TOG_COLOR_TREE_SYMLINK 6
322 #define TOG_COLOR_TREE_DIRECTORY 7
323 #define TOG_COLOR_TREE_EXECUTABLE 8
324 #define TOG_COLOR_COMMIT 9
325 #define TOG_COLOR_AUTHOR 10
326 #define TOG_COLOR_DATE 11
327 #define TOG_COLOR_REFS_HEADS 12
328 #define TOG_COLOR_REFS_TAGS 13
329 #define TOG_COLOR_REFS_REMOTES 14
331 struct tog_blame_cb_args {
332 struct tog_blame_line *lines; /* one per line */
333 int nlines;
335 struct tog_view *view;
336 struct got_object_id *commit_id;
337 int *quit;
338 };
340 struct tog_blame_thread_args {
341 const char *path;
342 struct got_repository *repo;
343 struct tog_blame_cb_args *cb_args;
344 int *complete;
345 got_cancel_cb cancel_cb;
346 void *cancel_arg;
347 };
349 struct tog_blame {
350 FILE *f;
351 off_t filesize;
352 struct tog_blame_line *lines;
353 int nlines;
354 off_t *line_offsets;
355 pthread_t thread;
356 struct tog_blame_thread_args thread_args;
357 struct tog_blame_cb_args cb_args;
358 const char *path;
359 };
361 struct tog_blame_view_state {
362 int first_displayed_line;
363 int last_displayed_line;
364 int selected_line;
365 int blame_complete;
366 int eof;
367 int done;
368 struct got_object_id_queue blamed_commits;
369 struct got_object_qid *blamed_commit;
370 char *path;
371 struct got_repository *repo;
372 struct got_object_id *commit_id;
373 struct tog_blame blame;
374 int matched_line;
375 struct tog_colors colors;
376 };
378 struct tog_parent_tree {
379 TAILQ_ENTRY(tog_parent_tree) entry;
380 struct got_tree_object *tree;
381 struct got_tree_entry *first_displayed_entry;
382 struct got_tree_entry *selected_entry;
383 int selected;
384 };
386 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
388 struct tog_tree_view_state {
389 char *tree_label;
390 struct got_tree_object *root;
391 struct got_tree_object *tree;
392 struct got_tree_entry *first_displayed_entry;
393 struct got_tree_entry *last_displayed_entry;
394 struct got_tree_entry *selected_entry;
395 int ndisplayed, selected, show_ids;
396 struct tog_parent_trees parents;
397 struct got_object_id *commit_id;
398 char *head_ref_name;
399 struct got_repository *repo;
400 struct got_tree_entry *matched_entry;
401 struct tog_colors colors;
402 };
404 struct tog_reflist_entry {
405 TAILQ_ENTRY(tog_reflist_entry) entry;
406 struct got_reference *ref;
407 int idx;
408 };
410 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 struct tog_ref_view_state {
413 struct got_reflist_head simplerefs; /* SIMPLEQ */
414 struct tog_reflist_head refs; /* TAILQ */
415 struct tog_reflist_entry *first_displayed_entry;
416 struct tog_reflist_entry *last_displayed_entry;
417 struct tog_reflist_entry *selected_entry;
418 int nrefs, ndisplayed, selected, show_ids;
419 struct got_repository *repo;
420 struct tog_reflist_entry *matched_entry;
421 struct tog_colors colors;
422 };
424 /*
425 * We implement two types of views: parent views and child views.
427 * The 'Tab' key switches focus between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
440 */
441 TAILQ_HEAD(tog_view_list_head, tog_view);
443 struct tog_view {
444 TAILQ_ENTRY(tog_view) entry;
445 WINDOW *window;
446 PANEL *panel;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
449 int focussed; /* Only set on one parent or child view at a time. */
450 int dying;
451 struct tog_view *parent;
452 struct tog_view *child;
454 /*
455 * This flag is initially set on parent views when a new child view
456 * is created. It gets toggled when the 'Tab' key switches focus
457 * between parent and child.
458 * The flag indicates whether focus should be passed on to our child
459 * view if this parent view gets picked for focus after another parent
460 * view was closed. This prevents child views from losing focus in such
461 * situations.
462 */
463 int focus_child;
465 /* type-specific state */
466 enum tog_view_type type;
467 union {
468 struct tog_diff_view_state diff;
469 struct tog_log_view_state log;
470 struct tog_blame_view_state blame;
471 struct tog_tree_view_state tree;
472 struct tog_ref_view_state ref;
473 } state;
475 const struct got_error *(*show)(struct tog_view *);
476 const struct got_error *(*input)(struct tog_view **,
477 struct tog_view *, int);
478 const struct got_error *(*close)(struct tog_view *);
480 const struct got_error *(*search_start)(struct tog_view *);
481 const struct got_error *(*search_next)(struct tog_view *);
482 int searching;
483 #define TOG_SEARCH_FORWARD 1
484 #define TOG_SEARCH_BACKWARD 2
485 int search_next_done;
486 #define TOG_SEARCH_HAVE_MORE 1
487 #define TOG_SEARCH_NO_MORE 2
488 #define TOG_SEARCH_HAVE_NONE 3
489 regex_t regex;
490 regmatch_t regmatch;
491 };
493 static const struct got_error *open_diff_view(struct tog_view *,
494 struct got_object_id *, struct got_object_id *,
495 const char *, const char *, int, int, int, struct tog_view *,
496 struct got_repository *);
497 static const struct got_error *show_diff_view(struct tog_view *);
498 static const struct got_error *input_diff_view(struct tog_view **,
499 struct tog_view *, int);
500 static const struct got_error* close_diff_view(struct tog_view *);
501 static const struct got_error *search_start_diff_view(struct tog_view *);
502 static const struct got_error *search_next_diff_view(struct tog_view *);
504 static const struct got_error *open_log_view(struct tog_view *,
505 struct got_object_id *, struct got_repository *,
506 const char *, const char *, int);
507 static const struct got_error * show_log_view(struct tog_view *);
508 static const struct got_error *input_log_view(struct tog_view **,
509 struct tog_view *, int);
510 static const struct got_error *close_log_view(struct tog_view *);
511 static const struct got_error *search_start_log_view(struct tog_view *);
512 static const struct got_error *search_next_log_view(struct tog_view *);
514 static const struct got_error *open_blame_view(struct tog_view *, char *,
515 struct got_object_id *, struct got_repository *);
516 static const struct got_error *show_blame_view(struct tog_view *);
517 static const struct got_error *input_blame_view(struct tog_view **,
518 struct tog_view *, int);
519 static const struct got_error *close_blame_view(struct tog_view *);
520 static const struct got_error *search_start_blame_view(struct tog_view *);
521 static const struct got_error *search_next_blame_view(struct tog_view *);
523 static const struct got_error *open_tree_view(struct tog_view *,
524 struct got_tree_object *, struct got_object_id *, const char *,
525 struct got_repository *);
526 static const struct got_error *show_tree_view(struct tog_view *);
527 static const struct got_error *input_tree_view(struct tog_view **,
528 struct tog_view *, int);
529 static const struct got_error *close_tree_view(struct tog_view *);
530 static const struct got_error *search_start_tree_view(struct tog_view *);
531 static const struct got_error *search_next_tree_view(struct tog_view *);
533 static const struct got_error *open_ref_view(struct tog_view *,
534 struct got_repository *);
535 static const struct got_error *show_ref_view(struct tog_view *);
536 static const struct got_error *input_ref_view(struct tog_view **,
537 struct tog_view *, int);
538 static const struct got_error *close_ref_view(struct tog_view *);
539 static const struct got_error *search_start_ref_view(struct tog_view *);
540 static const struct got_error *search_next_ref_view(struct tog_view *);
542 static volatile sig_atomic_t tog_sigwinch_received;
543 static volatile sig_atomic_t tog_sigpipe_received;
544 static volatile sig_atomic_t tog_sigcont_received;
546 static void
547 tog_sigwinch(int signo)
549 tog_sigwinch_received = 1;
552 static void
553 tog_sigpipe(int signo)
555 tog_sigpipe_received = 1;
558 static void
559 tog_sigcont(int signo)
561 tog_sigcont_received = 1;
564 static const struct got_error *
565 view_close(struct tog_view *view)
567 const struct got_error *err = NULL;
569 if (view->child) {
570 view_close(view->child);
571 view->child = NULL;
573 if (view->close)
574 err = view->close(view);
575 if (view->panel)
576 del_panel(view->panel);
577 if (view->window)
578 delwin(view->window);
579 free(view);
580 return err;
583 static struct tog_view *
584 view_open(int nlines, int ncols, int begin_y, int begin_x,
585 enum tog_view_type type)
587 struct tog_view *view = calloc(1, sizeof(*view));
589 if (view == NULL)
590 return NULL;
592 view->type = type;
593 view->lines = LINES;
594 view->cols = COLS;
595 view->nlines = nlines ? nlines : LINES - begin_y;
596 view->ncols = ncols ? ncols : COLS - begin_x;
597 view->begin_y = begin_y;
598 view->begin_x = begin_x;
599 view->window = newwin(nlines, ncols, begin_y, begin_x);
600 if (view->window == NULL) {
601 view_close(view);
602 return NULL;
604 view->panel = new_panel(view->window);
605 if (view->panel == NULL ||
606 set_panel_userptr(view->panel, view) != OK) {
607 view_close(view);
608 return NULL;
611 keypad(view->window, TRUE);
612 return view;
615 static int
616 view_split_begin_x(int begin_x)
618 if (begin_x > 0 || COLS < 120)
619 return 0;
620 return (COLS - MAX(COLS / 2, 80));
623 static const struct got_error *view_resize(struct tog_view *);
625 static const struct got_error *
626 view_splitscreen(struct tog_view *view)
628 const struct got_error *err = NULL;
630 view->begin_y = 0;
631 view->begin_x = view_split_begin_x(0);
632 view->nlines = LINES;
633 view->ncols = COLS - view->begin_x;
634 view->lines = LINES;
635 view->cols = COLS;
636 err = view_resize(view);
637 if (err)
638 return err;
640 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
641 return got_error_from_errno("mvwin");
643 return NULL;
646 static const struct got_error *
647 view_fullscreen(struct tog_view *view)
649 const struct got_error *err = NULL;
651 view->begin_x = 0;
652 view->begin_y = 0;
653 view->nlines = LINES;
654 view->ncols = COLS;
655 view->lines = LINES;
656 view->cols = COLS;
657 err = view_resize(view);
658 if (err)
659 return err;
661 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
662 return got_error_from_errno("mvwin");
664 return NULL;
667 static int
668 view_is_parent_view(struct tog_view *view)
670 return view->parent == NULL;
673 static const struct got_error *
674 view_resize(struct tog_view *view)
676 int nlines, ncols;
678 if (view->lines > LINES)
679 nlines = view->nlines - (view->lines - LINES);
680 else
681 nlines = view->nlines + (LINES - view->lines);
683 if (view->cols > COLS)
684 ncols = view->ncols - (view->cols - COLS);
685 else
686 ncols = view->ncols + (COLS - view->cols);
688 if (wresize(view->window, nlines, ncols) == ERR)
689 return got_error_from_errno("wresize");
690 if (replace_panel(view->panel, view->window) == ERR)
691 return got_error_from_errno("replace_panel");
692 wclear(view->window);
694 view->nlines = nlines;
695 view->ncols = ncols;
696 view->lines = LINES;
697 view->cols = COLS;
699 if (view->child) {
700 view->child->begin_x = view_split_begin_x(view->begin_x);
701 if (view->child->begin_x == 0) {
702 view_fullscreen(view->child);
703 if (view->child->focussed)
704 show_panel(view->child->panel);
705 else
706 show_panel(view->panel);
707 } else {
708 view_splitscreen(view->child);
709 show_panel(view->child->panel);
713 return NULL;
716 static const struct got_error *
717 view_close_child(struct tog_view *view)
719 const struct got_error *err = NULL;
721 if (view->child == NULL)
722 return NULL;
724 err = view_close(view->child);
725 view->child = NULL;
726 return err;
729 static void
730 view_set_child(struct tog_view *view, struct tog_view *child)
732 view->child = child;
733 child->parent = view;
736 static int
737 view_is_splitscreen(struct tog_view *view)
739 return view->begin_x > 0;
742 static void
743 tog_resizeterm(void)
745 int cols, lines;
746 struct winsize size;
748 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
749 cols = 80; /* Default */
750 lines = 24;
751 } else {
752 cols = size.ws_col;
753 lines = size.ws_row;
755 resize_term(lines, cols);
758 static const struct got_error *
759 view_search_start(struct tog_view *view)
761 const struct got_error *err = NULL;
762 char pattern[1024];
763 int ret;
765 if (view->nlines < 1)
766 return NULL;
768 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
769 wclrtoeol(view->window);
771 nocbreak();
772 echo();
773 ret = wgetnstr(view->window, pattern, sizeof(pattern));
774 cbreak();
775 noecho();
776 if (ret == ERR)
777 return NULL;
779 if (view->searching) {
780 regfree(&view->regex);
781 view->searching = 0;
784 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
785 err = view->search_start(view);
786 if (err) {
787 regfree(&view->regex);
788 return err;
790 view->searching = TOG_SEARCH_FORWARD;
791 view->search_next_done = 0;
792 view->search_next(view);
795 return NULL;
798 static const struct got_error *
799 view_input(struct tog_view **new, int *done, struct tog_view *view,
800 struct tog_view_list_head *views)
802 const struct got_error *err = NULL;
803 struct tog_view *v;
804 int ch, errcode;
806 *new = NULL;
808 /* Clear "no matches" indicator. */
809 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
810 view->search_next_done == TOG_SEARCH_HAVE_NONE)
811 view->search_next_done = TOG_SEARCH_HAVE_MORE;
813 if (view->searching && !view->search_next_done) {
814 errcode = pthread_mutex_unlock(&tog_mutex);
815 if (errcode)
816 return got_error_set_errno(errcode,
817 "pthread_mutex_unlock");
818 pthread_yield();
819 errcode = pthread_mutex_lock(&tog_mutex);
820 if (errcode)
821 return got_error_set_errno(errcode,
822 "pthread_mutex_lock");
823 view->search_next(view);
824 return NULL;
827 nodelay(stdscr, FALSE);
828 /* Allow threads to make progress while we are waiting for input. */
829 errcode = pthread_mutex_unlock(&tog_mutex);
830 if (errcode)
831 return got_error_set_errno(errcode, "pthread_mutex_unlock");
832 ch = wgetch(view->window);
833 errcode = pthread_mutex_lock(&tog_mutex);
834 if (errcode)
835 return got_error_set_errno(errcode, "pthread_mutex_lock");
836 nodelay(stdscr, TRUE);
838 if (tog_sigwinch_received || tog_sigcont_received) {
839 tog_resizeterm();
840 tog_sigwinch_received = 0;
841 tog_sigcont_received = 0;
842 TAILQ_FOREACH(v, views, entry) {
843 err = view_resize(v);
844 if (err)
845 return err;
846 err = v->input(new, v, KEY_RESIZE);
847 if (err)
848 return err;
849 if (v->child) {
850 err = view_resize(v->child);
851 if (err)
852 return err;
853 err = v->child->input(new, v->child,
854 KEY_RESIZE);
855 if (err)
856 return err;
861 switch (ch) {
862 case ERR:
863 break;
864 case '\t':
865 if (view->child) {
866 view->focussed = 0;
867 view->child->focussed = 1;
868 view->focus_child = 1;
869 } else if (view->parent) {
870 view->focussed = 0;
871 view->parent->focussed = 1;
872 view->parent->focus_child = 0;
874 break;
875 case 'q':
876 err = view->input(new, view, ch);
877 view->dying = 1;
878 break;
879 case 'Q':
880 *done = 1;
881 break;
882 case 'f':
883 if (view_is_parent_view(view)) {
884 if (view->child == NULL)
885 break;
886 if (view_is_splitscreen(view->child)) {
887 view->focussed = 0;
888 view->child->focussed = 1;
889 err = view_fullscreen(view->child);
890 } else
891 err = view_splitscreen(view->child);
892 if (err)
893 break;
894 err = view->child->input(new, view->child,
895 KEY_RESIZE);
896 } else {
897 if (view_is_splitscreen(view)) {
898 view->parent->focussed = 0;
899 view->focussed = 1;
900 err = view_fullscreen(view);
901 } else {
902 err = view_splitscreen(view);
904 if (err)
905 break;
906 err = view->input(new, view, KEY_RESIZE);
908 break;
909 case KEY_RESIZE:
910 break;
911 case '/':
912 if (view->search_start)
913 view_search_start(view);
914 else
915 err = view->input(new, view, ch);
916 break;
917 case 'N':
918 case 'n':
919 if (view->search_next) {
920 view->searching = (ch == 'n' ?
921 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
922 view->search_next_done = 0;
923 view->search_next(view);
924 } else
925 err = view->input(new, view, ch);
926 break;
927 default:
928 err = view->input(new, view, ch);
929 break;
932 return err;
935 void
936 view_vborder(struct tog_view *view)
938 PANEL *panel;
939 const struct tog_view *view_above;
941 if (view->parent)
942 return view_vborder(view->parent);
944 panel = panel_above(view->panel);
945 if (panel == NULL)
946 return;
948 view_above = panel_userptr(panel);
949 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
950 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
953 int
954 view_needs_focus_indication(struct tog_view *view)
956 if (view_is_parent_view(view)) {
957 if (view->child == NULL || view->child->focussed)
958 return 0;
959 if (!view_is_splitscreen(view->child))
960 return 0;
961 } else if (!view_is_splitscreen(view))
962 return 0;
964 return view->focussed;
967 static const struct got_error *
968 view_loop(struct tog_view *view)
970 const struct got_error *err = NULL;
971 struct tog_view_list_head views;
972 struct tog_view *new_view;
973 int fast_refresh = 10;
974 int done = 0, errcode;
976 errcode = pthread_mutex_lock(&tog_mutex);
977 if (errcode)
978 return got_error_set_errno(errcode, "pthread_mutex_lock");
980 TAILQ_INIT(&views);
981 TAILQ_INSERT_HEAD(&views, view, entry);
983 view->focussed = 1;
984 err = view->show(view);
985 if (err)
986 return err;
987 update_panels();
988 doupdate();
989 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
990 /* Refresh fast during initialization, then become slower. */
991 if (fast_refresh && fast_refresh-- == 0)
992 halfdelay(10); /* switch to once per second */
994 err = view_input(&new_view, &done, view, &views);
995 if (err)
996 break;
997 if (view->dying) {
998 struct tog_view *v, *prev = NULL;
1000 if (view_is_parent_view(view))
1001 prev = TAILQ_PREV(view, tog_view_list_head,
1002 entry);
1003 else if (view->parent)
1004 prev = view->parent;
1006 if (view->parent) {
1007 view->parent->child = NULL;
1008 view->parent->focus_child = 0;
1009 } else
1010 TAILQ_REMOVE(&views, view, entry);
1012 err = view_close(view);
1013 if (err)
1014 goto done;
1016 view = NULL;
1017 TAILQ_FOREACH(v, &views, entry) {
1018 if (v->focussed)
1019 break;
1021 if (view == NULL && new_view == NULL) {
1022 /* No view has focus. Try to pick one. */
1023 if (prev)
1024 view = prev;
1025 else if (!TAILQ_EMPTY(&views)) {
1026 view = TAILQ_LAST(&views,
1027 tog_view_list_head);
1029 if (view) {
1030 if (view->focus_child) {
1031 view->child->focussed = 1;
1032 view = view->child;
1033 } else
1034 view->focussed = 1;
1038 if (new_view) {
1039 struct tog_view *v, *t;
1040 /* Only allow one parent view per type. */
1041 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1042 if (v->type != new_view->type)
1043 continue;
1044 TAILQ_REMOVE(&views, v, entry);
1045 err = view_close(v);
1046 if (err)
1047 goto done;
1048 break;
1050 TAILQ_INSERT_TAIL(&views, new_view, entry);
1051 view = new_view;
1053 if (view) {
1054 if (view_is_parent_view(view)) {
1055 if (view->child && view->child->focussed)
1056 view = view->child;
1057 } else {
1058 if (view->parent && view->parent->focussed)
1059 view = view->parent;
1061 show_panel(view->panel);
1062 if (view->child && view_is_splitscreen(view->child))
1063 show_panel(view->child->panel);
1064 if (view->parent && view_is_splitscreen(view)) {
1065 err = view->parent->show(view->parent);
1066 if (err)
1067 goto done;
1069 err = view->show(view);
1070 if (err)
1071 goto done;
1072 if (view->child) {
1073 err = view->child->show(view->child);
1074 if (err)
1075 goto done;
1077 update_panels();
1078 doupdate();
1081 done:
1082 while (!TAILQ_EMPTY(&views)) {
1083 view = TAILQ_FIRST(&views);
1084 TAILQ_REMOVE(&views, view, entry);
1085 view_close(view);
1088 errcode = pthread_mutex_unlock(&tog_mutex);
1089 if (errcode && err == NULL)
1090 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1092 return err;
1095 __dead static void
1096 usage_log(void)
1098 endwin();
1099 fprintf(stderr,
1100 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1101 getprogname());
1102 exit(1);
1105 /* Create newly allocated wide-character string equivalent to a byte string. */
1106 static const struct got_error *
1107 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1109 char *vis = NULL;
1110 const struct got_error *err = NULL;
1112 *ws = NULL;
1113 *wlen = mbstowcs(NULL, s, 0);
1114 if (*wlen == (size_t)-1) {
1115 int vislen;
1116 if (errno != EILSEQ)
1117 return got_error_from_errno("mbstowcs");
1119 /* byte string invalid in current encoding; try to "fix" it */
1120 err = got_mbsavis(&vis, &vislen, s);
1121 if (err)
1122 return err;
1123 *wlen = mbstowcs(NULL, vis, 0);
1124 if (*wlen == (size_t)-1) {
1125 err = got_error_from_errno("mbstowcs"); /* give up */
1126 goto done;
1130 *ws = calloc(*wlen + 1, sizeof(**ws));
1131 if (*ws == NULL) {
1132 err = got_error_from_errno("calloc");
1133 goto done;
1136 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1137 err = got_error_from_errno("mbstowcs");
1138 done:
1139 free(vis);
1140 if (err) {
1141 free(*ws);
1142 *ws = NULL;
1143 *wlen = 0;
1145 return err;
1148 /* Format a line for display, ensuring that it won't overflow a width limit. */
1149 static const struct got_error *
1150 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1151 int col_tab_align)
1153 const struct got_error *err = NULL;
1154 int cols = 0;
1155 wchar_t *wline = NULL;
1156 size_t wlen;
1157 int i;
1159 *wlinep = NULL;
1160 *widthp = 0;
1162 err = mbs2ws(&wline, &wlen, line);
1163 if (err)
1164 return err;
1166 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1167 wline[wlen - 1] = L'\0';
1168 wlen--;
1170 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1171 wline[wlen - 1] = L'\0';
1172 wlen--;
1175 i = 0;
1176 while (i < wlen) {
1177 int width = wcwidth(wline[i]);
1179 if (width == 0) {
1180 i++;
1181 continue;
1184 if (width == 1 || width == 2) {
1185 if (cols + width > wlimit)
1186 break;
1187 cols += width;
1188 i++;
1189 } else if (width == -1) {
1190 if (wline[i] == L'\t') {
1191 width = TABSIZE -
1192 ((cols + col_tab_align) % TABSIZE);
1193 } else {
1194 width = 1;
1195 wline[i] = L'.';
1197 if (cols + width > wlimit)
1198 break;
1199 cols += width;
1200 i++;
1201 } else {
1202 err = got_error_from_errno("wcwidth");
1203 goto done;
1206 wline[i] = L'\0';
1207 if (widthp)
1208 *widthp = cols;
1209 done:
1210 if (err)
1211 free(wline);
1212 else
1213 *wlinep = wline;
1214 return err;
1217 static const struct got_error*
1218 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1219 struct got_object_id *id, struct got_repository *repo)
1221 static const struct got_error *err = NULL;
1222 struct got_reflist_entry *re;
1223 char *s;
1224 const char *name;
1226 *refs_str = NULL;
1228 SIMPLEQ_FOREACH(re, refs, entry) {
1229 struct got_tag_object *tag = NULL;
1230 struct got_object_id *ref_id;
1231 int cmp;
1233 name = got_ref_get_name(re->ref);
1234 if (strcmp(name, GOT_REF_HEAD) == 0)
1235 continue;
1236 if (strncmp(name, "refs/", 5) == 0)
1237 name += 5;
1238 if (strncmp(name, "got/", 4) == 0)
1239 continue;
1240 if (strncmp(name, "heads/", 6) == 0)
1241 name += 6;
1242 if (strncmp(name, "remotes/", 8) == 0) {
1243 name += 8;
1244 s = strstr(name, "/" GOT_REF_HEAD);
1245 if (s != NULL && s[strlen(s)] == '\0')
1246 continue;
1248 err = got_ref_resolve(&ref_id, repo, re->ref);
1249 if (err)
1250 break;
1251 if (strncmp(name, "tags/", 5) == 0) {
1252 err = got_object_open_as_tag(&tag, repo, ref_id);
1253 if (err) {
1254 if (err->code != GOT_ERR_OBJ_TYPE) {
1255 free(ref_id);
1256 break;
1258 /* Ref points at something other than a tag. */
1259 err = NULL;
1260 tag = NULL;
1263 cmp = got_object_id_cmp(tag ?
1264 got_object_tag_get_object_id(tag) : ref_id, id);
1265 free(ref_id);
1266 if (tag)
1267 got_object_tag_close(tag);
1268 if (cmp != 0)
1269 continue;
1270 s = *refs_str;
1271 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1272 s ? ", " : "", name) == -1) {
1273 err = got_error_from_errno("asprintf");
1274 free(s);
1275 *refs_str = NULL;
1276 break;
1278 free(s);
1281 return err;
1284 static const struct got_error *
1285 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1286 int col_tab_align)
1288 char *smallerthan, *at;
1290 smallerthan = strchr(author, '<');
1291 if (smallerthan && smallerthan[1] != '\0')
1292 author = smallerthan + 1;
1293 at = strchr(author, '@');
1294 if (at)
1295 *at = '\0';
1296 return format_line(wauthor, author_width, author, limit, col_tab_align);
1299 static const struct got_error *
1300 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1301 struct got_object_id *id, const size_t date_display_cols,
1302 int author_display_cols)
1304 struct tog_log_view_state *s = &view->state.log;
1305 const struct got_error *err = NULL;
1306 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1307 char *logmsg0 = NULL, *logmsg = NULL;
1308 char *author = NULL;
1309 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1310 int author_width, logmsg_width;
1311 char *newline, *line = NULL;
1312 int col, limit;
1313 const int avail = view->ncols;
1314 struct tm tm;
1315 time_t committer_time;
1316 struct tog_color *tc;
1318 committer_time = got_object_commit_get_committer_time(commit);
1319 if (localtime_r(&committer_time, &tm) == NULL)
1320 return got_error_from_errno("localtime_r");
1321 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1322 >= sizeof(datebuf))
1323 return got_error(GOT_ERR_NO_SPACE);
1325 if (avail <= date_display_cols)
1326 limit = MIN(sizeof(datebuf) - 1, avail);
1327 else
1328 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1329 tc = get_color(&s->colors, TOG_COLOR_DATE);
1330 if (tc)
1331 wattr_on(view->window,
1332 COLOR_PAIR(tc->colorpair), NULL);
1333 waddnstr(view->window, datebuf, limit);
1334 if (tc)
1335 wattr_off(view->window,
1336 COLOR_PAIR(tc->colorpair), NULL);
1337 col = limit;
1338 if (col > avail)
1339 goto done;
1341 if (avail >= 120) {
1342 char *id_str;
1343 err = got_object_id_str(&id_str, id);
1344 if (err)
1345 goto done;
1346 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1347 if (tc)
1348 wattr_on(view->window,
1349 COLOR_PAIR(tc->colorpair), NULL);
1350 wprintw(view->window, "%.8s ", id_str);
1351 if (tc)
1352 wattr_off(view->window,
1353 COLOR_PAIR(tc->colorpair), NULL);
1354 free(id_str);
1355 col += 9;
1356 if (col > avail)
1357 goto done;
1360 author = strdup(got_object_commit_get_author(commit));
1361 if (author == NULL) {
1362 err = got_error_from_errno("strdup");
1363 goto done;
1365 err = format_author(&wauthor, &author_width, author, avail - col, col);
1366 if (err)
1367 goto done;
1368 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1369 if (tc)
1370 wattr_on(view->window,
1371 COLOR_PAIR(tc->colorpair), NULL);
1372 waddwstr(view->window, wauthor);
1373 if (tc)
1374 wattr_off(view->window,
1375 COLOR_PAIR(tc->colorpair), NULL);
1376 col += author_width;
1377 while (col < avail && author_width < author_display_cols + 2) {
1378 waddch(view->window, ' ');
1379 col++;
1380 author_width++;
1382 if (col > avail)
1383 goto done;
1385 err = got_object_commit_get_logmsg(&logmsg0, commit);
1386 if (err)
1387 goto done;
1388 logmsg = logmsg0;
1389 while (*logmsg == '\n')
1390 logmsg++;
1391 newline = strchr(logmsg, '\n');
1392 if (newline)
1393 *newline = '\0';
1394 limit = avail - col;
1395 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1396 if (err)
1397 goto done;
1398 waddwstr(view->window, wlogmsg);
1399 col += logmsg_width;
1400 while (col < avail) {
1401 waddch(view->window, ' ');
1402 col++;
1404 done:
1405 free(logmsg0);
1406 free(wlogmsg);
1407 free(author);
1408 free(wauthor);
1409 free(line);
1410 return err;
1413 static struct commit_queue_entry *
1414 alloc_commit_queue_entry(struct got_commit_object *commit,
1415 struct got_object_id *id)
1417 struct commit_queue_entry *entry;
1419 entry = calloc(1, sizeof(*entry));
1420 if (entry == NULL)
1421 return NULL;
1423 entry->id = id;
1424 entry->commit = commit;
1425 return entry;
1428 static void
1429 pop_commit(struct commit_queue *commits)
1431 struct commit_queue_entry *entry;
1433 entry = TAILQ_FIRST(&commits->head);
1434 TAILQ_REMOVE(&commits->head, entry, entry);
1435 got_object_commit_close(entry->commit);
1436 commits->ncommits--;
1437 /* Don't free entry->id! It is owned by the commit graph. */
1438 free(entry);
1441 static void
1442 free_commits(struct commit_queue *commits)
1444 while (!TAILQ_EMPTY(&commits->head))
1445 pop_commit(commits);
1448 static const struct got_error *
1449 match_commit(int *have_match, struct got_object_id *id,
1450 struct got_commit_object *commit, regex_t *regex)
1452 const struct got_error *err = NULL;
1453 regmatch_t regmatch;
1454 char *id_str = NULL, *logmsg = NULL;
1456 *have_match = 0;
1458 err = got_object_id_str(&id_str, id);
1459 if (err)
1460 return err;
1462 err = got_object_commit_get_logmsg(&logmsg, commit);
1463 if (err)
1464 goto done;
1466 if (regexec(regex, got_object_commit_get_author(commit), 1,
1467 &regmatch, 0) == 0 ||
1468 regexec(regex, got_object_commit_get_committer(commit), 1,
1469 &regmatch, 0) == 0 ||
1470 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1471 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1472 *have_match = 1;
1473 done:
1474 free(id_str);
1475 free(logmsg);
1476 return err;
1479 static const struct got_error *
1480 queue_commits(struct tog_log_thread_args *a)
1482 const struct got_error *err = NULL;
1485 * We keep all commits open throughout the lifetime of the log
1486 * view in order to avoid having to re-fetch commits from disk
1487 * while updating the display.
1489 do {
1490 struct got_object_id *id;
1491 struct got_commit_object *commit;
1492 struct commit_queue_entry *entry;
1493 int errcode;
1495 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1496 NULL, NULL);
1497 if (err || id == NULL)
1498 break;
1500 err = got_object_open_as_commit(&commit, a->repo, id);
1501 if (err)
1502 break;
1503 entry = alloc_commit_queue_entry(commit, id);
1504 if (entry == NULL) {
1505 err = got_error_from_errno("alloc_commit_queue_entry");
1506 break;
1509 errcode = pthread_mutex_lock(&tog_mutex);
1510 if (errcode) {
1511 err = got_error_set_errno(errcode,
1512 "pthread_mutex_lock");
1513 break;
1516 entry->idx = a->commits->ncommits;
1517 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1518 a->commits->ncommits++;
1520 if (*a->searching == TOG_SEARCH_FORWARD &&
1521 !*a->search_next_done) {
1522 int have_match;
1523 err = match_commit(&have_match, id, commit, a->regex);
1524 if (err)
1525 break;
1526 if (have_match)
1527 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1530 errcode = pthread_mutex_unlock(&tog_mutex);
1531 if (errcode && err == NULL)
1532 err = got_error_set_errno(errcode,
1533 "pthread_mutex_unlock");
1534 if (err)
1535 break;
1536 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1538 return err;
1541 static void
1542 select_commit(struct tog_log_view_state *s)
1544 struct commit_queue_entry *entry;
1545 int ncommits = 0;
1547 entry = s->first_displayed_entry;
1548 while (entry) {
1549 if (ncommits == s->selected) {
1550 s->selected_entry = entry;
1551 break;
1553 entry = TAILQ_NEXT(entry, entry);
1554 ncommits++;
1558 static const struct got_error *
1559 draw_commits(struct tog_view *view)
1561 const struct got_error *err = NULL;
1562 struct tog_log_view_state *s = &view->state.log;
1563 struct commit_queue_entry *entry = s->selected_entry;
1564 const int limit = view->nlines;
1565 int width;
1566 int ncommits, author_cols = 4;
1567 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1568 char *refs_str = NULL;
1569 wchar_t *wline;
1570 struct tog_color *tc;
1571 static const size_t date_display_cols = 12;
1573 if (s->selected_entry &&
1574 !(view->searching && view->search_next_done == 0)) {
1575 err = got_object_id_str(&id_str, s->selected_entry->id);
1576 if (err)
1577 return err;
1578 err = build_refs_str(&refs_str, &s->refs,
1579 s->selected_entry->id, s->repo);
1580 if (err)
1581 goto done;
1584 if (s->thread_args.commits_needed == 0)
1585 halfdelay(10); /* disable fast refresh */
1587 if (s->thread_args.commits_needed > 0) {
1588 if (asprintf(&ncommits_str, " [%d/%d] %s",
1589 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1590 (view->searching && !view->search_next_done) ?
1591 "searching..." : "loading...") == -1) {
1592 err = got_error_from_errno("asprintf");
1593 goto done;
1595 } else {
1596 const char *search_str = NULL;
1598 if (view->searching) {
1599 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1600 search_str = "no more matches";
1601 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1602 search_str = "no matches found";
1603 else if (!view->search_next_done)
1604 search_str = "searching...";
1607 if (asprintf(&ncommits_str, " [%d/%d] %s",
1608 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1609 search_str ? search_str :
1610 (refs_str ? refs_str : "")) == -1) {
1611 err = got_error_from_errno("asprintf");
1612 goto done;
1616 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1617 if (asprintf(&header, "commit %s %s%s",
1618 id_str ? id_str : "........................................",
1619 s->in_repo_path, ncommits_str) == -1) {
1620 err = got_error_from_errno("asprintf");
1621 header = NULL;
1622 goto done;
1624 } else if (asprintf(&header, "commit %s%s",
1625 id_str ? id_str : "........................................",
1626 ncommits_str) == -1) {
1627 err = got_error_from_errno("asprintf");
1628 header = NULL;
1629 goto done;
1631 err = format_line(&wline, &width, header, view->ncols, 0);
1632 if (err)
1633 goto done;
1635 werase(view->window);
1637 if (view_needs_focus_indication(view))
1638 wstandout(view->window);
1639 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1640 if (tc)
1641 wattr_on(view->window,
1642 COLOR_PAIR(tc->colorpair), NULL);
1643 waddwstr(view->window, wline);
1644 if (tc)
1645 wattr_off(view->window,
1646 COLOR_PAIR(tc->colorpair), NULL);
1647 while (width < view->ncols) {
1648 waddch(view->window, ' ');
1649 width++;
1651 if (view_needs_focus_indication(view))
1652 wstandend(view->window);
1653 free(wline);
1654 if (limit <= 1)
1655 goto done;
1657 /* Grow author column size if necessary. */
1658 entry = s->first_displayed_entry;
1659 ncommits = 0;
1660 while (entry) {
1661 char *author;
1662 wchar_t *wauthor;
1663 int width;
1664 if (ncommits >= limit - 1)
1665 break;
1666 author = strdup(got_object_commit_get_author(entry->commit));
1667 if (author == NULL) {
1668 err = got_error_from_errno("strdup");
1669 goto done;
1671 err = format_author(&wauthor, &width, author, COLS,
1672 date_display_cols);
1673 if (author_cols < width)
1674 author_cols = width;
1675 free(wauthor);
1676 free(author);
1677 ncommits++;
1678 entry = TAILQ_NEXT(entry, entry);
1681 entry = s->first_displayed_entry;
1682 s->last_displayed_entry = s->first_displayed_entry;
1683 ncommits = 0;
1684 while (entry) {
1685 if (ncommits >= limit - 1)
1686 break;
1687 if (ncommits == s->selected)
1688 wstandout(view->window);
1689 err = draw_commit(view, entry->commit, entry->id,
1690 date_display_cols, author_cols);
1691 if (ncommits == s->selected)
1692 wstandend(view->window);
1693 if (err)
1694 goto done;
1695 ncommits++;
1696 s->last_displayed_entry = entry;
1697 entry = TAILQ_NEXT(entry, entry);
1700 view_vborder(view);
1701 done:
1702 free(id_str);
1703 free(refs_str);
1704 free(ncommits_str);
1705 free(header);
1706 return err;
1709 static void
1710 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1712 struct commit_queue_entry *entry;
1713 int nscrolled = 0;
1715 entry = TAILQ_FIRST(&s->commits.head);
1716 if (s->first_displayed_entry == entry)
1717 return;
1719 entry = s->first_displayed_entry;
1720 while (entry && nscrolled < maxscroll) {
1721 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1722 if (entry) {
1723 s->first_displayed_entry = entry;
1724 nscrolled++;
1729 static const struct got_error *
1730 trigger_log_thread(struct tog_view *view, int wait)
1732 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1733 int errcode;
1735 halfdelay(1); /* fast refresh while loading commits */
1737 while (ta->commits_needed > 0) {
1738 if (ta->log_complete)
1739 break;
1741 /* Wake the log thread. */
1742 errcode = pthread_cond_signal(&ta->need_commits);
1743 if (errcode)
1744 return got_error_set_errno(errcode,
1745 "pthread_cond_signal");
1748 * The mutex will be released while the view loop waits
1749 * in wgetch(), at which time the log thread will run.
1751 if (!wait)
1752 break;
1754 /* Display progress update in log view. */
1755 show_log_view(view);
1756 update_panels();
1757 doupdate();
1759 /* Wait right here while next commit is being loaded. */
1760 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1761 if (errcode)
1762 return got_error_set_errno(errcode,
1763 "pthread_cond_wait");
1765 /* Display progress update in log view. */
1766 show_log_view(view);
1767 update_panels();
1768 doupdate();
1771 return NULL;
1774 static const struct got_error *
1775 log_scroll_down(struct tog_view *view, int maxscroll)
1777 struct tog_log_view_state *s = &view->state.log;
1778 const struct got_error *err = NULL;
1779 struct commit_queue_entry *pentry;
1780 int nscrolled = 0, ncommits_needed;
1782 if (s->last_displayed_entry == NULL)
1783 return NULL;
1785 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1786 if (s->commits.ncommits < ncommits_needed &&
1787 !s->thread_args.log_complete) {
1789 * Ask the log thread for required amount of commits.
1791 s->thread_args.commits_needed += maxscroll;
1792 err = trigger_log_thread(view, 1);
1793 if (err)
1794 return err;
1797 do {
1798 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1799 if (pentry == NULL)
1800 break;
1802 s->last_displayed_entry = pentry;
1804 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1805 if (pentry == NULL)
1806 break;
1807 s->first_displayed_entry = pentry;
1808 } while (++nscrolled < maxscroll);
1810 return err;
1813 static const struct got_error *
1814 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1815 struct got_commit_object *commit, struct got_object_id *commit_id,
1816 struct tog_view *log_view, struct got_repository *repo)
1818 const struct got_error *err;
1819 struct got_object_qid *parent_id;
1820 struct tog_view *diff_view;
1822 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1823 if (diff_view == NULL)
1824 return got_error_from_errno("view_open");
1826 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1827 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1828 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1829 if (err == NULL)
1830 *new_view = diff_view;
1831 return err;
1834 static const struct got_error *
1835 tree_view_visit_subtree(struct tog_tree_view_state *s,
1836 struct got_tree_object *subtree)
1838 struct tog_parent_tree *parent;
1840 parent = calloc(1, sizeof(*parent));
1841 if (parent == NULL)
1842 return got_error_from_errno("calloc");
1844 parent->tree = s->tree;
1845 parent->first_displayed_entry = s->first_displayed_entry;
1846 parent->selected_entry = s->selected_entry;
1847 parent->selected = s->selected;
1848 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1849 s->tree = subtree;
1850 s->selected = 0;
1851 s->first_displayed_entry = NULL;
1852 return NULL;
1855 static const struct got_error *
1856 tree_view_walk_path(struct tog_tree_view_state *s,
1857 struct got_object_id *commit_id, const char *path)
1859 const struct got_error *err = NULL;
1860 struct got_tree_object *tree = NULL;
1861 const char *p;
1862 char *slash, *subpath = NULL;
1864 /* Walk the path and open corresponding tree objects. */
1865 p = path;
1866 while (*p) {
1867 struct got_tree_entry *te;
1868 struct got_object_id *tree_id;
1869 char *te_name;
1871 while (p[0] == '/')
1872 p++;
1874 /* Ensure the correct subtree entry is selected. */
1875 slash = strchr(p, '/');
1876 if (slash == NULL)
1877 te_name = strdup(p);
1878 else
1879 te_name = strndup(p, slash - p);
1880 if (te_name == NULL) {
1881 err = got_error_from_errno("strndup");
1882 break;
1884 te = got_object_tree_find_entry(s->tree, te_name);
1885 if (te == NULL) {
1886 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1887 free(te_name);
1888 break;
1890 free(te_name);
1891 s->first_displayed_entry = s->selected_entry = te;
1893 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1894 break; /* jump to this file's entry */
1896 slash = strchr(p, '/');
1897 if (slash)
1898 subpath = strndup(path, slash - path);
1899 else
1900 subpath = strdup(path);
1901 if (subpath == NULL) {
1902 err = got_error_from_errno("strdup");
1903 break;
1906 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1907 subpath);
1908 if (err)
1909 break;
1911 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1912 free(tree_id);
1913 if (err)
1914 break;
1916 err = tree_view_visit_subtree(s, tree);
1917 if (err) {
1918 got_object_tree_close(tree);
1919 break;
1921 if (slash == NULL)
1922 break;
1923 free(subpath);
1924 subpath = NULL;
1925 p = slash;
1928 free(subpath);
1929 return err;
1932 static const struct got_error *
1933 browse_commit_tree(struct tog_view **new_view, int begin_x,
1934 struct commit_queue_entry *entry, const char *path,
1935 const char *head_ref_name, struct got_repository *repo)
1937 const struct got_error *err = NULL;
1938 struct got_tree_object *tree;
1939 struct tog_tree_view_state *s;
1940 struct tog_view *tree_view;
1942 err = got_object_open_as_tree(&tree, repo,
1943 got_object_commit_get_tree_id(entry->commit));
1944 if (err)
1945 return err;
1947 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1948 if (tree_view == NULL)
1949 return got_error_from_errno("view_open");
1951 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1952 if (err) {
1953 got_object_tree_close(tree);
1954 return err;
1956 s = &tree_view->state.tree;
1958 *new_view = tree_view;
1960 if (got_path_is_root_dir(path))
1961 return NULL;
1963 return tree_view_walk_path(s, entry->id, path);
1966 static const struct got_error *
1967 block_signals_used_by_main_thread(void)
1969 sigset_t sigset;
1970 int errcode;
1972 if (sigemptyset(&sigset) == -1)
1973 return got_error_from_errno("sigemptyset");
1975 /* tog handles SIGWINCH and SIGCONT */
1976 if (sigaddset(&sigset, SIGWINCH) == -1)
1977 return got_error_from_errno("sigaddset");
1978 if (sigaddset(&sigset, SIGCONT) == -1)
1979 return got_error_from_errno("sigaddset");
1981 /* ncurses handles SIGTSTP */
1982 if (sigaddset(&sigset, SIGTSTP) == -1)
1983 return got_error_from_errno("sigaddset");
1985 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1986 if (errcode)
1987 return got_error_set_errno(errcode, "pthread_sigmask");
1989 return NULL;
1992 static void *
1993 log_thread(void *arg)
1995 const struct got_error *err = NULL;
1996 int errcode = 0;
1997 struct tog_log_thread_args *a = arg;
1998 int done = 0;
2000 err = block_signals_used_by_main_thread();
2001 if (err)
2002 return (void *)err;
2004 while (!done && !err && !tog_sigpipe_received) {
2005 err = queue_commits(a);
2006 if (err) {
2007 if (err->code != GOT_ERR_ITER_COMPLETED)
2008 return (void *)err;
2009 err = NULL;
2010 done = 1;
2011 } else if (a->commits_needed > 0)
2012 a->commits_needed--;
2014 errcode = pthread_mutex_lock(&tog_mutex);
2015 if (errcode) {
2016 err = got_error_set_errno(errcode,
2017 "pthread_mutex_lock");
2018 break;
2019 } else if (*a->quit)
2020 done = 1;
2021 else if (*a->first_displayed_entry == NULL) {
2022 *a->first_displayed_entry =
2023 TAILQ_FIRST(&a->commits->head);
2024 *a->selected_entry = *a->first_displayed_entry;
2027 errcode = pthread_cond_signal(&a->commit_loaded);
2028 if (errcode) {
2029 err = got_error_set_errno(errcode,
2030 "pthread_cond_signal");
2031 pthread_mutex_unlock(&tog_mutex);
2032 break;
2035 if (done)
2036 a->commits_needed = 0;
2037 else {
2038 if (a->commits_needed == 0) {
2039 errcode = pthread_cond_wait(&a->need_commits,
2040 &tog_mutex);
2041 if (errcode)
2042 err = got_error_set_errno(errcode,
2043 "pthread_cond_wait");
2044 if (*a->quit)
2045 done = 1;
2049 errcode = pthread_mutex_unlock(&tog_mutex);
2050 if (errcode && err == NULL)
2051 err = got_error_set_errno(errcode,
2052 "pthread_mutex_unlock");
2054 a->log_complete = 1;
2055 return (void *)err;
2058 static const struct got_error *
2059 stop_log_thread(struct tog_log_view_state *s)
2061 const struct got_error *err = NULL;
2062 int errcode;
2064 if (s->thread) {
2065 s->quit = 1;
2066 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2067 if (errcode)
2068 return got_error_set_errno(errcode,
2069 "pthread_cond_signal");
2070 errcode = pthread_mutex_unlock(&tog_mutex);
2071 if (errcode)
2072 return got_error_set_errno(errcode,
2073 "pthread_mutex_unlock");
2074 errcode = pthread_join(s->thread, (void **)&err);
2075 if (errcode)
2076 return got_error_set_errno(errcode, "pthread_join");
2077 errcode = pthread_mutex_lock(&tog_mutex);
2078 if (errcode)
2079 return got_error_set_errno(errcode,
2080 "pthread_mutex_lock");
2081 s->thread = NULL;
2084 if (s->thread_args.repo) {
2085 got_repo_close(s->thread_args.repo);
2086 s->thread_args.repo = NULL;
2089 if (s->thread_args.graph) {
2090 got_commit_graph_close(s->thread_args.graph);
2091 s->thread_args.graph = NULL;
2094 return err;
2097 static const struct got_error *
2098 close_log_view(struct tog_view *view)
2100 const struct got_error *err = NULL;
2101 struct tog_log_view_state *s = &view->state.log;
2102 int errcode;
2104 err = stop_log_thread(s);
2106 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2107 if (errcode && err == NULL)
2108 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2110 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2111 if (errcode && err == NULL)
2112 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2114 free_commits(&s->commits);
2115 free(s->in_repo_path);
2116 s->in_repo_path = NULL;
2117 free(s->start_id);
2118 s->start_id = NULL;
2119 free(s->head_ref_name);
2120 s->head_ref_name = NULL;
2121 got_ref_list_free(&s->refs);
2122 return err;
2125 static const struct got_error *
2126 search_start_log_view(struct tog_view *view)
2128 struct tog_log_view_state *s = &view->state.log;
2130 s->matched_entry = NULL;
2131 s->search_entry = NULL;
2132 return NULL;
2135 static const struct got_error *
2136 search_next_log_view(struct tog_view *view)
2138 const struct got_error *err = NULL;
2139 struct tog_log_view_state *s = &view->state.log;
2140 struct commit_queue_entry *entry;
2142 /* Display progress update in log view. */
2143 show_log_view(view);
2144 update_panels();
2145 doupdate();
2147 if (s->search_entry) {
2148 int errcode, ch;
2149 errcode = pthread_mutex_unlock(&tog_mutex);
2150 if (errcode)
2151 return got_error_set_errno(errcode,
2152 "pthread_mutex_unlock");
2153 ch = wgetch(view->window);
2154 errcode = pthread_mutex_lock(&tog_mutex);
2155 if (errcode)
2156 return got_error_set_errno(errcode,
2157 "pthread_mutex_lock");
2158 if (ch == KEY_BACKSPACE) {
2159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2160 return NULL;
2162 if (view->searching == TOG_SEARCH_FORWARD)
2163 entry = TAILQ_NEXT(s->search_entry, entry);
2164 else
2165 entry = TAILQ_PREV(s->search_entry,
2166 commit_queue_head, entry);
2167 } else if (s->matched_entry) {
2168 if (view->searching == TOG_SEARCH_FORWARD)
2169 entry = TAILQ_NEXT(s->matched_entry, entry);
2170 else
2171 entry = TAILQ_PREV(s->matched_entry,
2172 commit_queue_head, entry);
2173 } else {
2174 if (view->searching == TOG_SEARCH_FORWARD)
2175 entry = TAILQ_FIRST(&s->commits.head);
2176 else
2177 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2180 while (1) {
2181 int have_match = 0;
2183 if (entry == NULL) {
2184 if (s->thread_args.log_complete ||
2185 view->searching == TOG_SEARCH_BACKWARD) {
2186 view->search_next_done =
2187 (s->matched_entry == NULL ?
2188 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2189 s->search_entry = NULL;
2190 return NULL;
2193 * Poke the log thread for more commits and return,
2194 * allowing the main loop to make progress. Search
2195 * will resume at s->search_entry once we come back.
2197 s->thread_args.commits_needed++;
2198 return trigger_log_thread(view, 0);
2201 err = match_commit(&have_match, entry->id, entry->commit,
2202 &view->regex);
2203 if (err)
2204 break;
2205 if (have_match) {
2206 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2207 s->matched_entry = entry;
2208 break;
2211 s->search_entry = entry;
2212 if (view->searching == TOG_SEARCH_FORWARD)
2213 entry = TAILQ_NEXT(entry, entry);
2214 else
2215 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2218 if (s->matched_entry) {
2219 int cur = s->selected_entry->idx;
2220 while (cur < s->matched_entry->idx) {
2221 err = input_log_view(NULL, view, KEY_DOWN);
2222 if (err)
2223 return err;
2224 cur++;
2226 while (cur > s->matched_entry->idx) {
2227 err = input_log_view(NULL, view, KEY_UP);
2228 if (err)
2229 return err;
2230 cur--;
2234 s->search_entry = NULL;
2236 return NULL;
2239 static const struct got_error *
2240 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2241 struct got_repository *repo, const char *head_ref_name,
2242 const char *in_repo_path, int log_branches)
2244 const struct got_error *err = NULL;
2245 struct tog_log_view_state *s = &view->state.log;
2246 struct got_repository *thread_repo = NULL;
2247 struct got_commit_graph *thread_graph = NULL;
2248 int errcode;
2250 SIMPLEQ_INIT(&s->refs);
2252 if (in_repo_path != s->in_repo_path) {
2253 free(s->in_repo_path);
2254 s->in_repo_path = strdup(in_repo_path);
2255 if (s->in_repo_path == NULL)
2256 return got_error_from_errno("strdup");
2259 /* The commit queue only contains commits being displayed. */
2260 TAILQ_INIT(&s->commits.head);
2261 s->commits.ncommits = 0;
2263 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2264 if (err)
2265 goto done;
2267 s->repo = repo;
2268 if (head_ref_name) {
2269 s->head_ref_name = strdup(head_ref_name);
2270 if (s->head_ref_name == NULL) {
2271 err = got_error_from_errno("strdup");
2272 goto done;
2275 s->start_id = got_object_id_dup(start_id);
2276 if (s->start_id == NULL) {
2277 err = got_error_from_errno("got_object_id_dup");
2278 goto done;
2280 s->log_branches = log_branches;
2282 SIMPLEQ_INIT(&s->colors);
2283 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2284 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2285 get_color_value("TOG_COLOR_COMMIT"));
2286 if (err)
2287 goto done;
2288 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2289 get_color_value("TOG_COLOR_AUTHOR"));
2290 if (err) {
2291 free_colors(&s->colors);
2292 goto done;
2294 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2295 get_color_value("TOG_COLOR_DATE"));
2296 if (err) {
2297 free_colors(&s->colors);
2298 goto done;
2302 view->show = show_log_view;
2303 view->input = input_log_view;
2304 view->close = close_log_view;
2305 view->search_start = search_start_log_view;
2306 view->search_next = search_next_log_view;
2308 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2309 if (err)
2310 goto done;
2311 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2312 !s->log_branches);
2313 if (err)
2314 goto done;
2315 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2316 s->repo, NULL, NULL);
2317 if (err)
2318 goto done;
2320 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2321 if (errcode) {
2322 err = got_error_set_errno(errcode, "pthread_cond_init");
2323 goto done;
2325 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2326 if (errcode) {
2327 err = got_error_set_errno(errcode, "pthread_cond_init");
2328 goto done;
2331 s->thread_args.commits_needed = view->nlines;
2332 s->thread_args.graph = thread_graph;
2333 s->thread_args.commits = &s->commits;
2334 s->thread_args.in_repo_path = s->in_repo_path;
2335 s->thread_args.start_id = s->start_id;
2336 s->thread_args.repo = thread_repo;
2337 s->thread_args.log_complete = 0;
2338 s->thread_args.quit = &s->quit;
2339 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2340 s->thread_args.selected_entry = &s->selected_entry;
2341 s->thread_args.searching = &view->searching;
2342 s->thread_args.search_next_done = &view->search_next_done;
2343 s->thread_args.regex = &view->regex;
2344 done:
2345 if (err)
2346 close_log_view(view);
2347 return err;
2350 static const struct got_error *
2351 show_log_view(struct tog_view *view)
2353 const struct got_error *err;
2354 struct tog_log_view_state *s = &view->state.log;
2356 if (s->thread == NULL) {
2357 int errcode = pthread_create(&s->thread, NULL, log_thread,
2358 &s->thread_args);
2359 if (errcode)
2360 return got_error_set_errno(errcode, "pthread_create");
2361 if (s->thread_args.commits_needed > 0) {
2362 err = trigger_log_thread(view, 1);
2363 if (err)
2364 return err;
2368 return draw_commits(view);
2371 static const struct got_error *
2372 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2374 const struct got_error *err = NULL;
2375 struct tog_log_view_state *s = &view->state.log;
2376 struct tog_view *diff_view = NULL, *tree_view = NULL;
2377 struct tog_view *ref_view = NULL;
2378 int begin_x = 0;
2380 switch (ch) {
2381 case 'q':
2382 s->quit = 1;
2383 break;
2384 case 'k':
2385 case KEY_UP:
2386 case '<':
2387 case ',':
2388 if (s->first_displayed_entry == NULL)
2389 break;
2390 if (s->selected > 0)
2391 s->selected--;
2392 else
2393 log_scroll_up(s, 1);
2394 select_commit(s);
2395 break;
2396 case KEY_PPAGE:
2397 case CTRL('b'):
2398 if (s->first_displayed_entry == NULL)
2399 break;
2400 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2401 s->selected = 0;
2402 else
2403 log_scroll_up(s, view->nlines - 1);
2404 select_commit(s);
2405 break;
2406 case 'j':
2407 case KEY_DOWN:
2408 case '>':
2409 case '.':
2410 if (s->first_displayed_entry == NULL)
2411 break;
2412 if (s->selected < MIN(view->nlines - 2,
2413 s->commits.ncommits - 1))
2414 s->selected++;
2415 else {
2416 err = log_scroll_down(view, 1);
2417 if (err)
2418 break;
2420 select_commit(s);
2421 break;
2422 case KEY_NPAGE:
2423 case CTRL('f'): {
2424 struct commit_queue_entry *first;
2425 first = s->first_displayed_entry;
2426 if (first == NULL)
2427 break;
2428 err = log_scroll_down(view, view->nlines - 1);
2429 if (err)
2430 break;
2431 if (first == s->first_displayed_entry &&
2432 s->selected < MIN(view->nlines - 2,
2433 s->commits.ncommits - 1)) {
2434 /* can't scroll further down */
2435 s->selected = MIN(view->nlines - 2,
2436 s->commits.ncommits - 1);
2438 select_commit(s);
2439 break;
2441 case KEY_RESIZE:
2442 if (s->selected > view->nlines - 2)
2443 s->selected = view->nlines - 2;
2444 if (s->selected > s->commits.ncommits - 1)
2445 s->selected = s->commits.ncommits - 1;
2446 select_commit(s);
2447 if (s->commits.ncommits < view->nlines - 1 &&
2448 !s->thread_args.log_complete) {
2449 s->thread_args.commits_needed += (view->nlines - 1) -
2450 s->commits.ncommits;
2451 err = trigger_log_thread(view, 1);
2453 break;
2454 case KEY_ENTER:
2455 case ' ':
2456 case '\r':
2457 if (s->selected_entry == NULL)
2458 break;
2459 if (view_is_parent_view(view))
2460 begin_x = view_split_begin_x(view->begin_x);
2461 err = open_diff_view_for_commit(&diff_view, begin_x,
2462 s->selected_entry->commit, s->selected_entry->id,
2463 view, s->repo);
2464 if (err)
2465 break;
2466 view->focussed = 0;
2467 diff_view->focussed = 1;
2468 if (view_is_parent_view(view)) {
2469 err = view_close_child(view);
2470 if (err)
2471 return err;
2472 view_set_child(view, diff_view);
2473 view->focus_child = 1;
2474 } else
2475 *new_view = diff_view;
2476 break;
2477 case 't':
2478 if (s->selected_entry == NULL)
2479 break;
2480 if (view_is_parent_view(view))
2481 begin_x = view_split_begin_x(view->begin_x);
2482 err = browse_commit_tree(&tree_view, begin_x,
2483 s->selected_entry, s->in_repo_path, s->head_ref_name,
2484 s->repo);
2485 if (err)
2486 break;
2487 view->focussed = 0;
2488 tree_view->focussed = 1;
2489 if (view_is_parent_view(view)) {
2490 err = view_close_child(view);
2491 if (err)
2492 return err;
2493 view_set_child(view, tree_view);
2494 view->focus_child = 1;
2495 } else
2496 *new_view = tree_view;
2497 break;
2498 case KEY_BACKSPACE:
2499 case CTRL('l'):
2500 case 'B':
2501 if (ch == KEY_BACKSPACE &&
2502 got_path_is_root_dir(s->in_repo_path))
2503 break;
2504 err = stop_log_thread(s);
2505 if (err)
2506 return err;
2507 if (ch == KEY_BACKSPACE) {
2508 char *parent_path;
2509 err = got_path_dirname(&parent_path, s->in_repo_path);
2510 if (err)
2511 return err;
2512 free(s->in_repo_path);
2513 s->in_repo_path = parent_path;
2514 s->thread_args.in_repo_path = s->in_repo_path;
2515 } else if (ch == CTRL('l')) {
2516 struct got_object_id *start_id;
2517 err = got_repo_match_object_id(&start_id, NULL,
2518 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2519 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2520 if (err)
2521 return err;
2522 free(s->start_id);
2523 s->start_id = start_id;
2524 s->thread_args.start_id = s->start_id;
2525 } else /* 'B' */
2526 s->log_branches = !s->log_branches;
2528 err = got_repo_open(&s->thread_args.repo,
2529 got_repo_get_path(s->repo), NULL);
2530 if (err)
2531 return err;
2532 got_ref_list_free(&s->refs);
2533 err = got_ref_list(&s->refs, s->repo, NULL,
2534 got_ref_cmp_by_name, NULL);
2535 if (err)
2536 return err;
2537 err = got_commit_graph_open(&s->thread_args.graph,
2538 s->in_repo_path, !s->log_branches);
2539 if (err)
2540 return err;
2541 err = got_commit_graph_iter_start(s->thread_args.graph,
2542 s->start_id, s->repo, NULL, NULL);
2543 if (err)
2544 return err;
2545 free_commits(&s->commits);
2546 s->first_displayed_entry = NULL;
2547 s->last_displayed_entry = NULL;
2548 s->selected_entry = NULL;
2549 s->selected = 0;
2550 s->thread_args.log_complete = 0;
2551 s->quit = 0;
2552 s->thread_args.commits_needed = view->nlines;
2553 break;
2554 case 'r':
2555 if (view_is_parent_view(view))
2556 begin_x = view_split_begin_x(view->begin_x);
2557 ref_view = view_open(view->nlines, view->ncols,
2558 view->begin_y, begin_x, TOG_VIEW_REF);
2559 if (ref_view == NULL)
2560 return got_error_from_errno("view_open");
2561 err = open_ref_view(ref_view, s->repo);
2562 if (err) {
2563 view_close(ref_view);
2564 return err;
2566 view->focussed = 0;
2567 ref_view->focussed = 1;
2568 if (view_is_parent_view(view)) {
2569 err = view_close_child(view);
2570 if (err)
2571 return err;
2572 view_set_child(view, ref_view);
2573 view->focus_child = 1;
2574 } else
2575 *new_view = ref_view;
2576 break;
2577 default:
2578 break;
2581 return err;
2584 static const struct got_error *
2585 apply_unveil(const char *repo_path, const char *worktree_path)
2587 const struct got_error *error;
2589 #ifdef PROFILE
2590 if (unveil("gmon.out", "rwc") != 0)
2591 return got_error_from_errno2("unveil", "gmon.out");
2592 #endif
2593 if (repo_path && unveil(repo_path, "r") != 0)
2594 return got_error_from_errno2("unveil", repo_path);
2596 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2597 return got_error_from_errno2("unveil", worktree_path);
2599 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2600 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2602 error = got_privsep_unveil_exec_helpers();
2603 if (error != NULL)
2604 return error;
2606 if (unveil(NULL, NULL) != 0)
2607 return got_error_from_errno("unveil");
2609 return NULL;
2612 static void
2613 init_curses(void)
2615 initscr();
2616 cbreak();
2617 halfdelay(1); /* Do fast refresh while initial view is loading. */
2618 noecho();
2619 nonl();
2620 intrflush(stdscr, FALSE);
2621 keypad(stdscr, TRUE);
2622 curs_set(0);
2623 if (getenv("TOG_COLORS") != NULL) {
2624 start_color();
2625 use_default_colors();
2627 signal(SIGWINCH, tog_sigwinch);
2628 signal(SIGPIPE, tog_sigpipe);
2629 signal(SIGCONT, tog_sigcont);
2632 static const struct got_error *
2633 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2634 struct got_repository *repo, struct got_worktree *worktree)
2636 const struct got_error *err = NULL;
2638 if (argc == 0) {
2639 *in_repo_path = strdup("/");
2640 if (*in_repo_path == NULL)
2641 return got_error_from_errno("strdup");
2642 return NULL;
2645 if (worktree) {
2646 const char *prefix = got_worktree_get_path_prefix(worktree);
2647 char *p;
2649 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2650 if (err)
2651 return err;
2652 if (asprintf(in_repo_path, "%s%s%s", prefix,
2653 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2654 p) == -1) {
2655 err = got_error_from_errno("asprintf");
2656 *in_repo_path = NULL;
2658 free(p);
2659 } else
2660 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2662 return err;
2665 static const struct got_error *
2666 cmd_log(int argc, char *argv[])
2668 const struct got_error *error;
2669 struct got_repository *repo = NULL;
2670 struct got_worktree *worktree = NULL;
2671 struct got_object_id *start_id = NULL;
2672 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2673 char *start_commit = NULL, *label = NULL;
2674 struct got_reference *ref = NULL;
2675 const char *head_ref_name = NULL;
2676 int ch, log_branches = 0;
2677 struct tog_view *view;
2679 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2680 switch (ch) {
2681 case 'b':
2682 log_branches = 1;
2683 break;
2684 case 'c':
2685 start_commit = optarg;
2686 break;
2687 case 'r':
2688 repo_path = realpath(optarg, NULL);
2689 if (repo_path == NULL)
2690 return got_error_from_errno2("realpath",
2691 optarg);
2692 break;
2693 default:
2694 usage_log();
2695 /* NOTREACHED */
2699 argc -= optind;
2700 argv += optind;
2702 if (argc > 1)
2703 usage_log();
2705 if (repo_path == NULL) {
2706 cwd = getcwd(NULL, 0);
2707 if (cwd == NULL)
2708 return got_error_from_errno("getcwd");
2709 error = got_worktree_open(&worktree, cwd);
2710 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2711 goto done;
2712 if (worktree)
2713 repo_path =
2714 strdup(got_worktree_get_repo_path(worktree));
2715 else
2716 repo_path = strdup(cwd);
2717 if (repo_path == NULL) {
2718 error = got_error_from_errno("strdup");
2719 goto done;
2723 error = got_repo_open(&repo, repo_path, NULL);
2724 if (error != NULL)
2725 goto done;
2727 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2728 repo, worktree);
2729 if (error)
2730 goto done;
2732 init_curses();
2734 error = apply_unveil(got_repo_get_path(repo),
2735 worktree ? got_worktree_get_root_path(worktree) : NULL);
2736 if (error)
2737 goto done;
2739 if (start_commit == NULL) {
2740 error = got_repo_match_object_id(&start_id, &label,
2741 worktree ? got_worktree_get_head_ref_name(worktree) :
2742 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2743 if (error)
2744 goto done;
2745 head_ref_name = label;
2746 } else {
2747 error = got_ref_open(&ref, repo, start_commit, 0);
2748 if (error == NULL)
2749 head_ref_name = got_ref_get_name(ref);
2750 else if (error->code != GOT_ERR_NOT_REF)
2751 goto done;
2752 error = got_repo_match_object_id(&start_id, NULL,
2753 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2754 if (error)
2755 goto done;
2758 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2759 if (view == NULL) {
2760 error = got_error_from_errno("view_open");
2761 goto done;
2763 error = open_log_view(view, start_id, repo, head_ref_name,
2764 in_repo_path, log_branches);
2765 if (error)
2766 goto done;
2767 if (worktree) {
2768 /* Release work tree lock. */
2769 got_worktree_close(worktree);
2770 worktree = NULL;
2772 error = view_loop(view);
2773 done:
2774 free(in_repo_path);
2775 free(repo_path);
2776 free(cwd);
2777 free(start_id);
2778 free(label);
2779 if (ref)
2780 got_ref_close(ref);
2781 if (repo)
2782 got_repo_close(repo);
2783 if (worktree)
2784 got_worktree_close(worktree);
2785 return error;
2788 __dead static void
2789 usage_diff(void)
2791 endwin();
2792 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2793 "[-w] object1 object2\n", getprogname());
2794 exit(1);
2797 static int
2798 match_line(const char *line, regex_t *regex, size_t nmatch,
2799 regmatch_t *regmatch)
2801 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2804 struct tog_color *
2805 match_color(struct tog_colors *colors, const char *line)
2807 struct tog_color *tc = NULL;
2809 SIMPLEQ_FOREACH(tc, colors, entry) {
2810 if (match_line(line, &tc->regex, 0, NULL))
2811 return tc;
2814 return NULL;
2817 static const struct got_error *
2818 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2819 WINDOW *window, regmatch_t *regmatch)
2821 const struct got_error *err = NULL;
2822 wchar_t *wline;
2823 int width;
2824 char *s;
2826 *wtotal = 0;
2828 s = strndup(line, regmatch->rm_so);
2829 if (s == NULL)
2830 return got_error_from_errno("strndup");
2832 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2833 if (err) {
2834 free(s);
2835 return err;
2837 waddwstr(window, wline);
2838 free(wline);
2839 free(s);
2840 wlimit -= width;
2841 *wtotal += width;
2843 if (wlimit > 0) {
2844 s = strndup(line + regmatch->rm_so,
2845 regmatch->rm_eo - regmatch->rm_so);
2846 if (s == NULL) {
2847 err = got_error_from_errno("strndup");
2848 free(s);
2849 return err;
2851 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2852 if (err) {
2853 free(s);
2854 return err;
2856 wattr_on(window, A_STANDOUT, NULL);
2857 waddwstr(window, wline);
2858 wattr_off(window, A_STANDOUT, NULL);
2859 free(wline);
2860 free(s);
2861 wlimit -= width;
2862 *wtotal += width;
2865 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2866 err = format_line(&wline, &width,
2867 line + regmatch->rm_eo, wlimit, col_tab_align);
2868 if (err)
2869 return err;
2870 waddwstr(window, wline);
2871 free(wline);
2872 *wtotal += width;
2875 return NULL;
2878 static const struct got_error *
2879 draw_file(struct tog_view *view, const char *header)
2881 struct tog_diff_view_state *s = &view->state.diff;
2882 regmatch_t *regmatch = &view->regmatch;
2883 const struct got_error *err;
2884 int nprinted = 0;
2885 char *line;
2886 size_t linesize = 0;
2887 ssize_t linelen;
2888 struct tog_color *tc;
2889 wchar_t *wline;
2890 int width;
2891 int max_lines = view->nlines;
2892 int nlines = s->nlines;
2893 off_t line_offset;
2895 line_offset = s->line_offsets[s->first_displayed_line - 1];
2896 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2897 return got_error_from_errno("fseek");
2899 werase(view->window);
2901 if (header) {
2902 if (asprintf(&line, "[%d/%d] %s",
2903 s->first_displayed_line - 1 + s->selected_line, nlines,
2904 header) == -1)
2905 return got_error_from_errno("asprintf");
2906 err = format_line(&wline, &width, line, view->ncols, 0);
2907 free(line);
2908 if (err)
2909 return err;
2911 if (view_needs_focus_indication(view))
2912 wstandout(view->window);
2913 waddwstr(view->window, wline);
2914 free(wline);
2915 wline = NULL;
2916 if (view_needs_focus_indication(view))
2917 wstandend(view->window);
2918 if (width <= view->ncols - 1)
2919 waddch(view->window, '\n');
2921 if (max_lines <= 1)
2922 return NULL;
2923 max_lines--;
2926 s->eof = 0;
2927 line = NULL;
2928 while (max_lines > 0 && nprinted < max_lines) {
2929 linelen = getline(&line, &linesize, s->f);
2930 if (linelen == -1) {
2931 if (feof(s->f)) {
2932 s->eof = 1;
2933 break;
2935 free(line);
2936 return got_ferror(s->f, GOT_ERR_IO);
2939 tc = match_color(&s->colors, line);
2940 if (tc)
2941 wattr_on(view->window,
2942 COLOR_PAIR(tc->colorpair), NULL);
2943 if (s->first_displayed_line + nprinted == s->matched_line &&
2944 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2945 err = add_matched_line(&width, line, view->ncols, 0,
2946 view->window, regmatch);
2947 if (err) {
2948 free(line);
2949 return err;
2951 } else {
2952 err = format_line(&wline, &width, line, view->ncols, 0);
2953 if (err) {
2954 free(line);
2955 return err;
2957 waddwstr(view->window, wline);
2958 free(wline);
2959 wline = NULL;
2961 if (tc)
2962 wattr_off(view->window,
2963 COLOR_PAIR(tc->colorpair), NULL);
2964 if (width <= view->ncols - 1)
2965 waddch(view->window, '\n');
2966 nprinted++;
2968 free(line);
2969 if (nprinted >= 1)
2970 s->last_displayed_line = s->first_displayed_line +
2971 (nprinted - 1);
2972 else
2973 s->last_displayed_line = s->first_displayed_line;
2975 view_vborder(view);
2977 if (s->eof) {
2978 while (nprinted < view->nlines) {
2979 waddch(view->window, '\n');
2980 nprinted++;
2983 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2984 if (err) {
2985 return err;
2988 wstandout(view->window);
2989 waddwstr(view->window, wline);
2990 free(wline);
2991 wline = NULL;
2992 wstandend(view->window);
2995 return NULL;
2998 static char *
2999 get_datestr(time_t *time, char *datebuf)
3001 struct tm mytm, *tm;
3002 char *p, *s;
3004 tm = gmtime_r(time, &mytm);
3005 if (tm == NULL)
3006 return NULL;
3007 s = asctime_r(tm, datebuf);
3008 if (s == NULL)
3009 return NULL;
3010 p = strchr(s, '\n');
3011 if (p)
3012 *p = '\0';
3013 return s;
3016 static const struct got_error *
3017 get_changed_paths(struct got_pathlist_head *paths,
3018 struct got_commit_object *commit, struct got_repository *repo)
3020 const struct got_error *err = NULL;
3021 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3022 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3023 struct got_object_qid *qid;
3025 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3026 if (qid != NULL) {
3027 struct got_commit_object *pcommit;
3028 err = got_object_open_as_commit(&pcommit, repo,
3029 qid->id);
3030 if (err)
3031 return err;
3033 tree_id1 = got_object_commit_get_tree_id(pcommit);
3034 got_object_commit_close(pcommit);
3038 if (tree_id1) {
3039 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3040 if (err)
3041 goto done;
3044 tree_id2 = got_object_commit_get_tree_id(commit);
3045 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3046 if (err)
3047 goto done;
3049 err = got_diff_tree(tree1, tree2, "", "", repo,
3050 got_diff_tree_collect_changed_paths, paths, 0);
3051 done:
3052 if (tree1)
3053 got_object_tree_close(tree1);
3054 if (tree2)
3055 got_object_tree_close(tree2);
3056 return err;
3059 static const struct got_error *
3060 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3062 off_t *p;
3064 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3065 if (p == NULL)
3066 return got_error_from_errno("reallocarray");
3067 *line_offsets = p;
3068 (*line_offsets)[*nlines] = off;
3069 (*nlines)++;
3070 return NULL;
3073 static const struct got_error *
3074 write_commit_info(off_t **line_offsets, size_t *nlines,
3075 struct got_object_id *commit_id, struct got_reflist_head *refs,
3076 struct got_repository *repo, FILE *outfile)
3078 const struct got_error *err = NULL;
3079 char datebuf[26], *datestr;
3080 struct got_commit_object *commit;
3081 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3082 time_t committer_time;
3083 const char *author, *committer;
3084 char *refs_str = NULL;
3085 struct got_pathlist_head changed_paths;
3086 struct got_pathlist_entry *pe;
3087 off_t outoff = 0;
3088 int n;
3090 TAILQ_INIT(&changed_paths);
3092 if (refs) {
3093 err = build_refs_str(&refs_str, refs, commit_id, repo);
3094 if (err)
3095 return err;
3098 err = got_object_open_as_commit(&commit, repo, commit_id);
3099 if (err)
3100 return err;
3102 err = got_object_id_str(&id_str, commit_id);
3103 if (err) {
3104 err = got_error_from_errno("got_object_id_str");
3105 goto done;
3108 err = add_line_offset(line_offsets, nlines, 0);
3109 if (err)
3110 goto done;
3112 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3113 refs_str ? refs_str : "", refs_str ? ")" : "");
3114 if (n < 0) {
3115 err = got_error_from_errno("fprintf");
3116 goto done;
3118 outoff += n;
3119 err = add_line_offset(line_offsets, nlines, outoff);
3120 if (err)
3121 goto done;
3123 n = fprintf(outfile, "from: %s\n",
3124 got_object_commit_get_author(commit));
3125 if (n < 0) {
3126 err = got_error_from_errno("fprintf");
3127 goto done;
3129 outoff += n;
3130 err = add_line_offset(line_offsets, nlines, outoff);
3131 if (err)
3132 goto done;
3134 committer_time = got_object_commit_get_committer_time(commit);
3135 datestr = get_datestr(&committer_time, datebuf);
3136 if (datestr) {
3137 n = fprintf(outfile, "date: %s UTC\n", datestr);
3138 if (n < 0) {
3139 err = got_error_from_errno("fprintf");
3140 goto done;
3142 outoff += n;
3143 err = add_line_offset(line_offsets, nlines, outoff);
3144 if (err)
3145 goto done;
3147 author = got_object_commit_get_author(commit);
3148 committer = got_object_commit_get_committer(commit);
3149 if (strcmp(author, committer) != 0) {
3150 n = fprintf(outfile, "via: %s\n", committer);
3151 if (n < 0) {
3152 err = got_error_from_errno("fprintf");
3153 goto done;
3155 outoff += n;
3156 err = add_line_offset(line_offsets, nlines, outoff);
3157 if (err)
3158 goto done;
3160 err = got_object_commit_get_logmsg(&logmsg, commit);
3161 if (err)
3162 goto done;
3163 s = logmsg;
3164 while ((line = strsep(&s, "\n")) != NULL) {
3165 n = fprintf(outfile, "%s\n", line);
3166 if (n < 0) {
3167 err = got_error_from_errno("fprintf");
3168 goto done;
3170 outoff += n;
3171 err = add_line_offset(line_offsets, nlines, outoff);
3172 if (err)
3173 goto done;
3176 err = get_changed_paths(&changed_paths, commit, repo);
3177 if (err)
3178 goto done;
3179 TAILQ_FOREACH(pe, &changed_paths, entry) {
3180 struct got_diff_changed_path *cp = pe->data;
3181 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3182 if (n < 0) {
3183 err = got_error_from_errno("fprintf");
3184 goto done;
3186 outoff += n;
3187 err = add_line_offset(line_offsets, nlines, outoff);
3188 if (err)
3189 goto done;
3190 free((char *)pe->path);
3191 free(pe->data);
3194 fputc('\n', outfile);
3195 outoff++;
3196 err = add_line_offset(line_offsets, nlines, outoff);
3197 done:
3198 got_pathlist_free(&changed_paths);
3199 free(id_str);
3200 free(logmsg);
3201 free(refs_str);
3202 got_object_commit_close(commit);
3203 if (err) {
3204 free(*line_offsets);
3205 *line_offsets = NULL;
3206 *nlines = 0;
3208 return err;
3211 static const struct got_error *
3212 create_diff(struct tog_diff_view_state *s)
3214 const struct got_error *err = NULL;
3215 FILE *f = NULL;
3216 int obj_type;
3218 free(s->line_offsets);
3219 s->line_offsets = malloc(sizeof(off_t));
3220 if (s->line_offsets == NULL)
3221 return got_error_from_errno("malloc");
3222 s->nlines = 0;
3224 f = got_opentemp();
3225 if (f == NULL) {
3226 err = got_error_from_errno("got_opentemp");
3227 goto done;
3229 if (s->f && fclose(s->f) != 0) {
3230 err = got_error_from_errno("fclose");
3231 goto done;
3233 s->f = f;
3235 if (s->id1)
3236 err = got_object_get_type(&obj_type, s->repo, s->id1);
3237 else
3238 err = got_object_get_type(&obj_type, s->repo, s->id2);
3239 if (err)
3240 goto done;
3242 switch (obj_type) {
3243 case GOT_OBJ_TYPE_BLOB:
3244 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3245 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3246 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3247 break;
3248 case GOT_OBJ_TYPE_TREE:
3249 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3250 s->id1, s->id2, "", "", s->diff_context,
3251 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3252 break;
3253 case GOT_OBJ_TYPE_COMMIT: {
3254 const struct got_object_id_queue *parent_ids;
3255 struct got_object_qid *pid;
3256 struct got_commit_object *commit2;
3258 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3259 if (err)
3260 goto done;
3261 /* Show commit info if we're diffing to a parent/root commit. */
3262 if (s->id1 == NULL) {
3263 err = write_commit_info(&s->line_offsets, &s->nlines,
3264 s->id2, &s->refs, s->repo, s->f);
3265 if (err)
3266 goto done;
3267 } else {
3268 parent_ids = got_object_commit_get_parent_ids(commit2);
3269 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3270 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3271 err = write_commit_info(
3272 &s->line_offsets, &s->nlines,
3273 s->id2, &s->refs, s->repo, s->f);
3274 if (err)
3275 goto done;
3276 break;
3280 got_object_commit_close(commit2);
3282 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3283 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3284 s->force_text_diff, s->repo, s->f);
3285 break;
3287 default:
3288 err = got_error(GOT_ERR_OBJ_TYPE);
3289 break;
3291 if (err)
3292 goto done;
3293 done:
3294 if (s->f && fflush(s->f) != 0 && err == NULL)
3295 err = got_error_from_errno("fflush");
3296 return err;
3299 static void
3300 diff_view_indicate_progress(struct tog_view *view)
3302 mvwaddstr(view->window, 0, 0, "diffing...");
3303 update_panels();
3304 doupdate();
3307 static const struct got_error *
3308 search_start_diff_view(struct tog_view *view)
3310 struct tog_diff_view_state *s = &view->state.diff;
3312 s->matched_line = 0;
3313 return NULL;
3316 static const struct got_error *
3317 search_next_diff_view(struct tog_view *view)
3319 struct tog_diff_view_state *s = &view->state.diff;
3320 int lineno;
3321 char *line = NULL;
3322 size_t linesize = 0;
3323 ssize_t linelen;
3325 if (!view->searching) {
3326 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3327 return NULL;
3330 if (s->matched_line) {
3331 if (view->searching == TOG_SEARCH_FORWARD)
3332 lineno = s->matched_line + 1;
3333 else
3334 lineno = s->matched_line - 1;
3335 } else {
3336 if (view->searching == TOG_SEARCH_FORWARD)
3337 lineno = 1;
3338 else
3339 lineno = s->nlines;
3342 while (1) {
3343 off_t offset;
3345 if (lineno <= 0 || lineno > s->nlines) {
3346 if (s->matched_line == 0) {
3347 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3348 break;
3351 if (view->searching == TOG_SEARCH_FORWARD)
3352 lineno = 1;
3353 else
3354 lineno = s->nlines;
3357 offset = s->line_offsets[lineno - 1];
3358 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3359 free(line);
3360 return got_error_from_errno("fseeko");
3362 linelen = getline(&line, &linesize, s->f);
3363 if (linelen != -1 &&
3364 match_line(line, &view->regex, 1, &view->regmatch)) {
3365 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3366 s->matched_line = lineno;
3367 break;
3369 if (view->searching == TOG_SEARCH_FORWARD)
3370 lineno++;
3371 else
3372 lineno--;
3374 free(line);
3376 if (s->matched_line) {
3377 s->first_displayed_line = s->matched_line;
3378 s->selected_line = 1;
3381 return NULL;
3384 static const struct got_error *
3385 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3386 struct got_object_id *id2, const char *label1, const char *label2,
3387 int diff_context, int ignore_whitespace, int force_text_diff,
3388 struct tog_view *log_view, struct got_repository *repo)
3390 const struct got_error *err;
3391 struct tog_diff_view_state *s = &view->state.diff;
3393 SIMPLEQ_INIT(&s->refs);
3395 if (id1 != NULL && id2 != NULL) {
3396 int type1, type2;
3397 err = got_object_get_type(&type1, repo, id1);
3398 if (err)
3399 return err;
3400 err = got_object_get_type(&type2, repo, id2);
3401 if (err)
3402 return err;
3404 if (type1 != type2)
3405 return got_error(GOT_ERR_OBJ_TYPE);
3407 s->first_displayed_line = 1;
3408 s->last_displayed_line = view->nlines;
3409 s->selected_line = 1;
3410 s->repo = repo;
3411 s->id1 = id1;
3412 s->id2 = id2;
3413 s->label1 = label1;
3414 s->label2 = label2;
3416 if (id1) {
3417 s->id1 = got_object_id_dup(id1);
3418 if (s->id1 == NULL)
3419 return got_error_from_errno("got_object_id_dup");
3420 } else
3421 s->id1 = NULL;
3423 s->id2 = got_object_id_dup(id2);
3424 if (s->id2 == NULL) {
3425 free(s->id1);
3426 s->id1 = NULL;
3427 return got_error_from_errno("got_object_id_dup");
3429 s->f = NULL;
3430 s->first_displayed_line = 1;
3431 s->last_displayed_line = view->nlines;
3432 s->diff_context = diff_context;
3433 s->ignore_whitespace = ignore_whitespace;
3434 s->force_text_diff = force_text_diff;
3435 s->log_view = log_view;
3436 s->repo = repo;
3438 SIMPLEQ_INIT(&s->colors);
3439 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3440 err = add_color(&s->colors,
3441 "^-", TOG_COLOR_DIFF_MINUS,
3442 get_color_value("TOG_COLOR_DIFF_MINUS"));
3443 if (err)
3444 return err;
3445 err = add_color(&s->colors, "^\\+",
3446 TOG_COLOR_DIFF_PLUS,
3447 get_color_value("TOG_COLOR_DIFF_PLUS"));
3448 if (err) {
3449 free_colors(&s->colors);
3450 return err;
3452 err = add_color(&s->colors,
3453 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3454 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3455 if (err) {
3456 free_colors(&s->colors);
3457 return err;
3460 err = add_color(&s->colors,
3461 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3462 TOG_COLOR_DIFF_META,
3463 get_color_value("TOG_COLOR_DIFF_META"));
3464 if (err) {
3465 free_colors(&s->colors);
3466 return err;
3469 err = add_color(&s->colors,
3470 "^(from|via): ", TOG_COLOR_AUTHOR,
3471 get_color_value("TOG_COLOR_AUTHOR"));
3472 if (err) {
3473 free_colors(&s->colors);
3474 return err;
3477 err = add_color(&s->colors,
3478 "^date: ", TOG_COLOR_DATE,
3479 get_color_value("TOG_COLOR_DATE"));
3480 if (err) {
3481 free_colors(&s->colors);
3482 return err;
3486 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3487 if (err) {
3488 free(s->id1);
3489 s->id1 = NULL;
3490 free(s->id2);
3491 s->id2 = NULL;
3492 free_colors(&s->colors);
3493 return err;
3496 if (log_view && view_is_splitscreen(view))
3497 show_log_view(log_view); /* draw vborder */
3498 diff_view_indicate_progress(view);
3500 s->line_offsets = NULL;
3501 s->nlines = 0;
3502 err = create_diff(s);
3503 if (err) {
3504 free(s->id1);
3505 s->id1 = NULL;
3506 free(s->id2);
3507 s->id2 = NULL;
3508 free_colors(&s->colors);
3509 got_ref_list_free(&s->refs);
3510 return err;
3513 view->show = show_diff_view;
3514 view->input = input_diff_view;
3515 view->close = close_diff_view;
3516 view->search_start = search_start_diff_view;
3517 view->search_next = search_next_diff_view;
3519 return NULL;
3522 static const struct got_error *
3523 close_diff_view(struct tog_view *view)
3525 const struct got_error *err = NULL;
3526 struct tog_diff_view_state *s = &view->state.diff;
3528 free(s->id1);
3529 s->id1 = NULL;
3530 free(s->id2);
3531 s->id2 = NULL;
3532 if (s->f && fclose(s->f) == EOF)
3533 err = got_error_from_errno("fclose");
3534 free_colors(&s->colors);
3535 free(s->line_offsets);
3536 s->line_offsets = NULL;
3537 s->nlines = 0;
3538 got_ref_list_free(&s->refs);
3539 return err;
3542 static const struct got_error *
3543 show_diff_view(struct tog_view *view)
3545 const struct got_error *err;
3546 struct tog_diff_view_state *s = &view->state.diff;
3547 char *id_str1 = NULL, *id_str2, *header;
3548 const char *label1, *label2;
3550 if (s->id1) {
3551 err = got_object_id_str(&id_str1, s->id1);
3552 if (err)
3553 return err;
3554 label1 = s->label1 ? : id_str1;
3555 } else
3556 label1 = "/dev/null";
3558 err = got_object_id_str(&id_str2, s->id2);
3559 if (err)
3560 return err;
3561 label2 = s->label2 ? : id_str2;
3563 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3564 err = got_error_from_errno("asprintf");
3565 free(id_str1);
3566 free(id_str2);
3567 return err;
3569 free(id_str1);
3570 free(id_str2);
3572 return draw_file(view, header);
3575 static const struct got_error *
3576 set_selected_commit(struct tog_diff_view_state *s,
3577 struct commit_queue_entry *entry)
3579 const struct got_error *err;
3580 const struct got_object_id_queue *parent_ids;
3581 struct got_commit_object *selected_commit;
3582 struct got_object_qid *pid;
3584 free(s->id2);
3585 s->id2 = got_object_id_dup(entry->id);
3586 if (s->id2 == NULL)
3587 return got_error_from_errno("got_object_id_dup");
3589 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3590 if (err)
3591 return err;
3592 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3593 free(s->id1);
3594 pid = SIMPLEQ_FIRST(parent_ids);
3595 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3596 got_object_commit_close(selected_commit);
3597 return NULL;
3600 static const struct got_error *
3601 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3603 const struct got_error *err = NULL;
3604 struct tog_diff_view_state *s = &view->state.diff;
3605 struct tog_log_view_state *ls;
3606 struct commit_queue_entry *old_selected_entry;
3607 char *line = NULL;
3608 size_t linesize = 0;
3609 ssize_t linelen;
3610 int i;
3612 switch (ch) {
3613 case 'a':
3614 case 'w':
3615 if (ch == 'a')
3616 s->force_text_diff = !s->force_text_diff;
3617 if (ch == 'w')
3618 s->ignore_whitespace = !s->ignore_whitespace;
3619 wclear(view->window);
3620 s->first_displayed_line = 1;
3621 s->last_displayed_line = view->nlines;
3622 diff_view_indicate_progress(view);
3623 err = create_diff(s);
3624 break;
3625 case 'k':
3626 case KEY_UP:
3627 if (s->first_displayed_line > 1)
3628 s->first_displayed_line--;
3629 break;
3630 case KEY_PPAGE:
3631 case CTRL('b'):
3632 if (s->first_displayed_line == 1)
3633 break;
3634 i = 0;
3635 while (i++ < view->nlines - 1 &&
3636 s->first_displayed_line > 1)
3637 s->first_displayed_line--;
3638 break;
3639 case 'j':
3640 case KEY_DOWN:
3641 if (!s->eof)
3642 s->first_displayed_line++;
3643 break;
3644 case KEY_NPAGE:
3645 case CTRL('f'):
3646 case ' ':
3647 if (s->eof)
3648 break;
3649 i = 0;
3650 while (!s->eof && i++ < view->nlines - 1) {
3651 linelen = getline(&line, &linesize, s->f);
3652 s->first_displayed_line++;
3653 if (linelen == -1) {
3654 if (feof(s->f)) {
3655 s->eof = 1;
3656 } else
3657 err = got_ferror(s->f, GOT_ERR_IO);
3658 break;
3661 free(line);
3662 break;
3663 case '[':
3664 if (s->diff_context > 0) {
3665 s->diff_context--;
3666 diff_view_indicate_progress(view);
3667 err = create_diff(s);
3668 if (s->first_displayed_line + view->nlines - 1 >
3669 s->nlines) {
3670 s->first_displayed_line = 1;
3671 s->last_displayed_line = view->nlines;
3674 break;
3675 case ']':
3676 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3677 s->diff_context++;
3678 diff_view_indicate_progress(view);
3679 err = create_diff(s);
3681 break;
3682 case '<':
3683 case ',':
3684 if (s->log_view == NULL)
3685 break;
3686 ls = &s->log_view->state.log;
3687 old_selected_entry = ls->selected_entry;
3689 err = input_log_view(NULL, s->log_view, KEY_UP);
3690 if (err)
3691 break;
3693 if (old_selected_entry == ls->selected_entry)
3694 break;
3696 err = set_selected_commit(s, ls->selected_entry);
3697 if (err)
3698 break;
3700 s->first_displayed_line = 1;
3701 s->last_displayed_line = view->nlines;
3703 diff_view_indicate_progress(view);
3704 err = create_diff(s);
3705 break;
3706 case '>':
3707 case '.':
3708 if (s->log_view == NULL)
3709 break;
3710 ls = &s->log_view->state.log;
3711 old_selected_entry = ls->selected_entry;
3713 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3714 if (err)
3715 break;
3717 if (old_selected_entry == ls->selected_entry)
3718 break;
3720 err = set_selected_commit(s, ls->selected_entry);
3721 if (err)
3722 break;
3724 s->first_displayed_line = 1;
3725 s->last_displayed_line = view->nlines;
3727 diff_view_indicate_progress(view);
3728 err = create_diff(s);
3729 break;
3730 default:
3731 break;
3734 return err;
3737 static const struct got_error *
3738 cmd_diff(int argc, char *argv[])
3740 const struct got_error *error = NULL;
3741 struct got_repository *repo = NULL;
3742 struct got_worktree *worktree = NULL;
3743 struct got_object_id *id1 = NULL, *id2 = NULL;
3744 char *repo_path = NULL, *cwd = NULL;
3745 char *id_str1 = NULL, *id_str2 = NULL;
3746 char *label1 = NULL, *label2 = NULL;
3747 int diff_context = 3, ignore_whitespace = 0;
3748 int ch, force_text_diff = 0;
3749 const char *errstr;
3750 struct tog_view *view;
3752 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3753 switch (ch) {
3754 case 'a':
3755 force_text_diff = 1;
3756 break;
3757 case 'C':
3758 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3759 &errstr);
3760 if (errstr != NULL)
3761 err(1, "-C option %s", errstr);
3762 break;
3763 case 'r':
3764 repo_path = realpath(optarg, NULL);
3765 if (repo_path == NULL)
3766 return got_error_from_errno2("realpath",
3767 optarg);
3768 got_path_strip_trailing_slashes(repo_path);
3769 break;
3770 case 'w':
3771 ignore_whitespace = 1;
3772 break;
3773 default:
3774 usage_diff();
3775 /* NOTREACHED */
3779 argc -= optind;
3780 argv += optind;
3782 if (argc == 0) {
3783 usage_diff(); /* TODO show local worktree changes */
3784 } else if (argc == 2) {
3785 id_str1 = argv[0];
3786 id_str2 = argv[1];
3787 } else
3788 usage_diff();
3790 if (repo_path == NULL) {
3791 cwd = getcwd(NULL, 0);
3792 if (cwd == NULL)
3793 return got_error_from_errno("getcwd");
3794 error = got_worktree_open(&worktree, cwd);
3795 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3796 goto done;
3797 if (worktree)
3798 repo_path =
3799 strdup(got_worktree_get_repo_path(worktree));
3800 else
3801 repo_path = strdup(cwd);
3802 if (repo_path == NULL) {
3803 error = got_error_from_errno("strdup");
3804 goto done;
3808 error = got_repo_open(&repo, repo_path, NULL);
3809 if (error)
3810 goto done;
3812 init_curses();
3814 error = apply_unveil(got_repo_get_path(repo), NULL);
3815 if (error)
3816 goto done;
3818 error = got_repo_match_object_id(&id1, &label1, id_str1,
3819 GOT_OBJ_TYPE_ANY, 1, repo);
3820 if (error)
3821 goto done;
3823 error = got_repo_match_object_id(&id2, &label2, id_str2,
3824 GOT_OBJ_TYPE_ANY, 1, repo);
3825 if (error)
3826 goto done;
3828 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3829 if (view == NULL) {
3830 error = got_error_from_errno("view_open");
3831 goto done;
3833 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3834 ignore_whitespace, force_text_diff, NULL, repo);
3835 if (error)
3836 goto done;
3837 error = view_loop(view);
3838 done:
3839 free(label1);
3840 free(label2);
3841 free(repo_path);
3842 free(cwd);
3843 if (repo)
3844 got_repo_close(repo);
3845 if (worktree)
3846 got_worktree_close(worktree);
3847 return error;
3850 __dead static void
3851 usage_blame(void)
3853 endwin();
3854 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3855 getprogname());
3856 exit(1);
3859 struct tog_blame_line {
3860 int annotated;
3861 struct got_object_id *id;
3864 static const struct got_error *
3865 draw_blame(struct tog_view *view)
3867 struct tog_blame_view_state *s = &view->state.blame;
3868 struct tog_blame *blame = &s->blame;
3869 regmatch_t *regmatch = &view->regmatch;
3870 const struct got_error *err;
3871 int lineno = 0, nprinted = 0;
3872 char *line = NULL;
3873 size_t linesize = 0;
3874 ssize_t linelen;
3875 wchar_t *wline;
3876 int width;
3877 struct tog_blame_line *blame_line;
3878 struct got_object_id *prev_id = NULL;
3879 char *id_str;
3880 struct tog_color *tc;
3882 err = got_object_id_str(&id_str, s->blamed_commit->id);
3883 if (err)
3884 return err;
3886 rewind(blame->f);
3887 werase(view->window);
3889 if (asprintf(&line, "commit %s", id_str) == -1) {
3890 err = got_error_from_errno("asprintf");
3891 free(id_str);
3892 return err;
3895 err = format_line(&wline, &width, line, view->ncols, 0);
3896 free(line);
3897 line = NULL;
3898 if (err)
3899 return err;
3900 if (view_needs_focus_indication(view))
3901 wstandout(view->window);
3902 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3903 if (tc)
3904 wattr_on(view->window,
3905 COLOR_PAIR(tc->colorpair), NULL);
3906 waddwstr(view->window, wline);
3907 if (tc)
3908 wattr_off(view->window,
3909 COLOR_PAIR(tc->colorpair), NULL);
3910 if (view_needs_focus_indication(view))
3911 wstandend(view->window);
3912 free(wline);
3913 wline = NULL;
3914 if (width < view->ncols - 1)
3915 waddch(view->window, '\n');
3917 if (asprintf(&line, "[%d/%d] %s%s",
3918 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3919 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3920 free(id_str);
3921 return got_error_from_errno("asprintf");
3923 free(id_str);
3924 err = format_line(&wline, &width, line, view->ncols, 0);
3925 free(line);
3926 line = NULL;
3927 if (err)
3928 return err;
3929 waddwstr(view->window, wline);
3930 free(wline);
3931 wline = NULL;
3932 if (width < view->ncols - 1)
3933 waddch(view->window, '\n');
3935 s->eof = 0;
3936 while (nprinted < view->nlines - 2) {
3937 linelen = getline(&line, &linesize, blame->f);
3938 if (linelen == -1) {
3939 if (feof(blame->f)) {
3940 s->eof = 1;
3941 break;
3943 free(line);
3944 return got_ferror(blame->f, GOT_ERR_IO);
3946 if (++lineno < s->first_displayed_line)
3947 continue;
3949 if (view->focussed && nprinted == s->selected_line - 1)
3950 wstandout(view->window);
3952 if (blame->nlines > 0) {
3953 blame_line = &blame->lines[lineno - 1];
3954 if (blame_line->annotated && prev_id &&
3955 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3956 !(view->focussed &&
3957 nprinted == s->selected_line - 1)) {
3958 waddstr(view->window, " ");
3959 } else if (blame_line->annotated) {
3960 char *id_str;
3961 err = got_object_id_str(&id_str, blame_line->id);
3962 if (err) {
3963 free(line);
3964 return err;
3966 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3967 if (tc)
3968 wattr_on(view->window,
3969 COLOR_PAIR(tc->colorpair), NULL);
3970 wprintw(view->window, "%.8s", id_str);
3971 if (tc)
3972 wattr_off(view->window,
3973 COLOR_PAIR(tc->colorpair), NULL);
3974 free(id_str);
3975 prev_id = blame_line->id;
3976 } else {
3977 waddstr(view->window, "........");
3978 prev_id = NULL;
3980 } else {
3981 waddstr(view->window, "........");
3982 prev_id = NULL;
3985 if (view->focussed && nprinted == s->selected_line - 1)
3986 wstandend(view->window);
3987 waddstr(view->window, " ");
3989 if (view->ncols <= 9) {
3990 width = 9;
3991 wline = wcsdup(L"");
3992 if (wline == NULL) {
3993 err = got_error_from_errno("wcsdup");
3994 free(line);
3995 return err;
3997 } else if (s->first_displayed_line + nprinted ==
3998 s->matched_line &&
3999 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4000 err = add_matched_line(&width, line, view->ncols - 9, 9,
4001 view->window, regmatch);
4002 if (err) {
4003 free(line);
4004 return err;
4006 width += 9;
4007 } else {
4008 err = format_line(&wline, &width, line,
4009 view->ncols - 9, 9);
4010 waddwstr(view->window, wline);
4011 free(wline);
4012 wline = NULL;
4013 width += 9;
4016 if (width <= view->ncols - 1)
4017 waddch(view->window, '\n');
4018 if (++nprinted == 1)
4019 s->first_displayed_line = lineno;
4021 free(line);
4022 s->last_displayed_line = lineno;
4024 view_vborder(view);
4026 return NULL;
4029 static const struct got_error *
4030 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4032 const struct got_error *err = NULL;
4033 struct tog_blame_cb_args *a = arg;
4034 struct tog_blame_line *line;
4035 int errcode;
4037 if (nlines != a->nlines ||
4038 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4039 return got_error(GOT_ERR_RANGE);
4041 errcode = pthread_mutex_lock(&tog_mutex);
4042 if (errcode)
4043 return got_error_set_errno(errcode, "pthread_mutex_lock");
4045 if (*a->quit) { /* user has quit the blame view */
4046 err = got_error(GOT_ERR_ITER_COMPLETED);
4047 goto done;
4050 if (lineno == -1)
4051 goto done; /* no change in this commit */
4053 line = &a->lines[lineno - 1];
4054 if (line->annotated)
4055 goto done;
4057 line->id = got_object_id_dup(id);
4058 if (line->id == NULL) {
4059 err = got_error_from_errno("got_object_id_dup");
4060 goto done;
4062 line->annotated = 1;
4063 done:
4064 errcode = pthread_mutex_unlock(&tog_mutex);
4065 if (errcode)
4066 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4067 return err;
4070 static void *
4071 blame_thread(void *arg)
4073 const struct got_error *err;
4074 struct tog_blame_thread_args *ta = arg;
4075 struct tog_blame_cb_args *a = ta->cb_args;
4076 int errcode;
4078 err = block_signals_used_by_main_thread();
4079 if (err)
4080 return (void *)err;
4082 err = got_blame(ta->path, a->commit_id, ta->repo,
4083 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4084 if (err && err->code == GOT_ERR_CANCELLED)
4085 err = NULL;
4087 errcode = pthread_mutex_lock(&tog_mutex);
4088 if (errcode)
4089 return (void *)got_error_set_errno(errcode,
4090 "pthread_mutex_lock");
4092 got_repo_close(ta->repo);
4093 ta->repo = NULL;
4094 *ta->complete = 1;
4096 errcode = pthread_mutex_unlock(&tog_mutex);
4097 if (errcode && err == NULL)
4098 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4100 return (void *)err;
4103 static struct got_object_id *
4104 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4105 int first_displayed_line, int selected_line)
4107 struct tog_blame_line *line;
4109 if (nlines <= 0)
4110 return NULL;
4112 line = &lines[first_displayed_line - 1 + selected_line - 1];
4113 if (!line->annotated)
4114 return NULL;
4116 return line->id;
4119 static const struct got_error *
4120 stop_blame(struct tog_blame *blame)
4122 const struct got_error *err = NULL;
4123 int i;
4125 if (blame->thread) {
4126 int errcode;
4127 errcode = pthread_mutex_unlock(&tog_mutex);
4128 if (errcode)
4129 return got_error_set_errno(errcode,
4130 "pthread_mutex_unlock");
4131 errcode = pthread_join(blame->thread, (void **)&err);
4132 if (errcode)
4133 return got_error_set_errno(errcode, "pthread_join");
4134 errcode = pthread_mutex_lock(&tog_mutex);
4135 if (errcode)
4136 return got_error_set_errno(errcode,
4137 "pthread_mutex_lock");
4138 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4139 err = NULL;
4140 blame->thread = NULL;
4142 if (blame->thread_args.repo) {
4143 got_repo_close(blame->thread_args.repo);
4144 blame->thread_args.repo = NULL;
4146 if (blame->f) {
4147 if (fclose(blame->f) != 0 && err == NULL)
4148 err = got_error_from_errno("fclose");
4149 blame->f = NULL;
4151 if (blame->lines) {
4152 for (i = 0; i < blame->nlines; i++)
4153 free(blame->lines[i].id);
4154 free(blame->lines);
4155 blame->lines = NULL;
4157 free(blame->cb_args.commit_id);
4158 blame->cb_args.commit_id = NULL;
4160 return err;
4163 static const struct got_error *
4164 cancel_blame_view(void *arg)
4166 const struct got_error *err = NULL;
4167 int *done = arg;
4168 int errcode;
4170 errcode = pthread_mutex_lock(&tog_mutex);
4171 if (errcode)
4172 return got_error_set_errno(errcode,
4173 "pthread_mutex_unlock");
4175 if (*done)
4176 err = got_error(GOT_ERR_CANCELLED);
4178 errcode = pthread_mutex_unlock(&tog_mutex);
4179 if (errcode)
4180 return got_error_set_errno(errcode,
4181 "pthread_mutex_lock");
4183 return err;
4186 static const struct got_error *
4187 run_blame(struct tog_view *view)
4189 struct tog_blame_view_state *s = &view->state.blame;
4190 struct tog_blame *blame = &s->blame;
4191 const struct got_error *err = NULL;
4192 struct got_blob_object *blob = NULL;
4193 struct got_repository *thread_repo = NULL;
4194 struct got_object_id *obj_id = NULL;
4195 int obj_type;
4197 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4198 s->path);
4199 if (err)
4200 return err;
4202 err = got_object_get_type(&obj_type, s->repo, obj_id);
4203 if (err)
4204 goto done;
4206 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4207 err = got_error(GOT_ERR_OBJ_TYPE);
4208 goto done;
4211 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4212 if (err)
4213 goto done;
4214 blame->f = got_opentemp();
4215 if (blame->f == NULL) {
4216 err = got_error_from_errno("got_opentemp");
4217 goto done;
4219 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4220 &blame->line_offsets, blame->f, blob);
4221 if (err || blame->nlines == 0)
4222 goto done;
4224 /* Don't include \n at EOF in the blame line count. */
4225 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4226 blame->nlines--;
4228 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4229 if (blame->lines == NULL) {
4230 err = got_error_from_errno("calloc");
4231 goto done;
4234 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4235 if (err)
4236 goto done;
4238 blame->cb_args.view = view;
4239 blame->cb_args.lines = blame->lines;
4240 blame->cb_args.nlines = blame->nlines;
4241 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4242 if (blame->cb_args.commit_id == NULL) {
4243 err = got_error_from_errno("got_object_id_dup");
4244 goto done;
4246 blame->cb_args.quit = &s->done;
4248 blame->thread_args.path = s->path;
4249 blame->thread_args.repo = thread_repo;
4250 blame->thread_args.cb_args = &blame->cb_args;
4251 blame->thread_args.complete = &s->blame_complete;
4252 blame->thread_args.cancel_cb = cancel_blame_view;
4253 blame->thread_args.cancel_arg = &s->done;
4254 s->blame_complete = 0;
4256 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4257 s->first_displayed_line = 1;
4258 s->last_displayed_line = view->nlines;
4259 s->selected_line = 1;
4262 done:
4263 if (blob)
4264 got_object_blob_close(blob);
4265 free(obj_id);
4266 if (err)
4267 stop_blame(blame);
4268 return err;
4271 static const struct got_error *
4272 open_blame_view(struct tog_view *view, char *path,
4273 struct got_object_id *commit_id, struct got_repository *repo)
4275 const struct got_error *err = NULL;
4276 struct tog_blame_view_state *s = &view->state.blame;
4278 SIMPLEQ_INIT(&s->blamed_commits);
4280 s->path = strdup(path);
4281 if (s->path == NULL)
4282 return got_error_from_errno("strdup");
4284 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4285 if (err) {
4286 free(s->path);
4287 return err;
4290 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4291 s->first_displayed_line = 1;
4292 s->last_displayed_line = view->nlines;
4293 s->selected_line = 1;
4294 s->blame_complete = 0;
4295 s->repo = repo;
4296 s->commit_id = commit_id;
4297 memset(&s->blame, 0, sizeof(s->blame));
4299 SIMPLEQ_INIT(&s->colors);
4300 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4301 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4302 get_color_value("TOG_COLOR_COMMIT"));
4303 if (err)
4304 return err;
4307 view->show = show_blame_view;
4308 view->input = input_blame_view;
4309 view->close = close_blame_view;
4310 view->search_start = search_start_blame_view;
4311 view->search_next = search_next_blame_view;
4313 return run_blame(view);
4316 static const struct got_error *
4317 close_blame_view(struct tog_view *view)
4319 const struct got_error *err = NULL;
4320 struct tog_blame_view_state *s = &view->state.blame;
4322 if (s->blame.thread)
4323 err = stop_blame(&s->blame);
4325 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4326 struct got_object_qid *blamed_commit;
4327 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4328 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4329 got_object_qid_free(blamed_commit);
4332 free(s->path);
4333 free_colors(&s->colors);
4335 return err;
4338 static const struct got_error *
4339 search_start_blame_view(struct tog_view *view)
4341 struct tog_blame_view_state *s = &view->state.blame;
4343 s->matched_line = 0;
4344 return NULL;
4347 static const struct got_error *
4348 search_next_blame_view(struct tog_view *view)
4350 struct tog_blame_view_state *s = &view->state.blame;
4351 int lineno;
4352 char *line = NULL;
4353 size_t linesize = 0;
4354 ssize_t linelen;
4356 if (!view->searching) {
4357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4358 return NULL;
4361 if (s->matched_line) {
4362 if (view->searching == TOG_SEARCH_FORWARD)
4363 lineno = s->matched_line + 1;
4364 else
4365 lineno = s->matched_line - 1;
4366 } else {
4367 if (view->searching == TOG_SEARCH_FORWARD)
4368 lineno = 1;
4369 else
4370 lineno = s->blame.nlines;
4373 while (1) {
4374 off_t offset;
4376 if (lineno <= 0 || lineno > s->blame.nlines) {
4377 if (s->matched_line == 0) {
4378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4379 break;
4382 if (view->searching == TOG_SEARCH_FORWARD)
4383 lineno = 1;
4384 else
4385 lineno = s->blame.nlines;
4388 offset = s->blame.line_offsets[lineno - 1];
4389 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4390 free(line);
4391 return got_error_from_errno("fseeko");
4393 linelen = getline(&line, &linesize, s->blame.f);
4394 if (linelen != -1 &&
4395 match_line(line, &view->regex, 1, &view->regmatch)) {
4396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4397 s->matched_line = lineno;
4398 break;
4400 if (view->searching == TOG_SEARCH_FORWARD)
4401 lineno++;
4402 else
4403 lineno--;
4405 free(line);
4407 if (s->matched_line) {
4408 s->first_displayed_line = s->matched_line;
4409 s->selected_line = 1;
4412 return NULL;
4415 static const struct got_error *
4416 show_blame_view(struct tog_view *view)
4418 const struct got_error *err = NULL;
4419 struct tog_blame_view_state *s = &view->state.blame;
4420 int errcode;
4422 if (s->blame.thread == NULL) {
4423 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4424 &s->blame.thread_args);
4425 if (errcode)
4426 return got_error_set_errno(errcode, "pthread_create");
4428 halfdelay(1); /* fast refresh while annotating */
4431 if (s->blame_complete)
4432 halfdelay(10); /* disable fast refresh */
4434 err = draw_blame(view);
4436 view_vborder(view);
4437 return err;
4440 static const struct got_error *
4441 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4443 const struct got_error *err = NULL, *thread_err = NULL;
4444 struct tog_view *diff_view;
4445 struct tog_blame_view_state *s = &view->state.blame;
4446 int begin_x = 0;
4448 switch (ch) {
4449 case 'q':
4450 s->done = 1;
4451 break;
4452 case 'k':
4453 case KEY_UP:
4454 if (s->selected_line > 1)
4455 s->selected_line--;
4456 else if (s->selected_line == 1 &&
4457 s->first_displayed_line > 1)
4458 s->first_displayed_line--;
4459 break;
4460 case KEY_PPAGE:
4461 case CTRL('b'):
4462 if (s->first_displayed_line == 1) {
4463 s->selected_line = 1;
4464 break;
4466 if (s->first_displayed_line > view->nlines - 2)
4467 s->first_displayed_line -=
4468 (view->nlines - 2);
4469 else
4470 s->first_displayed_line = 1;
4471 break;
4472 case 'j':
4473 case KEY_DOWN:
4474 if (s->selected_line < view->nlines - 2 &&
4475 s->first_displayed_line +
4476 s->selected_line <= s->blame.nlines)
4477 s->selected_line++;
4478 else if (s->last_displayed_line <
4479 s->blame.nlines)
4480 s->first_displayed_line++;
4481 break;
4482 case 'b':
4483 case 'p': {
4484 struct got_object_id *id = NULL;
4485 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4486 s->first_displayed_line, s->selected_line);
4487 if (id == NULL)
4488 break;
4489 if (ch == 'p') {
4490 struct got_commit_object *commit;
4491 struct got_object_qid *pid;
4492 struct got_object_id *blob_id = NULL;
4493 int obj_type;
4494 err = got_object_open_as_commit(&commit,
4495 s->repo, id);
4496 if (err)
4497 break;
4498 pid = SIMPLEQ_FIRST(
4499 got_object_commit_get_parent_ids(commit));
4500 if (pid == NULL) {
4501 got_object_commit_close(commit);
4502 break;
4504 /* Check if path history ends here. */
4505 err = got_object_id_by_path(&blob_id, s->repo,
4506 pid->id, s->path);
4507 if (err) {
4508 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4509 err = NULL;
4510 got_object_commit_close(commit);
4511 break;
4513 err = got_object_get_type(&obj_type, s->repo,
4514 blob_id);
4515 free(blob_id);
4516 /* Can't blame non-blob type objects. */
4517 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4518 got_object_commit_close(commit);
4519 break;
4521 err = got_object_qid_alloc(&s->blamed_commit,
4522 pid->id);
4523 got_object_commit_close(commit);
4524 } else {
4525 if (got_object_id_cmp(id,
4526 s->blamed_commit->id) == 0)
4527 break;
4528 err = got_object_qid_alloc(&s->blamed_commit,
4529 id);
4531 if (err)
4532 break;
4533 s->done = 1;
4534 thread_err = stop_blame(&s->blame);
4535 s->done = 0;
4536 if (thread_err)
4537 break;
4538 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4539 s->blamed_commit, entry);
4540 err = run_blame(view);
4541 if (err)
4542 break;
4543 break;
4545 case 'B': {
4546 struct got_object_qid *first;
4547 first = SIMPLEQ_FIRST(&s->blamed_commits);
4548 if (!got_object_id_cmp(first->id, s->commit_id))
4549 break;
4550 s->done = 1;
4551 thread_err = stop_blame(&s->blame);
4552 s->done = 0;
4553 if (thread_err)
4554 break;
4555 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4556 got_object_qid_free(s->blamed_commit);
4557 s->blamed_commit =
4558 SIMPLEQ_FIRST(&s->blamed_commits);
4559 err = run_blame(view);
4560 if (err)
4561 break;
4562 break;
4564 case KEY_ENTER:
4565 case '\r': {
4566 struct got_object_id *id = NULL;
4567 struct got_object_qid *pid;
4568 struct got_commit_object *commit = NULL;
4569 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4570 s->first_displayed_line, s->selected_line);
4571 if (id == NULL)
4572 break;
4573 err = got_object_open_as_commit(&commit, s->repo, id);
4574 if (err)
4575 break;
4576 pid = SIMPLEQ_FIRST(
4577 got_object_commit_get_parent_ids(commit));
4578 if (view_is_parent_view(view))
4579 begin_x = view_split_begin_x(view->begin_x);
4580 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4581 if (diff_view == NULL) {
4582 got_object_commit_close(commit);
4583 err = got_error_from_errno("view_open");
4584 break;
4586 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4587 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4588 got_object_commit_close(commit);
4589 if (err) {
4590 view_close(diff_view);
4591 break;
4593 view->focussed = 0;
4594 diff_view->focussed = 1;
4595 if (view_is_parent_view(view)) {
4596 err = view_close_child(view);
4597 if (err)
4598 break;
4599 view_set_child(view, diff_view);
4600 view->focus_child = 1;
4601 } else
4602 *new_view = diff_view;
4603 if (err)
4604 break;
4605 break;
4607 case KEY_NPAGE:
4608 case CTRL('f'):
4609 case ' ':
4610 if (s->last_displayed_line >= s->blame.nlines &&
4611 s->selected_line >= MIN(s->blame.nlines,
4612 view->nlines - 2)) {
4613 break;
4615 if (s->last_displayed_line >= s->blame.nlines &&
4616 s->selected_line < view->nlines - 2) {
4617 s->selected_line = MIN(s->blame.nlines,
4618 view->nlines - 2);
4619 break;
4621 if (s->last_displayed_line + view->nlines - 2
4622 <= s->blame.nlines)
4623 s->first_displayed_line +=
4624 view->nlines - 2;
4625 else
4626 s->first_displayed_line =
4627 s->blame.nlines -
4628 (view->nlines - 3);
4629 break;
4630 case KEY_RESIZE:
4631 if (s->selected_line > view->nlines - 2) {
4632 s->selected_line = MIN(s->blame.nlines,
4633 view->nlines - 2);
4635 break;
4636 default:
4637 break;
4639 return thread_err ? thread_err : err;
4642 static const struct got_error *
4643 cmd_blame(int argc, char *argv[])
4645 const struct got_error *error;
4646 struct got_repository *repo = NULL;
4647 struct got_worktree *worktree = NULL;
4648 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4649 char *link_target = NULL;
4650 struct got_object_id *commit_id = NULL;
4651 char *commit_id_str = NULL;
4652 int ch;
4653 struct tog_view *view;
4655 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4656 switch (ch) {
4657 case 'c':
4658 commit_id_str = optarg;
4659 break;
4660 case 'r':
4661 repo_path = realpath(optarg, NULL);
4662 if (repo_path == NULL)
4663 return got_error_from_errno2("realpath",
4664 optarg);
4665 break;
4666 default:
4667 usage_blame();
4668 /* NOTREACHED */
4672 argc -= optind;
4673 argv += optind;
4675 if (argc != 1)
4676 usage_blame();
4678 if (repo_path == NULL) {
4679 cwd = getcwd(NULL, 0);
4680 if (cwd == NULL)
4681 return got_error_from_errno("getcwd");
4682 error = got_worktree_open(&worktree, cwd);
4683 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4684 goto done;
4685 if (worktree)
4686 repo_path =
4687 strdup(got_worktree_get_repo_path(worktree));
4688 else
4689 repo_path = strdup(cwd);
4690 if (repo_path == NULL) {
4691 error = got_error_from_errno("strdup");
4692 goto done;
4696 error = got_repo_open(&repo, repo_path, NULL);
4697 if (error != NULL)
4698 goto done;
4700 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4701 worktree);
4702 if (error)
4703 goto done;
4705 init_curses();
4707 error = apply_unveil(got_repo_get_path(repo), NULL);
4708 if (error)
4709 goto done;
4711 if (commit_id_str == NULL) {
4712 struct got_reference *head_ref;
4713 error = got_ref_open(&head_ref, repo, worktree ?
4714 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4715 if (error != NULL)
4716 goto done;
4717 error = got_ref_resolve(&commit_id, repo, head_ref);
4718 got_ref_close(head_ref);
4719 } else {
4720 error = got_repo_match_object_id(&commit_id, NULL,
4721 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4723 if (error != NULL)
4724 goto done;
4726 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4727 if (view == NULL) {
4728 error = got_error_from_errno("view_open");
4729 goto done;
4732 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4733 commit_id, repo);
4734 if (error)
4735 goto done;
4737 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4738 commit_id, repo);
4739 if (error)
4740 goto done;
4741 if (worktree) {
4742 /* Release work tree lock. */
4743 got_worktree_close(worktree);
4744 worktree = NULL;
4746 error = view_loop(view);
4747 done:
4748 free(repo_path);
4749 free(in_repo_path);
4750 free(link_target);
4751 free(cwd);
4752 free(commit_id);
4753 if (worktree)
4754 got_worktree_close(worktree);
4755 if (repo)
4756 got_repo_close(repo);
4757 return error;
4760 static const struct got_error *
4761 draw_tree_entries(struct tog_view *view, const char *parent_path)
4763 struct tog_tree_view_state *s = &view->state.tree;
4764 const struct got_error *err = NULL;
4765 struct got_tree_entry *te;
4766 wchar_t *wline;
4767 struct tog_color *tc;
4768 int width, n, i, nentries;
4769 int limit = view->nlines;
4771 s->ndisplayed = 0;
4773 werase(view->window);
4775 if (limit == 0)
4776 return NULL;
4778 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4779 if (err)
4780 return err;
4781 if (view_needs_focus_indication(view))
4782 wstandout(view->window);
4783 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4784 if (tc)
4785 wattr_on(view->window,
4786 COLOR_PAIR(tc->colorpair), NULL);
4787 waddwstr(view->window, wline);
4788 if (tc)
4789 wattr_off(view->window,
4790 COLOR_PAIR(tc->colorpair), NULL);
4791 if (view_needs_focus_indication(view))
4792 wstandend(view->window);
4793 free(wline);
4794 wline = NULL;
4795 if (width < view->ncols - 1)
4796 waddch(view->window, '\n');
4797 if (--limit <= 0)
4798 return NULL;
4799 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4800 if (err)
4801 return err;
4802 waddwstr(view->window, wline);
4803 free(wline);
4804 wline = NULL;
4805 if (width < view->ncols - 1)
4806 waddch(view->window, '\n');
4807 if (--limit <= 0)
4808 return NULL;
4809 waddch(view->window, '\n');
4810 if (--limit <= 0)
4811 return NULL;
4813 if (s->first_displayed_entry == NULL) {
4814 te = got_object_tree_get_first_entry(s->tree);
4815 if (s->selected == 0) {
4816 if (view->focussed)
4817 wstandout(view->window);
4818 s->selected_entry = NULL;
4820 waddstr(view->window, " ..\n"); /* parent directory */
4821 if (s->selected == 0 && view->focussed)
4822 wstandend(view->window);
4823 s->ndisplayed++;
4824 if (--limit <= 0)
4825 return NULL;
4826 n = 1;
4827 } else {
4828 n = 0;
4829 te = s->first_displayed_entry;
4832 nentries = got_object_tree_get_nentries(s->tree);
4833 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4834 char *line = NULL, *id_str = NULL, *link_target = NULL;
4835 const char *modestr = "";
4836 mode_t mode;
4838 te = got_object_tree_get_entry(s->tree, i);
4839 mode = got_tree_entry_get_mode(te);
4841 if (s->show_ids) {
4842 err = got_object_id_str(&id_str,
4843 got_tree_entry_get_id(te));
4844 if (err)
4845 return got_error_from_errno(
4846 "got_object_id_str");
4848 if (got_object_tree_entry_is_submodule(te))
4849 modestr = "$";
4850 else if (S_ISLNK(mode)) {
4851 int i;
4853 err = got_tree_entry_get_symlink_target(&link_target,
4854 te, s->repo);
4855 if (err) {
4856 free(id_str);
4857 return err;
4859 for (i = 0; i < strlen(link_target); i++) {
4860 if (!isprint((unsigned char)link_target[i]))
4861 link_target[i] = '?';
4863 modestr = "@";
4865 else if (S_ISDIR(mode))
4866 modestr = "/";
4867 else if (mode & S_IXUSR)
4868 modestr = "*";
4869 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4870 got_tree_entry_get_name(te), modestr,
4871 link_target ? " -> ": "",
4872 link_target ? link_target : "") == -1) {
4873 free(id_str);
4874 free(link_target);
4875 return got_error_from_errno("asprintf");
4877 free(id_str);
4878 free(link_target);
4879 err = format_line(&wline, &width, line, view->ncols, 0);
4880 if (err) {
4881 free(line);
4882 break;
4884 if (n == s->selected) {
4885 if (view->focussed)
4886 wstandout(view->window);
4887 s->selected_entry = te;
4889 tc = match_color(&s->colors, line);
4890 if (tc)
4891 wattr_on(view->window,
4892 COLOR_PAIR(tc->colorpair), NULL);
4893 waddwstr(view->window, wline);
4894 if (tc)
4895 wattr_off(view->window,
4896 COLOR_PAIR(tc->colorpair), NULL);
4897 if (width < view->ncols - 1)
4898 waddch(view->window, '\n');
4899 if (n == s->selected && view->focussed)
4900 wstandend(view->window);
4901 free(line);
4902 free(wline);
4903 wline = NULL;
4904 n++;
4905 s->ndisplayed++;
4906 s->last_displayed_entry = te;
4907 if (--limit <= 0)
4908 break;
4911 return err;
4914 static void
4915 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4917 struct got_tree_entry *te;
4918 int isroot = s->tree == s->root;
4919 int i = 0;
4921 if (s->first_displayed_entry == NULL)
4922 return;
4924 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4925 while (i++ < maxscroll) {
4926 if (te == NULL) {
4927 if (!isroot)
4928 s->first_displayed_entry = NULL;
4929 break;
4931 s->first_displayed_entry = te;
4932 te = got_tree_entry_get_prev(s->tree, te);
4936 static void
4937 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4939 struct got_tree_entry *next, *last;
4940 int n = 0;
4942 if (s->first_displayed_entry)
4943 next = got_tree_entry_get_next(s->tree,
4944 s->first_displayed_entry);
4945 else
4946 next = got_object_tree_get_first_entry(s->tree);
4948 last = s->last_displayed_entry;
4949 while (next && last && n++ < maxscroll) {
4950 last = got_tree_entry_get_next(s->tree, last);
4951 if (last) {
4952 s->first_displayed_entry = next;
4953 next = got_tree_entry_get_next(s->tree, next);
4958 static const struct got_error *
4959 tree_entry_path(char **path, struct tog_parent_trees *parents,
4960 struct got_tree_entry *te)
4962 const struct got_error *err = NULL;
4963 struct tog_parent_tree *pt;
4964 size_t len = 2; /* for leading slash and NUL */
4966 TAILQ_FOREACH(pt, parents, entry)
4967 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4968 + 1 /* slash */;
4969 if (te)
4970 len += strlen(got_tree_entry_get_name(te));
4972 *path = calloc(1, len);
4973 if (path == NULL)
4974 return got_error_from_errno("calloc");
4976 (*path)[0] = '/';
4977 pt = TAILQ_LAST(parents, tog_parent_trees);
4978 while (pt) {
4979 const char *name = got_tree_entry_get_name(pt->selected_entry);
4980 if (strlcat(*path, name, len) >= len) {
4981 err = got_error(GOT_ERR_NO_SPACE);
4982 goto done;
4984 if (strlcat(*path, "/", len) >= len) {
4985 err = got_error(GOT_ERR_NO_SPACE);
4986 goto done;
4988 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4990 if (te) {
4991 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4992 err = got_error(GOT_ERR_NO_SPACE);
4993 goto done;
4996 done:
4997 if (err) {
4998 free(*path);
4999 *path = NULL;
5001 return err;
5004 static const struct got_error *
5005 blame_tree_entry(struct tog_view **new_view, int begin_x,
5006 struct got_tree_entry *te, struct tog_parent_trees *parents,
5007 struct got_object_id *commit_id, struct got_repository *repo)
5009 const struct got_error *err = NULL;
5010 char *path;
5011 struct tog_view *blame_view;
5013 *new_view = NULL;
5015 err = tree_entry_path(&path, parents, te);
5016 if (err)
5017 return err;
5019 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5020 if (blame_view == NULL) {
5021 err = got_error_from_errno("view_open");
5022 goto done;
5025 err = open_blame_view(blame_view, path, commit_id, repo);
5026 if (err) {
5027 if (err->code == GOT_ERR_CANCELLED)
5028 err = NULL;
5029 view_close(blame_view);
5030 } else
5031 *new_view = blame_view;
5032 done:
5033 free(path);
5034 return err;
5037 static const struct got_error *
5038 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5039 struct tog_tree_view_state *s)
5041 struct tog_view *log_view;
5042 const struct got_error *err = NULL;
5043 char *path;
5045 *new_view = NULL;
5047 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5048 if (log_view == NULL)
5049 return got_error_from_errno("view_open");
5051 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5052 if (err)
5053 return err;
5055 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5056 path, 0);
5057 if (err)
5058 view_close(log_view);
5059 else
5060 *new_view = log_view;
5061 free(path);
5062 return err;
5065 static const struct got_error *
5066 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5067 struct got_object_id *commit_id, const char *head_ref_name,
5068 struct got_repository *repo)
5070 const struct got_error *err = NULL;
5071 char *commit_id_str = NULL;
5072 struct tog_tree_view_state *s = &view->state.tree;
5074 TAILQ_INIT(&s->parents);
5076 err = got_object_id_str(&commit_id_str, commit_id);
5077 if (err != NULL)
5078 goto done;
5080 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5081 err = got_error_from_errno("asprintf");
5082 goto done;
5085 s->root = s->tree = root;
5086 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5087 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5088 s->commit_id = got_object_id_dup(commit_id);
5089 if (s->commit_id == NULL) {
5090 err = got_error_from_errno("got_object_id_dup");
5091 goto done;
5093 if (head_ref_name) {
5094 s->head_ref_name = strdup(head_ref_name);
5095 if (s->head_ref_name == NULL) {
5096 err = got_error_from_errno("strdup");
5097 goto done;
5100 s->repo = repo;
5102 SIMPLEQ_INIT(&s->colors);
5104 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5105 err = add_color(&s->colors, "\\$$",
5106 TOG_COLOR_TREE_SUBMODULE,
5107 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5108 if (err)
5109 goto done;
5110 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5111 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5112 if (err) {
5113 free_colors(&s->colors);
5114 goto done;
5116 err = add_color(&s->colors, "/$",
5117 TOG_COLOR_TREE_DIRECTORY,
5118 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5119 if (err) {
5120 free_colors(&s->colors);
5121 goto done;
5124 err = add_color(&s->colors, "\\*$",
5125 TOG_COLOR_TREE_EXECUTABLE,
5126 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5127 if (err) {
5128 free_colors(&s->colors);
5129 goto done;
5132 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5133 get_color_value("TOG_COLOR_COMMIT"));
5134 if (err) {
5135 free_colors(&s->colors);
5136 goto done;
5140 view->show = show_tree_view;
5141 view->input = input_tree_view;
5142 view->close = close_tree_view;
5143 view->search_start = search_start_tree_view;
5144 view->search_next = search_next_tree_view;
5145 done:
5146 free(commit_id_str);
5147 if (err) {
5148 free(s->tree_label);
5149 s->tree_label = NULL;
5151 return err;
5154 static const struct got_error *
5155 close_tree_view(struct tog_view *view)
5157 struct tog_tree_view_state *s = &view->state.tree;
5159 free_colors(&s->colors);
5160 free(s->tree_label);
5161 s->tree_label = NULL;
5162 free(s->commit_id);
5163 s->commit_id = NULL;
5164 free(s->head_ref_name);
5165 s->head_ref_name = NULL;
5166 while (!TAILQ_EMPTY(&s->parents)) {
5167 struct tog_parent_tree *parent;
5168 parent = TAILQ_FIRST(&s->parents);
5169 TAILQ_REMOVE(&s->parents, parent, entry);
5170 free(parent);
5173 if (s->tree != s->root)
5174 got_object_tree_close(s->tree);
5175 got_object_tree_close(s->root);
5176 return NULL;
5179 static const struct got_error *
5180 search_start_tree_view(struct tog_view *view)
5182 struct tog_tree_view_state *s = &view->state.tree;
5184 s->matched_entry = NULL;
5185 return NULL;
5188 static int
5189 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5191 regmatch_t regmatch;
5193 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5194 0) == 0;
5197 static const struct got_error *
5198 search_next_tree_view(struct tog_view *view)
5200 struct tog_tree_view_state *s = &view->state.tree;
5201 struct got_tree_entry *te = NULL;
5203 if (!view->searching) {
5204 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5205 return NULL;
5208 if (s->matched_entry) {
5209 if (view->searching == TOG_SEARCH_FORWARD) {
5210 if (s->selected_entry)
5211 te = got_tree_entry_get_next(s->tree,
5212 s->selected_entry);
5213 else
5214 te = got_object_tree_get_first_entry(s->tree);
5215 } else {
5216 if (s->selected_entry == NULL)
5217 te = got_object_tree_get_last_entry(s->tree);
5218 else
5219 te = got_tree_entry_get_prev(s->tree,
5220 s->selected_entry);
5222 } else {
5223 if (view->searching == TOG_SEARCH_FORWARD)
5224 te = got_object_tree_get_first_entry(s->tree);
5225 else
5226 te = got_object_tree_get_last_entry(s->tree);
5229 while (1) {
5230 if (te == NULL) {
5231 if (s->matched_entry == NULL) {
5232 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5233 return NULL;
5235 if (view->searching == TOG_SEARCH_FORWARD)
5236 te = got_object_tree_get_first_entry(s->tree);
5237 else
5238 te = got_object_tree_get_last_entry(s->tree);
5241 if (match_tree_entry(te, &view->regex)) {
5242 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5243 s->matched_entry = te;
5244 break;
5247 if (view->searching == TOG_SEARCH_FORWARD)
5248 te = got_tree_entry_get_next(s->tree, te);
5249 else
5250 te = got_tree_entry_get_prev(s->tree, te);
5253 if (s->matched_entry) {
5254 s->first_displayed_entry = s->matched_entry;
5255 s->selected = 0;
5258 return NULL;
5261 static const struct got_error *
5262 show_tree_view(struct tog_view *view)
5264 const struct got_error *err = NULL;
5265 struct tog_tree_view_state *s = &view->state.tree;
5266 char *parent_path;
5268 err = tree_entry_path(&parent_path, &s->parents, NULL);
5269 if (err)
5270 return err;
5272 err = draw_tree_entries(view, parent_path);
5273 free(parent_path);
5275 view_vborder(view);
5276 return err;
5279 static const struct got_error *
5280 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5282 const struct got_error *err = NULL;
5283 struct tog_tree_view_state *s = &view->state.tree;
5284 struct tog_view *log_view, *ref_view;
5285 int begin_x = 0;
5287 switch (ch) {
5288 case 'i':
5289 s->show_ids = !s->show_ids;
5290 break;
5291 case 'l':
5292 if (!s->selected_entry)
5293 break;
5294 if (view_is_parent_view(view))
5295 begin_x = view_split_begin_x(view->begin_x);
5296 err = log_selected_tree_entry(&log_view, begin_x, s);
5297 view->focussed = 0;
5298 log_view->focussed = 1;
5299 if (view_is_parent_view(view)) {
5300 err = view_close_child(view);
5301 if (err)
5302 return err;
5303 view_set_child(view, log_view);
5304 view->focus_child = 1;
5305 } else
5306 *new_view = log_view;
5307 break;
5308 case 'r':
5309 if (view_is_parent_view(view))
5310 begin_x = view_split_begin_x(view->begin_x);
5311 ref_view = view_open(view->nlines, view->ncols,
5312 view->begin_y, begin_x, TOG_VIEW_REF);
5313 if (ref_view == NULL)
5314 return got_error_from_errno("view_open");
5315 err = open_ref_view(ref_view, s->repo);
5316 if (err) {
5317 view_close(ref_view);
5318 return err;
5320 view->focussed = 0;
5321 ref_view->focussed = 1;
5322 if (view_is_parent_view(view)) {
5323 err = view_close_child(view);
5324 if (err)
5325 return err;
5326 view_set_child(view, ref_view);
5327 view->focus_child = 1;
5328 } else
5329 *new_view = ref_view;
5330 break;
5331 case 'k':
5332 case KEY_UP:
5333 if (s->selected > 0) {
5334 s->selected--;
5335 break;
5337 tree_scroll_up(s, 1);
5338 break;
5339 case KEY_PPAGE:
5340 case CTRL('b'):
5341 if (s->tree == s->root) {
5342 if (got_object_tree_get_first_entry(s->tree) ==
5343 s->first_displayed_entry)
5344 s->selected = 0;
5345 } else {
5346 if (s->first_displayed_entry == NULL)
5347 s->selected = 0;
5349 tree_scroll_up(s, MAX(0, view->nlines - 3));
5350 break;
5351 case 'j':
5352 case KEY_DOWN:
5353 if (s->selected < s->ndisplayed - 1) {
5354 s->selected++;
5355 break;
5357 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5358 == NULL)
5359 /* can't scroll any further */
5360 break;
5361 tree_scroll_down(s, 1);
5362 break;
5363 case KEY_NPAGE:
5364 case CTRL('f'):
5365 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5366 == NULL) {
5367 /* can't scroll any further; move cursor down */
5368 if (s->selected < s->ndisplayed - 1)
5369 s->selected = s->ndisplayed - 1;
5370 break;
5372 tree_scroll_down(s, view->nlines - 3);
5373 break;
5374 case KEY_ENTER:
5375 case '\r':
5376 case KEY_BACKSPACE:
5377 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5378 struct tog_parent_tree *parent;
5379 /* user selected '..' */
5380 if (s->tree == s->root)
5381 break;
5382 parent = TAILQ_FIRST(&s->parents);
5383 TAILQ_REMOVE(&s->parents, parent,
5384 entry);
5385 got_object_tree_close(s->tree);
5386 s->tree = parent->tree;
5387 s->first_displayed_entry =
5388 parent->first_displayed_entry;
5389 s->selected_entry =
5390 parent->selected_entry;
5391 s->selected = parent->selected;
5392 free(parent);
5393 } else if (S_ISDIR(got_tree_entry_get_mode(
5394 s->selected_entry))) {
5395 struct got_tree_object *subtree;
5396 err = got_object_open_as_tree(&subtree, s->repo,
5397 got_tree_entry_get_id(s->selected_entry));
5398 if (err)
5399 break;
5400 err = tree_view_visit_subtree(s, subtree);
5401 if (err) {
5402 got_object_tree_close(subtree);
5403 break;
5405 } else if (S_ISREG(got_tree_entry_get_mode(
5406 s->selected_entry))) {
5407 struct tog_view *blame_view;
5408 int begin_x = view_is_parent_view(view) ?
5409 view_split_begin_x(view->begin_x) : 0;
5411 err = blame_tree_entry(&blame_view, begin_x,
5412 s->selected_entry, &s->parents,
5413 s->commit_id, s->repo);
5414 if (err)
5415 break;
5416 view->focussed = 0;
5417 blame_view->focussed = 1;
5418 if (view_is_parent_view(view)) {
5419 err = view_close_child(view);
5420 if (err)
5421 return err;
5422 view_set_child(view, blame_view);
5423 view->focus_child = 1;
5424 } else
5425 *new_view = blame_view;
5427 break;
5428 case KEY_RESIZE:
5429 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5430 s->selected = view->nlines - 4;
5431 break;
5432 default:
5433 break;
5436 return err;
5439 __dead static void
5440 usage_tree(void)
5442 endwin();
5443 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5444 getprogname());
5445 exit(1);
5448 static const struct got_error *
5449 cmd_tree(int argc, char *argv[])
5451 const struct got_error *error;
5452 struct got_repository *repo = NULL;
5453 struct got_worktree *worktree = NULL;
5454 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5455 struct got_object_id *commit_id = NULL;
5456 const char *commit_id_arg = NULL;
5457 char *label = NULL;
5458 struct got_commit_object *commit = NULL;
5459 struct got_tree_object *tree = NULL;
5460 struct got_reference *ref = NULL;
5461 const char *head_ref_name = NULL;
5462 int ch;
5463 struct tog_view *view;
5465 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5466 switch (ch) {
5467 case 'c':
5468 commit_id_arg = optarg;
5469 break;
5470 case 'r':
5471 repo_path = realpath(optarg, NULL);
5472 if (repo_path == NULL)
5473 return got_error_from_errno2("realpath",
5474 optarg);
5475 break;
5476 default:
5477 usage_tree();
5478 /* NOTREACHED */
5482 argc -= optind;
5483 argv += optind;
5485 if (argc > 1)
5486 usage_tree();
5488 if (repo_path == NULL) {
5489 cwd = getcwd(NULL, 0);
5490 if (cwd == NULL)
5491 return got_error_from_errno("getcwd");
5492 error = got_worktree_open(&worktree, cwd);
5493 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5494 goto done;
5495 if (worktree)
5496 repo_path =
5497 strdup(got_worktree_get_repo_path(worktree));
5498 else
5499 repo_path = strdup(cwd);
5500 if (repo_path == NULL) {
5501 error = got_error_from_errno("strdup");
5502 goto done;
5506 error = got_repo_open(&repo, repo_path, NULL);
5507 if (error != NULL)
5508 goto done;
5510 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5511 repo, worktree);
5512 if (error)
5513 goto done;
5515 init_curses();
5517 error = apply_unveil(got_repo_get_path(repo), NULL);
5518 if (error)
5519 goto done;
5521 if (commit_id_arg == NULL) {
5522 error = got_repo_match_object_id(&commit_id, &label,
5523 worktree ? got_worktree_get_head_ref_name(worktree) :
5524 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5525 if (error)
5526 goto done;
5527 head_ref_name = label;
5528 } else {
5529 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5530 if (error == NULL)
5531 head_ref_name = got_ref_get_name(ref);
5532 else if (error->code != GOT_ERR_NOT_REF)
5533 goto done;
5534 error = got_repo_match_object_id(&commit_id, NULL,
5535 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5536 if (error)
5537 goto done;
5540 error = got_object_open_as_commit(&commit, repo, commit_id);
5541 if (error)
5542 goto done;
5544 error = got_object_open_as_tree(&tree, repo,
5545 got_object_commit_get_tree_id(commit));
5546 if (error)
5547 goto done;
5549 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5550 if (view == NULL) {
5551 error = got_error_from_errno("view_open");
5552 goto done;
5554 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5555 if (error)
5556 goto done;
5557 if (!got_path_is_root_dir(in_repo_path)) {
5558 error = tree_view_walk_path(&view->state.tree, commit_id,
5559 in_repo_path);
5560 if (error)
5561 goto done;
5564 if (worktree) {
5565 /* Release work tree lock. */
5566 got_worktree_close(worktree);
5567 worktree = NULL;
5569 error = view_loop(view);
5570 done:
5571 free(repo_path);
5572 free(cwd);
5573 free(commit_id);
5574 free(label);
5575 if (ref)
5576 got_ref_close(ref);
5577 if (commit)
5578 got_object_commit_close(commit);
5579 if (tree)
5580 got_object_tree_close(tree);
5581 if (repo)
5582 got_repo_close(repo);
5583 return error;
5586 static const struct got_error *
5587 ref_view_load_refs(struct tog_ref_view_state *s)
5589 const struct got_error *err;
5590 struct got_reflist_entry *sre;
5591 struct tog_reflist_entry *re;
5593 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5594 got_ref_cmp_by_name, NULL);
5595 if (err)
5596 return err;
5598 s->nrefs = 0;
5599 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5600 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5601 continue;
5603 re = malloc(sizeof(*re));
5604 if (re == NULL)
5605 return got_error_from_errno("malloc");
5607 re->ref = sre->ref;
5608 re->idx = s->nrefs++;
5609 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5612 return NULL;
5615 void
5616 ref_view_free_refs(struct tog_ref_view_state *s)
5618 struct tog_reflist_entry *re;
5620 while (!TAILQ_EMPTY(&s->refs)) {
5621 re = TAILQ_FIRST(&s->refs);
5622 TAILQ_REMOVE(&s->refs, re, entry);
5623 free(re);
5625 got_ref_list_free(&s->simplerefs);
5628 static const struct got_error *
5629 open_ref_view(struct tog_view *view, struct got_repository *repo)
5631 const struct got_error *err = NULL;
5632 struct tog_ref_view_state *s = &view->state.ref;
5634 s->selected_entry = 0;
5635 s->repo = repo;
5637 SIMPLEQ_INIT(&s->simplerefs);
5638 TAILQ_INIT(&s->refs);
5639 SIMPLEQ_INIT(&s->colors);
5641 err = ref_view_load_refs(s);
5642 if (err)
5643 return err;
5645 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5647 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5648 err = add_color(&s->colors, "^refs/heads/",
5649 TOG_COLOR_REFS_HEADS,
5650 get_color_value("TOG_COLOR_REFS_HEADS"));
5651 if (err)
5652 goto done;
5654 err = add_color(&s->colors, "^refs/tags/",
5655 TOG_COLOR_REFS_TAGS,
5656 get_color_value("TOG_COLOR_REFS_TAGS"));
5657 if (err)
5658 goto done;
5660 err = add_color(&s->colors, "^refs/remotes/",
5661 TOG_COLOR_REFS_REMOTES,
5662 get_color_value("TOG_COLOR_REFS_REMOTES"));
5663 if (err)
5664 goto done;
5667 view->show = show_ref_view;
5668 view->input = input_ref_view;
5669 view->close = close_ref_view;
5670 view->search_start = search_start_ref_view;
5671 view->search_next = search_next_ref_view;
5672 done:
5673 if (err)
5674 free_colors(&s->colors);
5675 return err;
5678 static const struct got_error *
5679 close_ref_view(struct tog_view *view)
5681 struct tog_ref_view_state *s = &view->state.ref;
5683 ref_view_free_refs(s);
5684 free_colors(&s->colors);
5686 return NULL;
5689 static const struct got_error *
5690 resolve_reflist_entry(struct got_object_id **commit_id,
5691 struct tog_reflist_entry *re, struct got_repository *repo)
5693 const struct got_error *err = NULL;
5694 struct got_object_id *obj_id;
5695 struct got_tag_object *tag = NULL;
5696 int obj_type;
5698 *commit_id = NULL;
5700 err = got_ref_resolve(&obj_id, repo, re->ref);
5701 if (err)
5702 return err;
5704 err = got_object_get_type(&obj_type, repo, obj_id);
5705 if (err)
5706 goto done;
5708 switch (obj_type) {
5709 case GOT_OBJ_TYPE_COMMIT:
5710 *commit_id = obj_id;
5711 break;
5712 case GOT_OBJ_TYPE_TAG:
5713 err = got_object_open_as_tag(&tag, repo, obj_id);
5714 if (err)
5715 goto done;
5716 free(obj_id);
5717 err = got_object_get_type(&obj_type, repo,
5718 got_object_tag_get_object_id(tag));
5719 if (err)
5720 goto done;
5721 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5722 err = got_error(GOT_ERR_OBJ_TYPE);
5723 goto done;
5725 *commit_id = got_object_id_dup(
5726 got_object_tag_get_object_id(tag));
5727 if (*commit_id == NULL) {
5728 err = got_error_from_errno("got_object_id_dup");
5729 goto done;
5731 break;
5732 default:
5733 err = got_error(GOT_ERR_OBJ_TYPE);
5734 break;
5737 done:
5738 if (tag)
5739 got_object_tag_close(tag);
5740 if (err) {
5741 free(*commit_id);
5742 *commit_id = NULL;
5744 return err;
5747 static const struct got_error *
5748 log_ref_entry(struct tog_view **new_view, int begin_x,
5749 struct tog_reflist_entry *re, struct got_repository *repo)
5751 struct tog_view *log_view;
5752 const struct got_error *err = NULL;
5753 struct got_object_id *commit_id = NULL;
5755 *new_view = NULL;
5757 err = resolve_reflist_entry(&commit_id, re, repo);
5758 if (err) {
5759 if (err->code != GOT_ERR_OBJ_TYPE)
5760 return err;
5761 else
5762 return NULL;
5765 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5766 if (log_view == NULL) {
5767 err = got_error_from_errno("view_open");
5768 goto done;
5771 err = open_log_view(log_view, commit_id, repo,
5772 got_ref_get_name(re->ref), "", 0);
5773 done:
5774 if (err)
5775 view_close(log_view);
5776 else
5777 *new_view = log_view;
5778 free(commit_id);
5779 return err;
5782 static void
5783 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5785 struct tog_reflist_entry *re;
5786 int i = 0;
5788 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5789 return;
5791 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5792 while (i++ < maxscroll) {
5793 if (re == NULL)
5794 break;
5795 s->first_displayed_entry = re;
5796 re = TAILQ_PREV(re, tog_reflist_head, entry);
5800 static void
5801 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5803 struct tog_reflist_entry *next, *last;
5804 int n = 0;
5806 if (s->first_displayed_entry)
5807 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5808 else
5809 next = TAILQ_FIRST(&s->refs);
5811 last = s->last_displayed_entry;
5812 while (next && last && n++ < maxscroll) {
5813 last = TAILQ_NEXT(last, entry);
5814 if (last) {
5815 s->first_displayed_entry = next;
5816 next = TAILQ_NEXT(next, entry);
5821 static const struct got_error *
5822 search_start_ref_view(struct tog_view *view)
5824 struct tog_ref_view_state *s = &view->state.ref;
5826 s->matched_entry = NULL;
5827 return NULL;
5830 static int
5831 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5833 regmatch_t regmatch;
5835 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5836 0) == 0;
5839 static const struct got_error *
5840 search_next_ref_view(struct tog_view *view)
5842 struct tog_ref_view_state *s = &view->state.ref;
5843 struct tog_reflist_entry *re = NULL;
5845 if (!view->searching) {
5846 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5847 return NULL;
5850 if (s->matched_entry) {
5851 if (view->searching == TOG_SEARCH_FORWARD) {
5852 if (s->selected_entry)
5853 re = TAILQ_NEXT(s->selected_entry, entry);
5854 else
5855 re = TAILQ_PREV(s->selected_entry,
5856 tog_reflist_head, entry);
5857 } else {
5858 if (s->selected_entry == NULL)
5859 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5860 else
5861 re = TAILQ_PREV(s->selected_entry,
5862 tog_reflist_head, entry);
5864 } else {
5865 if (view->searching == TOG_SEARCH_FORWARD)
5866 re = TAILQ_FIRST(&s->refs);
5867 else
5868 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5871 while (1) {
5872 if (re == NULL) {
5873 if (s->matched_entry == NULL) {
5874 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5875 return NULL;
5877 if (view->searching == TOG_SEARCH_FORWARD)
5878 re = TAILQ_FIRST(&s->refs);
5879 else
5880 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5883 if (match_reflist_entry(re, &view->regex)) {
5884 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5885 s->matched_entry = re;
5886 break;
5889 if (view->searching == TOG_SEARCH_FORWARD)
5890 re = TAILQ_NEXT(re, entry);
5891 else
5892 re = TAILQ_PREV(re, tog_reflist_head, entry);
5895 if (s->matched_entry) {
5896 s->first_displayed_entry = s->matched_entry;
5897 s->selected = 0;
5900 return NULL;
5903 static const struct got_error *
5904 show_ref_view(struct tog_view *view)
5906 const struct got_error *err = NULL;
5907 struct tog_ref_view_state *s = &view->state.ref;
5908 struct tog_reflist_entry *re;
5909 char *line = NULL;
5910 wchar_t *wline;
5911 struct tog_color *tc;
5912 int width, n;
5913 int limit = view->nlines;
5915 werase(view->window);
5917 s->ndisplayed = 0;
5919 if (limit == 0)
5920 return NULL;
5922 re = s->first_displayed_entry;
5924 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5925 s->nrefs) == -1)
5926 return got_error_from_errno("asprintf");
5928 err = format_line(&wline, &width, line, view->ncols, 0);
5929 if (err) {
5930 free(line);
5931 return err;
5933 if (view_needs_focus_indication(view))
5934 wstandout(view->window);
5935 waddwstr(view->window, wline);
5936 if (view_needs_focus_indication(view))
5937 wstandend(view->window);
5938 free(wline);
5939 wline = NULL;
5940 free(line);
5941 line = NULL;
5942 if (width < view->ncols - 1)
5943 waddch(view->window, '\n');
5944 if (--limit <= 0)
5945 return NULL;
5947 n = 0;
5948 while (re && limit > 0) {
5949 char *line = NULL;
5951 if (got_ref_is_symbolic(re->ref)) {
5952 if (asprintf(&line, "%s -> %s",
5953 got_ref_get_name(re->ref),
5954 got_ref_get_symref_target(re->ref)) == -1)
5955 return got_error_from_errno("asprintf");
5956 } else if (s->show_ids) {
5957 struct got_object_id *id;
5958 char *id_str;
5959 err = got_ref_resolve(&id, s->repo, re->ref);
5960 if (err)
5961 return err;
5962 err = got_object_id_str(&id_str, id);
5963 if (err) {
5964 free(id);
5965 return err;
5967 if (asprintf(&line, "%s: %s",
5968 got_ref_get_name(re->ref), id_str) == -1) {
5969 err = got_error_from_errno("asprintf");
5970 free(id);
5971 free(id_str);
5972 return err;
5974 free(id);
5975 free(id_str);
5976 } else {
5977 line = strdup(got_ref_get_name(re->ref));
5978 if (line == NULL)
5979 return got_error_from_errno("strdup");
5982 err = format_line(&wline, &width, line, view->ncols, 0);
5983 if (err) {
5984 free(line);
5985 return err;
5987 if (n == s->selected) {
5988 if (view->focussed)
5989 wstandout(view->window);
5990 s->selected_entry = re;
5992 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5993 if (tc)
5994 wattr_on(view->window,
5995 COLOR_PAIR(tc->colorpair), NULL);
5996 waddwstr(view->window, wline);
5997 if (tc)
5998 wattr_off(view->window,
5999 COLOR_PAIR(tc->colorpair), NULL);
6000 if (width < view->ncols - 1)
6001 waddch(view->window, '\n');
6002 if (n == s->selected && view->focussed)
6003 wstandend(view->window);
6004 free(line);
6005 free(wline);
6006 wline = NULL;
6007 n++;
6008 s->ndisplayed++;
6009 s->last_displayed_entry = re;
6011 limit--;
6012 re = TAILQ_NEXT(re, entry);
6015 view_vborder(view);
6016 return err;
6019 static const struct got_error *
6020 browse_ref_tree(struct tog_view **new_view, int begin_x,
6021 struct tog_reflist_entry *re, struct got_repository *repo)
6023 const struct got_error *err = NULL;
6024 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6025 struct got_tree_object *tree = NULL;
6026 struct tog_view *tree_view;
6028 *new_view = NULL;
6030 err = resolve_reflist_entry(&commit_id, re, repo);
6031 if (err) {
6032 if (err->code != GOT_ERR_OBJ_TYPE)
6033 return err;
6034 else
6035 return NULL;
6038 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6039 if (err)
6040 goto done;
6042 err = got_object_open_as_tree(&tree, repo, tree_id);
6043 if (err)
6044 goto done;
6046 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6047 if (tree_view == NULL) {
6048 err = got_error_from_errno("view_open");
6049 goto done;
6052 err = open_tree_view(tree_view, tree, commit_id,
6053 got_ref_get_name(re->ref), repo);
6054 if (err)
6055 goto done;
6057 *new_view = tree_view;
6058 done:
6059 free(commit_id);
6060 free(tree_id);
6061 if (err) {
6062 if (tree)
6063 got_object_tree_close(tree);
6065 return err;
6067 static const struct got_error *
6068 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6070 const struct got_error *err = NULL;
6071 struct tog_ref_view_state *s = &view->state.ref;
6072 struct tog_view *log_view, *tree_view;
6073 int begin_x = 0;
6075 switch (ch) {
6076 case 'i':
6077 s->show_ids = !s->show_ids;
6078 break;
6079 case KEY_ENTER:
6080 case '\r':
6081 if (!s->selected_entry)
6082 break;
6083 if (view_is_parent_view(view))
6084 begin_x = view_split_begin_x(view->begin_x);
6085 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6086 s->repo);
6087 view->focussed = 0;
6088 log_view->focussed = 1;
6089 if (view_is_parent_view(view)) {
6090 err = view_close_child(view);
6091 if (err)
6092 return err;
6093 view_set_child(view, log_view);
6094 view->focus_child = 1;
6095 } else
6096 *new_view = log_view;
6097 break;
6098 case 't':
6099 if (!s->selected_entry)
6100 break;
6101 if (view_is_parent_view(view))
6102 begin_x = view_split_begin_x(view->begin_x);
6103 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6104 s->repo);
6105 if (err || tree_view == NULL)
6106 break;
6107 view->focussed = 0;
6108 tree_view->focussed = 1;
6109 if (view_is_parent_view(view)) {
6110 err = view_close_child(view);
6111 if (err)
6112 return err;
6113 view_set_child(view, tree_view);
6114 view->focus_child = 1;
6115 } else
6116 *new_view = tree_view;
6117 break;
6118 case 'k':
6119 case KEY_UP:
6120 if (s->selected > 0) {
6121 s->selected--;
6122 break;
6124 ref_scroll_up(s, 1);
6125 break;
6126 case KEY_PPAGE:
6127 case CTRL('b'):
6128 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6129 s->selected = 0;
6130 ref_scroll_up(s, MAX(0, view->nlines - 1));
6131 break;
6132 case 'j':
6133 case KEY_DOWN:
6134 if (s->selected < s->ndisplayed - 1) {
6135 s->selected++;
6136 break;
6138 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6139 /* can't scroll any further */
6140 break;
6141 ref_scroll_down(s, 1);
6142 break;
6143 case KEY_NPAGE:
6144 case CTRL('f'):
6145 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6146 /* can't scroll any further; move cursor down */
6147 if (s->selected < s->ndisplayed - 1)
6148 s->selected = s->ndisplayed - 1;
6149 break;
6151 ref_scroll_down(s, view->nlines - 1);
6152 break;
6153 case CTRL('l'):
6154 ref_view_free_refs(s);
6155 err = ref_view_load_refs(s);
6156 break;
6157 case KEY_RESIZE:
6158 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6159 s->selected = view->nlines - 2;
6160 break;
6161 default:
6162 break;
6165 return err;
6168 __dead static void
6169 usage_ref(void)
6171 endwin();
6172 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6173 getprogname());
6174 exit(1);
6177 static const struct got_error *
6178 cmd_ref(int argc, char *argv[])
6180 const struct got_error *error;
6181 struct got_repository *repo = NULL;
6182 struct got_worktree *worktree = NULL;
6183 char *cwd = NULL, *repo_path = NULL;
6184 int ch;
6185 struct tog_view *view;
6187 while ((ch = getopt(argc, argv, "r:")) != -1) {
6188 switch (ch) {
6189 case 'r':
6190 repo_path = realpath(optarg, NULL);
6191 if (repo_path == NULL)
6192 return got_error_from_errno2("realpath",
6193 optarg);
6194 break;
6195 default:
6196 usage_ref();
6197 /* NOTREACHED */
6201 argc -= optind;
6202 argv += optind;
6204 if (argc > 1)
6205 usage_ref();
6207 if (repo_path == NULL) {
6208 cwd = getcwd(NULL, 0);
6209 if (cwd == NULL)
6210 return got_error_from_errno("getcwd");
6211 error = got_worktree_open(&worktree, cwd);
6212 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6213 goto done;
6214 if (worktree)
6215 repo_path =
6216 strdup(got_worktree_get_repo_path(worktree));
6217 else
6218 repo_path = strdup(cwd);
6219 if (repo_path == NULL) {
6220 error = got_error_from_errno("strdup");
6221 goto done;
6225 error = got_repo_open(&repo, repo_path, NULL);
6226 if (error != NULL)
6227 goto done;
6229 init_curses();
6231 error = apply_unveil(got_repo_get_path(repo), NULL);
6232 if (error)
6233 goto done;
6235 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6236 if (view == NULL) {
6237 error = got_error_from_errno("view_open");
6238 goto done;
6241 error = open_ref_view(view, repo);
6242 if (error)
6243 goto done;
6245 if (worktree) {
6246 /* Release work tree lock. */
6247 got_worktree_close(worktree);
6248 worktree = NULL;
6250 error = view_loop(view);
6251 done:
6252 free(repo_path);
6253 free(cwd);
6254 if (repo)
6255 got_repo_close(repo);
6256 return error;
6259 static void
6260 list_commands(FILE *fp)
6262 size_t i;
6264 fprintf(fp, "commands:");
6265 for (i = 0; i < nitems(tog_commands); i++) {
6266 struct tog_cmd *cmd = &tog_commands[i];
6267 fprintf(fp, " %s", cmd->name);
6269 fputc('\n', fp);
6272 __dead static void
6273 usage(int hflag, int status)
6275 FILE *fp = (status == 0) ? stdout : stderr;
6277 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6278 getprogname());
6279 if (hflag) {
6280 fprintf(fp, "lazy usage: %s path\n", getprogname());
6281 list_commands(fp);
6283 exit(status);
6286 static char **
6287 make_argv(int argc, ...)
6289 va_list ap;
6290 char **argv;
6291 int i;
6293 va_start(ap, argc);
6295 argv = calloc(argc, sizeof(char *));
6296 if (argv == NULL)
6297 err(1, "calloc");
6298 for (i = 0; i < argc; i++) {
6299 argv[i] = strdup(va_arg(ap, char *));
6300 if (argv[i] == NULL)
6301 err(1, "strdup");
6304 va_end(ap);
6305 return argv;
6309 * Try to convert 'tog path' into a 'tog log path' command.
6310 * The user could simply have mistyped the command rather than knowingly
6311 * provided a path. So check whether argv[0] can in fact be resolved
6312 * to a path in the HEAD commit and print a special error if not.
6313 * This hack is for mpi@ <3
6315 static const struct got_error *
6316 tog_log_with_path(int argc, char *argv[])
6318 const struct got_error *error = NULL;
6319 struct tog_cmd *cmd = NULL;
6320 struct got_repository *repo = NULL;
6321 struct got_worktree *worktree = NULL;
6322 struct got_object_id *commit_id = NULL, *id = NULL;
6323 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6324 char *commit_id_str = NULL, **cmd_argv = NULL;
6326 cwd = getcwd(NULL, 0);
6327 if (cwd == NULL)
6328 return got_error_from_errno("getcwd");
6330 error = got_worktree_open(&worktree, cwd);
6331 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6332 goto done;
6334 if (worktree)
6335 repo_path = strdup(got_worktree_get_repo_path(worktree));
6336 else
6337 repo_path = strdup(cwd);
6338 if (repo_path == NULL) {
6339 error = got_error_from_errno("strdup");
6340 goto done;
6343 error = got_repo_open(&repo, repo_path, NULL);
6344 if (error != NULL)
6345 goto done;
6347 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6348 repo, worktree);
6349 if (error)
6350 goto done;
6352 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6353 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6354 GOT_OBJ_TYPE_COMMIT, 1, repo);
6355 if (error)
6356 goto done;
6358 if (worktree) {
6359 got_worktree_close(worktree);
6360 worktree = NULL;
6363 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6364 if (error) {
6365 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6366 goto done;
6367 fprintf(stderr, "%s: '%s' is no known command or path\n",
6368 getprogname(), argv[0]);
6369 usage(1, 1);
6370 /* not reached */
6373 got_repo_close(repo);
6374 repo = NULL;
6376 error = got_object_id_str(&commit_id_str, commit_id);
6377 if (error)
6378 goto done;
6380 cmd = &tog_commands[0]; /* log */
6381 argc = 4;
6382 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6383 error = cmd->cmd_main(argc, cmd_argv);
6384 done:
6385 if (repo)
6386 got_repo_close(repo);
6387 if (worktree)
6388 got_worktree_close(worktree);
6389 free(id);
6390 free(commit_id_str);
6391 free(commit_id);
6392 free(cwd);
6393 free(repo_path);
6394 free(in_repo_path);
6395 if (cmd_argv) {
6396 int i;
6397 for (i = 0; i < argc; i++)
6398 free(cmd_argv[i]);
6399 free(cmd_argv);
6401 return error;
6404 int
6405 main(int argc, char *argv[])
6407 const struct got_error *error = NULL;
6408 struct tog_cmd *cmd = NULL;
6409 int ch, hflag = 0, Vflag = 0;
6410 char **cmd_argv = NULL;
6411 static struct option longopts[] = {
6412 { "version", no_argument, NULL, 'V' },
6413 { NULL, 0, NULL, 0}
6416 setlocale(LC_CTYPE, "");
6418 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6419 switch (ch) {
6420 case 'h':
6421 hflag = 1;
6422 break;
6423 case 'V':
6424 Vflag = 1;
6425 break;
6426 default:
6427 usage(hflag, 1);
6428 /* NOTREACHED */
6432 argc -= optind;
6433 argv += optind;
6434 optind = 1;
6435 optreset = 1;
6437 if (Vflag) {
6438 got_version_print_str();
6439 return 0;
6442 #ifndef PROFILE
6443 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6444 NULL) == -1)
6445 err(1, "pledge");
6446 #endif
6448 if (argc == 0) {
6449 if (hflag)
6450 usage(hflag, 0);
6451 /* Build an argument vector which runs a default command. */
6452 cmd = &tog_commands[0];
6453 argc = 1;
6454 cmd_argv = make_argv(argc, cmd->name);
6455 } else {
6456 size_t i;
6458 /* Did the user specify a command? */
6459 for (i = 0; i < nitems(tog_commands); i++) {
6460 if (strncmp(tog_commands[i].name, argv[0],
6461 strlen(argv[0])) == 0) {
6462 cmd = &tog_commands[i];
6463 break;
6468 if (cmd == NULL) {
6469 if (argc != 1)
6470 usage(0, 1);
6471 /* No command specified; try log with a path */
6472 error = tog_log_with_path(argc, argv);
6473 } else {
6474 if (hflag)
6475 cmd->cmd_usage();
6476 else
6477 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6480 endwin();
6481 putchar('\n');
6482 if (cmd_argv) {
6483 int i;
6484 for (i = 0; i < argc; i++)
6485 free(cmd_argv[i]);
6486 free(cmd_argv);
6489 if (error && error->code != GOT_ERR_CANCELLED)
6490 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6491 return 0;