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 <util.h>
37 #include <limits.h>
38 #include <wchar.h>
39 #include <time.h>
40 #include <pthread.h>
41 #include <libgen.h>
42 #include <regex.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
114 int idx;
115 };
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
118 int ncommits;
119 struct commit_queue_head head;
120 };
122 struct tog_color {
123 SIMPLEQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 SIMPLEQ_HEAD(tog_colors, tog_color);
129 static const struct got_error *
130 add_color(struct tog_colors *colors, const char *pattern,
131 int idx, short color)
133 const struct got_error *err = NULL;
134 struct tog_color *tc;
135 int regerr = 0;
137 if (idx < 1 || idx > COLOR_PAIRS - 1)
138 return NULL;
140 init_pair(idx, color, -1);
142 tc = calloc(1, sizeof(*tc));
143 if (tc == NULL)
144 return got_error_from_errno("calloc");
145 regerr = regcomp(&tc->regex, pattern,
146 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
147 if (regerr) {
148 static char regerr_msg[512];
149 static char err_msg[512];
150 regerror(regerr, &tc->regex, regerr_msg,
151 sizeof(regerr_msg));
152 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
153 regerr_msg);
154 err = got_error_msg(GOT_ERR_REGEX, err_msg);
155 free(tc);
156 return err;
158 tc->colorpair = idx;
159 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
160 return NULL;
163 static void
164 free_colors(struct tog_colors *colors)
166 struct tog_color *tc;
168 while (!SIMPLEQ_EMPTY(colors)) {
169 tc = SIMPLEQ_FIRST(colors);
170 SIMPLEQ_REMOVE_HEAD(colors, entry);
171 regfree(&tc->regex);
172 free(tc);
176 struct tog_color *
177 get_color(struct tog_colors *colors, int colorpair)
179 struct tog_color *tc = NULL;
181 SIMPLEQ_FOREACH(tc, colors, entry) {
182 if (tc->colorpair == colorpair)
183 return tc;
186 return NULL;
189 static int
190 default_color_value(const char *envvar)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 return COLOR_MAGENTA;
194 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
195 return COLOR_CYAN;
196 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
197 return COLOR_YELLOW;
198 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
199 return COLOR_GREEN;
200 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 return COLOR_MAGENTA;
202 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 return COLOR_MAGENTA;
204 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
205 return COLOR_CYAN;
206 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
207 return COLOR_GREEN;
208 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
209 return COLOR_GREEN;
210 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
211 return COLOR_CYAN;
212 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 return COLOR_YELLOW;
214 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
215 return COLOR_GREEN;
216 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 return COLOR_MAGENTA;
218 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
219 return COLOR_YELLOW;
221 return -1;
224 static int
225 get_color_value(const char *envvar)
227 const char *val = getenv(envvar);
229 if (val == NULL)
230 return default_color_value(envvar);
232 if (strcasecmp(val, "black") == 0)
233 return COLOR_BLACK;
234 if (strcasecmp(val, "red") == 0)
235 return COLOR_RED;
236 if (strcasecmp(val, "green") == 0)
237 return COLOR_GREEN;
238 if (strcasecmp(val, "yellow") == 0)
239 return COLOR_YELLOW;
240 if (strcasecmp(val, "blue") == 0)
241 return COLOR_BLUE;
242 if (strcasecmp(val, "magenta") == 0)
243 return COLOR_MAGENTA;
244 if (strcasecmp(val, "cyan") == 0)
245 return COLOR_CYAN;
246 if (strcasecmp(val, "white") == 0)
247 return COLOR_WHITE;
248 if (strcasecmp(val, "default") == 0)
249 return -1;
251 return default_color_value(envvar);
255 struct tog_diff_view_state {
256 struct got_object_id *id1, *id2;
257 const char *label1, *label2;
258 FILE *f;
259 int first_displayed_line;
260 int last_displayed_line;
261 int eof;
262 int diff_context;
263 int ignore_whitespace;
264 int force_text_diff;
265 struct got_repository *repo;
266 struct got_reflist_head refs;
267 struct tog_colors colors;
268 size_t nlines;
269 off_t *line_offsets;
270 int matched_line;
271 int selected_line;
273 /* passed from log view; may be NULL */
274 struct tog_view *log_view;
275 };
277 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 struct tog_log_thread_args {
280 pthread_cond_t need_commits;
281 pthread_cond_t commit_loaded;
282 int commits_needed;
283 struct got_commit_graph *graph;
284 struct commit_queue *commits;
285 const char *in_repo_path;
286 struct got_object_id *start_id;
287 struct got_repository *repo;
288 int log_complete;
289 sig_atomic_t *quit;
290 struct commit_queue_entry **first_displayed_entry;
291 struct commit_queue_entry **selected_entry;
292 int *searching;
293 int *search_next_done;
294 regex_t *regex;
295 };
297 struct tog_log_view_state {
298 struct commit_queue commits;
299 struct commit_queue_entry *first_displayed_entry;
300 struct commit_queue_entry *last_displayed_entry;
301 struct commit_queue_entry *selected_entry;
302 int selected;
303 char *in_repo_path;
304 const char *head_ref_name;
305 int log_branches;
306 struct got_repository *repo;
307 struct got_reflist_head refs;
308 struct got_object_id *start_id;
309 sig_atomic_t quit;
310 pthread_t thread;
311 struct tog_log_thread_args thread_args;
312 struct commit_queue_entry *matched_entry;
313 struct commit_queue_entry *search_entry;
314 struct tog_colors colors;
315 };
317 #define TOG_COLOR_DIFF_MINUS 1
318 #define TOG_COLOR_DIFF_PLUS 2
319 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 #define TOG_COLOR_DIFF_META 4
321 #define TOG_COLOR_TREE_SUBMODULE 5
322 #define TOG_COLOR_TREE_SYMLINK 6
323 #define TOG_COLOR_TREE_DIRECTORY 7
324 #define TOG_COLOR_TREE_EXECUTABLE 8
325 #define TOG_COLOR_COMMIT 9
326 #define TOG_COLOR_AUTHOR 10
327 #define TOG_COLOR_DATE 11
328 #define TOG_COLOR_REFS_HEADS 12
329 #define TOG_COLOR_REFS_TAGS 13
330 #define TOG_COLOR_REFS_REMOTES 14
332 struct tog_blame_cb_args {
333 struct tog_blame_line *lines; /* one per line */
334 int nlines;
336 struct tog_view *view;
337 struct got_object_id *commit_id;
338 int *quit;
339 };
341 struct tog_blame_thread_args {
342 const char *path;
343 struct got_repository *repo;
344 struct tog_blame_cb_args *cb_args;
345 int *complete;
346 got_cancel_cb cancel_cb;
347 void *cancel_arg;
348 };
350 struct tog_blame {
351 FILE *f;
352 off_t filesize;
353 struct tog_blame_line *lines;
354 int nlines;
355 off_t *line_offsets;
356 pthread_t thread;
357 struct tog_blame_thread_args thread_args;
358 struct tog_blame_cb_args cb_args;
359 const char *path;
360 };
362 struct tog_blame_view_state {
363 int first_displayed_line;
364 int last_displayed_line;
365 int selected_line;
366 int blame_complete;
367 int eof;
368 int done;
369 struct got_object_id_queue blamed_commits;
370 struct got_object_qid *blamed_commit;
371 char *path;
372 struct got_repository *repo;
373 struct got_object_id *commit_id;
374 struct tog_blame blame;
375 int matched_line;
376 struct tog_colors colors;
377 };
379 struct tog_parent_tree {
380 TAILQ_ENTRY(tog_parent_tree) entry;
381 struct got_tree_object *tree;
382 struct got_tree_entry *first_displayed_entry;
383 struct got_tree_entry *selected_entry;
384 int selected;
385 };
387 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 struct tog_tree_view_state {
390 char *tree_label;
391 struct got_tree_object *root;
392 struct got_tree_object *tree;
393 struct got_tree_entry *first_displayed_entry;
394 struct got_tree_entry *last_displayed_entry;
395 struct got_tree_entry *selected_entry;
396 int ndisplayed, selected, show_ids;
397 struct tog_parent_trees parents;
398 struct got_object_id *commit_id;
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 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;
450 struct tog_view *parent;
451 struct tog_view *child;
453 /* type-specific state */
454 enum tog_view_type type;
455 union {
456 struct tog_diff_view_state diff;
457 struct tog_log_view_state log;
458 struct tog_blame_view_state blame;
459 struct tog_tree_view_state tree;
460 struct tog_ref_view_state ref;
461 } state;
463 const struct got_error *(*show)(struct tog_view *);
464 const struct got_error *(*input)(struct tog_view **,
465 struct tog_view **, struct tog_view**, struct tog_view *, int);
466 const struct got_error *(*close)(struct tog_view *);
468 const struct got_error *(*search_start)(struct tog_view *);
469 const struct got_error *(*search_next)(struct tog_view *);
470 int searching;
471 #define TOG_SEARCH_FORWARD 1
472 #define TOG_SEARCH_BACKWARD 2
473 int search_next_done;
474 #define TOG_SEARCH_HAVE_MORE 1
475 #define TOG_SEARCH_NO_MORE 2
476 #define TOG_SEARCH_HAVE_NONE 3
477 regex_t regex;
478 regmatch_t regmatch;
479 };
481 static const struct got_error *open_diff_view(struct tog_view *,
482 struct got_object_id *, struct got_object_id *,
483 const char *, const char *, int, int, int, struct tog_view *,
484 struct got_repository *);
485 static const struct got_error *show_diff_view(struct tog_view *);
486 static const struct got_error *input_diff_view(struct tog_view **,
487 struct tog_view **, struct tog_view **, struct tog_view *, int);
488 static const struct got_error* close_diff_view(struct tog_view *);
489 static const struct got_error *search_start_diff_view(struct tog_view *);
490 static const struct got_error *search_next_diff_view(struct tog_view *);
492 static const struct got_error *open_log_view(struct tog_view *,
493 struct got_object_id *, struct got_repository *,
494 const char *, const char *, int);
495 static const struct got_error * show_log_view(struct tog_view *);
496 static const struct got_error *input_log_view(struct tog_view **,
497 struct tog_view **, struct tog_view **, struct tog_view *, int);
498 static const struct got_error *close_log_view(struct tog_view *);
499 static const struct got_error *search_start_log_view(struct tog_view *);
500 static const struct got_error *search_next_log_view(struct tog_view *);
502 static const struct got_error *open_blame_view(struct tog_view *, char *,
503 struct got_object_id *, struct got_repository *);
504 static const struct got_error *show_blame_view(struct tog_view *);
505 static const struct got_error *input_blame_view(struct tog_view **,
506 struct tog_view **, struct tog_view **, struct tog_view *, int);
507 static const struct got_error *close_blame_view(struct tog_view *);
508 static const struct got_error *search_start_blame_view(struct tog_view *);
509 static const struct got_error *search_next_blame_view(struct tog_view *);
511 static const struct got_error *open_tree_view(struct tog_view *,
512 struct got_tree_object *, struct got_object_id *, struct got_repository *);
513 static const struct got_error *show_tree_view(struct tog_view *);
514 static const struct got_error *input_tree_view(struct tog_view **,
515 struct tog_view **, struct tog_view **, struct tog_view *, int);
516 static const struct got_error *close_tree_view(struct tog_view *);
517 static const struct got_error *search_start_tree_view(struct tog_view *);
518 static const struct got_error *search_next_tree_view(struct tog_view *);
520 static const struct got_error *open_ref_view(struct tog_view *,
521 struct got_repository *);
522 static const struct got_error *show_ref_view(struct tog_view *);
523 static const struct got_error *input_ref_view(struct tog_view **,
524 struct tog_view **, struct tog_view **, struct tog_view *, int);
525 static const struct got_error *close_ref_view(struct tog_view *);
526 static const struct got_error *search_start_ref_view(struct tog_view *);
527 static const struct got_error *search_next_ref_view(struct tog_view *);
529 static volatile sig_atomic_t tog_sigwinch_received;
530 static volatile sig_atomic_t tog_sigpipe_received;
531 static volatile sig_atomic_t tog_sigcont_received;
533 static void
534 tog_sigwinch(int signo)
536 tog_sigwinch_received = 1;
539 static void
540 tog_sigpipe(int signo)
542 tog_sigpipe_received = 1;
545 static void
546 tog_sigcont(int signo)
548 tog_sigcont_received = 1;
551 static const struct got_error *
552 view_close(struct tog_view *view)
554 const struct got_error *err = NULL;
556 if (view->child) {
557 view_close(view->child);
558 view->child = NULL;
560 if (view->close)
561 err = view->close(view);
562 if (view->panel)
563 del_panel(view->panel);
564 if (view->window)
565 delwin(view->window);
566 free(view);
567 return err;
570 static struct tog_view *
571 view_open(int nlines, int ncols, int begin_y, int begin_x,
572 enum tog_view_type type)
574 struct tog_view *view = calloc(1, sizeof(*view));
576 if (view == NULL)
577 return NULL;
579 view->type = type;
580 view->lines = LINES;
581 view->cols = COLS;
582 view->nlines = nlines ? nlines : LINES - begin_y;
583 view->ncols = ncols ? ncols : COLS - begin_x;
584 view->begin_y = begin_y;
585 view->begin_x = begin_x;
586 view->window = newwin(nlines, ncols, begin_y, begin_x);
587 if (view->window == NULL) {
588 view_close(view);
589 return NULL;
591 view->panel = new_panel(view->window);
592 if (view->panel == NULL ||
593 set_panel_userptr(view->panel, view) != OK) {
594 view_close(view);
595 return NULL;
598 keypad(view->window, TRUE);
599 return view;
602 static int
603 view_split_begin_x(int begin_x)
605 if (begin_x > 0 || COLS < 120)
606 return 0;
607 return (COLS - MAX(COLS / 2, 80));
610 static const struct got_error *view_resize(struct tog_view *);
612 static const struct got_error *
613 view_splitscreen(struct tog_view *view)
615 const struct got_error *err = NULL;
617 view->begin_y = 0;
618 view->begin_x = view_split_begin_x(0);
619 view->nlines = LINES;
620 view->ncols = COLS - view->begin_x;
621 view->lines = LINES;
622 view->cols = COLS;
623 err = view_resize(view);
624 if (err)
625 return err;
627 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
628 return got_error_from_errno("mvwin");
630 return NULL;
633 static const struct got_error *
634 view_fullscreen(struct tog_view *view)
636 const struct got_error *err = NULL;
638 view->begin_x = 0;
639 view->begin_y = 0;
640 view->nlines = LINES;
641 view->ncols = COLS;
642 view->lines = LINES;
643 view->cols = COLS;
644 err = view_resize(view);
645 if (err)
646 return err;
648 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
649 return got_error_from_errno("mvwin");
651 return NULL;
654 static int
655 view_is_parent_view(struct tog_view *view)
657 return view->parent == NULL;
660 static const struct got_error *
661 view_resize(struct tog_view *view)
663 int nlines, ncols;
665 if (view->lines > LINES)
666 nlines = view->nlines - (view->lines - LINES);
667 else
668 nlines = view->nlines + (LINES - view->lines);
670 if (view->cols > COLS)
671 ncols = view->ncols - (view->cols - COLS);
672 else
673 ncols = view->ncols + (COLS - view->cols);
675 if (wresize(view->window, nlines, ncols) == ERR)
676 return got_error_from_errno("wresize");
677 if (replace_panel(view->panel, view->window) == ERR)
678 return got_error_from_errno("replace_panel");
679 wclear(view->window);
681 view->nlines = nlines;
682 view->ncols = ncols;
683 view->lines = LINES;
684 view->cols = COLS;
686 if (view->child) {
687 view->child->begin_x = view_split_begin_x(view->begin_x);
688 if (view->child->begin_x == 0) {
689 view_fullscreen(view->child);
690 if (view->child->focussed)
691 show_panel(view->child->panel);
692 else
693 show_panel(view->panel);
694 } else {
695 view_splitscreen(view->child);
696 show_panel(view->child->panel);
700 return NULL;
703 static const struct got_error *
704 view_close_child(struct tog_view *view)
706 const struct got_error *err = NULL;
708 if (view->child == NULL)
709 return NULL;
711 err = view_close(view->child);
712 view->child = NULL;
713 return err;
716 static const struct got_error *
717 view_set_child(struct tog_view *view, struct tog_view *child)
719 const struct got_error *err = NULL;
721 view->child = child;
722 child->parent = view;
723 return err;
726 static int
727 view_is_splitscreen(struct tog_view *view)
729 return view->begin_x > 0;
732 static void
733 tog_resizeterm(void)
735 int cols, lines;
736 struct winsize size;
738 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
739 cols = 80; /* Default */
740 lines = 24;
741 } else {
742 cols = size.ws_col;
743 lines = size.ws_row;
745 resize_term(lines, cols);
748 static const struct got_error *
749 view_search_start(struct tog_view *view)
751 const struct got_error *err = NULL;
752 char pattern[1024];
753 int ret;
755 if (view->nlines < 1)
756 return NULL;
758 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
759 wclrtoeol(view->window);
761 nocbreak();
762 echo();
763 ret = wgetnstr(view->window, pattern, sizeof(pattern));
764 cbreak();
765 noecho();
766 if (ret == ERR)
767 return NULL;
769 if (view->searching) {
770 regfree(&view->regex);
771 view->searching = 0;
774 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
775 err = view->search_start(view);
776 if (err) {
777 regfree(&view->regex);
778 return err;
780 view->searching = TOG_SEARCH_FORWARD;
781 view->search_next_done = 0;
782 view->search_next(view);
785 return NULL;
788 static const struct got_error *
789 view_input(struct tog_view **new, struct tog_view **dead,
790 struct tog_view **focus, int *done, struct tog_view *view,
791 struct tog_view_list_head *views)
793 const struct got_error *err = NULL;
794 struct tog_view *v;
795 int ch, errcode;
797 *new = NULL;
798 *dead = NULL;
799 *focus = NULL;
801 /* Clear "no matches" indicator. */
802 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
803 view->search_next_done == TOG_SEARCH_HAVE_NONE)
804 view->search_next_done = TOG_SEARCH_HAVE_MORE;
806 if (view->searching && !view->search_next_done) {
807 errcode = pthread_mutex_unlock(&tog_mutex);
808 if (errcode)
809 return got_error_set_errno(errcode,
810 "pthread_mutex_unlock");
811 pthread_yield();
812 errcode = pthread_mutex_lock(&tog_mutex);
813 if (errcode)
814 return got_error_set_errno(errcode,
815 "pthread_mutex_lock");
816 view->search_next(view);
817 return NULL;
820 nodelay(stdscr, FALSE);
821 /* Allow threads to make progress while we are waiting for input. */
822 errcode = pthread_mutex_unlock(&tog_mutex);
823 if (errcode)
824 return got_error_set_errno(errcode, "pthread_mutex_unlock");
825 ch = wgetch(view->window);
826 errcode = pthread_mutex_lock(&tog_mutex);
827 if (errcode)
828 return got_error_set_errno(errcode, "pthread_mutex_lock");
829 nodelay(stdscr, TRUE);
831 if (tog_sigwinch_received || tog_sigcont_received) {
832 tog_resizeterm();
833 tog_sigwinch_received = 0;
834 tog_sigcont_received = 0;
835 TAILQ_FOREACH(v, views, entry) {
836 err = view_resize(v);
837 if (err)
838 return err;
839 err = v->input(new, dead, focus, v, KEY_RESIZE);
840 if (err)
841 return err;
845 switch (ch) {
846 case ERR:
847 break;
848 case '\t':
849 if (view->child) {
850 *focus = view->child;
851 } else if (view->parent) {
852 *focus = view->parent;
854 break;
855 case 'q':
856 err = view->input(new, dead, focus, view, ch);
857 *dead = view;
858 break;
859 case 'Q':
860 *done = 1;
861 break;
862 case 'f':
863 if (view_is_parent_view(view)) {
864 if (view->child == NULL)
865 break;
866 if (view_is_splitscreen(view->child)) {
867 *focus = view->child;
868 err = view_fullscreen(view->child);
869 } else
870 err = view_splitscreen(view->child);
871 if (err)
872 break;
873 err = view->child->input(new, dead, focus,
874 view->child, KEY_RESIZE);
875 } else {
876 if (view_is_splitscreen(view)) {
877 *focus = view;
878 err = view_fullscreen(view);
879 } else {
880 err = view_splitscreen(view);
882 if (err)
883 break;
884 err = view->input(new, dead, focus, view,
885 KEY_RESIZE);
887 break;
888 case KEY_RESIZE:
889 break;
890 case '/':
891 if (view->search_start)
892 view_search_start(view);
893 else
894 err = view->input(new, dead, focus, view, ch);
895 break;
896 case 'N':
897 case 'n':
898 if (view->search_next) {
899 view->searching = (ch == 'n' ?
900 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
901 view->search_next_done = 0;
902 view->search_next(view);
903 } else
904 err = view->input(new, dead, focus, view, ch);
905 break;
906 default:
907 err = view->input(new, dead, focus, view, ch);
908 break;
911 return err;
914 void
915 view_vborder(struct tog_view *view)
917 PANEL *panel;
918 struct tog_view *view_above;
920 if (view->parent)
921 return view_vborder(view->parent);
923 panel = panel_above(view->panel);
924 if (panel == NULL)
925 return;
927 view_above = panel_userptr(panel);
928 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
929 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
932 int
933 view_needs_focus_indication(struct tog_view *view)
935 if (view_is_parent_view(view)) {
936 if (view->child == NULL || view->child->focussed)
937 return 0;
938 if (!view_is_splitscreen(view->child))
939 return 0;
940 } else if (!view_is_splitscreen(view))
941 return 0;
943 return view->focussed;
946 static const struct got_error *
947 view_loop(struct tog_view *view)
949 const struct got_error *err = NULL;
950 struct tog_view_list_head views;
951 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
952 int fast_refresh = 10;
953 int done = 0, errcode;
955 errcode = pthread_mutex_lock(&tog_mutex);
956 if (errcode)
957 return got_error_set_errno(errcode, "pthread_mutex_lock");
959 TAILQ_INIT(&views);
960 TAILQ_INSERT_HEAD(&views, view, entry);
962 main_view = view;
963 view->focussed = 1;
964 err = view->show(view);
965 if (err)
966 return err;
967 update_panels();
968 doupdate();
969 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
970 /* Refresh fast during initialization, then become slower. */
971 if (fast_refresh && fast_refresh-- == 0)
972 halfdelay(10); /* switch to once per second */
974 err = view_input(&new_view, &dead_view, &focus_view, &done,
975 view, &views);
976 if (err)
977 break;
978 if (dead_view) {
979 struct tog_view *prev = NULL;
981 if (view_is_parent_view(dead_view))
982 prev = TAILQ_PREV(dead_view,
983 tog_view_list_head, entry);
984 else if (view->parent != dead_view)
985 prev = view->parent;
987 if (dead_view->parent)
988 dead_view->parent->child = NULL;
989 else
990 TAILQ_REMOVE(&views, dead_view, entry);
992 err = view_close(dead_view);
993 if (err || (dead_view == main_view && new_view == NULL))
994 goto done;
996 if (view == dead_view) {
997 if (focus_view)
998 view = focus_view;
999 else if (prev)
1000 view = prev;
1001 else if (!TAILQ_EMPTY(&views))
1002 view = TAILQ_LAST(&views,
1003 tog_view_list_head);
1004 else
1005 view = NULL;
1006 if (view) {
1007 if (view->child &&
1008 view->child->focussed)
1009 focus_view = view->child;
1010 else
1011 focus_view = view;
1015 if (new_view) {
1016 struct tog_view *v, *t;
1017 /* Only allow one parent view per type. */
1018 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1019 if (v->type != new_view->type)
1020 continue;
1021 TAILQ_REMOVE(&views, v, entry);
1022 err = view_close(v);
1023 if (err)
1024 goto done;
1025 break;
1027 TAILQ_INSERT_TAIL(&views, new_view, entry);
1028 view = new_view;
1029 if (focus_view == NULL)
1030 focus_view = new_view;
1032 if (focus_view) {
1033 show_panel(focus_view->panel);
1034 if (view)
1035 view->focussed = 0;
1036 focus_view->focussed = 1;
1037 view = focus_view;
1038 if (new_view)
1039 show_panel(new_view->panel);
1040 if (view->child && view_is_splitscreen(view->child))
1041 show_panel(view->child->panel);
1043 if (view) {
1044 if (focus_view == NULL) {
1045 view->focussed = 1;
1046 show_panel(view->panel);
1047 if (view->child && view_is_splitscreen(view->child))
1048 show_panel(view->child->panel);
1049 focus_view = view;
1051 if (view->parent) {
1052 err = view->parent->show(view->parent);
1053 if (err)
1054 goto done;
1056 err = view->show(view);
1057 if (err)
1058 goto done;
1059 if (view->child) {
1060 err = view->child->show(view->child);
1061 if (err)
1062 goto done;
1064 update_panels();
1065 doupdate();
1068 done:
1069 while (!TAILQ_EMPTY(&views)) {
1070 view = TAILQ_FIRST(&views);
1071 TAILQ_REMOVE(&views, view, entry);
1072 view_close(view);
1075 errcode = pthread_mutex_unlock(&tog_mutex);
1076 if (errcode && err == NULL)
1077 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1079 return err;
1082 __dead static void
1083 usage_log(void)
1085 endwin();
1086 fprintf(stderr,
1087 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1088 getprogname());
1089 exit(1);
1092 /* Create newly allocated wide-character string equivalent to a byte string. */
1093 static const struct got_error *
1094 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1096 char *vis = NULL;
1097 const struct got_error *err = NULL;
1099 *ws = NULL;
1100 *wlen = mbstowcs(NULL, s, 0);
1101 if (*wlen == (size_t)-1) {
1102 int vislen;
1103 if (errno != EILSEQ)
1104 return got_error_from_errno("mbstowcs");
1106 /* byte string invalid in current encoding; try to "fix" it */
1107 err = got_mbsavis(&vis, &vislen, s);
1108 if (err)
1109 return err;
1110 *wlen = mbstowcs(NULL, vis, 0);
1111 if (*wlen == (size_t)-1) {
1112 err = got_error_from_errno("mbstowcs"); /* give up */
1113 goto done;
1117 *ws = calloc(*wlen + 1, sizeof(**ws));
1118 if (*ws == NULL) {
1119 err = got_error_from_errno("calloc");
1120 goto done;
1123 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1124 err = got_error_from_errno("mbstowcs");
1125 done:
1126 free(vis);
1127 if (err) {
1128 free(*ws);
1129 *ws = NULL;
1130 *wlen = 0;
1132 return err;
1135 /* Format a line for display, ensuring that it won't overflow a width limit. */
1136 static const struct got_error *
1137 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1138 int col_tab_align)
1140 const struct got_error *err = NULL;
1141 int cols = 0;
1142 wchar_t *wline = NULL;
1143 size_t wlen;
1144 int i;
1146 *wlinep = NULL;
1147 *widthp = 0;
1149 err = mbs2ws(&wline, &wlen, line);
1150 if (err)
1151 return err;
1153 i = 0;
1154 while (i < wlen) {
1155 int width = wcwidth(wline[i]);
1157 if (width == 0) {
1158 i++;
1159 continue;
1162 if (width == 1 || width == 2) {
1163 if (cols + width > wlimit)
1164 break;
1165 cols += width;
1166 i++;
1167 } else if (width == -1) {
1168 if (wline[i] == L'\t') {
1169 width = TABSIZE -
1170 ((cols + col_tab_align) % TABSIZE);
1171 if (cols + width > wlimit)
1172 break;
1173 cols += width;
1175 i++;
1176 } else {
1177 err = got_error_from_errno("wcwidth");
1178 goto done;
1181 wline[i] = L'\0';
1182 if (widthp)
1183 *widthp = cols;
1184 done:
1185 if (err)
1186 free(wline);
1187 else
1188 *wlinep = wline;
1189 return err;
1192 static const struct got_error*
1193 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1194 struct got_object_id *id, struct got_repository *repo)
1196 static const struct got_error *err = NULL;
1197 struct got_reflist_entry *re;
1198 char *s;
1199 const char *name;
1201 *refs_str = NULL;
1203 SIMPLEQ_FOREACH(re, refs, entry) {
1204 struct got_tag_object *tag = NULL;
1205 struct got_object_id *ref_id;
1206 int cmp;
1208 name = got_ref_get_name(re->ref);
1209 if (strcmp(name, GOT_REF_HEAD) == 0)
1210 continue;
1211 if (strncmp(name, "refs/", 5) == 0)
1212 name += 5;
1213 if (strncmp(name, "got/", 4) == 0)
1214 continue;
1215 if (strncmp(name, "heads/", 6) == 0)
1216 name += 6;
1217 if (strncmp(name, "remotes/", 8) == 0) {
1218 name += 8;
1219 s = strstr(name, "/" GOT_REF_HEAD);
1220 if (s != NULL && s[strlen(s)] == '\0')
1221 continue;
1223 err = got_ref_resolve(&ref_id, repo, re->ref);
1224 if (err)
1225 break;
1226 if (strncmp(name, "tags/", 5) == 0) {
1227 err = got_object_open_as_tag(&tag, repo, ref_id);
1228 if (err) {
1229 if (err->code != GOT_ERR_OBJ_TYPE) {
1230 free(ref_id);
1231 break;
1233 /* Ref points at something other than a tag. */
1234 err = NULL;
1235 tag = NULL;
1238 cmp = got_object_id_cmp(tag ?
1239 got_object_tag_get_object_id(tag) : ref_id, id);
1240 free(ref_id);
1241 if (tag)
1242 got_object_tag_close(tag);
1243 if (cmp != 0)
1244 continue;
1245 s = *refs_str;
1246 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1247 s ? ", " : "", name) == -1) {
1248 err = got_error_from_errno("asprintf");
1249 free(s);
1250 *refs_str = NULL;
1251 break;
1253 free(s);
1256 return err;
1259 static const struct got_error *
1260 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1261 int col_tab_align)
1263 char *smallerthan, *at;
1265 smallerthan = strchr(author, '<');
1266 if (smallerthan && smallerthan[1] != '\0')
1267 author = smallerthan + 1;
1268 at = strchr(author, '@');
1269 if (at)
1270 *at = '\0';
1271 return format_line(wauthor, author_width, author, limit, col_tab_align);
1274 static const struct got_error *
1275 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1276 struct got_object_id *id, const size_t date_display_cols,
1277 int author_display_cols)
1279 struct tog_log_view_state *s = &view->state.log;
1280 const struct got_error *err = NULL;
1281 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1282 char *logmsg0 = NULL, *logmsg = NULL;
1283 char *author = NULL;
1284 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1285 int author_width, logmsg_width;
1286 char *newline, *line = NULL;
1287 int col, limit;
1288 const int avail = view->ncols;
1289 struct tm tm;
1290 time_t committer_time;
1291 struct tog_color *tc;
1293 committer_time = got_object_commit_get_committer_time(commit);
1294 if (localtime_r(&committer_time, &tm) == NULL)
1295 return got_error_from_errno("localtime_r");
1296 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1297 >= sizeof(datebuf))
1298 return got_error(GOT_ERR_NO_SPACE);
1300 if (avail <= date_display_cols)
1301 limit = MIN(sizeof(datebuf) - 1, avail);
1302 else
1303 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1304 tc = get_color(&s->colors, TOG_COLOR_DATE);
1305 if (tc)
1306 wattr_on(view->window,
1307 COLOR_PAIR(tc->colorpair), NULL);
1308 waddnstr(view->window, datebuf, limit);
1309 if (tc)
1310 wattr_off(view->window,
1311 COLOR_PAIR(tc->colorpair), NULL);
1312 col = limit;
1313 if (col > avail)
1314 goto done;
1316 if (avail >= 120) {
1317 char *id_str;
1318 err = got_object_id_str(&id_str, id);
1319 if (err)
1320 goto done;
1321 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1322 if (tc)
1323 wattr_on(view->window,
1324 COLOR_PAIR(tc->colorpair), NULL);
1325 wprintw(view->window, "%.8s ", id_str);
1326 if (tc)
1327 wattr_off(view->window,
1328 COLOR_PAIR(tc->colorpair), NULL);
1329 free(id_str);
1330 col += 9;
1331 if (col > avail)
1332 goto done;
1335 author = strdup(got_object_commit_get_author(commit));
1336 if (author == NULL) {
1337 err = got_error_from_errno("strdup");
1338 goto done;
1340 err = format_author(&wauthor, &author_width, author, avail - col, col);
1341 if (err)
1342 goto done;
1343 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1344 if (tc)
1345 wattr_on(view->window,
1346 COLOR_PAIR(tc->colorpair), NULL);
1347 waddwstr(view->window, wauthor);
1348 if (tc)
1349 wattr_off(view->window,
1350 COLOR_PAIR(tc->colorpair), NULL);
1351 col += author_width;
1352 while (col < avail && author_width < author_display_cols + 2) {
1353 waddch(view->window, ' ');
1354 col++;
1355 author_width++;
1357 if (col > avail)
1358 goto done;
1360 err = got_object_commit_get_logmsg(&logmsg0, commit);
1361 if (err)
1362 goto done;
1363 logmsg = logmsg0;
1364 while (*logmsg == '\n')
1365 logmsg++;
1366 newline = strchr(logmsg, '\n');
1367 if (newline)
1368 *newline = '\0';
1369 limit = avail - col;
1370 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1371 if (err)
1372 goto done;
1373 waddwstr(view->window, wlogmsg);
1374 col += logmsg_width;
1375 while (col < avail) {
1376 waddch(view->window, ' ');
1377 col++;
1379 done:
1380 free(logmsg0);
1381 free(wlogmsg);
1382 free(author);
1383 free(wauthor);
1384 free(line);
1385 return err;
1388 static struct commit_queue_entry *
1389 alloc_commit_queue_entry(struct got_commit_object *commit,
1390 struct got_object_id *id)
1392 struct commit_queue_entry *entry;
1394 entry = calloc(1, sizeof(*entry));
1395 if (entry == NULL)
1396 return NULL;
1398 entry->id = id;
1399 entry->commit = commit;
1400 return entry;
1403 static void
1404 pop_commit(struct commit_queue *commits)
1406 struct commit_queue_entry *entry;
1408 entry = TAILQ_FIRST(&commits->head);
1409 TAILQ_REMOVE(&commits->head, entry, entry);
1410 got_object_commit_close(entry->commit);
1411 commits->ncommits--;
1412 /* Don't free entry->id! It is owned by the commit graph. */
1413 free(entry);
1416 static void
1417 free_commits(struct commit_queue *commits)
1419 while (!TAILQ_EMPTY(&commits->head))
1420 pop_commit(commits);
1423 static const struct got_error *
1424 match_commit(int *have_match, struct got_object_id *id,
1425 struct got_commit_object *commit, regex_t *regex)
1427 const struct got_error *err = NULL;
1428 regmatch_t regmatch;
1429 char *id_str = NULL, *logmsg = NULL;
1431 *have_match = 0;
1433 err = got_object_id_str(&id_str, id);
1434 if (err)
1435 return err;
1437 err = got_object_commit_get_logmsg(&logmsg, commit);
1438 if (err)
1439 goto done;
1441 if (regexec(regex, got_object_commit_get_author(commit), 1,
1442 &regmatch, 0) == 0 ||
1443 regexec(regex, got_object_commit_get_committer(commit), 1,
1444 &regmatch, 0) == 0 ||
1445 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1446 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1447 *have_match = 1;
1448 done:
1449 free(id_str);
1450 free(logmsg);
1451 return err;
1454 static const struct got_error *
1455 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1456 int minqueue, struct got_repository *repo, const char *path,
1457 int *searching, int *search_next_done, regex_t *regex)
1459 const struct got_error *err = NULL;
1460 int nqueued = 0;
1463 * We keep all commits open throughout the lifetime of the log
1464 * view in order to avoid having to re-fetch commits from disk
1465 * while updating the display.
1467 while (nqueued < minqueue ||
1468 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1469 struct got_object_id *id;
1470 struct got_commit_object *commit;
1471 struct commit_queue_entry *entry;
1472 int errcode;
1474 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1475 if (err || id == NULL)
1476 break;
1478 err = got_object_open_as_commit(&commit, repo, id);
1479 if (err)
1480 break;
1481 entry = alloc_commit_queue_entry(commit, id);
1482 if (entry == NULL) {
1483 err = got_error_from_errno("alloc_commit_queue_entry");
1484 break;
1487 errcode = pthread_mutex_lock(&tog_mutex);
1488 if (errcode) {
1489 err = got_error_set_errno(errcode,
1490 "pthread_mutex_lock");
1491 break;
1494 entry->idx = commits->ncommits;
1495 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1496 nqueued++;
1497 commits->ncommits++;
1499 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1500 int have_match;
1501 err = match_commit(&have_match, id, commit, regex);
1502 if (err)
1503 break;
1504 if (have_match)
1505 *search_next_done = TOG_SEARCH_HAVE_MORE;
1508 errcode = pthread_mutex_unlock(&tog_mutex);
1509 if (errcode && err == NULL)
1510 err = got_error_set_errno(errcode,
1511 "pthread_mutex_unlock");
1512 if (err)
1513 break;
1516 return err;
1519 static const struct got_error *
1520 draw_commits(struct tog_view *view)
1522 const struct got_error *err = NULL;
1523 struct tog_log_view_state *s = &view->state.log;
1524 struct commit_queue_entry *entry;
1525 const int limit = view->nlines;
1526 int width;
1527 int ncommits, author_cols = 4;
1528 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1529 char *refs_str = NULL;
1530 wchar_t *wline;
1531 struct tog_color *tc;
1532 static const size_t date_display_cols = 12;
1534 entry = s->first_displayed_entry;
1535 ncommits = 0;
1536 while (entry) {
1537 if (ncommits == s->selected) {
1538 s->selected_entry = entry;
1539 break;
1541 entry = TAILQ_NEXT(entry, entry);
1542 ncommits++;
1545 if (s->selected_entry &&
1546 !(view->searching && view->search_next_done == 0)) {
1547 err = got_object_id_str(&id_str, s->selected_entry->id);
1548 if (err)
1549 return err;
1550 err = build_refs_str(&refs_str, &s->refs,
1551 s->selected_entry->id, s->repo);
1552 if (err)
1553 goto done;
1556 if (s->thread_args.commits_needed == 0)
1557 halfdelay(10); /* disable fast refresh */
1559 if (s->thread_args.commits_needed > 0) {
1560 if (asprintf(&ncommits_str, " [%d/%d] %s",
1561 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1562 (view->searching && !view->search_next_done) ?
1563 "searching..." : "loading...") == -1) {
1564 err = got_error_from_errno("asprintf");
1565 goto done;
1567 } else {
1568 const char *search_str = NULL;
1570 if (view->searching) {
1571 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1572 search_str = "no more matches";
1573 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1574 search_str = "no matches found";
1575 else if (!view->search_next_done)
1576 search_str = "searching...";
1579 if (asprintf(&ncommits_str, " [%d/%d] %s",
1580 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1581 search_str ? search_str :
1582 (refs_str ? refs_str : "")) == -1) {
1583 err = got_error_from_errno("asprintf");
1584 goto done;
1588 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1589 if (asprintf(&header, "commit %s %s%s",
1590 id_str ? id_str : "........................................",
1591 s->in_repo_path, ncommits_str) == -1) {
1592 err = got_error_from_errno("asprintf");
1593 header = NULL;
1594 goto done;
1596 } else if (asprintf(&header, "commit %s%s",
1597 id_str ? id_str : "........................................",
1598 ncommits_str) == -1) {
1599 err = got_error_from_errno("asprintf");
1600 header = NULL;
1601 goto done;
1603 err = format_line(&wline, &width, header, view->ncols, 0);
1604 if (err)
1605 goto done;
1607 werase(view->window);
1609 if (view_needs_focus_indication(view))
1610 wstandout(view->window);
1611 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1612 if (tc)
1613 wattr_on(view->window,
1614 COLOR_PAIR(tc->colorpair), NULL);
1615 waddwstr(view->window, wline);
1616 if (tc)
1617 wattr_off(view->window,
1618 COLOR_PAIR(tc->colorpair), NULL);
1619 while (width < view->ncols) {
1620 waddch(view->window, ' ');
1621 width++;
1623 if (view_needs_focus_indication(view))
1624 wstandend(view->window);
1625 free(wline);
1626 if (limit <= 1)
1627 goto done;
1629 /* Grow author column size if necessary. */
1630 entry = s->first_displayed_entry;
1631 ncommits = 0;
1632 while (entry) {
1633 char *author;
1634 wchar_t *wauthor;
1635 int width;
1636 if (ncommits >= limit - 1)
1637 break;
1638 author = strdup(got_object_commit_get_author(entry->commit));
1639 if (author == NULL) {
1640 err = got_error_from_errno("strdup");
1641 goto done;
1643 err = format_author(&wauthor, &width, author, COLS,
1644 date_display_cols);
1645 if (author_cols < width)
1646 author_cols = width;
1647 free(wauthor);
1648 free(author);
1649 ncommits++;
1650 entry = TAILQ_NEXT(entry, entry);
1653 entry = s->first_displayed_entry;
1654 s->last_displayed_entry = s->first_displayed_entry;
1655 ncommits = 0;
1656 while (entry) {
1657 if (ncommits >= limit - 1)
1658 break;
1659 if (ncommits == s->selected)
1660 wstandout(view->window);
1661 err = draw_commit(view, entry->commit, entry->id,
1662 date_display_cols, author_cols);
1663 if (ncommits == s->selected)
1664 wstandend(view->window);
1665 if (err)
1666 goto done;
1667 ncommits++;
1668 s->last_displayed_entry = entry;
1669 entry = TAILQ_NEXT(entry, entry);
1672 view_vborder(view);
1673 done:
1674 free(id_str);
1675 free(refs_str);
1676 free(ncommits_str);
1677 free(header);
1678 return err;
1681 static void
1682 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1684 struct commit_queue_entry *entry;
1685 int nscrolled = 0;
1687 entry = TAILQ_FIRST(&s->commits.head);
1688 if (s->first_displayed_entry == entry)
1689 return;
1691 entry = s->first_displayed_entry;
1692 while (entry && nscrolled < maxscroll) {
1693 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1694 if (entry) {
1695 s->first_displayed_entry = entry;
1696 nscrolled++;
1701 static const struct got_error *
1702 trigger_log_thread(struct tog_view *view, int wait)
1704 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1705 int errcode;
1707 halfdelay(1); /* fast refresh while loading commits */
1709 while (ta->commits_needed > 0) {
1710 if (ta->log_complete)
1711 break;
1713 /* Wake the log thread. */
1714 errcode = pthread_cond_signal(&ta->need_commits);
1715 if (errcode)
1716 return got_error_set_errno(errcode,
1717 "pthread_cond_signal");
1720 * The mutex will be released while the view loop waits
1721 * in wgetch(), at which time the log thread will run.
1723 if (!wait)
1724 break;
1726 /* Display progress update in log view. */
1727 show_log_view(view);
1728 update_panels();
1729 doupdate();
1731 /* Wait right here while next commit is being loaded. */
1732 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1733 if (errcode)
1734 return got_error_set_errno(errcode,
1735 "pthread_cond_wait");
1737 /* Display progress update in log view. */
1738 show_log_view(view);
1739 update_panels();
1740 doupdate();
1743 return NULL;
1746 static const struct got_error *
1747 log_scroll_down(struct tog_view *view, int maxscroll)
1749 struct tog_log_view_state *s = &view->state.log;
1750 const struct got_error *err = NULL;
1751 struct commit_queue_entry *pentry;
1752 int nscrolled = 0, ncommits_needed;
1754 if (s->last_displayed_entry == NULL)
1755 return NULL;
1757 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1758 if (s->commits.ncommits < ncommits_needed &&
1759 !s->thread_args.log_complete) {
1761 * Ask the log thread for required amount of commits.
1763 s->thread_args.commits_needed += maxscroll;
1764 err = trigger_log_thread(view, 1);
1765 if (err)
1766 return err;
1769 do {
1770 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1771 if (pentry == NULL)
1772 break;
1774 s->last_displayed_entry = pentry;
1776 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1777 if (pentry == NULL)
1778 break;
1779 s->first_displayed_entry = pentry;
1780 } while (++nscrolled < maxscroll);
1782 return err;
1785 static const struct got_error *
1786 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1787 struct got_commit_object *commit, struct got_object_id *commit_id,
1788 struct tog_view *log_view, struct got_repository *repo)
1790 const struct got_error *err;
1791 struct got_object_qid *parent_id;
1792 struct tog_view *diff_view;
1794 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1795 if (diff_view == NULL)
1796 return got_error_from_errno("view_open");
1798 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1799 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1800 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1801 if (err == NULL)
1802 *new_view = diff_view;
1803 return err;
1806 static const struct got_error *
1807 tree_view_visit_subtree(struct tog_tree_view_state *s,
1808 struct got_tree_object *subtree)
1810 struct tog_parent_tree *parent;
1812 parent = calloc(1, sizeof(*parent));
1813 if (parent == NULL)
1814 return got_error_from_errno("calloc");
1816 parent->tree = s->tree;
1817 parent->first_displayed_entry = s->first_displayed_entry;
1818 parent->selected_entry = s->selected_entry;
1819 parent->selected = s->selected;
1820 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1821 s->tree = subtree;
1822 s->selected = 0;
1823 s->first_displayed_entry = NULL;
1824 return NULL;
1827 static const struct got_error *
1828 tree_view_walk_path(struct tog_tree_view_state *s,
1829 struct got_object_id *commit_id, const char *path)
1831 const struct got_error *err = NULL;
1832 struct got_tree_object *tree = NULL;
1833 const char *p;
1834 char *slash, *subpath = NULL;
1836 /* Walk the path and open corresponding tree objects. */
1837 p = path;
1838 while (*p) {
1839 struct got_tree_entry *te;
1840 struct got_object_id *tree_id;
1841 char *te_name;
1843 while (p[0] == '/')
1844 p++;
1846 /* Ensure the correct subtree entry is selected. */
1847 slash = strchr(p, '/');
1848 if (slash == NULL)
1849 te_name = strdup(p);
1850 else
1851 te_name = strndup(p, slash - p);
1852 if (te_name == NULL) {
1853 err = got_error_from_errno("strndup");
1854 break;
1856 te = got_object_tree_find_entry(s->tree, te_name);
1857 if (te == NULL) {
1858 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1859 free(te_name);
1860 break;
1862 free(te_name);
1863 s->first_displayed_entry = s->selected_entry = te;
1865 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1866 break; /* jump to this file's entry */
1868 slash = strchr(p, '/');
1869 if (slash)
1870 subpath = strndup(path, slash - path);
1871 else
1872 subpath = strdup(path);
1873 if (subpath == NULL) {
1874 err = got_error_from_errno("strdup");
1875 break;
1878 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1879 subpath);
1880 if (err)
1881 break;
1883 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1884 free(tree_id);
1885 if (err)
1886 break;
1888 err = tree_view_visit_subtree(s, tree);
1889 if (err) {
1890 got_object_tree_close(tree);
1891 break;
1893 if (slash == NULL)
1894 break;
1895 free(subpath);
1896 subpath = NULL;
1897 p = slash;
1900 free(subpath);
1901 return err;
1904 static const struct got_error *
1905 browse_commit_tree(struct tog_view **new_view, int begin_x,
1906 struct commit_queue_entry *entry, const char *path,
1907 struct got_repository *repo)
1909 const struct got_error *err = NULL;
1910 struct got_tree_object *tree;
1911 struct tog_tree_view_state *s;
1912 struct tog_view *tree_view;
1914 err = got_object_open_as_tree(&tree, repo,
1915 got_object_commit_get_tree_id(entry->commit));
1916 if (err)
1917 return err;
1919 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1920 if (tree_view == NULL)
1921 return got_error_from_errno("view_open");
1923 err = open_tree_view(tree_view, tree, entry->id, repo);
1924 if (err) {
1925 got_object_tree_close(tree);
1926 return err;
1928 s = &tree_view->state.tree;
1930 *new_view = tree_view;
1932 if (got_path_is_root_dir(path))
1933 return NULL;
1935 return tree_view_walk_path(s, entry->id, path);
1938 static const struct got_error *
1939 block_signals_used_by_main_thread(void)
1941 sigset_t sigset;
1942 int errcode;
1944 if (sigemptyset(&sigset) == -1)
1945 return got_error_from_errno("sigemptyset");
1947 /* tog handles SIGWINCH and SIGCONT */
1948 if (sigaddset(&sigset, SIGWINCH) == -1)
1949 return got_error_from_errno("sigaddset");
1950 if (sigaddset(&sigset, SIGCONT) == -1)
1951 return got_error_from_errno("sigaddset");
1953 /* ncurses handles SIGTSTP */
1954 if (sigaddset(&sigset, SIGTSTP) == -1)
1955 return got_error_from_errno("sigaddset");
1957 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1958 if (errcode)
1959 return got_error_set_errno(errcode, "pthread_sigmask");
1961 return NULL;
1964 static void *
1965 log_thread(void *arg)
1967 const struct got_error *err = NULL;
1968 int errcode = 0;
1969 struct tog_log_thread_args *a = arg;
1970 int done = 0;
1972 err = block_signals_used_by_main_thread();
1973 if (err)
1974 return (void *)err;
1976 while (!done && !err && !tog_sigpipe_received) {
1977 err = queue_commits(a->graph, a->commits, 1, a->repo,
1978 a->in_repo_path, a->searching, a->search_next_done,
1979 a->regex);
1980 if (err) {
1981 if (err->code != GOT_ERR_ITER_COMPLETED)
1982 return (void *)err;
1983 err = NULL;
1984 done = 1;
1985 } else if (a->commits_needed > 0)
1986 a->commits_needed--;
1988 errcode = pthread_mutex_lock(&tog_mutex);
1989 if (errcode) {
1990 err = got_error_set_errno(errcode,
1991 "pthread_mutex_lock");
1992 break;
1993 } else if (*a->quit)
1994 done = 1;
1995 else if (*a->first_displayed_entry == NULL) {
1996 *a->first_displayed_entry =
1997 TAILQ_FIRST(&a->commits->head);
1998 *a->selected_entry = *a->first_displayed_entry;
2001 errcode = pthread_cond_signal(&a->commit_loaded);
2002 if (errcode) {
2003 err = got_error_set_errno(errcode,
2004 "pthread_cond_signal");
2005 pthread_mutex_unlock(&tog_mutex);
2006 break;
2009 if (done)
2010 a->commits_needed = 0;
2011 else {
2012 if (a->commits_needed == 0) {
2013 errcode = pthread_cond_wait(&a->need_commits,
2014 &tog_mutex);
2015 if (errcode)
2016 err = got_error_set_errno(errcode,
2017 "pthread_cond_wait");
2021 errcode = pthread_mutex_unlock(&tog_mutex);
2022 if (errcode && err == NULL)
2023 err = got_error_set_errno(errcode,
2024 "pthread_mutex_unlock");
2026 a->log_complete = 1;
2027 return (void *)err;
2030 static const struct got_error *
2031 stop_log_thread(struct tog_log_view_state *s)
2033 const struct got_error *err = NULL;
2034 int errcode;
2036 if (s->thread) {
2037 s->quit = 1;
2038 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2039 if (errcode)
2040 return got_error_set_errno(errcode,
2041 "pthread_cond_signal");
2042 errcode = pthread_mutex_unlock(&tog_mutex);
2043 if (errcode)
2044 return got_error_set_errno(errcode,
2045 "pthread_mutex_unlock");
2046 errcode = pthread_join(s->thread, (void **)&err);
2047 if (errcode)
2048 return got_error_set_errno(errcode, "pthread_join");
2049 errcode = pthread_mutex_lock(&tog_mutex);
2050 if (errcode)
2051 return got_error_set_errno(errcode,
2052 "pthread_mutex_lock");
2053 s->thread = NULL;
2056 if (s->thread_args.repo) {
2057 got_repo_close(s->thread_args.repo);
2058 s->thread_args.repo = NULL;
2061 if (s->thread_args.graph) {
2062 got_commit_graph_close(s->thread_args.graph);
2063 s->thread_args.graph = NULL;
2066 return err;
2069 static const struct got_error *
2070 close_log_view(struct tog_view *view)
2072 const struct got_error *err = NULL;
2073 struct tog_log_view_state *s = &view->state.log;
2074 int errcode;
2076 err = stop_log_thread(s);
2078 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2079 if (errcode && err == NULL)
2080 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2082 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2083 if (errcode && err == NULL)
2084 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2086 free_commits(&s->commits);
2087 free(s->in_repo_path);
2088 s->in_repo_path = NULL;
2089 free(s->start_id);
2090 s->start_id = NULL;
2091 got_ref_list_free(&s->refs);
2092 return err;
2095 static const struct got_error *
2096 search_start_log_view(struct tog_view *view)
2098 struct tog_log_view_state *s = &view->state.log;
2100 s->matched_entry = NULL;
2101 s->search_entry = NULL;
2102 return NULL;
2105 static const struct got_error *
2106 search_next_log_view(struct tog_view *view)
2108 const struct got_error *err = NULL;
2109 struct tog_log_view_state *s = &view->state.log;
2110 struct commit_queue_entry *entry;
2112 /* Display progress update in log view. */
2113 show_log_view(view);
2114 update_panels();
2115 doupdate();
2117 if (s->search_entry) {
2118 int errcode, ch;
2119 errcode = pthread_mutex_unlock(&tog_mutex);
2120 if (errcode)
2121 return got_error_set_errno(errcode,
2122 "pthread_mutex_unlock");
2123 ch = wgetch(view->window);
2124 errcode = pthread_mutex_lock(&tog_mutex);
2125 if (errcode)
2126 return got_error_set_errno(errcode,
2127 "pthread_mutex_lock");
2128 if (ch == KEY_BACKSPACE) {
2129 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2130 return NULL;
2132 if (view->searching == TOG_SEARCH_FORWARD)
2133 entry = TAILQ_NEXT(s->search_entry, entry);
2134 else
2135 entry = TAILQ_PREV(s->search_entry,
2136 commit_queue_head, entry);
2137 } else if (s->matched_entry) {
2138 if (view->searching == TOG_SEARCH_FORWARD)
2139 entry = TAILQ_NEXT(s->matched_entry, entry);
2140 else
2141 entry = TAILQ_PREV(s->matched_entry,
2142 commit_queue_head, entry);
2143 } else {
2144 if (view->searching == TOG_SEARCH_FORWARD)
2145 entry = TAILQ_FIRST(&s->commits.head);
2146 else
2147 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2150 while (1) {
2151 int have_match = 0;
2153 if (entry == NULL) {
2154 if (s->thread_args.log_complete ||
2155 view->searching == TOG_SEARCH_BACKWARD) {
2156 view->search_next_done =
2157 (s->matched_entry == NULL ?
2158 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2159 s->search_entry = NULL;
2160 return NULL;
2163 * Poke the log thread for more commits and return,
2164 * allowing the main loop to make progress. Search
2165 * will resume at s->search_entry once we come back.
2167 s->thread_args.commits_needed++;
2168 return trigger_log_thread(view, 0);
2171 err = match_commit(&have_match, entry->id, entry->commit,
2172 &view->regex);
2173 if (err)
2174 break;
2175 if (have_match) {
2176 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2177 s->matched_entry = entry;
2178 break;
2181 s->search_entry = entry;
2182 if (view->searching == TOG_SEARCH_FORWARD)
2183 entry = TAILQ_NEXT(entry, entry);
2184 else
2185 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2188 if (s->matched_entry) {
2189 int cur = s->selected_entry->idx;
2190 while (cur < s->matched_entry->idx) {
2191 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2192 if (err)
2193 return err;
2194 cur++;
2196 while (cur > s->matched_entry->idx) {
2197 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2198 if (err)
2199 return err;
2200 cur--;
2204 s->search_entry = NULL;
2206 return NULL;
2209 static const struct got_error *
2210 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2211 struct got_repository *repo, const char *head_ref_name,
2212 const char *in_repo_path, int log_branches)
2214 const struct got_error *err = NULL;
2215 struct tog_log_view_state *s = &view->state.log;
2216 struct got_repository *thread_repo = NULL;
2217 struct got_commit_graph *thread_graph = NULL;
2218 int errcode;
2220 SIMPLEQ_INIT(&s->refs);
2222 if (in_repo_path != s->in_repo_path) {
2223 free(s->in_repo_path);
2224 s->in_repo_path = strdup(in_repo_path);
2225 if (s->in_repo_path == NULL)
2226 return got_error_from_errno("strdup");
2229 /* The commit queue only contains commits being displayed. */
2230 TAILQ_INIT(&s->commits.head);
2231 s->commits.ncommits = 0;
2233 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2234 if (err)
2235 goto done;
2237 s->repo = repo;
2238 s->head_ref_name = head_ref_name;
2239 s->start_id = got_object_id_dup(start_id);
2240 if (s->start_id == NULL) {
2241 err = got_error_from_errno("got_object_id_dup");
2242 goto done;
2244 s->log_branches = log_branches;
2246 SIMPLEQ_INIT(&s->colors);
2247 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2248 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2249 get_color_value("TOG_COLOR_COMMIT"));
2250 if (err)
2251 goto done;
2252 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2253 get_color_value("TOG_COLOR_AUTHOR"));
2254 if (err) {
2255 free_colors(&s->colors);
2256 goto done;
2258 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2259 get_color_value("TOG_COLOR_DATE"));
2260 if (err) {
2261 free_colors(&s->colors);
2262 goto done;
2266 view->show = show_log_view;
2267 view->input = input_log_view;
2268 view->close = close_log_view;
2269 view->search_start = search_start_log_view;
2270 view->search_next = search_next_log_view;
2272 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2273 if (err)
2274 goto done;
2275 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2276 !s->log_branches);
2277 if (err)
2278 goto done;
2279 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2280 s->repo, NULL, NULL);
2281 if (err)
2282 goto done;
2284 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2285 if (errcode) {
2286 err = got_error_set_errno(errcode, "pthread_cond_init");
2287 goto done;
2289 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2290 if (errcode) {
2291 err = got_error_set_errno(errcode, "pthread_cond_init");
2292 goto done;
2295 s->thread_args.commits_needed = view->nlines;
2296 s->thread_args.graph = thread_graph;
2297 s->thread_args.commits = &s->commits;
2298 s->thread_args.in_repo_path = s->in_repo_path;
2299 s->thread_args.start_id = s->start_id;
2300 s->thread_args.repo = thread_repo;
2301 s->thread_args.log_complete = 0;
2302 s->thread_args.quit = &s->quit;
2303 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2304 s->thread_args.selected_entry = &s->selected_entry;
2305 s->thread_args.searching = &view->searching;
2306 s->thread_args.search_next_done = &view->search_next_done;
2307 s->thread_args.regex = &view->regex;
2308 done:
2309 if (err)
2310 close_log_view(view);
2311 return err;
2314 static const struct got_error *
2315 show_log_view(struct tog_view *view)
2317 const struct got_error *err;
2318 struct tog_log_view_state *s = &view->state.log;
2320 if (s->thread == NULL) {
2321 int errcode = pthread_create(&s->thread, NULL, log_thread,
2322 &s->thread_args);
2323 if (errcode)
2324 return got_error_set_errno(errcode, "pthread_create");
2325 if (s->thread_args.commits_needed > 0) {
2326 err = trigger_log_thread(view, 1);
2327 if (err)
2328 return err;
2332 return draw_commits(view);
2335 static const struct got_error *
2336 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2337 struct tog_view **focus_view, struct tog_view *view, int ch)
2339 const struct got_error *err = NULL;
2340 struct tog_log_view_state *s = &view->state.log;
2341 char *parent_path, *in_repo_path = NULL;
2342 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2343 struct tog_view *ref_view = NULL;
2344 int begin_x = 0;
2345 struct got_object_id *start_id;
2347 switch (ch) {
2348 case 'q':
2349 s->quit = 1;
2350 break;
2351 case 'k':
2352 case KEY_UP:
2353 case '<':
2354 case ',':
2355 if (s->first_displayed_entry == NULL)
2356 break;
2357 if (s->selected > 0)
2358 s->selected--;
2359 else
2360 log_scroll_up(s, 1);
2361 break;
2362 case KEY_PPAGE:
2363 case CTRL('b'):
2364 if (s->first_displayed_entry == NULL)
2365 break;
2366 if (TAILQ_FIRST(&s->commits.head) ==
2367 s->first_displayed_entry) {
2368 s->selected = 0;
2369 break;
2371 log_scroll_up(s, view->nlines - 1);
2372 break;
2373 case 'j':
2374 case KEY_DOWN:
2375 case '>':
2376 case '.':
2377 if (s->first_displayed_entry == NULL)
2378 break;
2379 if (s->selected < MIN(view->nlines - 2,
2380 s->commits.ncommits - 1)) {
2381 s->selected++;
2382 break;
2384 err = log_scroll_down(view, 1);
2385 break;
2386 case KEY_NPAGE:
2387 case CTRL('f'): {
2388 struct commit_queue_entry *first;
2389 first = s->first_displayed_entry;
2390 if (first == NULL)
2391 break;
2392 err = log_scroll_down(view, view->nlines - 1);
2393 if (err)
2394 break;
2395 if (first == s->first_displayed_entry &&
2396 s->selected < MIN(view->nlines - 2,
2397 s->commits.ncommits - 1)) {
2398 /* can't scroll further down */
2399 s->selected = MIN(view->nlines - 2,
2400 s->commits.ncommits - 1);
2402 err = NULL;
2403 break;
2405 case KEY_RESIZE:
2406 if (s->selected > view->nlines - 2)
2407 s->selected = view->nlines - 2;
2408 if (s->selected > s->commits.ncommits - 1)
2409 s->selected = s->commits.ncommits - 1;
2410 if (s->commits.ncommits < view->nlines - 1 &&
2411 !s->thread_args.log_complete) {
2412 s->thread_args.commits_needed += (view->nlines - 1) -
2413 s->commits.ncommits;
2414 err = trigger_log_thread(view, 1);
2416 break;
2417 case KEY_ENTER:
2418 case ' ':
2419 case '\r':
2420 if (s->selected_entry == NULL)
2421 break;
2422 if (view_is_parent_view(view))
2423 begin_x = view_split_begin_x(view->begin_x);
2424 err = open_diff_view_for_commit(&diff_view, begin_x,
2425 s->selected_entry->commit, s->selected_entry->id,
2426 view, s->repo);
2427 if (err)
2428 break;
2429 if (view_is_parent_view(view)) {
2430 err = view_close_child(view);
2431 if (err)
2432 return err;
2433 err = view_set_child(view, diff_view);
2434 if (err) {
2435 view_close(diff_view);
2436 break;
2438 *focus_view = diff_view;
2439 } else
2440 *new_view = diff_view;
2441 break;
2442 case 't':
2443 if (s->selected_entry == NULL)
2444 break;
2445 if (view_is_parent_view(view))
2446 begin_x = view_split_begin_x(view->begin_x);
2447 err = browse_commit_tree(&tree_view, begin_x,
2448 s->selected_entry, s->in_repo_path, s->repo);
2449 if (err)
2450 break;
2451 if (view_is_parent_view(view)) {
2452 err = view_close_child(view);
2453 if (err)
2454 return err;
2455 err = view_set_child(view, tree_view);
2456 if (err) {
2457 view_close(tree_view);
2458 break;
2460 *focus_view = tree_view;
2461 } else
2462 *new_view = tree_view;
2463 break;
2464 case KEY_BACKSPACE:
2465 if (got_path_cmp(s->in_repo_path, "/",
2466 strlen(s->in_repo_path), 1) == 0)
2467 break;
2468 err = got_path_dirname(&parent_path, s->in_repo_path);
2469 if (err)
2470 return err;
2471 err = stop_log_thread(s);
2472 if (err) {
2473 free(parent_path);
2474 return err;
2476 lv = view_open(view->nlines, view->ncols,
2477 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2478 if (lv == NULL) {
2479 free(parent_path);
2480 return got_error_from_errno("view_open");
2482 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2483 parent_path, s->log_branches);
2484 free(parent_path);
2485 if (err)
2486 return err;;
2487 if (view_is_parent_view(view))
2488 *new_view = lv;
2489 else {
2490 view_set_child(view->parent, lv);
2491 *focus_view = lv;
2493 break;
2494 case CTRL('l'):
2495 err = stop_log_thread(s);
2496 if (err)
2497 return err;
2498 lv = view_open(view->nlines, view->ncols,
2499 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2500 if (lv == NULL)
2501 return got_error_from_errno("view_open");
2502 err = got_repo_match_object_id(&start_id, NULL,
2503 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2504 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2505 if (err) {
2506 view_close(lv);
2507 return err;
2509 in_repo_path = strdup(s->in_repo_path);
2510 if (in_repo_path == NULL) {
2511 free(start_id);
2512 view_close(lv);
2513 return got_error_from_errno("strdup");
2515 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2516 in_repo_path, s->log_branches);
2517 if (err) {
2518 free(start_id);
2519 view_close(lv);
2520 return err;;
2522 *dead_view = view;
2523 *new_view = lv;
2524 break;
2525 case 'B':
2526 s->log_branches = !s->log_branches;
2527 err = stop_log_thread(s);
2528 if (err)
2529 return err;
2530 lv = view_open(view->nlines, view->ncols,
2531 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2532 if (lv == NULL)
2533 return got_error_from_errno("view_open");
2534 err = open_log_view(lv, s->start_id, s->repo,
2535 s->head_ref_name, s->in_repo_path, s->log_branches);
2536 if (err) {
2537 view_close(lv);
2538 return err;;
2540 *dead_view = view;
2541 *new_view = lv;
2542 break;
2543 case 'r':
2544 if (view_is_parent_view(view))
2545 begin_x = view_split_begin_x(view->begin_x);
2546 ref_view = view_open(view->nlines, view->ncols,
2547 view->begin_y, begin_x, TOG_VIEW_REF);
2548 if (ref_view == NULL)
2549 return got_error_from_errno("view_open");
2550 err = open_ref_view(ref_view, s->repo);
2551 if (err) {
2552 view_close(ref_view);
2553 return err;
2555 if (view_is_parent_view(view)) {
2556 err = view_close_child(view);
2557 if (err)
2558 return err;
2559 err = view_set_child(view, ref_view);
2560 if (err) {
2561 view_close(ref_view);
2562 break;
2564 *focus_view = ref_view;
2565 } else
2566 *new_view = ref_view;
2567 break;
2568 default:
2569 break;
2572 return err;
2575 static const struct got_error *
2576 apply_unveil(const char *repo_path, const char *worktree_path)
2578 const struct got_error *error;
2580 #ifdef PROFILE
2581 if (unveil("gmon.out", "rwc") != 0)
2582 return got_error_from_errno2("unveil", "gmon.out");
2583 #endif
2584 if (repo_path && unveil(repo_path, "r") != 0)
2585 return got_error_from_errno2("unveil", repo_path);
2587 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2588 return got_error_from_errno2("unveil", worktree_path);
2590 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2591 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2593 error = got_privsep_unveil_exec_helpers();
2594 if (error != NULL)
2595 return error;
2597 if (unveil(NULL, NULL) != 0)
2598 return got_error_from_errno("unveil");
2600 return NULL;
2603 static void
2604 init_curses(void)
2606 initscr();
2607 cbreak();
2608 halfdelay(1); /* Do fast refresh while initial view is loading. */
2609 noecho();
2610 nonl();
2611 intrflush(stdscr, FALSE);
2612 keypad(stdscr, TRUE);
2613 curs_set(0);
2614 if (getenv("TOG_COLORS") != NULL) {
2615 start_color();
2616 use_default_colors();
2618 signal(SIGWINCH, tog_sigwinch);
2619 signal(SIGPIPE, tog_sigpipe);
2620 signal(SIGCONT, tog_sigcont);
2623 static const struct got_error *
2624 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2625 struct got_repository *repo, struct got_worktree *worktree)
2627 const struct got_error *err = NULL;
2629 if (argc == 0) {
2630 *in_repo_path = strdup("/");
2631 if (*in_repo_path == NULL)
2632 return got_error_from_errno("strdup");
2633 return NULL;
2636 if (worktree) {
2637 const char *prefix = got_worktree_get_path_prefix(worktree);
2638 char *p;
2640 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2641 if (err)
2642 return err;
2643 if (asprintf(in_repo_path, "%s%s%s", prefix,
2644 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2645 p) == -1) {
2646 err = got_error_from_errno("asprintf");
2647 *in_repo_path = NULL;
2649 free(p);
2650 } else
2651 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2653 return err;
2656 static const struct got_error *
2657 cmd_log(int argc, char *argv[])
2659 const struct got_error *error;
2660 struct got_repository *repo = NULL;
2661 struct got_worktree *worktree = NULL;
2662 struct got_object_id *start_id = NULL;
2663 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2664 char *start_commit = NULL, *head_ref_name = NULL;
2665 int ch, log_branches = 0;
2666 struct tog_view *view;
2668 #ifndef PROFILE
2669 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2670 NULL) == -1)
2671 err(1, "pledge");
2672 #endif
2674 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2675 switch (ch) {
2676 case 'b':
2677 log_branches = 1;
2678 break;
2679 case 'c':
2680 start_commit = optarg;
2681 break;
2682 case 'r':
2683 repo_path = realpath(optarg, NULL);
2684 if (repo_path == NULL)
2685 return got_error_from_errno2("realpath",
2686 optarg);
2687 break;
2688 default:
2689 usage_log();
2690 /* NOTREACHED */
2694 argc -= optind;
2695 argv += optind;
2697 if (argc > 1)
2698 usage_log();
2700 cwd = getcwd(NULL, 0);
2701 if (cwd == NULL)
2702 return got_error_from_errno("getcwd");
2704 error = got_worktree_open(&worktree, cwd);
2705 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2706 goto done;
2708 if (repo_path == NULL) {
2709 if (worktree)
2710 repo_path =
2711 strdup(got_worktree_get_repo_path(worktree));
2712 else
2713 repo_path = strdup(cwd);
2715 if (repo_path == NULL) {
2716 error = got_error_from_errno("strdup");
2717 goto done;
2720 error = got_repo_open(&repo, repo_path, NULL);
2721 if (error != NULL)
2722 goto done;
2724 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2725 repo, worktree);
2726 if (error)
2727 goto done;
2729 init_curses();
2731 error = apply_unveil(got_repo_get_path(repo),
2732 worktree ? got_worktree_get_root_path(worktree) : NULL);
2733 if (error)
2734 goto done;
2736 if (start_commit == NULL)
2737 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2738 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2739 GOT_OBJ_TYPE_COMMIT, 1, repo);
2740 else
2741 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2742 GOT_OBJ_TYPE_COMMIT, 1, repo);
2743 if (error != NULL)
2744 goto done;
2746 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2747 if (view == NULL) {
2748 error = got_error_from_errno("view_open");
2749 goto done;
2751 if (worktree) {
2752 head_ref_name = strdup(
2753 got_worktree_get_head_ref_name(worktree));
2754 if (head_ref_name == NULL) {
2755 error = got_error_from_errno("strdup");
2756 goto done;
2759 error = open_log_view(view, start_id, repo, head_ref_name,
2760 in_repo_path, log_branches);
2761 if (error)
2762 goto done;
2763 if (worktree) {
2764 /* Release work tree lock. */
2765 got_worktree_close(worktree);
2766 worktree = NULL;
2768 error = view_loop(view);
2769 done:
2770 free(in_repo_path);
2771 free(repo_path);
2772 free(cwd);
2773 free(start_id);
2774 free(head_ref_name);
2775 if (repo)
2776 got_repo_close(repo);
2777 if (worktree)
2778 got_worktree_close(worktree);
2779 return error;
2782 __dead static void
2783 usage_diff(void)
2785 endwin();
2786 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2787 "[-w] object1 object2\n", getprogname());
2788 exit(1);
2791 static char *
2792 parse_next_line(FILE *f, size_t *len)
2794 char *line;
2795 size_t linelen;
2796 size_t lineno;
2797 const char delim[3] = { '\0', '\0', '\0'};
2799 line = fparseln(f, &linelen, &lineno, delim, 0);
2800 if (len)
2801 *len = linelen;
2802 return line;
2805 static int
2806 match_line(const char *line, regex_t *regex, size_t nmatch,
2807 regmatch_t *regmatch)
2809 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2812 struct tog_color *
2813 match_color(struct tog_colors *colors, const char *line)
2815 struct tog_color *tc = NULL;
2817 SIMPLEQ_FOREACH(tc, colors, entry) {
2818 if (match_line(line, &tc->regex, 0, NULL))
2819 return tc;
2822 return NULL;
2825 static const struct got_error *
2826 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2827 WINDOW *window, regmatch_t *regmatch)
2829 const struct got_error *err = NULL;
2830 wchar_t *wline;
2831 int width;
2832 char *s;
2834 *wtotal = 0;
2836 s = strndup(line, regmatch->rm_so);
2837 if (s == NULL)
2838 return got_error_from_errno("strndup");
2840 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2841 if (err) {
2842 free(s);
2843 return err;
2845 waddwstr(window, wline);
2846 free(wline);
2847 free(s);
2848 wlimit -= width;
2849 *wtotal += width;
2851 if (wlimit > 0) {
2852 s = strndup(line + regmatch->rm_so,
2853 regmatch->rm_eo - regmatch->rm_so);
2854 if (s == NULL) {
2855 err = got_error_from_errno("strndup");
2856 free(s);
2857 return err;
2859 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2860 if (err) {
2861 free(s);
2862 return err;
2864 wattr_on(window, A_STANDOUT, NULL);
2865 waddwstr(window, wline);
2866 wattr_off(window, A_STANDOUT, NULL);
2867 free(wline);
2868 free(s);
2869 wlimit -= width;
2870 *wtotal += width;
2873 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2874 err = format_line(&wline, &width,
2875 line + regmatch->rm_eo, wlimit, col_tab_align);
2876 if (err)
2877 return err;
2878 waddwstr(window, wline);
2879 free(wline);
2880 *wtotal += width;
2883 return NULL;
2886 static const struct got_error *
2887 draw_file(struct tog_view *view, const char *header)
2889 struct tog_diff_view_state *s = &view->state.diff;
2890 regmatch_t *regmatch = &view->regmatch;
2891 const struct got_error *err;
2892 int nprinted = 0;
2893 char *line;
2894 struct tog_color *tc;
2895 size_t len;
2896 wchar_t *wline;
2897 int width;
2898 int max_lines = view->nlines;
2899 int nlines = s->nlines;
2900 off_t line_offset;
2902 line_offset = s->line_offsets[s->first_displayed_line - 1];
2903 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2904 return got_error_from_errno("fseek");
2906 werase(view->window);
2908 if (header) {
2909 if (asprintf(&line, "[%d/%d] %s",
2910 s->first_displayed_line - 1 + s->selected_line, nlines,
2911 header) == -1)
2912 return got_error_from_errno("asprintf");
2913 err = format_line(&wline, &width, line, view->ncols, 0);
2914 free(line);
2915 if (err)
2916 return err;
2918 if (view_needs_focus_indication(view))
2919 wstandout(view->window);
2920 waddwstr(view->window, wline);
2921 free(wline);
2922 wline = NULL;
2923 if (view_needs_focus_indication(view))
2924 wstandend(view->window);
2925 if (width <= view->ncols - 1)
2926 waddch(view->window, '\n');
2928 if (max_lines <= 1)
2929 return NULL;
2930 max_lines--;
2933 s->eof = 0;
2934 while (max_lines > 0 && nprinted < max_lines) {
2935 line = parse_next_line(s->f, &len);
2936 if (line == NULL) {
2937 s->eof = 1;
2938 break;
2941 tc = match_color(&s->colors, line);
2942 if (tc)
2943 wattr_on(view->window,
2944 COLOR_PAIR(tc->colorpair), NULL);
2945 if (s->first_displayed_line + nprinted == s->matched_line &&
2946 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2947 err = add_matched_line(&width, line, view->ncols, 0,
2948 view->window, regmatch);
2949 if (err) {
2950 free(line);
2951 return err;
2953 } else {
2954 err = format_line(&wline, &width, line, view->ncols, 0);
2955 if (err) {
2956 free(line);
2957 return err;
2959 waddwstr(view->window, wline);
2960 free(wline);
2961 wline = NULL;
2963 if (tc)
2964 wattr_off(view->window,
2965 COLOR_PAIR(tc->colorpair), NULL);
2966 if (width <= view->ncols - 1)
2967 waddch(view->window, '\n');
2968 nprinted++;
2969 free(line);
2971 if (nprinted >= 1)
2972 s->last_displayed_line = s->first_displayed_line +
2973 (nprinted - 1);
2974 else
2975 s->last_displayed_line = s->first_displayed_line;
2977 view_vborder(view);
2979 if (s->eof) {
2980 while (nprinted < view->nlines) {
2981 waddch(view->window, '\n');
2982 nprinted++;
2985 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2986 if (err) {
2987 return err;
2990 wstandout(view->window);
2991 waddwstr(view->window, wline);
2992 free(wline);
2993 wline = NULL;
2994 wstandend(view->window);
2997 return NULL;
3000 static char *
3001 get_datestr(time_t *time, char *datebuf)
3003 struct tm mytm, *tm;
3004 char *p, *s;
3006 tm = gmtime_r(time, &mytm);
3007 if (tm == NULL)
3008 return NULL;
3009 s = asctime_r(tm, datebuf);
3010 if (s == NULL)
3011 return NULL;
3012 p = strchr(s, '\n');
3013 if (p)
3014 *p = '\0';
3015 return s;
3018 static const struct got_error *
3019 get_changed_paths(struct got_pathlist_head *paths,
3020 struct got_commit_object *commit, struct got_repository *repo)
3022 const struct got_error *err = NULL;
3023 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3024 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3025 struct got_object_qid *qid;
3027 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3028 if (qid != NULL) {
3029 struct got_commit_object *pcommit;
3030 err = got_object_open_as_commit(&pcommit, repo,
3031 qid->id);
3032 if (err)
3033 return err;
3035 tree_id1 = got_object_commit_get_tree_id(pcommit);
3036 got_object_commit_close(pcommit);
3040 if (tree_id1) {
3041 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3042 if (err)
3043 goto done;
3046 tree_id2 = got_object_commit_get_tree_id(commit);
3047 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3048 if (err)
3049 goto done;
3051 err = got_diff_tree(tree1, tree2, "", "", repo,
3052 got_diff_tree_collect_changed_paths, paths, 0);
3053 done:
3054 if (tree1)
3055 got_object_tree_close(tree1);
3056 if (tree2)
3057 got_object_tree_close(tree2);
3058 return err;
3061 static const struct got_error *
3062 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3064 off_t *p;
3066 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3067 if (p == NULL)
3068 return got_error_from_errno("reallocarray");
3069 *line_offsets = p;
3070 (*line_offsets)[*nlines] = off;
3071 (*nlines)++;
3072 return NULL;
3075 static const struct got_error *
3076 write_commit_info(off_t **line_offsets, size_t *nlines,
3077 struct got_object_id *commit_id, struct got_reflist_head *refs,
3078 struct got_repository *repo, FILE *outfile)
3080 const struct got_error *err = NULL;
3081 char datebuf[26], *datestr;
3082 struct got_commit_object *commit;
3083 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3084 time_t committer_time;
3085 const char *author, *committer;
3086 char *refs_str = NULL;
3087 struct got_pathlist_head changed_paths;
3088 struct got_pathlist_entry *pe;
3089 off_t outoff = 0;
3090 int n;
3092 TAILQ_INIT(&changed_paths);
3094 if (refs) {
3095 err = build_refs_str(&refs_str, refs, commit_id, repo);
3096 if (err)
3097 return err;
3100 err = got_object_open_as_commit(&commit, repo, commit_id);
3101 if (err)
3102 return err;
3104 err = got_object_id_str(&id_str, commit_id);
3105 if (err) {
3106 err = got_error_from_errno("got_object_id_str");
3107 goto done;
3110 err = add_line_offset(line_offsets, nlines, 0);
3111 if (err)
3112 goto done;
3114 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3115 refs_str ? refs_str : "", refs_str ? ")" : "");
3116 if (n < 0) {
3117 err = got_error_from_errno("fprintf");
3118 goto done;
3120 outoff += n;
3121 err = add_line_offset(line_offsets, nlines, outoff);
3122 if (err)
3123 goto done;
3125 n = fprintf(outfile, "from: %s\n",
3126 got_object_commit_get_author(commit));
3127 if (n < 0) {
3128 err = got_error_from_errno("fprintf");
3129 goto done;
3131 outoff += n;
3132 err = add_line_offset(line_offsets, nlines, outoff);
3133 if (err)
3134 goto done;
3136 committer_time = got_object_commit_get_committer_time(commit);
3137 datestr = get_datestr(&committer_time, datebuf);
3138 if (datestr) {
3139 n = fprintf(outfile, "date: %s UTC\n", datestr);
3140 if (n < 0) {
3141 err = got_error_from_errno("fprintf");
3142 goto done;
3144 outoff += n;
3145 err = add_line_offset(line_offsets, nlines, outoff);
3146 if (err)
3147 goto done;
3149 author = got_object_commit_get_author(commit);
3150 committer = got_object_commit_get_committer(commit);
3151 if (strcmp(author, committer) != 0) {
3152 n = fprintf(outfile, "via: %s\n", committer);
3153 if (n < 0) {
3154 err = got_error_from_errno("fprintf");
3155 goto done;
3157 outoff += n;
3158 err = add_line_offset(line_offsets, nlines, outoff);
3159 if (err)
3160 goto done;
3162 err = got_object_commit_get_logmsg(&logmsg, commit);
3163 if (err)
3164 goto done;
3165 s = logmsg;
3166 while ((line = strsep(&s, "\n")) != NULL) {
3167 n = fprintf(outfile, "%s\n", line);
3168 if (n < 0) {
3169 err = got_error_from_errno("fprintf");
3170 goto done;
3172 outoff += n;
3173 err = add_line_offset(line_offsets, nlines, outoff);
3174 if (err)
3175 goto done;
3178 err = get_changed_paths(&changed_paths, commit, repo);
3179 if (err)
3180 goto done;
3181 TAILQ_FOREACH(pe, &changed_paths, entry) {
3182 struct got_diff_changed_path *cp = pe->data;
3183 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3184 if (n < 0) {
3185 err = got_error_from_errno("fprintf");
3186 goto done;
3188 outoff += n;
3189 err = add_line_offset(line_offsets, nlines, outoff);
3190 if (err)
3191 goto done;
3192 free((char *)pe->path);
3193 free(pe->data);
3196 fputc('\n', outfile);
3197 outoff++;
3198 err = add_line_offset(line_offsets, nlines, outoff);
3199 done:
3200 got_pathlist_free(&changed_paths);
3201 free(id_str);
3202 free(logmsg);
3203 free(refs_str);
3204 got_object_commit_close(commit);
3205 if (err) {
3206 free(*line_offsets);
3207 *line_offsets = NULL;
3208 *nlines = 0;
3210 return err;
3213 static const struct got_error *
3214 create_diff(struct tog_diff_view_state *s)
3216 const struct got_error *err = NULL;
3217 FILE *f = NULL;
3218 int obj_type;
3220 free(s->line_offsets);
3221 s->line_offsets = malloc(sizeof(off_t));
3222 if (s->line_offsets == NULL)
3223 return got_error_from_errno("malloc");
3224 s->nlines = 0;
3226 f = got_opentemp();
3227 if (f == NULL) {
3228 err = got_error_from_errno("got_opentemp");
3229 goto done;
3231 if (s->f && fclose(s->f) != 0) {
3232 err = got_error_from_errno("fclose");
3233 goto done;
3235 s->f = f;
3237 if (s->id1)
3238 err = got_object_get_type(&obj_type, s->repo, s->id1);
3239 else
3240 err = got_object_get_type(&obj_type, s->repo, s->id2);
3241 if (err)
3242 goto done;
3244 switch (obj_type) {
3245 case GOT_OBJ_TYPE_BLOB:
3246 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3247 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3248 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3249 break;
3250 case GOT_OBJ_TYPE_TREE:
3251 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3252 s->id1, s->id2, "", "", s->diff_context,
3253 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3254 break;
3255 case GOT_OBJ_TYPE_COMMIT: {
3256 const struct got_object_id_queue *parent_ids;
3257 struct got_object_qid *pid;
3258 struct got_commit_object *commit2;
3260 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3261 if (err)
3262 goto done;
3263 /* Show commit info if we're diffing to a parent/root commit. */
3264 if (s->id1 == NULL) {
3265 err = write_commit_info(&s->line_offsets, &s->nlines,
3266 s->id2, &s->refs, s->repo, s->f);
3267 if (err)
3268 goto done;
3269 } else {
3270 parent_ids = got_object_commit_get_parent_ids(commit2);
3271 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3272 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3273 err = write_commit_info(
3274 &s->line_offsets, &s->nlines,
3275 s->id2, &s->refs, s->repo, s->f);
3276 if (err)
3277 goto done;
3278 break;
3282 got_object_commit_close(commit2);
3284 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3285 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3286 s->force_text_diff, s->repo, s->f);
3287 break;
3289 default:
3290 err = got_error(GOT_ERR_OBJ_TYPE);
3291 break;
3293 if (err)
3294 goto done;
3295 done:
3296 if (s->f && fflush(s->f) != 0 && err == NULL)
3297 err = got_error_from_errno("fflush");
3298 return err;
3301 static void
3302 diff_view_indicate_progress(struct tog_view *view)
3304 mvwaddstr(view->window, 0, 0, "diffing...");
3305 update_panels();
3306 doupdate();
3309 static const struct got_error *
3310 search_start_diff_view(struct tog_view *view)
3312 struct tog_diff_view_state *s = &view->state.diff;
3314 s->matched_line = 0;
3315 return NULL;
3318 static const struct got_error *
3319 search_next_diff_view(struct tog_view *view)
3321 struct tog_diff_view_state *s = &view->state.diff;
3322 int lineno;
3324 if (!view->searching) {
3325 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3326 return NULL;
3329 if (s->matched_line) {
3330 if (view->searching == TOG_SEARCH_FORWARD)
3331 lineno = s->matched_line + 1;
3332 else
3333 lineno = s->matched_line - 1;
3334 } else {
3335 if (view->searching == TOG_SEARCH_FORWARD)
3336 lineno = 1;
3337 else
3338 lineno = s->nlines;
3341 while (1) {
3342 char *line = NULL;
3343 off_t offset;
3344 size_t len;
3346 if (lineno <= 0 || lineno > s->nlines) {
3347 if (s->matched_line == 0) {
3348 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3349 free(line);
3350 break;
3353 if (view->searching == TOG_SEARCH_FORWARD)
3354 lineno = 1;
3355 else
3356 lineno = s->nlines;
3359 offset = s->line_offsets[lineno - 1];
3360 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3361 free(line);
3362 return got_error_from_errno("fseeko");
3364 free(line);
3365 line = parse_next_line(s->f, &len);
3366 if (line &&
3367 match_line(line, &view->regex, 1, &view->regmatch)) {
3368 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3369 s->matched_line = lineno;
3370 free(line);
3371 break;
3373 free(line);
3374 if (view->searching == TOG_SEARCH_FORWARD)
3375 lineno++;
3376 else
3377 lineno--;
3380 if (s->matched_line) {
3381 s->first_displayed_line = s->matched_line;
3382 s->selected_line = 1;
3385 return NULL;
3388 static const struct got_error *
3389 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3390 struct got_object_id *id2, const char *label1, const char *label2,
3391 int diff_context, int ignore_whitespace, int force_text_diff,
3392 struct tog_view *log_view, struct got_repository *repo)
3394 const struct got_error *err;
3395 struct tog_diff_view_state *s = &view->state.diff;
3397 SIMPLEQ_INIT(&s->refs);
3399 if (id1 != NULL && id2 != NULL) {
3400 int type1, type2;
3401 err = got_object_get_type(&type1, repo, id1);
3402 if (err)
3403 return err;
3404 err = got_object_get_type(&type2, repo, id2);
3405 if (err)
3406 return err;
3408 if (type1 != type2)
3409 return got_error(GOT_ERR_OBJ_TYPE);
3411 s->first_displayed_line = 1;
3412 s->last_displayed_line = view->nlines;
3413 s->selected_line = 1;
3414 s->repo = repo;
3415 s->id1 = id1;
3416 s->id2 = id2;
3417 s->label1 = label1;
3418 s->label2 = label2;
3420 if (id1) {
3421 s->id1 = got_object_id_dup(id1);
3422 if (s->id1 == NULL)
3423 return got_error_from_errno("got_object_id_dup");
3424 } else
3425 s->id1 = NULL;
3427 s->id2 = got_object_id_dup(id2);
3428 if (s->id2 == NULL) {
3429 free(s->id1);
3430 s->id1 = NULL;
3431 return got_error_from_errno("got_object_id_dup");
3433 s->f = NULL;
3434 s->first_displayed_line = 1;
3435 s->last_displayed_line = view->nlines;
3436 s->diff_context = diff_context;
3437 s->ignore_whitespace = ignore_whitespace;
3438 s->force_text_diff = force_text_diff;
3439 s->log_view = log_view;
3440 s->repo = repo;
3442 SIMPLEQ_INIT(&s->colors);
3443 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3444 err = add_color(&s->colors,
3445 "^-", TOG_COLOR_DIFF_MINUS,
3446 get_color_value("TOG_COLOR_DIFF_MINUS"));
3447 if (err)
3448 return err;
3449 err = add_color(&s->colors, "^\\+",
3450 TOG_COLOR_DIFF_PLUS,
3451 get_color_value("TOG_COLOR_DIFF_PLUS"));
3452 if (err) {
3453 free_colors(&s->colors);
3454 return err;
3456 err = add_color(&s->colors,
3457 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3458 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3459 if (err) {
3460 free_colors(&s->colors);
3461 return err;
3464 err = add_color(&s->colors,
3465 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3466 TOG_COLOR_DIFF_META,
3467 get_color_value("TOG_COLOR_DIFF_META"));
3468 if (err) {
3469 free_colors(&s->colors);
3470 return err;
3473 err = add_color(&s->colors,
3474 "^(from|via): ", TOG_COLOR_AUTHOR,
3475 get_color_value("TOG_COLOR_AUTHOR"));
3476 if (err) {
3477 free_colors(&s->colors);
3478 return err;
3481 err = add_color(&s->colors,
3482 "^date: ", TOG_COLOR_DATE,
3483 get_color_value("TOG_COLOR_DATE"));
3484 if (err) {
3485 free_colors(&s->colors);
3486 return err;
3490 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3491 if (err) {
3492 free(s->id1);
3493 s->id1 = NULL;
3494 free(s->id2);
3495 s->id2 = NULL;
3496 free_colors(&s->colors);
3497 return err;
3500 if (log_view && view_is_splitscreen(view))
3501 show_log_view(log_view); /* draw vborder */
3502 diff_view_indicate_progress(view);
3504 s->line_offsets = NULL;
3505 s->nlines = 0;
3506 err = create_diff(s);
3507 if (err) {
3508 free(s->id1);
3509 s->id1 = NULL;
3510 free(s->id2);
3511 s->id2 = NULL;
3512 free_colors(&s->colors);
3513 got_ref_list_free(&s->refs);
3514 return err;
3517 view->show = show_diff_view;
3518 view->input = input_diff_view;
3519 view->close = close_diff_view;
3520 view->search_start = search_start_diff_view;
3521 view->search_next = search_next_diff_view;
3523 return NULL;
3526 static const struct got_error *
3527 close_diff_view(struct tog_view *view)
3529 const struct got_error *err = NULL;
3530 struct tog_diff_view_state *s = &view->state.diff;
3532 free(s->id1);
3533 s->id1 = NULL;
3534 free(s->id2);
3535 s->id2 = NULL;
3536 if (s->f && fclose(s->f) == EOF)
3537 err = got_error_from_errno("fclose");
3538 free_colors(&s->colors);
3539 free(s->line_offsets);
3540 s->line_offsets = NULL;
3541 s->nlines = 0;
3542 got_ref_list_free(&s->refs);
3543 return err;
3546 static const struct got_error *
3547 show_diff_view(struct tog_view *view)
3549 const struct got_error *err;
3550 struct tog_diff_view_state *s = &view->state.diff;
3551 char *id_str1 = NULL, *id_str2, *header;
3552 const char *label1, *label2;
3554 if (s->id1) {
3555 err = got_object_id_str(&id_str1, s->id1);
3556 if (err)
3557 return err;
3558 label1 = s->label1 ? : id_str1;
3559 } else
3560 label1 = "/dev/null";
3562 err = got_object_id_str(&id_str2, s->id2);
3563 if (err)
3564 return err;
3565 label2 = s->label2 ? : id_str2;
3567 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3568 err = got_error_from_errno("asprintf");
3569 free(id_str1);
3570 free(id_str2);
3571 return err;
3573 free(id_str1);
3574 free(id_str2);
3576 return draw_file(view, header);
3579 static const struct got_error *
3580 set_selected_commit(struct tog_diff_view_state *s,
3581 struct commit_queue_entry *entry)
3583 const struct got_error *err;
3584 const struct got_object_id_queue *parent_ids;
3585 struct got_commit_object *selected_commit;
3586 struct got_object_qid *pid;
3588 free(s->id2);
3589 s->id2 = got_object_id_dup(entry->id);
3590 if (s->id2 == NULL)
3591 return got_error_from_errno("got_object_id_dup");
3593 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3594 if (err)
3595 return err;
3596 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3597 free(s->id1);
3598 pid = SIMPLEQ_FIRST(parent_ids);
3599 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3600 got_object_commit_close(selected_commit);
3601 return NULL;
3604 static const struct got_error *
3605 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3606 struct tog_view **focus_view, struct tog_view *view, int ch)
3608 const struct got_error *err = NULL;
3609 struct tog_diff_view_state *s = &view->state.diff;
3610 struct tog_log_view_state *ls;
3611 struct commit_queue_entry *entry;
3612 int i;
3614 switch (ch) {
3615 case 'a':
3616 case 'w':
3617 if (ch == 'a')
3618 s->force_text_diff = !s->force_text_diff;
3619 if (ch == 'w')
3620 s->ignore_whitespace = !s->ignore_whitespace;
3621 wclear(view->window);
3622 s->first_displayed_line = 1;
3623 s->last_displayed_line = view->nlines;
3624 diff_view_indicate_progress(view);
3625 err = create_diff(s);
3626 break;
3627 case 'k':
3628 case KEY_UP:
3629 if (s->first_displayed_line > 1)
3630 s->first_displayed_line--;
3631 break;
3632 case KEY_PPAGE:
3633 case CTRL('b'):
3634 if (s->first_displayed_line == 1)
3635 break;
3636 i = 0;
3637 while (i++ < view->nlines - 1 &&
3638 s->first_displayed_line > 1)
3639 s->first_displayed_line--;
3640 break;
3641 case 'j':
3642 case KEY_DOWN:
3643 if (!s->eof)
3644 s->first_displayed_line++;
3645 break;
3646 case KEY_NPAGE:
3647 case CTRL('f'):
3648 case ' ':
3649 if (s->eof)
3650 break;
3651 i = 0;
3652 while (!s->eof && i++ < view->nlines - 1) {
3653 char *line;
3654 line = parse_next_line(s->f, NULL);
3655 s->first_displayed_line++;
3656 if (line == NULL)
3657 break;
3659 break;
3660 case '[':
3661 if (s->diff_context > 0) {
3662 s->diff_context--;
3663 diff_view_indicate_progress(view);
3664 err = create_diff(s);
3665 if (s->first_displayed_line + view->nlines - 1 >
3666 s->nlines) {
3667 s->first_displayed_line = 1;
3668 s->last_displayed_line = view->nlines;
3671 break;
3672 case ']':
3673 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3674 s->diff_context++;
3675 diff_view_indicate_progress(view);
3676 err = create_diff(s);
3678 break;
3679 case '<':
3680 case ',':
3681 if (s->log_view == NULL)
3682 break;
3683 ls = &s->log_view->state.log;
3684 entry = TAILQ_PREV(ls->selected_entry,
3685 commit_queue_head, entry);
3686 if (entry == NULL)
3687 break;
3689 err = input_log_view(NULL, NULL, NULL, s->log_view,
3690 KEY_UP);
3691 if (err)
3692 break;
3694 err = set_selected_commit(s, entry);
3695 if (err)
3696 break;
3698 s->first_displayed_line = 1;
3699 s->last_displayed_line = view->nlines;
3701 diff_view_indicate_progress(view);
3702 err = create_diff(s);
3703 break;
3704 case '>':
3705 case '.':
3706 if (s->log_view == NULL)
3707 break;
3708 ls = &s->log_view->state.log;
3710 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3711 ls->thread_args.commits_needed++;
3712 err = trigger_log_thread(s->log_view, 1);
3713 if (err)
3714 break;
3716 err = input_log_view(NULL, NULL, NULL, s->log_view,
3717 KEY_DOWN);
3718 if (err)
3719 break;
3721 entry = TAILQ_NEXT(ls->selected_entry, entry);
3722 if (entry == NULL)
3723 break;
3725 err = set_selected_commit(s, entry);
3726 if (err)
3727 break;
3729 s->first_displayed_line = 1;
3730 s->last_displayed_line = view->nlines;
3732 diff_view_indicate_progress(view);
3733 err = create_diff(s);
3734 break;
3735 default:
3736 break;
3739 return err;
3742 static const struct got_error *
3743 cmd_diff(int argc, char *argv[])
3745 const struct got_error *error = NULL;
3746 struct got_repository *repo = NULL;
3747 struct got_worktree *worktree = NULL;
3748 struct got_object_id *id1 = NULL, *id2 = NULL;
3749 char *repo_path = NULL, *cwd = NULL;
3750 char *id_str1 = NULL, *id_str2 = NULL;
3751 char *label1 = NULL, *label2 = NULL;
3752 int diff_context = 3, ignore_whitespace = 0;
3753 int ch, force_text_diff = 0;
3754 const char *errstr;
3755 struct tog_view *view;
3757 #ifndef PROFILE
3758 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3759 NULL) == -1)
3760 err(1, "pledge");
3761 #endif
3762 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3763 switch (ch) {
3764 case 'a':
3765 force_text_diff = 1;
3766 break;
3767 case 'C':
3768 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3769 &errstr);
3770 if (errstr != NULL)
3771 err(1, "-C option %s", errstr);
3772 break;
3773 case 'r':
3774 repo_path = realpath(optarg, NULL);
3775 if (repo_path == NULL)
3776 return got_error_from_errno2("realpath",
3777 optarg);
3778 got_path_strip_trailing_slashes(repo_path);
3779 break;
3780 case 'w':
3781 ignore_whitespace = 1;
3782 break;
3783 default:
3784 usage_diff();
3785 /* NOTREACHED */
3789 argc -= optind;
3790 argv += optind;
3792 if (argc == 0) {
3793 usage_diff(); /* TODO show local worktree changes */
3794 } else if (argc == 2) {
3795 id_str1 = argv[0];
3796 id_str2 = argv[1];
3797 } else
3798 usage_diff();
3800 cwd = getcwd(NULL, 0);
3801 if (cwd == NULL)
3802 return got_error_from_errno("getcwd");
3804 error = got_worktree_open(&worktree, cwd);
3805 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3806 goto done;
3808 if (repo_path == NULL) {
3809 if (worktree)
3810 repo_path =
3811 strdup(got_worktree_get_repo_path(worktree));
3812 else
3813 repo_path = strdup(cwd);
3815 if (repo_path == NULL) {
3816 error = got_error_from_errno("strdup");
3817 goto done;
3820 error = got_repo_open(&repo, repo_path, NULL);
3821 if (error)
3822 goto done;
3824 init_curses();
3826 error = apply_unveil(got_repo_get_path(repo), NULL);
3827 if (error)
3828 goto done;
3830 error = got_repo_match_object_id(&id1, &label1, id_str1,
3831 GOT_OBJ_TYPE_ANY, 1, repo);
3832 if (error)
3833 goto done;
3835 error = got_repo_match_object_id(&id2, &label2, id_str2,
3836 GOT_OBJ_TYPE_ANY, 1, repo);
3837 if (error)
3838 goto done;
3840 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3841 if (view == NULL) {
3842 error = got_error_from_errno("view_open");
3843 goto done;
3845 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3846 ignore_whitespace, force_text_diff, NULL, repo);
3847 if (error)
3848 goto done;
3849 error = view_loop(view);
3850 done:
3851 free(label1);
3852 free(label2);
3853 free(repo_path);
3854 free(cwd);
3855 if (repo)
3856 got_repo_close(repo);
3857 if (worktree)
3858 got_worktree_close(worktree);
3859 return error;
3862 __dead static void
3863 usage_blame(void)
3865 endwin();
3866 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3867 getprogname());
3868 exit(1);
3871 struct tog_blame_line {
3872 int annotated;
3873 struct got_object_id *id;
3876 static const struct got_error *
3877 draw_blame(struct tog_view *view)
3879 struct tog_blame_view_state *s = &view->state.blame;
3880 struct tog_blame *blame = &s->blame;
3881 regmatch_t *regmatch = &view->regmatch;
3882 const struct got_error *err;
3883 int lineno = 0, nprinted = 0;
3884 char *line;
3885 size_t len;
3886 wchar_t *wline;
3887 int width;
3888 struct tog_blame_line *blame_line;
3889 struct got_object_id *prev_id = NULL;
3890 char *id_str;
3891 struct tog_color *tc;
3893 err = got_object_id_str(&id_str, s->blamed_commit->id);
3894 if (err)
3895 return err;
3897 rewind(blame->f);
3898 werase(view->window);
3900 if (asprintf(&line, "commit %s", id_str) == -1) {
3901 err = got_error_from_errno("asprintf");
3902 free(id_str);
3903 return err;
3906 err = format_line(&wline, &width, line, view->ncols, 0);
3907 free(line);
3908 line = NULL;
3909 if (err)
3910 return err;
3911 if (view_needs_focus_indication(view))
3912 wstandout(view->window);
3913 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3914 if (tc)
3915 wattr_on(view->window,
3916 COLOR_PAIR(tc->colorpair), NULL);
3917 waddwstr(view->window, wline);
3918 if (tc)
3919 wattr_off(view->window,
3920 COLOR_PAIR(tc->colorpair), NULL);
3921 if (view_needs_focus_indication(view))
3922 wstandend(view->window);
3923 free(wline);
3924 wline = NULL;
3925 if (width < view->ncols - 1)
3926 waddch(view->window, '\n');
3928 if (asprintf(&line, "[%d/%d] %s%s",
3929 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3930 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3931 free(id_str);
3932 return got_error_from_errno("asprintf");
3934 free(id_str);
3935 err = format_line(&wline, &width, line, view->ncols, 0);
3936 free(line);
3937 line = NULL;
3938 if (err)
3939 return err;
3940 waddwstr(view->window, wline);
3941 free(wline);
3942 wline = NULL;
3943 if (width < view->ncols - 1)
3944 waddch(view->window, '\n');
3946 s->eof = 0;
3947 while (nprinted < view->nlines - 2) {
3948 line = parse_next_line(blame->f, &len);
3949 if (line == NULL) {
3950 s->eof = 1;
3951 break;
3953 if (++lineno < s->first_displayed_line) {
3954 free(line);
3955 continue;
3958 if (view->focussed && nprinted == s->selected_line - 1)
3959 wstandout(view->window);
3961 if (blame->nlines > 0) {
3962 blame_line = &blame->lines[lineno - 1];
3963 if (blame_line->annotated && prev_id &&
3964 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3965 !(view->focussed &&
3966 nprinted == s->selected_line - 1)) {
3967 waddstr(view->window, " ");
3968 } else if (blame_line->annotated) {
3969 char *id_str;
3970 err = got_object_id_str(&id_str, blame_line->id);
3971 if (err) {
3972 free(line);
3973 return err;
3975 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3976 if (tc)
3977 wattr_on(view->window,
3978 COLOR_PAIR(tc->colorpair), NULL);
3979 wprintw(view->window, "%.8s", id_str);
3980 if (tc)
3981 wattr_off(view->window,
3982 COLOR_PAIR(tc->colorpair), NULL);
3983 free(id_str);
3984 prev_id = blame_line->id;
3985 } else {
3986 waddstr(view->window, "........");
3987 prev_id = NULL;
3989 } else {
3990 waddstr(view->window, "........");
3991 prev_id = NULL;
3994 if (view->focussed && nprinted == s->selected_line - 1)
3995 wstandend(view->window);
3996 waddstr(view->window, " ");
3998 if (view->ncols <= 9) {
3999 width = 9;
4000 wline = wcsdup(L"");
4001 if (wline == NULL) {
4002 err = got_error_from_errno("wcsdup");
4003 free(line);
4004 return err;
4006 } else if (s->first_displayed_line + nprinted ==
4007 s->matched_line &&
4008 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4009 err = add_matched_line(&width, line, view->ncols - 9, 9,
4010 view->window, regmatch);
4011 if (err) {
4012 free(line);
4013 return err;
4015 width += 9;
4016 } else {
4017 err = format_line(&wline, &width, line,
4018 view->ncols - 9, 9);
4019 waddwstr(view->window, wline);
4020 free(wline);
4021 wline = NULL;
4022 width += 9;
4025 if (width <= view->ncols - 1)
4026 waddch(view->window, '\n');
4027 if (++nprinted == 1)
4028 s->first_displayed_line = lineno;
4029 free(line);
4031 s->last_displayed_line = lineno;
4033 view_vborder(view);
4035 return NULL;
4038 static const struct got_error *
4039 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4041 const struct got_error *err = NULL;
4042 struct tog_blame_cb_args *a = arg;
4043 struct tog_blame_line *line;
4044 int errcode;
4046 if (nlines != a->nlines ||
4047 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4048 return got_error(GOT_ERR_RANGE);
4050 errcode = pthread_mutex_lock(&tog_mutex);
4051 if (errcode)
4052 return got_error_set_errno(errcode, "pthread_mutex_lock");
4054 if (*a->quit) { /* user has quit the blame view */
4055 err = got_error(GOT_ERR_ITER_COMPLETED);
4056 goto done;
4059 if (lineno == -1)
4060 goto done; /* no change in this commit */
4062 line = &a->lines[lineno - 1];
4063 if (line->annotated)
4064 goto done;
4066 line->id = got_object_id_dup(id);
4067 if (line->id == NULL) {
4068 err = got_error_from_errno("got_object_id_dup");
4069 goto done;
4071 line->annotated = 1;
4072 done:
4073 errcode = pthread_mutex_unlock(&tog_mutex);
4074 if (errcode)
4075 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4076 return err;
4079 static void *
4080 blame_thread(void *arg)
4082 const struct got_error *err;
4083 struct tog_blame_thread_args *ta = arg;
4084 struct tog_blame_cb_args *a = ta->cb_args;
4085 int errcode;
4087 err = block_signals_used_by_main_thread();
4088 if (err)
4089 return (void *)err;
4091 err = got_blame(ta->path, a->commit_id, ta->repo,
4092 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4093 if (err && err->code == GOT_ERR_CANCELLED)
4094 err = NULL;
4096 errcode = pthread_mutex_lock(&tog_mutex);
4097 if (errcode)
4098 return (void *)got_error_set_errno(errcode,
4099 "pthread_mutex_lock");
4101 got_repo_close(ta->repo);
4102 ta->repo = NULL;
4103 *ta->complete = 1;
4105 errcode = pthread_mutex_unlock(&tog_mutex);
4106 if (errcode && err == NULL)
4107 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4109 return (void *)err;
4112 static struct got_object_id *
4113 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4114 int first_displayed_line, int selected_line)
4116 struct tog_blame_line *line;
4118 if (nlines <= 0)
4119 return NULL;
4121 line = &lines[first_displayed_line - 1 + selected_line - 1];
4122 if (!line->annotated)
4123 return NULL;
4125 return line->id;
4128 static const struct got_error *
4129 stop_blame(struct tog_blame *blame)
4131 const struct got_error *err = NULL;
4132 int i;
4134 if (blame->thread) {
4135 int errcode;
4136 errcode = pthread_mutex_unlock(&tog_mutex);
4137 if (errcode)
4138 return got_error_set_errno(errcode,
4139 "pthread_mutex_unlock");
4140 errcode = pthread_join(blame->thread, (void **)&err);
4141 if (errcode)
4142 return got_error_set_errno(errcode, "pthread_join");
4143 errcode = pthread_mutex_lock(&tog_mutex);
4144 if (errcode)
4145 return got_error_set_errno(errcode,
4146 "pthread_mutex_lock");
4147 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4148 err = NULL;
4149 blame->thread = NULL;
4151 if (blame->thread_args.repo) {
4152 got_repo_close(blame->thread_args.repo);
4153 blame->thread_args.repo = NULL;
4155 if (blame->f) {
4156 if (fclose(blame->f) != 0 && err == NULL)
4157 err = got_error_from_errno("fclose");
4158 blame->f = NULL;
4160 if (blame->lines) {
4161 for (i = 0; i < blame->nlines; i++)
4162 free(blame->lines[i].id);
4163 free(blame->lines);
4164 blame->lines = NULL;
4166 free(blame->cb_args.commit_id);
4167 blame->cb_args.commit_id = NULL;
4169 return err;
4172 static const struct got_error *
4173 cancel_blame_view(void *arg)
4175 const struct got_error *err = NULL;
4176 int *done = arg;
4177 int errcode;
4179 errcode = pthread_mutex_lock(&tog_mutex);
4180 if (errcode)
4181 return got_error_set_errno(errcode,
4182 "pthread_mutex_unlock");
4184 if (*done)
4185 err = got_error(GOT_ERR_CANCELLED);
4187 errcode = pthread_mutex_unlock(&tog_mutex);
4188 if (errcode)
4189 return got_error_set_errno(errcode,
4190 "pthread_mutex_lock");
4192 return err;
4195 static const struct got_error *
4196 run_blame(struct tog_view *view)
4198 struct tog_blame_view_state *s = &view->state.blame;
4199 struct tog_blame *blame = &s->blame;
4200 const struct got_error *err = NULL;
4201 struct got_blob_object *blob = NULL;
4202 struct got_repository *thread_repo = NULL;
4203 struct got_object_id *obj_id = NULL;
4204 int obj_type;
4206 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4207 s->path);
4208 if (err)
4209 return err;
4211 err = got_object_get_type(&obj_type, s->repo, obj_id);
4212 if (err)
4213 goto done;
4215 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4216 err = got_error(GOT_ERR_OBJ_TYPE);
4217 goto done;
4220 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4221 if (err)
4222 goto done;
4223 blame->f = got_opentemp();
4224 if (blame->f == NULL) {
4225 err = got_error_from_errno("got_opentemp");
4226 goto done;
4228 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4229 &blame->line_offsets, blame->f, blob);
4230 if (err || blame->nlines == 0)
4231 goto done;
4233 /* Don't include \n at EOF in the blame line count. */
4234 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4235 blame->nlines--;
4237 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4238 if (blame->lines == NULL) {
4239 err = got_error_from_errno("calloc");
4240 goto done;
4243 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4244 if (err)
4245 goto done;
4247 blame->cb_args.view = view;
4248 blame->cb_args.lines = blame->lines;
4249 blame->cb_args.nlines = blame->nlines;
4250 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4251 if (blame->cb_args.commit_id == NULL) {
4252 err = got_error_from_errno("got_object_id_dup");
4253 goto done;
4255 blame->cb_args.quit = &s->done;
4257 blame->thread_args.path = s->path;
4258 blame->thread_args.repo = thread_repo;
4259 blame->thread_args.cb_args = &blame->cb_args;
4260 blame->thread_args.complete = &s->blame_complete;
4261 blame->thread_args.cancel_cb = cancel_blame_view;
4262 blame->thread_args.cancel_arg = &s->done;
4263 s->blame_complete = 0;
4265 done:
4266 if (blob)
4267 got_object_blob_close(blob);
4268 free(obj_id);
4269 if (err)
4270 stop_blame(blame);
4271 return err;
4274 static const struct got_error *
4275 open_blame_view(struct tog_view *view, char *path,
4276 struct got_object_id *commit_id, struct got_repository *repo)
4278 const struct got_error *err = NULL;
4279 struct tog_blame_view_state *s = &view->state.blame;
4281 SIMPLEQ_INIT(&s->blamed_commits);
4283 s->path = strdup(path);
4284 if (s->path == NULL)
4285 return got_error_from_errno("strdup");
4287 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4288 if (err) {
4289 free(s->path);
4290 return err;
4293 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4294 s->first_displayed_line = 1;
4295 s->last_displayed_line = view->nlines;
4296 s->selected_line = 1;
4297 s->blame_complete = 0;
4298 s->repo = repo;
4299 s->commit_id = commit_id;
4300 memset(&s->blame, 0, sizeof(s->blame));
4302 SIMPLEQ_INIT(&s->colors);
4303 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4304 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4305 get_color_value("TOG_COLOR_COMMIT"));
4306 if (err)
4307 return err;
4310 view->show = show_blame_view;
4311 view->input = input_blame_view;
4312 view->close = close_blame_view;
4313 view->search_start = search_start_blame_view;
4314 view->search_next = search_next_blame_view;
4316 return run_blame(view);
4319 static const struct got_error *
4320 close_blame_view(struct tog_view *view)
4322 const struct got_error *err = NULL;
4323 struct tog_blame_view_state *s = &view->state.blame;
4325 if (s->blame.thread)
4326 err = stop_blame(&s->blame);
4328 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4329 struct got_object_qid *blamed_commit;
4330 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4331 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4332 got_object_qid_free(blamed_commit);
4335 free(s->path);
4336 free_colors(&s->colors);
4338 return err;
4341 static const struct got_error *
4342 search_start_blame_view(struct tog_view *view)
4344 struct tog_blame_view_state *s = &view->state.blame;
4346 s->matched_line = 0;
4347 return NULL;
4350 static const struct got_error *
4351 search_next_blame_view(struct tog_view *view)
4353 struct tog_blame_view_state *s = &view->state.blame;
4354 int lineno;
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 char *line = NULL;
4375 off_t offset;
4376 size_t len;
4378 if (lineno <= 0 || lineno > s->blame.nlines) {
4379 if (s->matched_line == 0) {
4380 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4381 free(line);
4382 break;
4385 if (view->searching == TOG_SEARCH_FORWARD)
4386 lineno = 1;
4387 else
4388 lineno = s->blame.nlines;
4391 offset = s->blame.line_offsets[lineno - 1];
4392 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4393 free(line);
4394 return got_error_from_errno("fseeko");
4396 free(line);
4397 line = parse_next_line(s->blame.f, &len);
4398 if (line &&
4399 match_line(line, &view->regex, 1, &view->regmatch)) {
4400 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 s->matched_line = lineno;
4402 free(line);
4403 break;
4405 free(line);
4406 if (view->searching == TOG_SEARCH_FORWARD)
4407 lineno++;
4408 else
4409 lineno--;
4412 if (s->matched_line) {
4413 s->first_displayed_line = s->matched_line;
4414 s->selected_line = 1;
4417 return NULL;
4420 static const struct got_error *
4421 show_blame_view(struct tog_view *view)
4423 const struct got_error *err = NULL;
4424 struct tog_blame_view_state *s = &view->state.blame;
4425 int errcode;
4427 if (s->blame.thread == NULL) {
4428 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4429 &s->blame.thread_args);
4430 if (errcode)
4431 return got_error_set_errno(errcode, "pthread_create");
4433 halfdelay(1); /* fast refresh while annotating */
4436 if (s->blame_complete)
4437 halfdelay(10); /* disable fast refresh */
4439 err = draw_blame(view);
4441 view_vborder(view);
4442 return err;
4445 static const struct got_error *
4446 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4447 struct tog_view **focus_view, struct tog_view *view, int ch)
4449 const struct got_error *err = NULL, *thread_err = NULL;
4450 struct tog_view *diff_view;
4451 struct tog_blame_view_state *s = &view->state.blame;
4452 int begin_x = 0;
4454 switch (ch) {
4455 case 'q':
4456 s->done = 1;
4457 break;
4458 case 'k':
4459 case KEY_UP:
4460 if (s->selected_line > 1)
4461 s->selected_line--;
4462 else if (s->selected_line == 1 &&
4463 s->first_displayed_line > 1)
4464 s->first_displayed_line--;
4465 break;
4466 case KEY_PPAGE:
4467 case CTRL('b'):
4468 if (s->first_displayed_line == 1) {
4469 s->selected_line = 1;
4470 break;
4472 if (s->first_displayed_line > view->nlines - 2)
4473 s->first_displayed_line -=
4474 (view->nlines - 2);
4475 else
4476 s->first_displayed_line = 1;
4477 break;
4478 case 'j':
4479 case KEY_DOWN:
4480 if (s->selected_line < view->nlines - 2 &&
4481 s->first_displayed_line +
4482 s->selected_line <= s->blame.nlines)
4483 s->selected_line++;
4484 else if (s->last_displayed_line <
4485 s->blame.nlines)
4486 s->first_displayed_line++;
4487 break;
4488 case 'b':
4489 case 'p': {
4490 struct got_object_id *id = NULL;
4491 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4492 s->first_displayed_line, s->selected_line);
4493 if (id == NULL)
4494 break;
4495 if (ch == 'p') {
4496 struct got_commit_object *commit;
4497 struct got_object_qid *pid;
4498 struct got_object_id *blob_id = NULL;
4499 int obj_type;
4500 err = got_object_open_as_commit(&commit,
4501 s->repo, id);
4502 if (err)
4503 break;
4504 pid = SIMPLEQ_FIRST(
4505 got_object_commit_get_parent_ids(commit));
4506 if (pid == NULL) {
4507 got_object_commit_close(commit);
4508 break;
4510 /* Check if path history ends here. */
4511 err = got_object_id_by_path(&blob_id, s->repo,
4512 pid->id, s->path);
4513 if (err) {
4514 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4515 err = NULL;
4516 got_object_commit_close(commit);
4517 break;
4519 err = got_object_get_type(&obj_type, s->repo,
4520 blob_id);
4521 free(blob_id);
4522 /* Can't blame non-blob type objects. */
4523 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4524 got_object_commit_close(commit);
4525 break;
4527 err = got_object_qid_alloc(&s->blamed_commit,
4528 pid->id);
4529 got_object_commit_close(commit);
4530 } else {
4531 if (got_object_id_cmp(id,
4532 s->blamed_commit->id) == 0)
4533 break;
4534 err = got_object_qid_alloc(&s->blamed_commit,
4535 id);
4537 if (err)
4538 break;
4539 s->done = 1;
4540 thread_err = stop_blame(&s->blame);
4541 s->done = 0;
4542 if (thread_err)
4543 break;
4544 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4545 s->blamed_commit, entry);
4546 err = run_blame(view);
4547 if (err)
4548 break;
4549 break;
4551 case 'B': {
4552 struct got_object_qid *first;
4553 first = SIMPLEQ_FIRST(&s->blamed_commits);
4554 if (!got_object_id_cmp(first->id, s->commit_id))
4555 break;
4556 s->done = 1;
4557 thread_err = stop_blame(&s->blame);
4558 s->done = 0;
4559 if (thread_err)
4560 break;
4561 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4562 got_object_qid_free(s->blamed_commit);
4563 s->blamed_commit =
4564 SIMPLEQ_FIRST(&s->blamed_commits);
4565 err = run_blame(view);
4566 if (err)
4567 break;
4568 break;
4570 case KEY_ENTER:
4571 case '\r': {
4572 struct got_object_id *id = NULL;
4573 struct got_object_qid *pid;
4574 struct got_commit_object *commit = NULL;
4575 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4576 s->first_displayed_line, s->selected_line);
4577 if (id == NULL)
4578 break;
4579 err = got_object_open_as_commit(&commit, s->repo, id);
4580 if (err)
4581 break;
4582 pid = SIMPLEQ_FIRST(
4583 got_object_commit_get_parent_ids(commit));
4584 if (view_is_parent_view(view))
4585 begin_x = view_split_begin_x(view->begin_x);
4586 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4587 if (diff_view == NULL) {
4588 got_object_commit_close(commit);
4589 err = got_error_from_errno("view_open");
4590 break;
4592 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4593 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4594 got_object_commit_close(commit);
4595 if (err) {
4596 view_close(diff_view);
4597 break;
4599 if (view_is_parent_view(view)) {
4600 err = view_close_child(view);
4601 if (err)
4602 break;
4603 err = view_set_child(view, diff_view);
4604 if (err) {
4605 view_close(diff_view);
4606 break;
4608 *focus_view = diff_view;
4609 } else
4610 *new_view = diff_view;
4611 if (err)
4612 break;
4613 break;
4615 case KEY_NPAGE:
4616 case CTRL('f'):
4617 case ' ':
4618 if (s->last_displayed_line >= s->blame.nlines &&
4619 s->selected_line >= MIN(s->blame.nlines,
4620 view->nlines - 2)) {
4621 break;
4623 if (s->last_displayed_line >= s->blame.nlines &&
4624 s->selected_line < view->nlines - 2) {
4625 s->selected_line = MIN(s->blame.nlines,
4626 view->nlines - 2);
4627 break;
4629 if (s->last_displayed_line + view->nlines - 2
4630 <= s->blame.nlines)
4631 s->first_displayed_line +=
4632 view->nlines - 2;
4633 else
4634 s->first_displayed_line =
4635 s->blame.nlines -
4636 (view->nlines - 3);
4637 break;
4638 case KEY_RESIZE:
4639 if (s->selected_line > view->nlines - 2) {
4640 s->selected_line = MIN(s->blame.nlines,
4641 view->nlines - 2);
4643 break;
4644 default:
4645 break;
4647 return thread_err ? thread_err : err;
4650 static const struct got_error *
4651 cmd_blame(int argc, char *argv[])
4653 const struct got_error *error;
4654 struct got_repository *repo = NULL;
4655 struct got_worktree *worktree = NULL;
4656 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4657 char *link_target = NULL;
4658 struct got_object_id *commit_id = NULL;
4659 char *commit_id_str = NULL;
4660 int ch;
4661 struct tog_view *view;
4663 #ifndef PROFILE
4664 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4665 NULL) == -1)
4666 err(1, "pledge");
4667 #endif
4669 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4670 switch (ch) {
4671 case 'c':
4672 commit_id_str = optarg;
4673 break;
4674 case 'r':
4675 repo_path = realpath(optarg, NULL);
4676 if (repo_path == NULL)
4677 return got_error_from_errno2("realpath",
4678 optarg);
4679 break;
4680 default:
4681 usage_blame();
4682 /* NOTREACHED */
4686 argc -= optind;
4687 argv += optind;
4689 if (argc != 1)
4690 usage_blame();
4692 cwd = getcwd(NULL, 0);
4693 if (cwd == NULL)
4694 return got_error_from_errno("getcwd");
4696 error = got_worktree_open(&worktree, cwd);
4697 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4698 goto done;
4700 if (repo_path == NULL) {
4701 if (worktree)
4702 repo_path =
4703 strdup(got_worktree_get_repo_path(worktree));
4704 else
4705 repo_path = strdup(cwd);
4707 if (repo_path == NULL) {
4708 error = got_error_from_errno("strdup");
4709 goto done;
4712 error = got_repo_open(&repo, repo_path, NULL);
4713 if (error != NULL)
4714 goto done;
4716 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4717 worktree);
4718 if (error)
4719 goto done;
4721 init_curses();
4723 error = apply_unveil(got_repo_get_path(repo), NULL);
4724 if (error)
4725 goto done;
4727 if (commit_id_str == NULL) {
4728 struct got_reference *head_ref;
4729 error = got_ref_open(&head_ref, repo, worktree ?
4730 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4731 if (error != NULL)
4732 goto done;
4733 error = got_ref_resolve(&commit_id, repo, head_ref);
4734 got_ref_close(head_ref);
4735 } else {
4736 error = got_repo_match_object_id(&commit_id, NULL,
4737 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4739 if (error != NULL)
4740 goto done;
4742 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4743 if (view == NULL) {
4744 error = got_error_from_errno("view_open");
4745 goto done;
4748 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4749 commit_id, repo);
4750 if (error)
4751 goto done;
4753 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4754 commit_id, repo);
4755 if (error)
4756 goto done;
4757 if (worktree) {
4758 /* Release work tree lock. */
4759 got_worktree_close(worktree);
4760 worktree = NULL;
4762 error = view_loop(view);
4763 done:
4764 free(repo_path);
4765 free(in_repo_path);
4766 free(link_target);
4767 free(cwd);
4768 free(commit_id);
4769 if (worktree)
4770 got_worktree_close(worktree);
4771 if (repo)
4772 got_repo_close(repo);
4773 return error;
4776 static const struct got_error *
4777 draw_tree_entries(struct tog_view *view, const char *parent_path)
4779 struct tog_tree_view_state *s = &view->state.tree;
4780 const struct got_error *err = NULL;
4781 struct got_tree_entry *te;
4782 wchar_t *wline;
4783 struct tog_color *tc;
4784 int width, n, i, nentries;
4785 int limit = view->nlines;
4787 s->ndisplayed = 0;
4789 werase(view->window);
4791 if (limit == 0)
4792 return NULL;
4794 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4795 if (err)
4796 return err;
4797 if (view_needs_focus_indication(view))
4798 wstandout(view->window);
4799 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4800 if (tc)
4801 wattr_on(view->window,
4802 COLOR_PAIR(tc->colorpair), NULL);
4803 waddwstr(view->window, wline);
4804 if (tc)
4805 wattr_off(view->window,
4806 COLOR_PAIR(tc->colorpair), NULL);
4807 if (view_needs_focus_indication(view))
4808 wstandend(view->window);
4809 free(wline);
4810 wline = NULL;
4811 if (width < view->ncols - 1)
4812 waddch(view->window, '\n');
4813 if (--limit <= 0)
4814 return NULL;
4815 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4816 if (err)
4817 return err;
4818 waddwstr(view->window, wline);
4819 free(wline);
4820 wline = NULL;
4821 if (width < view->ncols - 1)
4822 waddch(view->window, '\n');
4823 if (--limit <= 0)
4824 return NULL;
4825 waddch(view->window, '\n');
4826 if (--limit <= 0)
4827 return NULL;
4829 if (s->first_displayed_entry == NULL) {
4830 te = got_object_tree_get_first_entry(s->tree);
4831 if (s->selected == 0) {
4832 if (view->focussed)
4833 wstandout(view->window);
4834 s->selected_entry = NULL;
4836 waddstr(view->window, " ..\n"); /* parent directory */
4837 if (s->selected == 0 && view->focussed)
4838 wstandend(view->window);
4839 s->ndisplayed++;
4840 if (--limit <= 0)
4841 return NULL;
4842 n = 1;
4843 } else {
4844 n = 0;
4845 te = s->first_displayed_entry;
4848 nentries = got_object_tree_get_nentries(s->tree);
4849 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4850 char *line = NULL, *id_str = NULL, *link_target = NULL;
4851 const char *modestr = "";
4852 mode_t mode;
4854 te = got_object_tree_get_entry(s->tree, i);
4855 mode = got_tree_entry_get_mode(te);
4857 if (s->show_ids) {
4858 err = got_object_id_str(&id_str,
4859 got_tree_entry_get_id(te));
4860 if (err)
4861 return got_error_from_errno(
4862 "got_object_id_str");
4864 if (got_object_tree_entry_is_submodule(te))
4865 modestr = "$";
4866 else if (S_ISLNK(mode)) {
4867 int i;
4869 err = got_tree_entry_get_symlink_target(&link_target,
4870 te, s->repo);
4871 if (err) {
4872 free(id_str);
4873 return err;
4875 for (i = 0; i < strlen(link_target); i++) {
4876 if (!isprint((unsigned char)link_target[i]))
4877 link_target[i] = '?';
4879 modestr = "@";
4881 else if (S_ISDIR(mode))
4882 modestr = "/";
4883 else if (mode & S_IXUSR)
4884 modestr = "*";
4885 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4886 got_tree_entry_get_name(te), modestr,
4887 link_target ? " -> ": "",
4888 link_target ? link_target : "") == -1) {
4889 free(id_str);
4890 free(link_target);
4891 return got_error_from_errno("asprintf");
4893 free(id_str);
4894 free(link_target);
4895 err = format_line(&wline, &width, line, view->ncols, 0);
4896 if (err) {
4897 free(line);
4898 break;
4900 if (n == s->selected) {
4901 if (view->focussed)
4902 wstandout(view->window);
4903 s->selected_entry = te;
4905 tc = match_color(&s->colors, line);
4906 if (tc)
4907 wattr_on(view->window,
4908 COLOR_PAIR(tc->colorpair), NULL);
4909 waddwstr(view->window, wline);
4910 if (tc)
4911 wattr_off(view->window,
4912 COLOR_PAIR(tc->colorpair), NULL);
4913 if (width < view->ncols - 1)
4914 waddch(view->window, '\n');
4915 if (n == s->selected && view->focussed)
4916 wstandend(view->window);
4917 free(line);
4918 free(wline);
4919 wline = NULL;
4920 n++;
4921 s->ndisplayed++;
4922 s->last_displayed_entry = te;
4923 if (--limit <= 0)
4924 break;
4927 return err;
4930 static void
4931 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4933 struct got_tree_entry *te;
4934 int isroot = s->tree == s->root;
4935 int i = 0;
4937 if (s->first_displayed_entry == NULL)
4938 return;
4940 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4941 while (i++ < maxscroll) {
4942 if (te == NULL) {
4943 if (!isroot)
4944 s->first_displayed_entry = NULL;
4945 break;
4947 s->first_displayed_entry = te;
4948 te = got_tree_entry_get_prev(s->tree, te);
4952 static void
4953 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4955 struct got_tree_entry *next, *last;
4956 int n = 0;
4958 if (s->first_displayed_entry)
4959 next = got_tree_entry_get_next(s->tree,
4960 s->first_displayed_entry);
4961 else
4962 next = got_object_tree_get_first_entry(s->tree);
4964 last = s->last_displayed_entry;
4965 while (next && last && n++ < maxscroll) {
4966 last = got_tree_entry_get_next(s->tree, last);
4967 if (last) {
4968 s->first_displayed_entry = next;
4969 next = got_tree_entry_get_next(s->tree, next);
4974 static const struct got_error *
4975 tree_entry_path(char **path, struct tog_parent_trees *parents,
4976 struct got_tree_entry *te)
4978 const struct got_error *err = NULL;
4979 struct tog_parent_tree *pt;
4980 size_t len = 2; /* for leading slash and NUL */
4982 TAILQ_FOREACH(pt, parents, entry)
4983 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4984 + 1 /* slash */;
4985 if (te)
4986 len += strlen(got_tree_entry_get_name(te));
4988 *path = calloc(1, len);
4989 if (path == NULL)
4990 return got_error_from_errno("calloc");
4992 (*path)[0] = '/';
4993 pt = TAILQ_LAST(parents, tog_parent_trees);
4994 while (pt) {
4995 const char *name = got_tree_entry_get_name(pt->selected_entry);
4996 if (strlcat(*path, name, len) >= len) {
4997 err = got_error(GOT_ERR_NO_SPACE);
4998 goto done;
5000 if (strlcat(*path, "/", len) >= len) {
5001 err = got_error(GOT_ERR_NO_SPACE);
5002 goto done;
5004 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5006 if (te) {
5007 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5008 err = got_error(GOT_ERR_NO_SPACE);
5009 goto done;
5012 done:
5013 if (err) {
5014 free(*path);
5015 *path = NULL;
5017 return err;
5020 static const struct got_error *
5021 blame_tree_entry(struct tog_view **new_view, int begin_x,
5022 struct got_tree_entry *te, struct tog_parent_trees *parents,
5023 struct got_object_id *commit_id, struct got_repository *repo)
5025 const struct got_error *err = NULL;
5026 char *path;
5027 struct tog_view *blame_view;
5029 *new_view = NULL;
5031 err = tree_entry_path(&path, parents, te);
5032 if (err)
5033 return err;
5035 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5036 if (blame_view == NULL) {
5037 err = got_error_from_errno("view_open");
5038 goto done;
5041 err = open_blame_view(blame_view, path, commit_id, repo);
5042 if (err) {
5043 if (err->code == GOT_ERR_CANCELLED)
5044 err = NULL;
5045 view_close(blame_view);
5046 } else
5047 *new_view = blame_view;
5048 done:
5049 free(path);
5050 return err;
5053 static const struct got_error *
5054 log_tree_entry(struct tog_view **new_view, int begin_x,
5055 struct got_tree_entry *te, struct tog_parent_trees *parents,
5056 struct got_object_id *commit_id, struct got_repository *repo)
5058 struct tog_view *log_view;
5059 const struct got_error *err = NULL;
5060 char *path;
5062 *new_view = NULL;
5064 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5065 if (log_view == NULL)
5066 return got_error_from_errno("view_open");
5068 err = tree_entry_path(&path, parents, te);
5069 if (err)
5070 return err;
5072 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5073 if (err)
5074 view_close(log_view);
5075 else
5076 *new_view = log_view;
5077 free(path);
5078 return err;
5081 static const struct got_error *
5082 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5083 struct got_object_id *commit_id, struct got_repository *repo)
5085 const struct got_error *err = NULL;
5086 char *commit_id_str = NULL;
5087 struct tog_tree_view_state *s = &view->state.tree;
5089 TAILQ_INIT(&s->parents);
5091 err = got_object_id_str(&commit_id_str, commit_id);
5092 if (err != NULL)
5093 goto done;
5095 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5096 err = got_error_from_errno("asprintf");
5097 goto done;
5100 s->root = s->tree = root;
5101 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5102 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5103 s->commit_id = got_object_id_dup(commit_id);
5104 if (s->commit_id == NULL) {
5105 err = got_error_from_errno("got_object_id_dup");
5106 goto done;
5108 s->repo = repo;
5110 SIMPLEQ_INIT(&s->colors);
5112 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5113 err = add_color(&s->colors, "\\$$",
5114 TOG_COLOR_TREE_SUBMODULE,
5115 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5116 if (err)
5117 goto done;
5118 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5119 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5120 if (err) {
5121 free_colors(&s->colors);
5122 goto done;
5124 err = add_color(&s->colors, "/$",
5125 TOG_COLOR_TREE_DIRECTORY,
5126 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5127 if (err) {
5128 free_colors(&s->colors);
5129 goto done;
5132 err = add_color(&s->colors, "\\*$",
5133 TOG_COLOR_TREE_EXECUTABLE,
5134 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5135 if (err) {
5136 free_colors(&s->colors);
5137 goto done;
5140 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5141 get_color_value("TOG_COLOR_COMMIT"));
5142 if (err) {
5143 free_colors(&s->colors);
5144 goto done;
5148 view->show = show_tree_view;
5149 view->input = input_tree_view;
5150 view->close = close_tree_view;
5151 view->search_start = search_start_tree_view;
5152 view->search_next = search_next_tree_view;
5153 done:
5154 free(commit_id_str);
5155 if (err) {
5156 free(s->tree_label);
5157 s->tree_label = NULL;
5159 return err;
5162 static const struct got_error *
5163 close_tree_view(struct tog_view *view)
5165 struct tog_tree_view_state *s = &view->state.tree;
5167 free_colors(&s->colors);
5168 free(s->tree_label);
5169 s->tree_label = NULL;
5170 free(s->commit_id);
5171 s->commit_id = NULL;
5172 while (!TAILQ_EMPTY(&s->parents)) {
5173 struct tog_parent_tree *parent;
5174 parent = TAILQ_FIRST(&s->parents);
5175 TAILQ_REMOVE(&s->parents, parent, entry);
5176 free(parent);
5179 if (s->tree != s->root)
5180 got_object_tree_close(s->tree);
5181 got_object_tree_close(s->root);
5182 return NULL;
5185 static const struct got_error *
5186 search_start_tree_view(struct tog_view *view)
5188 struct tog_tree_view_state *s = &view->state.tree;
5190 s->matched_entry = NULL;
5191 return NULL;
5194 static int
5195 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5197 regmatch_t regmatch;
5199 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5200 0) == 0;
5203 static const struct got_error *
5204 search_next_tree_view(struct tog_view *view)
5206 struct tog_tree_view_state *s = &view->state.tree;
5207 struct got_tree_entry *te = NULL;
5209 if (!view->searching) {
5210 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5211 return NULL;
5214 if (s->matched_entry) {
5215 if (view->searching == TOG_SEARCH_FORWARD) {
5216 if (s->selected_entry)
5217 te = got_tree_entry_get_next(s->tree,
5218 s->selected_entry);
5219 else
5220 te = got_object_tree_get_first_entry(s->tree);
5221 } else {
5222 if (s->selected_entry == NULL)
5223 te = got_object_tree_get_last_entry(s->tree);
5224 else
5225 te = got_tree_entry_get_prev(s->tree,
5226 s->selected_entry);
5228 } else {
5229 if (view->searching == TOG_SEARCH_FORWARD)
5230 te = got_object_tree_get_first_entry(s->tree);
5231 else
5232 te = got_object_tree_get_last_entry(s->tree);
5235 while (1) {
5236 if (te == NULL) {
5237 if (s->matched_entry == NULL) {
5238 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5239 return NULL;
5241 if (view->searching == TOG_SEARCH_FORWARD)
5242 te = got_object_tree_get_first_entry(s->tree);
5243 else
5244 te = got_object_tree_get_last_entry(s->tree);
5247 if (match_tree_entry(te, &view->regex)) {
5248 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5249 s->matched_entry = te;
5250 break;
5253 if (view->searching == TOG_SEARCH_FORWARD)
5254 te = got_tree_entry_get_next(s->tree, te);
5255 else
5256 te = got_tree_entry_get_prev(s->tree, te);
5259 if (s->matched_entry) {
5260 s->first_displayed_entry = s->matched_entry;
5261 s->selected = 0;
5264 return NULL;
5267 static const struct got_error *
5268 show_tree_view(struct tog_view *view)
5270 const struct got_error *err = NULL;
5271 struct tog_tree_view_state *s = &view->state.tree;
5272 char *parent_path;
5274 err = tree_entry_path(&parent_path, &s->parents, NULL);
5275 if (err)
5276 return err;
5278 err = draw_tree_entries(view, parent_path);
5279 free(parent_path);
5281 view_vborder(view);
5282 return err;
5285 static const struct got_error *
5286 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5287 struct tog_view **focus_view, struct tog_view *view, int ch)
5289 const struct got_error *err = NULL;
5290 struct tog_tree_view_state *s = &view->state.tree;
5291 struct tog_view *log_view, *ref_view;
5292 int begin_x = 0;
5294 switch (ch) {
5295 case 'i':
5296 s->show_ids = !s->show_ids;
5297 break;
5298 case 'l':
5299 if (!s->selected_entry)
5300 break;
5301 if (view_is_parent_view(view))
5302 begin_x = view_split_begin_x(view->begin_x);
5303 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5304 &s->parents, s->commit_id, s->repo);
5305 if (view_is_parent_view(view)) {
5306 err = view_close_child(view);
5307 if (err)
5308 return err;
5309 err = view_set_child(view, log_view);
5310 if (err) {
5311 view_close(log_view);
5312 break;
5314 *focus_view = log_view;
5315 } else
5316 *new_view = log_view;
5317 break;
5318 case 'r':
5319 if (view_is_parent_view(view))
5320 begin_x = view_split_begin_x(view->begin_x);
5321 ref_view = view_open(view->nlines, view->ncols,
5322 view->begin_y, begin_x, TOG_VIEW_REF);
5323 if (ref_view == NULL)
5324 return got_error_from_errno("view_open");
5325 err = open_ref_view(ref_view, s->repo);
5326 if (err) {
5327 view_close(ref_view);
5328 return err;
5330 if (view_is_parent_view(view)) {
5331 err = view_close_child(view);
5332 if (err)
5333 return err;
5334 err = view_set_child(view, ref_view);
5335 if (err) {
5336 view_close(ref_view);
5337 break;
5339 *focus_view = ref_view;
5340 } else
5341 *new_view = ref_view;
5342 break;
5343 case 'k':
5344 case KEY_UP:
5345 if (s->selected > 0) {
5346 s->selected--;
5347 break;
5349 tree_scroll_up(s, 1);
5350 break;
5351 case KEY_PPAGE:
5352 case CTRL('b'):
5353 if (s->tree == s->root) {
5354 if (got_object_tree_get_first_entry(s->tree) ==
5355 s->first_displayed_entry)
5356 s->selected = 0;
5357 } else {
5358 if (s->first_displayed_entry == NULL)
5359 s->selected = 0;
5361 tree_scroll_up(s, MAX(0, view->nlines - 3));
5362 break;
5363 case 'j':
5364 case KEY_DOWN:
5365 if (s->selected < s->ndisplayed - 1) {
5366 s->selected++;
5367 break;
5369 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5370 == NULL)
5371 /* can't scroll any further */
5372 break;
5373 tree_scroll_down(s, 1);
5374 break;
5375 case KEY_NPAGE:
5376 case CTRL('f'):
5377 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5378 == NULL) {
5379 /* can't scroll any further; move cursor down */
5380 if (s->selected < s->ndisplayed - 1)
5381 s->selected = s->ndisplayed - 1;
5382 break;
5384 tree_scroll_down(s, view->nlines - 3);
5385 break;
5386 case KEY_ENTER:
5387 case '\r':
5388 case KEY_BACKSPACE:
5389 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5390 struct tog_parent_tree *parent;
5391 /* user selected '..' */
5392 if (s->tree == s->root)
5393 break;
5394 parent = TAILQ_FIRST(&s->parents);
5395 TAILQ_REMOVE(&s->parents, parent,
5396 entry);
5397 got_object_tree_close(s->tree);
5398 s->tree = parent->tree;
5399 s->first_displayed_entry =
5400 parent->first_displayed_entry;
5401 s->selected_entry =
5402 parent->selected_entry;
5403 s->selected = parent->selected;
5404 free(parent);
5405 } else if (S_ISDIR(got_tree_entry_get_mode(
5406 s->selected_entry))) {
5407 struct got_tree_object *subtree;
5408 err = got_object_open_as_tree(&subtree, s->repo,
5409 got_tree_entry_get_id(s->selected_entry));
5410 if (err)
5411 break;
5412 err = tree_view_visit_subtree(s, subtree);
5413 if (err) {
5414 got_object_tree_close(subtree);
5415 break;
5417 } else if (S_ISREG(got_tree_entry_get_mode(
5418 s->selected_entry))) {
5419 struct tog_view *blame_view;
5420 int begin_x = view_is_parent_view(view) ?
5421 view_split_begin_x(view->begin_x) : 0;
5423 err = blame_tree_entry(&blame_view, begin_x,
5424 s->selected_entry, &s->parents,
5425 s->commit_id, s->repo);
5426 if (err)
5427 break;
5428 if (view_is_parent_view(view)) {
5429 err = view_close_child(view);
5430 if (err)
5431 return err;
5432 err = view_set_child(view, blame_view);
5433 if (err) {
5434 view_close(blame_view);
5435 break;
5437 *focus_view = blame_view;
5438 } else
5439 *new_view = blame_view;
5441 break;
5442 case KEY_RESIZE:
5443 if (s->selected > view->nlines)
5444 s->selected = s->ndisplayed - 1;
5445 break;
5446 default:
5447 break;
5450 return err;
5453 __dead static void
5454 usage_tree(void)
5456 endwin();
5457 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5458 getprogname());
5459 exit(1);
5462 static const struct got_error *
5463 cmd_tree(int argc, char *argv[])
5465 const struct got_error *error;
5466 struct got_repository *repo = NULL;
5467 struct got_worktree *worktree = NULL;
5468 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5469 struct got_object_id *commit_id = NULL;
5470 char *commit_id_arg = NULL;
5471 struct got_commit_object *commit = NULL;
5472 struct got_tree_object *tree = NULL;
5473 int ch;
5474 struct tog_view *view;
5476 #ifndef PROFILE
5477 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5478 NULL) == -1)
5479 err(1, "pledge");
5480 #endif
5482 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5483 switch (ch) {
5484 case 'c':
5485 commit_id_arg = optarg;
5486 break;
5487 case 'r':
5488 repo_path = realpath(optarg, NULL);
5489 if (repo_path == NULL)
5490 return got_error_from_errno2("realpath",
5491 optarg);
5492 break;
5493 default:
5494 usage_tree();
5495 /* NOTREACHED */
5499 argc -= optind;
5500 argv += optind;
5502 if (argc > 1)
5503 usage_tree();
5505 cwd = getcwd(NULL, 0);
5506 if (cwd == NULL)
5507 return got_error_from_errno("getcwd");
5509 error = got_worktree_open(&worktree, cwd);
5510 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5511 goto done;
5513 if (repo_path == NULL) {
5514 if (worktree)
5515 repo_path =
5516 strdup(got_worktree_get_repo_path(worktree));
5517 else
5518 repo_path = strdup(cwd);
5520 if (repo_path == NULL) {
5521 error = got_error_from_errno("strdup");
5522 goto done;
5525 error = got_repo_open(&repo, repo_path, NULL);
5526 if (error != NULL)
5527 goto done;
5529 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5530 repo, worktree);
5531 if (error)
5532 goto done;
5534 init_curses();
5536 error = apply_unveil(got_repo_get_path(repo), NULL);
5537 if (error)
5538 goto done;
5540 error = got_repo_match_object_id(&commit_id, NULL,
5541 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5542 GOT_OBJ_TYPE_COMMIT, 1, repo);
5543 if (error)
5544 goto done;
5546 error = got_object_open_as_commit(&commit, repo, commit_id);
5547 if (error)
5548 goto done;
5550 error = got_object_open_as_tree(&tree, repo,
5551 got_object_commit_get_tree_id(commit));
5552 if (error)
5553 goto done;
5555 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5556 if (view == NULL) {
5557 error = got_error_from_errno("view_open");
5558 goto done;
5560 error = open_tree_view(view, tree, commit_id, repo);
5561 if (error)
5562 goto done;
5563 if (!got_path_is_root_dir(in_repo_path)) {
5564 error = tree_view_walk_path(&view->state.tree, commit_id,
5565 in_repo_path);
5566 if (error)
5567 goto done;
5570 if (worktree) {
5571 /* Release work tree lock. */
5572 got_worktree_close(worktree);
5573 worktree = NULL;
5575 error = view_loop(view);
5576 done:
5577 free(repo_path);
5578 free(cwd);
5579 free(commit_id);
5580 if (commit)
5581 got_object_commit_close(commit);
5582 if (tree)
5583 got_object_tree_close(tree);
5584 if (repo)
5585 got_repo_close(repo);
5586 return error;
5589 static const struct got_error *
5590 ref_view_load_refs(struct tog_ref_view_state *s)
5592 const struct got_error *err;
5593 struct got_reflist_entry *sre;
5594 struct tog_reflist_entry *re;
5596 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5597 got_ref_cmp_by_name, NULL);
5598 if (err)
5599 return err;
5601 s->nrefs = 0;
5602 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5603 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5604 continue;
5606 re = malloc(sizeof(*re));
5607 if (re == NULL)
5608 return got_error_from_errno("malloc");
5610 re->ref = sre->ref;
5611 re->idx = s->nrefs++;
5612 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5615 return NULL;
5618 void
5619 ref_view_free_refs(struct tog_ref_view_state *s)
5621 struct tog_reflist_entry *re;
5623 while (!TAILQ_EMPTY(&s->refs)) {
5624 re = TAILQ_FIRST(&s->refs);
5625 TAILQ_REMOVE(&s->refs, re, entry);
5626 free(re);
5628 got_ref_list_free(&s->simplerefs);
5631 static const struct got_error *
5632 open_ref_view(struct tog_view *view, struct got_repository *repo)
5634 const struct got_error *err = NULL;
5635 struct tog_ref_view_state *s = &view->state.ref;
5637 s->selected_entry = 0;
5638 s->repo = repo;
5640 SIMPLEQ_INIT(&s->simplerefs);
5641 TAILQ_INIT(&s->refs);
5642 SIMPLEQ_INIT(&s->colors);
5644 err = ref_view_load_refs(s);
5645 if (err)
5646 return err;
5648 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5650 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5651 err = add_color(&s->colors, "^refs/heads/",
5652 TOG_COLOR_REFS_HEADS,
5653 get_color_value("TOG_COLOR_REFS_HEADS"));
5654 if (err)
5655 goto done;
5657 err = add_color(&s->colors, "^refs/tags/",
5658 TOG_COLOR_REFS_TAGS,
5659 get_color_value("TOG_COLOR_REFS_TAGS"));
5660 if (err)
5661 goto done;
5663 err = add_color(&s->colors, "^refs/remotes/",
5664 TOG_COLOR_REFS_REMOTES,
5665 get_color_value("TOG_COLOR_REFS_REMOTES"));
5666 if (err)
5667 goto done;
5670 view->show = show_ref_view;
5671 view->input = input_ref_view;
5672 view->close = close_ref_view;
5673 view->search_start = search_start_ref_view;
5674 view->search_next = search_next_ref_view;
5675 done:
5676 if (err)
5677 free_colors(&s->colors);
5678 return err;
5681 static const struct got_error *
5682 close_ref_view(struct tog_view *view)
5684 struct tog_ref_view_state *s = &view->state.ref;
5686 ref_view_free_refs(s);
5687 free_colors(&s->colors);
5689 return NULL;
5692 static const struct got_error *
5693 resolve_reflist_entry(struct got_object_id **commit_id,
5694 struct tog_reflist_entry *re, struct got_repository *repo)
5696 const struct got_error *err = NULL;
5697 struct got_object_id *obj_id;
5698 struct got_tag_object *tag = NULL;
5699 int obj_type;
5701 *commit_id = NULL;
5703 err = got_ref_resolve(&obj_id, repo, re->ref);
5704 if (err)
5705 return err;
5707 err = got_object_get_type(&obj_type, repo, obj_id);
5708 if (err)
5709 goto done;
5711 switch (obj_type) {
5712 case GOT_OBJ_TYPE_COMMIT:
5713 *commit_id = obj_id;
5714 break;
5715 case GOT_OBJ_TYPE_TAG:
5716 err = got_object_open_as_tag(&tag, repo, obj_id);
5717 if (err)
5718 goto done;
5719 free(obj_id);
5720 err = got_object_get_type(&obj_type, repo,
5721 got_object_tag_get_object_id(tag));
5722 if (err)
5723 goto done;
5724 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5725 err = got_error(GOT_ERR_OBJ_TYPE);
5726 goto done;
5728 *commit_id = got_object_id_dup(
5729 got_object_tag_get_object_id(tag));
5730 if (*commit_id == NULL) {
5731 err = got_error_from_errno("got_object_id_dup");
5732 goto done;
5734 break;
5735 default:
5736 err = got_error(GOT_ERR_OBJ_TYPE);
5737 break;
5740 done:
5741 if (tag)
5742 got_object_tag_close(tag);
5743 if (err) {
5744 free(*commit_id);
5745 *commit_id = NULL;
5747 return err;
5750 static const struct got_error *
5751 log_ref_entry(struct tog_view **new_view, int begin_x,
5752 struct tog_reflist_entry *re, struct got_repository *repo)
5754 struct tog_view *log_view;
5755 const struct got_error *err = NULL;
5756 struct got_object_id *commit_id = NULL;
5758 *new_view = NULL;
5760 err = resolve_reflist_entry(&commit_id, re, repo);
5761 if (err) {
5762 if (err->code != GOT_ERR_OBJ_TYPE)
5763 return err;
5764 else
5765 return NULL;
5768 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5769 if (log_view == NULL) {
5770 err = got_error_from_errno("view_open");
5771 goto done;
5774 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5775 done:
5776 if (err)
5777 view_close(log_view);
5778 else
5779 *new_view = log_view;
5780 free(commit_id);
5781 return err;
5784 static void
5785 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5787 struct tog_reflist_entry *re;
5788 int i = 0;
5790 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5791 return;
5793 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5794 while (i++ < maxscroll) {
5795 if (re == NULL)
5796 break;
5797 s->first_displayed_entry = re;
5798 re = TAILQ_PREV(re, tog_reflist_head, entry);
5802 static void
5803 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5805 struct tog_reflist_entry *next, *last;
5806 int n = 0;
5808 if (s->first_displayed_entry)
5809 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5810 else
5811 next = TAILQ_FIRST(&s->refs);
5813 last = s->last_displayed_entry;
5814 while (next && last && n++ < maxscroll) {
5815 last = TAILQ_NEXT(last, entry);
5816 if (last) {
5817 s->first_displayed_entry = next;
5818 next = TAILQ_NEXT(next, entry);
5823 static const struct got_error *
5824 search_start_ref_view(struct tog_view *view)
5826 struct tog_ref_view_state *s = &view->state.ref;
5828 s->matched_entry = NULL;
5829 return NULL;
5832 static int
5833 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5835 regmatch_t regmatch;
5837 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5838 0) == 0;
5841 static const struct got_error *
5842 search_next_ref_view(struct tog_view *view)
5844 struct tog_ref_view_state *s = &view->state.ref;
5845 struct tog_reflist_entry *re = NULL;
5847 if (!view->searching) {
5848 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5849 return NULL;
5852 if (s->matched_entry) {
5853 if (view->searching == TOG_SEARCH_FORWARD) {
5854 if (s->selected_entry)
5855 re = TAILQ_NEXT(s->selected_entry, entry);
5856 else
5857 re = TAILQ_PREV(s->selected_entry,
5858 tog_reflist_head, entry);
5859 } else {
5860 if (s->selected_entry == NULL)
5861 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5862 else
5863 re = TAILQ_PREV(s->selected_entry,
5864 tog_reflist_head, entry);
5866 } else {
5867 if (view->searching == TOG_SEARCH_FORWARD)
5868 re = TAILQ_FIRST(&s->refs);
5869 else
5870 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5873 while (1) {
5874 if (re == NULL) {
5875 if (s->matched_entry == NULL) {
5876 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5877 return NULL;
5879 if (view->searching == TOG_SEARCH_FORWARD)
5880 re = TAILQ_FIRST(&s->refs);
5881 else
5882 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5885 if (match_reflist_entry(re, &view->regex)) {
5886 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5887 s->matched_entry = re;
5888 break;
5891 if (view->searching == TOG_SEARCH_FORWARD)
5892 re = TAILQ_NEXT(re, entry);
5893 else
5894 re = TAILQ_PREV(re, tog_reflist_head, entry);
5897 if (s->matched_entry) {
5898 s->first_displayed_entry = s->matched_entry;
5899 s->selected = 0;
5902 return NULL;
5905 static const struct got_error *
5906 show_ref_view(struct tog_view *view)
5908 const struct got_error *err = NULL;
5909 struct tog_ref_view_state *s = &view->state.ref;
5910 struct tog_reflist_entry *re;
5911 char *line = NULL;
5912 wchar_t *wline;
5913 struct tog_color *tc;
5914 int width, n;
5915 int limit = view->nlines;
5917 werase(view->window);
5919 s->ndisplayed = 0;
5921 if (limit == 0)
5922 return NULL;
5924 re = s->first_displayed_entry;
5926 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5927 s->nrefs) == -1)
5928 return got_error_from_errno("asprintf");
5930 err = format_line(&wline, &width, line, view->ncols, 0);
5931 if (err) {
5932 free(line);
5933 return err;
5935 if (view_needs_focus_indication(view))
5936 wstandout(view->window);
5937 waddwstr(view->window, wline);
5938 if (view_needs_focus_indication(view))
5939 wstandend(view->window);
5940 free(wline);
5941 wline = NULL;
5942 free(line);
5943 line = NULL;
5944 if (width < view->ncols - 1)
5945 waddch(view->window, '\n');
5946 if (--limit <= 0)
5947 return NULL;
5949 n = 0;
5950 while (re && limit > 0) {
5951 char *line = NULL;
5953 if (got_ref_is_symbolic(re->ref)) {
5954 if (asprintf(&line, "%s -> %s",
5955 got_ref_get_name(re->ref),
5956 got_ref_get_symref_target(re->ref)) == -1)
5957 return got_error_from_errno("asprintf");
5958 } else if (s->show_ids) {
5959 struct got_object_id *id;
5960 char *id_str;
5961 err = got_ref_resolve(&id, s->repo, re->ref);
5962 if (err)
5963 return err;
5964 err = got_object_id_str(&id_str, id);
5965 if (err) {
5966 free(id);
5967 return err;
5969 if (asprintf(&line, "%s: %s",
5970 got_ref_get_name(re->ref), id_str) == -1) {
5971 err = got_error_from_errno("asprintf");
5972 free(id);
5973 free(id_str);
5974 return err;
5976 free(id);
5977 free(id_str);
5978 } else {
5979 line = strdup(got_ref_get_name(re->ref));
5980 if (line == NULL)
5981 return got_error_from_errno("strdup");
5984 err = format_line(&wline, &width, line, view->ncols, 0);
5985 if (err) {
5986 free(line);
5987 return err;
5989 if (n == s->selected) {
5990 if (view->focussed)
5991 wstandout(view->window);
5992 s->selected_entry = re;
5994 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5995 if (tc)
5996 wattr_on(view->window,
5997 COLOR_PAIR(tc->colorpair), NULL);
5998 waddwstr(view->window, wline);
5999 if (tc)
6000 wattr_off(view->window,
6001 COLOR_PAIR(tc->colorpair), NULL);
6002 if (width < view->ncols - 1)
6003 waddch(view->window, '\n');
6004 if (n == s->selected && view->focussed)
6005 wstandend(view->window);
6006 free(line);
6007 free(wline);
6008 wline = NULL;
6009 n++;
6010 s->ndisplayed++;
6011 s->last_displayed_entry = re;
6013 limit--;
6014 re = TAILQ_NEXT(re, entry);
6017 view_vborder(view);
6018 return err;
6021 static const struct got_error *
6022 browse_ref_tree(struct tog_view **new_view, int begin_x,
6023 struct tog_reflist_entry *re, struct got_repository *repo)
6025 const struct got_error *err = NULL;
6026 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6027 struct got_tree_object *tree = NULL;
6028 struct tog_view *tree_view;
6030 *new_view = NULL;
6032 err = resolve_reflist_entry(&commit_id, re, repo);
6033 if (err) {
6034 if (err->code != GOT_ERR_OBJ_TYPE)
6035 return err;
6036 else
6037 return NULL;
6040 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6041 if (err)
6042 goto done;
6044 err = got_object_open_as_tree(&tree, repo, tree_id);
6045 if (err)
6046 goto done;
6048 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6049 if (tree_view == NULL) {
6050 err = got_error_from_errno("view_open");
6051 goto done;
6054 err = open_tree_view(tree_view, tree, commit_id, repo);
6055 if (err)
6056 goto done;
6058 *new_view = tree_view;
6059 done:
6060 free(commit_id);
6061 free(tree_id);
6062 if (err) {
6063 if (tree)
6064 got_object_tree_close(tree);
6066 return err;
6068 static const struct got_error *
6069 input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6070 struct tog_view **focus_view, struct tog_view *view, int ch)
6072 const struct got_error *err = NULL;
6073 struct tog_ref_view_state *s = &view->state.ref;
6074 struct tog_view *log_view, *tree_view;
6075 int begin_x = 0;
6077 switch (ch) {
6078 case 'i':
6079 s->show_ids = !s->show_ids;
6080 break;
6081 case KEY_ENTER:
6082 case '\r':
6083 if (!s->selected_entry)
6084 break;
6085 if (view_is_parent_view(view))
6086 begin_x = view_split_begin_x(view->begin_x);
6087 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6088 s->repo);
6089 if (view_is_parent_view(view)) {
6090 err = view_close_child(view);
6091 if (err)
6092 return err;
6093 err = view_set_child(view, log_view);
6094 if (err) {
6095 view_close(log_view);
6096 break;
6098 *focus_view = log_view;
6099 } else
6100 *new_view = log_view;
6101 break;
6102 case 't':
6103 if (!s->selected_entry)
6104 break;
6105 if (view_is_parent_view(view))
6106 begin_x = view_split_begin_x(view->begin_x);
6107 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6108 s->repo);
6109 if (err || tree_view == NULL)
6110 break;
6111 if (view_is_parent_view(view)) {
6112 err = view_close_child(view);
6113 if (err)
6114 return err;
6115 err = view_set_child(view, tree_view);
6116 if (err) {
6117 view_close(tree_view);
6118 break;
6120 *focus_view = tree_view;
6121 } else
6122 *new_view = tree_view;
6123 break;
6124 case 'k':
6125 case KEY_UP:
6126 if (s->selected > 0) {
6127 s->selected--;
6128 break;
6130 ref_scroll_up(s, 1);
6131 break;
6132 case KEY_PPAGE:
6133 case CTRL('b'):
6134 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6135 s->selected = 0;
6136 ref_scroll_up(s, MAX(0, view->nlines - 1));
6137 break;
6138 case 'j':
6139 case KEY_DOWN:
6140 if (s->selected < s->ndisplayed - 1) {
6141 s->selected++;
6142 break;
6144 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6145 /* can't scroll any further */
6146 break;
6147 ref_scroll_down(s, 1);
6148 break;
6149 case KEY_NPAGE:
6150 case CTRL('f'):
6151 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6152 /* can't scroll any further; move cursor down */
6153 if (s->selected < s->ndisplayed - 1)
6154 s->selected = s->ndisplayed - 1;
6155 break;
6157 ref_scroll_down(s, view->nlines - 1);
6158 break;
6159 case CTRL('l'):
6160 ref_view_free_refs(s);
6161 err = ref_view_load_refs(s);
6162 break;
6163 case KEY_RESIZE:
6164 if (s->selected > view->nlines)
6165 s->selected = s->ndisplayed - 1;
6166 break;
6167 default:
6168 break;
6171 return err;
6174 __dead static void
6175 usage_ref(void)
6177 endwin();
6178 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6179 getprogname());
6180 exit(1);
6183 static const struct got_error *
6184 cmd_ref(int argc, char *argv[])
6186 const struct got_error *error;
6187 struct got_repository *repo = NULL;
6188 struct got_worktree *worktree = NULL;
6189 char *cwd = NULL, *repo_path = NULL;
6190 int ch;
6191 struct tog_view *view;
6193 #ifndef PROFILE
6194 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6195 NULL) == -1)
6196 err(1, "pledge");
6197 #endif
6199 while ((ch = getopt(argc, argv, "r:")) != -1) {
6200 switch (ch) {
6201 case 'r':
6202 repo_path = realpath(optarg, NULL);
6203 if (repo_path == NULL)
6204 return got_error_from_errno2("realpath",
6205 optarg);
6206 break;
6207 default:
6208 usage_ref();
6209 /* NOTREACHED */
6213 argc -= optind;
6214 argv += optind;
6216 if (argc > 1)
6217 usage_ref();
6219 cwd = getcwd(NULL, 0);
6220 if (cwd == NULL)
6221 return got_error_from_errno("getcwd");
6223 error = got_worktree_open(&worktree, cwd);
6224 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6225 goto done;
6227 if (repo_path == NULL) {
6228 if (worktree)
6229 repo_path =
6230 strdup(got_worktree_get_repo_path(worktree));
6231 else
6232 repo_path = strdup(cwd);
6234 if (repo_path == NULL) {
6235 error = got_error_from_errno("strdup");
6236 goto done;
6239 error = got_repo_open(&repo, repo_path, NULL);
6240 if (error != NULL)
6241 goto done;
6243 init_curses();
6245 error = apply_unveil(got_repo_get_path(repo), NULL);
6246 if (error)
6247 goto done;
6249 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6250 if (view == NULL) {
6251 error = got_error_from_errno("view_open");
6252 goto done;
6255 error = open_ref_view(view, repo);
6256 if (error)
6257 goto done;
6259 if (worktree) {
6260 /* Release work tree lock. */
6261 got_worktree_close(worktree);
6262 worktree = NULL;
6264 error = view_loop(view);
6265 done:
6266 free(repo_path);
6267 free(cwd);
6268 if (repo)
6269 got_repo_close(repo);
6270 return error;
6273 static void
6274 list_commands(FILE *fp)
6276 int i;
6278 fprintf(fp, "commands:");
6279 for (i = 0; i < nitems(tog_commands); i++) {
6280 struct tog_cmd *cmd = &tog_commands[i];
6281 fprintf(fp, " %s", cmd->name);
6283 fputc('\n', fp);
6286 __dead static void
6287 usage(int hflag, int status)
6289 FILE *fp = (status == 0) ? stdout : stderr;
6291 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6292 getprogname());
6293 if (hflag) {
6294 fprintf(fp, "lazy usage: %s path\n", getprogname());
6295 list_commands(fp);
6297 exit(status);
6300 static char **
6301 make_argv(int argc, ...)
6303 va_list ap;
6304 char **argv;
6305 int i;
6307 va_start(ap, argc);
6309 argv = calloc(argc, sizeof(char *));
6310 if (argv == NULL)
6311 err(1, "calloc");
6312 for (i = 0; i < argc; i++) {
6313 argv[i] = strdup(va_arg(ap, char *));
6314 if (argv[i] == NULL)
6315 err(1, "strdup");
6318 va_end(ap);
6319 return argv;
6323 * Try to convert 'tog path' into a 'tog log path' command.
6324 * The user could simply have mistyped the command rather than knowingly
6325 * provided a path. So check whether argv[0] can in fact be resolved
6326 * to a path in the HEAD commit and print a special error if not.
6327 * This hack is for mpi@ <3
6329 static const struct got_error *
6330 tog_log_with_path(int argc, char *argv[])
6332 const struct got_error *error = NULL;
6333 struct tog_cmd *cmd = NULL;
6334 struct got_repository *repo = NULL;
6335 struct got_worktree *worktree = NULL;
6336 struct got_object_id *commit_id = NULL, *id = NULL;
6337 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6338 char *commit_id_str = NULL, **cmd_argv = NULL;
6340 cwd = getcwd(NULL, 0);
6341 if (cwd == NULL)
6342 return got_error_from_errno("getcwd");
6344 error = got_worktree_open(&worktree, cwd);
6345 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6346 goto done;
6348 if (worktree)
6349 repo_path = strdup(got_worktree_get_repo_path(worktree));
6350 else
6351 repo_path = strdup(cwd);
6352 if (repo_path == NULL) {
6353 error = got_error_from_errno("strdup");
6354 goto done;
6357 error = got_repo_open(&repo, repo_path, NULL);
6358 if (error != NULL)
6359 goto done;
6361 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6362 repo, worktree);
6363 if (error)
6364 goto done;
6366 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6367 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6368 GOT_OBJ_TYPE_COMMIT, 1, repo);
6369 if (error)
6370 goto done;
6372 if (worktree) {
6373 got_worktree_close(worktree);
6374 worktree = NULL;
6377 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6378 if (error) {
6379 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6380 goto done;
6381 fprintf(stderr, "%s: '%s' is no known command or path\n",
6382 getprogname(), argv[0]);
6383 usage(1, 1);
6384 /* not reached */
6387 got_repo_close(repo);
6388 repo = NULL;
6390 error = got_object_id_str(&commit_id_str, commit_id);
6391 if (error)
6392 goto done;
6394 cmd = &tog_commands[0]; /* log */
6395 argc = 4;
6396 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6397 error = cmd->cmd_main(argc, cmd_argv);
6398 done:
6399 if (repo)
6400 got_repo_close(repo);
6401 if (worktree)
6402 got_worktree_close(worktree);
6403 free(id);
6404 free(commit_id_str);
6405 free(commit_id);
6406 free(cwd);
6407 free(repo_path);
6408 free(in_repo_path);
6409 if (cmd_argv) {
6410 int i;
6411 for (i = 0; i < argc; i++)
6412 free(cmd_argv[i]);
6413 free(cmd_argv);
6415 return error;
6418 int
6419 main(int argc, char *argv[])
6421 const struct got_error *error = NULL;
6422 struct tog_cmd *cmd = NULL;
6423 int ch, hflag = 0, Vflag = 0;
6424 char **cmd_argv = NULL;
6425 static struct option longopts[] = {
6426 { "version", no_argument, NULL, 'V' },
6427 { NULL, 0, NULL, 0}
6430 setlocale(LC_CTYPE, "");
6432 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6433 switch (ch) {
6434 case 'h':
6435 hflag = 1;
6436 break;
6437 case 'V':
6438 Vflag = 1;
6439 break;
6440 default:
6441 usage(hflag, 1);
6442 /* NOTREACHED */
6446 argc -= optind;
6447 argv += optind;
6448 optind = 1;
6449 optreset = 1;
6451 if (Vflag) {
6452 got_version_print_str();
6453 return 0;
6456 if (argc == 0) {
6457 if (hflag)
6458 usage(hflag, 0);
6459 /* Build an argument vector which runs a default command. */
6460 cmd = &tog_commands[0];
6461 argc = 1;
6462 cmd_argv = make_argv(argc, cmd->name);
6463 } else {
6464 int i;
6466 /* Did the user specify a command? */
6467 for (i = 0; i < nitems(tog_commands); i++) {
6468 if (strncmp(tog_commands[i].name, argv[0],
6469 strlen(argv[0])) == 0) {
6470 cmd = &tog_commands[i];
6471 break;
6476 if (cmd == NULL) {
6477 if (argc != 1)
6478 usage(0, 1);
6479 /* No command specified; try log with a path */
6480 error = tog_log_with_path(argc, argv);
6481 } else {
6482 if (hflag)
6483 cmd->cmd_usage();
6484 else
6485 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6488 endwin();
6489 putchar('\n');
6490 if (cmd_argv) {
6491 int i;
6492 for (i = 0; i < argc; i++)
6493 free(cmd_argv[i]);
6494 free(cmd_argv);
6497 if (error && error->code != GOT_ERR_CANCELLED)
6498 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6499 return 0;