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 int dying;
451 struct tog_view *parent;
452 struct tog_view *child;
454 /* type-specific state */
455 enum tog_view_type type;
456 union {
457 struct tog_diff_view_state diff;
458 struct tog_log_view_state log;
459 struct tog_blame_view_state blame;
460 struct tog_tree_view_state tree;
461 struct tog_ref_view_state ref;
462 } state;
464 const struct got_error *(*show)(struct tog_view *);
465 const struct got_error *(*input)(struct tog_view **,
466 struct tog_view **, struct tog_view *, int);
467 const struct got_error *(*close)(struct tog_view *);
469 const struct got_error *(*search_start)(struct tog_view *);
470 const struct got_error *(*search_next)(struct tog_view *);
471 int searching;
472 #define TOG_SEARCH_FORWARD 1
473 #define TOG_SEARCH_BACKWARD 2
474 int search_next_done;
475 #define TOG_SEARCH_HAVE_MORE 1
476 #define TOG_SEARCH_NO_MORE 2
477 #define TOG_SEARCH_HAVE_NONE 3
478 regex_t regex;
479 regmatch_t regmatch;
480 };
482 static const struct got_error *open_diff_view(struct tog_view *,
483 struct got_object_id *, struct got_object_id *,
484 const char *, const char *, int, int, int, struct tog_view *,
485 struct got_repository *);
486 static const struct got_error *show_diff_view(struct tog_view *);
487 static const struct got_error *input_diff_view(struct tog_view **,
488 struct tog_view **, struct tog_view *, int);
489 static const struct got_error* close_diff_view(struct tog_view *);
490 static const struct got_error *search_start_diff_view(struct tog_view *);
491 static const struct got_error *search_next_diff_view(struct tog_view *);
493 static const struct got_error *open_log_view(struct tog_view *,
494 struct got_object_id *, struct got_repository *,
495 const char *, const char *, int);
496 static const struct got_error * show_log_view(struct tog_view *);
497 static const struct got_error *input_log_view(struct tog_view **,
498 struct tog_view **, struct tog_view *, int);
499 static const struct got_error *close_log_view(struct tog_view *);
500 static const struct got_error *search_start_log_view(struct tog_view *);
501 static const struct got_error *search_next_log_view(struct tog_view *);
503 static const struct got_error *open_blame_view(struct tog_view *, char *,
504 struct got_object_id *, struct got_repository *);
505 static const struct got_error *show_blame_view(struct tog_view *);
506 static const struct got_error *input_blame_view(struct tog_view **,
507 struct tog_view **, struct tog_view *, int);
508 static const struct got_error *close_blame_view(struct tog_view *);
509 static const struct got_error *search_start_blame_view(struct tog_view *);
510 static const struct got_error *search_next_blame_view(struct tog_view *);
512 static const struct got_error *open_tree_view(struct tog_view *,
513 struct got_tree_object *, struct got_object_id *, struct got_repository *);
514 static const struct got_error *show_tree_view(struct tog_view *);
515 static const struct got_error *input_tree_view(struct tog_view **,
516 struct tog_view **, struct tog_view *, int);
517 static const struct got_error *close_tree_view(struct tog_view *);
518 static const struct got_error *search_start_tree_view(struct tog_view *);
519 static const struct got_error *search_next_tree_view(struct tog_view *);
521 static const struct got_error *open_ref_view(struct tog_view *,
522 struct got_repository *);
523 static const struct got_error *show_ref_view(struct tog_view *);
524 static const struct got_error *input_ref_view(struct tog_view **,
525 struct tog_view **, struct tog_view *, int);
526 static const struct got_error *close_ref_view(struct tog_view *);
527 static const struct got_error *search_start_ref_view(struct tog_view *);
528 static const struct got_error *search_next_ref_view(struct tog_view *);
530 static volatile sig_atomic_t tog_sigwinch_received;
531 static volatile sig_atomic_t tog_sigpipe_received;
532 static volatile sig_atomic_t tog_sigcont_received;
534 static void
535 tog_sigwinch(int signo)
537 tog_sigwinch_received = 1;
540 static void
541 tog_sigpipe(int signo)
543 tog_sigpipe_received = 1;
546 static void
547 tog_sigcont(int signo)
549 tog_sigcont_received = 1;
552 static const struct got_error *
553 view_close(struct tog_view *view)
555 const struct got_error *err = NULL;
557 if (view->child) {
558 view_close(view->child);
559 view->child = NULL;
561 if (view->close)
562 err = view->close(view);
563 if (view->panel)
564 del_panel(view->panel);
565 if (view->window)
566 delwin(view->window);
567 free(view);
568 return err;
571 static struct tog_view *
572 view_open(int nlines, int ncols, int begin_y, int begin_x,
573 enum tog_view_type type)
575 struct tog_view *view = calloc(1, sizeof(*view));
577 if (view == NULL)
578 return NULL;
580 view->type = type;
581 view->lines = LINES;
582 view->cols = COLS;
583 view->nlines = nlines ? nlines : LINES - begin_y;
584 view->ncols = ncols ? ncols : COLS - begin_x;
585 view->begin_y = begin_y;
586 view->begin_x = begin_x;
587 view->window = newwin(nlines, ncols, begin_y, begin_x);
588 if (view->window == NULL) {
589 view_close(view);
590 return NULL;
592 view->panel = new_panel(view->window);
593 if (view->panel == NULL ||
594 set_panel_userptr(view->panel, view) != OK) {
595 view_close(view);
596 return NULL;
599 keypad(view->window, TRUE);
600 return view;
603 static int
604 view_split_begin_x(int begin_x)
606 if (begin_x > 0 || COLS < 120)
607 return 0;
608 return (COLS - MAX(COLS / 2, 80));
611 static const struct got_error *view_resize(struct tog_view *);
613 static const struct got_error *
614 view_splitscreen(struct tog_view *view)
616 const struct got_error *err = NULL;
618 view->begin_y = 0;
619 view->begin_x = view_split_begin_x(0);
620 view->nlines = LINES;
621 view->ncols = COLS - view->begin_x;
622 view->lines = LINES;
623 view->cols = COLS;
624 err = view_resize(view);
625 if (err)
626 return err;
628 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
629 return got_error_from_errno("mvwin");
631 return NULL;
634 static const struct got_error *
635 view_fullscreen(struct tog_view *view)
637 const struct got_error *err = NULL;
639 view->begin_x = 0;
640 view->begin_y = 0;
641 view->nlines = LINES;
642 view->ncols = COLS;
643 view->lines = LINES;
644 view->cols = COLS;
645 err = view_resize(view);
646 if (err)
647 return err;
649 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
650 return got_error_from_errno("mvwin");
652 return NULL;
655 static int
656 view_is_parent_view(struct tog_view *view)
658 return view->parent == NULL;
661 static const struct got_error *
662 view_resize(struct tog_view *view)
664 int nlines, ncols;
666 if (view->lines > LINES)
667 nlines = view->nlines - (view->lines - LINES);
668 else
669 nlines = view->nlines + (LINES - view->lines);
671 if (view->cols > COLS)
672 ncols = view->ncols - (view->cols - COLS);
673 else
674 ncols = view->ncols + (COLS - view->cols);
676 if (wresize(view->window, nlines, ncols) == ERR)
677 return got_error_from_errno("wresize");
678 if (replace_panel(view->panel, view->window) == ERR)
679 return got_error_from_errno("replace_panel");
680 wclear(view->window);
682 view->nlines = nlines;
683 view->ncols = ncols;
684 view->lines = LINES;
685 view->cols = COLS;
687 if (view->child) {
688 view->child->begin_x = view_split_begin_x(view->begin_x);
689 if (view->child->begin_x == 0) {
690 view_fullscreen(view->child);
691 if (view->child->focussed)
692 show_panel(view->child->panel);
693 else
694 show_panel(view->panel);
695 } else {
696 view_splitscreen(view->child);
697 show_panel(view->child->panel);
701 return NULL;
704 static const struct got_error *
705 view_close_child(struct tog_view *view)
707 const struct got_error *err = NULL;
709 if (view->child == NULL)
710 return NULL;
712 err = view_close(view->child);
713 view->child = NULL;
714 return err;
717 static void
718 view_set_child(struct tog_view *view, struct tog_view *child)
720 view->child = child;
721 child->parent = view;
724 static int
725 view_is_splitscreen(struct tog_view *view)
727 return view->begin_x > 0;
730 static void
731 tog_resizeterm(void)
733 int cols, lines;
734 struct winsize size;
736 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
737 cols = 80; /* Default */
738 lines = 24;
739 } else {
740 cols = size.ws_col;
741 lines = size.ws_row;
743 resize_term(lines, cols);
746 static const struct got_error *
747 view_search_start(struct tog_view *view)
749 const struct got_error *err = NULL;
750 char pattern[1024];
751 int ret;
753 if (view->nlines < 1)
754 return NULL;
756 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
757 wclrtoeol(view->window);
759 nocbreak();
760 echo();
761 ret = wgetnstr(view->window, pattern, sizeof(pattern));
762 cbreak();
763 noecho();
764 if (ret == ERR)
765 return NULL;
767 if (view->searching) {
768 regfree(&view->regex);
769 view->searching = 0;
772 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
773 err = view->search_start(view);
774 if (err) {
775 regfree(&view->regex);
776 return err;
778 view->searching = TOG_SEARCH_FORWARD;
779 view->search_next_done = 0;
780 view->search_next(view);
783 return NULL;
786 static const struct got_error *
787 view_input(struct tog_view **new, struct tog_view **focus, int *done,
788 struct tog_view *view, struct tog_view_list_head *views)
790 const struct got_error *err = NULL;
791 struct tog_view *v;
792 int ch, errcode;
794 *new = NULL;
795 *focus = NULL;
797 /* Clear "no matches" indicator. */
798 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
799 view->search_next_done == TOG_SEARCH_HAVE_NONE)
800 view->search_next_done = TOG_SEARCH_HAVE_MORE;
802 if (view->searching && !view->search_next_done) {
803 errcode = pthread_mutex_unlock(&tog_mutex);
804 if (errcode)
805 return got_error_set_errno(errcode,
806 "pthread_mutex_unlock");
807 pthread_yield();
808 errcode = pthread_mutex_lock(&tog_mutex);
809 if (errcode)
810 return got_error_set_errno(errcode,
811 "pthread_mutex_lock");
812 view->search_next(view);
813 return NULL;
816 nodelay(stdscr, FALSE);
817 /* Allow threads to make progress while we are waiting for input. */
818 errcode = pthread_mutex_unlock(&tog_mutex);
819 if (errcode)
820 return got_error_set_errno(errcode, "pthread_mutex_unlock");
821 ch = wgetch(view->window);
822 errcode = pthread_mutex_lock(&tog_mutex);
823 if (errcode)
824 return got_error_set_errno(errcode, "pthread_mutex_lock");
825 nodelay(stdscr, TRUE);
827 if (tog_sigwinch_received || tog_sigcont_received) {
828 tog_resizeterm();
829 tog_sigwinch_received = 0;
830 tog_sigcont_received = 0;
831 TAILQ_FOREACH(v, views, entry) {
832 err = view_resize(v);
833 if (err)
834 return err;
835 err = v->input(new, focus, v, KEY_RESIZE);
836 if (err)
837 return err;
841 switch (ch) {
842 case ERR:
843 break;
844 case '\t':
845 if (view->child) {
846 *focus = view->child;
847 } else if (view->parent) {
848 *focus = view->parent;
850 break;
851 case 'q':
852 err = view->input(new, focus, view, ch);
853 view->dying = 1;
854 break;
855 case 'Q':
856 *done = 1;
857 break;
858 case 'f':
859 if (view_is_parent_view(view)) {
860 if (view->child == NULL)
861 break;
862 if (view_is_splitscreen(view->child)) {
863 *focus = view->child;
864 err = view_fullscreen(view->child);
865 } else
866 err = view_splitscreen(view->child);
867 if (err)
868 break;
869 err = view->child->input(new, focus, view->child,
870 KEY_RESIZE);
871 } else {
872 if (view_is_splitscreen(view)) {
873 *focus = view;
874 err = view_fullscreen(view);
875 } else {
876 err = view_splitscreen(view);
878 if (err)
879 break;
880 err = view->input(new, focus, view, KEY_RESIZE);
882 break;
883 case KEY_RESIZE:
884 break;
885 case '/':
886 if (view->search_start)
887 view_search_start(view);
888 else
889 err = view->input(new, focus, view, ch);
890 break;
891 case 'N':
892 case 'n':
893 if (view->search_next) {
894 view->searching = (ch == 'n' ?
895 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
896 view->search_next_done = 0;
897 view->search_next(view);
898 } else
899 err = view->input(new, focus, view, ch);
900 break;
901 default:
902 err = view->input(new, focus, view, ch);
903 break;
906 return err;
909 void
910 view_vborder(struct tog_view *view)
912 PANEL *panel;
913 struct tog_view *view_above;
915 if (view->parent)
916 return view_vborder(view->parent);
918 panel = panel_above(view->panel);
919 if (panel == NULL)
920 return;
922 view_above = panel_userptr(panel);
923 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
924 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
927 int
928 view_needs_focus_indication(struct tog_view *view)
930 if (view_is_parent_view(view)) {
931 if (view->child == NULL || view->child->focussed)
932 return 0;
933 if (!view_is_splitscreen(view->child))
934 return 0;
935 } else if (!view_is_splitscreen(view))
936 return 0;
938 return view->focussed;
941 static const struct got_error *
942 view_loop(struct tog_view *view)
944 const struct got_error *err = NULL;
945 struct tog_view_list_head views;
946 struct tog_view *new_view, *focus_view, *main_view;
947 int fast_refresh = 10;
948 int done = 0, errcode;
950 errcode = pthread_mutex_lock(&tog_mutex);
951 if (errcode)
952 return got_error_set_errno(errcode, "pthread_mutex_lock");
954 TAILQ_INIT(&views);
955 TAILQ_INSERT_HEAD(&views, view, entry);
957 main_view = view;
958 view->focussed = 1;
959 err = view->show(view);
960 if (err)
961 return err;
962 update_panels();
963 doupdate();
964 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
965 /* Refresh fast during initialization, then become slower. */
966 if (fast_refresh && fast_refresh-- == 0)
967 halfdelay(10); /* switch to once per second */
969 err = view_input(&new_view, &focus_view, &done, view, &views);
970 if (err)
971 break;
972 if (view->dying) {
973 struct tog_view *prev = NULL;
975 if (view_is_parent_view(view))
976 prev = TAILQ_PREV(view, tog_view_list_head,
977 entry);
978 else if (view->parent != view)
979 prev = view->parent;
981 if (view->parent)
982 view->parent->child = NULL;
983 else
984 TAILQ_REMOVE(&views, view, entry);
986 err = view_close(view);
987 if (err || (view == main_view && new_view == NULL))
988 goto done;
990 if (focus_view)
991 view = focus_view;
992 else if (prev)
993 view = prev;
994 else if (!TAILQ_EMPTY(&views))
995 view = TAILQ_LAST(&views,
996 tog_view_list_head);
997 else
998 view = NULL;
999 if (view) {
1000 if (view->child &&
1001 view->child->focussed)
1002 focus_view = view->child;
1003 else
1004 focus_view = view;
1007 if (new_view) {
1008 struct tog_view *v, *t;
1009 /* Only allow one parent view per type. */
1010 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1011 if (v->type != new_view->type)
1012 continue;
1013 TAILQ_REMOVE(&views, v, entry);
1014 err = view_close(v);
1015 if (err)
1016 goto done;
1017 break;
1019 TAILQ_INSERT_TAIL(&views, new_view, entry);
1020 view = new_view;
1021 if (focus_view == NULL)
1022 focus_view = new_view;
1024 if (focus_view) {
1025 show_panel(focus_view->panel);
1026 if (view)
1027 view->focussed = 0;
1028 focus_view->focussed = 1;
1029 view = focus_view;
1030 if (new_view)
1031 show_panel(new_view->panel);
1032 if (view->child && view_is_splitscreen(view->child))
1033 show_panel(view->child->panel);
1035 if (view) {
1036 if (focus_view == NULL) {
1037 view->focussed = 1;
1038 show_panel(view->panel);
1039 if (view->child && view_is_splitscreen(view->child))
1040 show_panel(view->child->panel);
1041 focus_view = view;
1043 if (view->parent) {
1044 err = view->parent->show(view->parent);
1045 if (err)
1046 goto done;
1048 err = view->show(view);
1049 if (err)
1050 goto done;
1051 if (view->child) {
1052 err = view->child->show(view->child);
1053 if (err)
1054 goto done;
1056 update_panels();
1057 doupdate();
1060 done:
1061 while (!TAILQ_EMPTY(&views)) {
1062 view = TAILQ_FIRST(&views);
1063 TAILQ_REMOVE(&views, view, entry);
1064 view_close(view);
1067 errcode = pthread_mutex_unlock(&tog_mutex);
1068 if (errcode && err == NULL)
1069 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1071 return err;
1074 __dead static void
1075 usage_log(void)
1077 endwin();
1078 fprintf(stderr,
1079 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1080 getprogname());
1081 exit(1);
1084 /* Create newly allocated wide-character string equivalent to a byte string. */
1085 static const struct got_error *
1086 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1088 char *vis = NULL;
1089 const struct got_error *err = NULL;
1091 *ws = NULL;
1092 *wlen = mbstowcs(NULL, s, 0);
1093 if (*wlen == (size_t)-1) {
1094 int vislen;
1095 if (errno != EILSEQ)
1096 return got_error_from_errno("mbstowcs");
1098 /* byte string invalid in current encoding; try to "fix" it */
1099 err = got_mbsavis(&vis, &vislen, s);
1100 if (err)
1101 return err;
1102 *wlen = mbstowcs(NULL, vis, 0);
1103 if (*wlen == (size_t)-1) {
1104 err = got_error_from_errno("mbstowcs"); /* give up */
1105 goto done;
1109 *ws = calloc(*wlen + 1, sizeof(**ws));
1110 if (*ws == NULL) {
1111 err = got_error_from_errno("calloc");
1112 goto done;
1115 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1116 err = got_error_from_errno("mbstowcs");
1117 done:
1118 free(vis);
1119 if (err) {
1120 free(*ws);
1121 *ws = NULL;
1122 *wlen = 0;
1124 return err;
1127 /* Format a line for display, ensuring that it won't overflow a width limit. */
1128 static const struct got_error *
1129 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1130 int col_tab_align)
1132 const struct got_error *err = NULL;
1133 int cols = 0;
1134 wchar_t *wline = NULL;
1135 size_t wlen;
1136 int i;
1138 *wlinep = NULL;
1139 *widthp = 0;
1141 err = mbs2ws(&wline, &wlen, line);
1142 if (err)
1143 return err;
1145 i = 0;
1146 while (i < wlen) {
1147 int width = wcwidth(wline[i]);
1149 if (width == 0) {
1150 i++;
1151 continue;
1154 if (width == 1 || width == 2) {
1155 if (cols + width > wlimit)
1156 break;
1157 cols += width;
1158 i++;
1159 } else if (width == -1) {
1160 if (wline[i] == L'\t') {
1161 width = TABSIZE -
1162 ((cols + col_tab_align) % TABSIZE);
1163 if (cols + width > wlimit)
1164 break;
1165 cols += width;
1167 i++;
1168 } else {
1169 err = got_error_from_errno("wcwidth");
1170 goto done;
1173 wline[i] = L'\0';
1174 if (widthp)
1175 *widthp = cols;
1176 done:
1177 if (err)
1178 free(wline);
1179 else
1180 *wlinep = wline;
1181 return err;
1184 static const struct got_error*
1185 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1186 struct got_object_id *id, struct got_repository *repo)
1188 static const struct got_error *err = NULL;
1189 struct got_reflist_entry *re;
1190 char *s;
1191 const char *name;
1193 *refs_str = NULL;
1195 SIMPLEQ_FOREACH(re, refs, entry) {
1196 struct got_tag_object *tag = NULL;
1197 struct got_object_id *ref_id;
1198 int cmp;
1200 name = got_ref_get_name(re->ref);
1201 if (strcmp(name, GOT_REF_HEAD) == 0)
1202 continue;
1203 if (strncmp(name, "refs/", 5) == 0)
1204 name += 5;
1205 if (strncmp(name, "got/", 4) == 0)
1206 continue;
1207 if (strncmp(name, "heads/", 6) == 0)
1208 name += 6;
1209 if (strncmp(name, "remotes/", 8) == 0) {
1210 name += 8;
1211 s = strstr(name, "/" GOT_REF_HEAD);
1212 if (s != NULL && s[strlen(s)] == '\0')
1213 continue;
1215 err = got_ref_resolve(&ref_id, repo, re->ref);
1216 if (err)
1217 break;
1218 if (strncmp(name, "tags/", 5) == 0) {
1219 err = got_object_open_as_tag(&tag, repo, ref_id);
1220 if (err) {
1221 if (err->code != GOT_ERR_OBJ_TYPE) {
1222 free(ref_id);
1223 break;
1225 /* Ref points at something other than a tag. */
1226 err = NULL;
1227 tag = NULL;
1230 cmp = got_object_id_cmp(tag ?
1231 got_object_tag_get_object_id(tag) : ref_id, id);
1232 free(ref_id);
1233 if (tag)
1234 got_object_tag_close(tag);
1235 if (cmp != 0)
1236 continue;
1237 s = *refs_str;
1238 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1239 s ? ", " : "", name) == -1) {
1240 err = got_error_from_errno("asprintf");
1241 free(s);
1242 *refs_str = NULL;
1243 break;
1245 free(s);
1248 return err;
1251 static const struct got_error *
1252 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1253 int col_tab_align)
1255 char *smallerthan, *at;
1257 smallerthan = strchr(author, '<');
1258 if (smallerthan && smallerthan[1] != '\0')
1259 author = smallerthan + 1;
1260 at = strchr(author, '@');
1261 if (at)
1262 *at = '\0';
1263 return format_line(wauthor, author_width, author, limit, col_tab_align);
1266 static const struct got_error *
1267 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1268 struct got_object_id *id, const size_t date_display_cols,
1269 int author_display_cols)
1271 struct tog_log_view_state *s = &view->state.log;
1272 const struct got_error *err = NULL;
1273 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1274 char *logmsg0 = NULL, *logmsg = NULL;
1275 char *author = NULL;
1276 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1277 int author_width, logmsg_width;
1278 char *newline, *line = NULL;
1279 int col, limit;
1280 const int avail = view->ncols;
1281 struct tm tm;
1282 time_t committer_time;
1283 struct tog_color *tc;
1285 committer_time = got_object_commit_get_committer_time(commit);
1286 if (localtime_r(&committer_time, &tm) == NULL)
1287 return got_error_from_errno("localtime_r");
1288 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1289 >= sizeof(datebuf))
1290 return got_error(GOT_ERR_NO_SPACE);
1292 if (avail <= date_display_cols)
1293 limit = MIN(sizeof(datebuf) - 1, avail);
1294 else
1295 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1296 tc = get_color(&s->colors, TOG_COLOR_DATE);
1297 if (tc)
1298 wattr_on(view->window,
1299 COLOR_PAIR(tc->colorpair), NULL);
1300 waddnstr(view->window, datebuf, limit);
1301 if (tc)
1302 wattr_off(view->window,
1303 COLOR_PAIR(tc->colorpair), NULL);
1304 col = limit;
1305 if (col > avail)
1306 goto done;
1308 if (avail >= 120) {
1309 char *id_str;
1310 err = got_object_id_str(&id_str, id);
1311 if (err)
1312 goto done;
1313 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1314 if (tc)
1315 wattr_on(view->window,
1316 COLOR_PAIR(tc->colorpair), NULL);
1317 wprintw(view->window, "%.8s ", id_str);
1318 if (tc)
1319 wattr_off(view->window,
1320 COLOR_PAIR(tc->colorpair), NULL);
1321 free(id_str);
1322 col += 9;
1323 if (col > avail)
1324 goto done;
1327 author = strdup(got_object_commit_get_author(commit));
1328 if (author == NULL) {
1329 err = got_error_from_errno("strdup");
1330 goto done;
1332 err = format_author(&wauthor, &author_width, author, avail - col, col);
1333 if (err)
1334 goto done;
1335 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1336 if (tc)
1337 wattr_on(view->window,
1338 COLOR_PAIR(tc->colorpair), NULL);
1339 waddwstr(view->window, wauthor);
1340 if (tc)
1341 wattr_off(view->window,
1342 COLOR_PAIR(tc->colorpair), NULL);
1343 col += author_width;
1344 while (col < avail && author_width < author_display_cols + 2) {
1345 waddch(view->window, ' ');
1346 col++;
1347 author_width++;
1349 if (col > avail)
1350 goto done;
1352 err = got_object_commit_get_logmsg(&logmsg0, commit);
1353 if (err)
1354 goto done;
1355 logmsg = logmsg0;
1356 while (*logmsg == '\n')
1357 logmsg++;
1358 newline = strchr(logmsg, '\n');
1359 if (newline)
1360 *newline = '\0';
1361 limit = avail - col;
1362 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1363 if (err)
1364 goto done;
1365 waddwstr(view->window, wlogmsg);
1366 col += logmsg_width;
1367 while (col < avail) {
1368 waddch(view->window, ' ');
1369 col++;
1371 done:
1372 free(logmsg0);
1373 free(wlogmsg);
1374 free(author);
1375 free(wauthor);
1376 free(line);
1377 return err;
1380 static struct commit_queue_entry *
1381 alloc_commit_queue_entry(struct got_commit_object *commit,
1382 struct got_object_id *id)
1384 struct commit_queue_entry *entry;
1386 entry = calloc(1, sizeof(*entry));
1387 if (entry == NULL)
1388 return NULL;
1390 entry->id = id;
1391 entry->commit = commit;
1392 return entry;
1395 static void
1396 pop_commit(struct commit_queue *commits)
1398 struct commit_queue_entry *entry;
1400 entry = TAILQ_FIRST(&commits->head);
1401 TAILQ_REMOVE(&commits->head, entry, entry);
1402 got_object_commit_close(entry->commit);
1403 commits->ncommits--;
1404 /* Don't free entry->id! It is owned by the commit graph. */
1405 free(entry);
1408 static void
1409 free_commits(struct commit_queue *commits)
1411 while (!TAILQ_EMPTY(&commits->head))
1412 pop_commit(commits);
1415 static const struct got_error *
1416 match_commit(int *have_match, struct got_object_id *id,
1417 struct got_commit_object *commit, regex_t *regex)
1419 const struct got_error *err = NULL;
1420 regmatch_t regmatch;
1421 char *id_str = NULL, *logmsg = NULL;
1423 *have_match = 0;
1425 err = got_object_id_str(&id_str, id);
1426 if (err)
1427 return err;
1429 err = got_object_commit_get_logmsg(&logmsg, commit);
1430 if (err)
1431 goto done;
1433 if (regexec(regex, got_object_commit_get_author(commit), 1,
1434 &regmatch, 0) == 0 ||
1435 regexec(regex, got_object_commit_get_committer(commit), 1,
1436 &regmatch, 0) == 0 ||
1437 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1438 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1439 *have_match = 1;
1440 done:
1441 free(id_str);
1442 free(logmsg);
1443 return err;
1446 static const struct got_error *
1447 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1448 int minqueue, struct got_repository *repo, const char *path,
1449 int *searching, int *search_next_done, regex_t *regex)
1451 const struct got_error *err = NULL;
1452 int nqueued = 0;
1455 * We keep all commits open throughout the lifetime of the log
1456 * view in order to avoid having to re-fetch commits from disk
1457 * while updating the display.
1459 while (nqueued < minqueue ||
1460 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1461 struct got_object_id *id;
1462 struct got_commit_object *commit;
1463 struct commit_queue_entry *entry;
1464 int errcode;
1466 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1467 if (err || id == NULL)
1468 break;
1470 err = got_object_open_as_commit(&commit, repo, id);
1471 if (err)
1472 break;
1473 entry = alloc_commit_queue_entry(commit, id);
1474 if (entry == NULL) {
1475 err = got_error_from_errno("alloc_commit_queue_entry");
1476 break;
1479 errcode = pthread_mutex_lock(&tog_mutex);
1480 if (errcode) {
1481 err = got_error_set_errno(errcode,
1482 "pthread_mutex_lock");
1483 break;
1486 entry->idx = commits->ncommits;
1487 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1488 nqueued++;
1489 commits->ncommits++;
1491 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1492 int have_match;
1493 err = match_commit(&have_match, id, commit, regex);
1494 if (err)
1495 break;
1496 if (have_match)
1497 *search_next_done = TOG_SEARCH_HAVE_MORE;
1500 errcode = pthread_mutex_unlock(&tog_mutex);
1501 if (errcode && err == NULL)
1502 err = got_error_set_errno(errcode,
1503 "pthread_mutex_unlock");
1504 if (err)
1505 break;
1508 return err;
1511 static const struct got_error *
1512 draw_commits(struct tog_view *view)
1514 const struct got_error *err = NULL;
1515 struct tog_log_view_state *s = &view->state.log;
1516 struct commit_queue_entry *entry;
1517 const int limit = view->nlines;
1518 int width;
1519 int ncommits, author_cols = 4;
1520 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1521 char *refs_str = NULL;
1522 wchar_t *wline;
1523 struct tog_color *tc;
1524 static const size_t date_display_cols = 12;
1526 entry = s->first_displayed_entry;
1527 ncommits = 0;
1528 while (entry) {
1529 if (ncommits == s->selected) {
1530 s->selected_entry = entry;
1531 break;
1533 entry = TAILQ_NEXT(entry, entry);
1534 ncommits++;
1537 if (s->selected_entry &&
1538 !(view->searching && view->search_next_done == 0)) {
1539 err = got_object_id_str(&id_str, s->selected_entry->id);
1540 if (err)
1541 return err;
1542 err = build_refs_str(&refs_str, &s->refs,
1543 s->selected_entry->id, s->repo);
1544 if (err)
1545 goto done;
1548 if (s->thread_args.commits_needed == 0)
1549 halfdelay(10); /* disable fast refresh */
1551 if (s->thread_args.commits_needed > 0) {
1552 if (asprintf(&ncommits_str, " [%d/%d] %s",
1553 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1554 (view->searching && !view->search_next_done) ?
1555 "searching..." : "loading...") == -1) {
1556 err = got_error_from_errno("asprintf");
1557 goto done;
1559 } else {
1560 const char *search_str = NULL;
1562 if (view->searching) {
1563 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1564 search_str = "no more matches";
1565 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1566 search_str = "no matches found";
1567 else if (!view->search_next_done)
1568 search_str = "searching...";
1571 if (asprintf(&ncommits_str, " [%d/%d] %s",
1572 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1573 search_str ? search_str :
1574 (refs_str ? refs_str : "")) == -1) {
1575 err = got_error_from_errno("asprintf");
1576 goto done;
1580 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1581 if (asprintf(&header, "commit %s %s%s",
1582 id_str ? id_str : "........................................",
1583 s->in_repo_path, ncommits_str) == -1) {
1584 err = got_error_from_errno("asprintf");
1585 header = NULL;
1586 goto done;
1588 } else if (asprintf(&header, "commit %s%s",
1589 id_str ? id_str : "........................................",
1590 ncommits_str) == -1) {
1591 err = got_error_from_errno("asprintf");
1592 header = NULL;
1593 goto done;
1595 err = format_line(&wline, &width, header, view->ncols, 0);
1596 if (err)
1597 goto done;
1599 werase(view->window);
1601 if (view_needs_focus_indication(view))
1602 wstandout(view->window);
1603 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1604 if (tc)
1605 wattr_on(view->window,
1606 COLOR_PAIR(tc->colorpair), NULL);
1607 waddwstr(view->window, wline);
1608 if (tc)
1609 wattr_off(view->window,
1610 COLOR_PAIR(tc->colorpair), NULL);
1611 while (width < view->ncols) {
1612 waddch(view->window, ' ');
1613 width++;
1615 if (view_needs_focus_indication(view))
1616 wstandend(view->window);
1617 free(wline);
1618 if (limit <= 1)
1619 goto done;
1621 /* Grow author column size if necessary. */
1622 entry = s->first_displayed_entry;
1623 ncommits = 0;
1624 while (entry) {
1625 char *author;
1626 wchar_t *wauthor;
1627 int width;
1628 if (ncommits >= limit - 1)
1629 break;
1630 author = strdup(got_object_commit_get_author(entry->commit));
1631 if (author == NULL) {
1632 err = got_error_from_errno("strdup");
1633 goto done;
1635 err = format_author(&wauthor, &width, author, COLS,
1636 date_display_cols);
1637 if (author_cols < width)
1638 author_cols = width;
1639 free(wauthor);
1640 free(author);
1641 ncommits++;
1642 entry = TAILQ_NEXT(entry, entry);
1645 entry = s->first_displayed_entry;
1646 s->last_displayed_entry = s->first_displayed_entry;
1647 ncommits = 0;
1648 while (entry) {
1649 if (ncommits >= limit - 1)
1650 break;
1651 if (ncommits == s->selected)
1652 wstandout(view->window);
1653 err = draw_commit(view, entry->commit, entry->id,
1654 date_display_cols, author_cols);
1655 if (ncommits == s->selected)
1656 wstandend(view->window);
1657 if (err)
1658 goto done;
1659 ncommits++;
1660 s->last_displayed_entry = entry;
1661 entry = TAILQ_NEXT(entry, entry);
1664 view_vborder(view);
1665 done:
1666 free(id_str);
1667 free(refs_str);
1668 free(ncommits_str);
1669 free(header);
1670 return err;
1673 static void
1674 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1676 struct commit_queue_entry *entry;
1677 int nscrolled = 0;
1679 entry = TAILQ_FIRST(&s->commits.head);
1680 if (s->first_displayed_entry == entry)
1681 return;
1683 entry = s->first_displayed_entry;
1684 while (entry && nscrolled < maxscroll) {
1685 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1686 if (entry) {
1687 s->first_displayed_entry = entry;
1688 nscrolled++;
1693 static const struct got_error *
1694 trigger_log_thread(struct tog_view *view, int wait)
1696 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1697 int errcode;
1699 halfdelay(1); /* fast refresh while loading commits */
1701 while (ta->commits_needed > 0) {
1702 if (ta->log_complete)
1703 break;
1705 /* Wake the log thread. */
1706 errcode = pthread_cond_signal(&ta->need_commits);
1707 if (errcode)
1708 return got_error_set_errno(errcode,
1709 "pthread_cond_signal");
1712 * The mutex will be released while the view loop waits
1713 * in wgetch(), at which time the log thread will run.
1715 if (!wait)
1716 break;
1718 /* Display progress update in log view. */
1719 show_log_view(view);
1720 update_panels();
1721 doupdate();
1723 /* Wait right here while next commit is being loaded. */
1724 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1725 if (errcode)
1726 return got_error_set_errno(errcode,
1727 "pthread_cond_wait");
1729 /* Display progress update in log view. */
1730 show_log_view(view);
1731 update_panels();
1732 doupdate();
1735 return NULL;
1738 static const struct got_error *
1739 log_scroll_down(struct tog_view *view, int maxscroll)
1741 struct tog_log_view_state *s = &view->state.log;
1742 const struct got_error *err = NULL;
1743 struct commit_queue_entry *pentry;
1744 int nscrolled = 0, ncommits_needed;
1746 if (s->last_displayed_entry == NULL)
1747 return NULL;
1749 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1750 if (s->commits.ncommits < ncommits_needed &&
1751 !s->thread_args.log_complete) {
1753 * Ask the log thread for required amount of commits.
1755 s->thread_args.commits_needed += maxscroll;
1756 err = trigger_log_thread(view, 1);
1757 if (err)
1758 return err;
1761 do {
1762 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1763 if (pentry == NULL)
1764 break;
1766 s->last_displayed_entry = pentry;
1768 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1769 if (pentry == NULL)
1770 break;
1771 s->first_displayed_entry = pentry;
1772 } while (++nscrolled < maxscroll);
1774 return err;
1777 static const struct got_error *
1778 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1779 struct got_commit_object *commit, struct got_object_id *commit_id,
1780 struct tog_view *log_view, struct got_repository *repo)
1782 const struct got_error *err;
1783 struct got_object_qid *parent_id;
1784 struct tog_view *diff_view;
1786 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1787 if (diff_view == NULL)
1788 return got_error_from_errno("view_open");
1790 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1791 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1792 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1793 if (err == NULL)
1794 *new_view = diff_view;
1795 return err;
1798 static const struct got_error *
1799 tree_view_visit_subtree(struct tog_tree_view_state *s,
1800 struct got_tree_object *subtree)
1802 struct tog_parent_tree *parent;
1804 parent = calloc(1, sizeof(*parent));
1805 if (parent == NULL)
1806 return got_error_from_errno("calloc");
1808 parent->tree = s->tree;
1809 parent->first_displayed_entry = s->first_displayed_entry;
1810 parent->selected_entry = s->selected_entry;
1811 parent->selected = s->selected;
1812 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1813 s->tree = subtree;
1814 s->selected = 0;
1815 s->first_displayed_entry = NULL;
1816 return NULL;
1819 static const struct got_error *
1820 tree_view_walk_path(struct tog_tree_view_state *s,
1821 struct got_object_id *commit_id, const char *path)
1823 const struct got_error *err = NULL;
1824 struct got_tree_object *tree = NULL;
1825 const char *p;
1826 char *slash, *subpath = NULL;
1828 /* Walk the path and open corresponding tree objects. */
1829 p = path;
1830 while (*p) {
1831 struct got_tree_entry *te;
1832 struct got_object_id *tree_id;
1833 char *te_name;
1835 while (p[0] == '/')
1836 p++;
1838 /* Ensure the correct subtree entry is selected. */
1839 slash = strchr(p, '/');
1840 if (slash == NULL)
1841 te_name = strdup(p);
1842 else
1843 te_name = strndup(p, slash - p);
1844 if (te_name == NULL) {
1845 err = got_error_from_errno("strndup");
1846 break;
1848 te = got_object_tree_find_entry(s->tree, te_name);
1849 if (te == NULL) {
1850 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1851 free(te_name);
1852 break;
1854 free(te_name);
1855 s->first_displayed_entry = s->selected_entry = te;
1857 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1858 break; /* jump to this file's entry */
1860 slash = strchr(p, '/');
1861 if (slash)
1862 subpath = strndup(path, slash - path);
1863 else
1864 subpath = strdup(path);
1865 if (subpath == NULL) {
1866 err = got_error_from_errno("strdup");
1867 break;
1870 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1871 subpath);
1872 if (err)
1873 break;
1875 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1876 free(tree_id);
1877 if (err)
1878 break;
1880 err = tree_view_visit_subtree(s, tree);
1881 if (err) {
1882 got_object_tree_close(tree);
1883 break;
1885 if (slash == NULL)
1886 break;
1887 free(subpath);
1888 subpath = NULL;
1889 p = slash;
1892 free(subpath);
1893 return err;
1896 static const struct got_error *
1897 browse_commit_tree(struct tog_view **new_view, int begin_x,
1898 struct commit_queue_entry *entry, const char *path,
1899 struct got_repository *repo)
1901 const struct got_error *err = NULL;
1902 struct got_tree_object *tree;
1903 struct tog_tree_view_state *s;
1904 struct tog_view *tree_view;
1906 err = got_object_open_as_tree(&tree, repo,
1907 got_object_commit_get_tree_id(entry->commit));
1908 if (err)
1909 return err;
1911 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1912 if (tree_view == NULL)
1913 return got_error_from_errno("view_open");
1915 err = open_tree_view(tree_view, tree, entry->id, repo);
1916 if (err) {
1917 got_object_tree_close(tree);
1918 return err;
1920 s = &tree_view->state.tree;
1922 *new_view = tree_view;
1924 if (got_path_is_root_dir(path))
1925 return NULL;
1927 return tree_view_walk_path(s, entry->id, path);
1930 static const struct got_error *
1931 block_signals_used_by_main_thread(void)
1933 sigset_t sigset;
1934 int errcode;
1936 if (sigemptyset(&sigset) == -1)
1937 return got_error_from_errno("sigemptyset");
1939 /* tog handles SIGWINCH and SIGCONT */
1940 if (sigaddset(&sigset, SIGWINCH) == -1)
1941 return got_error_from_errno("sigaddset");
1942 if (sigaddset(&sigset, SIGCONT) == -1)
1943 return got_error_from_errno("sigaddset");
1945 /* ncurses handles SIGTSTP */
1946 if (sigaddset(&sigset, SIGTSTP) == -1)
1947 return got_error_from_errno("sigaddset");
1949 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1950 if (errcode)
1951 return got_error_set_errno(errcode, "pthread_sigmask");
1953 return NULL;
1956 static void *
1957 log_thread(void *arg)
1959 const struct got_error *err = NULL;
1960 int errcode = 0;
1961 struct tog_log_thread_args *a = arg;
1962 int done = 0;
1964 err = block_signals_used_by_main_thread();
1965 if (err)
1966 return (void *)err;
1968 while (!done && !err && !tog_sigpipe_received) {
1969 err = queue_commits(a->graph, a->commits, 1, a->repo,
1970 a->in_repo_path, a->searching, a->search_next_done,
1971 a->regex);
1972 if (err) {
1973 if (err->code != GOT_ERR_ITER_COMPLETED)
1974 return (void *)err;
1975 err = NULL;
1976 done = 1;
1977 } else if (a->commits_needed > 0)
1978 a->commits_needed--;
1980 errcode = pthread_mutex_lock(&tog_mutex);
1981 if (errcode) {
1982 err = got_error_set_errno(errcode,
1983 "pthread_mutex_lock");
1984 break;
1985 } else if (*a->quit)
1986 done = 1;
1987 else if (*a->first_displayed_entry == NULL) {
1988 *a->first_displayed_entry =
1989 TAILQ_FIRST(&a->commits->head);
1990 *a->selected_entry = *a->first_displayed_entry;
1993 errcode = pthread_cond_signal(&a->commit_loaded);
1994 if (errcode) {
1995 err = got_error_set_errno(errcode,
1996 "pthread_cond_signal");
1997 pthread_mutex_unlock(&tog_mutex);
1998 break;
2001 if (done)
2002 a->commits_needed = 0;
2003 else {
2004 if (a->commits_needed == 0) {
2005 errcode = pthread_cond_wait(&a->need_commits,
2006 &tog_mutex);
2007 if (errcode)
2008 err = got_error_set_errno(errcode,
2009 "pthread_cond_wait");
2013 errcode = pthread_mutex_unlock(&tog_mutex);
2014 if (errcode && err == NULL)
2015 err = got_error_set_errno(errcode,
2016 "pthread_mutex_unlock");
2018 a->log_complete = 1;
2019 return (void *)err;
2022 static const struct got_error *
2023 stop_log_thread(struct tog_log_view_state *s)
2025 const struct got_error *err = NULL;
2026 int errcode;
2028 if (s->thread) {
2029 s->quit = 1;
2030 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2031 if (errcode)
2032 return got_error_set_errno(errcode,
2033 "pthread_cond_signal");
2034 errcode = pthread_mutex_unlock(&tog_mutex);
2035 if (errcode)
2036 return got_error_set_errno(errcode,
2037 "pthread_mutex_unlock");
2038 errcode = pthread_join(s->thread, (void **)&err);
2039 if (errcode)
2040 return got_error_set_errno(errcode, "pthread_join");
2041 errcode = pthread_mutex_lock(&tog_mutex);
2042 if (errcode)
2043 return got_error_set_errno(errcode,
2044 "pthread_mutex_lock");
2045 s->thread = NULL;
2048 if (s->thread_args.repo) {
2049 got_repo_close(s->thread_args.repo);
2050 s->thread_args.repo = NULL;
2053 if (s->thread_args.graph) {
2054 got_commit_graph_close(s->thread_args.graph);
2055 s->thread_args.graph = NULL;
2058 return err;
2061 static const struct got_error *
2062 close_log_view(struct tog_view *view)
2064 const struct got_error *err = NULL;
2065 struct tog_log_view_state *s = &view->state.log;
2066 int errcode;
2068 err = stop_log_thread(s);
2070 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2071 if (errcode && err == NULL)
2072 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2074 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2075 if (errcode && err == NULL)
2076 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2078 free_commits(&s->commits);
2079 free(s->in_repo_path);
2080 s->in_repo_path = NULL;
2081 free(s->start_id);
2082 s->start_id = NULL;
2083 got_ref_list_free(&s->refs);
2084 return err;
2087 static const struct got_error *
2088 search_start_log_view(struct tog_view *view)
2090 struct tog_log_view_state *s = &view->state.log;
2092 s->matched_entry = NULL;
2093 s->search_entry = NULL;
2094 return NULL;
2097 static const struct got_error *
2098 search_next_log_view(struct tog_view *view)
2100 const struct got_error *err = NULL;
2101 struct tog_log_view_state *s = &view->state.log;
2102 struct commit_queue_entry *entry;
2104 /* Display progress update in log view. */
2105 show_log_view(view);
2106 update_panels();
2107 doupdate();
2109 if (s->search_entry) {
2110 int errcode, ch;
2111 errcode = pthread_mutex_unlock(&tog_mutex);
2112 if (errcode)
2113 return got_error_set_errno(errcode,
2114 "pthread_mutex_unlock");
2115 ch = wgetch(view->window);
2116 errcode = pthread_mutex_lock(&tog_mutex);
2117 if (errcode)
2118 return got_error_set_errno(errcode,
2119 "pthread_mutex_lock");
2120 if (ch == KEY_BACKSPACE) {
2121 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2122 return NULL;
2124 if (view->searching == TOG_SEARCH_FORWARD)
2125 entry = TAILQ_NEXT(s->search_entry, entry);
2126 else
2127 entry = TAILQ_PREV(s->search_entry,
2128 commit_queue_head, entry);
2129 } else if (s->matched_entry) {
2130 if (view->searching == TOG_SEARCH_FORWARD)
2131 entry = TAILQ_NEXT(s->matched_entry, entry);
2132 else
2133 entry = TAILQ_PREV(s->matched_entry,
2134 commit_queue_head, entry);
2135 } else {
2136 if (view->searching == TOG_SEARCH_FORWARD)
2137 entry = TAILQ_FIRST(&s->commits.head);
2138 else
2139 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2142 while (1) {
2143 int have_match = 0;
2145 if (entry == NULL) {
2146 if (s->thread_args.log_complete ||
2147 view->searching == TOG_SEARCH_BACKWARD) {
2148 view->search_next_done =
2149 (s->matched_entry == NULL ?
2150 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2151 s->search_entry = NULL;
2152 return NULL;
2155 * Poke the log thread for more commits and return,
2156 * allowing the main loop to make progress. Search
2157 * will resume at s->search_entry once we come back.
2159 s->thread_args.commits_needed++;
2160 return trigger_log_thread(view, 0);
2163 err = match_commit(&have_match, entry->id, entry->commit,
2164 &view->regex);
2165 if (err)
2166 break;
2167 if (have_match) {
2168 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2169 s->matched_entry = entry;
2170 break;
2173 s->search_entry = entry;
2174 if (view->searching == TOG_SEARCH_FORWARD)
2175 entry = TAILQ_NEXT(entry, entry);
2176 else
2177 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2180 if (s->matched_entry) {
2181 int cur = s->selected_entry->idx;
2182 while (cur < s->matched_entry->idx) {
2183 err = input_log_view(NULL, NULL, view, KEY_DOWN);
2184 if (err)
2185 return err;
2186 cur++;
2188 while (cur > s->matched_entry->idx) {
2189 err = input_log_view(NULL, NULL, view, KEY_UP);
2190 if (err)
2191 return err;
2192 cur--;
2196 s->search_entry = NULL;
2198 return NULL;
2201 static const struct got_error *
2202 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2203 struct got_repository *repo, const char *head_ref_name,
2204 const char *in_repo_path, int log_branches)
2206 const struct got_error *err = NULL;
2207 struct tog_log_view_state *s = &view->state.log;
2208 struct got_repository *thread_repo = NULL;
2209 struct got_commit_graph *thread_graph = NULL;
2210 int errcode;
2212 SIMPLEQ_INIT(&s->refs);
2214 if (in_repo_path != s->in_repo_path) {
2215 free(s->in_repo_path);
2216 s->in_repo_path = strdup(in_repo_path);
2217 if (s->in_repo_path == NULL)
2218 return got_error_from_errno("strdup");
2221 /* The commit queue only contains commits being displayed. */
2222 TAILQ_INIT(&s->commits.head);
2223 s->commits.ncommits = 0;
2225 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2226 if (err)
2227 goto done;
2229 s->repo = repo;
2230 s->head_ref_name = head_ref_name;
2231 s->start_id = got_object_id_dup(start_id);
2232 if (s->start_id == NULL) {
2233 err = got_error_from_errno("got_object_id_dup");
2234 goto done;
2236 s->log_branches = log_branches;
2238 SIMPLEQ_INIT(&s->colors);
2239 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2240 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2241 get_color_value("TOG_COLOR_COMMIT"));
2242 if (err)
2243 goto done;
2244 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2245 get_color_value("TOG_COLOR_AUTHOR"));
2246 if (err) {
2247 free_colors(&s->colors);
2248 goto done;
2250 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2251 get_color_value("TOG_COLOR_DATE"));
2252 if (err) {
2253 free_colors(&s->colors);
2254 goto done;
2258 view->show = show_log_view;
2259 view->input = input_log_view;
2260 view->close = close_log_view;
2261 view->search_start = search_start_log_view;
2262 view->search_next = search_next_log_view;
2264 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2265 if (err)
2266 goto done;
2267 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2268 !s->log_branches);
2269 if (err)
2270 goto done;
2271 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2272 s->repo, NULL, NULL);
2273 if (err)
2274 goto done;
2276 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2277 if (errcode) {
2278 err = got_error_set_errno(errcode, "pthread_cond_init");
2279 goto done;
2281 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2282 if (errcode) {
2283 err = got_error_set_errno(errcode, "pthread_cond_init");
2284 goto done;
2287 s->thread_args.commits_needed = view->nlines;
2288 s->thread_args.graph = thread_graph;
2289 s->thread_args.commits = &s->commits;
2290 s->thread_args.in_repo_path = s->in_repo_path;
2291 s->thread_args.start_id = s->start_id;
2292 s->thread_args.repo = thread_repo;
2293 s->thread_args.log_complete = 0;
2294 s->thread_args.quit = &s->quit;
2295 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2296 s->thread_args.selected_entry = &s->selected_entry;
2297 s->thread_args.searching = &view->searching;
2298 s->thread_args.search_next_done = &view->search_next_done;
2299 s->thread_args.regex = &view->regex;
2300 done:
2301 if (err)
2302 close_log_view(view);
2303 return err;
2306 static const struct got_error *
2307 show_log_view(struct tog_view *view)
2309 const struct got_error *err;
2310 struct tog_log_view_state *s = &view->state.log;
2312 if (s->thread == NULL) {
2313 int errcode = pthread_create(&s->thread, NULL, log_thread,
2314 &s->thread_args);
2315 if (errcode)
2316 return got_error_set_errno(errcode, "pthread_create");
2317 if (s->thread_args.commits_needed > 0) {
2318 err = trigger_log_thread(view, 1);
2319 if (err)
2320 return err;
2324 return draw_commits(view);
2327 static const struct got_error *
2328 input_log_view(struct tog_view **new_view, struct tog_view **focus_view,
2329 struct tog_view *view, int ch)
2331 const struct got_error *err = NULL;
2332 struct tog_log_view_state *s = &view->state.log;
2333 char *parent_path, *in_repo_path = NULL;
2334 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2335 struct tog_view *ref_view = NULL;
2336 int begin_x = 0;
2337 struct got_object_id *start_id;
2339 switch (ch) {
2340 case 'q':
2341 s->quit = 1;
2342 break;
2343 case 'k':
2344 case KEY_UP:
2345 case '<':
2346 case ',':
2347 if (s->first_displayed_entry == NULL)
2348 break;
2349 if (s->selected > 0)
2350 s->selected--;
2351 else
2352 log_scroll_up(s, 1);
2353 break;
2354 case KEY_PPAGE:
2355 case CTRL('b'):
2356 if (s->first_displayed_entry == NULL)
2357 break;
2358 if (TAILQ_FIRST(&s->commits.head) ==
2359 s->first_displayed_entry) {
2360 s->selected = 0;
2361 break;
2363 log_scroll_up(s, view->nlines - 1);
2364 break;
2365 case 'j':
2366 case KEY_DOWN:
2367 case '>':
2368 case '.':
2369 if (s->first_displayed_entry == NULL)
2370 break;
2371 if (s->selected < MIN(view->nlines - 2,
2372 s->commits.ncommits - 1)) {
2373 s->selected++;
2374 break;
2376 err = log_scroll_down(view, 1);
2377 break;
2378 case KEY_NPAGE:
2379 case CTRL('f'): {
2380 struct commit_queue_entry *first;
2381 first = s->first_displayed_entry;
2382 if (first == NULL)
2383 break;
2384 err = log_scroll_down(view, view->nlines - 1);
2385 if (err)
2386 break;
2387 if (first == s->first_displayed_entry &&
2388 s->selected < MIN(view->nlines - 2,
2389 s->commits.ncommits - 1)) {
2390 /* can't scroll further down */
2391 s->selected = MIN(view->nlines - 2,
2392 s->commits.ncommits - 1);
2394 err = NULL;
2395 break;
2397 case KEY_RESIZE:
2398 if (s->selected > view->nlines - 2)
2399 s->selected = view->nlines - 2;
2400 if (s->selected > s->commits.ncommits - 1)
2401 s->selected = s->commits.ncommits - 1;
2402 if (s->commits.ncommits < view->nlines - 1 &&
2403 !s->thread_args.log_complete) {
2404 s->thread_args.commits_needed += (view->nlines - 1) -
2405 s->commits.ncommits;
2406 err = trigger_log_thread(view, 1);
2408 break;
2409 case KEY_ENTER:
2410 case ' ':
2411 case '\r':
2412 if (s->selected_entry == NULL)
2413 break;
2414 if (view_is_parent_view(view))
2415 begin_x = view_split_begin_x(view->begin_x);
2416 err = open_diff_view_for_commit(&diff_view, begin_x,
2417 s->selected_entry->commit, s->selected_entry->id,
2418 view, s->repo);
2419 if (err)
2420 break;
2421 if (view_is_parent_view(view)) {
2422 err = view_close_child(view);
2423 if (err)
2424 return err;
2425 view_set_child(view, diff_view);
2426 *focus_view = diff_view;
2427 } else
2428 *new_view = diff_view;
2429 break;
2430 case 't':
2431 if (s->selected_entry == NULL)
2432 break;
2433 if (view_is_parent_view(view))
2434 begin_x = view_split_begin_x(view->begin_x);
2435 err = browse_commit_tree(&tree_view, begin_x,
2436 s->selected_entry, s->in_repo_path, s->repo);
2437 if (err)
2438 break;
2439 if (view_is_parent_view(view)) {
2440 err = view_close_child(view);
2441 if (err)
2442 return err;
2443 view_set_child(view, tree_view);
2444 *focus_view = tree_view;
2445 } else
2446 *new_view = tree_view;
2447 break;
2448 case KEY_BACKSPACE:
2449 if (got_path_cmp(s->in_repo_path, "/",
2450 strlen(s->in_repo_path), 1) == 0)
2451 break;
2452 err = got_path_dirname(&parent_path, s->in_repo_path);
2453 if (err)
2454 return err;
2455 err = stop_log_thread(s);
2456 if (err) {
2457 free(parent_path);
2458 return err;
2460 lv = view_open(view->nlines, view->ncols,
2461 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2462 if (lv == NULL) {
2463 free(parent_path);
2464 return got_error_from_errno("view_open");
2466 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2467 parent_path, s->log_branches);
2468 free(parent_path);
2469 if (err)
2470 return err;;
2471 if (view_is_parent_view(view))
2472 *new_view = lv;
2473 else {
2474 view_set_child(view->parent, lv);
2475 *focus_view = lv;
2477 break;
2478 case CTRL('l'):
2479 err = stop_log_thread(s);
2480 if (err)
2481 return err;
2482 lv = view_open(view->nlines, view->ncols,
2483 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2484 if (lv == NULL)
2485 return got_error_from_errno("view_open");
2486 err = got_repo_match_object_id(&start_id, NULL,
2487 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2488 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2489 if (err) {
2490 view_close(lv);
2491 return err;
2493 in_repo_path = strdup(s->in_repo_path);
2494 if (in_repo_path == NULL) {
2495 free(start_id);
2496 view_close(lv);
2497 return got_error_from_errno("strdup");
2499 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2500 in_repo_path, s->log_branches);
2501 if (err) {
2502 free(start_id);
2503 view_close(lv);
2504 return err;;
2506 view->dying = 1;
2507 *new_view = lv;
2508 break;
2509 case 'B':
2510 s->log_branches = !s->log_branches;
2511 err = stop_log_thread(s);
2512 if (err)
2513 return err;
2514 lv = view_open(view->nlines, view->ncols,
2515 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2516 if (lv == NULL)
2517 return got_error_from_errno("view_open");
2518 err = open_log_view(lv, s->start_id, s->repo,
2519 s->head_ref_name, s->in_repo_path, s->log_branches);
2520 if (err) {
2521 view_close(lv);
2522 return err;;
2524 view->dying = 1;
2525 *new_view = lv;
2526 break;
2527 case 'r':
2528 if (view_is_parent_view(view))
2529 begin_x = view_split_begin_x(view->begin_x);
2530 ref_view = view_open(view->nlines, view->ncols,
2531 view->begin_y, begin_x, TOG_VIEW_REF);
2532 if (ref_view == NULL)
2533 return got_error_from_errno("view_open");
2534 err = open_ref_view(ref_view, s->repo);
2535 if (err) {
2536 view_close(ref_view);
2537 return err;
2539 if (view_is_parent_view(view)) {
2540 err = view_close_child(view);
2541 if (err)
2542 return err;
2543 view_set_child(view, ref_view);
2544 *focus_view = ref_view;
2545 } else
2546 *new_view = ref_view;
2547 break;
2548 default:
2549 break;
2552 return err;
2555 static const struct got_error *
2556 apply_unveil(const char *repo_path, const char *worktree_path)
2558 const struct got_error *error;
2560 #ifdef PROFILE
2561 if (unveil("gmon.out", "rwc") != 0)
2562 return got_error_from_errno2("unveil", "gmon.out");
2563 #endif
2564 if (repo_path && unveil(repo_path, "r") != 0)
2565 return got_error_from_errno2("unveil", repo_path);
2567 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2568 return got_error_from_errno2("unveil", worktree_path);
2570 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2571 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2573 error = got_privsep_unveil_exec_helpers();
2574 if (error != NULL)
2575 return error;
2577 if (unveil(NULL, NULL) != 0)
2578 return got_error_from_errno("unveil");
2580 return NULL;
2583 static void
2584 init_curses(void)
2586 initscr();
2587 cbreak();
2588 halfdelay(1); /* Do fast refresh while initial view is loading. */
2589 noecho();
2590 nonl();
2591 intrflush(stdscr, FALSE);
2592 keypad(stdscr, TRUE);
2593 curs_set(0);
2594 if (getenv("TOG_COLORS") != NULL) {
2595 start_color();
2596 use_default_colors();
2598 signal(SIGWINCH, tog_sigwinch);
2599 signal(SIGPIPE, tog_sigpipe);
2600 signal(SIGCONT, tog_sigcont);
2603 static const struct got_error *
2604 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2605 struct got_repository *repo, struct got_worktree *worktree)
2607 const struct got_error *err = NULL;
2609 if (argc == 0) {
2610 *in_repo_path = strdup("/");
2611 if (*in_repo_path == NULL)
2612 return got_error_from_errno("strdup");
2613 return NULL;
2616 if (worktree) {
2617 const char *prefix = got_worktree_get_path_prefix(worktree);
2618 char *p;
2620 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2621 if (err)
2622 return err;
2623 if (asprintf(in_repo_path, "%s%s%s", prefix,
2624 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2625 p) == -1) {
2626 err = got_error_from_errno("asprintf");
2627 *in_repo_path = NULL;
2629 free(p);
2630 } else
2631 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2633 return err;
2636 static const struct got_error *
2637 cmd_log(int argc, char *argv[])
2639 const struct got_error *error;
2640 struct got_repository *repo = NULL;
2641 struct got_worktree *worktree = NULL;
2642 struct got_object_id *start_id = NULL;
2643 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2644 char *start_commit = NULL, *head_ref_name = NULL;
2645 int ch, log_branches = 0;
2646 struct tog_view *view;
2648 #ifndef PROFILE
2649 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2650 NULL) == -1)
2651 err(1, "pledge");
2652 #endif
2654 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2655 switch (ch) {
2656 case 'b':
2657 log_branches = 1;
2658 break;
2659 case 'c':
2660 start_commit = optarg;
2661 break;
2662 case 'r':
2663 repo_path = realpath(optarg, NULL);
2664 if (repo_path == NULL)
2665 return got_error_from_errno2("realpath",
2666 optarg);
2667 break;
2668 default:
2669 usage_log();
2670 /* NOTREACHED */
2674 argc -= optind;
2675 argv += optind;
2677 if (argc > 1)
2678 usage_log();
2680 cwd = getcwd(NULL, 0);
2681 if (cwd == NULL)
2682 return got_error_from_errno("getcwd");
2684 error = got_worktree_open(&worktree, cwd);
2685 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2686 goto done;
2688 if (repo_path == NULL) {
2689 if (worktree)
2690 repo_path =
2691 strdup(got_worktree_get_repo_path(worktree));
2692 else
2693 repo_path = strdup(cwd);
2695 if (repo_path == NULL) {
2696 error = got_error_from_errno("strdup");
2697 goto done;
2700 error = got_repo_open(&repo, repo_path, NULL);
2701 if (error != NULL)
2702 goto done;
2704 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2705 repo, worktree);
2706 if (error)
2707 goto done;
2709 init_curses();
2711 error = apply_unveil(got_repo_get_path(repo),
2712 worktree ? got_worktree_get_root_path(worktree) : NULL);
2713 if (error)
2714 goto done;
2716 if (start_commit == NULL)
2717 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2718 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2719 GOT_OBJ_TYPE_COMMIT, 1, repo);
2720 else
2721 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2722 GOT_OBJ_TYPE_COMMIT, 1, repo);
2723 if (error != NULL)
2724 goto done;
2726 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2727 if (view == NULL) {
2728 error = got_error_from_errno("view_open");
2729 goto done;
2731 if (worktree) {
2732 head_ref_name = strdup(
2733 got_worktree_get_head_ref_name(worktree));
2734 if (head_ref_name == NULL) {
2735 error = got_error_from_errno("strdup");
2736 goto done;
2739 error = open_log_view(view, start_id, repo, head_ref_name,
2740 in_repo_path, log_branches);
2741 if (error)
2742 goto done;
2743 if (worktree) {
2744 /* Release work tree lock. */
2745 got_worktree_close(worktree);
2746 worktree = NULL;
2748 error = view_loop(view);
2749 done:
2750 free(in_repo_path);
2751 free(repo_path);
2752 free(cwd);
2753 free(start_id);
2754 free(head_ref_name);
2755 if (repo)
2756 got_repo_close(repo);
2757 if (worktree)
2758 got_worktree_close(worktree);
2759 return error;
2762 __dead static void
2763 usage_diff(void)
2765 endwin();
2766 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2767 "[-w] object1 object2\n", getprogname());
2768 exit(1);
2771 static char *
2772 parse_next_line(FILE *f, size_t *len)
2774 char *line;
2775 size_t linelen;
2776 size_t lineno;
2777 const char delim[3] = { '\0', '\0', '\0'};
2779 line = fparseln(f, &linelen, &lineno, delim, 0);
2780 if (len)
2781 *len = linelen;
2782 return line;
2785 static int
2786 match_line(const char *line, regex_t *regex, size_t nmatch,
2787 regmatch_t *regmatch)
2789 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2792 struct tog_color *
2793 match_color(struct tog_colors *colors, const char *line)
2795 struct tog_color *tc = NULL;
2797 SIMPLEQ_FOREACH(tc, colors, entry) {
2798 if (match_line(line, &tc->regex, 0, NULL))
2799 return tc;
2802 return NULL;
2805 static const struct got_error *
2806 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2807 WINDOW *window, regmatch_t *regmatch)
2809 const struct got_error *err = NULL;
2810 wchar_t *wline;
2811 int width;
2812 char *s;
2814 *wtotal = 0;
2816 s = strndup(line, regmatch->rm_so);
2817 if (s == NULL)
2818 return got_error_from_errno("strndup");
2820 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2821 if (err) {
2822 free(s);
2823 return err;
2825 waddwstr(window, wline);
2826 free(wline);
2827 free(s);
2828 wlimit -= width;
2829 *wtotal += width;
2831 if (wlimit > 0) {
2832 s = strndup(line + regmatch->rm_so,
2833 regmatch->rm_eo - regmatch->rm_so);
2834 if (s == NULL) {
2835 err = got_error_from_errno("strndup");
2836 free(s);
2837 return err;
2839 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2840 if (err) {
2841 free(s);
2842 return err;
2844 wattr_on(window, A_STANDOUT, NULL);
2845 waddwstr(window, wline);
2846 wattr_off(window, A_STANDOUT, NULL);
2847 free(wline);
2848 free(s);
2849 wlimit -= width;
2850 *wtotal += width;
2853 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2854 err = format_line(&wline, &width,
2855 line + regmatch->rm_eo, wlimit, col_tab_align);
2856 if (err)
2857 return err;
2858 waddwstr(window, wline);
2859 free(wline);
2860 *wtotal += width;
2863 return NULL;
2866 static const struct got_error *
2867 draw_file(struct tog_view *view, const char *header)
2869 struct tog_diff_view_state *s = &view->state.diff;
2870 regmatch_t *regmatch = &view->regmatch;
2871 const struct got_error *err;
2872 int nprinted = 0;
2873 char *line;
2874 struct tog_color *tc;
2875 size_t len;
2876 wchar_t *wline;
2877 int width;
2878 int max_lines = view->nlines;
2879 int nlines = s->nlines;
2880 off_t line_offset;
2882 line_offset = s->line_offsets[s->first_displayed_line - 1];
2883 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2884 return got_error_from_errno("fseek");
2886 werase(view->window);
2888 if (header) {
2889 if (asprintf(&line, "[%d/%d] %s",
2890 s->first_displayed_line - 1 + s->selected_line, nlines,
2891 header) == -1)
2892 return got_error_from_errno("asprintf");
2893 err = format_line(&wline, &width, line, view->ncols, 0);
2894 free(line);
2895 if (err)
2896 return err;
2898 if (view_needs_focus_indication(view))
2899 wstandout(view->window);
2900 waddwstr(view->window, wline);
2901 free(wline);
2902 wline = NULL;
2903 if (view_needs_focus_indication(view))
2904 wstandend(view->window);
2905 if (width <= view->ncols - 1)
2906 waddch(view->window, '\n');
2908 if (max_lines <= 1)
2909 return NULL;
2910 max_lines--;
2913 s->eof = 0;
2914 while (max_lines > 0 && nprinted < max_lines) {
2915 line = parse_next_line(s->f, &len);
2916 if (line == NULL) {
2917 s->eof = 1;
2918 break;
2921 tc = match_color(&s->colors, line);
2922 if (tc)
2923 wattr_on(view->window,
2924 COLOR_PAIR(tc->colorpair), NULL);
2925 if (s->first_displayed_line + nprinted == s->matched_line &&
2926 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2927 err = add_matched_line(&width, line, view->ncols, 0,
2928 view->window, regmatch);
2929 if (err) {
2930 free(line);
2931 return err;
2933 } else {
2934 err = format_line(&wline, &width, line, view->ncols, 0);
2935 if (err) {
2936 free(line);
2937 return err;
2939 waddwstr(view->window, wline);
2940 free(wline);
2941 wline = NULL;
2943 if (tc)
2944 wattr_off(view->window,
2945 COLOR_PAIR(tc->colorpair), NULL);
2946 if (width <= view->ncols - 1)
2947 waddch(view->window, '\n');
2948 nprinted++;
2949 free(line);
2951 if (nprinted >= 1)
2952 s->last_displayed_line = s->first_displayed_line +
2953 (nprinted - 1);
2954 else
2955 s->last_displayed_line = s->first_displayed_line;
2957 view_vborder(view);
2959 if (s->eof) {
2960 while (nprinted < view->nlines) {
2961 waddch(view->window, '\n');
2962 nprinted++;
2965 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2966 if (err) {
2967 return err;
2970 wstandout(view->window);
2971 waddwstr(view->window, wline);
2972 free(wline);
2973 wline = NULL;
2974 wstandend(view->window);
2977 return NULL;
2980 static char *
2981 get_datestr(time_t *time, char *datebuf)
2983 struct tm mytm, *tm;
2984 char *p, *s;
2986 tm = gmtime_r(time, &mytm);
2987 if (tm == NULL)
2988 return NULL;
2989 s = asctime_r(tm, datebuf);
2990 if (s == NULL)
2991 return NULL;
2992 p = strchr(s, '\n');
2993 if (p)
2994 *p = '\0';
2995 return s;
2998 static const struct got_error *
2999 get_changed_paths(struct got_pathlist_head *paths,
3000 struct got_commit_object *commit, struct got_repository *repo)
3002 const struct got_error *err = NULL;
3003 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3004 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3005 struct got_object_qid *qid;
3007 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3008 if (qid != NULL) {
3009 struct got_commit_object *pcommit;
3010 err = got_object_open_as_commit(&pcommit, repo,
3011 qid->id);
3012 if (err)
3013 return err;
3015 tree_id1 = got_object_commit_get_tree_id(pcommit);
3016 got_object_commit_close(pcommit);
3020 if (tree_id1) {
3021 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3022 if (err)
3023 goto done;
3026 tree_id2 = got_object_commit_get_tree_id(commit);
3027 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3028 if (err)
3029 goto done;
3031 err = got_diff_tree(tree1, tree2, "", "", repo,
3032 got_diff_tree_collect_changed_paths, paths, 0);
3033 done:
3034 if (tree1)
3035 got_object_tree_close(tree1);
3036 if (tree2)
3037 got_object_tree_close(tree2);
3038 return err;
3041 static const struct got_error *
3042 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3044 off_t *p;
3046 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3047 if (p == NULL)
3048 return got_error_from_errno("reallocarray");
3049 *line_offsets = p;
3050 (*line_offsets)[*nlines] = off;
3051 (*nlines)++;
3052 return NULL;
3055 static const struct got_error *
3056 write_commit_info(off_t **line_offsets, size_t *nlines,
3057 struct got_object_id *commit_id, struct got_reflist_head *refs,
3058 struct got_repository *repo, FILE *outfile)
3060 const struct got_error *err = NULL;
3061 char datebuf[26], *datestr;
3062 struct got_commit_object *commit;
3063 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3064 time_t committer_time;
3065 const char *author, *committer;
3066 char *refs_str = NULL;
3067 struct got_pathlist_head changed_paths;
3068 struct got_pathlist_entry *pe;
3069 off_t outoff = 0;
3070 int n;
3072 TAILQ_INIT(&changed_paths);
3074 if (refs) {
3075 err = build_refs_str(&refs_str, refs, commit_id, repo);
3076 if (err)
3077 return err;
3080 err = got_object_open_as_commit(&commit, repo, commit_id);
3081 if (err)
3082 return err;
3084 err = got_object_id_str(&id_str, commit_id);
3085 if (err) {
3086 err = got_error_from_errno("got_object_id_str");
3087 goto done;
3090 err = add_line_offset(line_offsets, nlines, 0);
3091 if (err)
3092 goto done;
3094 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3095 refs_str ? refs_str : "", refs_str ? ")" : "");
3096 if (n < 0) {
3097 err = got_error_from_errno("fprintf");
3098 goto done;
3100 outoff += n;
3101 err = add_line_offset(line_offsets, nlines, outoff);
3102 if (err)
3103 goto done;
3105 n = fprintf(outfile, "from: %s\n",
3106 got_object_commit_get_author(commit));
3107 if (n < 0) {
3108 err = got_error_from_errno("fprintf");
3109 goto done;
3111 outoff += n;
3112 err = add_line_offset(line_offsets, nlines, outoff);
3113 if (err)
3114 goto done;
3116 committer_time = got_object_commit_get_committer_time(commit);
3117 datestr = get_datestr(&committer_time, datebuf);
3118 if (datestr) {
3119 n = fprintf(outfile, "date: %s UTC\n", datestr);
3120 if (n < 0) {
3121 err = got_error_from_errno("fprintf");
3122 goto done;
3124 outoff += n;
3125 err = add_line_offset(line_offsets, nlines, outoff);
3126 if (err)
3127 goto done;
3129 author = got_object_commit_get_author(commit);
3130 committer = got_object_commit_get_committer(commit);
3131 if (strcmp(author, committer) != 0) {
3132 n = fprintf(outfile, "via: %s\n", committer);
3133 if (n < 0) {
3134 err = got_error_from_errno("fprintf");
3135 goto done;
3137 outoff += n;
3138 err = add_line_offset(line_offsets, nlines, outoff);
3139 if (err)
3140 goto done;
3142 err = got_object_commit_get_logmsg(&logmsg, commit);
3143 if (err)
3144 goto done;
3145 s = logmsg;
3146 while ((line = strsep(&s, "\n")) != NULL) {
3147 n = fprintf(outfile, "%s\n", line);
3148 if (n < 0) {
3149 err = got_error_from_errno("fprintf");
3150 goto done;
3152 outoff += n;
3153 err = add_line_offset(line_offsets, nlines, outoff);
3154 if (err)
3155 goto done;
3158 err = get_changed_paths(&changed_paths, commit, repo);
3159 if (err)
3160 goto done;
3161 TAILQ_FOREACH(pe, &changed_paths, entry) {
3162 struct got_diff_changed_path *cp = pe->data;
3163 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3164 if (n < 0) {
3165 err = got_error_from_errno("fprintf");
3166 goto done;
3168 outoff += n;
3169 err = add_line_offset(line_offsets, nlines, outoff);
3170 if (err)
3171 goto done;
3172 free((char *)pe->path);
3173 free(pe->data);
3176 fputc('\n', outfile);
3177 outoff++;
3178 err = add_line_offset(line_offsets, nlines, outoff);
3179 done:
3180 got_pathlist_free(&changed_paths);
3181 free(id_str);
3182 free(logmsg);
3183 free(refs_str);
3184 got_object_commit_close(commit);
3185 if (err) {
3186 free(*line_offsets);
3187 *line_offsets = NULL;
3188 *nlines = 0;
3190 return err;
3193 static const struct got_error *
3194 create_diff(struct tog_diff_view_state *s)
3196 const struct got_error *err = NULL;
3197 FILE *f = NULL;
3198 int obj_type;
3200 free(s->line_offsets);
3201 s->line_offsets = malloc(sizeof(off_t));
3202 if (s->line_offsets == NULL)
3203 return got_error_from_errno("malloc");
3204 s->nlines = 0;
3206 f = got_opentemp();
3207 if (f == NULL) {
3208 err = got_error_from_errno("got_opentemp");
3209 goto done;
3211 if (s->f && fclose(s->f) != 0) {
3212 err = got_error_from_errno("fclose");
3213 goto done;
3215 s->f = f;
3217 if (s->id1)
3218 err = got_object_get_type(&obj_type, s->repo, s->id1);
3219 else
3220 err = got_object_get_type(&obj_type, s->repo, s->id2);
3221 if (err)
3222 goto done;
3224 switch (obj_type) {
3225 case GOT_OBJ_TYPE_BLOB:
3226 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3227 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3228 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3229 break;
3230 case GOT_OBJ_TYPE_TREE:
3231 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3232 s->id1, s->id2, "", "", s->diff_context,
3233 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3234 break;
3235 case GOT_OBJ_TYPE_COMMIT: {
3236 const struct got_object_id_queue *parent_ids;
3237 struct got_object_qid *pid;
3238 struct got_commit_object *commit2;
3240 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3241 if (err)
3242 goto done;
3243 /* Show commit info if we're diffing to a parent/root commit. */
3244 if (s->id1 == NULL) {
3245 err = write_commit_info(&s->line_offsets, &s->nlines,
3246 s->id2, &s->refs, s->repo, s->f);
3247 if (err)
3248 goto done;
3249 } else {
3250 parent_ids = got_object_commit_get_parent_ids(commit2);
3251 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3252 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3253 err = write_commit_info(
3254 &s->line_offsets, &s->nlines,
3255 s->id2, &s->refs, s->repo, s->f);
3256 if (err)
3257 goto done;
3258 break;
3262 got_object_commit_close(commit2);
3264 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3265 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3266 s->force_text_diff, s->repo, s->f);
3267 break;
3269 default:
3270 err = got_error(GOT_ERR_OBJ_TYPE);
3271 break;
3273 if (err)
3274 goto done;
3275 done:
3276 if (s->f && fflush(s->f) != 0 && err == NULL)
3277 err = got_error_from_errno("fflush");
3278 return err;
3281 static void
3282 diff_view_indicate_progress(struct tog_view *view)
3284 mvwaddstr(view->window, 0, 0, "diffing...");
3285 update_panels();
3286 doupdate();
3289 static const struct got_error *
3290 search_start_diff_view(struct tog_view *view)
3292 struct tog_diff_view_state *s = &view->state.diff;
3294 s->matched_line = 0;
3295 return NULL;
3298 static const struct got_error *
3299 search_next_diff_view(struct tog_view *view)
3301 struct tog_diff_view_state *s = &view->state.diff;
3302 int lineno;
3304 if (!view->searching) {
3305 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3306 return NULL;
3309 if (s->matched_line) {
3310 if (view->searching == TOG_SEARCH_FORWARD)
3311 lineno = s->matched_line + 1;
3312 else
3313 lineno = s->matched_line - 1;
3314 } else {
3315 if (view->searching == TOG_SEARCH_FORWARD)
3316 lineno = 1;
3317 else
3318 lineno = s->nlines;
3321 while (1) {
3322 char *line = NULL;
3323 off_t offset;
3324 size_t len;
3326 if (lineno <= 0 || lineno > s->nlines) {
3327 if (s->matched_line == 0) {
3328 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3329 free(line);
3330 break;
3333 if (view->searching == TOG_SEARCH_FORWARD)
3334 lineno = 1;
3335 else
3336 lineno = s->nlines;
3339 offset = s->line_offsets[lineno - 1];
3340 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3341 free(line);
3342 return got_error_from_errno("fseeko");
3344 free(line);
3345 line = parse_next_line(s->f, &len);
3346 if (line &&
3347 match_line(line, &view->regex, 1, &view->regmatch)) {
3348 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3349 s->matched_line = lineno;
3350 free(line);
3351 break;
3353 free(line);
3354 if (view->searching == TOG_SEARCH_FORWARD)
3355 lineno++;
3356 else
3357 lineno--;
3360 if (s->matched_line) {
3361 s->first_displayed_line = s->matched_line;
3362 s->selected_line = 1;
3365 return NULL;
3368 static const struct got_error *
3369 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3370 struct got_object_id *id2, const char *label1, const char *label2,
3371 int diff_context, int ignore_whitespace, int force_text_diff,
3372 struct tog_view *log_view, struct got_repository *repo)
3374 const struct got_error *err;
3375 struct tog_diff_view_state *s = &view->state.diff;
3377 SIMPLEQ_INIT(&s->refs);
3379 if (id1 != NULL && id2 != NULL) {
3380 int type1, type2;
3381 err = got_object_get_type(&type1, repo, id1);
3382 if (err)
3383 return err;
3384 err = got_object_get_type(&type2, repo, id2);
3385 if (err)
3386 return err;
3388 if (type1 != type2)
3389 return got_error(GOT_ERR_OBJ_TYPE);
3391 s->first_displayed_line = 1;
3392 s->last_displayed_line = view->nlines;
3393 s->selected_line = 1;
3394 s->repo = repo;
3395 s->id1 = id1;
3396 s->id2 = id2;
3397 s->label1 = label1;
3398 s->label2 = label2;
3400 if (id1) {
3401 s->id1 = got_object_id_dup(id1);
3402 if (s->id1 == NULL)
3403 return got_error_from_errno("got_object_id_dup");
3404 } else
3405 s->id1 = NULL;
3407 s->id2 = got_object_id_dup(id2);
3408 if (s->id2 == NULL) {
3409 free(s->id1);
3410 s->id1 = NULL;
3411 return got_error_from_errno("got_object_id_dup");
3413 s->f = NULL;
3414 s->first_displayed_line = 1;
3415 s->last_displayed_line = view->nlines;
3416 s->diff_context = diff_context;
3417 s->ignore_whitespace = ignore_whitespace;
3418 s->force_text_diff = force_text_diff;
3419 s->log_view = log_view;
3420 s->repo = repo;
3422 SIMPLEQ_INIT(&s->colors);
3423 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3424 err = add_color(&s->colors,
3425 "^-", TOG_COLOR_DIFF_MINUS,
3426 get_color_value("TOG_COLOR_DIFF_MINUS"));
3427 if (err)
3428 return err;
3429 err = add_color(&s->colors, "^\\+",
3430 TOG_COLOR_DIFF_PLUS,
3431 get_color_value("TOG_COLOR_DIFF_PLUS"));
3432 if (err) {
3433 free_colors(&s->colors);
3434 return err;
3436 err = add_color(&s->colors,
3437 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3438 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3439 if (err) {
3440 free_colors(&s->colors);
3441 return err;
3444 err = add_color(&s->colors,
3445 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3446 TOG_COLOR_DIFF_META,
3447 get_color_value("TOG_COLOR_DIFF_META"));
3448 if (err) {
3449 free_colors(&s->colors);
3450 return err;
3453 err = add_color(&s->colors,
3454 "^(from|via): ", TOG_COLOR_AUTHOR,
3455 get_color_value("TOG_COLOR_AUTHOR"));
3456 if (err) {
3457 free_colors(&s->colors);
3458 return err;
3461 err = add_color(&s->colors,
3462 "^date: ", TOG_COLOR_DATE,
3463 get_color_value("TOG_COLOR_DATE"));
3464 if (err) {
3465 free_colors(&s->colors);
3466 return err;
3470 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3471 if (err) {
3472 free(s->id1);
3473 s->id1 = NULL;
3474 free(s->id2);
3475 s->id2 = NULL;
3476 free_colors(&s->colors);
3477 return err;
3480 if (log_view && view_is_splitscreen(view))
3481 show_log_view(log_view); /* draw vborder */
3482 diff_view_indicate_progress(view);
3484 s->line_offsets = NULL;
3485 s->nlines = 0;
3486 err = create_diff(s);
3487 if (err) {
3488 free(s->id1);
3489 s->id1 = NULL;
3490 free(s->id2);
3491 s->id2 = NULL;
3492 free_colors(&s->colors);
3493 got_ref_list_free(&s->refs);
3494 return err;
3497 view->show = show_diff_view;
3498 view->input = input_diff_view;
3499 view->close = close_diff_view;
3500 view->search_start = search_start_diff_view;
3501 view->search_next = search_next_diff_view;
3503 return NULL;
3506 static const struct got_error *
3507 close_diff_view(struct tog_view *view)
3509 const struct got_error *err = NULL;
3510 struct tog_diff_view_state *s = &view->state.diff;
3512 free(s->id1);
3513 s->id1 = NULL;
3514 free(s->id2);
3515 s->id2 = NULL;
3516 if (s->f && fclose(s->f) == EOF)
3517 err = got_error_from_errno("fclose");
3518 free_colors(&s->colors);
3519 free(s->line_offsets);
3520 s->line_offsets = NULL;
3521 s->nlines = 0;
3522 got_ref_list_free(&s->refs);
3523 return err;
3526 static const struct got_error *
3527 show_diff_view(struct tog_view *view)
3529 const struct got_error *err;
3530 struct tog_diff_view_state *s = &view->state.diff;
3531 char *id_str1 = NULL, *id_str2, *header;
3532 const char *label1, *label2;
3534 if (s->id1) {
3535 err = got_object_id_str(&id_str1, s->id1);
3536 if (err)
3537 return err;
3538 label1 = s->label1 ? : id_str1;
3539 } else
3540 label1 = "/dev/null";
3542 err = got_object_id_str(&id_str2, s->id2);
3543 if (err)
3544 return err;
3545 label2 = s->label2 ? : id_str2;
3547 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3548 err = got_error_from_errno("asprintf");
3549 free(id_str1);
3550 free(id_str2);
3551 return err;
3553 free(id_str1);
3554 free(id_str2);
3556 return draw_file(view, header);
3559 static const struct got_error *
3560 set_selected_commit(struct tog_diff_view_state *s,
3561 struct commit_queue_entry *entry)
3563 const struct got_error *err;
3564 const struct got_object_id_queue *parent_ids;
3565 struct got_commit_object *selected_commit;
3566 struct got_object_qid *pid;
3568 free(s->id2);
3569 s->id2 = got_object_id_dup(entry->id);
3570 if (s->id2 == NULL)
3571 return got_error_from_errno("got_object_id_dup");
3573 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3574 if (err)
3575 return err;
3576 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3577 free(s->id1);
3578 pid = SIMPLEQ_FIRST(parent_ids);
3579 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3580 got_object_commit_close(selected_commit);
3581 return NULL;
3584 static const struct got_error *
3585 input_diff_view(struct tog_view **new_view, struct tog_view **focus_view,
3586 struct tog_view *view, int ch)
3588 const struct got_error *err = NULL;
3589 struct tog_diff_view_state *s = &view->state.diff;
3590 struct tog_log_view_state *ls;
3591 struct commit_queue_entry *entry;
3592 int i;
3594 switch (ch) {
3595 case 'a':
3596 case 'w':
3597 if (ch == 'a')
3598 s->force_text_diff = !s->force_text_diff;
3599 if (ch == 'w')
3600 s->ignore_whitespace = !s->ignore_whitespace;
3601 wclear(view->window);
3602 s->first_displayed_line = 1;
3603 s->last_displayed_line = view->nlines;
3604 diff_view_indicate_progress(view);
3605 err = create_diff(s);
3606 break;
3607 case 'k':
3608 case KEY_UP:
3609 if (s->first_displayed_line > 1)
3610 s->first_displayed_line--;
3611 break;
3612 case KEY_PPAGE:
3613 case CTRL('b'):
3614 if (s->first_displayed_line == 1)
3615 break;
3616 i = 0;
3617 while (i++ < view->nlines - 1 &&
3618 s->first_displayed_line > 1)
3619 s->first_displayed_line--;
3620 break;
3621 case 'j':
3622 case KEY_DOWN:
3623 if (!s->eof)
3624 s->first_displayed_line++;
3625 break;
3626 case KEY_NPAGE:
3627 case CTRL('f'):
3628 case ' ':
3629 if (s->eof)
3630 break;
3631 i = 0;
3632 while (!s->eof && i++ < view->nlines - 1) {
3633 char *line;
3634 line = parse_next_line(s->f, NULL);
3635 s->first_displayed_line++;
3636 if (line == NULL)
3637 break;
3639 break;
3640 case '[':
3641 if (s->diff_context > 0) {
3642 s->diff_context--;
3643 diff_view_indicate_progress(view);
3644 err = create_diff(s);
3645 if (s->first_displayed_line + view->nlines - 1 >
3646 s->nlines) {
3647 s->first_displayed_line = 1;
3648 s->last_displayed_line = view->nlines;
3651 break;
3652 case ']':
3653 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3654 s->diff_context++;
3655 diff_view_indicate_progress(view);
3656 err = create_diff(s);
3658 break;
3659 case '<':
3660 case ',':
3661 if (s->log_view == NULL)
3662 break;
3663 ls = &s->log_view->state.log;
3664 entry = TAILQ_PREV(ls->selected_entry,
3665 commit_queue_head, entry);
3666 if (entry == NULL)
3667 break;
3669 err = input_log_view(NULL, NULL, s->log_view, KEY_UP);
3670 if (err)
3671 break;
3673 err = set_selected_commit(s, entry);
3674 if (err)
3675 break;
3677 s->first_displayed_line = 1;
3678 s->last_displayed_line = view->nlines;
3680 diff_view_indicate_progress(view);
3681 err = create_diff(s);
3682 break;
3683 case '>':
3684 case '.':
3685 if (s->log_view == NULL)
3686 break;
3687 ls = &s->log_view->state.log;
3689 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3690 ls->thread_args.commits_needed++;
3691 err = trigger_log_thread(s->log_view, 1);
3692 if (err)
3693 break;
3695 err = input_log_view(NULL, NULL, s->log_view, KEY_DOWN);
3696 if (err)
3697 break;
3699 entry = TAILQ_NEXT(ls->selected_entry, entry);
3700 if (entry == NULL)
3701 break;
3703 err = set_selected_commit(s, entry);
3704 if (err)
3705 break;
3707 s->first_displayed_line = 1;
3708 s->last_displayed_line = view->nlines;
3710 diff_view_indicate_progress(view);
3711 err = create_diff(s);
3712 break;
3713 default:
3714 break;
3717 return err;
3720 static const struct got_error *
3721 cmd_diff(int argc, char *argv[])
3723 const struct got_error *error = NULL;
3724 struct got_repository *repo = NULL;
3725 struct got_worktree *worktree = NULL;
3726 struct got_object_id *id1 = NULL, *id2 = NULL;
3727 char *repo_path = NULL, *cwd = NULL;
3728 char *id_str1 = NULL, *id_str2 = NULL;
3729 char *label1 = NULL, *label2 = NULL;
3730 int diff_context = 3, ignore_whitespace = 0;
3731 int ch, force_text_diff = 0;
3732 const char *errstr;
3733 struct tog_view *view;
3735 #ifndef PROFILE
3736 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3737 NULL) == -1)
3738 err(1, "pledge");
3739 #endif
3740 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3741 switch (ch) {
3742 case 'a':
3743 force_text_diff = 1;
3744 break;
3745 case 'C':
3746 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3747 &errstr);
3748 if (errstr != NULL)
3749 err(1, "-C option %s", errstr);
3750 break;
3751 case 'r':
3752 repo_path = realpath(optarg, NULL);
3753 if (repo_path == NULL)
3754 return got_error_from_errno2("realpath",
3755 optarg);
3756 got_path_strip_trailing_slashes(repo_path);
3757 break;
3758 case 'w':
3759 ignore_whitespace = 1;
3760 break;
3761 default:
3762 usage_diff();
3763 /* NOTREACHED */
3767 argc -= optind;
3768 argv += optind;
3770 if (argc == 0) {
3771 usage_diff(); /* TODO show local worktree changes */
3772 } else if (argc == 2) {
3773 id_str1 = argv[0];
3774 id_str2 = argv[1];
3775 } else
3776 usage_diff();
3778 cwd = getcwd(NULL, 0);
3779 if (cwd == NULL)
3780 return got_error_from_errno("getcwd");
3782 error = got_worktree_open(&worktree, cwd);
3783 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3784 goto done;
3786 if (repo_path == NULL) {
3787 if (worktree)
3788 repo_path =
3789 strdup(got_worktree_get_repo_path(worktree));
3790 else
3791 repo_path = strdup(cwd);
3793 if (repo_path == NULL) {
3794 error = got_error_from_errno("strdup");
3795 goto done;
3798 error = got_repo_open(&repo, repo_path, NULL);
3799 if (error)
3800 goto done;
3802 init_curses();
3804 error = apply_unveil(got_repo_get_path(repo), NULL);
3805 if (error)
3806 goto done;
3808 error = got_repo_match_object_id(&id1, &label1, id_str1,
3809 GOT_OBJ_TYPE_ANY, 1, repo);
3810 if (error)
3811 goto done;
3813 error = got_repo_match_object_id(&id2, &label2, id_str2,
3814 GOT_OBJ_TYPE_ANY, 1, repo);
3815 if (error)
3816 goto done;
3818 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3819 if (view == NULL) {
3820 error = got_error_from_errno("view_open");
3821 goto done;
3823 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3824 ignore_whitespace, force_text_diff, NULL, repo);
3825 if (error)
3826 goto done;
3827 error = view_loop(view);
3828 done:
3829 free(label1);
3830 free(label2);
3831 free(repo_path);
3832 free(cwd);
3833 if (repo)
3834 got_repo_close(repo);
3835 if (worktree)
3836 got_worktree_close(worktree);
3837 return error;
3840 __dead static void
3841 usage_blame(void)
3843 endwin();
3844 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3845 getprogname());
3846 exit(1);
3849 struct tog_blame_line {
3850 int annotated;
3851 struct got_object_id *id;
3854 static const struct got_error *
3855 draw_blame(struct tog_view *view)
3857 struct tog_blame_view_state *s = &view->state.blame;
3858 struct tog_blame *blame = &s->blame;
3859 regmatch_t *regmatch = &view->regmatch;
3860 const struct got_error *err;
3861 int lineno = 0, nprinted = 0;
3862 char *line;
3863 size_t len;
3864 wchar_t *wline;
3865 int width;
3866 struct tog_blame_line *blame_line;
3867 struct got_object_id *prev_id = NULL;
3868 char *id_str;
3869 struct tog_color *tc;
3871 err = got_object_id_str(&id_str, s->blamed_commit->id);
3872 if (err)
3873 return err;
3875 rewind(blame->f);
3876 werase(view->window);
3878 if (asprintf(&line, "commit %s", id_str) == -1) {
3879 err = got_error_from_errno("asprintf");
3880 free(id_str);
3881 return err;
3884 err = format_line(&wline, &width, line, view->ncols, 0);
3885 free(line);
3886 line = NULL;
3887 if (err)
3888 return err;
3889 if (view_needs_focus_indication(view))
3890 wstandout(view->window);
3891 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3892 if (tc)
3893 wattr_on(view->window,
3894 COLOR_PAIR(tc->colorpair), NULL);
3895 waddwstr(view->window, wline);
3896 if (tc)
3897 wattr_off(view->window,
3898 COLOR_PAIR(tc->colorpair), NULL);
3899 if (view_needs_focus_indication(view))
3900 wstandend(view->window);
3901 free(wline);
3902 wline = NULL;
3903 if (width < view->ncols - 1)
3904 waddch(view->window, '\n');
3906 if (asprintf(&line, "[%d/%d] %s%s",
3907 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3908 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3909 free(id_str);
3910 return got_error_from_errno("asprintf");
3912 free(id_str);
3913 err = format_line(&wline, &width, line, view->ncols, 0);
3914 free(line);
3915 line = NULL;
3916 if (err)
3917 return err;
3918 waddwstr(view->window, wline);
3919 free(wline);
3920 wline = NULL;
3921 if (width < view->ncols - 1)
3922 waddch(view->window, '\n');
3924 s->eof = 0;
3925 while (nprinted < view->nlines - 2) {
3926 line = parse_next_line(blame->f, &len);
3927 if (line == NULL) {
3928 s->eof = 1;
3929 break;
3931 if (++lineno < s->first_displayed_line) {
3932 free(line);
3933 continue;
3936 if (view->focussed && nprinted == s->selected_line - 1)
3937 wstandout(view->window);
3939 if (blame->nlines > 0) {
3940 blame_line = &blame->lines[lineno - 1];
3941 if (blame_line->annotated && prev_id &&
3942 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3943 !(view->focussed &&
3944 nprinted == s->selected_line - 1)) {
3945 waddstr(view->window, " ");
3946 } else if (blame_line->annotated) {
3947 char *id_str;
3948 err = got_object_id_str(&id_str, blame_line->id);
3949 if (err) {
3950 free(line);
3951 return err;
3953 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3954 if (tc)
3955 wattr_on(view->window,
3956 COLOR_PAIR(tc->colorpair), NULL);
3957 wprintw(view->window, "%.8s", id_str);
3958 if (tc)
3959 wattr_off(view->window,
3960 COLOR_PAIR(tc->colorpair), NULL);
3961 free(id_str);
3962 prev_id = blame_line->id;
3963 } else {
3964 waddstr(view->window, "........");
3965 prev_id = NULL;
3967 } else {
3968 waddstr(view->window, "........");
3969 prev_id = NULL;
3972 if (view->focussed && nprinted == s->selected_line - 1)
3973 wstandend(view->window);
3974 waddstr(view->window, " ");
3976 if (view->ncols <= 9) {
3977 width = 9;
3978 wline = wcsdup(L"");
3979 if (wline == NULL) {
3980 err = got_error_from_errno("wcsdup");
3981 free(line);
3982 return err;
3984 } else if (s->first_displayed_line + nprinted ==
3985 s->matched_line &&
3986 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3987 err = add_matched_line(&width, line, view->ncols - 9, 9,
3988 view->window, regmatch);
3989 if (err) {
3990 free(line);
3991 return err;
3993 width += 9;
3994 } else {
3995 err = format_line(&wline, &width, line,
3996 view->ncols - 9, 9);
3997 waddwstr(view->window, wline);
3998 free(wline);
3999 wline = NULL;
4000 width += 9;
4003 if (width <= view->ncols - 1)
4004 waddch(view->window, '\n');
4005 if (++nprinted == 1)
4006 s->first_displayed_line = lineno;
4007 free(line);
4009 s->last_displayed_line = lineno;
4011 view_vborder(view);
4013 return NULL;
4016 static const struct got_error *
4017 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4019 const struct got_error *err = NULL;
4020 struct tog_blame_cb_args *a = arg;
4021 struct tog_blame_line *line;
4022 int errcode;
4024 if (nlines != a->nlines ||
4025 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4026 return got_error(GOT_ERR_RANGE);
4028 errcode = pthread_mutex_lock(&tog_mutex);
4029 if (errcode)
4030 return got_error_set_errno(errcode, "pthread_mutex_lock");
4032 if (*a->quit) { /* user has quit the blame view */
4033 err = got_error(GOT_ERR_ITER_COMPLETED);
4034 goto done;
4037 if (lineno == -1)
4038 goto done; /* no change in this commit */
4040 line = &a->lines[lineno - 1];
4041 if (line->annotated)
4042 goto done;
4044 line->id = got_object_id_dup(id);
4045 if (line->id == NULL) {
4046 err = got_error_from_errno("got_object_id_dup");
4047 goto done;
4049 line->annotated = 1;
4050 done:
4051 errcode = pthread_mutex_unlock(&tog_mutex);
4052 if (errcode)
4053 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4054 return err;
4057 static void *
4058 blame_thread(void *arg)
4060 const struct got_error *err;
4061 struct tog_blame_thread_args *ta = arg;
4062 struct tog_blame_cb_args *a = ta->cb_args;
4063 int errcode;
4065 err = block_signals_used_by_main_thread();
4066 if (err)
4067 return (void *)err;
4069 err = got_blame(ta->path, a->commit_id, ta->repo,
4070 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4071 if (err && err->code == GOT_ERR_CANCELLED)
4072 err = NULL;
4074 errcode = pthread_mutex_lock(&tog_mutex);
4075 if (errcode)
4076 return (void *)got_error_set_errno(errcode,
4077 "pthread_mutex_lock");
4079 got_repo_close(ta->repo);
4080 ta->repo = NULL;
4081 *ta->complete = 1;
4083 errcode = pthread_mutex_unlock(&tog_mutex);
4084 if (errcode && err == NULL)
4085 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4087 return (void *)err;
4090 static struct got_object_id *
4091 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4092 int first_displayed_line, int selected_line)
4094 struct tog_blame_line *line;
4096 if (nlines <= 0)
4097 return NULL;
4099 line = &lines[first_displayed_line - 1 + selected_line - 1];
4100 if (!line->annotated)
4101 return NULL;
4103 return line->id;
4106 static const struct got_error *
4107 stop_blame(struct tog_blame *blame)
4109 const struct got_error *err = NULL;
4110 int i;
4112 if (blame->thread) {
4113 int errcode;
4114 errcode = pthread_mutex_unlock(&tog_mutex);
4115 if (errcode)
4116 return got_error_set_errno(errcode,
4117 "pthread_mutex_unlock");
4118 errcode = pthread_join(blame->thread, (void **)&err);
4119 if (errcode)
4120 return got_error_set_errno(errcode, "pthread_join");
4121 errcode = pthread_mutex_lock(&tog_mutex);
4122 if (errcode)
4123 return got_error_set_errno(errcode,
4124 "pthread_mutex_lock");
4125 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4126 err = NULL;
4127 blame->thread = NULL;
4129 if (blame->thread_args.repo) {
4130 got_repo_close(blame->thread_args.repo);
4131 blame->thread_args.repo = NULL;
4133 if (blame->f) {
4134 if (fclose(blame->f) != 0 && err == NULL)
4135 err = got_error_from_errno("fclose");
4136 blame->f = NULL;
4138 if (blame->lines) {
4139 for (i = 0; i < blame->nlines; i++)
4140 free(blame->lines[i].id);
4141 free(blame->lines);
4142 blame->lines = NULL;
4144 free(blame->cb_args.commit_id);
4145 blame->cb_args.commit_id = NULL;
4147 return err;
4150 static const struct got_error *
4151 cancel_blame_view(void *arg)
4153 const struct got_error *err = NULL;
4154 int *done = arg;
4155 int errcode;
4157 errcode = pthread_mutex_lock(&tog_mutex);
4158 if (errcode)
4159 return got_error_set_errno(errcode,
4160 "pthread_mutex_unlock");
4162 if (*done)
4163 err = got_error(GOT_ERR_CANCELLED);
4165 errcode = pthread_mutex_unlock(&tog_mutex);
4166 if (errcode)
4167 return got_error_set_errno(errcode,
4168 "pthread_mutex_lock");
4170 return err;
4173 static const struct got_error *
4174 run_blame(struct tog_view *view)
4176 struct tog_blame_view_state *s = &view->state.blame;
4177 struct tog_blame *blame = &s->blame;
4178 const struct got_error *err = NULL;
4179 struct got_blob_object *blob = NULL;
4180 struct got_repository *thread_repo = NULL;
4181 struct got_object_id *obj_id = NULL;
4182 int obj_type;
4184 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4185 s->path);
4186 if (err)
4187 return err;
4189 err = got_object_get_type(&obj_type, s->repo, obj_id);
4190 if (err)
4191 goto done;
4193 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4194 err = got_error(GOT_ERR_OBJ_TYPE);
4195 goto done;
4198 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4199 if (err)
4200 goto done;
4201 blame->f = got_opentemp();
4202 if (blame->f == NULL) {
4203 err = got_error_from_errno("got_opentemp");
4204 goto done;
4206 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4207 &blame->line_offsets, blame->f, blob);
4208 if (err || blame->nlines == 0)
4209 goto done;
4211 /* Don't include \n at EOF in the blame line count. */
4212 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4213 blame->nlines--;
4215 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4216 if (blame->lines == NULL) {
4217 err = got_error_from_errno("calloc");
4218 goto done;
4221 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4222 if (err)
4223 goto done;
4225 blame->cb_args.view = view;
4226 blame->cb_args.lines = blame->lines;
4227 blame->cb_args.nlines = blame->nlines;
4228 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4229 if (blame->cb_args.commit_id == NULL) {
4230 err = got_error_from_errno("got_object_id_dup");
4231 goto done;
4233 blame->cb_args.quit = &s->done;
4235 blame->thread_args.path = s->path;
4236 blame->thread_args.repo = thread_repo;
4237 blame->thread_args.cb_args = &blame->cb_args;
4238 blame->thread_args.complete = &s->blame_complete;
4239 blame->thread_args.cancel_cb = cancel_blame_view;
4240 blame->thread_args.cancel_arg = &s->done;
4241 s->blame_complete = 0;
4243 done:
4244 if (blob)
4245 got_object_blob_close(blob);
4246 free(obj_id);
4247 if (err)
4248 stop_blame(blame);
4249 return err;
4252 static const struct got_error *
4253 open_blame_view(struct tog_view *view, char *path,
4254 struct got_object_id *commit_id, struct got_repository *repo)
4256 const struct got_error *err = NULL;
4257 struct tog_blame_view_state *s = &view->state.blame;
4259 SIMPLEQ_INIT(&s->blamed_commits);
4261 s->path = strdup(path);
4262 if (s->path == NULL)
4263 return got_error_from_errno("strdup");
4265 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4266 if (err) {
4267 free(s->path);
4268 return err;
4271 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4272 s->first_displayed_line = 1;
4273 s->last_displayed_line = view->nlines;
4274 s->selected_line = 1;
4275 s->blame_complete = 0;
4276 s->repo = repo;
4277 s->commit_id = commit_id;
4278 memset(&s->blame, 0, sizeof(s->blame));
4280 SIMPLEQ_INIT(&s->colors);
4281 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4282 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4283 get_color_value("TOG_COLOR_COMMIT"));
4284 if (err)
4285 return err;
4288 view->show = show_blame_view;
4289 view->input = input_blame_view;
4290 view->close = close_blame_view;
4291 view->search_start = search_start_blame_view;
4292 view->search_next = search_next_blame_view;
4294 return run_blame(view);
4297 static const struct got_error *
4298 close_blame_view(struct tog_view *view)
4300 const struct got_error *err = NULL;
4301 struct tog_blame_view_state *s = &view->state.blame;
4303 if (s->blame.thread)
4304 err = stop_blame(&s->blame);
4306 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4307 struct got_object_qid *blamed_commit;
4308 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4309 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4310 got_object_qid_free(blamed_commit);
4313 free(s->path);
4314 free_colors(&s->colors);
4316 return err;
4319 static const struct got_error *
4320 search_start_blame_view(struct tog_view *view)
4322 struct tog_blame_view_state *s = &view->state.blame;
4324 s->matched_line = 0;
4325 return NULL;
4328 static const struct got_error *
4329 search_next_blame_view(struct tog_view *view)
4331 struct tog_blame_view_state *s = &view->state.blame;
4332 int lineno;
4334 if (!view->searching) {
4335 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4336 return NULL;
4339 if (s->matched_line) {
4340 if (view->searching == TOG_SEARCH_FORWARD)
4341 lineno = s->matched_line + 1;
4342 else
4343 lineno = s->matched_line - 1;
4344 } else {
4345 if (view->searching == TOG_SEARCH_FORWARD)
4346 lineno = 1;
4347 else
4348 lineno = s->blame.nlines;
4351 while (1) {
4352 char *line = NULL;
4353 off_t offset;
4354 size_t len;
4356 if (lineno <= 0 || lineno > s->blame.nlines) {
4357 if (s->matched_line == 0) {
4358 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4359 free(line);
4360 break;
4363 if (view->searching == TOG_SEARCH_FORWARD)
4364 lineno = 1;
4365 else
4366 lineno = s->blame.nlines;
4369 offset = s->blame.line_offsets[lineno - 1];
4370 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4371 free(line);
4372 return got_error_from_errno("fseeko");
4374 free(line);
4375 line = parse_next_line(s->blame.f, &len);
4376 if (line &&
4377 match_line(line, &view->regex, 1, &view->regmatch)) {
4378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4379 s->matched_line = lineno;
4380 free(line);
4381 break;
4383 free(line);
4384 if (view->searching == TOG_SEARCH_FORWARD)
4385 lineno++;
4386 else
4387 lineno--;
4390 if (s->matched_line) {
4391 s->first_displayed_line = s->matched_line;
4392 s->selected_line = 1;
4395 return NULL;
4398 static const struct got_error *
4399 show_blame_view(struct tog_view *view)
4401 const struct got_error *err = NULL;
4402 struct tog_blame_view_state *s = &view->state.blame;
4403 int errcode;
4405 if (s->blame.thread == NULL) {
4406 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4407 &s->blame.thread_args);
4408 if (errcode)
4409 return got_error_set_errno(errcode, "pthread_create");
4411 halfdelay(1); /* fast refresh while annotating */
4414 if (s->blame_complete)
4415 halfdelay(10); /* disable fast refresh */
4417 err = draw_blame(view);
4419 view_vborder(view);
4420 return err;
4423 static const struct got_error *
4424 input_blame_view(struct tog_view **new_view, struct tog_view **focus_view,
4425 struct tog_view *view, int ch)
4427 const struct got_error *err = NULL, *thread_err = NULL;
4428 struct tog_view *diff_view;
4429 struct tog_blame_view_state *s = &view->state.blame;
4430 int begin_x = 0;
4432 switch (ch) {
4433 case 'q':
4434 s->done = 1;
4435 break;
4436 case 'k':
4437 case KEY_UP:
4438 if (s->selected_line > 1)
4439 s->selected_line--;
4440 else if (s->selected_line == 1 &&
4441 s->first_displayed_line > 1)
4442 s->first_displayed_line--;
4443 break;
4444 case KEY_PPAGE:
4445 case CTRL('b'):
4446 if (s->first_displayed_line == 1) {
4447 s->selected_line = 1;
4448 break;
4450 if (s->first_displayed_line > view->nlines - 2)
4451 s->first_displayed_line -=
4452 (view->nlines - 2);
4453 else
4454 s->first_displayed_line = 1;
4455 break;
4456 case 'j':
4457 case KEY_DOWN:
4458 if (s->selected_line < view->nlines - 2 &&
4459 s->first_displayed_line +
4460 s->selected_line <= s->blame.nlines)
4461 s->selected_line++;
4462 else if (s->last_displayed_line <
4463 s->blame.nlines)
4464 s->first_displayed_line++;
4465 break;
4466 case 'b':
4467 case 'p': {
4468 struct got_object_id *id = NULL;
4469 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4470 s->first_displayed_line, s->selected_line);
4471 if (id == NULL)
4472 break;
4473 if (ch == 'p') {
4474 struct got_commit_object *commit;
4475 struct got_object_qid *pid;
4476 struct got_object_id *blob_id = NULL;
4477 int obj_type;
4478 err = got_object_open_as_commit(&commit,
4479 s->repo, id);
4480 if (err)
4481 break;
4482 pid = SIMPLEQ_FIRST(
4483 got_object_commit_get_parent_ids(commit));
4484 if (pid == NULL) {
4485 got_object_commit_close(commit);
4486 break;
4488 /* Check if path history ends here. */
4489 err = got_object_id_by_path(&blob_id, s->repo,
4490 pid->id, s->path);
4491 if (err) {
4492 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4493 err = NULL;
4494 got_object_commit_close(commit);
4495 break;
4497 err = got_object_get_type(&obj_type, s->repo,
4498 blob_id);
4499 free(blob_id);
4500 /* Can't blame non-blob type objects. */
4501 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4502 got_object_commit_close(commit);
4503 break;
4505 err = got_object_qid_alloc(&s->blamed_commit,
4506 pid->id);
4507 got_object_commit_close(commit);
4508 } else {
4509 if (got_object_id_cmp(id,
4510 s->blamed_commit->id) == 0)
4511 break;
4512 err = got_object_qid_alloc(&s->blamed_commit,
4513 id);
4515 if (err)
4516 break;
4517 s->done = 1;
4518 thread_err = stop_blame(&s->blame);
4519 s->done = 0;
4520 if (thread_err)
4521 break;
4522 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4523 s->blamed_commit, entry);
4524 err = run_blame(view);
4525 if (err)
4526 break;
4527 break;
4529 case 'B': {
4530 struct got_object_qid *first;
4531 first = SIMPLEQ_FIRST(&s->blamed_commits);
4532 if (!got_object_id_cmp(first->id, s->commit_id))
4533 break;
4534 s->done = 1;
4535 thread_err = stop_blame(&s->blame);
4536 s->done = 0;
4537 if (thread_err)
4538 break;
4539 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4540 got_object_qid_free(s->blamed_commit);
4541 s->blamed_commit =
4542 SIMPLEQ_FIRST(&s->blamed_commits);
4543 err = run_blame(view);
4544 if (err)
4545 break;
4546 break;
4548 case KEY_ENTER:
4549 case '\r': {
4550 struct got_object_id *id = NULL;
4551 struct got_object_qid *pid;
4552 struct got_commit_object *commit = NULL;
4553 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4554 s->first_displayed_line, s->selected_line);
4555 if (id == NULL)
4556 break;
4557 err = got_object_open_as_commit(&commit, s->repo, id);
4558 if (err)
4559 break;
4560 pid = SIMPLEQ_FIRST(
4561 got_object_commit_get_parent_ids(commit));
4562 if (view_is_parent_view(view))
4563 begin_x = view_split_begin_x(view->begin_x);
4564 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4565 if (diff_view == NULL) {
4566 got_object_commit_close(commit);
4567 err = got_error_from_errno("view_open");
4568 break;
4570 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4571 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4572 got_object_commit_close(commit);
4573 if (err) {
4574 view_close(diff_view);
4575 break;
4577 if (view_is_parent_view(view)) {
4578 err = view_close_child(view);
4579 if (err)
4580 break;
4581 view_set_child(view, diff_view);
4582 *focus_view = diff_view;
4583 } else
4584 *new_view = diff_view;
4585 if (err)
4586 break;
4587 break;
4589 case KEY_NPAGE:
4590 case CTRL('f'):
4591 case ' ':
4592 if (s->last_displayed_line >= s->blame.nlines &&
4593 s->selected_line >= MIN(s->blame.nlines,
4594 view->nlines - 2)) {
4595 break;
4597 if (s->last_displayed_line >= s->blame.nlines &&
4598 s->selected_line < view->nlines - 2) {
4599 s->selected_line = MIN(s->blame.nlines,
4600 view->nlines - 2);
4601 break;
4603 if (s->last_displayed_line + view->nlines - 2
4604 <= s->blame.nlines)
4605 s->first_displayed_line +=
4606 view->nlines - 2;
4607 else
4608 s->first_displayed_line =
4609 s->blame.nlines -
4610 (view->nlines - 3);
4611 break;
4612 case KEY_RESIZE:
4613 if (s->selected_line > view->nlines - 2) {
4614 s->selected_line = MIN(s->blame.nlines,
4615 view->nlines - 2);
4617 break;
4618 default:
4619 break;
4621 return thread_err ? thread_err : err;
4624 static const struct got_error *
4625 cmd_blame(int argc, char *argv[])
4627 const struct got_error *error;
4628 struct got_repository *repo = NULL;
4629 struct got_worktree *worktree = NULL;
4630 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4631 char *link_target = NULL;
4632 struct got_object_id *commit_id = NULL;
4633 char *commit_id_str = NULL;
4634 int ch;
4635 struct tog_view *view;
4637 #ifndef PROFILE
4638 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4639 NULL) == -1)
4640 err(1, "pledge");
4641 #endif
4643 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4644 switch (ch) {
4645 case 'c':
4646 commit_id_str = optarg;
4647 break;
4648 case 'r':
4649 repo_path = realpath(optarg, NULL);
4650 if (repo_path == NULL)
4651 return got_error_from_errno2("realpath",
4652 optarg);
4653 break;
4654 default:
4655 usage_blame();
4656 /* NOTREACHED */
4660 argc -= optind;
4661 argv += optind;
4663 if (argc != 1)
4664 usage_blame();
4666 cwd = getcwd(NULL, 0);
4667 if (cwd == NULL)
4668 return got_error_from_errno("getcwd");
4670 error = got_worktree_open(&worktree, cwd);
4671 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4672 goto done;
4674 if (repo_path == NULL) {
4675 if (worktree)
4676 repo_path =
4677 strdup(got_worktree_get_repo_path(worktree));
4678 else
4679 repo_path = strdup(cwd);
4681 if (repo_path == NULL) {
4682 error = got_error_from_errno("strdup");
4683 goto done;
4686 error = got_repo_open(&repo, repo_path, NULL);
4687 if (error != NULL)
4688 goto done;
4690 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4691 worktree);
4692 if (error)
4693 goto done;
4695 init_curses();
4697 error = apply_unveil(got_repo_get_path(repo), NULL);
4698 if (error)
4699 goto done;
4701 if (commit_id_str == NULL) {
4702 struct got_reference *head_ref;
4703 error = got_ref_open(&head_ref, repo, worktree ?
4704 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4705 if (error != NULL)
4706 goto done;
4707 error = got_ref_resolve(&commit_id, repo, head_ref);
4708 got_ref_close(head_ref);
4709 } else {
4710 error = got_repo_match_object_id(&commit_id, NULL,
4711 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4713 if (error != NULL)
4714 goto done;
4716 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4717 if (view == NULL) {
4718 error = got_error_from_errno("view_open");
4719 goto done;
4722 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4723 commit_id, repo);
4724 if (error)
4725 goto done;
4727 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4728 commit_id, repo);
4729 if (error)
4730 goto done;
4731 if (worktree) {
4732 /* Release work tree lock. */
4733 got_worktree_close(worktree);
4734 worktree = NULL;
4736 error = view_loop(view);
4737 done:
4738 free(repo_path);
4739 free(in_repo_path);
4740 free(link_target);
4741 free(cwd);
4742 free(commit_id);
4743 if (worktree)
4744 got_worktree_close(worktree);
4745 if (repo)
4746 got_repo_close(repo);
4747 return error;
4750 static const struct got_error *
4751 draw_tree_entries(struct tog_view *view, const char *parent_path)
4753 struct tog_tree_view_state *s = &view->state.tree;
4754 const struct got_error *err = NULL;
4755 struct got_tree_entry *te;
4756 wchar_t *wline;
4757 struct tog_color *tc;
4758 int width, n, i, nentries;
4759 int limit = view->nlines;
4761 s->ndisplayed = 0;
4763 werase(view->window);
4765 if (limit == 0)
4766 return NULL;
4768 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4769 if (err)
4770 return err;
4771 if (view_needs_focus_indication(view))
4772 wstandout(view->window);
4773 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4774 if (tc)
4775 wattr_on(view->window,
4776 COLOR_PAIR(tc->colorpair), NULL);
4777 waddwstr(view->window, wline);
4778 if (tc)
4779 wattr_off(view->window,
4780 COLOR_PAIR(tc->colorpair), NULL);
4781 if (view_needs_focus_indication(view))
4782 wstandend(view->window);
4783 free(wline);
4784 wline = NULL;
4785 if (width < view->ncols - 1)
4786 waddch(view->window, '\n');
4787 if (--limit <= 0)
4788 return NULL;
4789 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4790 if (err)
4791 return err;
4792 waddwstr(view->window, wline);
4793 free(wline);
4794 wline = NULL;
4795 if (width < view->ncols - 1)
4796 waddch(view->window, '\n');
4797 if (--limit <= 0)
4798 return NULL;
4799 waddch(view->window, '\n');
4800 if (--limit <= 0)
4801 return NULL;
4803 if (s->first_displayed_entry == NULL) {
4804 te = got_object_tree_get_first_entry(s->tree);
4805 if (s->selected == 0) {
4806 if (view->focussed)
4807 wstandout(view->window);
4808 s->selected_entry = NULL;
4810 waddstr(view->window, " ..\n"); /* parent directory */
4811 if (s->selected == 0 && view->focussed)
4812 wstandend(view->window);
4813 s->ndisplayed++;
4814 if (--limit <= 0)
4815 return NULL;
4816 n = 1;
4817 } else {
4818 n = 0;
4819 te = s->first_displayed_entry;
4822 nentries = got_object_tree_get_nentries(s->tree);
4823 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4824 char *line = NULL, *id_str = NULL, *link_target = NULL;
4825 const char *modestr = "";
4826 mode_t mode;
4828 te = got_object_tree_get_entry(s->tree, i);
4829 mode = got_tree_entry_get_mode(te);
4831 if (s->show_ids) {
4832 err = got_object_id_str(&id_str,
4833 got_tree_entry_get_id(te));
4834 if (err)
4835 return got_error_from_errno(
4836 "got_object_id_str");
4838 if (got_object_tree_entry_is_submodule(te))
4839 modestr = "$";
4840 else if (S_ISLNK(mode)) {
4841 int i;
4843 err = got_tree_entry_get_symlink_target(&link_target,
4844 te, s->repo);
4845 if (err) {
4846 free(id_str);
4847 return err;
4849 for (i = 0; i < strlen(link_target); i++) {
4850 if (!isprint((unsigned char)link_target[i]))
4851 link_target[i] = '?';
4853 modestr = "@";
4855 else if (S_ISDIR(mode))
4856 modestr = "/";
4857 else if (mode & S_IXUSR)
4858 modestr = "*";
4859 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4860 got_tree_entry_get_name(te), modestr,
4861 link_target ? " -> ": "",
4862 link_target ? link_target : "") == -1) {
4863 free(id_str);
4864 free(link_target);
4865 return got_error_from_errno("asprintf");
4867 free(id_str);
4868 free(link_target);
4869 err = format_line(&wline, &width, line, view->ncols, 0);
4870 if (err) {
4871 free(line);
4872 break;
4874 if (n == s->selected) {
4875 if (view->focussed)
4876 wstandout(view->window);
4877 s->selected_entry = te;
4879 tc = match_color(&s->colors, line);
4880 if (tc)
4881 wattr_on(view->window,
4882 COLOR_PAIR(tc->colorpair), NULL);
4883 waddwstr(view->window, wline);
4884 if (tc)
4885 wattr_off(view->window,
4886 COLOR_PAIR(tc->colorpair), NULL);
4887 if (width < view->ncols - 1)
4888 waddch(view->window, '\n');
4889 if (n == s->selected && view->focussed)
4890 wstandend(view->window);
4891 free(line);
4892 free(wline);
4893 wline = NULL;
4894 n++;
4895 s->ndisplayed++;
4896 s->last_displayed_entry = te;
4897 if (--limit <= 0)
4898 break;
4901 return err;
4904 static void
4905 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4907 struct got_tree_entry *te;
4908 int isroot = s->tree == s->root;
4909 int i = 0;
4911 if (s->first_displayed_entry == NULL)
4912 return;
4914 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4915 while (i++ < maxscroll) {
4916 if (te == NULL) {
4917 if (!isroot)
4918 s->first_displayed_entry = NULL;
4919 break;
4921 s->first_displayed_entry = te;
4922 te = got_tree_entry_get_prev(s->tree, te);
4926 static void
4927 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4929 struct got_tree_entry *next, *last;
4930 int n = 0;
4932 if (s->first_displayed_entry)
4933 next = got_tree_entry_get_next(s->tree,
4934 s->first_displayed_entry);
4935 else
4936 next = got_object_tree_get_first_entry(s->tree);
4938 last = s->last_displayed_entry;
4939 while (next && last && n++ < maxscroll) {
4940 last = got_tree_entry_get_next(s->tree, last);
4941 if (last) {
4942 s->first_displayed_entry = next;
4943 next = got_tree_entry_get_next(s->tree, next);
4948 static const struct got_error *
4949 tree_entry_path(char **path, struct tog_parent_trees *parents,
4950 struct got_tree_entry *te)
4952 const struct got_error *err = NULL;
4953 struct tog_parent_tree *pt;
4954 size_t len = 2; /* for leading slash and NUL */
4956 TAILQ_FOREACH(pt, parents, entry)
4957 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4958 + 1 /* slash */;
4959 if (te)
4960 len += strlen(got_tree_entry_get_name(te));
4962 *path = calloc(1, len);
4963 if (path == NULL)
4964 return got_error_from_errno("calloc");
4966 (*path)[0] = '/';
4967 pt = TAILQ_LAST(parents, tog_parent_trees);
4968 while (pt) {
4969 const char *name = got_tree_entry_get_name(pt->selected_entry);
4970 if (strlcat(*path, name, len) >= len) {
4971 err = got_error(GOT_ERR_NO_SPACE);
4972 goto done;
4974 if (strlcat(*path, "/", len) >= len) {
4975 err = got_error(GOT_ERR_NO_SPACE);
4976 goto done;
4978 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4980 if (te) {
4981 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4982 err = got_error(GOT_ERR_NO_SPACE);
4983 goto done;
4986 done:
4987 if (err) {
4988 free(*path);
4989 *path = NULL;
4991 return err;
4994 static const struct got_error *
4995 blame_tree_entry(struct tog_view **new_view, int begin_x,
4996 struct got_tree_entry *te, struct tog_parent_trees *parents,
4997 struct got_object_id *commit_id, struct got_repository *repo)
4999 const struct got_error *err = NULL;
5000 char *path;
5001 struct tog_view *blame_view;
5003 *new_view = NULL;
5005 err = tree_entry_path(&path, parents, te);
5006 if (err)
5007 return err;
5009 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5010 if (blame_view == NULL) {
5011 err = got_error_from_errno("view_open");
5012 goto done;
5015 err = open_blame_view(blame_view, path, commit_id, repo);
5016 if (err) {
5017 if (err->code == GOT_ERR_CANCELLED)
5018 err = NULL;
5019 view_close(blame_view);
5020 } else
5021 *new_view = blame_view;
5022 done:
5023 free(path);
5024 return err;
5027 static const struct got_error *
5028 log_tree_entry(struct tog_view **new_view, int begin_x,
5029 struct got_tree_entry *te, struct tog_parent_trees *parents,
5030 struct got_object_id *commit_id, struct got_repository *repo)
5032 struct tog_view *log_view;
5033 const struct got_error *err = NULL;
5034 char *path;
5036 *new_view = NULL;
5038 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5039 if (log_view == NULL)
5040 return got_error_from_errno("view_open");
5042 err = tree_entry_path(&path, parents, te);
5043 if (err)
5044 return err;
5046 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5047 if (err)
5048 view_close(log_view);
5049 else
5050 *new_view = log_view;
5051 free(path);
5052 return err;
5055 static const struct got_error *
5056 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5057 struct got_object_id *commit_id, struct got_repository *repo)
5059 const struct got_error *err = NULL;
5060 char *commit_id_str = NULL;
5061 struct tog_tree_view_state *s = &view->state.tree;
5063 TAILQ_INIT(&s->parents);
5065 err = got_object_id_str(&commit_id_str, commit_id);
5066 if (err != NULL)
5067 goto done;
5069 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5070 err = got_error_from_errno("asprintf");
5071 goto done;
5074 s->root = s->tree = root;
5075 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5076 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5077 s->commit_id = got_object_id_dup(commit_id);
5078 if (s->commit_id == NULL) {
5079 err = got_error_from_errno("got_object_id_dup");
5080 goto done;
5082 s->repo = repo;
5084 SIMPLEQ_INIT(&s->colors);
5086 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5087 err = add_color(&s->colors, "\\$$",
5088 TOG_COLOR_TREE_SUBMODULE,
5089 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5090 if (err)
5091 goto done;
5092 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5093 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5094 if (err) {
5095 free_colors(&s->colors);
5096 goto done;
5098 err = add_color(&s->colors, "/$",
5099 TOG_COLOR_TREE_DIRECTORY,
5100 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5101 if (err) {
5102 free_colors(&s->colors);
5103 goto done;
5106 err = add_color(&s->colors, "\\*$",
5107 TOG_COLOR_TREE_EXECUTABLE,
5108 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5109 if (err) {
5110 free_colors(&s->colors);
5111 goto done;
5114 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5115 get_color_value("TOG_COLOR_COMMIT"));
5116 if (err) {
5117 free_colors(&s->colors);
5118 goto done;
5122 view->show = show_tree_view;
5123 view->input = input_tree_view;
5124 view->close = close_tree_view;
5125 view->search_start = search_start_tree_view;
5126 view->search_next = search_next_tree_view;
5127 done:
5128 free(commit_id_str);
5129 if (err) {
5130 free(s->tree_label);
5131 s->tree_label = NULL;
5133 return err;
5136 static const struct got_error *
5137 close_tree_view(struct tog_view *view)
5139 struct tog_tree_view_state *s = &view->state.tree;
5141 free_colors(&s->colors);
5142 free(s->tree_label);
5143 s->tree_label = NULL;
5144 free(s->commit_id);
5145 s->commit_id = NULL;
5146 while (!TAILQ_EMPTY(&s->parents)) {
5147 struct tog_parent_tree *parent;
5148 parent = TAILQ_FIRST(&s->parents);
5149 TAILQ_REMOVE(&s->parents, parent, entry);
5150 free(parent);
5153 if (s->tree != s->root)
5154 got_object_tree_close(s->tree);
5155 got_object_tree_close(s->root);
5156 return NULL;
5159 static const struct got_error *
5160 search_start_tree_view(struct tog_view *view)
5162 struct tog_tree_view_state *s = &view->state.tree;
5164 s->matched_entry = NULL;
5165 return NULL;
5168 static int
5169 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5171 regmatch_t regmatch;
5173 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5174 0) == 0;
5177 static const struct got_error *
5178 search_next_tree_view(struct tog_view *view)
5180 struct tog_tree_view_state *s = &view->state.tree;
5181 struct got_tree_entry *te = NULL;
5183 if (!view->searching) {
5184 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5185 return NULL;
5188 if (s->matched_entry) {
5189 if (view->searching == TOG_SEARCH_FORWARD) {
5190 if (s->selected_entry)
5191 te = got_tree_entry_get_next(s->tree,
5192 s->selected_entry);
5193 else
5194 te = got_object_tree_get_first_entry(s->tree);
5195 } else {
5196 if (s->selected_entry == NULL)
5197 te = got_object_tree_get_last_entry(s->tree);
5198 else
5199 te = got_tree_entry_get_prev(s->tree,
5200 s->selected_entry);
5202 } else {
5203 if (view->searching == TOG_SEARCH_FORWARD)
5204 te = got_object_tree_get_first_entry(s->tree);
5205 else
5206 te = got_object_tree_get_last_entry(s->tree);
5209 while (1) {
5210 if (te == NULL) {
5211 if (s->matched_entry == NULL) {
5212 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5213 return NULL;
5215 if (view->searching == TOG_SEARCH_FORWARD)
5216 te = got_object_tree_get_first_entry(s->tree);
5217 else
5218 te = got_object_tree_get_last_entry(s->tree);
5221 if (match_tree_entry(te, &view->regex)) {
5222 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5223 s->matched_entry = te;
5224 break;
5227 if (view->searching == TOG_SEARCH_FORWARD)
5228 te = got_tree_entry_get_next(s->tree, te);
5229 else
5230 te = got_tree_entry_get_prev(s->tree, te);
5233 if (s->matched_entry) {
5234 s->first_displayed_entry = s->matched_entry;
5235 s->selected = 0;
5238 return NULL;
5241 static const struct got_error *
5242 show_tree_view(struct tog_view *view)
5244 const struct got_error *err = NULL;
5245 struct tog_tree_view_state *s = &view->state.tree;
5246 char *parent_path;
5248 err = tree_entry_path(&parent_path, &s->parents, NULL);
5249 if (err)
5250 return err;
5252 err = draw_tree_entries(view, parent_path);
5253 free(parent_path);
5255 view_vborder(view);
5256 return err;
5259 static const struct got_error *
5260 input_tree_view(struct tog_view **new_view, struct tog_view **focus_view,
5261 struct tog_view *view, int ch)
5263 const struct got_error *err = NULL;
5264 struct tog_tree_view_state *s = &view->state.tree;
5265 struct tog_view *log_view, *ref_view;
5266 int begin_x = 0;
5268 switch (ch) {
5269 case 'i':
5270 s->show_ids = !s->show_ids;
5271 break;
5272 case 'l':
5273 if (!s->selected_entry)
5274 break;
5275 if (view_is_parent_view(view))
5276 begin_x = view_split_begin_x(view->begin_x);
5277 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5278 &s->parents, s->commit_id, s->repo);
5279 if (view_is_parent_view(view)) {
5280 err = view_close_child(view);
5281 if (err)
5282 return err;
5283 view_set_child(view, log_view);
5284 *focus_view = log_view;
5285 } else
5286 *new_view = log_view;
5287 break;
5288 case 'r':
5289 if (view_is_parent_view(view))
5290 begin_x = view_split_begin_x(view->begin_x);
5291 ref_view = view_open(view->nlines, view->ncols,
5292 view->begin_y, begin_x, TOG_VIEW_REF);
5293 if (ref_view == NULL)
5294 return got_error_from_errno("view_open");
5295 err = open_ref_view(ref_view, s->repo);
5296 if (err) {
5297 view_close(ref_view);
5298 return err;
5300 if (view_is_parent_view(view)) {
5301 err = view_close_child(view);
5302 if (err)
5303 return err;
5304 view_set_child(view, ref_view);
5305 *focus_view = ref_view;
5306 } else
5307 *new_view = ref_view;
5308 break;
5309 case 'k':
5310 case KEY_UP:
5311 if (s->selected > 0) {
5312 s->selected--;
5313 break;
5315 tree_scroll_up(s, 1);
5316 break;
5317 case KEY_PPAGE:
5318 case CTRL('b'):
5319 if (s->tree == s->root) {
5320 if (got_object_tree_get_first_entry(s->tree) ==
5321 s->first_displayed_entry)
5322 s->selected = 0;
5323 } else {
5324 if (s->first_displayed_entry == NULL)
5325 s->selected = 0;
5327 tree_scroll_up(s, MAX(0, view->nlines - 3));
5328 break;
5329 case 'j':
5330 case KEY_DOWN:
5331 if (s->selected < s->ndisplayed - 1) {
5332 s->selected++;
5333 break;
5335 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5336 == NULL)
5337 /* can't scroll any further */
5338 break;
5339 tree_scroll_down(s, 1);
5340 break;
5341 case KEY_NPAGE:
5342 case CTRL('f'):
5343 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5344 == NULL) {
5345 /* can't scroll any further; move cursor down */
5346 if (s->selected < s->ndisplayed - 1)
5347 s->selected = s->ndisplayed - 1;
5348 break;
5350 tree_scroll_down(s, view->nlines - 3);
5351 break;
5352 case KEY_ENTER:
5353 case '\r':
5354 case KEY_BACKSPACE:
5355 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5356 struct tog_parent_tree *parent;
5357 /* user selected '..' */
5358 if (s->tree == s->root)
5359 break;
5360 parent = TAILQ_FIRST(&s->parents);
5361 TAILQ_REMOVE(&s->parents, parent,
5362 entry);
5363 got_object_tree_close(s->tree);
5364 s->tree = parent->tree;
5365 s->first_displayed_entry =
5366 parent->first_displayed_entry;
5367 s->selected_entry =
5368 parent->selected_entry;
5369 s->selected = parent->selected;
5370 free(parent);
5371 } else if (S_ISDIR(got_tree_entry_get_mode(
5372 s->selected_entry))) {
5373 struct got_tree_object *subtree;
5374 err = got_object_open_as_tree(&subtree, s->repo,
5375 got_tree_entry_get_id(s->selected_entry));
5376 if (err)
5377 break;
5378 err = tree_view_visit_subtree(s, subtree);
5379 if (err) {
5380 got_object_tree_close(subtree);
5381 break;
5383 } else if (S_ISREG(got_tree_entry_get_mode(
5384 s->selected_entry))) {
5385 struct tog_view *blame_view;
5386 int begin_x = view_is_parent_view(view) ?
5387 view_split_begin_x(view->begin_x) : 0;
5389 err = blame_tree_entry(&blame_view, begin_x,
5390 s->selected_entry, &s->parents,
5391 s->commit_id, s->repo);
5392 if (err)
5393 break;
5394 if (view_is_parent_view(view)) {
5395 err = view_close_child(view);
5396 if (err)
5397 return err;
5398 view_set_child(view, blame_view);
5399 *focus_view = blame_view;
5400 } else
5401 *new_view = blame_view;
5403 break;
5404 case KEY_RESIZE:
5405 if (s->selected > view->nlines)
5406 s->selected = s->ndisplayed - 1;
5407 break;
5408 default:
5409 break;
5412 return err;
5415 __dead static void
5416 usage_tree(void)
5418 endwin();
5419 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5420 getprogname());
5421 exit(1);
5424 static const struct got_error *
5425 cmd_tree(int argc, char *argv[])
5427 const struct got_error *error;
5428 struct got_repository *repo = NULL;
5429 struct got_worktree *worktree = NULL;
5430 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5431 struct got_object_id *commit_id = NULL;
5432 char *commit_id_arg = NULL;
5433 struct got_commit_object *commit = NULL;
5434 struct got_tree_object *tree = NULL;
5435 int ch;
5436 struct tog_view *view;
5438 #ifndef PROFILE
5439 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5440 NULL) == -1)
5441 err(1, "pledge");
5442 #endif
5444 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5445 switch (ch) {
5446 case 'c':
5447 commit_id_arg = optarg;
5448 break;
5449 case 'r':
5450 repo_path = realpath(optarg, NULL);
5451 if (repo_path == NULL)
5452 return got_error_from_errno2("realpath",
5453 optarg);
5454 break;
5455 default:
5456 usage_tree();
5457 /* NOTREACHED */
5461 argc -= optind;
5462 argv += optind;
5464 if (argc > 1)
5465 usage_tree();
5467 cwd = getcwd(NULL, 0);
5468 if (cwd == NULL)
5469 return got_error_from_errno("getcwd");
5471 error = got_worktree_open(&worktree, cwd);
5472 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5473 goto done;
5475 if (repo_path == NULL) {
5476 if (worktree)
5477 repo_path =
5478 strdup(got_worktree_get_repo_path(worktree));
5479 else
5480 repo_path = strdup(cwd);
5482 if (repo_path == NULL) {
5483 error = got_error_from_errno("strdup");
5484 goto done;
5487 error = got_repo_open(&repo, repo_path, NULL);
5488 if (error != NULL)
5489 goto done;
5491 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5492 repo, worktree);
5493 if (error)
5494 goto done;
5496 init_curses();
5498 error = apply_unveil(got_repo_get_path(repo), NULL);
5499 if (error)
5500 goto done;
5502 error = got_repo_match_object_id(&commit_id, NULL,
5503 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5504 GOT_OBJ_TYPE_COMMIT, 1, repo);
5505 if (error)
5506 goto done;
5508 error = got_object_open_as_commit(&commit, repo, commit_id);
5509 if (error)
5510 goto done;
5512 error = got_object_open_as_tree(&tree, repo,
5513 got_object_commit_get_tree_id(commit));
5514 if (error)
5515 goto done;
5517 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5518 if (view == NULL) {
5519 error = got_error_from_errno("view_open");
5520 goto done;
5522 error = open_tree_view(view, tree, commit_id, repo);
5523 if (error)
5524 goto done;
5525 if (!got_path_is_root_dir(in_repo_path)) {
5526 error = tree_view_walk_path(&view->state.tree, commit_id,
5527 in_repo_path);
5528 if (error)
5529 goto done;
5532 if (worktree) {
5533 /* Release work tree lock. */
5534 got_worktree_close(worktree);
5535 worktree = NULL;
5537 error = view_loop(view);
5538 done:
5539 free(repo_path);
5540 free(cwd);
5541 free(commit_id);
5542 if (commit)
5543 got_object_commit_close(commit);
5544 if (tree)
5545 got_object_tree_close(tree);
5546 if (repo)
5547 got_repo_close(repo);
5548 return error;
5551 static const struct got_error *
5552 ref_view_load_refs(struct tog_ref_view_state *s)
5554 const struct got_error *err;
5555 struct got_reflist_entry *sre;
5556 struct tog_reflist_entry *re;
5558 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5559 got_ref_cmp_by_name, NULL);
5560 if (err)
5561 return err;
5563 s->nrefs = 0;
5564 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5565 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5566 continue;
5568 re = malloc(sizeof(*re));
5569 if (re == NULL)
5570 return got_error_from_errno("malloc");
5572 re->ref = sre->ref;
5573 re->idx = s->nrefs++;
5574 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5577 return NULL;
5580 void
5581 ref_view_free_refs(struct tog_ref_view_state *s)
5583 struct tog_reflist_entry *re;
5585 while (!TAILQ_EMPTY(&s->refs)) {
5586 re = TAILQ_FIRST(&s->refs);
5587 TAILQ_REMOVE(&s->refs, re, entry);
5588 free(re);
5590 got_ref_list_free(&s->simplerefs);
5593 static const struct got_error *
5594 open_ref_view(struct tog_view *view, struct got_repository *repo)
5596 const struct got_error *err = NULL;
5597 struct tog_ref_view_state *s = &view->state.ref;
5599 s->selected_entry = 0;
5600 s->repo = repo;
5602 SIMPLEQ_INIT(&s->simplerefs);
5603 TAILQ_INIT(&s->refs);
5604 SIMPLEQ_INIT(&s->colors);
5606 err = ref_view_load_refs(s);
5607 if (err)
5608 return err;
5610 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5612 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5613 err = add_color(&s->colors, "^refs/heads/",
5614 TOG_COLOR_REFS_HEADS,
5615 get_color_value("TOG_COLOR_REFS_HEADS"));
5616 if (err)
5617 goto done;
5619 err = add_color(&s->colors, "^refs/tags/",
5620 TOG_COLOR_REFS_TAGS,
5621 get_color_value("TOG_COLOR_REFS_TAGS"));
5622 if (err)
5623 goto done;
5625 err = add_color(&s->colors, "^refs/remotes/",
5626 TOG_COLOR_REFS_REMOTES,
5627 get_color_value("TOG_COLOR_REFS_REMOTES"));
5628 if (err)
5629 goto done;
5632 view->show = show_ref_view;
5633 view->input = input_ref_view;
5634 view->close = close_ref_view;
5635 view->search_start = search_start_ref_view;
5636 view->search_next = search_next_ref_view;
5637 done:
5638 if (err)
5639 free_colors(&s->colors);
5640 return err;
5643 static const struct got_error *
5644 close_ref_view(struct tog_view *view)
5646 struct tog_ref_view_state *s = &view->state.ref;
5648 ref_view_free_refs(s);
5649 free_colors(&s->colors);
5651 return NULL;
5654 static const struct got_error *
5655 resolve_reflist_entry(struct got_object_id **commit_id,
5656 struct tog_reflist_entry *re, struct got_repository *repo)
5658 const struct got_error *err = NULL;
5659 struct got_object_id *obj_id;
5660 struct got_tag_object *tag = NULL;
5661 int obj_type;
5663 *commit_id = NULL;
5665 err = got_ref_resolve(&obj_id, repo, re->ref);
5666 if (err)
5667 return err;
5669 err = got_object_get_type(&obj_type, repo, obj_id);
5670 if (err)
5671 goto done;
5673 switch (obj_type) {
5674 case GOT_OBJ_TYPE_COMMIT:
5675 *commit_id = obj_id;
5676 break;
5677 case GOT_OBJ_TYPE_TAG:
5678 err = got_object_open_as_tag(&tag, repo, obj_id);
5679 if (err)
5680 goto done;
5681 free(obj_id);
5682 err = got_object_get_type(&obj_type, repo,
5683 got_object_tag_get_object_id(tag));
5684 if (err)
5685 goto done;
5686 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5687 err = got_error(GOT_ERR_OBJ_TYPE);
5688 goto done;
5690 *commit_id = got_object_id_dup(
5691 got_object_tag_get_object_id(tag));
5692 if (*commit_id == NULL) {
5693 err = got_error_from_errno("got_object_id_dup");
5694 goto done;
5696 break;
5697 default:
5698 err = got_error(GOT_ERR_OBJ_TYPE);
5699 break;
5702 done:
5703 if (tag)
5704 got_object_tag_close(tag);
5705 if (err) {
5706 free(*commit_id);
5707 *commit_id = NULL;
5709 return err;
5712 static const struct got_error *
5713 log_ref_entry(struct tog_view **new_view, int begin_x,
5714 struct tog_reflist_entry *re, struct got_repository *repo)
5716 struct tog_view *log_view;
5717 const struct got_error *err = NULL;
5718 struct got_object_id *commit_id = NULL;
5720 *new_view = NULL;
5722 err = resolve_reflist_entry(&commit_id, re, repo);
5723 if (err) {
5724 if (err->code != GOT_ERR_OBJ_TYPE)
5725 return err;
5726 else
5727 return NULL;
5730 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5731 if (log_view == NULL) {
5732 err = got_error_from_errno("view_open");
5733 goto done;
5736 err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5737 done:
5738 if (err)
5739 view_close(log_view);
5740 else
5741 *new_view = log_view;
5742 free(commit_id);
5743 return err;
5746 static void
5747 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5749 struct tog_reflist_entry *re;
5750 int i = 0;
5752 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5753 return;
5755 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5756 while (i++ < maxscroll) {
5757 if (re == NULL)
5758 break;
5759 s->first_displayed_entry = re;
5760 re = TAILQ_PREV(re, tog_reflist_head, entry);
5764 static void
5765 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5767 struct tog_reflist_entry *next, *last;
5768 int n = 0;
5770 if (s->first_displayed_entry)
5771 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5772 else
5773 next = TAILQ_FIRST(&s->refs);
5775 last = s->last_displayed_entry;
5776 while (next && last && n++ < maxscroll) {
5777 last = TAILQ_NEXT(last, entry);
5778 if (last) {
5779 s->first_displayed_entry = next;
5780 next = TAILQ_NEXT(next, entry);
5785 static const struct got_error *
5786 search_start_ref_view(struct tog_view *view)
5788 struct tog_ref_view_state *s = &view->state.ref;
5790 s->matched_entry = NULL;
5791 return NULL;
5794 static int
5795 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5797 regmatch_t regmatch;
5799 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5800 0) == 0;
5803 static const struct got_error *
5804 search_next_ref_view(struct tog_view *view)
5806 struct tog_ref_view_state *s = &view->state.ref;
5807 struct tog_reflist_entry *re = NULL;
5809 if (!view->searching) {
5810 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5811 return NULL;
5814 if (s->matched_entry) {
5815 if (view->searching == TOG_SEARCH_FORWARD) {
5816 if (s->selected_entry)
5817 re = TAILQ_NEXT(s->selected_entry, entry);
5818 else
5819 re = TAILQ_PREV(s->selected_entry,
5820 tog_reflist_head, entry);
5821 } else {
5822 if (s->selected_entry == NULL)
5823 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5824 else
5825 re = TAILQ_PREV(s->selected_entry,
5826 tog_reflist_head, entry);
5828 } else {
5829 if (view->searching == TOG_SEARCH_FORWARD)
5830 re = TAILQ_FIRST(&s->refs);
5831 else
5832 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5835 while (1) {
5836 if (re == NULL) {
5837 if (s->matched_entry == NULL) {
5838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5839 return NULL;
5841 if (view->searching == TOG_SEARCH_FORWARD)
5842 re = TAILQ_FIRST(&s->refs);
5843 else
5844 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5847 if (match_reflist_entry(re, &view->regex)) {
5848 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5849 s->matched_entry = re;
5850 break;
5853 if (view->searching == TOG_SEARCH_FORWARD)
5854 re = TAILQ_NEXT(re, entry);
5855 else
5856 re = TAILQ_PREV(re, tog_reflist_head, entry);
5859 if (s->matched_entry) {
5860 s->first_displayed_entry = s->matched_entry;
5861 s->selected = 0;
5864 return NULL;
5867 static const struct got_error *
5868 show_ref_view(struct tog_view *view)
5870 const struct got_error *err = NULL;
5871 struct tog_ref_view_state *s = &view->state.ref;
5872 struct tog_reflist_entry *re;
5873 char *line = NULL;
5874 wchar_t *wline;
5875 struct tog_color *tc;
5876 int width, n;
5877 int limit = view->nlines;
5879 werase(view->window);
5881 s->ndisplayed = 0;
5883 if (limit == 0)
5884 return NULL;
5886 re = s->first_displayed_entry;
5888 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5889 s->nrefs) == -1)
5890 return got_error_from_errno("asprintf");
5892 err = format_line(&wline, &width, line, view->ncols, 0);
5893 if (err) {
5894 free(line);
5895 return err;
5897 if (view_needs_focus_indication(view))
5898 wstandout(view->window);
5899 waddwstr(view->window, wline);
5900 if (view_needs_focus_indication(view))
5901 wstandend(view->window);
5902 free(wline);
5903 wline = NULL;
5904 free(line);
5905 line = NULL;
5906 if (width < view->ncols - 1)
5907 waddch(view->window, '\n');
5908 if (--limit <= 0)
5909 return NULL;
5911 n = 0;
5912 while (re && limit > 0) {
5913 char *line = NULL;
5915 if (got_ref_is_symbolic(re->ref)) {
5916 if (asprintf(&line, "%s -> %s",
5917 got_ref_get_name(re->ref),
5918 got_ref_get_symref_target(re->ref)) == -1)
5919 return got_error_from_errno("asprintf");
5920 } else if (s->show_ids) {
5921 struct got_object_id *id;
5922 char *id_str;
5923 err = got_ref_resolve(&id, s->repo, re->ref);
5924 if (err)
5925 return err;
5926 err = got_object_id_str(&id_str, id);
5927 if (err) {
5928 free(id);
5929 return err;
5931 if (asprintf(&line, "%s: %s",
5932 got_ref_get_name(re->ref), id_str) == -1) {
5933 err = got_error_from_errno("asprintf");
5934 free(id);
5935 free(id_str);
5936 return err;
5938 free(id);
5939 free(id_str);
5940 } else {
5941 line = strdup(got_ref_get_name(re->ref));
5942 if (line == NULL)
5943 return got_error_from_errno("strdup");
5946 err = format_line(&wline, &width, line, view->ncols, 0);
5947 if (err) {
5948 free(line);
5949 return err;
5951 if (n == s->selected) {
5952 if (view->focussed)
5953 wstandout(view->window);
5954 s->selected_entry = re;
5956 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5957 if (tc)
5958 wattr_on(view->window,
5959 COLOR_PAIR(tc->colorpair), NULL);
5960 waddwstr(view->window, wline);
5961 if (tc)
5962 wattr_off(view->window,
5963 COLOR_PAIR(tc->colorpair), NULL);
5964 if (width < view->ncols - 1)
5965 waddch(view->window, '\n');
5966 if (n == s->selected && view->focussed)
5967 wstandend(view->window);
5968 free(line);
5969 free(wline);
5970 wline = NULL;
5971 n++;
5972 s->ndisplayed++;
5973 s->last_displayed_entry = re;
5975 limit--;
5976 re = TAILQ_NEXT(re, entry);
5979 view_vborder(view);
5980 return err;
5983 static const struct got_error *
5984 browse_ref_tree(struct tog_view **new_view, int begin_x,
5985 struct tog_reflist_entry *re, struct got_repository *repo)
5987 const struct got_error *err = NULL;
5988 struct got_object_id *commit_id = NULL, *tree_id = NULL;
5989 struct got_tree_object *tree = NULL;
5990 struct tog_view *tree_view;
5992 *new_view = NULL;
5994 err = resolve_reflist_entry(&commit_id, re, repo);
5995 if (err) {
5996 if (err->code != GOT_ERR_OBJ_TYPE)
5997 return err;
5998 else
5999 return NULL;
6002 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6003 if (err)
6004 goto done;
6006 err = got_object_open_as_tree(&tree, repo, tree_id);
6007 if (err)
6008 goto done;
6010 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6011 if (tree_view == NULL) {
6012 err = got_error_from_errno("view_open");
6013 goto done;
6016 err = open_tree_view(tree_view, tree, commit_id, repo);
6017 if (err)
6018 goto done;
6020 *new_view = tree_view;
6021 done:
6022 free(commit_id);
6023 free(tree_id);
6024 if (err) {
6025 if (tree)
6026 got_object_tree_close(tree);
6028 return err;
6030 static const struct got_error *
6031 input_ref_view(struct tog_view **new_view, struct tog_view **focus_view,
6032 struct tog_view *view, int ch)
6034 const struct got_error *err = NULL;
6035 struct tog_ref_view_state *s = &view->state.ref;
6036 struct tog_view *log_view, *tree_view;
6037 int begin_x = 0;
6039 switch (ch) {
6040 case 'i':
6041 s->show_ids = !s->show_ids;
6042 break;
6043 case KEY_ENTER:
6044 case '\r':
6045 if (!s->selected_entry)
6046 break;
6047 if (view_is_parent_view(view))
6048 begin_x = view_split_begin_x(view->begin_x);
6049 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6050 s->repo);
6051 if (view_is_parent_view(view)) {
6052 err = view_close_child(view);
6053 if (err)
6054 return err;
6055 view_set_child(view, log_view);
6056 *focus_view = log_view;
6057 } else
6058 *new_view = log_view;
6059 break;
6060 case 't':
6061 if (!s->selected_entry)
6062 break;
6063 if (view_is_parent_view(view))
6064 begin_x = view_split_begin_x(view->begin_x);
6065 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6066 s->repo);
6067 if (err || tree_view == NULL)
6068 break;
6069 if (view_is_parent_view(view)) {
6070 err = view_close_child(view);
6071 if (err)
6072 return err;
6073 view_set_child(view, tree_view);
6074 *focus_view = tree_view;
6075 } else
6076 *new_view = tree_view;
6077 break;
6078 case 'k':
6079 case KEY_UP:
6080 if (s->selected > 0) {
6081 s->selected--;
6082 break;
6084 ref_scroll_up(s, 1);
6085 break;
6086 case KEY_PPAGE:
6087 case CTRL('b'):
6088 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6089 s->selected = 0;
6090 ref_scroll_up(s, MAX(0, view->nlines - 1));
6091 break;
6092 case 'j':
6093 case KEY_DOWN:
6094 if (s->selected < s->ndisplayed - 1) {
6095 s->selected++;
6096 break;
6098 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6099 /* can't scroll any further */
6100 break;
6101 ref_scroll_down(s, 1);
6102 break;
6103 case KEY_NPAGE:
6104 case CTRL('f'):
6105 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6106 /* can't scroll any further; move cursor down */
6107 if (s->selected < s->ndisplayed - 1)
6108 s->selected = s->ndisplayed - 1;
6109 break;
6111 ref_scroll_down(s, view->nlines - 1);
6112 break;
6113 case CTRL('l'):
6114 ref_view_free_refs(s);
6115 err = ref_view_load_refs(s);
6116 break;
6117 case KEY_RESIZE:
6118 if (s->selected > view->nlines)
6119 s->selected = s->ndisplayed - 1;
6120 break;
6121 default:
6122 break;
6125 return err;
6128 __dead static void
6129 usage_ref(void)
6131 endwin();
6132 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6133 getprogname());
6134 exit(1);
6137 static const struct got_error *
6138 cmd_ref(int argc, char *argv[])
6140 const struct got_error *error;
6141 struct got_repository *repo = NULL;
6142 struct got_worktree *worktree = NULL;
6143 char *cwd = NULL, *repo_path = NULL;
6144 int ch;
6145 struct tog_view *view;
6147 #ifndef PROFILE
6148 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6149 NULL) == -1)
6150 err(1, "pledge");
6151 #endif
6153 while ((ch = getopt(argc, argv, "r:")) != -1) {
6154 switch (ch) {
6155 case 'r':
6156 repo_path = realpath(optarg, NULL);
6157 if (repo_path == NULL)
6158 return got_error_from_errno2("realpath",
6159 optarg);
6160 break;
6161 default:
6162 usage_ref();
6163 /* NOTREACHED */
6167 argc -= optind;
6168 argv += optind;
6170 if (argc > 1)
6171 usage_ref();
6173 cwd = getcwd(NULL, 0);
6174 if (cwd == NULL)
6175 return got_error_from_errno("getcwd");
6177 error = got_worktree_open(&worktree, cwd);
6178 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6179 goto done;
6181 if (repo_path == NULL) {
6182 if (worktree)
6183 repo_path =
6184 strdup(got_worktree_get_repo_path(worktree));
6185 else
6186 repo_path = strdup(cwd);
6188 if (repo_path == NULL) {
6189 error = got_error_from_errno("strdup");
6190 goto done;
6193 error = got_repo_open(&repo, repo_path, NULL);
6194 if (error != NULL)
6195 goto done;
6197 init_curses();
6199 error = apply_unveil(got_repo_get_path(repo), NULL);
6200 if (error)
6201 goto done;
6203 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6204 if (view == NULL) {
6205 error = got_error_from_errno("view_open");
6206 goto done;
6209 error = open_ref_view(view, repo);
6210 if (error)
6211 goto done;
6213 if (worktree) {
6214 /* Release work tree lock. */
6215 got_worktree_close(worktree);
6216 worktree = NULL;
6218 error = view_loop(view);
6219 done:
6220 free(repo_path);
6221 free(cwd);
6222 if (repo)
6223 got_repo_close(repo);
6224 return error;
6227 static void
6228 list_commands(FILE *fp)
6230 int i;
6232 fprintf(fp, "commands:");
6233 for (i = 0; i < nitems(tog_commands); i++) {
6234 struct tog_cmd *cmd = &tog_commands[i];
6235 fprintf(fp, " %s", cmd->name);
6237 fputc('\n', fp);
6240 __dead static void
6241 usage(int hflag, int status)
6243 FILE *fp = (status == 0) ? stdout : stderr;
6245 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6246 getprogname());
6247 if (hflag) {
6248 fprintf(fp, "lazy usage: %s path\n", getprogname());
6249 list_commands(fp);
6251 exit(status);
6254 static char **
6255 make_argv(int argc, ...)
6257 va_list ap;
6258 char **argv;
6259 int i;
6261 va_start(ap, argc);
6263 argv = calloc(argc, sizeof(char *));
6264 if (argv == NULL)
6265 err(1, "calloc");
6266 for (i = 0; i < argc; i++) {
6267 argv[i] = strdup(va_arg(ap, char *));
6268 if (argv[i] == NULL)
6269 err(1, "strdup");
6272 va_end(ap);
6273 return argv;
6277 * Try to convert 'tog path' into a 'tog log path' command.
6278 * The user could simply have mistyped the command rather than knowingly
6279 * provided a path. So check whether argv[0] can in fact be resolved
6280 * to a path in the HEAD commit and print a special error if not.
6281 * This hack is for mpi@ <3
6283 static const struct got_error *
6284 tog_log_with_path(int argc, char *argv[])
6286 const struct got_error *error = NULL;
6287 struct tog_cmd *cmd = NULL;
6288 struct got_repository *repo = NULL;
6289 struct got_worktree *worktree = NULL;
6290 struct got_object_id *commit_id = NULL, *id = NULL;
6291 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6292 char *commit_id_str = NULL, **cmd_argv = NULL;
6294 cwd = getcwd(NULL, 0);
6295 if (cwd == NULL)
6296 return got_error_from_errno("getcwd");
6298 error = got_worktree_open(&worktree, cwd);
6299 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6300 goto done;
6302 if (worktree)
6303 repo_path = strdup(got_worktree_get_repo_path(worktree));
6304 else
6305 repo_path = strdup(cwd);
6306 if (repo_path == NULL) {
6307 error = got_error_from_errno("strdup");
6308 goto done;
6311 error = got_repo_open(&repo, repo_path, NULL);
6312 if (error != NULL)
6313 goto done;
6315 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6316 repo, worktree);
6317 if (error)
6318 goto done;
6320 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6321 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6322 GOT_OBJ_TYPE_COMMIT, 1, repo);
6323 if (error)
6324 goto done;
6326 if (worktree) {
6327 got_worktree_close(worktree);
6328 worktree = NULL;
6331 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6332 if (error) {
6333 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6334 goto done;
6335 fprintf(stderr, "%s: '%s' is no known command or path\n",
6336 getprogname(), argv[0]);
6337 usage(1, 1);
6338 /* not reached */
6341 got_repo_close(repo);
6342 repo = NULL;
6344 error = got_object_id_str(&commit_id_str, commit_id);
6345 if (error)
6346 goto done;
6348 cmd = &tog_commands[0]; /* log */
6349 argc = 4;
6350 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6351 error = cmd->cmd_main(argc, cmd_argv);
6352 done:
6353 if (repo)
6354 got_repo_close(repo);
6355 if (worktree)
6356 got_worktree_close(worktree);
6357 free(id);
6358 free(commit_id_str);
6359 free(commit_id);
6360 free(cwd);
6361 free(repo_path);
6362 free(in_repo_path);
6363 if (cmd_argv) {
6364 int i;
6365 for (i = 0; i < argc; i++)
6366 free(cmd_argv[i]);
6367 free(cmd_argv);
6369 return error;
6372 int
6373 main(int argc, char *argv[])
6375 const struct got_error *error = NULL;
6376 struct tog_cmd *cmd = NULL;
6377 int ch, hflag = 0, Vflag = 0;
6378 char **cmd_argv = NULL;
6379 static struct option longopts[] = {
6380 { "version", no_argument, NULL, 'V' },
6381 { NULL, 0, NULL, 0}
6384 setlocale(LC_CTYPE, "");
6386 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6387 switch (ch) {
6388 case 'h':
6389 hflag = 1;
6390 break;
6391 case 'V':
6392 Vflag = 1;
6393 break;
6394 default:
6395 usage(hflag, 1);
6396 /* NOTREACHED */
6400 argc -= optind;
6401 argv += optind;
6402 optind = 1;
6403 optreset = 1;
6405 if (Vflag) {
6406 got_version_print_str();
6407 return 0;
6410 if (argc == 0) {
6411 if (hflag)
6412 usage(hflag, 0);
6413 /* Build an argument vector which runs a default command. */
6414 cmd = &tog_commands[0];
6415 argc = 1;
6416 cmd_argv = make_argv(argc, cmd->name);
6417 } else {
6418 int i;
6420 /* Did the user specify a command? */
6421 for (i = 0; i < nitems(tog_commands); i++) {
6422 if (strncmp(tog_commands[i].name, argv[0],
6423 strlen(argv[0])) == 0) {
6424 cmd = &tog_commands[i];
6425 break;
6430 if (cmd == NULL) {
6431 if (argc != 1)
6432 usage(0, 1);
6433 /* No command specified; try log with a path */
6434 error = tog_log_with_path(argc, argv);
6435 } else {
6436 if (hflag)
6437 cmd->cmd_usage();
6438 else
6439 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6442 endwin();
6443 putchar('\n');
6444 if (cmd_argv) {
6445 int i;
6446 for (i = 0; i < argc; i++)
6447 free(cmd_argv[i]);
6448 free(cmd_argv);
6451 if (error && error->code != GOT_ERR_CANCELLED)
6452 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6453 return 0;